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.
15 #pragma ident "%Z%%M% %I% %E% SMI"
19 #include <sys/types.h>
26 * xstr - extract and hash strings in a C program
33 off_t
hashit(char *, int);
35 char *savestr(char *);
36 off_t
yankstr(char **);
42 void found(int, off_t
, char *);
45 int fgetNUL(char *, int, FILE *);
48 int istail(char *, char *);
51 char *strings
= "strings";
60 main(int argc
, char **argv
)
63 while (argc
> 0 && argv
[0][0] == '-') {
64 char *cp
= &(*argv
++)[1];
87 (void) fprintf(stderr
,
88 "usage: xstr [ -v ] [ -c ] [ -l label ] [ - ] [ name ... ]\n");
91 if (signal(SIGINT
, SIG_IGN
) == SIG_DFL
)
92 (void) signal(SIGINT
, (void (*)(int))onintr
);
93 if (cflg
|| argc
== 0 && !readstd
)
96 strings
= savestr("/tmp/xstrXXXXXX");
97 tmpfd
= mkstemp(strings
);
100 (void) free(strings
);
105 while (readstd
|| argc
> 0) {
106 if (freopen("x.c", "w", stdout
) == NULL
)
107 perror("x.c"), (void) cleanup(), exit(1);
108 if (!readstd
&& freopen(argv
[0], "r", stdin
) == NULL
)
109 perror(argv
[0]), (void) cleanup(), exit(2);
123 char linebuf
[BUFSIZ
];
133 (void) printf("extern char\t%s[];\n", xname
);
135 if (fgets(linebuf
, sizeof (linebuf
), stdin
) == NULL
) {
143 if (linebuf
[0] == '#') {
144 if (linebuf
[1] == ' ' && isdigit(linebuf
[2]))
145 (void) printf("#line%s", &linebuf
[1]);
147 (void) printf("%s", linebuf
);
150 for (cp
= linebuf
; (c
= *cp
++) != 0; ) {
155 if ((ret
= (int)yankstr(&cp
)) == -1)
157 (void) printf("(&%s[%d])", xname
, ret
);
165 (void) putchar(*cp
++);
169 if (incomm
|| *cp
!= '*')
177 if (incomm
&& *cp
== '/') {
193 perror("x.c"), onintr();
205 while ((c
= *cp
++) != 0) {
217 if (fgets(linebuf
, sizeof (linebuf
), stdin
)
230 for (tp
= "b\bt\tr\rn\nf\f\\\\\"\""; (ch
= *tp
++) != 0;
243 c
<<= 3, c
+= *cp
++ - '0';
246 c
<<= 3, c
+= *cp
++ - '0';
255 return (hashit(dbuf
, 1));
262 return (isdigit(c
) && c
!= '8' && c
!= '9');
269 FILE *mesgread
= fopen(strings
, "r");
271 if (mesgread
== NULL
)
275 if (fgetNUL(buf
, sizeof (buf
), mesgread
) == NULL
)
277 (void) hashit(buf
, 0);
279 (void) fclose(mesgread
);
283 fgetNUL(char *obuf
, int rmdr
, FILE *file
)
288 while (--rmdr
> 0 && (c
= xgetc(file
)) != 0 && c
!= EOF
)
291 return ((feof(file
) || ferror(file
)) ? NULL
: 1);
312 hashit(char *str
, int new)
315 struct hash
*hp
, *hp0
;
317 hp
= hp0
= &bucket
[lastchr(str
) & 0177];
320 i
= istail(str
, hp
->hstr
);
322 return (hp
->hpt
+ i
);
324 if ((hp
= calloc(1, sizeof (*hp
))) == NULL
) {
330 hp
->hstr
= savestr(str
);
331 mesgpt
+= strlen(hp
->hstr
) + 1;
332 hp
->hnext
= hp0
->hnext
;
344 int old
= 0, new = 0;
346 for (i
= 0; i
< BUCKETS
; i
++)
347 for (hp
= bucket
[i
].hnext
; hp
!= NULL
; hp
= hp
->hnext
)
352 if (new == 0 && old
!= 0)
354 mesgwrit
= fopen(strings
, old
? "r+" : "w");
355 if (mesgwrit
== NULL
)
356 perror(strings
), (void) cleanup(), exit(4);
357 for (i
= 0; i
< BUCKETS
; i
++)
358 for (hp
= bucket
[i
].hnext
; hp
!= NULL
; hp
= hp
->hnext
) {
359 found(hp
->hnew
, hp
->hpt
, hp
->hstr
);
361 (void) fseek(mesgwrit
, hp
->hpt
, 0);
362 (void) fwrite(hp
->hstr
,
363 strlen(hp
->hstr
) + 1, 1, mesgwrit
);
364 if (ferror(mesgwrit
)) {
371 if (fclose(mesgwrit
) == EOF
)
372 perror(strings
), (void) cleanup(), exit(4);
376 found(int new, off_t off
, char *str
)
381 (void) fprintf(stderr
, "found at %d:", (int)off
);
383 (void) fprintf(stderr
, "new at %d:", (int)off
);
385 (void) fprintf(stderr
, "\n");
393 while ((c
= (*cp
++ & 0377)) != 0)
395 (void) fprintf(stderr
, "^%c", c
+ '`');
397 (void) fprintf(stderr
, "^?");
399 (void) fprintf(stderr
, "\\%03o", c
);
401 (void) fprintf(stderr
, "%c", c
);
407 FILE *strf
= fopen(strings
, "r");
411 perror(strings
), exit(5);
412 xdotcf
= fopen("xs.c", "w");
414 perror("xs.c"), exit(6);
415 (void) fprintf(xdotcf
, "char\t%s[] = {\n", xname
);
419 for (i
= 0; i
< 8; i
++) {
426 (void) fprintf(xdotcf
, "\n");
429 (void) fprintf(xdotcf
, "0x%02x,", c
);
431 (void) fprintf(xdotcf
, "\n");
434 (void) fprintf(xdotcf
, "};\n");
435 (void) fclose(xdotcf
);
444 if ((dp
= calloc(1, strlen(cp
) + 1)) == NULL
) {
448 return (strcpy(dp
, cp
));
455 while (cp
[0] && cp
[1])
461 istail(char *str
, char *of
)
463 int d
= strlen(of
) - strlen(str
);
465 if (d
< 0 || strcmp(&of
[d
], str
) != 0)
474 (void) signal(SIGINT
, SIG_IGN
);
476 (void) unlink("x.c");
477 (void) unlink("xs.c");
484 if (strings
[0] == '/') {
485 (void) unlink(strings
);