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