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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 static const char copyright
[] =
40 "@(#) Copyright (c) 1980, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid
[] = "@(#)xstr.c 8.1 (Berkeley) 6/9/93";
48 #include <sys/types.h>
58 #include "pathnames.h"
61 * xstr - extract and hash strings in a C program
67 #define ignore(a) ((void) a)
72 char cstrings
[] = "strings";
73 char *strings
= cstrings
;
81 int fgetNUL(char *, int, FILE *);
82 int istail(char *, char *);
86 off_t
hashit(char *, int);
87 off_t
yankstr(char **);
89 static void usage(void);
92 void found(int, off_t
, char *);
95 void process(const char *);
100 main(int argc
, char *argv
[])
105 while ((c
= getopt(argc
, argv
, "-cv")) != -1)
122 if (signal(SIGINT
, SIG_IGN
) == SIG_DFL
)
123 signal(SIGINT
, onintr
);
124 if (cflg
|| (argc
== 0 && !readstd
))
127 strings
= strdup(_PATH_TMP
);
129 err(1, "strdup() failed");
130 fdesc
= mkstemp(strings
);
132 err(1, "Unable to create temporary file");
136 while (readstd
|| argc
> 0) {
137 if (freopen("x.c", "w", stdout
) == NULL
)
139 if (!readstd
&& freopen(argv
[0], "r", stdin
) == NULL
)
140 err(2, "%s", argv
[0]);
150 if (strings
[0] == '/')
151 ignore(unlink(strings
));
158 fprintf(stderr
, "usage: xstr [-cv] [-] [file ...]\n");
162 char linebuf
[BUFSIZ
];
165 process(const char *name
)
172 printf("extern char\txstr[];\n");
174 if (fgets(linebuf
, sizeof linebuf
, stdin
) == NULL
) {
179 if (linebuf
[0] == '#') {
180 if (linebuf
[1] == ' ' && isdigit(linebuf
[2]))
181 printf("#line%s", &linebuf
[1]);
183 printf("%s", linebuf
);
186 for (cp
= linebuf
; (c
= *cp
++);) switch (c
) {
191 if ((ret
= (int) yankstr(&cp
)) == -1)
193 printf("(&xstr[%d])", ret
);
205 if (incomm
|| *cp
!= '*')
213 if (incomm
&& *cp
== '/') {
229 warn("x.c"), onintr(0);
240 static char tmp
[] = "b\bt\tr\rn\nf\f\\\\\"\"";
242 while ((c
= *cp
++)) {
243 if (dp
== dbuf
+ sizeof(dbuf
) - 3)
244 errx(1, "message too long");
256 if (fgets(linebuf
, sizeof linebuf
, stdin
)
265 for (tp
= tmp
; (ch
= *tp
++); tp
++)
277 c
<<= 3, c
+= *cp
++ - '0';
280 c
<<= 3, c
+= *cp
++ - '0';
289 return (hashit(dbuf
, 1));
295 return (isdigit(c
) && c
!= '8' && c
!= '9');
302 FILE *mesgread
= fopen(strings
, "r");
304 if (mesgread
== NULL
)
308 if (fgetNUL(buf
, sizeof buf
, mesgread
) == 0)
310 ignore(hashit(buf
, 0));
312 ignore(fclose(mesgread
));
316 fgetNUL(char *obuf
, int rmdr
, FILE *file
)
321 while (--rmdr
> 0 && (c
= xgetc(file
)) != 0 && c
!= EOF
)
324 return ((feof(file
) || ferror(file
)) ? 0 : 1);
345 hashit(char *str
, int new)
348 struct hash
*hp
, *hp0
;
350 hp
= hp0
= &bucket
[lastchr(str
) & 0177];
353 i
= istail(str
, hp
->hstr
);
355 return (hp
->hpt
+ i
);
357 if ((hp
= (struct hash
*) calloc(1, sizeof (*hp
))) == NULL
)
360 if (!(hp
->hstr
= strdup(str
)))
362 mesgpt
+= strlen(hp
->hstr
) + 1;
363 hp
->hnext
= hp0
->hnext
;
375 int old
= 0, new = 0;
377 for (i
= 0; i
< BUCKETS
; i
++)
378 for (hp
= bucket
[i
].hnext
; hp
!= NULL
; hp
= hp
->hnext
)
383 if (new == 0 && old
!= 0)
385 mesgwrit
= fopen(strings
, old
? "r+" : "w");
386 if (mesgwrit
== NULL
)
387 err(4, "%s", strings
);
388 for (i
= 0; i
< BUCKETS
; i
++)
389 for (hp
= bucket
[i
].hnext
; hp
!= NULL
; hp
= hp
->hnext
) {
390 found(hp
->hnew
, hp
->hpt
, hp
->hstr
);
392 fseek(mesgwrit
, hp
->hpt
, 0);
393 ignore(fwrite(hp
->hstr
, strlen(hp
->hstr
) + 1, 1, mesgwrit
));
394 if (ferror(mesgwrit
))
395 err(4, "%s", strings
);
398 if (fclose(mesgwrit
) == EOF
)
399 err(4, "%s", strings
);
403 found(int new, off_t off
, char *str
)
408 fprintf(stderr
, "found at %d:", (int) off
);
410 fprintf(stderr
, "new at %d:", (int) off
);
412 fprintf(stderr
, "\n");
420 while ((c
= (*cp
++ & 0377)))
422 fprintf(stderr
, "^%c", c
+ '`');
424 fprintf(stderr
, "^?");
426 fprintf(stderr
, "\\%03o", c
);
428 fprintf(stderr
, "%c", c
);
434 FILE *strf
= fopen(strings
, "r");
438 err(5, "%s", strings
);
439 xdotcf
= fopen("xs.c", "w");
442 fprintf(xdotcf
, "char\txstr[] = {\n");
446 for (i
= 0; i
< 8; i
++) {
453 fprintf(xdotcf
, "\n");
456 fprintf(xdotcf
, "0x%02x,", c
);
458 fprintf(xdotcf
, "\n");
461 fprintf(xdotcf
, "};\n");
462 ignore(fclose(xdotcf
));
463 ignore(fclose(strf
));
470 while (cp
[0] && cp
[1])
476 istail(char *str
, char *of
)
478 int d
= strlen(of
) - strlen(str
);
480 if (d
< 0 || strcmp(&of
[d
], str
) != 0)
486 onintr(int dummy __unused
)
489 ignore(signal(SIGINT
, SIG_IGN
));
490 if (strings
[0] == '/')
491 ignore(unlink(strings
));
492 ignore(unlink("x.c"));
493 ignore(unlink("xs.c"));