DragonFly BSD
DragonFly submit List (threaded) for 2005-02
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

Re: checknr WARNS6 cleanup


From: Chris Pressey <cpressey@xxxxxxxxxxxxxxx>
Date: Tue, 1 Feb 2005 22:08:46 -0800

On Sun, 16 Jan 2005 16:38:50 -0500
Larry Lansing <lansil@xxxxxxxxxxxxx> wrote:

> Makes checknr WARNS6-friendly.
> 
> - add WARNS6 to Makefile
> - constify several char * function arguments
> - reindent function declarations
> - rename some local variables to avoid name conflicts with globals
> - use a temporary char * for some strncpy() calls for
> const-correctness - eliminate use of the register keyword

Sorry to drop the ball on these cleanups - I've been busy.

Attached is a refined version of this patch.  I'm not really happy about
the malloc()s, but the other alternative is to copy all of br[] and
knowncmds[] into writeable buffers, which isn't very appetizing either.

Thoughts, anyone?

-Chris
Index: usr.bin/checknr/Makefile
===================================================================
RCS file: /home/dcvs/src/usr.bin/checknr/Makefile,v
retrieving revision 1.1
diff -u -r1.1 Makefile
--- usr.bin/checknr/Makefile	17 Jun 2003 02:56:01 -0000	1.1
+++ usr.bin/checknr/Makefile	19 Jan 2005 22:45:17 -0000
@@ -1,6 +1,7 @@
 #	@(#)Makefile	8.1 (Berkeley) 6/6/93
 
 PROG=	checknr
+WARNS?=	6
 CFLAGS+=-Wall
 
 .include <bsd.prog.mk>
Index: usr.bin/checknr/checknr.c
===================================================================
RCS file: /home/dcvs/src/usr.bin/checknr/checknr.c,v
retrieving revision 1.4
diff -u -r1.4 checknr.c
--- usr.bin/checknr/checknr.c	3 Nov 2003 19:31:28 -0000	1.4
+++ usr.bin/checknr/checknr.c	19 Jan 2005 23:09:53 -0000
@@ -52,18 +52,18 @@
 #define MAXBR	100	/* Max number of bracket pairs known */
 #define MAXCMDS	500	/* Max number of commands known */
 
