xref: /titanic_52/usr/src/cmd/vi/misc/mkstr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
28*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
29*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
30*7c478bd9Sstevel@tonic-gate  *
31*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
32*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
33*7c478bd9Sstevel@tonic-gate  * contributors.
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
37*7c478bd9Sstevel@tonic-gate #include <stdio.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #define	ungetchar(c)	ungetc(c, stdin)
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate long	ftell();
42*7c478bd9Sstevel@tonic-gate char	*calloc();
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * mkstr - create a string error message file by massaging C source
45*7c478bd9Sstevel@tonic-gate  *
46*7c478bd9Sstevel@tonic-gate  * Modified March 1978 to hash old messages to be able to recompile
47*7c478bd9Sstevel@tonic-gate  * without adding messages to the message file (usually)
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  * Program to create a string error message file
50*7c478bd9Sstevel@tonic-gate  * from a group of C programs.  Arguments are the name
51*7c478bd9Sstevel@tonic-gate  * of the file where the strings are to be placed, the
52*7c478bd9Sstevel@tonic-gate  * prefix of the new files where the processed source text
53*7c478bd9Sstevel@tonic-gate  * is to be placed, and the files to be processed.
54*7c478bd9Sstevel@tonic-gate  *
55*7c478bd9Sstevel@tonic-gate  * The program looks for 'error("' in the source stream.
56*7c478bd9Sstevel@tonic-gate  * Whenever it finds this, the following characters from the '"'
57*7c478bd9Sstevel@tonic-gate  * to a '"' are replaced by 'seekpt' where seekpt is a
58*7c478bd9Sstevel@tonic-gate  * pointer into the error message file.
59*7c478bd9Sstevel@tonic-gate  * If the '(' is not immediately followed by a '"' no change occurs.
60*7c478bd9Sstevel@tonic-gate  *
61*7c478bd9Sstevel@tonic-gate  * The optional '-' causes strings to be added at the end of the
62*7c478bd9Sstevel@tonic-gate  * existing error message file for recompilation of single routines.
63*7c478bd9Sstevel@tonic-gate  */
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate FILE	*mesgread, *mesgwrite;
67*7c478bd9Sstevel@tonic-gate char	*progname;
68*7c478bd9Sstevel@tonic-gate char	usagestr[] =	"usage: %s [ - ] mesgfile prefix file ...\n";
69*7c478bd9Sstevel@tonic-gate char	name[100], *np;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate main(argc, argv)
72*7c478bd9Sstevel@tonic-gate 	int argc;
73*7c478bd9Sstevel@tonic-gate 	char *argv[];
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	char addon = 0;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	argc--, progname = *argv++;
78*7c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[0][0] == '-')
79*7c478bd9Sstevel@tonic-gate 		addon++, argc--, argv++;
80*7c478bd9Sstevel@tonic-gate 	if (argc < 3)
81*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, usagestr, progname), exit(1);
82*7c478bd9Sstevel@tonic-gate 	mesgwrite = fopen(argv[0], addon ? "a" : "w");
83*7c478bd9Sstevel@tonic-gate 	if (mesgwrite == NULL)
84*7c478bd9Sstevel@tonic-gate 		perror(argv[0]), exit(1);
85*7c478bd9Sstevel@tonic-gate 	mesgread = fopen(argv[0], "r");
86*7c478bd9Sstevel@tonic-gate 	if (mesgread == NULL)
87*7c478bd9Sstevel@tonic-gate 		perror(argv[0]), exit(1);
88*7c478bd9Sstevel@tonic-gate 	inithash();
89*7c478bd9Sstevel@tonic-gate 	argc--, argv++;
90*7c478bd9Sstevel@tonic-gate 	strcpy(name, argv[0]);
91*7c478bd9Sstevel@tonic-gate 	np = name + strlen(name);
92*7c478bd9Sstevel@tonic-gate 	argc--, argv++;
93*7c478bd9Sstevel@tonic-gate 	do {
94*7c478bd9Sstevel@tonic-gate 		strcpy(np, argv[0]);
95*7c478bd9Sstevel@tonic-gate 		if (freopen(name, "w", stdout) == NULL)
96*7c478bd9Sstevel@tonic-gate 			perror(name), exit(1);
97*7c478bd9Sstevel@tonic-gate 		if (freopen(argv[0], "r", stdin) == NULL)
98*7c478bd9Sstevel@tonic-gate 			perror(argv[0]), exit(1);
99*7c478bd9Sstevel@tonic-gate 		process();
100*7c478bd9Sstevel@tonic-gate 		argc--, argv++;
101*7c478bd9Sstevel@tonic-gate 	} while (argc > 0);
102*7c478bd9Sstevel@tonic-gate 	exit(0);
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate process()
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	register c;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	for (;;) {
110*7c478bd9Sstevel@tonic-gate 		c = getchar();
111*7c478bd9Sstevel@tonic-gate 		if (c == EOF)
112*7c478bd9Sstevel@tonic-gate 			return;
113*7c478bd9Sstevel@tonic-gate 		if (c != 'e') {
114*7c478bd9Sstevel@tonic-gate 			putchar(c);
115*7c478bd9Sstevel@tonic-gate 			continue;
116*7c478bd9Sstevel@tonic-gate 		}
117*7c478bd9Sstevel@tonic-gate 		if (match("error(")) {
118*7c478bd9Sstevel@tonic-gate 			printf("error(");
119*7c478bd9Sstevel@tonic-gate 			c = getchar();
120*7c478bd9Sstevel@tonic-gate 			if (c != '"')
121*7c478bd9Sstevel@tonic-gate 				putchar(c);
122*7c478bd9Sstevel@tonic-gate 			else
123*7c478bd9Sstevel@tonic-gate 				copystr();
124*7c478bd9Sstevel@tonic-gate 		}
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate match(ocp)
129*7c478bd9Sstevel@tonic-gate 	char *ocp;
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	register char *cp;
132*7c478bd9Sstevel@tonic-gate 	register c;
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	for (cp = ocp + 1; *cp; cp++) {
135*7c478bd9Sstevel@tonic-gate 		c = getchar();
136*7c478bd9Sstevel@tonic-gate 		if (c != *cp) {
137*7c478bd9Sstevel@tonic-gate 			while (ocp < cp)
138*7c478bd9Sstevel@tonic-gate 				putchar(*ocp++);
139*7c478bd9Sstevel@tonic-gate 			ungetchar(c);
140*7c478bd9Sstevel@tonic-gate 			return (0);
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 	}
143*7c478bd9Sstevel@tonic-gate 	return (1);
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate copystr()
147*7c478bd9Sstevel@tonic-gate {
148*7c478bd9Sstevel@tonic-gate 	register c, ch;
149*7c478bd9Sstevel@tonic-gate 	char buf[512];
150*7c478bd9Sstevel@tonic-gate 	register char *cp = buf;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	for (;;) {
153*7c478bd9Sstevel@tonic-gate 		c = getchar();
154*7c478bd9Sstevel@tonic-gate 		if (c == EOF)
155*7c478bd9Sstevel@tonic-gate 			break;
156*7c478bd9Sstevel@tonic-gate 		switch (c) {
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 		case '"':
159*7c478bd9Sstevel@tonic-gate 			*cp++ = 0;
160*7c478bd9Sstevel@tonic-gate 			goto out;
161*7c478bd9Sstevel@tonic-gate 		case '\\':
162*7c478bd9Sstevel@tonic-gate 			c = getchar();
163*7c478bd9Sstevel@tonic-gate 			switch (c) {
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 			case 'b':
166*7c478bd9Sstevel@tonic-gate 				c = '\b';
167*7c478bd9Sstevel@tonic-gate 				break;
168*7c478bd9Sstevel@tonic-gate 			case 't':
169*7c478bd9Sstevel@tonic-gate 				c = '\t';
170*7c478bd9Sstevel@tonic-gate 				break;
171*7c478bd9Sstevel@tonic-gate 			case 'r':
172*7c478bd9Sstevel@tonic-gate 				c = '\r';
173*7c478bd9Sstevel@tonic-gate 				break;
174*7c478bd9Sstevel@tonic-gate 			case 'n':
175*7c478bd9Sstevel@tonic-gate 				c = '\n';
176*7c478bd9Sstevel@tonic-gate 				break;
177*7c478bd9Sstevel@tonic-gate 			case '\n':
178*7c478bd9Sstevel@tonic-gate 				continue;
179*7c478bd9Sstevel@tonic-gate 			case 'f':
180*7c478bd9Sstevel@tonic-gate 				c = '\f';
181*7c478bd9Sstevel@tonic-gate 				break;
182*7c478bd9Sstevel@tonic-gate 			case '0':
183*7c478bd9Sstevel@tonic-gate 				c = 0;
184*7c478bd9Sstevel@tonic-gate 				break;
185*7c478bd9Sstevel@tonic-gate 			case '\\':
186*7c478bd9Sstevel@tonic-gate 				break;
187*7c478bd9Sstevel@tonic-gate 			default:
188*7c478bd9Sstevel@tonic-gate 				if (!octdigit(c))
189*7c478bd9Sstevel@tonic-gate 					break;
190*7c478bd9Sstevel@tonic-gate 				c -= '0';
191*7c478bd9Sstevel@tonic-gate 				ch = getchar();
192*7c478bd9Sstevel@tonic-gate 				if (!octdigit(ch))
193*7c478bd9Sstevel@tonic-gate 					break;
194*7c478bd9Sstevel@tonic-gate 				c <<= 7, c += ch - '0';
195*7c478bd9Sstevel@tonic-gate 				ch = getchar();
196*7c478bd9Sstevel@tonic-gate 				if (!octdigit(ch))
197*7c478bd9Sstevel@tonic-gate 					break;
198*7c478bd9Sstevel@tonic-gate 				c <<= 3, c+= ch - '0', ch = -1;
199*7c478bd9Sstevel@tonic-gate 				break;
200*7c478bd9Sstevel@tonic-gate 			}
201*7c478bd9Sstevel@tonic-gate 		}
202*7c478bd9Sstevel@tonic-gate 		*cp++ = c;
203*7c478bd9Sstevel@tonic-gate 	}
204*7c478bd9Sstevel@tonic-gate out:
205*7c478bd9Sstevel@tonic-gate 	*cp = 0;
206*7c478bd9Sstevel@tonic-gate 	printf("%d", hashit(buf, 1, NULL));
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate octdigit(c)
210*7c478bd9Sstevel@tonic-gate 	char c;
211*7c478bd9Sstevel@tonic-gate {
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	return (c >= '0' && c <= '7');
214*7c478bd9Sstevel@tonic-gate }
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate inithash()
217*7c478bd9Sstevel@tonic-gate {
218*7c478bd9Sstevel@tonic-gate 	char buf[512];
219*7c478bd9Sstevel@tonic-gate 	int mesgpt = 0;
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	rewind(mesgread);
222*7c478bd9Sstevel@tonic-gate 	while (fgetNUL(buf, sizeof buf, mesgread) != NULL) {
223*7c478bd9Sstevel@tonic-gate 		hashit(buf, 0, mesgpt);
224*7c478bd9Sstevel@tonic-gate 		mesgpt += strlen(buf) + 2;
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate #define	NBUCKETS	511
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate struct	hash {
231*7c478bd9Sstevel@tonic-gate 	long	hval;
232*7c478bd9Sstevel@tonic-gate 	unsigned hpt;
233*7c478bd9Sstevel@tonic-gate 	struct	hash *hnext;
234*7c478bd9Sstevel@tonic-gate } *bucket[NBUCKETS];
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate hashit(str, really, fakept)
237*7c478bd9Sstevel@tonic-gate 	char *str;
238*7c478bd9Sstevel@tonic-gate 	char really;
239*7c478bd9Sstevel@tonic-gate 	unsigned fakept;
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate 	int i;
242*7c478bd9Sstevel@tonic-gate 	register struct hash *hp;
243*7c478bd9Sstevel@tonic-gate 	char buf[512];
244*7c478bd9Sstevel@tonic-gate 	long hashval = 0;
245*7c478bd9Sstevel@tonic-gate 	register char *cp;
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	if (really)
248*7c478bd9Sstevel@tonic-gate 		fflush(mesgwrite);
249*7c478bd9Sstevel@tonic-gate 	for (cp = str; *cp;)
250*7c478bd9Sstevel@tonic-gate 		hashval = (hashval << 1) + *cp++;
251*7c478bd9Sstevel@tonic-gate 	i = hashval % NBUCKETS;
252*7c478bd9Sstevel@tonic-gate 	if (i < 0)
253*7c478bd9Sstevel@tonic-gate 		i += NBUCKETS;
254*7c478bd9Sstevel@tonic-gate 	if (really != 0)
255*7c478bd9Sstevel@tonic-gate 		for (hp = bucket[i]; hp != 0; hp = hp->hnext)
256*7c478bd9Sstevel@tonic-gate 		if (hp->hval == hashval) {
257*7c478bd9Sstevel@tonic-gate 			fseek(mesgread, (long) hp->hpt, 0);
258*7c478bd9Sstevel@tonic-gate 			fgetNUL(buf, sizeof buf, mesgread);
259*7c478bd9Sstevel@tonic-gate /*
260*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf);
261*7c478bd9Sstevel@tonic-gate */
262*7c478bd9Sstevel@tonic-gate 			if (strcmp(buf, str) == 0)
263*7c478bd9Sstevel@tonic-gate 				break;
264*7c478bd9Sstevel@tonic-gate 		}
265*7c478bd9Sstevel@tonic-gate 	if (!really || hp == 0) {
266*7c478bd9Sstevel@tonic-gate 		hp = (struct hash *) calloc(1, sizeof *hp);
267*7c478bd9Sstevel@tonic-gate 		hp->hnext = bucket[i];
268*7c478bd9Sstevel@tonic-gate 		hp->hval = hashval;
269*7c478bd9Sstevel@tonic-gate 		hp->hpt = really ? ftell(mesgwrite) : fakept;
270*7c478bd9Sstevel@tonic-gate 		if (really) {
271*7c478bd9Sstevel@tonic-gate 			fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite);
272*7c478bd9Sstevel@tonic-gate 			fwrite("\n", sizeof (char), 1, mesgwrite);
273*7c478bd9Sstevel@tonic-gate 		}
274*7c478bd9Sstevel@tonic-gate 		bucket[i] = hp;
275*7c478bd9Sstevel@tonic-gate 	}
276*7c478bd9Sstevel@tonic-gate /*
277*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt);
278*7c478bd9Sstevel@tonic-gate */
279*7c478bd9Sstevel@tonic-gate 	return (hp->hpt);
280*7c478bd9Sstevel@tonic-gate }
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
283*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate fgetNUL(obuf, rmdr, file)
286*7c478bd9Sstevel@tonic-gate 	char *obuf;
287*7c478bd9Sstevel@tonic-gate 	register int rmdr;
288*7c478bd9Sstevel@tonic-gate 	FILE *file;
289*7c478bd9Sstevel@tonic-gate {
290*7c478bd9Sstevel@tonic-gate 	register c;
291*7c478bd9Sstevel@tonic-gate 	register char *buf = obuf;
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
294*7c478bd9Sstevel@tonic-gate 		*buf++ = c;
295*7c478bd9Sstevel@tonic-gate 	*buf++ = 0;
296*7c478bd9Sstevel@tonic-gate 	getc(file);
297*7c478bd9Sstevel@tonic-gate 	return ((feof(file) || ferror(file)) ? NULL : 1);
298*7c478bd9Sstevel@tonic-gate }
299