source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mandoc: Structural cleanup, no functional change: Mixing parser and
@ 2019-07-26 21:03 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2019-07-26 21:03 UTC (permalink / raw)
  To: source

Log Message:
-----------
Structural cleanup, no functional change:
Mixing parser and formatter state in the same struct was a bad idea,
so pull the parser state and configuration out of it.
This makes sure output options are not passed into parser functions
and parser options are not passed into output functions.
While here, add comments to the important local variables in main().

Modified Files:
--------------
    mandoc:
        main.c

Revision Data
-------------
Index: main.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/main.c,v
retrieving revision 1.333
retrieving revision 1.334
diff -Lmain.c -Lmain.c -u -p -r1.333 -r1.334
--- main.c
+++ main.c
@@ -78,12 +78,9 @@ enum	outt {
 };
 
 struct	curparse {
-	struct mparse	 *mp;
 	struct manoutput *outopts;	/* output options */
 	void		 *outdata;	/* data for output */
-	char		 *os_s;		/* operating system for display */
 	int		  wstop;	/* stop after a file with a warning */
-	enum mandoc_os	  os_e;		/* check base system conventions */
 	enum outt	  outtype;	/* which output to use */
 };
 
@@ -99,12 +96,13 @@ static	int		  fs_search(const struct man
 				const struct manpaths *, int, char**,
 				struct manpage **, size_t *);
 static	void		  outdata_alloc(struct curparse *);
-static	void		  parse(struct curparse *, int, const char *);
+static	void		  parse(struct mparse *, int, const char *,
+				struct curparse *);
 static	void		  passthrough(int, int);
 static	void		  run_pager(struct tag_files *);
 static	pid_t		  spawn_pager(struct tag_files *);
 static	void		  usage(enum argmode) __attribute__((__noreturn__));
-static	int		  woptions(struct curparse *, char *);
+static	int		  woptions(char *, enum mandoc_os *, int *);
 
 static	const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
 static	char		  help_arg[] = "help";
@@ -114,24 +112,31 @@ static	char		 *help_argv[] = {help_arg, 
 int
 main(int argc, char *argv[])
 {
-	struct manconf	 conf;
-	struct mansearch search;
+	struct manconf	 conf;		/* Manpaths and output options. */
 	struct curparse	 curp;
-	struct winsize	 ws;
-	struct tag_files *tag_files;
-	struct manpage	*res, *resp;
+	struct winsize	 ws;		/* Result of ioctl(TIOCGWINSZ). */
+	struct mansearch search;	/* Search options. */
+	struct manpage	*res, *resp;	/* Search results. */
+	struct mparse	*mp;		/* Opaque parser object. */
+	struct tag_files *tag_files;	/* Tagging state variables. */
+	const char	*conf_file;	/* -C: alternate config file. */
+	const char	*os_s;		/* -I: Operating system for display. */
 	const char	*progname, *sec, *thisarg;
-	char		*conf_file, *defpaths, *auxpaths;
-	char		*oarg, *tagarg;
+	char		*defpaths;	/* -M: override manpaths. */
+	char		*auxpaths;	/* -m: additional manpaths. */
+	char		*oarg;		/* -O: output option string. */
+	char		*tagarg;	/* -O tag: default value. */
 	unsigned char	*uc;
-	size_t		 i, sz, ssz;
+	size_t		 sz;		/* Number of elements in res[]. */
+	size_t		 i, ssz;
+	int		 options;	/* Parser options. */
+	int		 show_usage;	/* Invalid argument: give up. */
+	int		 use_pager;	/* According to command line. */
 	int		 prio, best_prio;
-	enum outmode	 outmode;
 	int		 fd, startdir;
-	int		 show_usage;
-	int		 options;
-	int		 use_pager;
 	int		 c;
+	enum mandoc_os	 os_e;		/* Check base system conventions. */
+	enum outmode	 outmode;	/* According to command line. */
 
 #if HAVE_PROGNAME
 	progname = getprogname();
@@ -164,8 +169,8 @@ main(int argc, char *argv[])
 	/* Search options. */
 
 	memset(&conf, 0, sizeof(conf));
-	conf_file = defpaths = NULL;
-	auxpaths = NULL;
+	conf_file = NULL;
+	defpaths = auxpaths = NULL;
 
 	memset(&search, 0, sizeof(struct mansearch));
 	search.outkey = "Nd";
@@ -184,6 +189,8 @@ main(int argc, char *argv[])
 
 	/* Parser and formatter options. */
 
+	os_e = MANDOC_OS_OTHER;
+	os_s = NULL;
 	memset(&curp, 0, sizeof(struct curparse));
 	curp.outtype = OUTT_LOCALE;
 	curp.outopts = &conf.output;
@@ -224,12 +231,12 @@ main(int argc, char *argv[])
 				    "-I %s", optarg);
 				return mandoc_msg_getrc();
 			}
