2 * Copyright 1989 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
10 * Copyright (c) 1980 Regents of the University of California.
11 * All rights reserved. The Berkeley software License Agreement
12 * specifies the terms and conditions for redistribution.
17 #include <sys/types.h>
24 * xstr - extract and hash strings in a C program
31 off_t
hashit(char *, int);
33 char *savestr(char *);
34 off_t
yankstr(char **);
40 void found(int, off_t
, char *);
43 int fgetNUL(char *, int, FILE *);
46 int istail(char *, char *);
49 char *strings
= "strings";
58 main(int argc
, char **argv
)
61 while (argc
> 0 && argv
[0][0] == '-') {
62 char *cp
= &(*argv
++)[1];
85 (void) fprintf(stderr
,
86 "usage: xstr [ -v ] [ -c ] [ -l label ] [ - ] [ name ... ]\n");
89 if (signal(SIGINT
, SIG_IGN
) == SIG_DFL
)
90 (void) signal(SIGINT
, (void (*)(int))onintr
);
91 if (cflg
|| argc
== 0 && !readstd
)
94 strings
= savestr("/tmp/xstrXXXXXX");
95 tmpfd
= mkstemp(strings
);
103 while (readstd
|| argc
> 0) {
104 if (freopen("x.c", "w", stdout
) == NULL
)
105 perror("x.c"), (void) cleanup(), exit(1);
106 if (!readstd
&& freopen(argv
[0], "r", stdin
) == NULL
)
107 perror(argv
[0]), (void) cleanup(), exit(2);
121 char linebuf
[BUFSIZ
];
131 (void) printf("extern char\t%s[];\n", xname
);
133 if (fgets(linebuf
, sizeof (linebuf
), stdin
) == NULL
) {
141 if (linebuf
[0] == '#') {
142 if (linebuf
[1] == ' ' && isdigit(linebuf
[2]))
143 (void) printf("#line%s", &linebuf
[1]);
145 (void) printf("%s", linebuf
);
148 for (cp
= linebuf
; (c
= *cp
++) != 0; ) {
153 if ((ret
= (int)yankstr(&cp
)) == -1)
155 (void) printf("(&%s[%d])", xname
, ret
);
163 (void) putchar(*cp
++);
167 if (incomm
|| *cp
!= '*')
175 if (incomm
&& *cp
== '/') {
191 perror("x.c"), onintr();
203 while ((c
= *cp
++) != 0) {
215 if (fgets(linebuf
, sizeof (linebuf
), stdin
)
228 for (tp
= "b\bt\tr\rn\nf\f\\\\\"\""; (ch
= *tp
++) != 0;
241 c
<<= 3, c
+= *cp
++ - '0';
244 c
<<= 3, c
+= *cp
++ - '0';
253 return (hashit(dbuf
, 1));
260 return (isdigit(c
) && c
!= '8' && c
!= '9');
267 FILE *mesgread
= fopen(strings
, "r");
269 if (mesgread
== NULL
)
273 if (fgetNUL(buf
, sizeof (buf
), mesgread
) == 0)
275 (void) hashit(buf
, 0);
277 (void) fclose(mesgread
);
281 fgetNUL(char *obuf
, int rmdr
, FILE *file
)
286 while (--rmdr
> 0 && (c
= xgetc(file
)) != 0 && c
!= EOF
)
289 return ((feof(file
) || ferror(file
)) ? 0 : 1);
310 hashit(char *str
, int new)
313 struct hash
*hp
, *hp0
;
315 hp
= hp0
= &bucket
[lastchr(str
) & 0177];
318 i
= istail(str
, hp
->hstr
);
320 return (hp
->hpt
+ i
);
322 if ((hp
= calloc(1, sizeof (*hp
))) == NULL
) {
328 hp
->hstr
= savestr(str
);
329 mesgpt
+= strlen(hp
->hstr
) + 1;
330 hp
->hnext
= hp0
->hnext
;
342 int old
= 0, new = 0;
344 for (i
= 0; i
< BUCKETS
; i
++)
345 for (hp
= bucket
[i
].hnext
; hp
!= NULL
; hp
= hp
->hnext
)
350 if (new == 0 && old
!= 0)
352 mesgwrit
= fopen(strings
, old
? "r+" : "w");
353 if (mesgwrit
== NULL
)
354 perror(strings
), (void) cleanup(), exit(4);
355 for (i
= 0; i
< BUCKETS
; i
++)
356 for (hp
= bucket
[i
].hnext
; hp
!= NULL
; hp
= hp
->hnext
) {
357 found(hp
->hnew
, hp
->hpt
, hp
->hstr
);
359 (void) fseek(mesgwrit
, hp
->hpt
, 0);
360 (void) fwrite(hp
->hstr
,
361 strlen(hp
->hstr
) + 1, 1, mesgwrit
);
362 if (ferror(mesgwrit
)) {
369 if (fclose(mesgwrit
) == EOF
)
370 perror(strings
), (void) cleanup(), exit(4);
374 found(int new, off_t off
, char *str
)
379 (void) fprintf(stderr
, "found at %d:", (int)off
);
381 (void) fprintf(stderr
, "new at %d:", (int)off
);
383 (void) fprintf(stderr
, "\n");
391 while ((c
= (*cp
++ & 0377)) != 0)
393 (void) fprintf(stderr
, "^%c", c
+ '`');
395 (void) fprintf(stderr
, "^?");
397 (void) fprintf(stderr
, "\\%03o", c
);
399 (void) fprintf(stderr
, "%c", c
);
405 FILE *strf
= fopen(strings
, "r");
409 perror(strings
), exit(5);
410 xdotcf
= fopen("xs.c", "w");
412 perror("xs.c"), exit(6);
413 (void) fprintf(xdotcf
, "char\t%s[] = {\n", xname
);
417 for (i
= 0; i
< 8; i
++) {
424 (void) fprintf(xdotcf
, "\n");
427 (void) fprintf(xdotcf
, "0x%02x,", c
);
429 (void) fprintf(xdotcf
, "\n");
432 (void) fprintf(xdotcf
, "};\n");
433 (void) fclose(xdotcf
);
442 if ((dp
= calloc(1, strlen(cp
) + 1)) == NULL
) {
446 return (strcpy(dp
, cp
));
453 while (cp
[0] && cp
[1])
459 istail(char *str
, char *of
)
461 int d
= strlen(of
) - strlen(str
);
463 if (d
< 0 || strcmp(&of
[d
], str
) != 0)
472 (void) signal(SIGINT
, SIG_IGN
);
474 (void) unlink("x.c");
475 (void) unlink("xs.c");
482 if (strings
[0] == '/') {
483 (void) unlink(strings
);