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
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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 static const char copyright[] =
40 "@(#) Copyright (c) 1980, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid[] = "@(#)xstr.c 8.1 (Berkeley) 6/9/93";
48 #include <sys/types.h>
58 #include "pathnames.h"
61 * xstr - extract and hash strings in a C program
67 #define ignore(a) ((void) a)
72 char cstrings[] = "strings";
73 char *strings = cstrings;
79 char lastchr __P((char *));
81 int fgetNUL __P((char *, int, FILE *));
82 int istail __P((char *, char *));
83 int octdigit __P((char));
84 int xgetc __P((FILE *));
86 off_t hashit __P((char *, int));
87 off_t yankstr __P((char **));
89 static void usage __P((void));
91 void flushsh __P((void));
92 void found __P((int, off_t, char *));
93 void inithash __P((void));
94 void onintr __P((int));
95 void process __P((const char *));
96 void prstr __P((char *));
97 void xsdotc __P((void));
106 while ((c = getopt(argc, argv, "-cv")) != -1)
123 if (signal(SIGINT, SIG_IGN) == SIG_DFL)
124 signal(SIGINT, onintr);
125 if (cflg || (argc == 0 && !readstd))
128 strings = mktemp(strdup(_PATH_TMP));
129 while (readstd || argc > 0) {
130 if (freopen("x.c", "w", stdout) == NULL)
132 if (!readstd && freopen(argv[0], "r", stdin) == NULL)
133 err(2, "%s", argv[0]);
143 if (strings[0] == '/')
144 ignore(unlink(strings));
151 fprintf(stderr, "usage: xstr [-v] [-c] [-] [name ...]\n");
155 char linebuf[BUFSIZ];
166 printf("extern char\txstr[];\n");
168 if (fgets(linebuf, sizeof linebuf, stdin) == NULL) {
173 if (linebuf[0] == '#') {
174 if (linebuf[1] == ' ' && isdigit(linebuf[2]))
175 printf("#line%s", &linebuf[1]);
177 printf("%s", linebuf);
180 for (cp = linebuf; (c = *cp++);) switch (c) {
185 if ((ret = (int) yankstr(&cp)) == -1)
187 printf("(&xstr[%d])", ret);
199 if (incomm || *cp != '*')
207 if (incomm && *cp == '/') {
223 warn("x.c"), onintr(0);
235 static char tmp[] = "b\bt\tr\rn\nf\f\\\\\"\"";
237 while ((c = *cp++)) {
249 if (fgets(linebuf, sizeof linebuf, stdin)
258 for (tp = tmp; (ch = *tp++); tp++)
270 c <<= 3, c += *cp++ - '0';
273 c <<= 3, c += *cp++ - '0';
282 return (hashit(dbuf, 1));
289 return (isdigit(c) && c != '8' && c != '9');
296 FILE *mesgread = fopen(strings, "r");
298 if (mesgread == NULL)
302 if (fgetNUL(buf, sizeof buf, mesgread) == 0)
304 ignore(hashit(buf, 0));
306 ignore(fclose(mesgread));
310 fgetNUL(obuf, rmdr, file)
318 while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF)
321 return ((feof(file) || ferror(file)) ? 0 : 1);
348 struct hash *hp, *hp0;
350 hp = hp0 = &bucket[lastchr(str) & 0177];
353 i = istail(str, hp->hstr);
355 return (hp->hpt + i);
357 if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL)
360 if (!(hp->hstr = strdup(str)))
362 mesgpt += strlen(hp->hstr) + 1;
363 hp->hnext = hp0->hnext;
375 int old = 0, new = 0;
377 for (i = 0; i < BUCKETS; i++)
378 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext)
383 if (new == 0 && old != 0)
385 mesgwrit = fopen(strings, old ? "r+" : "w");
386 if (mesgwrit == NULL)
387 perror(strings), exit(4);
388 for (i = 0; i < BUCKETS; i++)
389 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) {
390 found(hp->hnew, hp->hpt, hp->hstr);
392 fseek(mesgwrit, hp->hpt, 0);
393 ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, mesgwrit));
394 if (ferror(mesgwrit))
395 err(4, "%s", strings);
398 if (fclose(mesgwrit) == EOF)
399 err(4, "%s", strings);
411 fprintf(stderr, "found at %d:", (int) off);
413 fprintf(stderr, "new at %d:", (int) off);
415 fprintf(stderr, "\n");
424 while ((c = (*cp++ & 0377)))
426 fprintf(stderr, "^%c", c + '`');
428 fprintf(stderr, "^?");
430 fprintf(stderr, "\\%03o", c);
432 fprintf(stderr, "%c", c);
438 FILE *strf = fopen(strings, "r");
442 err(5, "%s", strings);
443 xdotcf = fopen("xs.c", "w");
446 fprintf(xdotcf, "char\txstr[] = {\n");
450 for (i = 0; i < 8; i++) {
457 fprintf(xdotcf, "\n");
460 fprintf(xdotcf, "0x%02x,", c);
462 fprintf(xdotcf, "\n");
465 fprintf(xdotcf, "};\n");
466 ignore(fclose(xdotcf));
467 ignore(fclose(strf));
475 while (cp[0] && cp[1])
484 int d = strlen(of) - strlen(str);
486 if (d < 0 || strcmp(&of[d], str) != 0)
496 ignore(signal(SIGINT, SIG_IGN));
497 if (strings[0] == '/')
498 ignore(unlink(strings));
499 ignore(unlink("x.c"));
500 ignore(unlink("xs.c"));