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