2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)mkstr.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
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 static FILE *mesgread, *mesgwrite;
82 static char name[100], *np;
85 int fgetNUL(char *, int, FILE *);
86 unsigned hashit(char *, int, unsigned);
88 int match(const char *);
94 main(int argc, char *argv[])
100 if (argc > 1 && argv[0][0] == '-')
101 addon++, argc--, argv++;
104 mesgwrite = fopen(argv[0], addon ? "a" : "w");
105 if (mesgwrite == NULL)
106 err(1, "%s", argv[0]);
107 mesgread = fopen(argv[0], "r");
108 if (mesgread == NULL)
109 err(1, "%s", argv[0]);
112 namelen = strlcpy(name, argv[0], sizeof(name));
113 if (namelen >= sizeof(name)) {
114 errno = ENAMETOOLONG;
115 err(1, "%s", argv[0]);
120 if (strlcpy(np, argv[0], sizeof(name) - namelen) >=
121 sizeof(name) - namelen) {
122 errno = ENAMETOOLONG;
123 err(1, "%s%s", name, argv[0]);
125 if (freopen(name, "w", stdout) == NULL)
127 if (freopen(argv[0], "r", stdin) == NULL)
128 err(1, "%s", argv[0]);
138 fprintf(stderr, "usage: mkstr [-] mesgfile prefix file ...\n");
155 if (match("error(")) {
167 match(const char *ocp)
172 for (cp = ocp + 1; *cp; cp++) {
192 if (cp == buf + sizeof(buf) - 2)
193 errx(1, "message too long");
235 c <<= 7, c += ch - '0';
239 c <<= 3, c+= ch - '0', ch = -1;
247 printf("%d", hashit(buf, 1, 0));
254 return (c >= '0' && c <= '7');
264 while (fgetNUL(buf, sizeof buf, mesgread) != 0) {
265 hashit(buf, 0, mesgpt);
266 mesgpt += strlen(buf) + 2;
279 hashit(char *str, int really, unsigned fakept)
290 hashval = (hashval << 1) + *cp++;
291 i = hashval % NBUCKETS;
295 for (hp = bucket[i]; hp != 0; hp = hp->hnext)
296 if (hp->hval == hashval) {
297 fseek(mesgread, (long) hp->hpt, 0);
298 fgetNUL(buf, sizeof buf, mesgread);
300 fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf);
302 if (strcmp(buf, str) == 0)
305 if (!really || hp == 0) {
306 hp = (struct hash *) calloc(1, sizeof *hp);
309 hp->hnext = bucket[i];
311 hp->hpt = really ? ftell(mesgwrite) : fakept;
313 fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite);
314 fwrite("\n", sizeof (char), 1, mesgwrite);
319 fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt);
325 fgetNUL(char *obuf, int rmdr, FILE *file)
330 while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
334 return ((feof(file) || ferror(file)) ? 0 : 1);