xref: /titanic_51/usr/src/cmd/vi/misc/xstr.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 /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  *	Copyright (c) 1999, by Sun Microsystems, Inc.
32*7c478bd9Sstevel@tonic-gate  *	All rights reserved.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
39*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
40*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
41*7c478bd9Sstevel@tonic-gate  *
42*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
43*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
44*7c478bd9Sstevel@tonic-gate  * contributors.
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #include <stdio.h>
48*7c478bd9Sstevel@tonic-gate #include <ctype.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
50*7c478bd9Sstevel@tonic-gate #include <signal.h>
51*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate /*
54*7c478bd9Sstevel@tonic-gate  * xstr - extract and hash strings in a C program
55*7c478bd9Sstevel@tonic-gate  */
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #define	ignore(a)	Ignore((char *) a)
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate off_t	tellpt;
60*7c478bd9Sstevel@tonic-gate off_t	hashit();
61*7c478bd9Sstevel@tonic-gate void	onintr();
62*7c478bd9Sstevel@tonic-gate char	*savestr();
63*7c478bd9Sstevel@tonic-gate char	*strcat();
64*7c478bd9Sstevel@tonic-gate char	*strcpy();
65*7c478bd9Sstevel@tonic-gate off_t	yankstr();
66*7c478bd9Sstevel@tonic-gate void	cleanup(void);
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate off_t	mesgpt;
69*7c478bd9Sstevel@tonic-gate char	*strings =	"strings";
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate int	cflg;
72*7c478bd9Sstevel@tonic-gate int	vflg;
73*7c478bd9Sstevel@tonic-gate int	readstd;
74*7c478bd9Sstevel@tonic-gate int	tmpfd;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate main(argc, argv)
77*7c478bd9Sstevel@tonic-gate 	int argc;
78*7c478bd9Sstevel@tonic-gate 	char *argv[];
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	argc--, argv++;
82*7c478bd9Sstevel@tonic-gate 	while (argc > 0 && argv[0][0] == '-') {
83*7c478bd9Sstevel@tonic-gate 		register char *cp = &(*argv++)[1];
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 		argc--;
86*7c478bd9Sstevel@tonic-gate 		if (*cp == 0) {
87*7c478bd9Sstevel@tonic-gate 			readstd++;
88*7c478bd9Sstevel@tonic-gate 			continue;
89*7c478bd9Sstevel@tonic-gate 		}
90*7c478bd9Sstevel@tonic-gate 		do switch (*cp++) {
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 		case 'c':
93*7c478bd9Sstevel@tonic-gate 			cflg++;
94*7c478bd9Sstevel@tonic-gate 			continue;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 		case 'v':
97*7c478bd9Sstevel@tonic-gate 			vflg++;
98*7c478bd9Sstevel@tonic-gate 			continue;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 		default:
101*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
102*7c478bd9Sstevel@tonic-gate 			    "usage: xstr [ -v ] [ -c ] [ - ] [ name ... ]\n");
103*7c478bd9Sstevel@tonic-gate 		} while (*cp);
104*7c478bd9Sstevel@tonic-gate 	}
105*7c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) == SIG_DFL)
106*7c478bd9Sstevel@tonic-gate 		signal(SIGINT, onintr);
107*7c478bd9Sstevel@tonic-gate 	if (cflg || argc == 0 && !readstd)
108*7c478bd9Sstevel@tonic-gate 		inithash();
109*7c478bd9Sstevel@tonic-gate 	else {
110*7c478bd9Sstevel@tonic-gate 		strings = savestr("/tmp/xstrXXXXXX");
111*7c478bd9Sstevel@tonic-gate 		tmpfd = mkstemp(strings);
112*7c478bd9Sstevel@tonic-gate 		if (tmpfd == -1) {
113*7c478bd9Sstevel@tonic-gate 			perror(strings);
114*7c478bd9Sstevel@tonic-gate 			(void) free(strings);
115*7c478bd9Sstevel@tonic-gate 			exit(9);
116*7c478bd9Sstevel@tonic-gate 		}
117*7c478bd9Sstevel@tonic-gate 		(void) close(tmpfd);
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 	while (readstd || argc > 0) {
120*7c478bd9Sstevel@tonic-gate 		if (freopen("x.c", "w", stdout) == NULL)
121*7c478bd9Sstevel@tonic-gate 			perror("x.c"), (void) cleanup(), exit(1);
122*7c478bd9Sstevel@tonic-gate 		if (!readstd && freopen(argv[0], "r", stdin) == NULL)
123*7c478bd9Sstevel@tonic-gate 			perror(argv[0]), (void) cleanup(), exit(2);
124*7c478bd9Sstevel@tonic-gate 		process("x.c");
125*7c478bd9Sstevel@tonic-gate 		if (readstd == 0)
126*7c478bd9Sstevel@tonic-gate 			argc--, argv++;
127*7c478bd9Sstevel@tonic-gate 		else
128*7c478bd9Sstevel@tonic-gate 			readstd = 0;
129*7c478bd9Sstevel@tonic-gate 	};
130*7c478bd9Sstevel@tonic-gate 	flushsh();
131*7c478bd9Sstevel@tonic-gate 	if (cflg == 0)
132*7c478bd9Sstevel@tonic-gate 		xsdotc();
133*7c478bd9Sstevel@tonic-gate 	(void) cleanup();
134*7c478bd9Sstevel@tonic-gate 	exit(0);
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate process(name)
138*7c478bd9Sstevel@tonic-gate 	char *name;
139*7c478bd9Sstevel@tonic-gate {
140*7c478bd9Sstevel@tonic-gate 	char *cp;
141*7c478bd9Sstevel@tonic-gate 	char linebuf[BUFSIZ];
142*7c478bd9Sstevel@tonic-gate 	register int c;
143*7c478bd9Sstevel@tonic-gate 	register int incomm = 0;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	printf("extern\tchar\txstr[];\n");
146*7c478bd9Sstevel@tonic-gate 	for (;;) {
147*7c478bd9Sstevel@tonic-gate 		if (fgets(linebuf, sizeof (linebuf), stdin) == NULL) {
148*7c478bd9Sstevel@tonic-gate 			if (ferror(stdin)) {
149*7c478bd9Sstevel@tonic-gate 				perror(name);
150*7c478bd9Sstevel@tonic-gate 				(void) cleanup();
151*7c478bd9Sstevel@tonic-gate 				exit(3);
152*7c478bd9Sstevel@tonic-gate 			}
153*7c478bd9Sstevel@tonic-gate 			break;
154*7c478bd9Sstevel@tonic-gate 		}
155*7c478bd9Sstevel@tonic-gate 		if (linebuf[0] == '#') {
156*7c478bd9Sstevel@tonic-gate 			if (linebuf[1] == ' ' && isdigit(linebuf[2]))
157*7c478bd9Sstevel@tonic-gate 				printf("#line%s", &linebuf[1]);
158*7c478bd9Sstevel@tonic-gate 			else
159*7c478bd9Sstevel@tonic-gate 				printf("%s", linebuf);
160*7c478bd9Sstevel@tonic-gate 			continue;
161*7c478bd9Sstevel@tonic-gate 		}
162*7c478bd9Sstevel@tonic-gate 		for (cp = linebuf; c = *cp++; ) {
163*7c478bd9Sstevel@tonic-gate 			switch (c) {
164*7c478bd9Sstevel@tonic-gate 				case '"':
165*7c478bd9Sstevel@tonic-gate 					if (incomm)
166*7c478bd9Sstevel@tonic-gate 						goto def;
167*7c478bd9Sstevel@tonic-gate 					printf("(&xstr[%d])",
168*7c478bd9Sstevel@tonic-gate 					    (int) yankstr(&cp));
169*7c478bd9Sstevel@tonic-gate 					break;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 				case '\'':
172*7c478bd9Sstevel@tonic-gate 					if (incomm)
173*7c478bd9Sstevel@tonic-gate 						goto def;
174*7c478bd9Sstevel@tonic-gate 					putchar(c);
175*7c478bd9Sstevel@tonic-gate 					if (*cp)
176*7c478bd9Sstevel@tonic-gate 						putchar(*cp++);
177*7c478bd9Sstevel@tonic-gate 					break;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 				case '/':
180*7c478bd9Sstevel@tonic-gate 					if (incomm || *cp != '*')
181*7c478bd9Sstevel@tonic-gate 						goto def;
182*7c478bd9Sstevel@tonic-gate 					incomm = 1;
183*7c478bd9Sstevel@tonic-gate 					cp++;
184*7c478bd9Sstevel@tonic-gate 					printf("/*");
185*7c478bd9Sstevel@tonic-gate 					continue;
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 				case '*':
188*7c478bd9Sstevel@tonic-gate 					if (incomm && *cp == '/') {
189*7c478bd9Sstevel@tonic-gate 						incomm = 0;
190*7c478bd9Sstevel@tonic-gate 						cp++;
191*7c478bd9Sstevel@tonic-gate 						printf("*/");
192*7c478bd9Sstevel@tonic-gate 						continue;
193*7c478bd9Sstevel@tonic-gate 					}
194*7c478bd9Sstevel@tonic-gate 					goto def;
195*7c478bd9Sstevel@tonic-gate def:
196*7c478bd9Sstevel@tonic-gate 				default:
197*7c478bd9Sstevel@tonic-gate 					putchar(c);
198*7c478bd9Sstevel@tonic-gate 					break;
199*7c478bd9Sstevel@tonic-gate 			}
200*7c478bd9Sstevel@tonic-gate 		}
201*7c478bd9Sstevel@tonic-gate 	}
202*7c478bd9Sstevel@tonic-gate 	if (ferror(stdout))
203*7c478bd9Sstevel@tonic-gate 		perror("x.c"), onintr();
204*7c478bd9Sstevel@tonic-gate }
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate off_t
207*7c478bd9Sstevel@tonic-gate yankstr(cpp)
208*7c478bd9Sstevel@tonic-gate 	register char **cpp;
209*7c478bd9Sstevel@tonic-gate {
210*7c478bd9Sstevel@tonic-gate 	register char *cp = *cpp;
211*7c478bd9Sstevel@tonic-gate 	register int c, ch;
212*7c478bd9Sstevel@tonic-gate 	char dbuf[BUFSIZ];
213*7c478bd9Sstevel@tonic-gate 	register char *dp = dbuf;
214*7c478bd9Sstevel@tonic-gate 	register char *tp;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	while (c = *cp++) {
217*7c478bd9Sstevel@tonic-gate 		switch (c) {
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 		case '"':
220*7c478bd9Sstevel@tonic-gate 			cp++;
221*7c478bd9Sstevel@tonic-gate 			goto out;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 		case '\\':
224*7c478bd9Sstevel@tonic-gate 			c = *cp++;
225*7c478bd9Sstevel@tonic-gate 			if (c == 0)
226*7c478bd9Sstevel@tonic-gate 				break;
227*7c478bd9Sstevel@tonic-gate 			if (c == '\n')
228*7c478bd9Sstevel@tonic-gate 				continue;
229*7c478bd9Sstevel@tonic-gate 			for (tp = "b\bt\tr\rn\nf\f\\\\\"\""; ch = *tp++; tp++)
230*7c478bd9Sstevel@tonic-gate 				if (c == ch) {
231*7c478bd9Sstevel@tonic-gate 					c = *tp;
232*7c478bd9Sstevel@tonic-gate 					goto gotc;
233*7c478bd9Sstevel@tonic-gate 				}
234*7c478bd9Sstevel@tonic-gate 			if (!octdigit(c)) {
235*7c478bd9Sstevel@tonic-gate 				*dp++ = '\\';
236*7c478bd9Sstevel@tonic-gate 				break;
237*7c478bd9Sstevel@tonic-gate 			}
238*7c478bd9Sstevel@tonic-gate 			c -= '0';
239*7c478bd9Sstevel@tonic-gate 			if (!octdigit(*cp))
240*7c478bd9Sstevel@tonic-gate 				break;
241*7c478bd9Sstevel@tonic-gate 			c <<= 3, c += *cp++ - '0';
242*7c478bd9Sstevel@tonic-gate 			if (!octdigit(*cp))
243*7c478bd9Sstevel@tonic-gate 				break;
244*7c478bd9Sstevel@tonic-gate 			c <<= 3, c += *cp++ - '0';
245*7c478bd9Sstevel@tonic-gate 			break;
246*7c478bd9Sstevel@tonic-gate 		}
247*7c478bd9Sstevel@tonic-gate gotc:
248*7c478bd9Sstevel@tonic-gate 		*dp++ = c;
249*7c478bd9Sstevel@tonic-gate 	}
250*7c478bd9Sstevel@tonic-gate out:
251*7c478bd9Sstevel@tonic-gate 	*cpp = --cp;
252*7c478bd9Sstevel@tonic-gate 	*dp = 0;
253*7c478bd9Sstevel@tonic-gate 	return (hashit(dbuf, 1));
254*7c478bd9Sstevel@tonic-gate }
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate octdigit(c)
257*7c478bd9Sstevel@tonic-gate 	char c;
258*7c478bd9Sstevel@tonic-gate {
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	return (isdigit(c) && c != '8' && c != '9');
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate inithash()
264*7c478bd9Sstevel@tonic-gate {
265*7c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
266*7c478bd9Sstevel@tonic-gate 	register FILE *mesgread = fopen(strings, "r");
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 	if (mesgread == NULL)
269*7c478bd9Sstevel@tonic-gate 		return;
270*7c478bd9Sstevel@tonic-gate 	for (;;) {
271*7c478bd9Sstevel@tonic-gate 		mesgpt = tellpt;
272*7c478bd9Sstevel@tonic-gate 		if (fgetNUL(buf, sizeof (buf), mesgread) == NULL)
273*7c478bd9Sstevel@tonic-gate 			break;
274*7c478bd9Sstevel@tonic-gate 		ignore(hashit(buf, 0));
275*7c478bd9Sstevel@tonic-gate 	}
276*7c478bd9Sstevel@tonic-gate 	ignore(fclose(mesgread));
277*7c478bd9Sstevel@tonic-gate }
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate fgetNUL(obuf, rmdr, file)
280*7c478bd9Sstevel@tonic-gate 	char *obuf;
281*7c478bd9Sstevel@tonic-gate 	register int rmdr;
282*7c478bd9Sstevel@tonic-gate 	FILE *file;
283*7c478bd9Sstevel@tonic-gate {
284*7c478bd9Sstevel@tonic-gate 	register c;
285*7c478bd9Sstevel@tonic-gate 	register char *buf = obuf;
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF)
288*7c478bd9Sstevel@tonic-gate 		*buf++ = c;
289*7c478bd9Sstevel@tonic-gate 	*buf++ = 0;
290*7c478bd9Sstevel@tonic-gate 	return ((feof(file) || ferror(file)) ? NULL : 1);
291*7c478bd9Sstevel@tonic-gate }
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate xgetc(file)
294*7c478bd9Sstevel@tonic-gate 	FILE *file;
295*7c478bd9Sstevel@tonic-gate {
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 	tellpt++;
298*7c478bd9Sstevel@tonic-gate 	return (getc(file));
299*7c478bd9Sstevel@tonic-gate }
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate #define	BUCKETS	128
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate struct	hash {
304*7c478bd9Sstevel@tonic-gate 	off_t	hpt;
305*7c478bd9Sstevel@tonic-gate 	char	*hstr;
306*7c478bd9Sstevel@tonic-gate 	struct	hash *hnext;
307*7c478bd9Sstevel@tonic-gate 	short	hnew;
308*7c478bd9Sstevel@tonic-gate } bucket[BUCKETS];
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate off_t
311*7c478bd9Sstevel@tonic-gate hashit(str, new)
312*7c478bd9Sstevel@tonic-gate 	char *str;
313*7c478bd9Sstevel@tonic-gate 	int new;
314*7c478bd9Sstevel@tonic-gate {
315*7c478bd9Sstevel@tonic-gate 	int i;
316*7c478bd9Sstevel@tonic-gate 	register struct hash *hp, *hp0;
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	hp = hp0 = &bucket[lastchr(str) & 0177];
319*7c478bd9Sstevel@tonic-gate 	while (hp->hnext) {
320*7c478bd9Sstevel@tonic-gate 		hp = hp->hnext;
321*7c478bd9Sstevel@tonic-gate 		i = istail(str, hp->hstr);
322*7c478bd9Sstevel@tonic-gate 		if (i >= 0)
323*7c478bd9Sstevel@tonic-gate 			return (hp->hpt + i);
324*7c478bd9Sstevel@tonic-gate 	}
325*7c478bd9Sstevel@tonic-gate 	hp = (struct hash *) calloc(1, sizeof (*hp));
326*7c478bd9Sstevel@tonic-gate 	hp->hpt = mesgpt;
327*7c478bd9Sstevel@tonic-gate 	hp->hstr = savestr(str);
328*7c478bd9Sstevel@tonic-gate 	mesgpt += strlen(hp->hstr) + 1;
329*7c478bd9Sstevel@tonic-gate 	hp->hnext = hp0->hnext;
330*7c478bd9Sstevel@tonic-gate 	hp->hnew = new;
331*7c478bd9Sstevel@tonic-gate 	hp0->hnext = hp;
332*7c478bd9Sstevel@tonic-gate 	return (hp->hpt);
333*7c478bd9Sstevel@tonic-gate }
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate flushsh()
336*7c478bd9Sstevel@tonic-gate {
337*7c478bd9Sstevel@tonic-gate 	register int i;
338*7c478bd9Sstevel@tonic-gate 	register struct hash *hp;
339*7c478bd9Sstevel@tonic-gate 	register FILE *mesgwrit;
340*7c478bd9Sstevel@tonic-gate 	register int old = 0, new = 0;
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < BUCKETS; i++)
343*7c478bd9Sstevel@tonic-gate 		for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext)
344*7c478bd9Sstevel@tonic-gate 			if (hp->hnew)
345*7c478bd9Sstevel@tonic-gate 				new++;
346*7c478bd9Sstevel@tonic-gate 			else
347*7c478bd9Sstevel@tonic-gate 				old++;
348*7c478bd9Sstevel@tonic-gate 	if (new == 0 && old != 0)
349*7c478bd9Sstevel@tonic-gate 		return;
350*7c478bd9Sstevel@tonic-gate 	mesgwrit = fopen(strings, old ? "r+" : "w");
351*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < BUCKETS; i++)
352*7c478bd9Sstevel@tonic-gate 		for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) {
353*7c478bd9Sstevel@tonic-gate 			found(hp->hnew, hp->hpt, hp->hstr);
354*7c478bd9Sstevel@tonic-gate 			if (hp->hnew) {
355*7c478bd9Sstevel@tonic-gate 				fseek(mesgwrit, hp->hpt, 0);
356*7c478bd9Sstevel@tonic-gate 				ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1,
357*7c478bd9Sstevel@tonic-gate 				    1, mesgwrit));
358*7c478bd9Sstevel@tonic-gate 				if (ferror(mesgwrit)) {
359*7c478bd9Sstevel@tonic-gate 					perror(strings);
360*7c478bd9Sstevel@tonic-gate 					(void) cleanup();
361*7c478bd9Sstevel@tonic-gate 					exit(4);
362*7c478bd9Sstevel@tonic-gate 				}
363*7c478bd9Sstevel@tonic-gate 			}
364*7c478bd9Sstevel@tonic-gate 		}
365*7c478bd9Sstevel@tonic-gate 	ignore(fclose(mesgwrit));
366*7c478bd9Sstevel@tonic-gate }
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate found(new, off, str)
369*7c478bd9Sstevel@tonic-gate 	int new;
370*7c478bd9Sstevel@tonic-gate 	off_t off;
371*7c478bd9Sstevel@tonic-gate 	char *str;
372*7c478bd9Sstevel@tonic-gate {
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	if (vflg == 0)
375*7c478bd9Sstevel@tonic-gate 		return;
376*7c478bd9Sstevel@tonic-gate 	if (!new)
377*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "found at %d:", (int) off);
378*7c478bd9Sstevel@tonic-gate 	else
379*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "new at %d:", (int) off);
380*7c478bd9Sstevel@tonic-gate 	prstr(str);
381*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
382*7c478bd9Sstevel@tonic-gate }
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate prstr(cp)
385*7c478bd9Sstevel@tonic-gate 	register char *cp;
386*7c478bd9Sstevel@tonic-gate {
387*7c478bd9Sstevel@tonic-gate 	register int c;
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	while (c = (*cp++ & 0377))
390*7c478bd9Sstevel@tonic-gate 		if (c < ' ')
391*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "^%c", c + '`');
392*7c478bd9Sstevel@tonic-gate 		else if (c == 0177)
393*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "^?");
394*7c478bd9Sstevel@tonic-gate 		else if (c > 0200)
395*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "\\%03o", c);
396*7c478bd9Sstevel@tonic-gate 		else
397*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%c", c);
398*7c478bd9Sstevel@tonic-gate }
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate xsdotc()
401*7c478bd9Sstevel@tonic-gate {
402*7c478bd9Sstevel@tonic-gate 	register FILE *strf = fopen(strings, "r");
403*7c478bd9Sstevel@tonic-gate 	register FILE *xdotcf;
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 	if (strf == NULL)
406*7c478bd9Sstevel@tonic-gate 		perror(strings), exit(5);
407*7c478bd9Sstevel@tonic-gate 	xdotcf = fopen("xs.c", "w");
408*7c478bd9Sstevel@tonic-gate 	if (xdotcf == NULL)
409*7c478bd9Sstevel@tonic-gate 		perror("xs.c"), exit(6);
410*7c478bd9Sstevel@tonic-gate 	fprintf(xdotcf, "char\txstr[] = {\n");
411*7c478bd9Sstevel@tonic-gate 	for (;;) {
412*7c478bd9Sstevel@tonic-gate 		register int i, c;
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < 8; i++) {
415*7c478bd9Sstevel@tonic-gate 			c = getc(strf);
416*7c478bd9Sstevel@tonic-gate 			if (ferror(strf)) {
417*7c478bd9Sstevel@tonic-gate 				perror(strings);
418*7c478bd9Sstevel@tonic-gate 				onintr();
419*7c478bd9Sstevel@tonic-gate 			}
420*7c478bd9Sstevel@tonic-gate 			if (feof(strf)) {
421*7c478bd9Sstevel@tonic-gate 				fprintf(xdotcf, "\n");
422*7c478bd9Sstevel@tonic-gate 				goto out;
423*7c478bd9Sstevel@tonic-gate 			}
424*7c478bd9Sstevel@tonic-gate 			fprintf(xdotcf, "0x%02x,", c);
425*7c478bd9Sstevel@tonic-gate 		}
426*7c478bd9Sstevel@tonic-gate 		fprintf(xdotcf, "\n");
427*7c478bd9Sstevel@tonic-gate 	}
428*7c478bd9Sstevel@tonic-gate out:
429*7c478bd9Sstevel@tonic-gate 	fprintf(xdotcf, "};\n");
430*7c478bd9Sstevel@tonic-gate 	ignore(fclose(xdotcf));
431*7c478bd9Sstevel@tonic-gate 	ignore(fclose(strf));
432*7c478bd9Sstevel@tonic-gate }
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate char *
435*7c478bd9Sstevel@tonic-gate savestr(cp)
436*7c478bd9Sstevel@tonic-gate 	register char *cp;
437*7c478bd9Sstevel@tonic-gate {
438*7c478bd9Sstevel@tonic-gate 	register char *dp = (char *) calloc(1, strlen(cp) + 1);
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 	return (strcpy(dp, cp));
441*7c478bd9Sstevel@tonic-gate }
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate Ignore(a)
444*7c478bd9Sstevel@tonic-gate 	char *a;
445*7c478bd9Sstevel@tonic-gate {
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 	a = a;
448*7c478bd9Sstevel@tonic-gate }
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate ignorf(a)
451*7c478bd9Sstevel@tonic-gate 	int (*a)();
452*7c478bd9Sstevel@tonic-gate {
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 	a = a;
455*7c478bd9Sstevel@tonic-gate }
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate lastchr(cp)
458*7c478bd9Sstevel@tonic-gate 	register char *cp;
459*7c478bd9Sstevel@tonic-gate {
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate 	while (cp[0] && cp[1])
462*7c478bd9Sstevel@tonic-gate 		cp++;
463*7c478bd9Sstevel@tonic-gate 	return (*cp);
464*7c478bd9Sstevel@tonic-gate }
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate istail(str, of)
467*7c478bd9Sstevel@tonic-gate 	register char *str, *of;
468*7c478bd9Sstevel@tonic-gate {
469*7c478bd9Sstevel@tonic-gate 	register int d = strlen(of) - strlen(str);
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 	if (d < 0 || strcmp(&of[d], str) != 0)
472*7c478bd9Sstevel@tonic-gate 		return (-1);
473*7c478bd9Sstevel@tonic-gate 	return (d);
474*7c478bd9Sstevel@tonic-gate }
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate void
477*7c478bd9Sstevel@tonic-gate onintr()
478*7c478bd9Sstevel@tonic-gate {
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate 	ignorf(signal(SIGINT, SIG_IGN));
481*7c478bd9Sstevel@tonic-gate 	(void) cleanup();
482*7c478bd9Sstevel@tonic-gate 	ignore(unlink("x.c"));
483*7c478bd9Sstevel@tonic-gate 	ignore(unlink("xs.c"));
484*7c478bd9Sstevel@tonic-gate 	exit(7);
485*7c478bd9Sstevel@tonic-gate }
486*7c478bd9Sstevel@tonic-gate void
487*7c478bd9Sstevel@tonic-gate cleanup(void)
488*7c478bd9Sstevel@tonic-gate {
489*7c478bd9Sstevel@tonic-gate 	if (strings[0] == '/') {
490*7c478bd9Sstevel@tonic-gate 		ignore(unlink(strings));
491*7c478bd9Sstevel@tonic-gate 	}
492*7c478bd9Sstevel@tonic-gate }
493