From 230bd5e545c7c7af3c3226de0dc169b2b8a7f770 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Fri, 15 May 2015 08:53:52 +0000 Subject: [PATCH] MFC: r282449, r282450, r282451, r282452, r282453, r282454, r282455, r282457, r282459, r282460, r282461 Modernize code: ansification, use c99 features Improve style(9) Better memory checking --- usr.bin/vgrind/Makefile | 2 +- usr.bin/vgrind/extern.h | 8 +- usr.bin/vgrind/regexp.c | 129 ++++++++++---------- usr.bin/vgrind/vfontedpr.c | 235 ++++++++++++++++++------------------- usr.bin/vgrind/vgrindefs.c | 46 ++++---- 5 files changed, 202 insertions(+), 218 deletions(-) diff --git a/usr.bin/vgrind/Makefile b/usr.bin/vgrind/Makefile index 612e504ecb2b..06d905239cb6 100644 --- a/usr.bin/vgrind/Makefile +++ b/usr.bin/vgrind/Makefile @@ -11,7 +11,7 @@ FILESDIR= ${SHAREDIR}/misc FILESDIR_tmac.vgrind= ${SHAREDIR}/tmac MAN= vgrind.1 vgrindefs.5 -WARNS?= 2 +WARNS?= 3 BINDIR= /usr/libexec SCRIPTSDIR=/usr/bin diff --git a/usr.bin/vgrind/extern.h b/usr.bin/vgrind/extern.h index 6616ca94ef72..812592f43359 100644 --- a/usr.bin/vgrind/extern.h +++ b/usr.bin/vgrind/extern.h @@ -31,9 +31,7 @@ * $FreeBSD$ */ -typedef int boolean; - -extern boolean _escaped; /* if last character was an escape */ +extern bool _escaped; /* if last character was an escape */ extern char *s_start; /* start of the current string */ extern char *l_acmbeg; /* string introducing a comment */ extern char *l_acmend; /* string ending a comment */ @@ -45,11 +43,11 @@ extern char *l_combeg; /* string introducing a comment */ extern char *l_comend; /* string ending a comment */ extern char l_escape; /* character used to escape characters */ extern char *l_keywds[]; /* keyword table address */ -extern boolean l_onecase; /* upper and lower case are equivalent */ +extern bool l_onecase; /* upper and lower case are equivalent */ extern char *l_prcbeg; /* regular expr for procedure begin */ extern char *l_strbeg; /* delimiter for string constant */ extern char *l_strend; /* delimiter for string constant */ -extern boolean l_toplex; /* procedures only defined at top lex level */ +extern bool l_toplex; /* procedures only defined at top lex level */ extern const char *language; /* the language indicator */ #include diff --git a/usr.bin/vgrind/regexp.c b/usr.bin/vgrind/regexp.c index 2da447d9e374..cd57835e5464 100644 --- a/usr.bin/vgrind/regexp.c +++ b/usr.bin/vgrind/regexp.c @@ -44,19 +44,16 @@ static const char sccsid[] = "@(#)regexp.c 8.1 (Berkeley) 6/6/93"; #include #include +#include #include #include "extern.h" -#define FALSE 0 -#define TRUE !(FALSE) -#define NIL 0 - static void expconv(void); -boolean _escaped; /* true if we are currently _escaped */ +bool _escaped; /* true if we are currently x_escaped */ char *s_start; /* start of string */ -boolean l_onecase; /* true if upper and lower equivalent */ +bool l_onecase; /* true if upper and lower equivalent */ #define makelower(c) (isupper((c)) ? tolower((c)) : (c)) @@ -66,9 +63,7 @@ boolean l_onecase; /* true if upper and lower equivalent */ */ int -STRNCMP(s1, s2, len) - register char *s1,*s2; - register int len; +STRNCMP(register char *s1, register char *s2, register int len) { if (l_onecase) { do @@ -147,18 +142,18 @@ STRNCMP(s1, s2, len) static char *ccre; /* pointer to current position in converted exp*/ static char *ure; /* pointer current position in unconverted exp */ +/* re: unconverted irregular expression */ char * -convexp(re) - char *re; /* unconverted irregular expression */ +convexp(char *re) { register char *cre; /* pointer to converted regular expression */ /* allocate room for the converted expression */ - if (re == NIL) - return (NIL); + if (re == NULL) + return (NULL); if (*re == '\0') - return (NIL); - cre = malloc (4 * strlen(re) + 3); + return (NULL); + cre = malloc(4 * strlen(re) + 3); ccre = cre; ure = re; @@ -182,9 +177,9 @@ expconv() register int temp; /* let the conversion begin */ - acs = NIL; - cs = NIL; - while (*ure != NIL) { + acs = NULL; + cs = NULL; + while (*ure) { switch (c = *ure++) { case '\\': @@ -192,7 +187,7 @@ expconv() /* escaped characters are just characters */ default: - if (cs == NIL || (*cs & STR) == 0) { + if (cs == NULL || (*cs & STR) == 0) { cs = ccre; *cs = STR; SCNT(cs) = 1; @@ -207,13 +202,13 @@ expconv() case 'd': case 'e': case 'p': - if (acs != NIL && acs != cs) { + if (acs != NULL && acs != cs) { do { temp = OCNT(acs); OCNT(acs) = ccre - acs; acs -= temp; } while (temp != 0); - acs = NIL; + acs = NULL; } cs = ccre; *cs = META; @@ -226,13 +221,13 @@ expconv() /* just put the symbol in */ case '^': case '$': - if (acs != NIL && acs != cs) { + if (acs != NULL && acs != cs) { do { temp = OCNT(acs); OCNT(acs) = ccre - acs; acs -= temp; } while (temp != 0); - acs = NIL; + acs = NULL; } cs = ccre; *cs = META; @@ -248,31 +243,31 @@ expconv() /* recurse and define a subexpression */ case '(': - if (acs != NIL && acs != cs) { + if (acs != NULL && acs != cs) { do { temp = OCNT(acs); OCNT(acs) = ccre - acs; acs -= temp; } while (temp != 0); - acs = NIL; + acs = NULL; } cs = ccre; *cs = OPER; OSYM(cs) = '('; ccre = ONEXT(cs); - expconv (); + expconv(); OCNT(cs) = ccre - cs; /* offset to next symbol */ break; /* return from a recursion */ case ')': - if (acs != NIL) { + if (acs != NULL) { do { temp = OCNT(acs); OCNT(acs) = ccre - acs; acs -= temp; } while (temp != 0); - acs = NIL; + acs = NULL; } cs = ccre; *cs = META; @@ -284,7 +279,7 @@ expconv() /* the third byte will contain an offset to jump over the */ /* alternate match in case the first did not fail */ case '|': - if (acs != NIL && acs != cs) + if (acs != NULL && acs != cs) OCNT(ccre) = ccre - acs; /* make a back pointer */ else OCNT(ccre) = 0; @@ -298,7 +293,7 @@ expconv() /* if its not a metasymbol just build a scharacter string */ default: - if (cs == NIL || (*cs & STR) == 0) { + if (cs == NULL || (*cs & STR) == 0) { cs = ccre; *cs = STR; SCNT(cs) = 1; @@ -309,13 +304,13 @@ expconv() break; } } - if (acs != NIL) { + if (acs != NULL) { do { temp = OCNT(acs); OCNT(acs) = ccre - acs; acs -= temp; } while (temp != 0); - acs = NIL; + acs = NULL; } return; } @@ -344,21 +339,23 @@ expconv() * character matched. */ +/* + * s: string to check for a match in + * re: a converted irregular expression + * mstring: where to put whatever matches a \p + */ char * -expmatch (s, re, mstring) - register char *s; /* string to check for a match in */ - register char *re; /* a converted irregular expression */ - register char *mstring; /* where to put whatever matches a \p */ +expmatch (register char *s, register char *re, register char *mstring) { register char *cs; /* the current symbol */ register char *ptr,*s1; /* temporary pointer */ - boolean matched; /* a temporary boolean */ + bool matched; /* a temporary bool */ /* initial conditions */ - if (re == NIL) - return (NIL); + if (re == NULL) + return (NULL); cs = re; - matched = FALSE; + matched = false; /* loop till expression string is exhausted (or at least pretty tired) */ while (*cs) { @@ -384,7 +381,7 @@ expmatch (s, re, mstring) } else { /* no match, error return */ - return (NIL); + return (NULL); } break; @@ -406,8 +403,8 @@ expmatch (s, re, mstring) /* this is a grouping, recurse */ case '(': - ptr = expmatch (s, ONEXT(cs), mstring); - if (ptr != NIL) { + ptr = expmatch(s, ONEXT(cs), mstring); + if (ptr != NULL) { /* the subexpression matched */ matched = 1; @@ -423,7 +420,7 @@ expmatch (s, re, mstring) } else { /* no match, error return */ - return (NIL); + return (NULL); } cs = OPTR(cs); break; @@ -443,35 +440,35 @@ expmatch (s, re, mstring) */ s1 = s; do { - ptr = expmatch (s1, MNEXT(cs), mstring); - if (ptr != NIL && s1 != s) { + ptr = expmatch(s1, MNEXT(cs), mstring); + if (ptr != NULL && s1 != s) { /* we have a match, remember the match */ strncpy (mstring, s, s1 - s); mstring[s1 - s] = '\0'; return (ptr); - } else if (ptr != NIL && (*cs & OPT)) { + } else if (ptr != NULL && (*cs & OPT)) { /* it was aoptional so no match is ok */ return (ptr); - } else if (ptr != NIL) { + } else if (ptr != NULL) { /* not optional and we still matched */ - return (NIL); + return (NULL); } if (!(isalnum(*s1) || *s1 == '_' || /* C++ destructor */ *s1 == '~' || /* C++ scope operator */ (strlen(s1) > 1 && *s1 == ':' && s1[1] == ':' && - (s1++, TRUE)))) - return (NIL); + (s1++, true)))) + return (NULL); if (*s1 == '\\') - _escaped = _escaped ? FALSE : TRUE; + _escaped = _escaped ? false : true; else - _escaped = FALSE; + _escaped = false; } while (*s1++); - return (NIL); + return (NULL); /* try to match anything */ case 'a': @@ -482,31 +479,31 @@ expmatch (s, re, mstring) */ s1 = s; do { - ptr = expmatch (s1, MNEXT(cs), mstring); - if (ptr != NIL && s1 != s) { + ptr = expmatch(s1, MNEXT(cs), mstring); + if (ptr != NULL && s1 != s) { /* we have a match */ return (ptr); - } else if (ptr != NIL && (*cs & OPT)) { + } else if (ptr != NULL && (*cs & OPT)) { /* it was aoptional so no match is ok */ return (ptr); - } else if (ptr != NIL) { + } else if (ptr != NULL) { /* not optional and we still matched */ - return (NIL); + return (NULL); } if (*s1 == '\\') - _escaped = _escaped ? FALSE : TRUE; + _escaped = _escaped ? false : true; else - _escaped = FALSE; + _escaped = false; } while (*s1++); - return (NIL); + return (NULL); /* fail if we are currently _escaped */ case 'e': if (_escaped) - return(NIL); + return(NULL); cs = MNEXT(cs); break; @@ -538,7 +535,7 @@ expmatch (s, re, mstring) } else /* no match, error return */ - return (NIL); + return (NULL); break; /* check for end of line */ @@ -562,7 +559,7 @@ expmatch (s, re, mstring) } else /* no match, error return */ - return (NIL); + return (NULL); break; /* check for start of line */ @@ -585,7 +582,7 @@ expmatch (s, re, mstring) } else /* no match, error return */ - return (NIL); + return (NULL); break; /* end of a subexpression, return success */ diff --git a/usr.bin/vgrind/vfontedpr.c b/usr.bin/vgrind/vfontedpr.c index 5baaec0b5f10..ec0b4e042eb4 100644 --- a/usr.bin/vgrind/vfontedpr.c +++ b/usr.bin/vgrind/vfontedpr.c @@ -47,14 +47,12 @@ static const char sccsid[] = "@(#)vfontedpr.c 8.1 (Berkeley) 6/6/93"; #include #include #include +#include #include #include #include "pathnames.h" #include "extern.h" -#define FALSE 0 -#define TRUE !(FALSE) -#define NIL 0 #define STANDARD 0 #define ALTERNATE 1 @@ -70,8 +68,8 @@ static const char sccsid[] = "@(#)vfontedpr.c 8.1 (Berkeley) 6/6/93"; #define PSMAX 20 /* size of procedure name stacking */ static int iskw(char *); -static boolean isproc(char *); -static void putKcp(char *, char *, boolean); +static bool isproc(char *); +static void putKcp(char *, char *, bool); static void putScp(char *); static void putcp(int); static int tabs(char *, char *); @@ -81,13 +79,13 @@ static int width(char *, char *); * The state variables */ -static boolean filter = FALSE; /* act as a filter (like eqn) */ -static boolean inchr; /* in a string constant */ -static boolean incomm; /* in a comment of the primary type */ -static boolean idx = FALSE; /* form an index */ -static boolean instr; /* in a string constant */ -static boolean nokeyw = FALSE; /* no keywords being flagged */ -static boolean pass = FALSE; /* +static bool filter = false; /* act as a filter (like eqn) */ +static bool inchr; /* in a string constant */ +static bool incomm; /* in a comment of the primary type */ +static bool idx = false; /* form an index */ +static bool instr; /* in a string constant */ +static bool nokeyw = false; /* no keywords being flagged */ +static bool pass = false; /* * when acting as a filter, pass indicates * whether we are currently processing * input. @@ -100,7 +98,7 @@ static char * defsfile[2] = { _PATH_VGRINDEFS, 0 }; static int margin; static int plstack[PSMAX]; /* the procedure nesting level stack */ static char pname[BUFSIZ+1]; -static boolean prccont; /* continue last procedure */ +static bool prccont; /* continue last procedure */ static int psptr; /* the stack index of the current procedure */ static char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */ @@ -122,15 +120,15 @@ char *l_nocom; /* regexp for non-comments */ char *l_prcbeg; /* regular expr for procedure begin */ char *l_strbeg; /* delimiter for string constant */ char *l_strend; /* delimiter for string constant */ -boolean l_toplex; /* procedures only defined at top lex level */ +bool l_toplex; /* procedures only defined at top lex level */ const char *language = "c"; /* the language indicator */ #define ps(x) printf("%s", x) +static char minus[] = "-"; +static char minusn[] = "-n"; int -main(argc, argv) - int argc; - char *argv[]; +main(int argc, char **argv) { const char *fname = ""; struct stat stbuf; @@ -160,9 +158,9 @@ main(argc, argv) /* act as a filter like eqn */ if (!strcmp(argv[0], "-f")) { - filter++; + filter = true; argv[0] = argv[argc-1]; - argv[argc-1] = strdup("-"); + argv[argc-1] = minus; continue; } @@ -174,13 +172,13 @@ main(argc, argv) /* build an index */ if (!strcmp(argv[0], "-x")) { - idx++; - argv[0] = strdup("-n"); + idx = true; + argv[0] = minusn; } /* indicate no keywords */ if (!strcmp(argv[0], "-n")) { - nokeyw++; + nokeyw = true; argc--, argv++; continue; } @@ -227,17 +225,17 @@ main(argc, argv) i = cgetent(&defs, defsfile, language); if (i == -1) { fprintf (stderr, "no entry for language %s\n", language); - exit (0); + exit(0); } else if (i == -2) { fprintf(stderr, "cannot find vgrindefs file %s\n", defsfile[0]); - exit (0); + exit(0); } else if (i == -3) { fprintf(stderr, "potential reference loop detected in vgrindefs file %s\n", defsfile[0]); exit(0); } if (cgetustr(defs, "kw", &cp) == -1) - nokeyw = TRUE; + nokeyw = true; else { char **cpp; @@ -250,7 +248,7 @@ main(argc, argv) while (*cp != ' ' && *cp != '\t' && *cp) cp++; } - *cpp = NIL; + *cpp = NULL; } cgetustr(defs, "pb", &cp); l_prcbeg = convexp(cp); @@ -282,10 +280,10 @@ main(argc, argv) /* initialize the program */ - incomm = FALSE; - instr = FALSE; - inchr = FALSE; - _escaped = FALSE; + incomm = false; + instr = false; + inchr = false; + _escaped = false; blklevel = 0; for (psptr=0; psptr 0 /* sanity */) blklevel--; @@ -465,13 +462,13 @@ skip: /* end of current procedure */ if (s != os) - ps ("\\c"); - ps ("\\c\n'-F\n"); + ps("\\c"); + ps("\\c\n'-F\n"); blklevel = plstack[psptr]; /* see if we should print the last proc name */ if (--psptr >= 0) - prccont = TRUE; + prccont = true; else psptr = -1; } @@ -480,8 +477,8 @@ skip: } /* start of a lexical block */ - if (blksptr != NIL) { - putKcp (s, blksptr - 1, FALSE); + if (blksptr != NULL) { + putKcp(s, blksptr - 1, false); s = blksptr; blklevel++; continue; @@ -489,64 +486,66 @@ skip: /* check for end of comment */ } else if (incomm) { - comptr = expmatch (s, l_comend, dummy); - acmptr = expmatch (s, l_acmend, dummy); - if (((comtype == STANDARD) && (comptr != NIL)) || - ((comtype == ALTERNATE) && (acmptr != NIL))) { + comptr = expmatch(s, l_comend, dummy); + acmptr = expmatch(s, l_acmend, dummy); + if (((comtype == STANDARD) && (comptr != NULL)) || + ((comtype == ALTERNATE) && (acmptr != NULL))) { if (comtype == STANDARD) { - putKcp (s, comptr-1, TRUE); + putKcp(s, comptr-1, true); s = comptr; } else { - putKcp (s, acmptr-1, TRUE); + putKcp(s, acmptr-1, true); s = acmptr; } - incomm = FALSE; + incomm = false; ps("\\c\n'-C\n"); continue; } else { - putKcp (s, s + strlen(s) -1, TRUE); + putKcp(s, s + strlen(s) -1, true); s = s + strlen(s); continue; } /* check for end of string */ } else if (instr) { - if ((strptr = expmatch (s, l_strend, dummy)) != NIL) { - putKcp (s, strptr-1, TRUE); + if ((strptr = expmatch(s, l_strend, dummy)) != NULL) { + putKcp(s, strptr-1, true); s = strptr; - instr = FALSE; + instr = false; continue; } else { - putKcp (s, s+strlen(s)-1, TRUE); + putKcp(s, s+strlen(s)-1, true); s = s + strlen(s); continue; } /* check for end of character string */ } else if (inchr) { - if ((chrptr = expmatch (s, l_chrend, dummy)) != NIL) { - putKcp (s, chrptr-1, TRUE); + if ((chrptr = expmatch(s, l_chrend, dummy)) != NULL) { + putKcp(s, chrptr-1, true); s = chrptr; - inchr = FALSE; + inchr = false; continue; } else { - putKcp (s, s+strlen(s)-1, TRUE); + putKcp(s, s+strlen(s)-1, true); s = s + strlen(s); continue; } } /* print out the line */ - putKcp (s, s + strlen(s) -1, FALSE); + putKcp(s, s + strlen(s) -1, false); s = s + strlen(s); } while (*s); } +/* + * start: start of string to write + * end: end of string to write + * force: true if we should force nokeyw + */ static void -putKcp (start, end, force) - char *start; /* start of string to write */ - char *end; /* end of string to write */ - boolean force; /* true if we should force nokeyw */ +putKcp(char *start, char *end, bool force) { int i; int xfld = 0; @@ -587,22 +586,20 @@ putKcp (start, end, force) } } - putcp ((unsigned char)*start++); + putcp((unsigned char)*start++); } } static int -tabs(s, os) - char *s, *os; +tabs(char *s, char *os) { return (width(s, os) / 8); } static int -width(s, os) - register char *s, *os; +width(register char *s, register char *os) { register int i = 0; @@ -622,8 +619,7 @@ width(s, os) } static void -putcp(c) - register int c; +putcp(register int c) { switch(c) { @@ -689,16 +685,15 @@ putcp(c) /* * look for a process beginning on this line */ -static boolean -isproc(s) - char *s; +static bool +isproc(char *s) { pname[0] = '\0'; if (!l_toplex || blklevel == 0) - if (expmatch (s, l_prcbeg, pname) != NIL) { - return (TRUE); + if (expmatch(s, l_prcbeg, pname) != NULL) { + return (true); } - return (FALSE); + return (false); } @@ -706,8 +701,7 @@ isproc(s) */ static int -iskw(s) - register char *s; +iskw(register char *s) { register char **ss = l_keywds; register int i = 1; @@ -720,4 +714,3 @@ iskw(s) return (i); return (0); } - diff --git a/usr.bin/vgrind/vgrindefs.c b/usr.bin/vgrind/vgrindefs.c index b811c2c21d90..c48a0483b0ca 100644 --- a/usr.bin/vgrind/vgrindefs.c +++ b/usr.bin/vgrind/vgrindefs.c @@ -55,30 +55,30 @@ __FBSDID("$FreeBSD$"); static char *tbuf; static char *filename; static int hopcount; /* detect infinite loops in termcap, init 0 */ -char *tskip(); -char *tgetstr(); -char *tdecode(); -char *getenv(); + +static int tnchktc(void); +static int tnamatch(char *); +static char *tskip(register char *); +static char *tdecode(register char *, char **); /* * Get an entry for terminal name in buffer bp, * from the termcap file. Parse is very rudimentary; * we just notice escaped newlines. */ -tgetent(bp, name, file) - char *bp, *name, *file; +int +tgetent(char *bp, char *name, char *file) { register char *cp; register int c; register int i = 0, cnt = 0; char ibuf[BUFSIZ]; - char *cp2; int tf; tbuf = bp; tf = 0; filename = file; - tf = open(filename, 0); + tf = open(filename, O_RDONLY); if (tf < 0) return (-1); for (;;) { @@ -125,7 +125,8 @@ tgetent(bp, name, file) * entries to say "like an HP2621 but doesn't turn on the labels". * Note that this works because of the left to right scan. */ -tnchktc() +static int +tnchktc(void) { register char *p, *q; char tcname[16]; /* name of similar terminal */ @@ -143,7 +144,7 @@ tnchktc() /* p now points to beginning of last field */ if (p[0] != 't' || p[1] != 'c') return(1); - strcpy(tcname,p+3); + strlcpy(tcname, p+3, 16); q = tcname; while (q && *q != ':') q++; @@ -161,7 +162,7 @@ tnchktc() write(STDERR_FILENO, "Vgrind entry too long\n", 23); q[BUFSIZ - (p-tbuf)] = 0; } - strcpy(p, q+1); + strlcpy(p, q+1, BUFSIZ - (p - holdtbuf)); tbuf = holdtbuf; return(1); } @@ -172,8 +173,8 @@ tnchktc() * against each such name. The normal : terminator after the last * name (before the first field) stops us. */ -tnamatch(np) - char *np; +static int +tnamatch(char *np) { register char *Np, *Bp; @@ -199,8 +200,7 @@ tnamatch(np) * into the termcap file in octal. */ static char * -tskip(bp) - register char *bp; +tskip(register char *bp) { while (*bp && *bp != ':') @@ -218,8 +218,8 @@ tskip(bp) * a # character. If the option is not found we return -1. * Note that we handle octal numbers beginning with 0. */ -tgetnum(id) - char *id; +int +tgetnum(char *id) { register int i, base; register char *bp = tbuf; @@ -251,8 +251,8 @@ tgetnum(id) * of the buffer. Return 1 if we find the option, or 0 if it is * not given. */ -tgetflag(id) - char *id; +int +tgetflag(char *id) { register char *bp = tbuf; @@ -278,8 +278,7 @@ tgetflag(id) * No checking on area overflow. */ char * -tgetstr(id, area) - char *id, **area; +tgetstr(char *id, char **area) { register char *bp = tbuf; @@ -303,13 +302,10 @@ tgetstr(id, area) * string capability escapes. */ static char * -tdecode(str, area) - register char *str; - char **area; +tdecode(register char *str, char **area) { register char *cp; register int c; - int i; cp = *area; while (c = *str++) {