* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
static char sccsid
[] = "@(#)xstr.c 5.3 (Berkeley) %G%";
* xstr - extract and hash strings in a C program
#define ignore(a) ((void) a)
char *strings
= "strings";
while (argc
> 0 && argv
[0][0] == '-') {
register char *cp
= &(*argv
++)[1];
fprintf(stderr
, "usage: xstr [ -v ] [ -c ] [ - ] [ name ... ]\n");
if (signal(SIGINT
, SIG_IGN
) == SIG_DFL
)
if (cflg
|| argc
== 0 && !readstd
)
strings
= mktemp(savestr("/tmp/xstrXXXXXX"));
while (readstd
|| argc
> 0) {
if (freopen("x.c", "w", stdout
) == NULL
)
if (!readstd
&& freopen(argv
[0], "r", stdin
) == NULL
)
perror(argv
[0]), exit(2);
printf("extern char\txstr[];\n");
if (fgets(linebuf
, sizeof linebuf
, stdin
) == NULL
) {
if (linebuf
[1] == ' ' && isdigit(linebuf
[2]))
printf("#line%s", &linebuf
[1]);
for (cp
= linebuf
; c
= *cp
++;) switch (c
) {
if ((ret
= (int) yankstr(&cp
)) == -1)
printf("(&xstr[%d])", ret
);
if (incomm
|| *cp
!= '*')
if (incomm
&& *cp
== '/') {
register char *cp
= *cpp
;
register char *dp
= dbuf
;
if (fgets(linebuf
, sizeof linebuf
, stdin
)
for (tp
= "b\bt\tr\rn\nf\f\\\\\"\""; ch
= *tp
++; tp
++)
c
<<= 3, c
+= *cp
++ - '0';
c
<<= 3, c
+= *cp
++ - '0';
return (hashit(dbuf
, 1));
return (isdigit(c
) && c
!= '8' && c
!= '9');
register FILE *mesgread
= fopen(strings
, "r");
if (fgetNUL(buf
, sizeof buf
, mesgread
) == NULL
)
ignore(fclose(mesgread
));
fgetNUL(obuf
, rmdr
, file
)
register char *buf
= obuf
;
while (--rmdr
> 0 && (c
= xgetc(file
)) != 0 && c
!= EOF
)
return ((feof(file
) || ferror(file
)) ? NULL
: 1);
register struct hash
*hp
, *hp0
;
hp
= hp0
= &bucket
[lastchr(str
) & 0177];
i
= istail(str
, hp
->hstr
);
if ((hp
= (struct hash
*) calloc(1, sizeof (*hp
))) == NULL
) {
mesgpt
+= strlen(hp
->hstr
) + 1;
register struct hash
*hp
;
register int old
= 0, new = 0;
for (i
= 0; i
< BUCKETS
; i
++)
for (hp
= bucket
[i
].hnext
; hp
!= NULL
; hp
= hp
->hnext
)
if (new == 0 && old
!= 0)
mesgwrit
= fopen(strings
, old
? "r+" : "w");
perror(strings
), exit(4);
for (i
= 0; i
< BUCKETS
; i
++)
for (hp
= bucket
[i
].hnext
; hp
!= NULL
; hp
= hp
->hnext
) {
found(hp
->hnew
, hp
->hpt
, hp
->hstr
);
fseek(mesgwrit
, hp
->hpt
, 0);
ignore(fwrite(hp
->hstr
, strlen(hp
->hstr
) + 1, 1, mesgwrit
));
perror(strings
), exit(4);
if (fclose(mesgwrit
) == EOF
)
perror(strings
), exit(4);
fprintf(stderr
, "found at %d:", (int) off
);
fprintf(stderr
, "new at %d:", (int) off
);
while (c
= (*cp
++ & 0377))
fprintf(stderr
, "^%c", c
+ '`');
fprintf(stderr
, "\\%03o", c
);
fprintf(stderr
, "%c", c
);
register FILE *strf
= fopen(strings
, "r");
perror(strings
), exit(5);
xdotcf
= fopen("xs.c", "w");
fprintf(xdotcf
, "char\txstr[] = {\n");
for (i
= 0; i
< 8; i
++) {
fprintf(xdotcf
, "0x%02x,", c
);
if ((dp
= (char *) calloc(1, strlen(cp
) + 1)) == NULL
) {
register int d
= strlen(of
) - strlen(str
);
if (d
< 0 || strcmp(&of
[d
], str
) != 0)
ignore(signal(SIGINT
, SIG_IGN
));