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