Annotation of src/usr.bin/mkstr/mkstr.c, Revision 1.2
1.2 ! deraadt 1: /* $OpenBSD: mkstr.c,v 1.4 1995/09/28 06:22:20 tls 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.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1980, 1993\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)mkstr.c 8.1 (Berkeley) 6/6/93";
46: #else
1.2 ! deraadt 47: static char rcsid[] = "$OpenBSD: mkstr.c,v 1.4 1995/09/28 06:22:20 tls Exp $";
1.1 deraadt 48: #endif
49: #endif /* not lint */
50:
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;
85: char *progname;
86: char usagestr[] = "usage: %s [ - ] mesgfile prefix file ...\n";
87: char name[100], *np;
88:
89: main(argc, argv)
90: int argc;
91: char *argv[];
92: {
93: char addon = 0;
94:
95: argc--, progname = *argv++;
96: if (argc > 1 && argv[0][0] == '-')
97: addon++, argc--, argv++;
98: if (argc < 3)
99: fprintf(stderr, usagestr, progname), exit(1);
100: mesgwrite = fopen(argv[0], addon ? "a" : "w");
101: if (mesgwrite == NULL)
102: perror(argv[0]), exit(1);
103: mesgread = fopen(argv[0], "r");
104: if (mesgread == NULL)
105: perror(argv[0]), exit(1);
106: inithash();
107: argc--, argv++;
108: strcpy(name, argv[0]);
109: np = name + strlen(name);
110: argc--, argv++;
111: do {
112: strcpy(np, argv[0]);
113: if (freopen(name, "w", stdout) == NULL)
114: perror(name), exit(1);
115: if (freopen(argv[0], "r", stdin) == NULL)
116: perror(argv[0]), exit(1);
117: process();
118: argc--, argv++;
119: } while (argc > 0);
120: exit(0);
121: }
122:
123: process()
124: {
125: register char *cp;
126: register c;
127:
128: for (;;) {
129: c = getchar();
130: if (c == EOF)
131: return;
132: if (c != 'e') {
133: putchar(c);
134: continue;
135: }
136: if (match("error(")) {
137: printf("error(");
138: c = getchar();
139: if (c != '"')
140: putchar(c);
141: else
142: copystr();
143: }
144: }
145: }
146:
147: match(ocp)
148: char *ocp;
149: {
150: register char *cp;
151: register c;
152:
153: for (cp = ocp + 1; *cp; cp++) {
154: c = getchar();
155: if (c != *cp) {
156: while (ocp < cp)
157: putchar(*ocp++);
158: ungetchar(c);
159: return (0);
160: }
161: }
162: return (1);
163: }
164:
165: copystr()
166: {
167: register c, ch;
168: char buf[512];
169: register char *cp = buf;
170:
171: for (;;) {
172: c = getchar();
173: if (c == EOF)
174: break;
175: switch (c) {
176:
177: case '"':
178: *cp++ = 0;
179: goto out;
180: case '\\':
181: c = getchar();
182: switch (c) {
183:
184: case 'b':
185: c = '\b';
186: break;
187: case 't':
188: c = '\t';
189: break;
190: case 'r':
191: c = '\r';
192: break;
193: case 'n':
194: c = '\n';
195: break;
196: case '\n':
197: continue;
198: case 'f':
199: c = '\f';
200: break;
201: case '0':
202: c = 0;
203: break;
204: case '\\':
205: break;
206: default:
207: if (!octdigit(c))
208: break;
209: c -= '0';
210: ch = getchar();
211: if (!octdigit(ch))
212: break;
213: c <<= 7, c += ch - '0';
214: ch = getchar();
215: if (!octdigit(ch))
216: break;
217: c <<= 3, c+= ch - '0', ch = -1;
218: break;
219: }
220: }
221: *cp++ = c;
222: }
223: out:
224: *cp = 0;
225: printf("%d", hashit(buf, 1, NULL));
226: }
227:
228: octdigit(c)
229: char c;
230: {
231:
232: return (c >= '0' && c <= '7');
233: }
234:
235: inithash()
236: {
237: char buf[512];
238: int mesgpt = 0;
239:
240: rewind(mesgread);
241: while (fgetNUL(buf, sizeof buf, mesgread) != NULL) {
242: hashit(buf, 0, mesgpt);
243: mesgpt += strlen(buf) + 2;
244: }
245: }
246:
247: #define NBUCKETS 511
248:
249: struct hash {
250: long hval;
251: unsigned hpt;
252: struct hash *hnext;
253: } *bucket[NBUCKETS];
254:
255: hashit(str, really, fakept)
256: char *str;
257: char really;
258: unsigned fakept;
259: {
260: int i;
261: register struct hash *hp;
262: char buf[512];
263: long hashval = 0;
264: register char *cp;
265:
266: if (really)
267: fflush(mesgwrite);
268: for (cp = str; *cp;)
269: hashval = (hashval << 1) + *cp++;
270: i = hashval % NBUCKETS;
271: if (i < 0)
272: i += NBUCKETS;
273: if (really != 0)
274: for (hp = bucket[i]; hp != 0; hp = hp->hnext)
275: if (hp->hval == hashval) {
276: fseek(mesgread, (long) hp->hpt, 0);
277: fgetNUL(buf, sizeof buf, mesgread);
278: /*
279: fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf);
280: */
281: if (strcmp(buf, str) == 0)
282: break;
283: }
284: if (!really || hp == 0) {
285: hp = (struct hash *) calloc(1, sizeof *hp);
286: hp->hnext = bucket[i];
287: hp->hval = hashval;
288: hp->hpt = really ? ftell(mesgwrite) : fakept;
289: if (really) {
290: fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite);
291: fwrite("\n", sizeof (char), 1, mesgwrite);
292: }
293: bucket[i] = hp;
294: }
295: /*
296: fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt);
297: */
298: return (hp->hpt);
299: }
300:
301: #include <sys/types.h>
302: #include <sys/stat.h>
303:
304: fgetNUL(obuf, rmdr, file)
305: char *obuf;
306: register int rmdr;
307: FILE *file;
308: {
309: register c;
310: register char *buf = obuf;
311:
312: while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
313: *buf++ = c;
314: *buf++ = 0;
315: getc(file);
316: return ((feof(file) || ferror(file)) ? NULL : 1);
317: }