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;
81 int fgetNUL(char *, int, FILE *);
82 int istail(char *, char *);
86 off_t hashit(char *, int);
87 off_t yankstr(char **);
89 static void usage(void);
92 void found(int, off_t, char *);
95 void process(const char *);
100 main(int argc, char *argv[])
104 while ((c = getopt(argc, argv, "-cv")) != -1)
121 if (signal(SIGINT, SIG_IGN) == SIG_DFL)
122 signal(SIGINT, onintr);
123 if (cflg || (argc == 0 && !readstd))
126 strings = mktemp(strdup(_PATH_TMP));
127 while (readstd || argc > 0) {
128 if (freopen("x.c", "w", stdout) == NULL)
130 if (!readstd && freopen(argv[0], "r", stdin) == NULL)
131 err(2, "%s", argv[0]);
141 if (strings[0] == '/')
142 ignore(unlink(strings));
149 fprintf(stderr, "usage: xstr [-cv] [-] [file ...]\n");
153 char linebuf[BUFSIZ];
156 process(const char *name)
163 printf("extern char\txstr[];\n");
165 if (fgets(linebuf, sizeof linebuf, stdin) == NULL) {
170 if (linebuf[0] == '#') {
171 if (linebuf[1] == ' ' && isdigit(linebuf[2]))
172 printf("#line%s", &linebuf[1]);
174 printf("%s", linebuf);
177 for (cp = linebuf; (c = *cp++);) switch (c) {
182 if ((ret = (int) yankstr(&cp)) == -1)
184 printf("(&xstr[%d])", ret);
196 if (incomm || *cp != '*')
204 if (incomm && *cp == '/') {
220 warn("x.c"), onintr(0);
231 static char tmp[] = "b\bt\tr\rn\nf\f\\\\\"\"";
233 while ((c = *cp++)) {
234 if (dp == dbuf + sizeof(dbuf) - 3)
235 errx(1, "message too long");
247 if (fgets(linebuf, sizeof linebuf, stdin)
256 for (tp = tmp; (ch = *tp++); tp++)
268 c <<= 3, c += *cp++ - '0';
271 c <<= 3, c += *cp++ - '0';
280 return (hashit(dbuf, 1));
286 return (isdigit(c) && c != '8' && c != '9');
293 FILE *mesgread = fopen(strings, "r");
295 if (mesgread == NULL)
299 if (fgetNUL(buf, sizeof buf, mesgread) == 0)
301 ignore(hashit(buf, 0));
303 ignore(fclose(mesgread));
307 fgetNUL(char *obuf, int rmdr, FILE *file)
312 while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF)
315 return ((feof(file) || ferror(file)) ? 0 : 1);
336 hashit(char *str, int new)
339 struct hash *hp, *hp0;
341 hp = hp0 = &bucket[lastchr(str) & 0177];
344 i = istail(str, hp->hstr);
346 return (hp->hpt + i);
348 if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL)
351 if (!(hp->hstr = strdup(str)))
353 mesgpt += strlen(hp->hstr) + 1;
354 hp->hnext = hp0->hnext;
366 int old = 0, new = 0;
368 for (i = 0; i < BUCKETS; i++)
369 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext)
374 if (new == 0 && old != 0)
376 mesgwrit = fopen(strings, old ? "r+" : "w");
377 if (mesgwrit == NULL)
378 err(4, "%s", strings);
379 for (i = 0; i < BUCKETS; i++)
380 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) {
381 found(hp->hnew, hp->hpt, hp->hstr);
383 fseek(mesgwrit, hp->hpt, 0);
384 ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, mesgwrit));
385 if (ferror(mesgwrit))
386 err(4, "%s", strings);
389 if (fclose(mesgwrit) == EOF)
390 err(4, "%s", strings);
394 found(int new, off_t off, char *str)
399 fprintf(stderr, "found at %d:", (int) off);
401 fprintf(stderr, "new at %d:", (int) off);
403 fprintf(stderr, "\n");
411 while ((c = (*cp++ & 0377)))
413 fprintf(stderr, "^%c", c + '`');
415 fprintf(stderr, "^?");
417 fprintf(stderr, "\\%03o", c);
419 fprintf(stderr, "%c", c);
425 FILE *strf = fopen(strings, "r");
429 err(5, "%s", strings);
430 xdotcf = fopen("xs.c", "w");
433 fprintf(xdotcf, "char\txstr[] = {\n");
437 for (i = 0; i < 8; i++) {
444 fprintf(xdotcf, "\n");
447 fprintf(xdotcf, "0x%02x,", c);
449 fprintf(xdotcf, "\n");
452 fprintf(xdotcf, "};\n");
453 ignore(fclose(xdotcf));
454 ignore(fclose(strf));
461 while (cp[0] && cp[1])
467 istail(char *str, char *of)
469 int d = strlen(of) - strlen(str);
471 if (d < 0 || strcmp(&of[d], str) != 0)
477 onintr(int dummy __unused)
480 ignore(signal(SIGINT, SIG_IGN));
481 if (strings[0] == '/')
482 ignore(unlink(strings));
483 ignore(unlink("x.c"));
484 ignore(unlink("xs.c"));