Remove some unneeded .Pp's.
[dragonfly.git] / usr.bin / mkstr / mkstr.c
blob13934407c2b403c5b7daba11800471f473dfc8f8
1 /*
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
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.
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.
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 * $DragonFly: src/usr.bin/mkstr/mkstr.c,v 1.3 2008/06/05 18:06:33 swildner Exp $
39 #include <err.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
45 #define ungetchar(c) ungetc(c, stdin)
48 * mkstr - create a string error message file by massaging C source
50 * Bill Joy UCB August 1977
52 * Modified March 1978 to hash old messages to be able to recompile
53 * without addding messages to the message file (usually)
55 * Based on an earlier program conceived by Bill Joy and Chuck Haley
57 * Program to create a string error message file
58 * from a group of C programs. Arguments are the name
59 * of the file where the strings are to be placed, the
60 * prefix of the new files where the processed source text
61 * is to be placed, and the files to be processed.
63 * The program looks for 'error("' in the source stream.
64 * Whenever it finds this, the following characters from the '"'
65 * to a '"' are replaced by 'seekpt' where seekpt is a
66 * pointer into the error message file.
67 * If the '(' is not immediately followed by a '"' no change occurs.
69 * The optional '-' causes strings to be added at the end of the
70 * existing error message file for recompilation of single routines.
73 FILE *mesgread, *mesgwrite;
74 char name[100], *np;
76 void copystr(void);
77 int fgetNUL(char *, int, FILE *);
78 unsigned hashit(char *, int, unsigned);
79 void inithash(void);
80 int match(const char *);
81 int octdigit(char);
82 void process(void);
83 void usage(void);
85 int
86 main(int argc, char *argv[])
88 char addon = 0;
89 size_t namelen;
91 argc--, argv++;
92 if (argc > 1 && argv[0][0] == '-')
93 addon++, argc--, argv++;
94 if (argc < 3)
95 usage();
96 mesgwrite = fopen(argv[0], addon ? "a" : "w");
97 if (mesgwrite == NULL)
98 err(1, "%s", argv[0]);
99 mesgread = fopen(argv[0], "r");
100 if (mesgread == NULL)
101 err(1, "%s", argv[0]);
102 inithash();
103 argc--, argv++;
104 namelen = strlcpy(name, argv[0], sizeof(name));
105 if (namelen >= sizeof(name)) {
106 errno = ENAMETOOLONG;
107 err(1, "%s", argv[0]);
109 np = name + namelen;
110 argc--, argv++;
111 do {
112 if (strlcpy(np, argv[0], sizeof(name) - namelen) >=
113 sizeof(name) - namelen) {
114 errno = ENAMETOOLONG;
115 err(1, "%s%s", name, argv[0]);
117 if (freopen(name, "w", stdout) == NULL)
118 err(1, "%s", name);
119 if (freopen(argv[0], "r", stdin) == NULL)
120 err(1, "%s", argv[0]);
121 process();
122 argc--, argv++;
123 } while (argc > 0);
124 exit(0);
127 void
128 usage(void)
130 fprintf(stderr, "usage: mkstr [-] mesgfile prefix file ...\n");
131 exit(1);
134 void
135 process(void)
137 int c;
139 for (;;) {
140 c = getchar();
141 if (c == EOF)
142 return;
143 if (c != 'e') {
144 putchar(c);
145 continue;
147 if (match("error(")) {
148 printf("error(");
149 c = getchar();
150 if (c != '"')
151 putchar(c);
152 else
153 copystr();
159 match(const char *ocp)
161 const char *cp;
162 int c;
164 for (cp = ocp + 1; *cp; cp++) {
165 c = getchar();
166 if (c != *cp) {
167 while (ocp < cp)
168 putchar(*ocp++);
169 ungetchar(c);
170 return (0);
173 return (1);
176 void
177 copystr(void)
179 int c, ch;
180 char buf[512];
181 char *cp = buf;
183 for (;;) {
184 if (cp == buf + sizeof(buf) - 2)
185 errx(1, "message too long");
186 c = getchar();
187 if (c == EOF)
188 break;
189 switch (c) {
191 case '"':
192 *cp++ = 0;
193 goto out;
194 case '\\':
195 c = getchar();
196 switch (c) {
198 case 'b':
199 c = '\b';
200 break;
201 case 't':
202 c = '\t';
203 break;
204 case 'r':
205 c = '\r';
206 break;
207 case 'n':
208 c = '\n';
209 break;
210 case '\n':
211 continue;
212 case 'f':
213 c = '\f';
214 break;
215 case '0':
216 c = 0;
217 break;
218 case '\\':
219 break;
220 default:
221 if (!octdigit(c))
222 break;
223 c -= '0';
224 ch = getchar();
225 if (!octdigit(ch))
226 break;
227 c <<= 7, c += ch - '0';
228 ch = getchar();
229 if (!octdigit(ch))
230 break;
231 c <<= 3, c+= ch - '0', ch = -1;
232 break;
235 *cp++ = c;
237 out:
238 *cp = 0;
239 printf("%d", hashit(buf, 1, 0));
243 octdigit(char c)
246 return (c >= '0' && c <= '7');
249 void
250 inithash(void)
252 char buf[512];
253 int mesgpt = 0;
255 rewind(mesgread);
256 while (fgetNUL(buf, sizeof buf, mesgread) != 0) {
257 hashit(buf, 0, mesgpt);
258 mesgpt += strlen(buf) + 2;
262 #define NBUCKETS 511
264 struct hash {
265 long hval;
266 unsigned hpt;
267 struct hash *hnext;
268 } *bucket[NBUCKETS];
270 unsigned
271 hashit(char *str, int really, unsigned fakept)
273 int i;
274 struct hash *hp;
275 char buf[512];
276 long hashval = 0;
277 char *cp;
279 if (really)
280 fflush(mesgwrite);
281 for (cp = str; *cp;)
282 hashval = (hashval << 1) + *cp++;
283 i = hashval % NBUCKETS;
284 if (i < 0)
285 i += NBUCKETS;
286 if (really != 0)
287 for (hp = bucket[i]; hp != 0; hp = hp->hnext)
288 if (hp->hval == hashval) {
289 fseek(mesgread, (long) hp->hpt, 0);
290 fgetNUL(buf, sizeof buf, mesgread);
292 fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf);
294 if (strcmp(buf, str) == 0)
295 break;
297 if (!really || hp == 0) {
298 hp = (struct hash *) calloc(1, sizeof *hp);
299 if (hp == NULL)
300 err(1, NULL);
301 hp->hnext = bucket[i];
302 hp->hval = hashval;
303 hp->hpt = really ? ftell(mesgwrite) : fakept;
304 if (really) {
305 fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite);
306 fwrite("\n", sizeof (char), 1, mesgwrite);
308 bucket[i] = hp;
311 fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt);
313 return (hp->hpt);
317 fgetNUL(char *obuf, int rmdr, FILE *file)
319 int c;
320 char *buf = obuf;
322 while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
323 *buf++ = c;
324 *buf++ = 0;
325 getc(file);
326 return ((feof(file) || ferror(file)) ? 0 : 1);