2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 static const char copyright[] =
38 "@(#) Copyright (c) 1980, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static const char sccsid[] = "@(#)xstr.c 8.1 (Berkeley) 6/9/93";
46 #include <sys/types.h>
56 #include "pathnames.h"
59 * xstr - extract and hash strings in a C program
65 #define ignore(a) ((void) a)
70 static char cstrings[] = "strings";
71 static char *strings = cstrings;
77 static char lastchr(char *);
79 static int fgetNUL(char *, int, FILE *);
80 static int istail(char *, char *);
81 static int octdigit(char);
82 static int xgetc(FILE *);
84 static off_t hashit(char *, int);
85 static off_t yankstr(char **);
87 static void usage(void);
89 static void flushsh(void);
90 static void found(int, off_t, char *);
91 static void inithash(void);
92 static void onintr(int);
93 static void process(const char *);
94 static void prstr(char *);
95 static void xsdotc(void);
98 main(int argc, char *argv[])
103 while ((c = getopt(argc, argv, "-cv")) != -1)
120 if (signal(SIGINT, SIG_IGN) == SIG_DFL)
121 signal(SIGINT, onintr);
122 if (cflg || (argc == 0 && !readstd))
125 strings = strdup(_PATH_TMP);
127 err(1, "strdup() failed");
128 fdesc = mkstemp(strings);
130 err(1, "Unable to create temporary file");
134 while (readstd || argc > 0) {
135 if (freopen("x.c", "w", stdout) == NULL)
137 if (!readstd && freopen(argv[0], "r", stdin) == NULL)
138 err(2, "%s", argv[0]);
148 if (strings[0] == '/')
149 ignore(unlink(strings));
156 fprintf(stderr, "usage: xstr [-cv] [-] [file ...]\n");
160 static char linebuf[BUFSIZ];
163 process(const char *name)
170 printf("extern char\txstr[];\n");
172 if (fgets(linebuf, sizeof linebuf, stdin) == NULL) {
177 if (linebuf[0] == '#') {
178 if (linebuf[1] == ' ' && isdigit(linebuf[2]))
179 printf("#line%s", &linebuf[1]);
181 printf("%s", linebuf);
184 for (cp = linebuf; (c = *cp++);) switch (c) {
189 if ((ret = (int) yankstr(&cp)) == -1)
191 printf("(&xstr[%d])", ret);
203 if (incomm || *cp != '*')
211 if (incomm && *cp == '/') {
227 warn("x.c"), onintr(0);
238 static char tmp[] = "b\bt\tr\rn\nf\f\\\\\"\"";
240 while ((c = *cp++)) {
241 if (dp == dbuf + sizeof(dbuf) - 3)
242 errx(1, "message too long");
254 if (fgets(linebuf, sizeof linebuf, stdin)
263 for (tp = tmp; (ch = *tp++); tp++)
275 c <<= 3, c += *cp++ - '0';
278 c <<= 3, c += *cp++ - '0';
287 return (hashit(dbuf, 1));
293 return (isdigit(c) && c != '8' && c != '9');
300 FILE *mesgread = fopen(strings, "r");
302 if (mesgread == NULL)
306 if (fgetNUL(buf, sizeof buf, mesgread) == 0)
308 ignore(hashit(buf, 0));
310 ignore(fclose(mesgread));
314 fgetNUL(char *obuf, int rmdr, FILE *file)
319 while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF)
322 return ((feof(file) || ferror(file)) ? 0 : 1);
343 hashit(char *str, int new)
346 struct hash *hp, *hp0;
348 hp = hp0 = &bucket[lastchr(str) & 0177];
351 i = istail(str, hp->hstr);
353 return (hp->hpt + i);
355 if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL)
358 if (!(hp->hstr = strdup(str)))
360 mesgpt += strlen(hp->hstr) + 1;
361 hp->hnext = hp0->hnext;
373 int old = 0, new = 0;
375 for (i = 0; i < BUCKETS; i++)
376 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext)
381 if (new == 0 && old != 0)
383 mesgwrit = fopen(strings, old ? "r+" : "w");
384 if (mesgwrit == NULL)
385 err(4, "%s", strings);
386 for (i = 0; i < BUCKETS; i++)
387 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) {
388 found(hp->hnew, hp->hpt, hp->hstr);
390 fseek(mesgwrit, hp->hpt, 0);
391 ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, mesgwrit));
392 if (ferror(mesgwrit))
393 err(4, "%s", strings);
396 if (fclose(mesgwrit) == EOF)
397 err(4, "%s", strings);
401 found(int new, off_t off, char *str)
406 fprintf(stderr, "found at %d:", (int) off);
408 fprintf(stderr, "new at %d:", (int) off);
410 fprintf(stderr, "\n");
418 while ((c = (*cp++ & 0377)))
420 fprintf(stderr, "^%c", c + '`');
422 fprintf(stderr, "^?");
424 fprintf(stderr, "\\%03o", c);
426 fprintf(stderr, "%c", c);
432 FILE *strf = fopen(strings, "r");
436 err(5, "%s", strings);
437 xdotcf = fopen("xs.c", "w");
440 fprintf(xdotcf, "char\txstr[] = {\n");
444 for (i = 0; i < 8; i++) {
451 fprintf(xdotcf, "\n");
454 fprintf(xdotcf, "0x%02x,", c);
456 fprintf(xdotcf, "\n");
459 fprintf(xdotcf, "};\n");
460 ignore(fclose(xdotcf));
461 ignore(fclose(strf));
468 while (cp[0] && cp[1])
474 istail(char *str, char *of)
476 int d = strlen(of) - strlen(str);
478 if (d < 0 || strcmp(&of[d], str) != 0)
484 onintr(int dummy __unused)
487 ignore(signal(SIGINT, SIG_IGN));
488 if (strings[0] == '/')
489 ignore(unlink(strings));
490 ignore(unlink("x.c"));
491 ignore(unlink("xs.c"));