xref: /illumos-gate/usr/src/cmd/regcmp/regcmp.c (revision 15a0c3a7)
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  */
227c478bd9Sstevel@tonic-gate /*
23b5514887Smuffin  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24b5514887Smuffin  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27b5514887Smuffin /*	Copyright (c) 1988 AT&T	*/
28b5514887Smuffin /*	  All Rights Reserved  	*/
29b5514887Smuffin 
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <locale.h>
327c478bd9Sstevel@tonic-gate #include <libgen.h>
33b5514887Smuffin #include <stdlib.h>
34b5514887Smuffin #include <unistd.h>
35b5514887Smuffin #include <string.h>
36b5514887Smuffin #include <sys/stat.h>
37b5514887Smuffin #include <fcntl.h>
38b5514887Smuffin 
397c478bd9Sstevel@tonic-gate FILE *iobuf;
407c478bd9Sstevel@tonic-gate int gotflg;
417c478bd9Sstevel@tonic-gate char ofile[64];
427c478bd9Sstevel@tonic-gate char a1[1024];
437c478bd9Sstevel@tonic-gate char a2[64];
447c478bd9Sstevel@tonic-gate int c;
457c478bd9Sstevel@tonic-gate 
46b5514887Smuffin int	getnm(char);
47b5514887Smuffin int	size(char *);
48b5514887Smuffin 
49b5514887Smuffin int
main(int argc,char ** argv)50b5514887Smuffin main(int argc, char **argv)
517c478bd9Sstevel@tonic-gate {
52b5514887Smuffin 	char *name, *str, *v;
537c478bd9Sstevel@tonic-gate 	char *bp, *cp, *sv;
547c478bd9Sstevel@tonic-gate 	char *message;
55b5514887Smuffin 	int k, cflg = 0;
567c478bd9Sstevel@tonic-gate 	int status = 0;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
597c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
607c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
617c478bd9Sstevel@tonic-gate #endif
627c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
637c478bd9Sstevel@tonic-gate 	if (argc > 1 && *argv[1] == '-') {
647c478bd9Sstevel@tonic-gate 		cflg++;
657c478bd9Sstevel@tonic-gate 		++argv;
667c478bd9Sstevel@tonic-gate 		argc--;
677c478bd9Sstevel@tonic-gate 	} else cflg = 0;
687c478bd9Sstevel@tonic-gate 	while (--argc) {
697c478bd9Sstevel@tonic-gate 		++argv;
707c478bd9Sstevel@tonic-gate 		bp = *argv;
717c478bd9Sstevel@tonic-gate 		if ((iobuf = fopen(*argv, "r")) == NULL) {
727c478bd9Sstevel@tonic-gate 			message = gettext("can not open ");
737c478bd9Sstevel@tonic-gate 			write(2, message, strlen(message));
747c478bd9Sstevel@tonic-gate 			write(2, *argv, size(*argv));
757c478bd9Sstevel@tonic-gate 			write(2, "\n", 1);
767c478bd9Sstevel@tonic-gate 			/* continues to next file, if any, but the */
777c478bd9Sstevel@tonic-gate 			/* exit status will indicate an error */
787c478bd9Sstevel@tonic-gate 			status = 1;
797c478bd9Sstevel@tonic-gate 			continue;
807c478bd9Sstevel@tonic-gate 		}
817c478bd9Sstevel@tonic-gate 		cp = ofile;
827c478bd9Sstevel@tonic-gate 		while (*++bp)
837c478bd9Sstevel@tonic-gate 			if (*bp == '/') *bp = '\0';
84*15a0c3a7SToomas Soome 		while (*--bp == '\0')
85*15a0c3a7SToomas Soome 			;
867c478bd9Sstevel@tonic-gate 		while (*bp != '\0' && bp > *argv) bp--;
877c478bd9Sstevel@tonic-gate 		while (*bp == 0)
887c478bd9Sstevel@tonic-gate 			bp++;
89*15a0c3a7SToomas Soome 		while (*cp++ = *bp++)
90*15a0c3a7SToomas Soome 			;
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') ||
107*15a0c3a7SToomas Soome 				    (c == ' '))
108*15a0c3a7SToomas Soome 					;
1097c478bd9Sstevel@tonic-gate 			else
1107c478bd9Sstevel@tonic-gate 				gotflg = 0;
1117c478bd9Sstevel@tonic-gate 			if (c == EOF) break;
1127c478bd9Sstevel@tonic-gate 			*name++ = c;
1137c478bd9Sstevel@tonic-gate 			while (((*name++ = c = getc(iobuf)) != ' ') &&
114*15a0c3a7SToomas Soome 			    (c != EOF) && (c != '\n'))
115*15a0c3a7SToomas Soome 				;
116*15a0c3a7SToomas Soome 			*--name = '\0';
117*15a0c3a7SToomas Soome 			while (((c = getc(iobuf)) == ' ') || (c == '\n'))
118*15a0c3a7SToomas Soome 				;
1197c478bd9Sstevel@tonic-gate 			if (c != '"') {
1207c478bd9Sstevel@tonic-gate 				if (c == EOF) {
1217c478bd9Sstevel@tonic-gate 					message = gettext("unexpected eof\n");
1227c478bd9Sstevel@tonic-gate 					write(2, message, strlen(message));
1237c478bd9Sstevel@tonic-gate 					exit(1);
1247c478bd9Sstevel@tonic-gate 				}
1257c478bd9Sstevel@tonic-gate 				message = gettext("missing initial quote for ");
1267c478bd9Sstevel@tonic-gate 				write(2, message, strlen(message));
1277c478bd9Sstevel@tonic-gate 				write(2, a2, size(a2));
1287c478bd9Sstevel@tonic-gate 				message =
1297c478bd9Sstevel@tonic-gate 				    gettext(" : remainder of line ignored\n");
1307c478bd9Sstevel@tonic-gate 				write(2, message, strlen(message));
131*15a0c3a7SToomas Soome 				while ((c = getc(iobuf)) != '\n')
132*15a0c3a7SToomas Soome 					;
1337c478bd9Sstevel@tonic-gate 				continue;
1347c478bd9Sstevel@tonic-gate 			}
1357c478bd9Sstevel@tonic-gate 			keeponl:
1367c478bd9Sstevel@tonic-gate 			while (gotflg || (c = getc(iobuf)) != EOF) {
1377c478bd9Sstevel@tonic-gate 				gotflg = 0;
1387c478bd9Sstevel@tonic-gate 				switch (c) {
1397c478bd9Sstevel@tonic-gate 				case '"':
1407c478bd9Sstevel@tonic-gate 					break;
1417c478bd9Sstevel@tonic-gate 				case '\\':
1427c478bd9Sstevel@tonic-gate 					switch (c = getc(iobuf)) {
1437c478bd9Sstevel@tonic-gate 					case 't':
1447c478bd9Sstevel@tonic-gate 						*str++ = '\011';
1457c478bd9Sstevel@tonic-gate 						continue;
1467c478bd9Sstevel@tonic-gate 					case 'n':
1477c478bd9Sstevel@tonic-gate 						*str++ = '\012';
1487c478bd9Sstevel@tonic-gate 						continue;
1497c478bd9Sstevel@tonic-gate 					case 'r':
1507c478bd9Sstevel@tonic-gate 						*str++ = '\015';
1517c478bd9Sstevel@tonic-gate 						continue;
1527c478bd9Sstevel@tonic-gate 					case 'b':
1537c478bd9Sstevel@tonic-gate 						*str++ = '\010';
1547c478bd9Sstevel@tonic-gate 						continue;
1557c478bd9Sstevel@tonic-gate 					case '\\':
1567c478bd9Sstevel@tonic-gate 						*str++ = '\\';
1577c478bd9Sstevel@tonic-gate 						continue;
1587c478bd9Sstevel@tonic-gate 					default:
1597c478bd9Sstevel@tonic-gate 						if (c <= '7' && c >= '0')
160b5514887Smuffin 							*str++ = getnm((char)c);
1617c478bd9Sstevel@tonic-gate 						else *str++ = c;
1627c478bd9Sstevel@tonic-gate 						continue;
1637c478bd9Sstevel@tonic-gate 					}
1647c478bd9Sstevel@tonic-gate 				default:
1657c478bd9Sstevel@tonic-gate 					*str++ = c;
1667c478bd9Sstevel@tonic-gate 				}
1677c478bd9Sstevel@tonic-gate 				if (c == '"') break;
1687c478bd9Sstevel@tonic-gate 			}
1697c478bd9Sstevel@tonic-gate 			if (c == EOF) {
1707c478bd9Sstevel@tonic-gate 				message = gettext("unexpected eof\n");
1717c478bd9Sstevel@tonic-gate 				write(2, message, strlen(message));
1727c478bd9Sstevel@tonic-gate 				exit(1);
1737c478bd9Sstevel@tonic-gate 			}
174*15a0c3a7SToomas Soome 			while (((c = getc(iobuf)) == '\n') || (c == ' '))
175*15a0c3a7SToomas Soome 				;
1767c478bd9Sstevel@tonic-gate 			if (c == '"') goto keeponl;
1777c478bd9Sstevel@tonic-gate 			else {
1787c478bd9Sstevel@tonic-gate 				gotflg++;
1797c478bd9Sstevel@tonic-gate 			}
1807c478bd9Sstevel@tonic-gate 			*str = '\0';
1817c478bd9Sstevel@tonic-gate 			if (!(sv = v = regcmp(a1, 0))) {
1827c478bd9Sstevel@tonic-gate 				message = gettext("fail: ");
1837c478bd9Sstevel@tonic-gate 				write(2, message, strlen(message));
1847c478bd9Sstevel@tonic-gate 				write(2, a2, size(a2));
1857c478bd9Sstevel@tonic-gate 				write(2, "\n", 1);
1867c478bd9Sstevel@tonic-gate 				continue;
1877c478bd9Sstevel@tonic-gate 			}
1887c478bd9Sstevel@tonic-gate 			printf("/* \"%s\" */\n", a1);
1897c478bd9Sstevel@tonic-gate 			printf("char %s[] = {\n", a2);
1907c478bd9Sstevel@tonic-gate 			while (__i_size > 0) {
1917c478bd9Sstevel@tonic-gate 				for (k = 0; k < 12; k++)
1927c478bd9Sstevel@tonic-gate 					if (__i_size-- > 0)
1937c478bd9Sstevel@tonic-gate 						printf("0%o, ", *v++);
1947c478bd9Sstevel@tonic-gate 				printf("\n");
1957c478bd9Sstevel@tonic-gate 			}
1967c478bd9Sstevel@tonic-gate 			printf("0};\n");
1977c478bd9Sstevel@tonic-gate 			free(sv);
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 		fclose(iobuf);
2007c478bd9Sstevel@tonic-gate 	}
201b5514887Smuffin 	return (status);
2027c478bd9Sstevel@tonic-gate }
203b5514887Smuffin 
204b5514887Smuffin int
size(char * p)205b5514887Smuffin size(char *p)
2067c478bd9Sstevel@tonic-gate {
207b5514887Smuffin 	int i;
208b5514887Smuffin 	char *q;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	i = 0;
2117c478bd9Sstevel@tonic-gate 	q = p;
2127c478bd9Sstevel@tonic-gate 	while (*q++) i++;
2137c478bd9Sstevel@tonic-gate 	return (i);
2147c478bd9Sstevel@tonic-gate }
215b5514887Smuffin 
216b5514887Smuffin int
getnm(char j)217b5514887Smuffin getnm(char j)
2187c478bd9Sstevel@tonic-gate {
219b5514887Smuffin 	int i;
220b5514887Smuffin 	int k;
2217c478bd9Sstevel@tonic-gate 	i = j - '0';
2227c478bd9Sstevel@tonic-gate 	k = 1;
2237c478bd9Sstevel@tonic-gate 	while (++k < 4 && (c = getc(iobuf)) >= '0' && c <= '7')
2247c478bd9Sstevel@tonic-gate 		i = (i*8+(c-'0'));
2257c478bd9Sstevel@tonic-gate 	if (k >= 4)
2267c478bd9Sstevel@tonic-gate 		c = getc(iobuf);
2277c478bd9Sstevel@tonic-gate 	gotflg++;
2287c478bd9Sstevel@tonic-gate 	return (i);
2297c478bd9Sstevel@tonic-gate }
230