xstr(1): Staticize.
[dragonfly.git] / usr.bin / xstr / xstr.c
blob9d52f56ea44c1252c949651d7fb4d10e9daa6b30
1 /*
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
7 * are met:
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
27 * SUCH DAMAGE.
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>
36 #include <ctype.h>
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <signal.h>
41 #include <string.h>
42 #include <unistd.h>
44 #include "pathnames.h"
47 * xstr - extract and hash strings in a C program
49 * Bill Joy UCB
50 * November, 1978
53 #define ignore(a) ((void) a)
55 static off_t tellpt;
57 static off_t mesgpt;
58 static char cstrings[] = "strings";
59 static char *strings = cstrings;
61 static int cflg;
62 static int vflg;
63 static int readstd;
65 static char lastchr(char *);
67 static int fgetNUL(char *, int, FILE *);
68 static int istail(char *, char *);
69 static int octdigit(char);
70 static int xgetc(FILE *);
72 static off_t hashit(char *, int);
73 static off_t yankstr(char **);
75 static void usage(void);
77 static void flushsh(void);
78 static void found(int, off_t, char *);
79 static void inithash(void);
80 static void onintr(int);
81 static void process(const char *);
82 static void prstr(char *);
83 static void xsdotc(void);
85 int
86 main(int argc, char *argv[])
88 int c;
89 int fdesc;
91 while ((c = getopt(argc, argv, "-cv")) != -1)
92 switch (c) {
93 case '-':
94 readstd++;
95 break;
96 case 'c':
97 cflg++;
98 break;
99 case 'v':
100 vflg++;
101 break;
102 default:
103 usage();
105 argc -= optind;
106 argv += optind;
108 if (signal(SIGINT, SIG_IGN) == SIG_DFL)
109 signal(SIGINT, onintr);
110 if (cflg || (argc == 0 && !readstd))
111 inithash();
112 else {
113 strings = strdup(_PATH_TMP);
114 if (strings == NULL)
115 err(1, "strdup() failed");
116 fdesc = mkstemp(strings);
117 if (fdesc == -1)
118 err(1, "Unable to create temporary file");
119 close(fdesc);
122 while (readstd || argc > 0) {
123 if (freopen("x.c", "w", stdout) == NULL)
124 err(1, "x.c");
125 if (!readstd && freopen(argv[0], "r", stdin) == NULL)
126 err(2, "%s", argv[0]);
127 process("x.c");
128 if (readstd == 0)
129 argc--, argv++;
130 else
131 readstd = 0;
133 flushsh();
134 if (cflg == 0)
135 xsdotc();
136 if (strings[0] == '/')
137 ignore(unlink(strings));
138 exit(0);
141 static void
142 usage(void)
144 fprintf(stderr, "usage: xstr [-cv] [-] [file ...]\n");
145 exit (1);
148 static char linebuf[BUFSIZ];
150 static void
151 process(const char *name)
153 char *cp;
154 int c;
155 int incomm = 0;
156 int ret;
158 printf("extern char\txstr[];\n");
159 for (;;) {
160 if (fgets(linebuf, sizeof linebuf, stdin) == NULL) {
161 if (ferror(stdin))
162 err(3, "%s", name);
163 break;
165 if (linebuf[0] == '#') {
166 if (linebuf[1] == ' ' && isdigit(linebuf[2]))
167 printf("#line%s", &linebuf[1]);
168 else
169 printf("%s", linebuf);
170 continue;
172 for (cp = linebuf; (c = *cp++);) switch (c) {
174 case '"':
175 if (incomm)
176 goto def;
177 if ((ret = (int) yankstr(&cp)) == -1)
178 goto out;
179 printf("(&xstr[%d])", ret);
180 break;
182 case '\'':
183 if (incomm)
184 goto def;
185 putchar(c);
186 if (*cp)
187 putchar(*cp++);
188 break;
190 case '/':
191 if (incomm || *cp != '*')
192 goto def;
193 incomm = 1;
194 cp++;
195 printf("/*");
196 continue;
198 case '*':
199 if (incomm && *cp == '/') {
200 incomm = 0;
201 cp++;
202 printf("*/");
203 continue;
205 goto def;
207 def:
208 default:
209 putchar(c);
210 break;
213 out:
214 if (ferror(stdout))
215 warn("x.c"), onintr(0);
218 static off_t
219 yankstr(char **cpp)
221 char *cp = *cpp;
222 int c, ch;
223 char dbuf[BUFSIZ];
224 char *dp = dbuf;
225 char *tp;
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");
231 switch (c) {
233 case '"':
234 cp++;
235 goto out;
237 case '\\':
238 c = *cp++;
239 if (c == 0)
240 break;
241 if (c == '\n') {
242 if (fgets(linebuf, sizeof linebuf, stdin)
243 == NULL) {
244 if (ferror(stdin))
245 err(3, "x.c");
246 return(-1);
248 cp = linebuf;
249 continue;
251 for (tp = tmp; (ch = *tp++); tp++)
252 if (c == ch) {
253 c = *tp;
254 goto gotc;
256 if (!octdigit(c)) {
257 *dp++ = '\\';
258 break;
260 c -= '0';
261 if (!octdigit(*cp))
262 break;
263 c <<= 3, c += *cp++ - '0';
264 if (!octdigit(*cp))
265 break;
266 c <<= 3, c += *cp++ - '0';
267 break;
269 gotc:
270 *dp++ = c;
272 out:
273 *cpp = --cp;
274 *dp = 0;
275 return (hashit(dbuf, 1));
278 static int
279 octdigit(char c)
281 return (isdigit(c) && c != '8' && c != '9');
284 static void
285 inithash(void)
287 char buf[BUFSIZ];
288 FILE *mesgread = fopen(strings, "r");
290 if (mesgread == NULL)
291 return;
292 for (;;) {
293 mesgpt = tellpt;
294 if (fgetNUL(buf, sizeof buf, mesgread) == 0)
295 break;
296 ignore(hashit(buf, 0));
298 ignore(fclose(mesgread));
301 static int
302 fgetNUL(char *obuf, int rmdr, FILE *file)
304 int c;
305 char *buf = obuf;
307 while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF)
308 *buf++ = c;
309 *buf++ = 0;
310 return ((feof(file) || ferror(file)) ? 0 : 1);
313 static int
314 xgetc(FILE *file)
317 tellpt++;
318 return (getc(file));
321 #define BUCKETS 128
323 static struct hash {
324 off_t hpt;
325 char *hstr;
326 struct hash *hnext;
327 short hnew;
328 } bucket[BUCKETS];
330 static off_t
331 hashit(char *str, int new)
333 int i;
334 struct hash *hp, *hp0;
336 hp = hp0 = &bucket[lastchr(str) & 0177];
337 while (hp->hnext) {
338 hp = hp->hnext;
339 i = istail(str, hp->hstr);
340 if (i >= 0)
341 return (hp->hpt + i);
343 if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL)
344 errx(8, "calloc");
345 hp->hpt = mesgpt;
346 if (!(hp->hstr = strdup(str)))
347 err(1, NULL);
348 mesgpt += strlen(hp->hstr) + 1;
349 hp->hnext = hp0->hnext;
350 hp->hnew = new;
351 hp0->hnext = hp;
352 return (hp->hpt);
355 static void
356 flushsh(void)
358 int i;
359 struct hash *hp;
360 FILE *mesgwrit;
361 int old = 0, new = 0;
363 for (i = 0; i < BUCKETS; i++)
364 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext)
365 if (hp->hnew)
366 new++;
367 else
368 old++;
369 if (new == 0 && old != 0)
370 return;
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);
377 if (hp->hnew) {
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);
388 static void
389 found(int new, off_t off, char *str)
391 if (vflg == 0)
392 return;
393 if (!new)
394 fprintf(stderr, "found at %d:", (int) off);
395 else
396 fprintf(stderr, "new at %d:", (int) off);
397 prstr(str);
398 fprintf(stderr, "\n");
401 static void
402 prstr(char *cp)
404 int c;
406 while ((c = (*cp++ & 0377)))
407 if (c < ' ')
408 fprintf(stderr, "^%c", c + '`');
409 else if (c == 0177)
410 fprintf(stderr, "^?");
411 else if (c > 0200)
412 fprintf(stderr, "\\%03o", c);
413 else
414 fprintf(stderr, "%c", c);
417 static void
418 xsdotc(void)
420 FILE *strf = fopen(strings, "r");
421 FILE *xdotcf;
423 if (strf == NULL)
424 err(5, "%s", strings);
425 xdotcf = fopen("xs.c", "w");
426 if (xdotcf == NULL)
427 err(6, "xs.c");
428 fprintf(xdotcf, "char\txstr[] = {\n");
429 for (;;) {
430 int i, c;
432 for (i = 0; i < 8; i++) {
433 c = getc(strf);
434 if (ferror(strf)) {
435 warn("%s", strings);
436 onintr(0);
438 if (feof(strf)) {
439 fprintf(xdotcf, "\n");
440 goto out;
442 fprintf(xdotcf, "0x%02x,", c);
444 fprintf(xdotcf, "\n");
446 out:
447 fprintf(xdotcf, "};\n");
448 ignore(fclose(xdotcf));
449 ignore(fclose(strf));
452 static char
453 lastchr(char *cp)
456 while (cp[0] && cp[1])
457 cp++;
458 return (*cp);
461 static int
462 istail(char *str, char *of)
464 int d = strlen(of) - strlen(str);
466 if (d < 0 || strcmp(&of[d], str) != 0)
467 return (-1);
468 return (d);
471 static void
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"));
480 exit(7);