-			if (curp.os_s != NULL) {
+			if (os_s != NULL) {
 				mandoc_msg(MANDOCERR_BADARG_DUPE, 0, 0,
 				    "-I %s", optarg);
 				return mandoc_msg_getrc();
 			}
-			curp.os_s = mandoc_strdup(optarg + 3);
+			os_s = optarg + 3;
 			break;
 		case 'K':
 			options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
@@ -295,7 +302,7 @@ main(int argc, char *argv[])
 			}
 			break;
 		case 'W':
-			if (woptions(&curp, optarg) == -1)
+			if (woptions(optarg, &os_e, &curp.wstop) == -1)
 				return mandoc_msg_getrc();
 			break;
 		case 'w':
@@ -548,7 +555,7 @@ main(int argc, char *argv[])
 	}
 
 	mchars_alloc();
-	curp.mp = mparse_alloc(options, curp.os_e, curp.os_s);
+	mp = mparse_alloc(options, os_e, os_s);
 
 	if (argc < 1) {
 		if (use_pager) {
@@ -558,7 +565,7 @@ main(int argc, char *argv[])
 		}
 		thisarg = "<stdin>";
 		mandoc_msg_setinfilename(thisarg);
-		parse(&curp, STDIN_FILENO, thisarg);
+		parse(mp, STDIN_FILENO, thisarg, &curp);
 		mandoc_msg_setinfilename(NULL);
 	}
 
@@ -588,7 +595,7 @@ main(int argc, char *argv[])
 			thisarg = *argv;
 
 		mandoc_msg_setinfilename(thisarg);
-		fd = mparse_open(curp.mp, thisarg);
+		fd = mparse_open(mp, thisarg);
 		if (fd != -1) {
 			if (use_pager) {
 				use_pager = 0;
@@ -598,7 +605,7 @@ main(int argc, char *argv[])
 			}
 
 			if (resp == NULL || resp->form == FORM_SRC)
-				parse(&curp, fd, thisarg);
+				parse(mp, fd, thisarg, &curp);
 			else
 				passthrough(fd, conf.output.synopsisonly);
 
@@ -634,7 +641,7 @@ main(int argc, char *argv[])
 		else
 			argv++;
 		if (--argc)
-			mparse_reset(curp.mp);
+			mparse_reset(mp);
 	}
 	if (startdir != -1) {
 		(void)fchdir(startdir);
@@ -660,7 +667,7 @@ main(int argc, char *argv[])
 		}
 	}
 	mandoc_xr_free();
-	mparse_free(curp.mp);
+	mparse_free(mp);
 	mchars_free();
 
 out:
@@ -668,7 +675,6 @@ out:
 		manconf_free(&conf);
 		mansearch_free(res, sz);
 	}
-	free(curp.os_s);
 
 	if (tag_files != NULL) {
 		fclose(stdout);
@@ -836,7 +842,7 @@ fs_search(const struct mansearch *cfg, c
 }
 
 static void
-parse(struct curparse *curp, int fd, const char *file)
+parse(struct mparse *mp, int fd, const char *file, struct curparse *curp)
 {
 	struct roff_meta *meta;
 
@@ -845,7 +851,7 @@ parse(struct curparse *curp, int fd, con
 	assert(file);
 	assert(fd >= 0);
 
-	mparse_readfd(curp->mp, fd, file);
+	mparse_readfd(mp, fd, file);
 	if (fd != STDIN_FILENO)
 		close(fd);
 
@@ -863,7 +869,7 @@ parse(struct curparse *curp, int fd, con
 		html_reset(curp);
 
 	mandoc_xr_reset();
-	meta = mparse_result(curp->mp);
+	meta = mparse_result(mp);
 
 	/* Execute the out device, if it exists. */
 
@@ -901,7 +907,7 @@ parse(struct curparse *curp, int fd, con
 			tree_man(curp->outdata, meta);
 			break;
 		case OUTT_MAN:
-			mparse_copy(curp->mp);
+			mparse_copy(mp);
 			break;
 		case OUTT_PDF:
 		case OUTT_ASCII:
@@ -1041,7 +1047,7 @@ done:
 }
 
 static int
-woptions(struct curparse *curp, char *arg)
+woptions(char *arg, enum mandoc_os *os_e, int *wstop)
 {
 	char		*v, *o;
 	const char	*toks[11];
@@ -1062,7 +1068,7 @@ woptions(struct curparse *curp, char *ar
 		o = arg;
 		switch (getsubopt(&arg, (char * const *)toks, &v)) {
 		case 0:
-			curp->wstop = 1;
+			*wstop = 1;
 			break;
 		case 1:
 		case 2:
@@ -1085,11 +1091,11 @@ woptions(struct curparse *curp, char *ar
 			break;
 		case 8:
 			mandoc_msg_setmin(MANDOCERR_BASE);
-			curp->os_e = MANDOC_OS_OPENBSD;
+			*os_e = MANDOC_OS_OPENBSD;
 			break;
 		case 9:
 			mandoc_msg_setmin(MANDOCERR_BASE);
-			curp->os_e = MANDOC_OS_NETBSD;
+			*os_e = MANDOC_OS_NETBSD;
 			break;
 		default:
 			mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0, "-W %s", o);
--
 To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-07-26 21:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-26 21:03 mandoc: Structural cleanup, no functional change: Mixing parser and schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).