xstr.c (7799f52a32f592a7efe259bc3411ba52d13db797) | xstr.c (7720a19d5d7456232e8e11909dd72d31ced622d1) |
---|---|
1/* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 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 --- 18 unchanged lines hidden (view full) --- 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 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint | 1/* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 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 --- 18 unchanged lines hidden (view full) --- 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 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint |
35static char copyright[] = | 35static const char copyright[] = |
36"@(#) Copyright (c) 1980, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint | 36"@(#) Copyright (c) 1980, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint |
41#if 0 |
|
41static char sccsid[] = "@(#)xstr.c 8.1 (Berkeley) 6/9/93"; | 42static char sccsid[] = "@(#)xstr.c 8.1 (Berkeley) 6/9/93"; |
43#endif 44static const char rcsid[] = 45 "$Id$"; |
|
42#endif /* not lint */ 43 44#include <sys/types.h> | 46#endif /* not lint */ 47 48#include <sys/types.h> |
45#include <signal.h> 46#include <errno.h> 47#include <unistd.h> 48#include <stdio.h> | |
49#include <ctype.h> | 49#include <ctype.h> |
50#include <err.h> 51#include <stdio.h> 52#include <stdlib.h> 53#include <signal.h> |
|
50#include <string.h> | 54#include <string.h> |
55#include <unistd.h> |
|
51#include "pathnames.h" 52 53/* 54 * xstr - extract and hash strings in a C program 55 * 56 * Bill Joy UCB 57 * November, 1978 58 */ --- 8 unchanged lines hidden (view full) --- 67 68off_t mesgpt; 69char *strings = "strings"; 70 71int cflg; 72int vflg; 73int readstd; 74 | 56#include "pathnames.h" 57 58/* 59 * xstr - extract and hash strings in a C program 60 * 61 * Bill Joy UCB 62 * November, 1978 63 */ --- 8 unchanged lines hidden (view full) --- 72 73off_t mesgpt; 74char *strings = "strings"; 75 76int cflg; 77int vflg; 78int readstd; 79 |
80static void usage __P((void)); 81int istail __P((char *, char *)); 82char lastchr __P((char *)); 83void xsdotc __P((void)); 84void prstr __P((char *)); 85void found __P((int, off_t, char *)); 86void flushsh __P((void)); 87int xgetc __P((FILE *)); 88int fgetNUL __P((char *, int, FILE *)); 89void inithash __P((void)); 90int octdigit __P((char)); 91void process __P((char *)); 92 93int |
|
75main(argc, argv) 76 int argc; 77 char *argv[]; 78{ | 94main(argc, argv) 95 int argc; 96 char *argv[]; 97{ |
98 int c; |
|
79 | 99 |
80 argc--, argv++; 81 while (argc > 0 && argv[0][0] == '-') { 82 register char *cp = &(*argv++)[1]; 83 84 argc--; 85 if (*cp == 0) { | 100 while ((c = getopt(argc, argv, "-cv")) != -1) 101 switch (c) { 102 case '-': |
86 readstd++; | 103 readstd++; |
87 continue; 88 } 89 do switch (*cp++) { 90 | 104 break; |
91 case 'c': 92 cflg++; | 105 case 'c': 106 cflg++; |
93 continue; 94 | 107 break; |
95 case 'v': 96 vflg++; | 108 case 'v': 109 vflg++; |
97 continue; 98 | 110 break; |
99 default: | 111 default: |
100 fprintf(stderr, "usage: xstr [ -v ] [ -c ] [ - ] [ name ... ]\n"); 101 } while (*cp); 102 } | 112 usage(); 113 } 114 argc -= optind; 115 argv += optind; 116 |
103 if (signal(SIGINT, SIG_IGN) == SIG_DFL) 104 signal(SIGINT, onintr); | 117 if (signal(SIGINT, SIG_IGN) == SIG_DFL) 118 signal(SIGINT, onintr); |
105 if (cflg || argc == 0 && !readstd) | 119 if (cflg || (argc == 0 && !readstd)) |
106 inithash(); 107 else 108 strings = mktemp(strdup(_PATH_TMP)); 109 while (readstd || argc > 0) { 110 if (freopen("x.c", "w", stdout) == NULL) | 120 inithash(); 121 else 122 strings = mktemp(strdup(_PATH_TMP)); 123 while (readstd || argc > 0) { 124 if (freopen("x.c", "w", stdout) == NULL) |
111 perror("x.c"), exit(1); | 125 err(1, "x.c"); |
112 if (!readstd && freopen(argv[0], "r", stdin) == NULL) | 126 if (!readstd && freopen(argv[0], "r", stdin) == NULL) |
113 perror(argv[0]), exit(2); | 127 err(2, "%s", argv[0]); |
114 process("x.c"); 115 if (readstd == 0) 116 argc--, argv++; 117 else 118 readstd = 0; 119 }; 120 flushsh(); 121 if (cflg == 0) 122 xsdotc(); 123 if (strings[0] == '/') 124 ignore(unlink(strings)); 125 exit(0); 126} 127 | 128 process("x.c"); 129 if (readstd == 0) 130 argc--, argv++; 131 else 132 readstd = 0; 133 }; 134 flushsh(); 135 if (cflg == 0) 136 xsdotc(); 137 if (strings[0] == '/') 138 ignore(unlink(strings)); 139 exit(0); 140} 141 |
142static void 143usage() 144{ 145 fprintf(stderr, "usage: xstr [-v] [-c] [-] [name ...]\n"); 146 exit (1); 147} 148 |
|
128char linebuf[BUFSIZ]; 129 | 149char linebuf[BUFSIZ]; 150 |
151void |
|
130process(name) 131 char *name; 132{ 133 char *cp; 134 register int c; 135 register int incomm = 0; 136 int ret; 137 138 printf("extern char\txstr[];\n"); 139 for (;;) { 140 if (fgets(linebuf, sizeof linebuf, stdin) == NULL) { | 152process(name) 153 char *name; 154{ 155 char *cp; 156 register int c; 157 register int incomm = 0; 158 int ret; 159 160 printf("extern char\txstr[];\n"); 161 for (;;) { 162 if (fgets(linebuf, sizeof linebuf, stdin) == NULL) { |
141 if (ferror(stdin)) { 142 perror(name); 143 exit(3); 144 } | 163 if (ferror(stdin)) 164 err(3, "%s", name); |
145 break; 146 } 147 if (linebuf[0] == '#') { 148 if (linebuf[1] == ' ' && isdigit(linebuf[2])) 149 printf("#line%s", &linebuf[1]); 150 else 151 printf("%s", linebuf); 152 continue; 153 } | 165 break; 166 } 167 if (linebuf[0] == '#') { 168 if (linebuf[1] == ' ' && isdigit(linebuf[2])) 169 printf("#line%s", &linebuf[1]); 170 else 171 printf("%s", linebuf); 172 continue; 173 } |
154 for (cp = linebuf; c = *cp++;) switch (c) { | 174 for (cp = linebuf; (c = *cp++);) switch (c) { |
155 156 case '"': 157 if (incomm) 158 goto def; 159 if ((ret = (int) yankstr(&cp)) == -1) 160 goto out; 161 printf("(&xstr[%d])", ret); 162 break; --- 26 unchanged lines hidden (view full) --- 189def: 190 default: 191 putchar(c); 192 break; 193 } 194 } 195out: 196 if (ferror(stdout)) | 175 176 case '"': 177 if (incomm) 178 goto def; 179 if ((ret = (int) yankstr(&cp)) == -1) 180 goto out; 181 printf("(&xstr[%d])", ret); 182 break; --- 26 unchanged lines hidden (view full) --- 209def: 210 default: 211 putchar(c); 212 break; 213 } 214 } 215out: 216 if (ferror(stdout)) |
197 perror("x.c"), onintr(); | 217 warn("x.c"), onintr(); |
198} 199 200off_t 201yankstr(cpp) 202 register char **cpp; 203{ 204 register char *cp = *cpp; 205 register int c, ch; 206 char dbuf[BUFSIZ]; 207 register char *dp = dbuf; 208 register char *tp; 209 | 218} 219 220off_t 221yankstr(cpp) 222 register char **cpp; 223{ 224 register char *cp = *cpp; 225 register int c, ch; 226 char dbuf[BUFSIZ]; 227 register char *dp = dbuf; 228 register char *tp; 229 |
210 while (c = *cp++) { | 230 while ((c = *cp++)) { |
211 switch (c) { 212 213 case '"': 214 cp++; 215 goto out; 216 217 case '\\': 218 c = *cp++; 219 if (c == 0) 220 break; 221 if (c == '\n') { 222 if (fgets(linebuf, sizeof linebuf, stdin) 223 == NULL) { | 231 switch (c) { 232 233 case '"': 234 cp++; 235 goto out; 236 237 case '\\': 238 c = *cp++; 239 if (c == 0) 240 break; 241 if (c == '\n') { 242 if (fgets(linebuf, sizeof linebuf, stdin) 243 == NULL) { |
224 if (ferror(stdin)) { 225 perror("x.c"); 226 exit(3); 227 } | 244 if (ferror(stdin)) 245 err(3, "x.c"); |
228 return(-1); 229 } 230 cp = linebuf; 231 continue; 232 } | 246 return(-1); 247 } 248 cp = linebuf; 249 continue; 250 } |
233 for (tp = "b\bt\tr\rn\nf\f\\\\\"\""; ch = *tp++; tp++) | 251 for (tp = "b\bt\tr\rn\nf\f\\\\\"\""; (ch = *tp++); tp++) |
234 if (c == ch) { 235 c = *tp; 236 goto gotc; 237 } 238 if (!octdigit(c)) { 239 *dp++ = '\\'; 240 break; 241 } --- 10 unchanged lines hidden (view full) --- 252 *dp++ = c; 253 } 254out: 255 *cpp = --cp; 256 *dp = 0; 257 return (hashit(dbuf, 1)); 258} 259 | 252 if (c == ch) { 253 c = *tp; 254 goto gotc; 255 } 256 if (!octdigit(c)) { 257 *dp++ = '\\'; 258 break; 259 } --- 10 unchanged lines hidden (view full) --- 270 *dp++ = c; 271 } 272out: 273 *cpp = --cp; 274 *dp = 0; 275 return (hashit(dbuf, 1)); 276} 277 |
278int |
|
260octdigit(c) 261 char c; 262{ | 279octdigit(c) 280 char c; 281{ |
263 | |
264 return (isdigit(c) && c != '8' && c != '9'); 265} 266 | 282 return (isdigit(c) && c != '8' && c != '9'); 283} 284 |
285void |
|
267inithash() 268{ 269 char buf[BUFSIZ]; 270 register FILE *mesgread = fopen(strings, "r"); 271 272 if (mesgread == NULL) 273 return; 274 for (;;) { 275 mesgpt = tellpt; | 286inithash() 287{ 288 char buf[BUFSIZ]; 289 register FILE *mesgread = fopen(strings, "r"); 290 291 if (mesgread == NULL) 292 return; 293 for (;;) { 294 mesgpt = tellpt; |
276 if (fgetNUL(buf, sizeof buf, mesgread) == NULL) | 295 if (fgetNUL(buf, sizeof buf, mesgread) == 0) |
277 break; 278 ignore(hashit(buf, 0)); 279 } 280 ignore(fclose(mesgread)); 281} 282 | 296 break; 297 ignore(hashit(buf, 0)); 298 } 299 ignore(fclose(mesgread)); 300} 301 |
302int |
|
283fgetNUL(obuf, rmdr, file) 284 char *obuf; 285 register int rmdr; 286 FILE *file; 287{ 288 register c; 289 register char *buf = obuf; 290 291 while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF) 292 *buf++ = c; 293 *buf++ = 0; | 303fgetNUL(obuf, rmdr, file) 304 char *obuf; 305 register int rmdr; 306 FILE *file; 307{ 308 register c; 309 register char *buf = obuf; 310 311 while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF) 312 *buf++ = c; 313 *buf++ = 0; |
294 return ((feof(file) || ferror(file)) ? NULL : 1); | 314 return ((feof(file) || ferror(file)) ? 0 : 1); |
295} 296 | 315} 316 |
317int |
|
297xgetc(file) 298 FILE *file; 299{ 300 301 tellpt++; 302 return (getc(file)); 303} 304 --- 16 unchanged lines hidden (view full) --- 321 322 hp = hp0 = &bucket[lastchr(str) & 0177]; 323 while (hp->hnext) { 324 hp = hp->hnext; 325 i = istail(str, hp->hstr); 326 if (i >= 0) 327 return (hp->hpt + i); 328 } | 318xgetc(file) 319 FILE *file; 320{ 321 322 tellpt++; 323 return (getc(file)); 324} 325 --- 16 unchanged lines hidden (view full) --- 342 343 hp = hp0 = &bucket[lastchr(str) & 0177]; 344 while (hp->hnext) { 345 hp = hp->hnext; 346 i = istail(str, hp->hstr); 347 if (i >= 0) 348 return (hp->hpt + i); 349 } |
329 if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL) { 330 perror("xstr"); 331 exit(8); 332 } | 350 if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL) 351 errx(8, "calloc"); |
333 hp->hpt = mesgpt; | 352 hp->hpt = mesgpt; |
334 if (!(hp->hstr = strdup(str))) { 335 (void)fprintf(stderr, "xstr: %s\n", strerror(errno)); 336 exit(1); 337 } | 353 if (!(hp->hstr = strdup(str))) 354 err(1, NULL); |
338 mesgpt += strlen(hp->hstr) + 1; 339 hp->hnext = hp0->hnext; 340 hp->hnew = new; 341 hp0->hnext = hp; 342 return (hp->hpt); 343} 344 | 355 mesgpt += strlen(hp->hstr) + 1; 356 hp->hnext = hp0->hnext; 357 hp->hnew = new; 358 hp0->hnext = hp; 359 return (hp->hpt); 360} 361 |
362void |
|
345flushsh() 346{ 347 register int i; 348 register struct hash *hp; 349 register FILE *mesgwrit; 350 register int old = 0, new = 0; 351 352 for (i = 0; i < BUCKETS; i++) --- 9 unchanged lines hidden (view full) --- 362 perror(strings), exit(4); 363 for (i = 0; i < BUCKETS; i++) 364 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) { 365 found(hp->hnew, hp->hpt, hp->hstr); 366 if (hp->hnew) { 367 fseek(mesgwrit, hp->hpt, 0); 368 ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, mesgwrit)); 369 if (ferror(mesgwrit)) | 363flushsh() 364{ 365 register int i; 366 register struct hash *hp; 367 register FILE *mesgwrit; 368 register int old = 0, new = 0; 369 370 for (i = 0; i < BUCKETS; i++) --- 9 unchanged lines hidden (view full) --- 380 perror(strings), exit(4); 381 for (i = 0; i < BUCKETS; i++) 382 for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) { 383 found(hp->hnew, hp->hpt, hp->hstr); 384 if (hp->hnew) { 385 fseek(mesgwrit, hp->hpt, 0); 386 ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, mesgwrit)); 387 if (ferror(mesgwrit)) |
370 perror(strings), exit(4); | 388 err(4, "%s", strings); |
371 } 372 } 373 if (fclose(mesgwrit) == EOF) | 389 } 390 } 391 if (fclose(mesgwrit) == EOF) |
374 perror(strings), exit(4); | 392 err(4, "%s", strings); |
375} 376 | 393} 394 |
395void |
|
377found(new, off, str) 378 int new; 379 off_t off; 380 char *str; 381{ 382 if (vflg == 0) 383 return; 384 if (!new) 385 fprintf(stderr, "found at %d:", (int) off); 386 else 387 fprintf(stderr, "new at %d:", (int) off); 388 prstr(str); 389 fprintf(stderr, "\n"); 390} 391 | 396found(new, off, str) 397 int new; 398 off_t off; 399 char *str; 400{ 401 if (vflg == 0) 402 return; 403 if (!new) 404 fprintf(stderr, "found at %d:", (int) off); 405 else 406 fprintf(stderr, "new at %d:", (int) off); 407 prstr(str); 408 fprintf(stderr, "\n"); 409} 410 |
411void |
|
392prstr(cp) 393 register char *cp; 394{ 395 register int c; 396 | 412prstr(cp) 413 register char *cp; 414{ 415 register int c; 416 |
397 while (c = (*cp++ & 0377)) | 417 while ((c = (*cp++ & 0377))) |
398 if (c < ' ') 399 fprintf(stderr, "^%c", c + '`'); 400 else if (c == 0177) 401 fprintf(stderr, "^?"); 402 else if (c > 0200) 403 fprintf(stderr, "\\%03o", c); 404 else 405 fprintf(stderr, "%c", c); 406} 407 | 418 if (c < ' ') 419 fprintf(stderr, "^%c", c + '`'); 420 else if (c == 0177) 421 fprintf(stderr, "^?"); 422 else if (c > 0200) 423 fprintf(stderr, "\\%03o", c); 424 else 425 fprintf(stderr, "%c", c); 426} 427 |
428void |
|
408xsdotc() 409{ 410 register FILE *strf = fopen(strings, "r"); 411 register FILE *xdotcf; 412 413 if (strf == NULL) | 429xsdotc() 430{ 431 register FILE *strf = fopen(strings, "r"); 432 register FILE *xdotcf; 433 434 if (strf == NULL) |
414 perror(strings), exit(5); | 435 err(5, "%s", strings); |
415 xdotcf = fopen("xs.c", "w"); 416 if (xdotcf == NULL) | 436 xdotcf = fopen("xs.c", "w"); 437 if (xdotcf == NULL) |
417 perror("xs.c"), exit(6); | 438 err(6, "xs.c"); |
418 fprintf(xdotcf, "char\txstr[] = {\n"); 419 for (;;) { 420 register int i, c; 421 422 for (i = 0; i < 8; i++) { 423 c = getc(strf); 424 if (ferror(strf)) { | 439 fprintf(xdotcf, "char\txstr[] = {\n"); 440 for (;;) { 441 register int i, c; 442 443 for (i = 0; i < 8; i++) { 444 c = getc(strf); 445 if (ferror(strf)) { |
425 perror(strings); | 446 warn("%s", strings); |
426 onintr(); 427 } 428 if (feof(strf)) { 429 fprintf(xdotcf, "\n"); 430 goto out; 431 } 432 fprintf(xdotcf, "0x%02x,", c); 433 } 434 fprintf(xdotcf, "\n"); 435 } 436out: 437 fprintf(xdotcf, "};\n"); 438 ignore(fclose(xdotcf)); 439 ignore(fclose(strf)); 440} 441 | 447 onintr(); 448 } 449 if (feof(strf)) { 450 fprintf(xdotcf, "\n"); 451 goto out; 452 } 453 fprintf(xdotcf, "0x%02x,", c); 454 } 455 fprintf(xdotcf, "\n"); 456 } 457out: 458 fprintf(xdotcf, "};\n"); 459 ignore(fclose(xdotcf)); 460 ignore(fclose(strf)); 461} 462 |
463char |
|
442lastchr(cp) 443 register char *cp; 444{ 445 446 while (cp[0] && cp[1]) 447 cp++; 448 return (*cp); 449} 450 | 464lastchr(cp) 465 register char *cp; 466{ 467 468 while (cp[0] && cp[1]) 469 cp++; 470 return (*cp); 471} 472 |
473int |
|
451istail(str, of) 452 register char *str, *of; 453{ 454 register int d = strlen(of) - strlen(str); 455 456 if (d < 0 || strcmp(&of[d], str) != 0) 457 return (-1); 458 return (d); --- 13 unchanged lines hidden --- | 474istail(str, of) 475 register char *str, *of; 476{ 477 register int d = strlen(of) - strlen(str); 478 479 if (d < 0 || strcmp(&of[d], str) != 0) 480 return (-1); 481 return (d); --- 13 unchanged lines hidden --- |