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. 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
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)xstr.c 8.1 (Berkeley) 6/9/93
31 * $FreeBSD: src/usr.bin/xstr/xstr.c,v 1.11 2008/05/13 09:42:03 kevlo Exp $
34 #include <sys/types.h>
44 #include "pathnames.h"
47 * xstr - extract and hash strings in a C program
53 #define ignore(a) ((void) a)
58 char cstrings[] = "strings";
59 char *strings = cstrings;
67 int fgetNUL(char *, int, FILE *);
68 int istail(char *, char *);
72 off_t hashit(char *, int);
73 off_t yankstr(char **);
75 static void usage(void);
78 void found(int, off_t, char *);
81 void process(const char *);
86 main(int argc, char *argv[])
91 while ((c = getopt(argc, argv, "-cv")) != -1)
108 if (signal(SIGINT, SIG_IGN) == SIG_DFL)
109 signal(SIGINT, onintr);
110 if (cflg || (argc == 0 && !readstd))
113 strings = strdup(_PATH_TMP);
115 err(1, "strdup() failed");
116 fdesc = mkstemp(strings);
118 err(1, "Unable to create temporary file");
122 while (readstd || argc > 0) {
123 if (freopen("x.c", "w", stdout) == NULL)
125 if (!readstd && freopen(argv[0], "r", stdin) == NULL)
126 err(2, "%s", argv[0]);
136 if (strings[0] == '/')
137 ignore(unlink(strings));
144 fprintf(stderr, "usage: xstr [-cv] [-] [file ...]\n");
148 char linebuf[BUFSIZ];
151 process(const char *name)
158 printf("extern char\txstr[];\n");
160 if (fgets(linebuf, sizeof linebuf, stdin) == NULL) {
165 if (linebuf[0] == '#') {
166 if (linebuf[1] == ' ' && isdigit(linebuf[2]))
167 printf("#line%s", &linebuf[1]);
169 printf("%s", linebuf);
172 for (cp = linebuf; (c = *cp++);) switch (c) {
177 if ((ret = (int) yankstr(&cp)) == -1)
179 printf("(&xstr[%d])", ret);
191 if (incomm || *cp != '*')
199 if (incomm && *cp == '/') {
215 warn("x.c"), onintr(0);
226 static char tmp[] = "b\bt\tr\rn\nf\f\\\\\"\"";
228 while ((c = *cp++)) {
229 if (dp == dbuf + sizeof(dbuf) - 3)
230 errx(1, "message too long");
242 if (fgets(linebuf, sizeof linebuf, stdin)
251 for (tp = tmp; (ch = *tp++); tp++)
263 c <<= 3, c += *cp++ - '0';
266 c <<= 3, c += *cp++ - '0';
275 return (hashit(dbuf, 1));
281 return (isdigit(c) && c != '8' && c != '9');
288 FILE *mesgread = fopen(strings, "r");
290 if (mesgread == NULL)
294 if (fgetNUL(buf, sizeof buf, mesgread) == 0)
296 ignore(hashit(buf, 0));
298 ignore(fclose(mesgread));
302 fgetNUL(char *obuf, int rmdr, FILE *file)
307 while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF)
310 return ((feof(file) || ferror(file)) ? 0 : 1);
331 hashit(char *str, int new)
334 struct hash *hp, *hp0;
336 hp = hp0 = &bucket[lastchr(str) & 0177];
339 i = istail(str, hp->hstr);
341 return (hp->hpt + i);
343 if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL)
346 if (!(hp->hstr = strdup(str)))
348 mesgpt += strlen(hp->hstr) + 1;
349 hp->hnext = hp0->hnext;
361 int old = 0, new = 0;
363 for (i = 0; i < BUCKETS; i++)
364 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext)
369 if (new == 0 && old != 0)
371 mesgwrit = fopen(strings, old ? "r+" : "w");
372 if (mesgwrit == NULL)
373 err(4, "%s", strings);
374 for (i = 0; i < BUCKETS; i++)
375 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) {
376 found(hp->hnew, hp->hpt, hp->hstr);
378 fseek(mesgwrit, hp->hpt, 0);
379 ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, mesgwrit));
380 if (ferror(mesgwrit))
381 err(4, "%s", strings);
384 if (fclose(mesgwrit) == EOF)
385 err(4, "%s", strings);
389 found(int new, off_t off, char *str)
394 fprintf(stderr, "found at %d:", (int) off);
396 fprintf(stderr, "new at %d:", (int) off);
398 fprintf(stderr, "\n");
406 while ((c = (*cp++ & 0377)))
408 fprintf(stderr, "^%c", c + '`');
410 fprintf(stderr, "^?");
412 fprintf(stderr, "\\%03o", c);
414 fprintf(stderr, "%c", c);
420 FILE *strf = fopen(strings, "r");
424 err(5, "%s", strings);
425 xdotcf = fopen("xs.c", "w");
428 fprintf(xdotcf, "char\txstr[] = {\n");
432 for (i = 0; i < 8; i++) {
439 fprintf(xdotcf, "\n");
442 fprintf(xdotcf, "0x%02x,", c);
444 fprintf(xdotcf, "\n");
447 fprintf(xdotcf, "};\n");
448 ignore(fclose(xdotcf));
449 ignore(fclose(strf));
456 while (cp[0] && cp[1])
462 istail(char *str, char *of)
464 int d = strlen(of) - strlen(str);
466 if (d < 0 || strcmp(&of[d], str) != 0)
472 onintr(int dummy __unused)
475 ignore(signal(SIGINT, SIG_IGN));
476 if (strings[0] == '/')
477 ignore(unlink(strings));
478 ignore(unlink("x.c"));
479 ignore(unlink("xs.c"));