source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: kristaps@mdocml.bsd.lv
To: source@mdocml.bsd.lv
Subject: mdocml: Clean-up in mdoc_argv.c: make CPP-defines into an enum,
Date: Thu, 17 Mar 2011 06:59:28 -0400 (EDT)	[thread overview]
Message-ID: <201103171059.p2HAxSE0003029@krisdoz.my.domain> (raw)

Log Message:
-----------
Clean-up in mdoc_argv.c: make CPP-defines into an enum, document, make
static arrays const, properly abort() for bogus switch cases, and be
obsessive about spacing and (void)-casting.

Modified Files:
--------------
    mdocml:
        mdoc_argv.c

Revision Data
-------------
Index: mdoc_argv.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mdoc_argv.c,v
retrieving revision 1.67
retrieving revision 1.68
diff -Lmdoc_argv.c -Lmdoc_argv.c -u -p -r1.67 -r1.68
--- mdoc_argv.c
+++ mdoc_argv.c
@@ -30,20 +30,7 @@
 #include "libmdoc.h"
 #include "libmandoc.h"
 
-/*
- * Routines to parse arguments of macros.  Arguments follow the syntax
- * of `-arg [val [valN...]]'.  Arguments come in all types:  quoted
- * arguments, multiple arguments per value, no-value arguments, etc.
- *
- * There's no limit to the number or arguments that may be allocated.
- */
-
-#define	ARGV_NONE	(1 << 0)
-#define	ARGV_SINGLE	(1 << 1)
-#define	ARGV_MULTI	(1 << 2)
-#define	ARGV_OPT_SINGLE	(1 << 3)
-
-#define	MULTI_STEP	 5
+#define	MULTI_STEP	 5 /* pre-allocate argument values */
 
 static	enum mdocargt	 argv_a2arg(enum mdoct, const char *);
 static	enum margserr	 args(struct mdoc *, int, int *, 
@@ -58,9 +45,14 @@ static	int		 argv_opt_single(struct mdoc
 static	int		 argv_multi(struct mdoc *, int, 
 				struct mdoc_argv *, int *, char *);
 
-/* Per-argument flags. */
+enum	argvflag {
+	ARGV_NONE, /* no args to flag (e.g., -split) */
+	ARGV_SINGLE, /* one arg to flag (e.g., -file xxx)  */
+	ARGV_MULTI, /* multiple args (e.g., -column xxx yyy) */
+	ARGV_OPT_SINGLE /* optional arg (e.g., -offset [xxx]) */
+};
 
-static	int mdoc_argvflags[MDOC_ARG_MAX] = {
+static	const enum argvflag argvflags[MDOC_ARG_MAX] = {
 	ARGV_NONE,	/* MDOC_Split */
 	ARGV_NONE,	/* MDOC_Nosplit */
 	ARGV_NONE,	/* MDOC_Ragged */
@@ -89,7 +81,7 @@ static	int mdoc_argvflags[MDOC_ARG_MAX] 
 	ARGV_NONE	/* MDOC_Symbolic */
 };
 
-static	int mdoc_argflags[MDOC_MAX] = {
+static	const int argflags[MDOC_MAX] = {
 	0, /* Ap */
 	0, /* Dd */
 	0, /* Dt */
@@ -214,7 +206,6 @@ static	int mdoc_argflags[MDOC_MAX] = {
 	0, /* Ta */
 };
 
-
 /*
  * Parse an argument from line text.  This comes in the form of -key
  * [value0...], which may either have a single mandatory value, at least
@@ -255,7 +246,7 @@ mdoc_argv(struct mdoc *m, int line, enum
 		buf[(*pos)++] = '\0';
 	}
 
-	(void)memset(&tmp, 0, sizeof(struct mdoc_argv));
+	memset(&tmp, 0, sizeof(struct mdoc_argv));
 	tmp.line = line;
 	tmp.pos = *pos;
 
@@ -281,13 +272,12 @@ mdoc_argv(struct mdoc *m, int line, enum
 	arg->argv = mandoc_realloc
 		(arg->argv, arg->argc * sizeof(struct mdoc_argv));
 
-	(void)memcpy(&arg->argv[(int)arg->argc - 1], 
+	memcpy(&arg->argv[(int)arg->argc - 1], 
 			&tmp, sizeof(struct mdoc_argv));
 
 	return(ARGV_ARG);
 }
 
-
 void
 mdoc_argv_free(struct mdoc_arg *p)
 {
@@ -310,7 +300,6 @@ mdoc_argv_free(struct mdoc_arg *p)
 	free(p);
 }
 
-
 void
 mdoc_argn_free(struct mdoc_arg *p, int iarg)
 {
@@ -329,7 +318,6 @@ mdoc_argn_free(struct mdoc_arg *p, int i
 		p->argv[iarg] = p->argv[iarg+1];
 }
 
-
 enum margserr
 mdoc_zargs(struct mdoc *m, int line, int *pos, 
 		char *buf, int flags, char **v)
@@ -338,7 +326,6 @@ mdoc_zargs(struct mdoc *m, int line, int
 	return(args(m, line, pos, buf, flags, v));
 }
 
-
 enum margserr
 mdoc_args(struct mdoc *m, int line, int *pos, 
 		char *buf, enum mdoct tok, char **v)
@@ -346,7 +333,7 @@ mdoc_args(struct mdoc *m, int line, int 
 	int		  fl;
 	struct mdoc_node *n;
 
-	fl = mdoc_argflags[tok];
+	fl = argflags[tok];
 
 	if (MDOC_It != tok)
 		return(args(m, line, pos, buf, fl, v));
@@ -370,7 +357,6 @@ mdoc_args(struct mdoc *m, int line, int 
 	return(args(m, line, pos, buf, fl, v));
 }
 
-
 static enum margserr
 args(struct mdoc *m, int line, int *pos, 
 		char *buf, int fl, char **v)
@@ -677,7 +663,6 @@ argv_a2arg(enum mdoct tok, const char *p
 	return(MDOC_ARG_MAX);
 }
 
-
 static int
 argv_multi(struct mdoc *m, int line, 
 		struct mdoc_argv *v, int *pos, char *buf)
@@ -704,7 +689,6 @@ argv_multi(struct mdoc *m, int line, 
 	return(1);
 }
 
-
 static int
 argv_opt_single(struct mdoc *m, int line, 
 		struct mdoc_argv *v, int *pos, char *buf)
@@ -728,7 +712,6 @@ argv_opt_single(struct mdoc *m, int line
 	return(1);
 }
 
-
 /*
  * Parse a single, mandatory value from the stream.
  */
@@ -756,7 +739,6 @@ argv_single(struct mdoc *m, int line, 
 	return(1);
 }
 
-
 /*
  * Determine rules for parsing arguments.  Arguments can either accept
  * no parameters, an optional single parameter, one parameter, or
@@ -770,16 +752,18 @@ argv(struct mdoc *mdoc, int line, 
 	v->sz = 0;
 	v->value = NULL;
 
-	switch (mdoc_argvflags[v->arg]) {
+	switch (argvflags[v->arg]) {
 	case (ARGV_SINGLE):
 		return(argv_single(mdoc, line, v, pos, buf));
 	case (ARGV_MULTI):
 		return(argv_multi(mdoc, line, v, pos, buf));
 	case (ARGV_OPT_SINGLE):
 		return(argv_opt_single(mdoc, line, v, pos, buf));
-	default:
-		/* ARGV_NONE */
+	case (ARGV_NONE):
 		break;
+	default:
+		abort();
+		/* NOTREACHED */
 	}
 
 	return(1);
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

                 reply	other threads:[~2011-03-17 10:59 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201103171059.p2HAxSE0003029@krisdoz.my.domain \
    --to=kristaps@mdocml.bsd.lv \
    --cc=source@mdocml.bsd.lv \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).