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