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