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