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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)xstr.c 8.1 (Berkeley) 6/9/93";
44 #include <sys/types.h>
54 #include "pathnames.h"
57 * xstr - extract and hash strings in a C program
63 #define ignore(a) ((void) a)
68 static char cstrings[] = "strings";
69 static char *strings = cstrings;
75 static char lastchr(char *);
77 static int fgetNUL(char *, int, FILE *);
78 static int istail(char *, char *);
79 static int octdigit(char);
80 static int xgetc(FILE *);
82 static off_t hashit(char *, int);
83 static off_t yankstr(char **);
85 static void usage(void);
87 static void flushsh(void);
88 static void found(int, off_t, char *);
89 static void inithash(void);
90 static void onintr(int);
91 static void process(const char *);
92 static void prstr(char *);
93 static void xsdotc(void);
96 main(int argc, char *argv[])
101 while ((c = getopt(argc, argv, "-cv")) != -1)
118 if (signal(SIGINT, SIG_IGN) == SIG_DFL)
119 signal(SIGINT, onintr);
120 if (cflg || (argc == 0 && !readstd))
123 strings = strdup(_PATH_TMP);
125 err(1, "strdup() failed");
126 fdesc = mkstemp(strings);
128 err(1, "Unable to create temporary file");
132 while (readstd || argc > 0) {
133 if (freopen("x.c", "w", stdout) == NULL)
135 if (!readstd && freopen(argv[0], "r", stdin) == NULL)
136 err(2, "%s", argv[0]);
146 if (strings[0] == '/')
147 ignore(unlink(strings));
154 fprintf(stderr, "usage: xstr [-cv] [-] [file ...]\n");
158 static char linebuf[BUFSIZ];
161 process(const char *name)
168 printf("extern char\txstr[];\n");
170 if (fgets(linebuf, sizeof linebuf, stdin) == NULL) {
175 if (linebuf[0] == '#') {
176 if (linebuf[1] == ' ' && isdigit(linebuf[2]))
177 printf("#line%s", &linebuf[1]);
179 printf("%s", linebuf);
182 for (cp = linebuf; (c = *cp++);) switch (c) {
187 if ((ret = (int) yankstr(&cp)) == -1)
189 printf("(&xstr[%d])", ret);
201 if (incomm || *cp != '*')
209 if (incomm && *cp == '/') {
225 warn("x.c"), onintr(0);
236 static char tmp[] = "b\bt\tr\rn\nf\f\\\\\"\"";
238 while ((c = *cp++)) {
239 if (dp == dbuf + sizeof(dbuf) - 3)
240 errx(1, "message too long");
252 if (fgets(linebuf, sizeof linebuf, stdin)
261 for (tp = tmp; (ch = *tp++); tp++)
273 c <<= 3, c += *cp++ - '0';
276 c <<= 3, c += *cp++ - '0';
285 return (hashit(dbuf, 1));
291 return (isdigit(c) && c != '8' && c != '9');
298 FILE *mesgread = fopen(strings, "r");
300 if (mesgread == NULL)
304 if (fgetNUL(buf, sizeof buf, mesgread) == 0)
306 ignore(hashit(buf, 0));
308 ignore(fclose(mesgread));
312 fgetNUL(char *obuf, int rmdr, FILE *file)
317 while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF)
320 return ((feof(file) || ferror(file)) ? 0 : 1);
341 hashit(char *str, int new)
344 struct hash *hp, *hp0;
346 hp = hp0 = &bucket[lastchr(str) & 0177];
349 i = istail(str, hp->hstr);
351 return (hp->hpt + i);
353 if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL)
356 if (!(hp->hstr = strdup(str)))
358 mesgpt += strlen(hp->hstr) + 1;
359 hp->hnext = hp0->hnext;
371 int old = 0, new = 0;
373 for (i = 0; i < BUCKETS; i++)
374 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext)
379 if (new == 0 && old != 0)
381 mesgwrit = fopen(strings, old ? "r+" : "w");
382 if (mesgwrit == NULL)
383 err(4, "%s", strings);
384 for (i = 0; i < BUCKETS; i++)
385 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) {
386 found(hp->hnew, hp->hpt, hp->hstr);
388 fseek(mesgwrit, hp->hpt, 0);
389 ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, mesgwrit));
390 if (ferror(mesgwrit))
391 err(4, "%s", strings);
394 if (fclose(mesgwrit) == EOF)
395 err(4, "%s", strings);
399 found(int new, off_t off, char *str)
404 fprintf(stderr, "found at %d:", (int) off);
406 fprintf(stderr, "new at %d:", (int) off);
408 fprintf(stderr, "\n");
416 while ((c = (*cp++ & 0377)))
418 fprintf(stderr, "^%c", c + '`');
420 fprintf(stderr, "^?");
422 fprintf(stderr, "\\%03o", c);
424 fprintf(stderr, "%c", c);
430 FILE *strf = fopen(strings, "r");
434 err(5, "%s", strings);
435 xdotcf = fopen("xs.c", "w");
438 fprintf(xdotcf, "char\txstr[] = {\n");
442 for (i = 0; i < 8; i++) {
449 fprintf(xdotcf, "\n");
452 fprintf(xdotcf, "0x%02x,", c);
454 fprintf(xdotcf, "\n");
457 fprintf(xdotcf, "};\n");
458 ignore(fclose(xdotcf));
459 ignore(fclose(strf));
466 while (cp[0] && cp[1])
472 istail(char *str, char *of)
474 int d = strlen(of) - strlen(str);
476 if (d < 0 || strcmp(&of[d], str) != 0)
482 onintr(int dummy __unused)
485 ignore(signal(SIGINT, SIG_IGN));
486 if (strings[0] == '/')
487 ignore(unlink(strings));
488 ignore(unlink("x.c"));
489 ignore(unlink("xs.c"));