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
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)mkstr.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/mkstr/mkstr.c,v 1.4.2.1 2002/11/16 01:07:42 tjr Exp $
44 #define ungetchar(c) ungetc(c, stdin)
47 * mkstr - create a string error message file by massaging C source
49 * Bill Joy UCB August 1977
51 * Modified March 1978 to hash old messages to be able to recompile
52 * without addding messages to the message file (usually)
54 * Based on an earlier program conceived by Bill Joy and Chuck Haley
56 * Program to create a string error message file
57 * from a group of C programs. Arguments are the name
58 * of the file where the strings are to be placed, the
59 * prefix of the new files where the processed source text
60 * is to be placed, and the files to be processed.
62 * The program looks for 'error("' in the source stream.
63 * Whenever it finds this, the following characters from the '"'
64 * to a '"' are replaced by 'seekpt' where seekpt is a
65 * pointer into the error message file.
66 * If the '(' is not immediately followed by a '"' no change occurs.
68 * The optional '-' causes strings to be added at the end of the
69 * existing error message file for recompilation of single routines.
72 FILE *mesgread, *mesgwrite;
76 int fgetNUL(char *, int, FILE *);
77 unsigned hashit(char *, int, unsigned);
79 int match(const char *);
85 main(int argc, char *argv[])
91 if (argc > 1 && argv[0][0] == '-')
92 addon++, argc--, argv++;
95 mesgwrite = fopen(argv[0], addon ? "a" : "w");
96 if (mesgwrite == NULL)
97 err(1, "%s", argv[0]);
98 mesgread = fopen(argv[0], "r");
100 err(1, "%s", argv[0]);
103 namelen = strlcpy(name, argv[0], sizeof(name));
104 if (namelen >= sizeof(name)) {
105 errno = ENAMETOOLONG;
106 err(1, "%s", argv[0]);
111 if (strlcpy(np, argv[0], sizeof(name) - namelen) >=
112 sizeof(name) - namelen) {
113 errno = ENAMETOOLONG;
114 err(1, "%s%s", name, argv[0]);
116 if (freopen(name, "w", stdout) == NULL)
118 if (freopen(argv[0], "r", stdin) == NULL)
119 err(1, "%s", argv[0]);
129 fprintf(stderr, "usage: mkstr [-] mesgfile prefix file ...\n");
146 if (match("error(")) {
158 match(const char *ocp)
163 for (cp = ocp + 1; *cp; cp++) {
183 if (cp == buf + sizeof(buf) - 2)
184 errx(1, "message too long");
226 c <<= 7, c += ch - '0';
230 c <<= 3, c+= ch - '0', ch = -1;
238 printf("%d", hashit(buf, 1, 0));
245 return (c >= '0' && c <= '7');
255 while (fgetNUL(buf, sizeof buf, mesgread) != 0) {
256 hashit(buf, 0, mesgpt);
257 mesgpt += strlen(buf) + 2;
270 hashit(char *str, int really, unsigned fakept)
281 hashval = (hashval << 1) + *cp++;
282 i = hashval % NBUCKETS;
286 for (hp = bucket[i]; hp != NULL; hp = hp->hnext)
287 if (hp->hval == hashval) {
288 fseek(mesgread, (long) hp->hpt, 0);
289 fgetNUL(buf, sizeof buf, mesgread);
291 fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf);
293 if (strcmp(buf, str) == 0)
296 if (!really || hp == NULL) {
297 hp = (struct hash *) calloc(1, sizeof *hp);
300 hp->hnext = bucket[i];
302 hp->hpt = really ? ftell(mesgwrite) : fakept;
304 fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite);
305 fwrite("\n", sizeof (char), 1, mesgwrite);
310 fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt);
316 fgetNUL(char *obuf, int rmdr, FILE *file)
321 while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
325 return ((feof(file) || ferror(file)) ? 0 : 1);