Annotation of src/usr.bin/mkstr/mkstr.c, Revision 1.8
1.8 ! millert 1: /* $OpenBSD: mkstr.c,v 1.7 2002/12/13 14:39:55 millert Exp $ */
1.1 deraadt 2: /* $NetBSD: mkstr.c,v 1.4 1995/09/28 06:22:20 tls Exp $ */
3:
4: /*
5: * Copyright (c) 1980, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.8 ! millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
1.7 millert 34: static const char copyright[] =
1.1 deraadt 35: "@(#) Copyright (c) 1980, 1993\n\
36: The Regents of the University of California. All rights reserved.\n";
37: #endif /* not lint */
38:
39: #ifndef lint
40: #if 0
1.7 millert 41: static const char sccsid[] = "@(#)mkstr.c 8.1 (Berkeley) 6/6/93";
1.1 deraadt 42: #else
1.8 ! millert 43: static const char rcsid[] = "$OpenBSD: mkstr.c,v 1.7 2002/12/13 14:39:55 millert Exp $";
1.1 deraadt 44: #endif
45: #endif /* not lint */
46:
1.7 millert 47: #include <sys/param.h>
1.3 deraadt 48: #include <sys/stat.h>
1.7 millert 49:
50: #include <err.h>
1.1 deraadt 51: #include <stdio.h>
52: #include <stdlib.h>
53: #include <string.h>
54:
55: #define ungetchar(c) ungetc(c, stdin)
56:
57: /*
58: * mkstr - create a string error message file by massaging C source
59: *
60: * Bill Joy UCB August 1977
61: *
62: * Modified March 1978 to hash old messages to be able to recompile
63: * without addding messages to the message file (usually)
64: *
65: * Based on an earlier program conceived by Bill Joy and Chuck Haley
66: *
67: * Program to create a string error message file
68: * from a group of C programs. Arguments are the name
69: * of the file where the strings are to be placed, the
70: * prefix of the new files where the processed source text
71: * is to be placed, and the files to be processed.
72: *
73: * The program looks for 'error("' in the source stream.
74: * Whenever it finds this, the following characters from the '"'
75: * to a '"' are replaced by 'seekpt' where seekpt is a
76: * pointer into the error message file.
77: * If the '(' is not immediately followed by a '"' no change occurs.
78: *
79: * The optional '-' causes strings to be added at the end of the
80: * existing error message file for recompilation of single routines.
81: */
82:
83:
84: FILE *mesgread, *mesgwrite;
1.7 millert 85: char name[MAXPATHLEN], *np;
1.1 deraadt 86:
1.5 millert 87: void inithash(void);
88: void process(void);
89: int match(char *);
90: void copystr(void);
91: int octdigit(char);
1.7 millert 92: unsigned int hashit(char *, char, unsigned int);
1.5 millert 93: int fgetNUL(char *, int, FILE *);
1.7 millert 94: __dead void usage(void);
1.3 deraadt 95:
96: int
1.7 millert 97: main(int argc, char **argv)
1.1 deraadt 98: {
1.7 millert 99: size_t n;
1.1 deraadt 100: char addon = 0;
101:
1.7 millert 102: argc--, argv++;
1.1 deraadt 103: if (argc > 1 && argv[0][0] == '-')
104: addon++, argc--, argv++;
105: if (argc < 3)
1.7 millert 106: usage();
1.1 deraadt 107: mesgwrite = fopen(argv[0], addon ? "a" : "w");
108: if (mesgwrite == NULL)
109: perror(argv[0]), exit(1);
110: mesgread = fopen(argv[0], "r");
111: if (mesgread == NULL)
112: perror(argv[0]), exit(1);
113: inithash();
114: argc--, argv++;
1.7 millert 115: if ((n = strlcpy(name, argv[0], sizeof(name))) >= sizeof(name))
116: errx(1, "%s too long", argv[0]);
117: np = name + n;
1.1 deraadt 118: argc--, argv++;
119: do {
1.7 millert 120: if (strlcpy(np, argv[0], sizeof(name) - n) >=
121: sizeof(name) - n)
122: errx(1, "%s too long", argv[0]);
1.1 deraadt 123: if (freopen(name, "w", stdout) == NULL)
124: perror(name), exit(1);
125: if (freopen(argv[0], "r", stdin) == NULL)
126: perror(argv[0]), exit(1);
127: process();
128: argc--, argv++;
129: } while (argc > 0);
1.7 millert 130: exit (0);
1.1 deraadt 131: }
132:
1.3 deraadt 133: void
1.7 millert 134: process(void)
1.1 deraadt 135: {
1.4 mpech 136: int c;
1.1 deraadt 137:
138: for (;;) {
139: c = getchar();
140: if (c == EOF)
141: return;
142: if (c != 'e') {
143: putchar(c);
144: continue;
145: }
146: if (match("error(")) {
147: printf("error(");
148: c = getchar();
149: if (c != '"')
150: putchar(c);
151: else
152: copystr();
153: }
154: }
155: }
156:
1.3 deraadt 157: int
1.7 millert 158: match(char *ocp)
1.1 deraadt 159: {
1.4 mpech 160: char *cp;
161: int c;
1.1 deraadt 162:
163: for (cp = ocp + 1; *cp; cp++) {
164: c = getchar();
165: if (c != *cp) {
166: while (ocp < cp)
167: putchar(*ocp++);
168: ungetchar(c);
169: return (0);
170: }
171: }
172: return (1);
173: }
174:
1.3 deraadt 175: void
1.7 millert 176: copystr(void)
1.1 deraadt 177: {
1.4 mpech 178: int c, ch;
1.1 deraadt 179: char buf[512];
1.4 mpech 180: char *cp = buf;
1.1 deraadt 181:
182: for (;;) {
183: c = getchar();
184: if (c == EOF)
185: break;
186: switch (c) {
187:
188: case '"':
189: *cp++ = 0;
190: goto out;
191: case '\\':
192: c = getchar();
193: switch (c) {
194:
195: case 'b':
196: c = '\b';
197: break;
198: case 't':
199: c = '\t';
200: break;
201: case 'r':
202: c = '\r';
203: break;
204: case 'n':
205: c = '\n';
206: break;
207: case '\n':
208: continue;
209: case 'f':
210: c = '\f';
211: break;
212: case '0':
213: c = 0;
214: break;
215: case '\\':
216: break;
217: default:
218: if (!octdigit(c))
219: break;
220: c -= '0';
221: ch = getchar();
222: if (!octdigit(ch))
223: break;
224: c <<= 7, c += ch - '0';
225: ch = getchar();
226: if (!octdigit(ch))
227: break;
228: c <<= 3, c+= ch - '0', ch = -1;
229: break;
230: }
231: }
232: *cp++ = c;
233: }
234: out:
235: *cp = 0;
1.3 deraadt 236: printf("%d", hashit(buf, 1, 0));
1.1 deraadt 237: }
238:
1.3 deraadt 239: int
1.7 millert 240: octdigit(char c)
1.1 deraadt 241: {
242:
243: return (c >= '0' && c <= '7');
244: }
245:
1.3 deraadt 246: void
1.7 millert 247: inithash(void)
1.1 deraadt 248: {
249: char buf[512];
250: int mesgpt = 0;
251:
252: rewind(mesgread);
1.3 deraadt 253: while (fgetNUL(buf, sizeof buf, mesgread) != 0) {
1.1 deraadt 254: hashit(buf, 0, mesgpt);
255: mesgpt += strlen(buf) + 2;
256: }
257: }
258:
259: #define NBUCKETS 511
260:
261: struct hash {
262: long hval;
1.6 deraadt 263: unsigned int hpt;
1.1 deraadt 264: struct hash *hnext;
265: } *bucket[NBUCKETS];
266:
1.6 deraadt 267: unsigned int
1.7 millert 268: hashit(char *str, char really, unsigned int fakept)
1.1 deraadt 269: {
270: int i;
1.4 mpech 271: struct hash *hp;
1.1 deraadt 272: char buf[512];
273: long hashval = 0;
1.4 mpech 274: char *cp;
1.1 deraadt 275:
276: if (really)
277: fflush(mesgwrite);
278: for (cp = str; *cp;)
279: hashval = (hashval << 1) + *cp++;
280: i = hashval % NBUCKETS;
281: if (i < 0)
282: i += NBUCKETS;
283: if (really != 0)
284: for (hp = bucket[i]; hp != 0; hp = hp->hnext)
285: if (hp->hval == hashval) {
286: fseek(mesgread, (long) hp->hpt, 0);
287: fgetNUL(buf, sizeof buf, mesgread);
1.7 millert 288: #ifdef DEBUG
1.1 deraadt 289: fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf);
1.7 millert 290: #endif
1.1 deraadt 291: if (strcmp(buf, str) == 0)
292: break;
293: }
294: if (!really || hp == 0) {
1.7 millert 295: if ((hp = (struct hash *) calloc(1, sizeof *hp)) == NULL)
296: err(1, "calloc");
1.1 deraadt 297: hp->hnext = bucket[i];
298: hp->hval = hashval;
299: hp->hpt = really ? ftell(mesgwrite) : fakept;
300: if (really) {
301: fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite);
302: fwrite("\n", sizeof (char), 1, mesgwrite);
303: }
304: bucket[i] = hp;
305: }
1.7 millert 306: #ifdef DEBUG
1.1 deraadt 307: fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt);
1.7 millert 308: #endif
1.1 deraadt 309: return (hp->hpt);
310: }
311:
1.3 deraadt 312: int
1.7 millert 313: fgetNUL(char *obuf, int rmdr, FILE *file)
1.1 deraadt 314: {
1.4 mpech 315: int c;
316: char *buf = obuf;
1.1 deraadt 317:
318: while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
319: *buf++ = c;
320: *buf++ = 0;
321: getc(file);
1.3 deraadt 322: return ((feof(file) || ferror(file)) ? 0 : 1);
1.7 millert 323: }
324:
325: __dead void
326: usage(void)
327: {
328: extern char *__progname;
329:
330: fprintf(stderr, "usage: %s [-] mesgfile prefix file ...\n", __progname);
331: exit(1);
1.1 deraadt 332: }