xref: /titanic_44/usr/src/cmd/regcmp/regcmp.c (revision b55148877d473978f0b46d593fd6213fa526fcc5)
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
217c478bd9Sstevel@tonic-gate  */
22*b5514887Smuffin /*
23*b5514887Smuffin  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*b5514887Smuffin  * Use is subject to license terms.
25*b5514887Smuffin  */
26*b5514887Smuffin 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*b5514887Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <locale.h>
347c478bd9Sstevel@tonic-gate #include <libgen.h>
35*b5514887Smuffin #include <stdlib.h>
36*b5514887Smuffin #include <unistd.h>
37*b5514887Smuffin #include <string.h>
38*b5514887Smuffin #include <sys/stat.h>
39*b5514887Smuffin #include <fcntl.h>
40*b5514887Smuffin 
417c478bd9Sstevel@tonic-gate FILE *iobuf;
427c478bd9Sstevel@tonic-gate int gotflg;
437c478bd9Sstevel@tonic-gate char ofile[64];
447c478bd9Sstevel@tonic-gate char a1[1024];
457c478bd9Sstevel@tonic-gate char a2[64];
467c478bd9Sstevel@tonic-gate int c;
477c478bd9Sstevel@tonic-gate 
48*b5514887Smuffin int	getnm(char);
49*b5514887Smuffin int	size(char *);
50*b5514887Smuffin 
51*b5514887Smuffin int
main(int argc,char ** argv)52*b5514887Smuffin main(int argc, char **argv)
537c478bd9Sstevel@tonic-gate {
54*b5514887Smuffin 	char *name, *str, *v;
557c478bd9Sstevel@tonic-gate 	char *bp, *cp, *sv;
567c478bd9Sstevel@tonic-gate 	char *message;
57*b5514887Smuffin 	int k, cflg = 0;
587c478bd9Sstevel@tonic-gate 	int status = 0;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
617c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
627c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
657c478bd9Sstevel@tonic-gate 	if (argc > 1 && *argv[1] == '-') {
667c478bd9Sstevel@tonic-gate 		cflg++;
677c478bd9Sstevel@tonic-gate 		++argv;
687c478bd9Sstevel@tonic-gate 		argc--;
697c478bd9Sstevel@tonic-gate 	} else cflg = 0;
707c478bd9Sstevel@tonic-gate 	while (--argc) {
717c478bd9Sstevel@tonic-gate 		++argv;
727c478bd9Sstevel@tonic-gate 		bp = *argv;
737c478bd9Sstevel@tonic-gate 		if ((iobuf = fopen(*argv, "r")) == NULL) {
747c478bd9Sstevel@tonic-gate 			message = gettext("can not open ");
757c478bd9Sstevel@tonic-gate 			write(2, message, strlen(message));
767c478bd9Sstevel@tonic-gate 			write(2, *argv, size(*argv));
777c478bd9Sstevel@tonic-gate 			write(2, "\n", 1);
787c478bd9Sstevel@tonic-gate 			/* continues to next file, if any, but the */
797c478bd9Sstevel@tonic-gate 			/* exit status will indicate an error */
807c478bd9Sstevel@tonic-gate 			status = 1;
817c478bd9Sstevel@tonic-gate 			continue;
827c478bd9Sstevel@tonic-gate 		}
837c478bd9Sstevel@tonic-gate 		cp = ofile;
847c478bd9Sstevel@tonic-gate 		while (*++bp)
857c478bd9Sstevel@tonic-gate 			if (*bp == '/') *bp = '\0';
867c478bd9Sstevel@tonic-gate 		while (*--bp == '\0');
877c478bd9Sstevel@tonic-gate 		while (*bp != '\0' && bp > *argv) bp--;
887c478bd9Sstevel@tonic-gate 		while (*bp == 0)
897c478bd9Sstevel@tonic-gate 			bp++;
907c478bd9Sstevel@tonic-gate 		while (*cp++ = *bp++);
917c478bd9Sstevel@tonic-gate 		cp--; *cp++ = '.';
927c478bd9Sstevel@tonic-gate 		if (cflg) *cp++ = 'c';
937c478bd9Sstevel@tonic-gate 		else *cp++ = 'i';
947c478bd9Sstevel@tonic-gate 		*cp = '\0';
957c478bd9Sstevel@tonic-gate 		close(1);
967c478bd9Sstevel@tonic-gate 		if (creat(ofile, 0644) < 0) {
977c478bd9Sstevel@tonic-gate 			message = gettext("can not create .i file\n");
987c478bd9Sstevel@tonic-gate 			write(2, message, strlen(message));
997c478bd9Sstevel@tonic-gate 			exit(1);
1007c478bd9Sstevel@tonic-gate 		}
1017c478bd9Sstevel@tonic-gate 		gotflg = 0;
1027c478bd9Sstevel@tonic-gate 		while (1) {
1037c478bd9Sstevel@tonic-gate 			str = a1;
1047c478bd9Sstevel@tonic-gate 			name = a2;
1057c478bd9Sstevel@tonic-gate 			if (!gotflg)
1067c478bd9Sstevel@tonic-gate 				while (((c = getc(iobuf)) == '\n') ||
1077c478bd9Sstevel@tonic-gate 				    (c == ' '));
1087c478bd9Sstevel@tonic-gate 			else
1097c478bd9Sstevel@tonic-gate 				gotflg = 0;
1107c478bd9Sstevel@tonic-gate 			if (c == EOF) break;
1117c478bd9Sstevel@tonic-gate 			*name++ = c;
1127c478bd9Sstevel@tonic-gate 			while (((*name++ = c = getc(iobuf)) != ' ') &&
1137c478bd9Sstevel@tonic-gate 			    (c != EOF) && (c != '\n'));
1147c478bd9Sstevel@tonic-gate 				*--name = '\0';
1157c478bd9Sstevel@tonic-gate 			while (((c = getc(iobuf)) == ' ') || (c == '\n'));
1167c478bd9Sstevel@tonic-gate 			if (c != '"') {
1177c478bd9Sstevel@tonic-gate 				if (c == EOF) {
1187c478bd9Sstevel@tonic-gate 					message = gettext("unexpected eof\n");
1197c478bd9Sstevel@tonic-gate 					write(2, message, strlen(message));
1207c478bd9Sstevel@tonic-gate 					exit(1);
1217c478bd9Sstevel@tonic-gate 				}
1227c478bd9Sstevel@tonic-gate 				message = gettext("missing initial quote for ");
1237c478bd9Sstevel@tonic-gate 				write(2, message, strlen(message));
1247c478bd9Sstevel@tonic-gate 				write(2, a2, size(a2));
1257c478bd9Sstevel@tonic-gate 				message =
1267c478bd9Sstevel@tonic-gate 				    gettext(" : remainder of line ignored\n");
1277c478bd9Sstevel@tonic-gate 				write(2, message, strlen(message));
1287c478bd9Sstevel@tonic-gate 				while ((c = getc(iobuf)) != '\n');
1297c478bd9Sstevel@tonic-gate 				continue;
1307c478bd9Sstevel@tonic-gate 			}
1317c478bd9Sstevel@tonic-gate 			keeponl:
1327c478bd9Sstevel@tonic-gate 			while (gotflg || (c = getc(iobuf)) != EOF) {
1337c478bd9Sstevel@tonic-gate 				gotflg = 0;
1347c478bd9Sstevel@tonic-gate 				switch (c) {
1357c478bd9Sstevel@tonic-gate 				case '"':
1367c478bd9Sstevel@tonic-gate 					break;
1377c478bd9Sstevel@tonic-gate 				case '\\':
1387c478bd9Sstevel@tonic-gate 					switch (c = getc(iobuf)) {
1397c478bd9Sstevel@tonic-gate 					case 't':
1407c478bd9Sstevel@tonic-gate 						*str++ = '\011';
1417c478bd9Sstevel@tonic-gate 						continue;
1427c478bd9Sstevel@tonic-gate 					case 'n':
1437c478bd9Sstevel@tonic-gate 						*str++ = '\012';
1447c478bd9Sstevel@tonic-gate 						continue;
1457c478bd9Sstevel@tonic-gate 					case 'r':
1467c478bd9Sstevel@tonic-gate 						*str++ = '\015';
1477c478bd9Sstevel@tonic-gate 						continue;
1487c478bd9Sstevel@tonic-gate 					case 'b':
1497c478bd9Sstevel@tonic-gate 						*str++ = '\010';
1507c478bd9Sstevel@tonic-gate 						continue;
1517c478bd9Sstevel@tonic-gate 					case '\\':
1527c478bd9Sstevel@tonic-gate 						*str++ = '\\';
1537c478bd9Sstevel@tonic-gate 						continue;
1547c478bd9Sstevel@tonic-gate 					default:
1557c478bd9Sstevel@tonic-gate 						if (c <= '7' && c >= '0')
156*b5514887Smuffin 							*str++ = getnm((char)c);
1577c478bd9Sstevel@tonic-gate 						else *str++ = c;
1587c478bd9Sstevel@tonic-gate 						continue;
1597c478bd9Sstevel@tonic-gate 					}
1607c478bd9Sstevel@tonic-gate 				default:
1617c478bd9Sstevel@tonic-gate 					*str++ = c;
1627c478bd9Sstevel@tonic-gate 				}
1637c478bd9Sstevel@tonic-gate 				if (c == '"') break;
1647c478bd9Sstevel@tonic-gate 			}
1657c478bd9Sstevel@tonic-gate 			if (c == EOF) {
1667c478bd9Sstevel@tonic-gate 				message = gettext("unexpected eof\n");
1677c478bd9Sstevel@tonic-gate 				write(2, message, strlen(message));
1687c478bd9Sstevel@tonic-gate 				exit(1);
1697c478bd9Sstevel@tonic-gate 			}
1707c478bd9Sstevel@tonic-gate 			while (((c = getc(iobuf)) == '\n') || (c == ' '));
1717c478bd9Sstevel@tonic-gate 			if (c == '"') goto keeponl;
1727c478bd9Sstevel@tonic-gate 			else {
1737c478bd9Sstevel@tonic-gate 				gotflg++;
1747c478bd9Sstevel@tonic-gate 			}
1757c478bd9Sstevel@tonic-gate 			*str = '\0';
1767c478bd9Sstevel@tonic-gate 			if (!(sv = v = regcmp(a1, 0))) {
1777c478bd9Sstevel@tonic-gate 				message = gettext("fail: ");
1787c478bd9Sstevel@tonic-gate 				write(2, message, strlen(message));
1797c478bd9Sstevel@tonic-gate 				write(2, a2, size(a2));
1807c478bd9Sstevel@tonic-gate 				write(2, "\n", 1);
1817c478bd9Sstevel@tonic-gate 				continue;
1827c478bd9Sstevel@tonic-gate 			}
1837c478bd9Sstevel@tonic-gate 			printf("/* \"%s\" */\n", a1);
1847c478bd9Sstevel@tonic-gate 			printf("char %s[] = {\n", a2);
1857c478bd9Sstevel@tonic-gate 			while (__i_size > 0) {
1867c478bd9Sstevel@tonic-gate 				for (k = 0; k < 12; k++)
1877c478bd9Sstevel@tonic-gate 					if (__i_size-- > 0)
1887c478bd9Sstevel@tonic-gate 						printf("0%o, ", *v++);
1897c478bd9Sstevel@tonic-gate 				printf("\n");
1907c478bd9Sstevel@tonic-gate 			}
1917c478bd9Sstevel@tonic-gate 			printf("0};\n");
1927c478bd9Sstevel@tonic-gate 			free(sv);
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 		fclose(iobuf);
1957c478bd9Sstevel@tonic-gate 	}
196*b5514887Smuffin 	return (status);
1977c478bd9Sstevel@tonic-gate }
198*b5514887Smuffin 
199*b5514887Smuffin int
size(char * p)200*b5514887Smuffin size(char *p)
2017c478bd9Sstevel@tonic-gate {
202*b5514887Smuffin 	int i;
203*b5514887Smuffin 	char *q;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	i = 0;
2067c478bd9Sstevel@tonic-gate 	q = p;
2077c478bd9Sstevel@tonic-gate 	while (*q++) i++;
2087c478bd9Sstevel@tonic-gate 	return (i);
2097c478bd9Sstevel@tonic-gate }
210*b5514887Smuffin 
211*b5514887Smuffin int
getnm(char j)212*b5514887Smuffin getnm(char j)
2137c478bd9Sstevel@tonic-gate {
214*b5514887Smuffin 	int i;
215*b5514887Smuffin 	int k;
2167c478bd9Sstevel@tonic-gate 	i = j - '0';
2177c478bd9Sstevel@tonic-gate 	k = 1;
2187c478bd9Sstevel@tonic-gate 	while (++k < 4 && (c = getc(iobuf)) >= '0' && c <= '7')
2197c478bd9Sstevel@tonic-gate 		i = (i*8+(c-'0'));
2207c478bd9Sstevel@tonic-gate 	if (k >= 4)
2217c478bd9Sstevel@tonic-gate 		c = getc(iobuf);
2227c478bd9Sstevel@tonic-gate 	gotflg++;
2237c478bd9Sstevel@tonic-gate 	return (i);
2247c478bd9Sstevel@tonic-gate }
225