-void addcmd(char *);
-void addmac(char *);
-int binsrch(char *);
-void checkknown(char *);
-void chkcmd(char *, char *);
-void complain(int);
-int eq(char *, char *);
-void nomatch(char *);
-void pe(int);
-void process(FILE *);
-void prop(int);
-static void usage(void);
+static void	addcmd(char *);
+static void	addmac(const char *);
+static int	binsrch(const char *);
+static void	checkknown(const char *);
+static void	chkcmd(const char *);
+static void	complain(int);
+static int	eq(const char *, const char *);
+static void	nomatch(const char *);
+static void	pe(int);
+static void	process(FILE *);
+static void	prop(int);
+static void	usage(void);
 
 /*
  * The stack on which we remember what we've seen so far.
@@ -80,8 +80,8 @@
  * The kinds of opening and closing brackets.
  */
 struct brstr {
-	char *opbr;
-	char *clbr;
+	const char *opbr;
+	const char *clbr;
 } br[MAXBR] = {
 	/* A few bare bones troff commands */
 #define SZ	0
@@ -138,7 +138,7 @@
  * All commands known to nroff, plus macro packages.
  * Used so we can complain about unrecognized commands.
  */
-char *knowncmds[MAXCMDS] = {
+const char *knowncmds[MAXCMDS] = {
 "$c", "$f", "$h", "$p", "$s", "(b", "(c", "(d", "(f", "(l", "(q", "(t",
 "(x", "(z", ")b", ")c", ")d", ")f", ")l", ")q", ")t", ")x", ")z", "++",
 "+c", "1C", "1c", "2C", "2c", "@(", "@)", "@C", "@D", "@F", "@I", "@M",
@@ -174,7 +174,7 @@
 
 int	lineno;		/* current line number in input file */
 char	line[256];	/* the current line */
-char	*cfilename;	/* name of current file */
+const char *cfilename;	/* name of current file */
 int	nfiles;		/* number of files to process */
 int	fflag;		/* -f: ignore \f */
 int	sflag;		/* -s: ignore \s */
@@ -186,7 +186,7 @@
 {
 	FILE *f;
 	int i;
-	char *cp;
+	char *cp, *tmp;
 	char b1[4];
 
 	/* Figure out how many known commands there are */
@@ -204,10 +204,14 @@
 			for (i=0; br[i].opbr; i++)
 				;
 			for (cp=argv[1]+3; cp[-1]; cp += 6) {
-				br[i].opbr = malloc(3);
-				strncpy(br[i].opbr, cp, 2);
-				br[i].clbr = malloc(3);
-				strncpy(br[i].clbr, cp+3, 2);
+				tmp = malloc(3);
+				strncpy(tmp, cp, 2);
+				br[i].opbr = tmp;
+
+				tmp = malloc(3);
+				strncpy(tmp, cp + 3, 2);
+				br[i].clbr = tmp;
+
 				addmac(br[i].opbr);	/* knows pairs are also known cmds */
 				addmac(br[i].clbr);
 				i++;
@@ -269,10 +273,10 @@
 	exit(1);
 }
 
-void
+static void
 process(FILE *f)
 {
-	register int i, n;
+	int i, n;
 	char mac[5];	/* The current macro or nroff command */
 	int pl;
 
@@ -306,7 +310,7 @@
 			if (eq(mac, "de"))
 				addcmd(line);
 
-			chkcmd(line, mac);
+			chkcmd(mac);
 		}
 
 		/*
@@ -365,7 +369,7 @@
 	}
 }
 
-void
+static void
 complain(int i)
 {
 	pe(stk[i].lno);
@@ -374,7 +378,7 @@
 	printf("\n");
 }
 
-void
+static void
 prop(int i)
 {
 	if (stk[i].pl == 0)
@@ -392,10 +396,10 @@
 	}
 }
 
-void
-chkcmd(char *line, char *mac)
+static void
+chkcmd(const char *mac)
 {
-	register int i;
+	int i;
 
 	/*
 	 * Check to see if it matches top of stack.
@@ -428,10 +432,10 @@
 	}
 }
 
-void
-nomatch(char *mac)
+static void
+nomatch(const char *mac)
 {
-	register int i, j;
+	int i, j;
 
 	/*
 	 * Look for a match further down on stack
@@ -473,23 +477,23 @@
 }
 
 /* eq: are two strings equal? */
-int
-eq(char *s1, char *s2)
+static int
+eq(const char *s1, const char *s2)
 {
 	return (strcmp(s1, s2) == 0);
 }
 
 /* print the first part of an error message, given the line number */
-void
-pe(int lineno)
+static void
+pe(int mylineno)
 {
 	if (nfiles > 1)
 		printf("%s: ", cfilename);
-	printf("%d: ", lineno);
+	printf("%d: ", mylineno);
 }
 
-void
-checkknown(char *mac)
+static void
+checkknown(const char *mac)
 {
 
 	if (eq(mac, "."))
@@ -506,18 +510,18 @@
 /*
  * We have a .de xx line in "line".  Add xx to the list of known commands.
  */
-void
-addcmd(char *line)
+static void
+addcmd(char *myline)
 {
 	char *mac;
 
 	/* grab the macro being defined */
-	mac = line+4;
+	mac = myline + 4;
 	while (isspace(*mac))
 		mac++;
 	if (*mac == 0) {
 		pe(lineno);
-		printf("illegal define: %s\n", line);
+		printf("illegal define: %s\n", myline);
 		return;
 	}
 	mac[2] = 0;
@@ -535,12 +539,13 @@
  * structure here but this is a quick-and-dirty job and I just don't
  * have time to mess with it.  (I wonder if this will come back to haunt
  * me someday?)  Anyway, I claim that .de is fairly rare in user
- * nroff programs, and the register loop below is pretty fast.
+ * nroff programs, and the loop below is pretty fast.
  */
-void
-addmac(char *mac)
+static void
+addmac(const char *mac)
 {
-	register char **src, **dest, **loc;
+	const char **src, **dest, **loc;
+	char *tmp;
 
 	if (binsrch(mac) >= 0){	/* it's OK to redefine something */
 #ifdef DEBUG
@@ -557,8 +562,11 @@
 	dest = src+1;
 	while (dest > loc)
 		*dest-- = *src--;
-	*loc = malloc(3);
-	strcpy(*loc, mac);
+
+	tmp = malloc(3);
+	strncpy(tmp, mac, 2);
+	*loc = tmp;
+
 	ncmds++;
 #ifdef DEBUG
 printf("after: %s %s %s %s %s, %d cmds\n", knowncmds[slot-2], knowncmds[slot-1], knowncmds[slot], knowncmds[slot+1], knowncmds[slot+2], ncmds);
@@ -569,13 +577,13 @@
  * Do a binary search in knowncmds for mac.
  * If found, return the index.  If not, return -1.
  */
-int
-binsrch(char *mac)
+static int
+binsrch(const char *mac)
 {
-	register char *p;	/* pointer to current cmd in list */
-	register int d;		/* difference if any */
-	register int mid;	/* mid point in binary search */
-	register int top, bot;	/* boundaries of bin search, inclusive */
+	const char *p;	/* pointer to current cmd in list */
+	int d;		/* difference if any */
+	int mid;	/* mid point in binary search */
+	int top, bot;	/* boundaries of bin search, inclusive */
 
 	top = ncmds-1;
 	bot = 0;


[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]