2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)mkstr.c 8.1 (Berkeley) 6/6/93";
44 static const char rcsid[] =
53 #define ungetchar(c) ungetc(c, stdin)
56 * mkstr - create a string error message file by massaging C source
58 * Bill Joy UCB August 1977
60 * Modified March 1978 to hash old messages to be able to recompile
61 * without addding messages to the message file (usually)
63 * Based on an earlier program conceived by Bill Joy and Chuck Haley
65 * Program to create a string error message file
66 * from a group of C programs. Arguments are the name
67 * of the file where the strings are to be placed, the
68 * prefix of the new files where the processed source text
69 * is to be placed, and the files to be processed.
71 * The program looks for 'error("' in the source stream.
72 * Whenever it finds this, the following characters from the '"'
73 * to a '"' are replaced by 'seekpt' where seekpt is a
74 * pointer into the error message file.
75 * If the '(' is not immediately followed by a '"' no change occurs.
77 * The optional '-' causes strings to be added at the end of the
78 * existing error message file for recompilation of single routines.
81 FILE *mesgread, *mesgwrite;
84 void copystr __P((void));
85 int fgetNUL __P((char *, int, FILE *));
86 unsigned hashit __P((char *, char, unsigned));
87 void inithash __P((void));
88 int match __P((const char *));
89 int octdigit __P((char));
90 void process __P((void));
91 static void usage __P((void));
101 if (argc > 1 && argv[0][0] == '-')
102 addon++, argc--, argv++;
105 mesgwrite = fopen(argv[0], addon ? "a" : "w");
106 if (mesgwrite == NULL)
107 err(1, "%s", argv[0]);
108 mesgread = fopen(argv[0], "r");
109 if (mesgread == NULL)
110 err(1, "%s", argv[0]);
113 strcpy(name, argv[0]);
114 np = name + strlen(name);
118 if (freopen(name, "w", stdout) == NULL)
120 if (freopen(argv[0], "r", stdin) == NULL)
121 err(1, "%s", argv[0]);
131 fprintf(stderr, "usage: mkstr [ - ] mesgfile prefix file ...\n");
148 if (match("error(")) {
166 for (cp = ocp + 1; *cp; cp++) {
183 register char *cp = buf;
227 c <<= 7, c += ch - '0';
231 c <<= 3, c+= ch - '0', ch = -1;
239 printf("%d", hashit(buf, 1, NULL));
247 return (c >= '0' && c <= '7');
257 while (fgetNUL(buf, sizeof buf, mesgread) != 0) {
258 hashit(buf, 0, mesgpt);
259 mesgpt += strlen(buf) + 2;
272 hashit(str, really, fakept)
278 register struct hash *hp;
286 hashval = (hashval << 1) + *cp++;
287 i = hashval % NBUCKETS;
291 for (hp = bucket[i]; hp != 0; hp = hp->hnext)
292 if (hp->hval == hashval) {
293 fseek(mesgread, (long) hp->hpt, 0);
294 fgetNUL(buf, sizeof buf, mesgread);
296 fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf);
298 if (strcmp(buf, str) == 0)
301 if (!really || hp == 0) {
302 hp = (struct hash *) calloc(1, sizeof *hp);
303 hp->hnext = bucket[i];
305 hp->hpt = really ? ftell(mesgwrite) : fakept;
307 fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite);
308 fwrite("\n", sizeof (char), 1, mesgwrite);
313 fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt);
318 #include <sys/types.h>
319 #include <sys/stat.h>
322 fgetNUL(obuf, rmdr, file)
328 register char *buf = obuf;
330 while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
334 return ((feof(file) || ferror(file)) ? 0 : 1);