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