18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 49b50d902SRodney W. Grimes * Copyright (c) 1980, 1993 59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 69b50d902SRodney W. Grimes * 79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 99b50d902SRodney W. Grimes * are met: 109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 179b50d902SRodney W. Grimes * without specific prior written permission. 189b50d902SRodney W. Grimes * 199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 299b50d902SRodney W. Grimes * SUCH DAMAGE. 309b50d902SRodney W. Grimes */ 319b50d902SRodney W. Grimes 32c451c0e6SMark Murray #include <sys/cdefs.h> 33c451c0e6SMark Murray 34c451c0e6SMark Murray __FBSDID("$FreeBSD$"); 35c451c0e6SMark Murray 369b50d902SRodney W. Grimes #ifndef lint 377720a19dSPhilippe Charnier static const char copyright[] = 389b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1993\n\ 399b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 40c451c0e6SMark Murray #endif 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes #ifndef lint 43c451c0e6SMark Murray static const char sccsid[] = "@(#)xstr.c 8.1 (Berkeley) 6/9/93"; 447720a19dSPhilippe Charnier #endif 459b50d902SRodney W. Grimes 469b50d902SRodney W. Grimes #include <sys/types.h> 47c451c0e6SMark Murray 489b50d902SRodney W. Grimes #include <ctype.h> 497720a19dSPhilippe Charnier #include <err.h> 507720a19dSPhilippe Charnier #include <stdio.h> 517720a19dSPhilippe Charnier #include <stdlib.h> 527720a19dSPhilippe Charnier #include <signal.h> 539b50d902SRodney W. Grimes #include <string.h> 547720a19dSPhilippe Charnier #include <unistd.h> 55c451c0e6SMark Murray 569b50d902SRodney W. Grimes #include "pathnames.h" 579b50d902SRodney W. Grimes 589b50d902SRodney W. Grimes /* 599b50d902SRodney W. Grimes * xstr - extract and hash strings in a C program 609b50d902SRodney W. Grimes * 619b50d902SRodney W. Grimes * Bill Joy UCB 629b50d902SRodney W. Grimes * November, 1978 639b50d902SRodney W. Grimes */ 649b50d902SRodney W. Grimes 659b50d902SRodney W. Grimes #define ignore(a) ((void) a) 669b50d902SRodney W. Grimes 67641835f4SEd Schouten static off_t tellpt; 689b50d902SRodney W. Grimes 69641835f4SEd Schouten static off_t mesgpt; 70641835f4SEd Schouten static char cstrings[] = "strings"; 71641835f4SEd Schouten static char *strings = cstrings; 729b50d902SRodney W. Grimes 73641835f4SEd Schouten static int cflg; 74641835f4SEd Schouten static int vflg; 75641835f4SEd Schouten static int readstd; 769b50d902SRodney W. Grimes 77641835f4SEd Schouten static char lastchr(char *); 78c451c0e6SMark Murray 79641835f4SEd Schouten static int fgetNUL(char *, int, FILE *); 80641835f4SEd Schouten static int istail(char *, char *); 81641835f4SEd Schouten static int octdigit(char); 82641835f4SEd Schouten static int xgetc(FILE *); 83c451c0e6SMark Murray 84641835f4SEd Schouten static off_t hashit(char *, int); 85641835f4SEd Schouten static off_t yankstr(char **); 86c451c0e6SMark Murray 87*cccdaf50SAlfonso Gregory static void usage(void) __dead2; 88c451c0e6SMark Murray 89641835f4SEd Schouten static void flushsh(void); 90641835f4SEd Schouten static void found(int, off_t, char *); 91641835f4SEd Schouten static void inithash(void); 92641835f4SEd Schouten static void onintr(int); 93641835f4SEd Schouten static void process(const char *); 94641835f4SEd Schouten static void prstr(char *); 95641835f4SEd Schouten static void xsdotc(void); 967720a19dSPhilippe Charnier 977720a19dSPhilippe Charnier int 98f4ac32deSDavid Malone main(int argc, char *argv[]) 999b50d902SRodney W. Grimes { 1007720a19dSPhilippe Charnier int c; 101dd0208b3SKevin Lo int fdesc; 1029b50d902SRodney W. Grimes 1037720a19dSPhilippe Charnier while ((c = getopt(argc, argv, "-cv")) != -1) 1047720a19dSPhilippe Charnier switch (c) { 1057720a19dSPhilippe Charnier case '-': 1069b50d902SRodney W. Grimes readstd++; 1077720a19dSPhilippe Charnier break; 1089b50d902SRodney W. Grimes case 'c': 1099b50d902SRodney W. Grimes cflg++; 1107720a19dSPhilippe Charnier break; 1119b50d902SRodney W. Grimes case 'v': 1129b50d902SRodney W. Grimes vflg++; 1137720a19dSPhilippe Charnier break; 1149b50d902SRodney W. Grimes default: 1157720a19dSPhilippe Charnier usage(); 1169b50d902SRodney W. Grimes } 1177720a19dSPhilippe Charnier argc -= optind; 1187720a19dSPhilippe Charnier argv += optind; 1197720a19dSPhilippe Charnier 1209b50d902SRodney W. Grimes if (signal(SIGINT, SIG_IGN) == SIG_DFL) 1219b50d902SRodney W. Grimes signal(SIGINT, onintr); 1227720a19dSPhilippe Charnier if (cflg || (argc == 0 && !readstd)) 1239b50d902SRodney W. Grimes inithash(); 124dd0208b3SKevin Lo else { 125dd0208b3SKevin Lo strings = strdup(_PATH_TMP); 126dd0208b3SKevin Lo if (strings == NULL) 127dd0208b3SKevin Lo err(1, "strdup() failed"); 128dd0208b3SKevin Lo fdesc = mkstemp(strings); 129dd0208b3SKevin Lo if (fdesc == -1) 130dd0208b3SKevin Lo err(1, "Unable to create temporary file"); 131dd0208b3SKevin Lo close(fdesc); 132dd0208b3SKevin Lo } 133dd0208b3SKevin Lo 1349b50d902SRodney W. Grimes while (readstd || argc > 0) { 1359b50d902SRodney W. Grimes if (freopen("x.c", "w", stdout) == NULL) 1367720a19dSPhilippe Charnier err(1, "x.c"); 1379b50d902SRodney W. Grimes if (!readstd && freopen(argv[0], "r", stdin) == NULL) 1387720a19dSPhilippe Charnier err(2, "%s", argv[0]); 1399b50d902SRodney W. Grimes process("x.c"); 1409b50d902SRodney W. Grimes if (readstd == 0) 1419b50d902SRodney W. Grimes argc--, argv++; 1429b50d902SRodney W. Grimes else 1439b50d902SRodney W. Grimes readstd = 0; 14480c7cc1cSPedro F. Giffuni } 1459b50d902SRodney W. Grimes flushsh(); 1469b50d902SRodney W. Grimes if (cflg == 0) 1479b50d902SRodney W. Grimes xsdotc(); 1489b50d902SRodney W. Grimes if (strings[0] == '/') 1499b50d902SRodney W. Grimes ignore(unlink(strings)); 1509b50d902SRodney W. Grimes exit(0); 1519b50d902SRodney W. Grimes } 1529b50d902SRodney W. Grimes 1537720a19dSPhilippe Charnier static void 154f4ac32deSDavid Malone usage(void) 1557720a19dSPhilippe Charnier { 1569e752adbSRuslan Ermilov fprintf(stderr, "usage: xstr [-cv] [-] [file ...]\n"); 1577720a19dSPhilippe Charnier exit (1); 1587720a19dSPhilippe Charnier } 1597720a19dSPhilippe Charnier 160641835f4SEd Schouten static char linebuf[BUFSIZ]; 1619b50d902SRodney W. Grimes 162641835f4SEd Schouten static void 163f4ac32deSDavid Malone process(const char *name) 1649b50d902SRodney W. Grimes { 1659b50d902SRodney W. Grimes char *cp; 166c451c0e6SMark Murray int c; 167c451c0e6SMark Murray int incomm = 0; 1689b50d902SRodney W. Grimes int ret; 1699b50d902SRodney W. Grimes 1709b50d902SRodney W. Grimes printf("extern char\txstr[];\n"); 1719b50d902SRodney W. Grimes for (;;) { 1729b50d902SRodney W. Grimes if (fgets(linebuf, sizeof linebuf, stdin) == NULL) { 1737720a19dSPhilippe Charnier if (ferror(stdin)) 1747720a19dSPhilippe Charnier err(3, "%s", name); 1759b50d902SRodney W. Grimes break; 1769b50d902SRodney W. Grimes } 1779b50d902SRodney W. Grimes if (linebuf[0] == '#') { 1789b50d902SRodney W. Grimes if (linebuf[1] == ' ' && isdigit(linebuf[2])) 1799b50d902SRodney W. Grimes printf("#line%s", &linebuf[1]); 1809b50d902SRodney W. Grimes else 1819b50d902SRodney W. Grimes printf("%s", linebuf); 1829b50d902SRodney W. Grimes continue; 1839b50d902SRodney W. Grimes } 1847720a19dSPhilippe Charnier for (cp = linebuf; (c = *cp++);) switch (c) { 1859b50d902SRodney W. Grimes 1869b50d902SRodney W. Grimes case '"': 1879b50d902SRodney W. Grimes if (incomm) 1889b50d902SRodney W. Grimes goto def; 1899b50d902SRodney W. Grimes if ((ret = (int) yankstr(&cp)) == -1) 1909b50d902SRodney W. Grimes goto out; 1919b50d902SRodney W. Grimes printf("(&xstr[%d])", ret); 1929b50d902SRodney W. Grimes break; 1939b50d902SRodney W. Grimes 1949b50d902SRodney W. Grimes case '\'': 1959b50d902SRodney W. Grimes if (incomm) 1969b50d902SRodney W. Grimes goto def; 1979b50d902SRodney W. Grimes putchar(c); 1989b50d902SRodney W. Grimes if (*cp) 1999b50d902SRodney W. Grimes putchar(*cp++); 2009b50d902SRodney W. Grimes break; 2019b50d902SRodney W. Grimes 2029b50d902SRodney W. Grimes case '/': 2039b50d902SRodney W. Grimes if (incomm || *cp != '*') 2049b50d902SRodney W. Grimes goto def; 2059b50d902SRodney W. Grimes incomm = 1; 2069b50d902SRodney W. Grimes cp++; 2079b50d902SRodney W. Grimes printf("/*"); 2089b50d902SRodney W. Grimes continue; 2099b50d902SRodney W. Grimes 2109b50d902SRodney W. Grimes case '*': 2119b50d902SRodney W. Grimes if (incomm && *cp == '/') { 2129b50d902SRodney W. Grimes incomm = 0; 2139b50d902SRodney W. Grimes cp++; 2149b50d902SRodney W. Grimes printf("*/"); 2159b50d902SRodney W. Grimes continue; 2169b50d902SRodney W. Grimes } 2179b50d902SRodney W. Grimes goto def; 2189b50d902SRodney W. Grimes 2199b50d902SRodney W. Grimes def: 2209b50d902SRodney W. Grimes default: 2219b50d902SRodney W. Grimes putchar(c); 2229b50d902SRodney W. Grimes break; 2239b50d902SRodney W. Grimes } 2249b50d902SRodney W. Grimes } 2259b50d902SRodney W. Grimes out: 2269b50d902SRodney W. Grimes if (ferror(stdout)) 227c451c0e6SMark Murray warn("x.c"), onintr(0); 2289b50d902SRodney W. Grimes } 2299b50d902SRodney W. Grimes 230641835f4SEd Schouten static off_t 231f4ac32deSDavid Malone yankstr(char **cpp) 2329b50d902SRodney W. Grimes { 233c451c0e6SMark Murray char *cp = *cpp; 234c451c0e6SMark Murray int c, ch; 2359b50d902SRodney W. Grimes char dbuf[BUFSIZ]; 236c451c0e6SMark Murray char *dp = dbuf; 237c451c0e6SMark Murray char *tp; 238c451c0e6SMark Murray static char tmp[] = "b\bt\tr\rn\nf\f\\\\\"\""; 2399b50d902SRodney W. Grimes 2407720a19dSPhilippe Charnier while ((c = *cp++)) { 241cb4cbf9cSTim J. Robbins if (dp == dbuf + sizeof(dbuf) - 3) 242cb4cbf9cSTim J. Robbins errx(1, "message too long"); 2439b50d902SRodney W. Grimes switch (c) { 2449b50d902SRodney W. Grimes 2459b50d902SRodney W. Grimes case '"': 2469b50d902SRodney W. Grimes cp++; 2479b50d902SRodney W. Grimes goto out; 2489b50d902SRodney W. Grimes 2499b50d902SRodney W. Grimes case '\\': 2509b50d902SRodney W. Grimes c = *cp++; 2519b50d902SRodney W. Grimes if (c == 0) 2529b50d902SRodney W. Grimes break; 2539b50d902SRodney W. Grimes if (c == '\n') { 2549b50d902SRodney W. Grimes if (fgets(linebuf, sizeof linebuf, stdin) 2559b50d902SRodney W. Grimes == NULL) { 2567720a19dSPhilippe Charnier if (ferror(stdin)) 2577720a19dSPhilippe Charnier err(3, "x.c"); 2589b50d902SRodney W. Grimes return(-1); 2599b50d902SRodney W. Grimes } 2609b50d902SRodney W. Grimes cp = linebuf; 2619b50d902SRodney W. Grimes continue; 2629b50d902SRodney W. Grimes } 263c451c0e6SMark Murray for (tp = tmp; (ch = *tp++); tp++) 2649b50d902SRodney W. Grimes if (c == ch) { 2659b50d902SRodney W. Grimes c = *tp; 2669b50d902SRodney W. Grimes goto gotc; 2679b50d902SRodney W. Grimes } 2689b50d902SRodney W. Grimes if (!octdigit(c)) { 2699b50d902SRodney W. Grimes *dp++ = '\\'; 2709b50d902SRodney W. Grimes break; 2719b50d902SRodney W. Grimes } 2729b50d902SRodney W. Grimes c -= '0'; 2739b50d902SRodney W. Grimes if (!octdigit(*cp)) 2749b50d902SRodney W. Grimes break; 2759b50d902SRodney W. Grimes c <<= 3, c += *cp++ - '0'; 2769b50d902SRodney W. Grimes if (!octdigit(*cp)) 2779b50d902SRodney W. Grimes break; 2789b50d902SRodney W. Grimes c <<= 3, c += *cp++ - '0'; 2799b50d902SRodney W. Grimes break; 2809b50d902SRodney W. Grimes } 2819b50d902SRodney W. Grimes gotc: 2829b50d902SRodney W. Grimes *dp++ = c; 2839b50d902SRodney W. Grimes } 2849b50d902SRodney W. Grimes out: 2859b50d902SRodney W. Grimes *cpp = --cp; 2869b50d902SRodney W. Grimes *dp = 0; 2879b50d902SRodney W. Grimes return (hashit(dbuf, 1)); 2889b50d902SRodney W. Grimes } 2899b50d902SRodney W. Grimes 290641835f4SEd Schouten static int 291f4ac32deSDavid Malone octdigit(char c) 2929b50d902SRodney W. Grimes { 2939b50d902SRodney W. Grimes return (isdigit(c) && c != '8' && c != '9'); 2949b50d902SRodney W. Grimes } 2959b50d902SRodney W. Grimes 296641835f4SEd Schouten static void 297f4ac32deSDavid Malone inithash(void) 2989b50d902SRodney W. Grimes { 2999b50d902SRodney W. Grimes char buf[BUFSIZ]; 300c451c0e6SMark Murray FILE *mesgread = fopen(strings, "r"); 3019b50d902SRodney W. Grimes 3029b50d902SRodney W. Grimes if (mesgread == NULL) 3039b50d902SRodney W. Grimes return; 3049b50d902SRodney W. Grimes for (;;) { 3059b50d902SRodney W. Grimes mesgpt = tellpt; 3067720a19dSPhilippe Charnier if (fgetNUL(buf, sizeof buf, mesgread) == 0) 3079b50d902SRodney W. Grimes break; 3089b50d902SRodney W. Grimes ignore(hashit(buf, 0)); 3099b50d902SRodney W. Grimes } 3109b50d902SRodney W. Grimes ignore(fclose(mesgread)); 3119b50d902SRodney W. Grimes } 3129b50d902SRodney W. Grimes 313641835f4SEd Schouten static int 314f4ac32deSDavid Malone fgetNUL(char *obuf, int rmdr, FILE *file) 3159b50d902SRodney W. Grimes { 316c451c0e6SMark Murray int c; 317c451c0e6SMark Murray char *buf = obuf; 3189b50d902SRodney W. Grimes 3199b50d902SRodney W. Grimes while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF) 3209b50d902SRodney W. Grimes *buf++ = c; 3219b50d902SRodney W. Grimes *buf++ = 0; 3227720a19dSPhilippe Charnier return ((feof(file) || ferror(file)) ? 0 : 1); 3239b50d902SRodney W. Grimes } 3249b50d902SRodney W. Grimes 325641835f4SEd Schouten static int 326f4ac32deSDavid Malone xgetc(FILE *file) 3279b50d902SRodney W. Grimes { 3289b50d902SRodney W. Grimes 3299b50d902SRodney W. Grimes tellpt++; 3309b50d902SRodney W. Grimes return (getc(file)); 3319b50d902SRodney W. Grimes } 3329b50d902SRodney W. Grimes 3339b50d902SRodney W. Grimes #define BUCKETS 128 3349b50d902SRodney W. Grimes 335641835f4SEd Schouten static struct hash { 3369b50d902SRodney W. Grimes off_t hpt; 3379b50d902SRodney W. Grimes char *hstr; 3389b50d902SRodney W. Grimes struct hash *hnext; 3399b50d902SRodney W. Grimes short hnew; 3409b50d902SRodney W. Grimes } bucket[BUCKETS]; 3419b50d902SRodney W. Grimes 342641835f4SEd Schouten static off_t 343f4ac32deSDavid Malone hashit(char *str, int new) 3449b50d902SRodney W. Grimes { 3459b50d902SRodney W. Grimes int i; 346c451c0e6SMark Murray struct hash *hp, *hp0; 3479b50d902SRodney W. Grimes 3489b50d902SRodney W. Grimes hp = hp0 = &bucket[lastchr(str) & 0177]; 3499b50d902SRodney W. Grimes while (hp->hnext) { 3509b50d902SRodney W. Grimes hp = hp->hnext; 3519b50d902SRodney W. Grimes i = istail(str, hp->hstr); 3529b50d902SRodney W. Grimes if (i >= 0) 3539b50d902SRodney W. Grimes return (hp->hpt + i); 3549b50d902SRodney W. Grimes } 3557720a19dSPhilippe Charnier if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL) 3567720a19dSPhilippe Charnier errx(8, "calloc"); 3579b50d902SRodney W. Grimes hp->hpt = mesgpt; 3587720a19dSPhilippe Charnier if (!(hp->hstr = strdup(str))) 3597720a19dSPhilippe Charnier err(1, NULL); 3609b50d902SRodney W. Grimes mesgpt += strlen(hp->hstr) + 1; 3619b50d902SRodney W. Grimes hp->hnext = hp0->hnext; 3629b50d902SRodney W. Grimes hp->hnew = new; 3639b50d902SRodney W. Grimes hp0->hnext = hp; 3649b50d902SRodney W. Grimes return (hp->hpt); 3659b50d902SRodney W. Grimes } 3669b50d902SRodney W. Grimes 367641835f4SEd Schouten static void 368f4ac32deSDavid Malone flushsh(void) 3699b50d902SRodney W. Grimes { 370c451c0e6SMark Murray int i; 371c451c0e6SMark Murray struct hash *hp; 372c451c0e6SMark Murray FILE *mesgwrit; 373c451c0e6SMark Murray int old = 0, new = 0; 3749b50d902SRodney W. Grimes 3759b50d902SRodney W. Grimes for (i = 0; i < BUCKETS; i++) 3769b50d902SRodney W. Grimes for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) 3779b50d902SRodney W. Grimes if (hp->hnew) 3789b50d902SRodney W. Grimes new++; 3799b50d902SRodney W. Grimes else 3809b50d902SRodney W. Grimes old++; 3819b50d902SRodney W. Grimes if (new == 0 && old != 0) 3829b50d902SRodney W. Grimes return; 3839b50d902SRodney W. Grimes mesgwrit = fopen(strings, old ? "r+" : "w"); 3849b50d902SRodney W. Grimes if (mesgwrit == NULL) 385d88ccf5dSTim J. Robbins err(4, "%s", strings); 3869b50d902SRodney W. Grimes for (i = 0; i < BUCKETS; i++) 3879b50d902SRodney W. Grimes for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) { 3889b50d902SRodney W. Grimes found(hp->hnew, hp->hpt, hp->hstr); 3899b50d902SRodney W. Grimes if (hp->hnew) { 3909b50d902SRodney W. Grimes fseek(mesgwrit, hp->hpt, 0); 3919b50d902SRodney W. Grimes ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, mesgwrit)); 3929b50d902SRodney W. Grimes if (ferror(mesgwrit)) 3937720a19dSPhilippe Charnier err(4, "%s", strings); 3949b50d902SRodney W. Grimes } 3959b50d902SRodney W. Grimes } 3969b50d902SRodney W. Grimes if (fclose(mesgwrit) == EOF) 3977720a19dSPhilippe Charnier err(4, "%s", strings); 3989b50d902SRodney W. Grimes } 3999b50d902SRodney W. Grimes 400641835f4SEd Schouten static void 401f4ac32deSDavid Malone found(int new, off_t off, char *str) 4029b50d902SRodney W. Grimes { 4039b50d902SRodney W. Grimes if (vflg == 0) 4049b50d902SRodney W. Grimes return; 4059b50d902SRodney W. Grimes if (!new) 4069b50d902SRodney W. Grimes fprintf(stderr, "found at %d:", (int) off); 4079b50d902SRodney W. Grimes else 4089b50d902SRodney W. Grimes fprintf(stderr, "new at %d:", (int) off); 4099b50d902SRodney W. Grimes prstr(str); 4109b50d902SRodney W. Grimes fprintf(stderr, "\n"); 4119b50d902SRodney W. Grimes } 4129b50d902SRodney W. Grimes 413641835f4SEd Schouten static void 414f4ac32deSDavid Malone prstr(char *cp) 4159b50d902SRodney W. Grimes { 416c451c0e6SMark Murray int c; 4179b50d902SRodney W. Grimes 4187720a19dSPhilippe Charnier while ((c = (*cp++ & 0377))) 4199b50d902SRodney W. Grimes if (c < ' ') 4209b50d902SRodney W. Grimes fprintf(stderr, "^%c", c + '`'); 4219b50d902SRodney W. Grimes else if (c == 0177) 4229b50d902SRodney W. Grimes fprintf(stderr, "^?"); 4239b50d902SRodney W. Grimes else if (c > 0200) 4249b50d902SRodney W. Grimes fprintf(stderr, "\\%03o", c); 4259b50d902SRodney W. Grimes else 4269b50d902SRodney W. Grimes fprintf(stderr, "%c", c); 4279b50d902SRodney W. Grimes } 4289b50d902SRodney W. Grimes 429641835f4SEd Schouten static void 430f4ac32deSDavid Malone xsdotc(void) 4319b50d902SRodney W. Grimes { 432c451c0e6SMark Murray FILE *strf = fopen(strings, "r"); 433c451c0e6SMark Murray FILE *xdotcf; 4349b50d902SRodney W. Grimes 4359b50d902SRodney W. Grimes if (strf == NULL) 4367720a19dSPhilippe Charnier err(5, "%s", strings); 4379b50d902SRodney W. Grimes xdotcf = fopen("xs.c", "w"); 4389b50d902SRodney W. Grimes if (xdotcf == NULL) 4397720a19dSPhilippe Charnier err(6, "xs.c"); 4409b50d902SRodney W. Grimes fprintf(xdotcf, "char\txstr[] = {\n"); 4419b50d902SRodney W. Grimes for (;;) { 442c451c0e6SMark Murray int i, c; 4439b50d902SRodney W. Grimes 4449b50d902SRodney W. Grimes for (i = 0; i < 8; i++) { 4459b50d902SRodney W. Grimes c = getc(strf); 4469b50d902SRodney W. Grimes if (ferror(strf)) { 4477720a19dSPhilippe Charnier warn("%s", strings); 448c451c0e6SMark Murray onintr(0); 4499b50d902SRodney W. Grimes } 4509b50d902SRodney W. Grimes if (feof(strf)) { 4519b50d902SRodney W. Grimes fprintf(xdotcf, "\n"); 4529b50d902SRodney W. Grimes goto out; 4539b50d902SRodney W. Grimes } 4549b50d902SRodney W. Grimes fprintf(xdotcf, "0x%02x,", c); 4559b50d902SRodney W. Grimes } 4569b50d902SRodney W. Grimes fprintf(xdotcf, "\n"); 4579b50d902SRodney W. Grimes } 4589b50d902SRodney W. Grimes out: 4599b50d902SRodney W. Grimes fprintf(xdotcf, "};\n"); 4609b50d902SRodney W. Grimes ignore(fclose(xdotcf)); 4619b50d902SRodney W. Grimes ignore(fclose(strf)); 4629b50d902SRodney W. Grimes } 4639b50d902SRodney W. Grimes 464641835f4SEd Schouten static char 465f4ac32deSDavid Malone lastchr(char *cp) 4669b50d902SRodney W. Grimes { 4679b50d902SRodney W. Grimes 4689b50d902SRodney W. Grimes while (cp[0] && cp[1]) 4699b50d902SRodney W. Grimes cp++; 4709b50d902SRodney W. Grimes return (*cp); 4719b50d902SRodney W. Grimes } 4729b50d902SRodney W. Grimes 473641835f4SEd Schouten static int 474f4ac32deSDavid Malone istail(char *str, char *of) 4759b50d902SRodney W. Grimes { 476c451c0e6SMark Murray int d = strlen(of) - strlen(str); 4779b50d902SRodney W. Grimes 4789b50d902SRodney W. Grimes if (d < 0 || strcmp(&of[d], str) != 0) 4799b50d902SRodney W. Grimes return (-1); 4809b50d902SRodney W. Grimes return (d); 4819b50d902SRodney W. Grimes } 4829b50d902SRodney W. Grimes 483641835f4SEd Schouten static void 484f4ac32deSDavid Malone onintr(int dummy __unused) 4859b50d902SRodney W. Grimes { 4869b50d902SRodney W. Grimes 4879b50d902SRodney W. Grimes ignore(signal(SIGINT, SIG_IGN)); 4889b50d902SRodney W. Grimes if (strings[0] == '/') 4899b50d902SRodney W. Grimes ignore(unlink(strings)); 4909b50d902SRodney W. Grimes ignore(unlink("x.c")); 4919b50d902SRodney W. Grimes ignore(unlink("xs.c")); 4929b50d902SRodney W. Grimes exit(7); 4939b50d902SRodney W. Grimes } 494