19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1980, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 352487a449SPhilippe Charnier static const char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1993\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #ifndef lint 412487a449SPhilippe Charnier #if 0 429b50d902SRodney W. Grimes static char sccsid[] = "@(#)mkstr.c 8.1 (Berkeley) 6/6/93"; 432487a449SPhilippe Charnier #endif 442487a449SPhilippe Charnier static const char rcsid[] = 4532744e40SJohn Birrell "$Id: mkstr.c,v 1.2 1997/07/24 07:05:02 charnier Exp $"; 469b50d902SRodney W. Grimes #endif /* not lint */ 479b50d902SRodney W. Grimes 482487a449SPhilippe Charnier #include <err.h> 499b50d902SRodney W. Grimes #include <stdio.h> 502487a449SPhilippe Charnier #include <stdlib.h> 5132744e40SJohn Birrell #include <string.h> 529b50d902SRodney W. Grimes 539b50d902SRodney W. Grimes #define ungetchar(c) ungetc(c, stdin) 549b50d902SRodney W. Grimes 559b50d902SRodney W. Grimes /* 569b50d902SRodney W. Grimes * mkstr - create a string error message file by massaging C source 579b50d902SRodney W. Grimes * 589b50d902SRodney W. Grimes * Bill Joy UCB August 1977 599b50d902SRodney W. Grimes * 609b50d902SRodney W. Grimes * Modified March 1978 to hash old messages to be able to recompile 619b50d902SRodney W. Grimes * without addding messages to the message file (usually) 629b50d902SRodney W. Grimes * 639b50d902SRodney W. Grimes * Based on an earlier program conceived by Bill Joy and Chuck Haley 649b50d902SRodney W. Grimes * 659b50d902SRodney W. Grimes * Program to create a string error message file 669b50d902SRodney W. Grimes * from a group of C programs. Arguments are the name 679b50d902SRodney W. Grimes * of the file where the strings are to be placed, the 689b50d902SRodney W. Grimes * prefix of the new files where the processed source text 699b50d902SRodney W. Grimes * is to be placed, and the files to be processed. 709b50d902SRodney W. Grimes * 719b50d902SRodney W. Grimes * The program looks for 'error("' in the source stream. 729b50d902SRodney W. Grimes * Whenever it finds this, the following characters from the '"' 739b50d902SRodney W. Grimes * to a '"' are replaced by 'seekpt' where seekpt is a 749b50d902SRodney W. Grimes * pointer into the error message file. 759b50d902SRodney W. Grimes * If the '(' is not immediately followed by a '"' no change occurs. 769b50d902SRodney W. Grimes * 779b50d902SRodney W. Grimes * The optional '-' causes strings to be added at the end of the 789b50d902SRodney W. Grimes * existing error message file for recompilation of single routines. 799b50d902SRodney W. Grimes */ 809b50d902SRodney W. Grimes 819b50d902SRodney W. Grimes FILE *mesgread, *mesgwrite; 829b50d902SRodney W. Grimes char name[100], *np; 839b50d902SRodney W. Grimes 842487a449SPhilippe Charnier void copystr __P((void)); 852487a449SPhilippe Charnier int fgetNUL __P((char *, int, FILE *)); 862487a449SPhilippe Charnier unsigned hashit __P((char *, char, unsigned)); 872487a449SPhilippe Charnier void inithash __P((void)); 882487a449SPhilippe Charnier int match __P((char *)); 892487a449SPhilippe Charnier int octdigit __P((char)); 902487a449SPhilippe Charnier void process __P((void)); 912487a449SPhilippe Charnier static void usage __P((void)); 922487a449SPhilippe Charnier 932487a449SPhilippe Charnier int 949b50d902SRodney W. Grimes main(argc, argv) 959b50d902SRodney W. Grimes int argc; 969b50d902SRodney W. Grimes char *argv[]; 979b50d902SRodney W. Grimes { 989b50d902SRodney W. Grimes char addon = 0; 999b50d902SRodney W. Grimes 1002487a449SPhilippe Charnier argc--, argv++; 1019b50d902SRodney W. Grimes if (argc > 1 && argv[0][0] == '-') 1029b50d902SRodney W. Grimes addon++, argc--, argv++; 1039b50d902SRodney W. Grimes if (argc < 3) 1042487a449SPhilippe Charnier usage(); 1059b50d902SRodney W. Grimes mesgwrite = fopen(argv[0], addon ? "a" : "w"); 1069b50d902SRodney W. Grimes if (mesgwrite == NULL) 1072487a449SPhilippe Charnier err(1, "%s", argv[0]); 1089b50d902SRodney W. Grimes mesgread = fopen(argv[0], "r"); 1099b50d902SRodney W. Grimes if (mesgread == NULL) 1102487a449SPhilippe Charnier err(1, "%s", argv[0]); 1119b50d902SRodney W. Grimes inithash(); 1129b50d902SRodney W. Grimes argc--, argv++; 1139b50d902SRodney W. Grimes strcpy(name, argv[0]); 1149b50d902SRodney W. Grimes np = name + strlen(name); 1159b50d902SRodney W. Grimes argc--, argv++; 1169b50d902SRodney W. Grimes do { 1179b50d902SRodney W. Grimes strcpy(np, argv[0]); 1189b50d902SRodney W. Grimes if (freopen(name, "w", stdout) == NULL) 1192487a449SPhilippe Charnier err(1, "%s", name); 1209b50d902SRodney W. Grimes if (freopen(argv[0], "r", stdin) == NULL) 1212487a449SPhilippe Charnier err(1, "%s", argv[0]); 1229b50d902SRodney W. Grimes process(); 1239b50d902SRodney W. Grimes argc--, argv++; 1249b50d902SRodney W. Grimes } while (argc > 0); 1259b50d902SRodney W. Grimes exit(0); 1269b50d902SRodney W. Grimes } 1279b50d902SRodney W. Grimes 1282487a449SPhilippe Charnier static void 1292487a449SPhilippe Charnier usage() 1302487a449SPhilippe Charnier { 1312487a449SPhilippe Charnier fprintf(stderr, "usage: mkstr [ - ] mesgfile prefix file ...\n"); 1322487a449SPhilippe Charnier exit(1); 1332487a449SPhilippe Charnier } 1342487a449SPhilippe Charnier 1352487a449SPhilippe Charnier void 1369b50d902SRodney W. Grimes process() 1379b50d902SRodney W. Grimes { 1389b50d902SRodney W. Grimes register c; 1399b50d902SRodney W. Grimes 1409b50d902SRodney W. Grimes for (;;) { 1419b50d902SRodney W. Grimes c = getchar(); 1429b50d902SRodney W. Grimes if (c == EOF) 1439b50d902SRodney W. Grimes return; 1449b50d902SRodney W. Grimes if (c != 'e') { 1459b50d902SRodney W. Grimes putchar(c); 1469b50d902SRodney W. Grimes continue; 1479b50d902SRodney W. Grimes } 1489b50d902SRodney W. Grimes if (match("error(")) { 1499b50d902SRodney W. Grimes printf("error("); 1509b50d902SRodney W. Grimes c = getchar(); 1519b50d902SRodney W. Grimes if (c != '"') 1529b50d902SRodney W. Grimes putchar(c); 1539b50d902SRodney W. Grimes else 1549b50d902SRodney W. Grimes copystr(); 1559b50d902SRodney W. Grimes } 1569b50d902SRodney W. Grimes } 1579b50d902SRodney W. Grimes } 1589b50d902SRodney W. Grimes 1592487a449SPhilippe Charnier int 1609b50d902SRodney W. Grimes match(ocp) 1619b50d902SRodney W. Grimes char *ocp; 1629b50d902SRodney W. Grimes { 1639b50d902SRodney W. Grimes register char *cp; 1649b50d902SRodney W. Grimes register c; 1659b50d902SRodney W. Grimes 1669b50d902SRodney W. Grimes for (cp = ocp + 1; *cp; cp++) { 1679b50d902SRodney W. Grimes c = getchar(); 1689b50d902SRodney W. Grimes if (c != *cp) { 1699b50d902SRodney W. Grimes while (ocp < cp) 1709b50d902SRodney W. Grimes putchar(*ocp++); 1719b50d902SRodney W. Grimes ungetchar(c); 1729b50d902SRodney W. Grimes return (0); 1739b50d902SRodney W. Grimes } 1749b50d902SRodney W. Grimes } 1759b50d902SRodney W. Grimes return (1); 1769b50d902SRodney W. Grimes } 1779b50d902SRodney W. Grimes 1782487a449SPhilippe Charnier void 1799b50d902SRodney W. Grimes copystr() 1809b50d902SRodney W. Grimes { 1819b50d902SRodney W. Grimes register c, ch; 1829b50d902SRodney W. Grimes char buf[512]; 1839b50d902SRodney W. Grimes register char *cp = buf; 1849b50d902SRodney W. Grimes 1859b50d902SRodney W. Grimes for (;;) { 1869b50d902SRodney W. Grimes c = getchar(); 1879b50d902SRodney W. Grimes if (c == EOF) 1889b50d902SRodney W. Grimes break; 1899b50d902SRodney W. Grimes switch (c) { 1909b50d902SRodney W. Grimes 1919b50d902SRodney W. Grimes case '"': 1929b50d902SRodney W. Grimes *cp++ = 0; 1939b50d902SRodney W. Grimes goto out; 1949b50d902SRodney W. Grimes case '\\': 1959b50d902SRodney W. Grimes c = getchar(); 1969b50d902SRodney W. Grimes switch (c) { 1979b50d902SRodney W. Grimes 1989b50d902SRodney W. Grimes case 'b': 1999b50d902SRodney W. Grimes c = '\b'; 2009b50d902SRodney W. Grimes break; 2019b50d902SRodney W. Grimes case 't': 2029b50d902SRodney W. Grimes c = '\t'; 2039b50d902SRodney W. Grimes break; 2049b50d902SRodney W. Grimes case 'r': 2059b50d902SRodney W. Grimes c = '\r'; 2069b50d902SRodney W. Grimes break; 2079b50d902SRodney W. Grimes case 'n': 2089b50d902SRodney W. Grimes c = '\n'; 2099b50d902SRodney W. Grimes break; 2109b50d902SRodney W. Grimes case '\n': 2119b50d902SRodney W. Grimes continue; 2129b50d902SRodney W. Grimes case 'f': 2139b50d902SRodney W. Grimes c = '\f'; 2149b50d902SRodney W. Grimes break; 2159b50d902SRodney W. Grimes case '0': 2169b50d902SRodney W. Grimes c = 0; 2179b50d902SRodney W. Grimes break; 2189b50d902SRodney W. Grimes case '\\': 2199b50d902SRodney W. Grimes break; 2209b50d902SRodney W. Grimes default: 2219b50d902SRodney W. Grimes if (!octdigit(c)) 2229b50d902SRodney W. Grimes break; 2239b50d902SRodney W. Grimes c -= '0'; 2249b50d902SRodney W. Grimes ch = getchar(); 2259b50d902SRodney W. Grimes if (!octdigit(ch)) 2269b50d902SRodney W. Grimes break; 2279b50d902SRodney W. Grimes c <<= 7, c += ch - '0'; 2289b50d902SRodney W. Grimes ch = getchar(); 2299b50d902SRodney W. Grimes if (!octdigit(ch)) 2309b50d902SRodney W. Grimes break; 2319b50d902SRodney W. Grimes c <<= 3, c+= ch - '0', ch = -1; 2329b50d902SRodney W. Grimes break; 2339b50d902SRodney W. Grimes } 2349b50d902SRodney W. Grimes } 2359b50d902SRodney W. Grimes *cp++ = c; 2369b50d902SRodney W. Grimes } 2379b50d902SRodney W. Grimes out: 2389b50d902SRodney W. Grimes *cp = 0; 2399b50d902SRodney W. Grimes printf("%d", hashit(buf, 1, NULL)); 2409b50d902SRodney W. Grimes } 2419b50d902SRodney W. Grimes 2422487a449SPhilippe Charnier int 2439b50d902SRodney W. Grimes octdigit(c) 2449b50d902SRodney W. Grimes char c; 2459b50d902SRodney W. Grimes { 2469b50d902SRodney W. Grimes 2479b50d902SRodney W. Grimes return (c >= '0' && c <= '7'); 2489b50d902SRodney W. Grimes } 2499b50d902SRodney W. Grimes 2502487a449SPhilippe Charnier void 2519b50d902SRodney W. Grimes inithash() 2529b50d902SRodney W. Grimes { 2539b50d902SRodney W. Grimes char buf[512]; 2549b50d902SRodney W. Grimes int mesgpt = 0; 2559b50d902SRodney W. Grimes 2569b50d902SRodney W. Grimes rewind(mesgread); 2572487a449SPhilippe Charnier while (fgetNUL(buf, sizeof buf, mesgread) != 0) { 2589b50d902SRodney W. Grimes hashit(buf, 0, mesgpt); 2599b50d902SRodney W. Grimes mesgpt += strlen(buf) + 2; 2609b50d902SRodney W. Grimes } 2619b50d902SRodney W. Grimes } 2629b50d902SRodney W. Grimes 2639b50d902SRodney W. Grimes #define NBUCKETS 511 2649b50d902SRodney W. Grimes 2659b50d902SRodney W. Grimes struct hash { 2669b50d902SRodney W. Grimes long hval; 2679b50d902SRodney W. Grimes unsigned hpt; 2689b50d902SRodney W. Grimes struct hash *hnext; 2699b50d902SRodney W. Grimes } *bucket[NBUCKETS]; 2709b50d902SRodney W. Grimes 2712487a449SPhilippe Charnier unsigned 2729b50d902SRodney W. Grimes hashit(str, really, fakept) 2739b50d902SRodney W. Grimes char *str; 2749b50d902SRodney W. Grimes char really; 2759b50d902SRodney W. Grimes unsigned fakept; 2769b50d902SRodney W. Grimes { 2779b50d902SRodney W. Grimes int i; 2789b50d902SRodney W. Grimes register struct hash *hp; 2799b50d902SRodney W. Grimes char buf[512]; 2809b50d902SRodney W. Grimes long hashval = 0; 2819b50d902SRodney W. Grimes register char *cp; 2829b50d902SRodney W. Grimes 2839b50d902SRodney W. Grimes if (really) 2849b50d902SRodney W. Grimes fflush(mesgwrite); 2859b50d902SRodney W. Grimes for (cp = str; *cp;) 2869b50d902SRodney W. Grimes hashval = (hashval << 1) + *cp++; 2879b50d902SRodney W. Grimes i = hashval % NBUCKETS; 2889b50d902SRodney W. Grimes if (i < 0) 2899b50d902SRodney W. Grimes i += NBUCKETS; 2909b50d902SRodney W. Grimes if (really != 0) 2919b50d902SRodney W. Grimes for (hp = bucket[i]; hp != 0; hp = hp->hnext) 2929b50d902SRodney W. Grimes if (hp->hval == hashval) { 2939b50d902SRodney W. Grimes fseek(mesgread, (long) hp->hpt, 0); 2949b50d902SRodney W. Grimes fgetNUL(buf, sizeof buf, mesgread); 2959b50d902SRodney W. Grimes /* 2969b50d902SRodney W. Grimes fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf); 2979b50d902SRodney W. Grimes */ 2989b50d902SRodney W. Grimes if (strcmp(buf, str) == 0) 2999b50d902SRodney W. Grimes break; 3009b50d902SRodney W. Grimes } 3019b50d902SRodney W. Grimes if (!really || hp == 0) { 3029b50d902SRodney W. Grimes hp = (struct hash *) calloc(1, sizeof *hp); 3039b50d902SRodney W. Grimes hp->hnext = bucket[i]; 3049b50d902SRodney W. Grimes hp->hval = hashval; 3059b50d902SRodney W. Grimes hp->hpt = really ? ftell(mesgwrite) : fakept; 3069b50d902SRodney W. Grimes if (really) { 3079b50d902SRodney W. Grimes fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite); 3089b50d902SRodney W. Grimes fwrite("\n", sizeof (char), 1, mesgwrite); 3099b50d902SRodney W. Grimes } 3109b50d902SRodney W. Grimes bucket[i] = hp; 3119b50d902SRodney W. Grimes } 3129b50d902SRodney W. Grimes /* 3139b50d902SRodney W. Grimes fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt); 3149b50d902SRodney W. Grimes */ 3159b50d902SRodney W. Grimes return (hp->hpt); 3169b50d902SRodney W. Grimes } 3179b50d902SRodney W. Grimes 3189b50d902SRodney W. Grimes #include <sys/types.h> 3199b50d902SRodney W. Grimes #include <sys/stat.h> 3209b50d902SRodney W. Grimes 3212487a449SPhilippe Charnier int 3229b50d902SRodney W. Grimes fgetNUL(obuf, rmdr, file) 3239b50d902SRodney W. Grimes char *obuf; 3249b50d902SRodney W. Grimes register int rmdr; 3259b50d902SRodney W. Grimes FILE *file; 3269b50d902SRodney W. Grimes { 3279b50d902SRodney W. Grimes register c; 3289b50d902SRodney W. Grimes register char *buf = obuf; 3299b50d902SRodney W. Grimes 3309b50d902SRodney W. Grimes while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF) 3319b50d902SRodney W. Grimes *buf++ = c; 3329b50d902SRodney W. Grimes *buf++ = 0; 3339b50d902SRodney W. Grimes getc(file); 3342487a449SPhilippe Charnier return ((feof(file) || ferror(file)) ? 0 : 1); 3359b50d902SRodney W. Grimes } 336