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.4.2.1 2002/11/16 01:01:21 tjr Exp $
36 * $DragonFly: src/usr.bin/xstr/xstr.c,v 1.3 2005/07/26 20:17:31 liamfoy Exp $
39 #include <sys/types.h>
49 #include "pathnames.h"
52 * xstr - extract and hash strings in a C program
58 #define ignore(a) ((void) a)
63 char cstrings[] = "strings";
64 char *strings = cstrings;
72 int fgetNUL(char *, int, FILE *);
73 int istail(char *, char *);
77 off_t hashit(char *, int);
78 off_t yankstr(char **);
80 static void usage(void);
83 void found(int, off_t, char *);
86 void process(const char *);
91 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 = mktemp(strdup(_PATH_TMP));
118 while (readstd || argc > 0) {
119 if (freopen("x.c", "w", stdout) == NULL)
121 if (!readstd && freopen(argv[0], "r", stdin) == NULL)
122 err(2, "%s", argv[0]);
132 if (strings[0] == '/')
133 ignore(unlink(strings));
140 fprintf(stderr, "usage: xstr [-v] [-c] [-] [name ...]\n");
144 char linebuf[BUFSIZ];
147 process(const char *name)
154 printf("extern char\txstr[];\n");
156 if (fgets(linebuf, sizeof linebuf, stdin) == NULL) {
161 if (linebuf[0] == '#') {
162 if (linebuf[1] == ' ' && isdigit(linebuf[2]))
163 printf("#line%s", &linebuf[1]);
165 printf("%s", linebuf);
168 for (cp = linebuf; (c = *cp++);) switch (c) {
173 if ((ret = (int) yankstr(&cp)) == -1)
175 printf("(&xstr[%d])", ret);
187 if (incomm || *cp != '*')
195 if (incomm && *cp == '/') {
211 warn("x.c"), onintr(0);
222 static char tmp[] = "b\bt\tr\rn\nf\f\\\\\"\"";
224 while ((c = *cp++)) {
225 if (dp == dbuf + sizeof(dbuf) - 3)
226 errx(1, "message too long");
238 if (fgets(linebuf, sizeof linebuf, stdin)
247 for (tp = tmp; (ch = *tp++); tp++)
259 c <<= 3, c += *cp++ - '0';
262 c <<= 3, c += *cp++ - '0';
271 return (hashit(dbuf, 1));
277 return (isdigit(c) && c != '8' && c != '9');
284 FILE *mesgread = fopen(strings, "r");
286 if (mesgread == NULL)
290 if (fgetNUL(buf, sizeof buf, mesgread) == 0)
292 ignore(hashit(buf, 0));
294 ignore(fclose(mesgread));
298 fgetNUL(char *obuf, int rmdr, FILE *file)
303 while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF)
306 return ((feof(file) || ferror(file)) ? 0 : 1);
327 hashit(char *str, int new)
330 struct hash *hp, *hp0;
332 hp = hp0 = &bucket[lastchr(str) & 0177];
335 i = istail(str, hp->hstr);
337 return (hp->hpt + i);
339 if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL)
342 if (!(hp->hstr = strdup(str)))
344 mesgpt += strlen(hp->hstr) + 1;
345 hp->hnext = hp0->hnext;
357 int old = 0, new = 0;
359 for (i = 0; i < BUCKETS; i++)
360 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext)
365 if (new == 0 && old != 0)
367 mesgwrit = fopen(strings, old ? "r+" : "w");
368 if (mesgwrit == NULL)
369 err(4, "%s", strings);
370 for (i = 0; i < BUCKETS; i++)
371 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) {
372 found(hp->hnew, hp->hpt, hp->hstr);
374 fseek(mesgwrit, hp->hpt, 0);
375 ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, mesgwrit));
376 if (ferror(mesgwrit))
377 err(4, "%s", strings);
380 if (fclose(mesgwrit) == EOF)
381 err(4, "%s", strings);
385 found(int new, off_t off, char *str)
390 fprintf(stderr, "found at %d:", (int) off);
392 fprintf(stderr, "new at %d:", (int) off);
394 fprintf(stderr, "\n");
402 while ((c = (*cp++ & 0377)))
404 fprintf(stderr, "^%c", c + '`');
406 fprintf(stderr, "^?");
408 fprintf(stderr, "\\%03o", c);
410 fprintf(stderr, "%c", c);
416 FILE *strf = fopen(strings, "r");
420 err(5, "%s", strings);
421 xdotcf = fopen("xs.c", "w");
424 fprintf(xdotcf, "char\txstr[] = {\n");
428 for (i = 0; i < 8; i++) {
435 fprintf(xdotcf, "\n");
438 fprintf(xdotcf, "0x%02x,", c);
440 fprintf(xdotcf, "\n");
443 fprintf(xdotcf, "};\n");
444 ignore(fclose(xdotcf));
445 ignore(fclose(strf));
452 while (cp[0] && cp[1])
458 istail(char *str, char *of)
460 int d = strlen(of) - strlen(str);
462 if (d < 0 || strcmp(&of[d], str) != 0)
468 onintr(int dummy __unused)
471 ignore(signal(SIGINT, SIG_IGN));
472 if (strings[0] == '/')
473 ignore(unlink(strings));
474 ignore(unlink("x.c"));
475 ignore(unlink("xs.c"));