xref: /freebsd/usr.bin/mkstr/mkstr.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro 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 
322487a449SPhilippe Charnier #include <err.h>
33c26c35e7STim J. Robbins #include <errno.h>
349b50d902SRodney W. Grimes #include <stdio.h>
352487a449SPhilippe Charnier #include <stdlib.h>
3632744e40SJohn Birrell #include <string.h>
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #define	ungetchar(c)	ungetc(c, stdin)
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes /*
419b50d902SRodney W. Grimes  * mkstr - create a string error message file by massaging C source
429b50d902SRodney W. Grimes  *
439b50d902SRodney W. Grimes  * Bill Joy UCB August 1977
449b50d902SRodney W. Grimes  *
459b50d902SRodney W. Grimes  * Modified March 1978 to hash old messages to be able to recompile
469b50d902SRodney W. Grimes  * without addding messages to the message file (usually)
479b50d902SRodney W. Grimes  *
489b50d902SRodney W. Grimes  * Based on an earlier program conceived by Bill Joy and Chuck Haley
499b50d902SRodney W. Grimes  *
509b50d902SRodney W. Grimes  * Program to create a string error message file
519b50d902SRodney W. Grimes  * from a group of C programs.  Arguments are the name
529b50d902SRodney W. Grimes  * of the file where the strings are to be placed, the
539b50d902SRodney W. Grimes  * prefix of the new files where the processed source text
549b50d902SRodney W. Grimes  * is to be placed, and the files to be processed.
559b50d902SRodney W. Grimes  *
569b50d902SRodney W. Grimes  * The program looks for 'error("' in the source stream.
579b50d902SRodney W. Grimes  * Whenever it finds this, the following characters from the '"'
589b50d902SRodney W. Grimes  * to a '"' are replaced by 'seekpt' where seekpt is a
599b50d902SRodney W. Grimes  * pointer into the error message file.
609b50d902SRodney W. Grimes  * If the '(' is not immediately followed by a '"' no change occurs.
619b50d902SRodney W. Grimes  *
629b50d902SRodney W. Grimes  * The optional '-' causes strings to be added at the end of the
639b50d902SRodney W. Grimes  * existing error message file for recompilation of single routines.
649b50d902SRodney W. Grimes  */
659b50d902SRodney W. Grimes 
661fbc22f5SBaptiste Daroussin static FILE	*mesgread, *mesgwrite;
671fbc22f5SBaptiste Daroussin static char	name[100], *np;
689b50d902SRodney W. Grimes 
69d3cb5dedSWarner Losh void copystr(void);
70d3cb5dedSWarner Losh int fgetNUL(char *, int, FILE *);
71ffc6a8e3SMark Murray unsigned hashit(char *, int, unsigned);
72d3cb5dedSWarner Losh void inithash(void);
73d3cb5dedSWarner Losh int match(const char *);
74d3cb5dedSWarner Losh int octdigit(char);
75d3cb5dedSWarner Losh void process(void);
76ffc6a8e3SMark Murray void usage(void);
772487a449SPhilippe Charnier 
782487a449SPhilippe Charnier int
main(int argc,char * argv[])79ffc6a8e3SMark Murray main(int argc, char *argv[])
809b50d902SRodney W. Grimes {
819b50d902SRodney W. Grimes 	char addon = 0;
82c26c35e7STim J. Robbins 	size_t namelen;
839b50d902SRodney W. Grimes 
842487a449SPhilippe Charnier 	argc--, argv++;
859b50d902SRodney W. Grimes 	if (argc > 1 && argv[0][0] == '-')
869b50d902SRodney W. Grimes 		addon++, argc--, argv++;
879b50d902SRodney W. Grimes 	if (argc < 3)
882487a449SPhilippe Charnier 		usage();
899b50d902SRodney W. Grimes 	mesgwrite = fopen(argv[0], addon ? "a" : "w");
909b50d902SRodney W. Grimes 	if (mesgwrite == NULL)
912487a449SPhilippe Charnier 		err(1, "%s", argv[0]);
929b50d902SRodney W. Grimes 	mesgread = fopen(argv[0], "r");
939b50d902SRodney W. Grimes 	if (mesgread == NULL)
942487a449SPhilippe Charnier 		err(1, "%s", argv[0]);
959b50d902SRodney W. Grimes 	inithash();
969b50d902SRodney W. Grimes 	argc--, argv++;
97c26c35e7STim J. Robbins 	namelen = strlcpy(name, argv[0], sizeof(name));
98c26c35e7STim J. Robbins 	if (namelen >= sizeof(name)) {
99c26c35e7STim J. Robbins 		errno = ENAMETOOLONG;
100c26c35e7STim J. Robbins 		err(1, "%s", argv[0]);
101c26c35e7STim J. Robbins 	}
102c26c35e7STim J. Robbins 	np = name + namelen;
1039b50d902SRodney W. Grimes 	argc--, argv++;
1049b50d902SRodney W. Grimes 	do {
105c26c35e7STim J. Robbins 		if (strlcpy(np, argv[0], sizeof(name) - namelen) >=
106c26c35e7STim J. Robbins 		    sizeof(name) - namelen) {
107c26c35e7STim J. Robbins 			errno = ENAMETOOLONG;
108c26c35e7STim J. Robbins 			err(1, "%s%s", name, argv[0]);
109c26c35e7STim J. Robbins 		}
1109b50d902SRodney W. Grimes 		if (freopen(name, "w", stdout) == NULL)
1112487a449SPhilippe Charnier 			err(1, "%s", name);
1129b50d902SRodney W. Grimes 		if (freopen(argv[0], "r", stdin) == NULL)
1132487a449SPhilippe Charnier 			err(1, "%s", argv[0]);
1149b50d902SRodney W. Grimes 		process();
1159b50d902SRodney W. Grimes 		argc--, argv++;
1169b50d902SRodney W. Grimes 	} while (argc > 0);
1179b50d902SRodney W. Grimes 	exit(0);
1189b50d902SRodney W. Grimes }
1199b50d902SRodney W. Grimes 
120ffc6a8e3SMark Murray void
usage(void)121ffc6a8e3SMark Murray usage(void)
1222487a449SPhilippe Charnier {
1232487a449SPhilippe Charnier 	fprintf(stderr, "usage: mkstr [-] mesgfile prefix file ...\n");
1242487a449SPhilippe Charnier 	exit(1);
1252487a449SPhilippe Charnier }
1262487a449SPhilippe Charnier 
1272487a449SPhilippe Charnier void
process(void)128ffc6a8e3SMark Murray process(void)
1299b50d902SRodney W. Grimes {
1301627c04dSDavid Malone 	int c;
1319b50d902SRodney W. Grimes 
1329b50d902SRodney W. Grimes 	for (;;) {
1339b50d902SRodney W. Grimes 		c = getchar();
1349b50d902SRodney W. Grimes 		if (c == EOF)
1359b50d902SRodney W. Grimes 			return;
1369b50d902SRodney W. Grimes 		if (c != 'e') {
1379b50d902SRodney W. Grimes 			putchar(c);
1389b50d902SRodney W. Grimes 			continue;
1399b50d902SRodney W. Grimes 		}
1409b50d902SRodney W. Grimes 		if (match("error(")) {
1419b50d902SRodney W. Grimes 			printf("error(");
1429b50d902SRodney W. Grimes 			c = getchar();
1439b50d902SRodney W. Grimes 			if (c != '"')
1449b50d902SRodney W. Grimes 				putchar(c);
1459b50d902SRodney W. Grimes 			else
1469b50d902SRodney W. Grimes 				copystr();
1479b50d902SRodney W. Grimes 		}
1489b50d902SRodney W. Grimes 	}
1499b50d902SRodney W. Grimes }
1509b50d902SRodney W. Grimes 
1512487a449SPhilippe Charnier int
match(const char * ocp)152ffc6a8e3SMark Murray match(const char *ocp)
1539b50d902SRodney W. Grimes {
1541627c04dSDavid Malone 	const char *cp;
1551627c04dSDavid Malone 	int c;
1569b50d902SRodney W. Grimes 
1579b50d902SRodney W. Grimes 	for (cp = ocp + 1; *cp; cp++) {
1589b50d902SRodney W. Grimes 		c = getchar();
1599b50d902SRodney W. Grimes 		if (c != *cp) {
1609b50d902SRodney W. Grimes 			while (ocp < cp)
1619b50d902SRodney W. Grimes 				putchar(*ocp++);
1629b50d902SRodney W. Grimes 			ungetchar(c);
1639b50d902SRodney W. Grimes 			return (0);
1649b50d902SRodney W. Grimes 		}
1659b50d902SRodney W. Grimes 	}
1669b50d902SRodney W. Grimes 	return (1);
1679b50d902SRodney W. Grimes }
1689b50d902SRodney W. Grimes 
1692487a449SPhilippe Charnier void
copystr(void)170ffc6a8e3SMark Murray copystr(void)
1719b50d902SRodney W. Grimes {
1721627c04dSDavid Malone 	int c, ch;
1739b50d902SRodney W. Grimes 	char buf[512];
174ffc6a8e3SMark Murray 	char *cp = buf;
1759b50d902SRodney W. Grimes 
1769b50d902SRodney W. Grimes 	for (;;) {
17792d2efaaSTim J. Robbins 		if (cp == buf + sizeof(buf) - 2)
17892d2efaaSTim J. Robbins 			errx(1, "message too long");
1799b50d902SRodney W. Grimes 		c = getchar();
1809b50d902SRodney W. Grimes 		if (c == EOF)
1819b50d902SRodney W. Grimes 			break;
1829b50d902SRodney W. Grimes 		switch (c) {
1839b50d902SRodney W. Grimes 
1849b50d902SRodney W. Grimes 		case '"':
1859b50d902SRodney W. Grimes 			*cp++ = 0;
1869b50d902SRodney W. Grimes 			goto out;
1879b50d902SRodney W. Grimes 		case '\\':
1889b50d902SRodney W. Grimes 			c = getchar();
1899b50d902SRodney W. Grimes 			switch (c) {
1909b50d902SRodney W. Grimes 
1919b50d902SRodney W. Grimes 			case 'b':
1929b50d902SRodney W. Grimes 				c = '\b';
1939b50d902SRodney W. Grimes 				break;
1949b50d902SRodney W. Grimes 			case 't':
1959b50d902SRodney W. Grimes 				c = '\t';
1969b50d902SRodney W. Grimes 				break;
1979b50d902SRodney W. Grimes 			case 'r':
1989b50d902SRodney W. Grimes 				c = '\r';
1999b50d902SRodney W. Grimes 				break;
2009b50d902SRodney W. Grimes 			case 'n':
2019b50d902SRodney W. Grimes 				c = '\n';
2029b50d902SRodney W. Grimes 				break;
2039b50d902SRodney W. Grimes 			case '\n':
2049b50d902SRodney W. Grimes 				continue;
2059b50d902SRodney W. Grimes 			case 'f':
2069b50d902SRodney W. Grimes 				c = '\f';
2079b50d902SRodney W. Grimes 				break;
2089b50d902SRodney W. Grimes 			case '0':
2099b50d902SRodney W. Grimes 				c = 0;
2109b50d902SRodney W. Grimes 				break;
2119b50d902SRodney W. Grimes 			case '\\':
2129b50d902SRodney W. Grimes 				break;
2139b50d902SRodney W. Grimes 			default:
2149b50d902SRodney W. Grimes 				if (!octdigit(c))
2159b50d902SRodney W. Grimes 					break;
2169b50d902SRodney W. Grimes 				c -= '0';
2179b50d902SRodney W. Grimes 				ch = getchar();
2189b50d902SRodney W. Grimes 				if (!octdigit(ch))
2199b50d902SRodney W. Grimes 					break;
2209b50d902SRodney W. Grimes 				c <<= 7, c += ch - '0';
2219b50d902SRodney W. Grimes 				ch = getchar();
2229b50d902SRodney W. Grimes 				if (!octdigit(ch))
2239b50d902SRodney W. Grimes 					break;
2249b50d902SRodney W. Grimes 				c <<= 3, c+= ch - '0', ch = -1;
2259b50d902SRodney W. Grimes 				break;
2269b50d902SRodney W. Grimes 			}
2279b50d902SRodney W. Grimes 		}
2289b50d902SRodney W. Grimes 		*cp++ = c;
2299b50d902SRodney W. Grimes 	}
2309b50d902SRodney W. Grimes out:
2319b50d902SRodney W. Grimes 	*cp = 0;
232f342fec6SBruce Evans 	printf("%d", hashit(buf, 1, 0));
2339b50d902SRodney W. Grimes }
2349b50d902SRodney W. Grimes 
2352487a449SPhilippe Charnier int
octdigit(char c)236ffc6a8e3SMark Murray octdigit(char c)
2379b50d902SRodney W. Grimes {
2389b50d902SRodney W. Grimes 
2399b50d902SRodney W. Grimes 	return (c >= '0' && c <= '7');
2409b50d902SRodney W. Grimes }
2419b50d902SRodney W. Grimes 
2422487a449SPhilippe Charnier void
inithash(void)243ffc6a8e3SMark Murray inithash(void)
2449b50d902SRodney W. Grimes {
2459b50d902SRodney W. Grimes 	char buf[512];
2469b50d902SRodney W. Grimes 	int mesgpt = 0;
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes 	rewind(mesgread);
2492487a449SPhilippe Charnier 	while (fgetNUL(buf, sizeof buf, mesgread) != 0) {
2509b50d902SRodney W. Grimes 		hashit(buf, 0, mesgpt);
2519b50d902SRodney W. Grimes 		mesgpt += strlen(buf) + 2;
2529b50d902SRodney W. Grimes 	}
2539b50d902SRodney W. Grimes }
2549b50d902SRodney W. Grimes 
2559b50d902SRodney W. Grimes #define	NBUCKETS	511
2569b50d902SRodney W. Grimes 
2571fbc22f5SBaptiste Daroussin static struct	hash {
2589b50d902SRodney W. Grimes 	long	hval;
2599b50d902SRodney W. Grimes 	unsigned hpt;
2609b50d902SRodney W. Grimes 	struct	hash *hnext;
2619b50d902SRodney W. Grimes } *bucket[NBUCKETS];
2629b50d902SRodney W. Grimes 
2632487a449SPhilippe Charnier unsigned
hashit(char * str,int really,unsigned fakept)264ffc6a8e3SMark Murray hashit(char *str, int really, unsigned fakept)
2659b50d902SRodney W. Grimes {
2669b50d902SRodney W. Grimes 	int i;
267ffc6a8e3SMark Murray 	struct hash *hp;
2689b50d902SRodney W. Grimes 	char buf[512];
2699b50d902SRodney W. Grimes 	long hashval = 0;
270ffc6a8e3SMark Murray 	char *cp;
2719b50d902SRodney W. Grimes 
2729b50d902SRodney W. Grimes 	if (really)
2739b50d902SRodney W. Grimes 		fflush(mesgwrite);
2749b50d902SRodney W. Grimes 	for (cp = str; *cp;)
2759b50d902SRodney W. Grimes 		hashval = (hashval << 1) + *cp++;
2769b50d902SRodney W. Grimes 	i = hashval % NBUCKETS;
2779b50d902SRodney W. Grimes 	if (i < 0)
2789b50d902SRodney W. Grimes 		i += NBUCKETS;
2799b50d902SRodney W. Grimes 	if (really != 0)
2809b50d902SRodney W. Grimes 		for (hp = bucket[i]; hp != 0; hp = hp->hnext)
2819b50d902SRodney W. Grimes 		if (hp->hval == hashval) {
2829b50d902SRodney W. Grimes 			fseek(mesgread, (long) hp->hpt, 0);
2839b50d902SRodney W. Grimes 			fgetNUL(buf, sizeof buf, mesgread);
2849b50d902SRodney W. Grimes /*
2859b50d902SRodney W. Grimes 			fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf);
2869b50d902SRodney W. Grimes */
2879b50d902SRodney W. Grimes 			if (strcmp(buf, str) == 0)
2889b50d902SRodney W. Grimes 				break;
2899b50d902SRodney W. Grimes 		}
2909b50d902SRodney W. Grimes 	if (!really || hp == 0) {
2919b50d902SRodney W. Grimes 		hp = (struct hash *) calloc(1, sizeof *hp);
2924b952f84SPhilippe Charnier 		if (hp == NULL)
2934b952f84SPhilippe Charnier 			err(1, NULL);
2949b50d902SRodney W. Grimes 		hp->hnext = bucket[i];
2959b50d902SRodney W. Grimes 		hp->hval = hashval;
2969b50d902SRodney W. Grimes 		hp->hpt = really ? ftell(mesgwrite) : fakept;
2979b50d902SRodney W. Grimes 		if (really) {
2989b50d902SRodney W. Grimes 			fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite);
2999b50d902SRodney W. Grimes 			fwrite("\n", sizeof (char), 1, mesgwrite);
3009b50d902SRodney W. Grimes 		}
3019b50d902SRodney W. Grimes 		bucket[i] = hp;
3029b50d902SRodney W. Grimes 	}
3039b50d902SRodney W. Grimes /*
3049b50d902SRodney W. Grimes 	fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt);
3059b50d902SRodney W. Grimes */
3069b50d902SRodney W. Grimes 	return (hp->hpt);
3079b50d902SRodney W. Grimes }
3089b50d902SRodney W. Grimes 
3092487a449SPhilippe Charnier int
fgetNUL(char * obuf,int rmdr,FILE * file)310ffc6a8e3SMark Murray fgetNUL(char *obuf, int rmdr, FILE *file)
3119b50d902SRodney W. Grimes {
3121627c04dSDavid Malone 	int c;
313ffc6a8e3SMark Murray 	char *buf = obuf;
3149b50d902SRodney W. Grimes 
3159b50d902SRodney W. Grimes 	while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
3169b50d902SRodney W. Grimes 		*buf++ = c;
3179b50d902SRodney W. Grimes 	*buf++ = 0;
3189b50d902SRodney W. Grimes 	getc(file);
3192487a449SPhilippe Charnier 	return ((feof(file) || ferror(file)) ? 0 : 1);
3209b50d902SRodney W. Grimes }
321