Apply rev 1.21 from src/lib/libcrypto/man/ssl.3:
[netbsd-mini2440.git] / usr.bin / mkstr / mkstr.c
blobe92fb5b97a521f29186be462061372ac222b878c
1 /* $NetBSD: mkstr.c,v 1.13 2009/04/12 14:28:30 lukem Exp $ */
3 /*
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
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.
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.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)mkstr.c 8.1 (Berkeley) 6/6/93";
41 #else
42 __RCSID("$NetBSD: mkstr.c,v 1.13 2009/04/12 14:28:30 lukem Exp $");
43 #endif
44 #endif /* not lint */
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
50 #define ungetchar(c) ungetc(c, stdin)
53 * mkstr - create a string error message file by massaging C source
55 * Bill Joy UCB August 1977
57 * Modified March 1978 to hash old messages to be able to recompile
58 * without addding messages to the message file (usually)
60 * Based on an earlier program conceived by Bill Joy and Chuck Haley
62 * Program to create a string error message file
63 * from a group of C programs. Arguments are the name
64 * of the file where the strings are to be placed, the
65 * prefix of the new files where the processed source text
66 * is to be placed, and the files to be processed.
68 * The program looks for 'error("' in the source stream.
69 * Whenever it finds this, the following characters from the '"'
70 * to a '"' are replaced by 'seekpt' where seekpt is a
71 * pointer into the error message file.
72 * If the '(' is not immediately followed by a '"' no change occurs.
74 * The optional '-' causes strings to be added at the end of the
75 * existing error message file for recompilation of single routines.
79 FILE *mesgread, *mesgwrite;
80 char *progname;
81 const char usagestr[] = "usage: %s [ - ] mesgfile prefix file ...\n";
82 char name[100], *np;
84 void process __P((void));
85 int main __P((int, char **));
86 int match __P((const char *));
87 int octdigit __P((char));
88 void inithash __P((void));
89 long hashit __P((char *, char, long));
90 void copystr __P((void));
91 int fgetNUL __P((char *, int, FILE *));
93 int
94 main(argc, argv)
95 int argc;
96 char *argv[];
98 char addon = 0;
100 argc--, progname = *argv++;
101 if (argc > 1 && argv[0][0] == '-')
102 addon++, argc--, argv++;
103 if (argc < 3)
104 fprintf(stderr, usagestr, progname), exit(1);
105 mesgwrite = fopen(argv[0], addon ? "a" : "w");
106 if (mesgwrite == NULL)
107 perror(argv[0]), exit(1);
108 mesgread = fopen(argv[0], "r");
109 if (mesgread == NULL)
110 perror(argv[0]), exit(1);
111 inithash();
112 argc--, argv++;
113 strlcpy(name, argv[0], sizeof(name));
114 np = name + strlen(name);
115 argc--, argv++;
116 do {
117 strlcpy(np, argv[0], sizeof(name) - (np - name));
118 if (freopen(name, "w", stdout) == NULL)
119 perror(name), exit(1);
120 if (freopen(argv[0], "r", stdin) == NULL)
121 perror(argv[0]), exit(1);
122 process();
123 argc--, argv++;
124 } while (argc > 0);
125 exit(0);
128 void
129 process()
131 int c;
133 for (;;) {
134 c = getchar();
135 if (c == EOF)
136 return;
137 if (c != 'e') {
138 putchar(c);
139 continue;
141 if (match("error(")) {
142 printf("error(");
143 c = getchar();
144 if (c != '"')
145 putchar(c);
146 else
147 copystr();
153 match(ocp)
154 const char *ocp;
156 const char *cp;
157 int c;
159 for (cp = ocp + 1; *cp; cp++) {
160 c = getchar();
161 if (c != *cp) {
162 while (ocp < cp)
163 putchar(*ocp++);
164 ungetchar(c);
165 return (0);
168 return (1);
171 void
172 copystr()
174 int c, ch;
175 char buf[512];
176 char *cp = buf;
178 for (;;) {
179 c = getchar();
180 if (c == EOF)
181 break;
182 switch (c) {
184 case '"':
185 *cp++ = 0;
186 goto out;
187 case '\\':
188 c = getchar();
189 switch (c) {
191 case 'b':
192 c = '\b';
193 break;
194 case 't':
195 c = '\t';
196 break;
197 case 'r':
198 c = '\r';
199 break;
200 case 'n':
201 c = '\n';
202 break;
203 case '\n':
204 continue;
205 case 'f':
206 c = '\f';
207 break;
208 case '0':
209 c = 0;
210 break;
211 case '\\':
212 break;
213 default:
214 if (!octdigit(c))
215 break;
216 c -= '0';
217 ch = getchar();
218 if (!octdigit(ch))
219 break;
220 c <<= 7, c += ch - '0';
221 ch = getchar();
222 if (!octdigit(ch))
223 break;
224 c <<= 3, c+= ch - '0', ch = -1;
225 break;
228 *cp++ = c;
230 out:
231 *cp = 0;
232 printf("%ld", hashit(buf, 1, 0));
236 octdigit(c)
237 char c;
240 return (c >= '0' && c <= '7');
243 void
244 inithash()
246 char buf[512];
247 long mesgpt = 0;
249 rewind(mesgread);
250 while (fgetNUL(buf, sizeof buf, mesgread) != 0) {
251 hashit(buf, 0, mesgpt);
252 mesgpt += strlen(buf) + 2;
256 #define NBUCKETS 511
258 struct hash {
259 long hval;
260 long hpt;
261 struct hash *hnext;
262 } *bucket[NBUCKETS];
264 long
265 hashit(str, really, fakept)
266 char *str;
267 char really;
268 long fakept;
270 int i;
271 struct hash *hp;
272 char buf[512];
273 long hashval = 0;
274 char *cp;
276 #ifdef __GNUC__
277 hp = NULL; /* XXX gcc */
278 #endif
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, hp->hpt, 0);
290 fgetNUL(buf, sizeof buf, mesgread);
292 fprintf(stderr, "Got (from %ld) %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 hp->hnext = bucket[i];
300 hp->hval = hashval;
301 hp->hpt = really ? ftell(mesgwrite) : fakept;
302 if (really) {
303 fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite);
304 fwrite("\n", sizeof (char), 1, mesgwrite);
306 bucket[i] = hp;
309 fprintf(stderr, "%s hashed to %ld at %ld\n", str, hp->hval, hp->hpt);
311 return (hp->hpt);
314 #include <sys/types.h>
315 #include <sys/stat.h>
318 fgetNUL(obuf, rmdr, file)
319 char *obuf;
320 int rmdr;
321 FILE *file;
323 int c;
324 char *buf = obuf;
326 while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
327 *buf++ = c;
328 *buf++ = 0;
329 getc(file);
330 return ((feof(file) || ferror(file)) ? 0 : 1);