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
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)xstr.c 8.1 (Berkeley) 6/9/93
35 * $FreeBSD: src/usr.bin/xstr/xstr.c,v 1.11 2008/05/13 09:42:03 kevlo Exp $
38 #include <sys/types.h>
48 #include "pathnames.h"
51 * xstr - extract and hash strings in a C program
57 #define ignore(a) ((void) a)
62 char cstrings[] = "strings";
63 char *strings = cstrings;
71 int fgetNUL(char *, int, FILE *);
72 int istail(char *, char *);
76 off_t hashit(char *, int);
77 off_t yankstr(char **);
79 static void usage(void);
82 void found(int, off_t, char *);
85 void process(const char *);
90 main(int argc, char *argv[])
95 while ((c = getopt(argc, argv, "-cv")) != -1)
112 if (signal(SIGINT, SIG_IGN) == SIG_DFL)
113 signal(SIGINT, onintr);
114 if (cflg || (argc == 0 && !readstd))
117 strings = strdup(_PATH_TMP);
119 err(1, "strdup() failed");
120 fdesc = mkstemp(strings);
122 err(1, "Unable to create temporary file");
126 while (readstd || argc > 0) {
127 if (freopen("x.c", "w", stdout) == NULL)
129 if (!readstd && freopen(argv[0], "r", stdin) == NULL)
130 err(2, "%s", argv[0]);
140 if (strings[0] == '/')
141 ignore(unlink(strings));
148 fprintf(stderr, "usage: xstr [-cv] [-] [file ...]\n");
152 char linebuf[BUFSIZ];
155 process(const char *name)
162 printf("extern char\txstr[];\n");
164 if (fgets(linebuf, sizeof linebuf, stdin) == NULL) {
169 if (linebuf[0] == '#') {
170 if (linebuf[1] == ' ' && isdigit(linebuf[2]))
171 printf("#line%s", &linebuf[1]);
173 printf("%s", linebuf);
176 for (cp = linebuf; (c = *cp++);) switch (c) {
181 if ((ret = (int) yankstr(&cp)) == -1)
183 printf("(&xstr[%d])", ret);
195 if (incomm || *cp != '*')
203 if (incomm && *cp == '/') {
219 warn("x.c"), onintr(0);
230 static char tmp[] = "b\bt\tr\rn\nf\f\\\\\"\"";
232 while ((c = *cp++)) {
233 if (dp == dbuf + sizeof(dbuf) - 3)
234 errx(1, "message too long");
246 if (fgets(linebuf, sizeof linebuf, stdin)
255 for (tp = tmp; (ch = *tp++); tp++)
267 c <<= 3, c += *cp++ - '0';
270 c <<= 3, c += *cp++ - '0';
279 return (hashit(dbuf, 1));
285 return (isdigit(c) && c != '8' && c != '9');
292 FILE *mesgread = fopen(strings, "r");
294 if (mesgread == NULL)
298 if (fgetNUL(buf, sizeof buf, mesgread) == 0)
300 ignore(hashit(buf, 0));
302 ignore(fclose(mesgread));
306 fgetNUL(char *obuf, int rmdr, FILE *file)
311 while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF)
314 return ((feof(file) || ferror(file)) ? 0 : 1);
335 hashit(char *str, int new)
338 struct hash *hp, *hp0;
340 hp = hp0 = &bucket[lastchr(str) & 0177];
343 i = istail(str, hp->hstr);
345 return (hp->hpt + i);
347 if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL)
350 if (!(hp->hstr = strdup(str)))
352 mesgpt += strlen(hp->hstr) + 1;
353 hp->hnext = hp0->hnext;
365 int old = 0, new = 0;
367 for (i = 0; i < BUCKETS; i++)
368 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext)
373 if (new == 0 && old != 0)
375 mesgwrit = fopen(strings, old ? "r+" : "w");
376 if (mesgwrit == NULL)
377 err(4, "%s", strings);
378 for (i = 0; i < BUCKETS; i++)
379 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) {
380 found(hp->hnew, hp->hpt, hp->hstr);
382 fseek(mesgwrit, hp->hpt, 0);
383 ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, mesgwrit));
384 if (ferror(mesgwrit))
385 err(4, "%s", strings);
388 if (fclose(mesgwrit) == EOF)
389 err(4, "%s", strings);
393 found(int new, off_t off, char *str)
398 fprintf(stderr, "found at %d:", (int) off);
400 fprintf(stderr, "new at %d:", (int) off);
402 fprintf(stderr, "\n");
410 while ((c = (*cp++ & 0377)))
412 fprintf(stderr, "^%c", c + '`');
414 fprintf(stderr, "^?");
416 fprintf(stderr, "\\%03o", c);
418 fprintf(stderr, "%c", c);
424 FILE *strf = fopen(strings, "r");
428 err(5, "%s", strings);
429 xdotcf = fopen("xs.c", "w");
432 fprintf(xdotcf, "char\txstr[] = {\n");
436 for (i = 0; i < 8; i++) {
443 fprintf(xdotcf, "\n");
446 fprintf(xdotcf, "0x%02x,", c);
448 fprintf(xdotcf, "\n");
451 fprintf(xdotcf, "};\n");
452 ignore(fclose(xdotcf));
453 ignore(fclose(strf));
460 while (cp[0] && cp[1])
466 istail(char *str, char *of)
468 int d = strlen(of) - strlen(str);
470 if (d < 0 || strcmp(&of[d], str) != 0)
476 onintr(int dummy __unused)
479 ignore(signal(SIGINT, SIG_IGN));
480 if (strings[0] == '/')
481 ignore(unlink(strings));
482 ignore(unlink("x.c"));
483 ignore(unlink("xs.c"));