Annotation of src/usr.bin/mkstr/mkstr.c, Revision 1.7
1.7 ! millert 1: /* $OpenBSD: mkstr.c,v 1.6 2002/05/27 03:14:22 deraadt 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
1.7 ! millert 38: static const char copyright[] =
1.1 deraadt 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
1.7 ! millert 45: static const char sccsid[] = "@(#)mkstr.c 8.1 (Berkeley) 6/6/93";
1.1 deraadt 46: #else
1.7 ! millert 47: static const char rcsid[] = "$OpenBSD: mkstr.c,v 1.6 2002/05/27 03:14:22 deraadt Exp $";
1.1 deraadt 48: #endif
49: #endif /* not lint */
50:
1.7 ! millert 51: #include <sys/param.h>
1.3 deraadt 52: #include <sys/stat.h>
1.7 ! millert 53:
! 54: #include <err.h>
1.1 deraadt 55: #include <stdio.h>
56: #include <stdlib.h>
57: #include <string.h>
58:
59: #define ungetchar(c) ungetc(c, stdin)
60:
61: /*
62: * mkstr - create a string error message file by massaging C source
63: *
64: * Bill Joy UCB August 1977
65: *
66: * Modified March 1978 to hash old messages to be able to recompile
67: * without addding messages to the message file (usually)
68: *
69: * Based on an earlier program conceived by Bill Joy and Chuck Haley
70: *
71: * Program to create a string error message file
72: * from a group of C programs. Arguments are the name
73: * of the file where the strings are to be placed, the
74: * prefix of the new files where the processed source text
75: * is to be placed, and the files to be processed.
76: *
77: * The program looks for 'error("' in the source stream.
78: * Whenever it finds this, the following characters from the '"'
79: * to a '"' are replaced by 'seekpt' where seekpt is a
80: * pointer into the error message file.
81: * If the '(' is not immediately followed by a '"' no change occurs.
82: *
83: * The optional '-' causes strings to be added at the end of the
84: * existing error message file for recompilation of single routines.
85: */
86:
87:
88: FILE *mesgread, *mesgwrite;
1.7 ! millert 89: char name[MAXPATHLEN], *np;
1.1 deraadt 90:
1.5 millert 91: void inithash(void);
92: void process(void);
93: int match(char *);
94: void copystr(void);
95: int octdigit(char);
1.7 ! millert 96: unsigned int hashit(char *, char, unsigned int);
1.5 millert 97: int fgetNUL(char *, int, FILE *);
1.7 ! millert 98: __dead void usage(void);
1.3 deraadt 99:
100: int
1.7 ! millert 101: main(int argc, char **argv)
1.1 deraadt 102: {
1.7 ! millert 103: size_t n;
1.1 deraadt 104: char addon = 0;
105:
1.7 ! millert 106: argc--, argv++;
1.1 deraadt 107: if (argc > 1 && argv[0][0] == '-')
108: addon++, argc--, argv++;
109: if (argc < 3)
1.7 ! millert 110: usage();
1.1 deraadt 111: mesgwrite = fopen(argv[0], addon ? "a" : "w");
112: if (mesgwrite == NULL)
113: perror(argv[0]), exit(1);
114: mesgread = fopen(argv[0], "r");
115: if (mesgread == NULL)
116: perror(argv[0]), exit(1);
117: inithash();
118: argc--, argv++;
1.7 ! millert 119: if ((n = strlcpy(name, argv[0], sizeof(name))) >= sizeof(name))
! 120: errx(1, "%s too long", argv[0]);
! 121: np = name + n;
1.1 deraadt 122: argc--, argv++;
123: do {
1.7 ! millert 124: if (strlcpy(np, argv[0], sizeof(name) - n) >=
! 125: sizeof(name) - n)
! 126: errx(1, "%s too long", argv[0]);
1.1 deraadt 127: if (freopen(name, "w", stdout) == NULL)
128: perror(name), exit(1);
129: if (freopen(argv[0], "r", stdin) == NULL)
130: perror(argv[0]), exit(1);
131: process();
132: argc--, argv++;
133: } while (argc > 0);
1.7 ! millert 134: exit (0);
1.1 deraadt 135: }
136:
1.3 deraadt 137: void
1.7 ! millert 138: process(void)
1.1 deraadt 139: {
1.4 mpech 140: int c;
1.1 deraadt 141:
142: for (;;) {
143: c = getchar();
144: if (c == EOF)
145: return;
146: if (c != 'e') {
147: putchar(c);
148: continue;
149: }
150: if (match("error(")) {
151: printf("error(");
152: c = getchar();
153: if (c != '"')
154: putchar(c);
155: else
156: copystr();
157: }
158: }
159: }
160:
1.3 deraadt 161: int
1.7 ! millert 162: match(char *ocp)
1.1 deraadt 163: {
1.4 mpech 164: char *cp;
165: int c;
1.1 deraadt 166:
167: for (cp = ocp + 1; *cp; cp++) {
168: c = getchar();
169: if (c != *cp) {
170: while (ocp < cp)
171: putchar(*ocp++);
172: ungetchar(c);
173: return (0);
174: }
175: }
176: return (1);
177: }
178:
1.3 deraadt 179: void
1.7 ! millert 180: copystr(void)
1.1 deraadt 181: {
1.4 mpech 182: int c, ch;
1.1 deraadt 183: char buf[512];
1.4 mpech 184: char *cp = buf;
1.1 deraadt 185:
186: for (;;) {
187: c = getchar();
188: if (c == EOF)
189: break;
190: switch (c) {
191:
192: case '"':
193: *cp++ = 0;
194: goto out;
195: case '\\':
196: c = getchar();
197: switch (c) {
198:
199: case 'b':
200: c = '\b';
201: break;
202: case 't':
203: c = '\t';
204: break;
205: case 'r':
206: c = '\r';
207: break;
208: case 'n':
209: c = '\n';
210: break;
211: case '\n':
212: continue;
213: case 'f':
214: c = '\f';
215: break;
216: case '0':
217: c = 0;
218: break;
219: case '\\':
220: break;
221: default:
222: if (!octdigit(c))
223: break;
224: c -= '0';
225: ch = getchar();
226: if (!octdigit(ch))
227: break;
228: c <<= 7, c += ch - '0';
229: ch = getchar();
230: if (!octdigit(ch))
231: break;
232: c <<= 3, c+= ch - '0', ch = -1;
233: break;
234: }
235: }
236: *cp++ = c;
237: }
238: out:
239: *cp = 0;
1.3 deraadt 240: printf("%d", hashit(buf, 1, 0));
1.1 deraadt 241: }
242:
1.3 deraadt 243: int
1.7 ! millert 244: octdigit(char c)
1.1 deraadt 245: {
246:
247: return (c >= '0' && c <= '7');
248: }
249:
1.3 deraadt 250: void
1.7 ! millert 251: inithash(void)
1.1 deraadt 252: {
253: char buf[512];
254: int mesgpt = 0;
255:
256: rewind(mesgread);
1.3 deraadt 257: while (fgetNUL(buf, sizeof buf, mesgread) != 0) {
1.1 deraadt 258: hashit(buf, 0, mesgpt);
259: mesgpt += strlen(buf) + 2;
260: }
261: }
262:
263: #define NBUCKETS 511
264:
265: struct hash {
266: long hval;
1.6 deraadt 267: unsigned int hpt;
1.1 deraadt 268: struct hash *hnext;
269: } *bucket[NBUCKETS];
270:
1.6 deraadt 271: unsigned int
1.7 ! millert 272: hashit(char *str, char really, unsigned int fakept)
1.1 deraadt 273: {
274: int i;
1.4 mpech 275: struct hash *hp;
1.1 deraadt 276: char buf[512];
277: long hashval = 0;
1.4 mpech 278: char *cp;
1.1 deraadt 279:
280: if (really)
281: fflush(mesgwrite);
282: for (cp = str; *cp;)
283: hashval = (hashval << 1) + *cp++;
284: i = hashval % NBUCKETS;
285: if (i < 0)
286: i += NBUCKETS;
287: if (really != 0)
288: for (hp = bucket[i]; hp != 0; hp = hp->hnext)
289: if (hp->hval == hashval) {
290: fseek(mesgread, (long) hp->hpt, 0);
291: fgetNUL(buf, sizeof buf, mesgread);
1.7 ! millert 292: #ifdef DEBUG
1.1 deraadt 293: fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf);
1.7 ! millert 294: #endif
1.1 deraadt 295: if (strcmp(buf, str) == 0)
296: break;
297: }
298: if (!really || hp == 0) {
1.7 ! millert 299: if ((hp = (struct hash *) calloc(1, sizeof *hp)) == NULL)
! 300: err(1, "calloc");
1.1 deraadt 301: hp->hnext = bucket[i];
302: hp->hval = hashval;
303: hp->hpt = really ? ftell(mesgwrite) : fakept;
304: if (really) {
305: fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite);
306: fwrite("\n", sizeof (char), 1, mesgwrite);
307: }
308: bucket[i] = hp;
309: }
1.7 ! millert 310: #ifdef DEBUG
1.1 deraadt 311: fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt);
1.7 ! millert 312: #endif
1.1 deraadt 313: return (hp->hpt);
314: }
315:
1.3 deraadt 316: int
1.7 ! millert 317: fgetNUL(char *obuf, int rmdr, FILE *file)
1.1 deraadt 318: {
1.4 mpech 319: int c;
320: char *buf = obuf;
1.1 deraadt 321:
322: while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
323: *buf++ = c;
324: *buf++ = 0;
325: getc(file);
1.3 deraadt 326: return ((feof(file) || ferror(file)) ? 0 : 1);
1.7 ! millert 327: }
! 328:
! 329: __dead void
! 330: usage(void)
! 331: {
! 332: extern char *__progname;
! 333:
! 334: fprintf(stderr, "usage: %s [-] mesgfile prefix file ...\n", __progname);
! 335: exit(1);
1.1 deraadt 336: }