checknr WARNS6 cleanup

Larry Lansing lansil at fuzzynerd.com
Sun Jan 16 13:40:28 PST 2005


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

--
Larry Lansing


Index: Makefile
===================================================================
RCS file: /home/dcvs/src/usr.bin/checknr/Makefile,v
retrieving revision 1.1
diff -u -r1.1 Makefile
--- Makefile	17 Jun 2003 02:56:01 -0000	1.1
+++ Makefile	16 Jan 2005 20:51:31 -0000
@@ -1,6 +1,7 @@
 #	@(#)Makefile	8.1 (Berkeley) 6/6/93
 
 PROG=	checknr
+WARNS?=	6
 CFLAGS+=-Wall
 
 .include <bsd.prog.mk>
Index: checknr.c
===================================================================
RCS file: /home/dcvs/src/usr.bin/checknr/checknr.c,v
retrieving revision 1.4
diff -u -r1.4 checknr.c
--- checknr.c	3 Nov 2003 19:31:28 -0000	1.4
+++ checknr.c	16 Jan 2005 21:31:28 -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);
+void		addcmd(char *);
+void		addmac(const char *);
+int		binsrch(const char *);
+void		checkknown(const char *);
+void		chkcmd(const char *);
+void		complain(int);
+int		eq(const char *, const char *);
+void		nomatch(const char *);
+void		pe(int);
+void		process(FILE *);
+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++;
@@ -272,7 +276,7 @@
 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);
 		}
 
 		/*
@@ -393,9 +397,9 @@
 }
 
 void
-chkcmd(char *line, char *mac)
+chkcmd(const char *mac)
 {
-	register int i;
+	int i;
 
 	/*
 	 * Check to see if it matches top of stack.
@@ -429,9 +433,9 @@
 }
 
 void
-nomatch(char *mac)
+nomatch(const char *mac)
 {
-	register int i, j;
+	int i, j;
 
 	/*
 	 * Look for a match further down on stack
@@ -474,22 +478,22 @@
 
 /* eq: are two strings equal? */
 int
-eq(char *s1, char *s2)
+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)
+pe(int mylineno)
 {
 	if (nfiles > 1)
 		printf("%s: ", cfilename);
-	printf("%d: ", lineno);
+	printf("%d: ", mylineno);
 }
 
 void
-checkknown(char *mac)
+checkknown(const char *mac)
 {
 
 	if (eq(mac, "."))
@@ -507,17 +511,17 @@
  * We have a .de xx line in "line".  Add xx to the list of known commands.
  */
 void
-addcmd(char *line)
+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)
+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);
+	strcpy(tmp, mac);
+	*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);
@@ -570,12 +578,12 @@
  * If found, return the index.  If not, return -1.
  */
 int
-binsrch(char *mac)
+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;





More information about the Submit mailing list