xref: /freebsd/usr.bin/mkstr/mkstr.c (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)mkstr.c	8.1 (Berkeley) 6/6/93";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #define	ungetchar(c)	ungetc(c, stdin)
54 
55 /*
56  * mkstr - create a string error message file by massaging C source
57  *
58  * Bill Joy UCB August 1977
59  *
60  * Modified March 1978 to hash old messages to be able to recompile
61  * without addding messages to the message file (usually)
62  *
63  * Based on an earlier program conceived by Bill Joy and Chuck Haley
64  *
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.
70  *
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.
76  *
77  * The optional '-' causes strings to be added at the end of the
78  * existing error message file for recompilation of single routines.
79  */
80 
81 static FILE	*mesgread, *mesgwrite;
82 static char	name[100], *np;
83 
84 void copystr(void);
85 int fgetNUL(char *, int, FILE *);
86 unsigned hashit(char *, int, unsigned);
87 void inithash(void);
88 int match(const char *);
89 int octdigit(char);
90 void process(void);
91 void usage(void);
92 
93 int
94 main(int argc, char *argv[])
95 {
96 	char addon = 0;
97 	size_t namelen;
98 
99 	argc--, argv++;
100 	if (argc > 1 && argv[0][0] == '-')
101 		addon++, argc--, argv++;
102 	if (argc < 3)
103 		usage();
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]);
110 	inithash();
111 	argc--, argv++;
112 	namelen = strlcpy(name, argv[0], sizeof(name));
113 	if (namelen >= sizeof(name)) {
114 		errno = ENAMETOOLONG;
115 		err(1, "%s", argv[0]);
116 	}
117 	np = name + namelen;
118 	argc--, argv++;
119 	do {
120 		if (strlcpy(np, argv[0], sizeof(name) - namelen) >=
121 		    sizeof(name) - namelen) {
122 			errno = ENAMETOOLONG;
123 			err(1, "%s%s", name, argv[0]);
124 		}
125 		if (freopen(name, "w", stdout) == NULL)
126 			err(1, "%s", name);
127 		if (freopen(argv[0], "r", stdin) == NULL)
128 			err(1, "%s", argv[0]);
129 		process();
130 		argc--, argv++;
131 	} while (argc > 0);
132 	exit(0);
133 }
134 
135 void
136 usage(void)
137 {
138 	fprintf(stderr, "usage: mkstr [-] mesgfile prefix file ...\n");
139 	exit(1);
140 }
141 
142 void
143 process(void)
144 {
145 	int c;
146 
147 	for (;;) {
148 		c = getchar();
149 		if (c == EOF)
150 			return;
151 		if (c != 'e') {
152 			putchar(c);
153 			continue;
154 		}
155 		if (match("error(")) {
156 			printf("error(");
157 			c = getchar();
158 			if (c != '"')
159 				putchar(c);
160 			else
161 				copystr();
162 		}
163 	}
164 }
165 
166 int
167 match(const char *ocp)
168 {
169 	const char *cp;
170 	int c;
171 
172 	for (cp = ocp + 1; *cp; cp++) {
173 		c = getchar();
174 		if (c != *cp) {
175 			while (ocp < cp)
176 				putchar(*ocp++);
177 			ungetchar(c);
178 			return (0);
179 		}
180 	}
181 	return (1);
182 }
183 
184 void
185 copystr(void)
186 {
187 	int c, ch;
188 	char buf[512];
189 	char *cp = buf;
190 
191 	for (;;) {
192 		if (cp == buf + sizeof(buf) - 2)
193 			errx(1, "message too long");
194 		c = getchar();
195 		if (c == EOF)
196 			break;
197 		switch (c) {
198 
199 		case '"':
200 			*cp++ = 0;
201 			goto out;
202 		case '\\':
203 			c = getchar();
204 			switch (c) {
205 
206 			case 'b':
207 				c = '\b';
208 				break;
209 			case 't':
210 				c = '\t';
211 				break;
212 			case 'r':
213 				c = '\r';
214 				break;
215 			case 'n':
216 				c = '\n';
217 				break;
218 			case '\n':
219 				continue;
220 			case 'f':
221 				c = '\f';
222 				break;
223 			case '0':
224 				c = 0;
225 				break;
226 			case '\\':
227 				break;
228 			default:
229 				if (!octdigit(c))
230 					break;
231 				c -= '0';
232 				ch = getchar();
233 				if (!octdigit(ch))
234 					break;
235 				c <<= 7, c += ch - '0';
236 				ch = getchar();
237 				if (!octdigit(ch))
238 					break;
239 				c <<= 3, c+= ch - '0', ch = -1;
240 				break;
241 			}
242 		}
243 		*cp++ = c;
244 	}
245 out:
246 	*cp = 0;
247 	printf("%d", hashit(buf, 1, 0));
248 }
249 
250 int
251 octdigit(char c)
252 {
253 
254 	return (c >= '0' && c <= '7');
255 }
256 
257 void
258 inithash(void)
259 {
260 	char buf[512];
261 	int mesgpt = 0;
262 
263 	rewind(mesgread);
264 	while (fgetNUL(buf, sizeof buf, mesgread) != 0) {
265 		hashit(buf, 0, mesgpt);
266 		mesgpt += strlen(buf) + 2;
267 	}
268 }
269 
270 #define	NBUCKETS	511
271 
272 static struct	hash {
273 	long	hval;
274 	unsigned hpt;
275 	struct	hash *hnext;
276 } *bucket[NBUCKETS];
277 
278 unsigned
279 hashit(char *str, int really, unsigned fakept)
280 {
281 	int i;
282 	struct hash *hp;
283 	char buf[512];
284 	long hashval = 0;
285 	char *cp;
286 
287 	if (really)
288 		fflush(mesgwrite);
289 	for (cp = str; *cp;)
290 		hashval = (hashval << 1) + *cp++;
291 	i = hashval % NBUCKETS;
292 	if (i < 0)
293 		i += NBUCKETS;
294 	if (really != 0)
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);
299 /*
300 			fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf);
301 */
302 			if (strcmp(buf, str) == 0)
303 				break;
304 		}
305 	if (!really || hp == 0) {
306 		hp = (struct hash *) calloc(1, sizeof *hp);
307 		if (hp == NULL)
308 			err(1, NULL);
309 		hp->hnext = bucket[i];
310 		hp->hval = hashval;
311 		hp->hpt = really ? ftell(mesgwrite) : fakept;
312 		if (really) {
313 			fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite);
314 			fwrite("\n", sizeof (char), 1, mesgwrite);
315 		}
316 		bucket[i] = hp;
317 	}
318 /*
319 	fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt);
320 */
321 	return (hp->hpt);
322 }
323 
324 int
325 fgetNUL(char *obuf, int rmdr, FILE *file)
326 {
327 	int c;
328 	char *buf = obuf;
329 
330 	while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
331 		*buf++ = c;
332 	*buf++ = 0;
333 	getc(file);
334 	return ((feof(file) || ferror(file)) ? 0 : 1);
335 }
336