xref: /titanic_51/usr/src/ucbcmd/mkstr/mkstr.c (revision 78b6ed601aa3a251030edda42ce9770e9c21567a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*78b6ed60Scraigm  */
22*78b6ed60Scraigm 
23*78b6ed60Scraigm /*
247c478bd9Sstevel@tonic-gate  * Copyright 1992 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * mkstr - create a string error message file by massaging C source
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * Bill Joy UCB August 1977
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * Modified March 1978 to hash old messages to be able to recompile
367c478bd9Sstevel@tonic-gate  * without addding messages to the message file (usually)
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * Based on an earlier program conceived by Bill Joy and Chuck Haley
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * Program to create a string error message file
417c478bd9Sstevel@tonic-gate  * from a group of C programs.  Arguments are the name
427c478bd9Sstevel@tonic-gate  * of the file where the strings are to be placed, the
437c478bd9Sstevel@tonic-gate  * prefix of the new files where the processed source text
447c478bd9Sstevel@tonic-gate  * is to be placed, and the files to be processed.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * The program looks for 'error("' in the source stream.
477c478bd9Sstevel@tonic-gate  * Whenever it finds this, the following characters from the '"'
487c478bd9Sstevel@tonic-gate  * to a '"' are replaced by 'seekpt' where seekpt is a
497c478bd9Sstevel@tonic-gate  * pointer into the error message file.
507c478bd9Sstevel@tonic-gate  * If the '(' is not immediately followed by a '"' no change occurs.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * The optional '-' causes strings to be added at the end of the
537c478bd9Sstevel@tonic-gate  * existing error message file for recompilation of single routines.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate 
56*78b6ed60Scraigm #include <stdio.h>
57*78b6ed60Scraigm #include <locale.h>
58*78b6ed60Scraigm 
59*78b6ed60Scraigm #define	ungetchar(c)	ungetc(c, stdin)
60*78b6ed60Scraigm 
61*78b6ed60Scraigm long	ftell();
62*78b6ed60Scraigm char	*calloc();
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate FILE	*mesgread, *mesgwrite;
657c478bd9Sstevel@tonic-gate char	*progname;
667c478bd9Sstevel@tonic-gate char	usagestr[] =	"usage: %s [ - ] mesgfile prefix file ...\n";
677c478bd9Sstevel@tonic-gate char	name[100], *np;
687c478bd9Sstevel@tonic-gate 
69*78b6ed60Scraigm int hashit(char *str, char really, unsigned int fakept);
70*78b6ed60Scraigm void process(void);
71*78b6ed60Scraigm void inithash(void);
72*78b6ed60Scraigm int octdigit(char c);
73*78b6ed60Scraigm void copystr(void);
74*78b6ed60Scraigm 
75*78b6ed60Scraigm int
76*78b6ed60Scraigm main(int argc, char *argv[])
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	char addon = 0;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
837c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	argc--, progname = *argv++;
887c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[0][0] == '-')
897c478bd9Sstevel@tonic-gate 		addon++, argc--, argv++;
907c478bd9Sstevel@tonic-gate 	if (argc < 3)
917c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(usagestr), progname), exit(1);
927c478bd9Sstevel@tonic-gate 	mesgwrite = fopen(argv[0], addon ? "a" : "w");
937c478bd9Sstevel@tonic-gate 	if (mesgwrite == NULL)
947c478bd9Sstevel@tonic-gate 		perror(argv[0]), exit(1);
957c478bd9Sstevel@tonic-gate 	mesgread = fopen(argv[0], "r");
967c478bd9Sstevel@tonic-gate 	if (mesgread == NULL)
977c478bd9Sstevel@tonic-gate 		perror(argv[0]), exit(1);
987c478bd9Sstevel@tonic-gate 	inithash();
997c478bd9Sstevel@tonic-gate 	argc--, argv++;
1007c478bd9Sstevel@tonic-gate 	strcpy(name, argv[0]);
1017c478bd9Sstevel@tonic-gate 	np = name + strlen(name);
1027c478bd9Sstevel@tonic-gate 	argc--, argv++;
1037c478bd9Sstevel@tonic-gate 	do {
1047c478bd9Sstevel@tonic-gate 		strcpy(np, argv[0]);
1057c478bd9Sstevel@tonic-gate 		if (freopen(name, "w", stdout) == NULL)
1067c478bd9Sstevel@tonic-gate 			perror(name), exit(1);
1077c478bd9Sstevel@tonic-gate 		if (freopen(argv[0], "r", stdin) == NULL)
1087c478bd9Sstevel@tonic-gate 			perror(argv[0]), exit(1);
1097c478bd9Sstevel@tonic-gate 		process();
1107c478bd9Sstevel@tonic-gate 		argc--, argv++;
1117c478bd9Sstevel@tonic-gate 	} while (argc > 0);
112*78b6ed60Scraigm 	return (0);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
115*78b6ed60Scraigm void
116*78b6ed60Scraigm process(void)
1177c478bd9Sstevel@tonic-gate {
118*78b6ed60Scraigm 	char *cp;
119*78b6ed60Scraigm 	int c;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	for (;;) {
1227c478bd9Sstevel@tonic-gate 		c = getchar();
1237c478bd9Sstevel@tonic-gate 		if (c == EOF)
1247c478bd9Sstevel@tonic-gate 			return;
1257c478bd9Sstevel@tonic-gate 		if (c != 'e') {
1267c478bd9Sstevel@tonic-gate 			putchar(c);
1277c478bd9Sstevel@tonic-gate 			continue;
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 		if (match("error(")) {
1307c478bd9Sstevel@tonic-gate 			printf(gettext("error("));
1317c478bd9Sstevel@tonic-gate 			c = getchar();
1327c478bd9Sstevel@tonic-gate 			if (c != '"')
1337c478bd9Sstevel@tonic-gate 				putchar(c);
1347c478bd9Sstevel@tonic-gate 			else
1357c478bd9Sstevel@tonic-gate 				copystr();
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
140*78b6ed60Scraigm int
141*78b6ed60Scraigm match(char *ocp)
1427c478bd9Sstevel@tonic-gate {
143*78b6ed60Scraigm 	char *cp;
144*78b6ed60Scraigm 	int c;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	for (cp = ocp + 1; *cp; cp++) {
1477c478bd9Sstevel@tonic-gate 		c = getchar();
1487c478bd9Sstevel@tonic-gate 		if (c != *cp) {
1497c478bd9Sstevel@tonic-gate 			while (ocp < cp)
1507c478bd9Sstevel@tonic-gate 				putchar(*ocp++);
1517c478bd9Sstevel@tonic-gate 			ungetchar(c);
1527c478bd9Sstevel@tonic-gate 			return (0);
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	return (1);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
158*78b6ed60Scraigm void
159*78b6ed60Scraigm copystr(void)
1607c478bd9Sstevel@tonic-gate {
161*78b6ed60Scraigm 	int c, ch;
1627c478bd9Sstevel@tonic-gate 	char buf[512];
163*78b6ed60Scraigm 	char *cp = buf;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	for (;;) {
1667c478bd9Sstevel@tonic-gate 		c = getchar();
1677c478bd9Sstevel@tonic-gate 		if (c == EOF)
1687c478bd9Sstevel@tonic-gate 			break;
1697c478bd9Sstevel@tonic-gate 		switch (c) {
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 		case '"':
1727c478bd9Sstevel@tonic-gate 			*cp++ = 0;
1737c478bd9Sstevel@tonic-gate 			goto out;
1747c478bd9Sstevel@tonic-gate 		case '\\':
1757c478bd9Sstevel@tonic-gate 			c = getchar();
1767c478bd9Sstevel@tonic-gate 			switch (c) {
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 			case 'b':
1797c478bd9Sstevel@tonic-gate 				c = '\b';
1807c478bd9Sstevel@tonic-gate 				break;
1817c478bd9Sstevel@tonic-gate 			case 't':
1827c478bd9Sstevel@tonic-gate 				c = '\t';
1837c478bd9Sstevel@tonic-gate 				break;
1847c478bd9Sstevel@tonic-gate 			case 'r':
1857c478bd9Sstevel@tonic-gate 				c = '\r';
1867c478bd9Sstevel@tonic-gate 				break;
1877c478bd9Sstevel@tonic-gate 			case 'n':
1887c478bd9Sstevel@tonic-gate 				c = '\n';
1897c478bd9Sstevel@tonic-gate 				break;
1907c478bd9Sstevel@tonic-gate 			case '\n':
1917c478bd9Sstevel@tonic-gate 				continue;
1927c478bd9Sstevel@tonic-gate 			case 'f':
1937c478bd9Sstevel@tonic-gate 				c = '\f';
1947c478bd9Sstevel@tonic-gate 				break;
1957c478bd9Sstevel@tonic-gate 			case '0':
1967c478bd9Sstevel@tonic-gate 				c = 0;
1977c478bd9Sstevel@tonic-gate 				break;
1987c478bd9Sstevel@tonic-gate 			case '\\':
1997c478bd9Sstevel@tonic-gate 				break;
2007c478bd9Sstevel@tonic-gate 			default:
2017c478bd9Sstevel@tonic-gate 				if (!octdigit(c))
2027c478bd9Sstevel@tonic-gate 					break;
2037c478bd9Sstevel@tonic-gate 				c -= '0';
2047c478bd9Sstevel@tonic-gate 				ch = getchar();
2057c478bd9Sstevel@tonic-gate 				if (!octdigit(ch))
2067c478bd9Sstevel@tonic-gate 					break;
2077c478bd9Sstevel@tonic-gate 				c <<= 7, c += ch - '0';
2087c478bd9Sstevel@tonic-gate 				ch = getchar();
2097c478bd9Sstevel@tonic-gate 				if (!octdigit(ch))
2107c478bd9Sstevel@tonic-gate 					break;
2117c478bd9Sstevel@tonic-gate 				c <<= 3, c+= ch - '0', ch = -1;
2127c478bd9Sstevel@tonic-gate 				break;
2137c478bd9Sstevel@tonic-gate 			}
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 		*cp++ = c;
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate out:
2187c478bd9Sstevel@tonic-gate 	*cp = 0;
2197c478bd9Sstevel@tonic-gate 	printf("%d", hashit(buf, 1, NULL));
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
222*78b6ed60Scraigm int
223*78b6ed60Scraigm octdigit(char c)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	return (c >= '0' && c <= '7');
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
229*78b6ed60Scraigm void
230*78b6ed60Scraigm inithash(void)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	char buf[512];
2337c478bd9Sstevel@tonic-gate 	int mesgpt = 0;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	rewind(mesgread);
2367c478bd9Sstevel@tonic-gate 	while (fgetNUL(buf, sizeof buf, mesgread) != NULL) {
2377c478bd9Sstevel@tonic-gate 		hashit(buf, 0, mesgpt);
2387c478bd9Sstevel@tonic-gate 		mesgpt += strlen(buf) + 2;
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate #define	NBUCKETS	511
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate struct	hash {
2457c478bd9Sstevel@tonic-gate 	long	hval;
246*78b6ed60Scraigm 	unsigned int hpt;
2477c478bd9Sstevel@tonic-gate 	struct	hash *hnext;
2487c478bd9Sstevel@tonic-gate } *bucket[NBUCKETS];
2497c478bd9Sstevel@tonic-gate 
250*78b6ed60Scraigm int
251*78b6ed60Scraigm hashit(char *str, char really, unsigned int fakept)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	int i;
254*78b6ed60Scraigm 	struct hash *hp;
2557c478bd9Sstevel@tonic-gate 	char buf[512];
2567c478bd9Sstevel@tonic-gate 	long hashval = 0;
257*78b6ed60Scraigm 	char *cp;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if (really)
2607c478bd9Sstevel@tonic-gate 		fflush(mesgwrite);
2617c478bd9Sstevel@tonic-gate 	for (cp = str; *cp;)
2627c478bd9Sstevel@tonic-gate 		hashval = (hashval << 1) + *cp++;
2637c478bd9Sstevel@tonic-gate 	i = hashval % NBUCKETS;
2647c478bd9Sstevel@tonic-gate 	if (i < 0)
2657c478bd9Sstevel@tonic-gate 		i += NBUCKETS;
2667c478bd9Sstevel@tonic-gate 	if (really != 0)
2677c478bd9Sstevel@tonic-gate 		for (hp = bucket[i]; hp != 0; hp = hp->hnext)
2687c478bd9Sstevel@tonic-gate 		if (hp->hval == hashval) {
2697c478bd9Sstevel@tonic-gate 			fseek(mesgread, (long) hp->hpt, 0);
2707c478bd9Sstevel@tonic-gate 			fgetNUL(buf, sizeof buf, mesgread);
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Got (from %d) %s\n"), hp->hpt, buf);
2737c478bd9Sstevel@tonic-gate */
2747c478bd9Sstevel@tonic-gate 			if (strcmp(buf, str) == 0)
2757c478bd9Sstevel@tonic-gate 				break;
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 	if (!really || hp == 0) {
2787c478bd9Sstevel@tonic-gate 		hp = (struct hash *) calloc(1, sizeof *hp);
2797c478bd9Sstevel@tonic-gate 		hp->hnext = bucket[i];
2807c478bd9Sstevel@tonic-gate 		hp->hval = hashval;
2817c478bd9Sstevel@tonic-gate 		hp->hpt = really ? ftell(mesgwrite) : fakept;
2827c478bd9Sstevel@tonic-gate 		if (really) {
2837c478bd9Sstevel@tonic-gate 			fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite);
2847c478bd9Sstevel@tonic-gate 			fwrite("\n", sizeof (char), 1, mesgwrite);
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		bucket[i] = hp;
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("%s hashed to %ld at %d\n"), str, hp->hval, hp->hpt);
2907c478bd9Sstevel@tonic-gate */
2917c478bd9Sstevel@tonic-gate 	return (hp->hpt);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate #include <sys/types.h>
2957c478bd9Sstevel@tonic-gate #include <sys/stat.h>
2967c478bd9Sstevel@tonic-gate 
297*78b6ed60Scraigm int
298*78b6ed60Scraigm fgetNUL(char *obuf, int rmdr, FILE *file)
2997c478bd9Sstevel@tonic-gate {
300*78b6ed60Scraigm 	int c;
301*78b6ed60Scraigm 	char *buf = obuf;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
3047c478bd9Sstevel@tonic-gate 		*buf++ = c;
3057c478bd9Sstevel@tonic-gate 	*buf++ = 0;
3067c478bd9Sstevel@tonic-gate 	getc(file);
3077c478bd9Sstevel@tonic-gate 	return ((feof(file) || ferror(file)) ? NULL : 1);
3087c478bd9Sstevel@tonic-gate }
309