ansify and knf

This commit is contained in:
christos 2003-11-08 18:12:01 +00:00
parent 302ec37ca8
commit 60d5c19922
1 changed files with 115 additions and 130 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: xstr.c,v 1.14 2003/08/07 11:17:51 agc Exp $ */ /* $NetBSD: xstr.c,v 1.15 2003/11/08 18:12:01 christos Exp $ */
/* /*
* Copyright (c) 1980, 1993 * Copyright (c) 1980, 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
#if 0 #if 0
static char sccsid[] = "@(#)xstr.c 8.1 (Berkeley) 6/9/93"; static char sccsid[] = "@(#)xstr.c 8.1 (Berkeley) 6/9/93";
#else #else
__RCSID("$NetBSD: xstr.c,v 1.14 2003/08/07 11:17:51 agc Exp $"); __RCSID("$NetBSD: xstr.c,v 1.15 2003/11/08 18:12:01 christos Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -61,21 +61,21 @@ __RCSID("$NetBSD: xstr.c,v 1.14 2003/08/07 11:17:51 agc Exp $");
* November, 1978 * November, 1978
*/ */
static off_t hashit __P((char *, int)); static off_t hashit(const char *, int);
static void onintr __P((int)); static void onintr(int);
static off_t yankstr __P((char **)); static off_t yankstr(char **);
static int octdigit __P((char)); static int octdigit(char);
static void inithash __P((void)); static void inithash(void);
static int fgetNUL __P((char *, int, FILE *)); static int fgetNUL(char *, int, FILE *);
static int xgetc __P((FILE *)); static int xgetc(FILE *);
static void flushsh __P((void)); static void flushsh(void);
static void found __P((int, off_t, char *)); static void found(int, off_t, const char *);
static void prstr __P((char *)); static void prstr(const char *);
static void xsdotc __P((void)); static void xsdotc(void);
static char lastchr __P((char *)); static char lastchr(const char *);
static int istail __P((char *, char *)); static int istail(const char *, const char *);
static void process __P((char *)); static void process(const char *);
static void usage __P((void)); static void usage(void);
static off_t tellpt; static off_t tellpt;
static off_t mesgpt; static off_t mesgpt;
@ -95,12 +95,10 @@ static struct hash {
short hnew; short hnew;
} bucket[BUCKETS]; } bucket[BUCKETS];
int main __P((int, char *[])); int main(int, char *[]);
int int
main(argc, argv) main(int argc, char *argv[])
int argc;
char *argv[];
{ {
int c; int c;
@ -160,8 +158,7 @@ main(argc, argv)
} }
static void static void
process(name) process(const char *name)
char *name;
{ {
char *cp; char *cp;
int c; int c;
@ -185,73 +182,75 @@ process(name)
printf("%s", linebuf); printf("%s", linebuf);
continue; continue;
} }
for (cp = linebuf; (c = *cp++);) switch (c) { for (cp = linebuf; (c = *cp++);)
switch (c) {
case '"': case '"':
if (incomm || inasm) if (incomm || inasm)
goto def;
if ((ret = (int) yankstr(&cp)) == -1)
goto out;
printf("(&%s[%d])", array, ret);
break;
case '\'':
if (incomm || inasm)
goto def;
putchar(c);
if (*cp)
putchar(*cp++);
break;
case '/':
if (incomm || *cp != '*')
goto def;
incomm = 1;
cp++;
printf("/*");
continue;
case '*':
if (incomm && *cp == '/') {
incomm = 0;
cp++;
printf("*/");
continue;
}
goto def;
case '(':
if (!incomm && inasm)
asmparnest++;
goto def;
case ')':
if (!incomm && inasm && !--asmparnest)
inasm = 0;
goto def;
case '_':
if (incomm || inasm)
goto def;
if (!strncmp(cp, "_asm", 4)) {
cp += 4;
printf("__asm");
if (!strncmp(cp, "__", 2)) {
cp += 2;
printf("__");
}
if (isalnum(*cp) || *cp == '_')
goto def; goto def;
asmparnest = 0; if ((ret = (int) yankstr(&cp)) == -1)
inasm = 1; goto out;
} else printf("(&%s[%d])", array, ret);
break;
case '\'':
if (incomm || inasm)
goto def;
putchar(c);
if (*cp)
putchar(*cp++);
break;
case '/':
if (incomm || *cp != '*')
goto def;
incomm = 1;
cp++;
printf("/*");
continue;
case '*':
if (incomm && *cp == '/') {
incomm = 0;
cp++;
printf("*/");
continue;
}
goto def; goto def;
break;
case '(':
if (!incomm && inasm)
asmparnest++;
goto def;
case ')':
if (!incomm && inasm && !--asmparnest)
inasm = 0;
goto def;
case '_':
if (incomm || inasm)
goto def;
if (!strncmp(cp, "_asm", 4)) {
cp += 4;
printf("__asm");
if (!strncmp(cp, "__", 2)) {
cp += 2;
printf("__");
}
if (isalnum((unsigned char)*cp) ||
*cp == '_')
goto def;
asmparnest = 0;
inasm = 1;
} else
goto def;
break;
def: def:
default: default:
putchar(c); putchar(c);
break; break;
} }
} }
out: out:
if (ferror(stdout)) { if (ferror(stdout)) {
@ -261,8 +260,7 @@ out:
} }
static off_t static off_t
yankstr(cpp) yankstr(char **cpp)
char **cpp;
{ {
char *cp = *cpp; char *cp = *cpp;
int c, ch; int c, ch;
@ -270,7 +268,7 @@ yankstr(cpp)
char *dp = dbuf; char *dp = dbuf;
char *tp; char *tp;
while ((c = *cp++)) { while ((c = *cp++) != '\0') {
switch (c) { switch (c) {
case '"': case '"':
@ -315,7 +313,7 @@ gotc:
out: out:
*cpp = --cp; *cpp = --cp;
*dp = 0; *dp = 0;
return (hashit(dbuf, 1)); return hashit(dbuf, 1);
} }
static int static int
@ -327,7 +325,7 @@ octdigit(c)
} }
static void static void
inithash() inithash(void)
{ {
char buf[BUFSIZ]; char buf[BUFSIZ];
FILE *mesgread = fopen(strings, "r"); FILE *mesgread = fopen(strings, "r");
@ -344,34 +342,28 @@ inithash()
} }
static int static int
fgetNUL(obuf, rmdr, file) fgetNUL(char *obuf, int rmdr, FILE *file)
char *obuf;
int rmdr;
FILE *file;
{ {
int c; int c;
char *buf = obuf; char *buf = obuf;
while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF) while (--rmdr > 0 && (c = xgetc(file) != 0 && c != EOF))
*buf++ = c; *buf++ = c;
*buf++ = 0; *buf++ = 0;
return ((feof(file) || ferror(file)) ? 0 : 1); return (feof(file) || ferror(file)) ? 0 : 1;
} }
static int static int
xgetc(file) xgetc(FILE *file)
FILE *file;
{ {
tellpt++; tellpt++;
return (getc(file)); return getc(file);
} }
static off_t static off_t
hashit(str, new) hashit(const char *str, int new)
char *str;
int new;
{ {
int i; int i;
struct hash *hp, *hp0; struct hash *hp, *hp0;
@ -383,7 +375,7 @@ hashit(str, new)
if (i >= 0) if (i >= 0)
return (hp->hpt + i); return (hp->hpt + i);
} }
if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL) if ((hp = calloc(1, sizeof (*hp))) == NULL)
err(1, NULL); err(1, NULL);
hp->hpt = mesgpt; hp->hpt = mesgpt;
if ((hp->hstr = strdup(str)) == NULL) if ((hp->hstr = strdup(str)) == NULL)
@ -396,7 +388,7 @@ hashit(str, new)
} }
static void static void
flushsh() flushsh(void)
{ {
int i; int i;
struct hash *hp; struct hash *hp;
@ -418,7 +410,7 @@ flushsh()
for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) { for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) {
found(hp->hnew, hp->hpt, hp->hstr); found(hp->hnew, hp->hpt, hp->hstr);
if (hp->hnew) { if (hp->hnew) {
fseek(mesgwrit, hp->hpt, 0); (void)fseek(mesgwrit, hp->hpt, 0);
(void)fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, (void)fwrite(hp->hstr, strlen(hp->hstr) + 1, 1,
mesgwrit); mesgwrit);
if (ferror(mesgwrit)) if (ferror(mesgwrit))
@ -430,10 +422,7 @@ flushsh()
} }
static void static void
found(new, off, str) found(int new, off_t off, const char *str)
int new;
off_t off;
char *str;
{ {
if (vflg == 0) if (vflg == 0)
return; return;
@ -446,24 +435,23 @@ found(new, off, str)
} }
static void static void
prstr(cp) prstr(const char *cp)
char *cp;
{ {
int c; int c;
while ((c = (*cp++ & 0377))) while ((c = (*cp++ & 0377)) != '\0')
if (c < ' ') if (c < ' ')
fprintf(stderr, "^%c", c + '`'); (void)fprintf(stderr, "^%c", c + '`');
else if (c == 0177) else if (c == 0177)
fprintf(stderr, "^?"); (void)fprintf(stderr, "^?");
else if (c > 0200) else if (c > 0200)
fprintf(stderr, "\\%03o", c); (void)fprintf(stderr, "\\%03o", c);
else else
fprintf(stderr, "%c", c); (void)fprintf(stderr, "%c", c);
} }
static void static void
xsdotc() xsdotc(void)
{ {
FILE *strf = fopen(strings, "r"); FILE *strf = fopen(strings, "r");
FILE *xdotcf; FILE *xdotcf;
@ -473,7 +461,7 @@ xsdotc()
xdotcf = fopen("xs.c", "w"); xdotcf = fopen("xs.c", "w");
if (xdotcf == NULL) if (xdotcf == NULL)
err(1, "Cannot open `%s'", "xs.c"); err(1, "Cannot open `%s'", "xs.c");
fprintf(xdotcf, "char\t%s[] = {\n", array); (void)fprintf(xdotcf, "char\t%s[] = {\n", array);
for (;;) { for (;;) {
int i, c; int i, c;
@ -484,22 +472,21 @@ xsdotc()
onintr(1); onintr(1);
} }
if (feof(strf)) { if (feof(strf)) {
fprintf(xdotcf, "\n"); (void)fprintf(xdotcf, "\n");
goto out; goto out;
} }
fprintf(xdotcf, "0x%02x,", c); (void)fprintf(xdotcf, "0x%02x,", c);
} }
fprintf(xdotcf, "\n"); (void)fprintf(xdotcf, "\n");
} }
out: out:
fprintf(xdotcf, "};\n"); (void)fprintf(xdotcf, "};\n");
(void)fclose(xdotcf); (void)fclose(xdotcf);
(void)fclose(strf); (void)fclose(strf);
} }
static char static char
lastchr(cp) lastchr(const char *cp)
char *cp;
{ {
while (cp[0] && cp[1]) while (cp[0] && cp[1])
@ -508,8 +495,7 @@ lastchr(cp)
} }
static int static int
istail(str, of) istail(const char *str, const char *of)
char *str, *of;
{ {
int d = strlen(of) - strlen(str); int d = strlen(of) - strlen(str);
@ -519,8 +505,7 @@ istail(str, of)
} }
static void static void
onintr(dummy) onintr(int dummy)
int dummy;
{ {
(void)signal(SIGINT, SIG_IGN); (void)signal(SIGINT, SIG_IGN);
@ -532,7 +517,7 @@ onintr(dummy)
} }
static void static void
usage() usage(void)
{ {
(void)fprintf(stderr, "Usage: %s [-vc] [-l array] [-] [<name> ...]\n", (void)fprintf(stderr, "Usage: %s [-vc] [-l array] [-] [<name> ...]\n",