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 (c) 1996, by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All Rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
27*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate *************************************************************************
44*0Sstevel@tonic-gate *			COPYRIGHT NOTICE				*
45*0Sstevel@tonic-gate *************************************************************************
46*0Sstevel@tonic-gate *	This software is copyright(C) 1982 by Pavel Curtis		*
47*0Sstevel@tonic-gate *									*
48*0Sstevel@tonic-gate *	Permission is granted to reproduce and distribute		*
49*0Sstevel@tonic-gate *	this file by any means so long as no fee is charged		*
50*0Sstevel@tonic-gate *	above a nominal handling fee and so long as this		*
51*0Sstevel@tonic-gate *	notice is always included in the copies.			*
52*0Sstevel@tonic-gate *									*
53*0Sstevel@tonic-gate *	Other rights are reserved except as explicitly granted		*
54*0Sstevel@tonic-gate *	by written permission of the author.				*
55*0Sstevel@tonic-gate *		Pavel Curtis						*
56*0Sstevel@tonic-gate *		Computer Science Dept.					*
57*0Sstevel@tonic-gate *		405 Upson Halli						*
58*0Sstevel@tonic-gate *		Cornell Universityi					*
59*0Sstevel@tonic-gate *		Ithaca, NY 14853					*
60*0Sstevel@tonic-gate *									*
61*0Sstevel@tonic-gate *		Ph- (607) 256-4934					*
62*0Sstevel@tonic-gate *									*
63*0Sstevel@tonic-gate *		Pavel.Cornell@Udel-Relay(ARPAnet)			*
64*0Sstevel@tonic-gate *		decvax!cornell!pavel(UUCPnet)				*
65*0Sstevel@tonic-gate *********************************************************************** */
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /*
68*0Sstevel@tonic-gate  *	comp_main.c --- Main program for terminfo compiler
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  *  $Log:	RCS/comp_main.v $
71*0Sstevel@tonic-gate  * Revision 2.1  82/10/25  14:45:37  pavel
72*0Sstevel@tonic-gate  * Added Copyright Notice
73*0Sstevel@tonic-gate  *
74*0Sstevel@tonic-gate  * Revision 2.0  82/10/24  15:16:37  pavel
75*0Sstevel@tonic-gate  * Beta-one Test Release
76*0Sstevel@tonic-gate  *
77*0Sstevel@tonic-gate  * Revision 1.3  82/08/23  22:29:36  pavel
78*0Sstevel@tonic-gate  * The REAL Alpha-one Release Version
79*0Sstevel@tonic-gate  *
80*0Sstevel@tonic-gate  * Revision 1.2  82/08/19  19:09:49  pavel
81*0Sstevel@tonic-gate  * Alpha Test Release One
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  * Revision 1.1  82/08/12  18:36:55  pavel
84*0Sstevel@tonic-gate  * Initial revision
85*0Sstevel@tonic-gate  *
86*0Sstevel@tonic-gate  *
87*0Sstevel@tonic-gate  */
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate #define	EXTERN		/* EXTERN=extern in other .c files */
91*0Sstevel@tonic-gate #include <sys/types.h>
92*0Sstevel@tonic-gate #include <sys/stat.h>
93*0Sstevel@tonic-gate #include <stdlib.h>
94*0Sstevel@tonic-gate #include <unistd.h>
95*0Sstevel@tonic-gate #include "compiler.h"
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate char	*source_file = "./terminfo.src";
98*0Sstevel@tonic-gate char	*destination = SRCDIR;
99*0Sstevel@tonic-gate char	*usage_string = "[-v[n]] [-c] source-file\n";
100*0Sstevel@tonic-gate char	check_only = 0;
101*0Sstevel@tonic-gate char	*progname;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate extern void make_hash_table();	/* should be in a header file :-( */
104*0Sstevel@tonic-gate extern void compile();		/* should be in a header file :-( */
105*0Sstevel@tonic-gate extern void syserr_abort();		/* should be in a header file :-( */
106*0Sstevel@tonic-gate static void init();
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate main(int argc, char *argv[])
109*0Sstevel@tonic-gate {
110*0Sstevel@tonic-gate 	int	i;
111*0Sstevel@tonic-gate 	int	argflag = FALSE;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	debug_level = 0;
114*0Sstevel@tonic-gate 	progname = argv[0];
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	umask(022);
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
119*0Sstevel@tonic-gate 	    if (argv[i][0] == '-') {
120*0Sstevel@tonic-gate 		switch (argv[i][1]) {
121*0Sstevel@tonic-gate 		    case 'c':
122*0Sstevel@tonic-gate 			check_only = 1;
123*0Sstevel@tonic-gate 			break;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 		    case 'v':
126*0Sstevel@tonic-gate 			debug_level = argv[i][2]  ?  atoi(&argv[i][2])  :  1;
127*0Sstevel@tonic-gate 			break;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 		    default:
130*0Sstevel@tonic-gate 			fprintf(stderr,
131*0Sstevel@tonic-gate 			    "%s: Unknown option. Usage is:\n\t%s: %s\n",
132*0Sstevel@tonic-gate 			    argv[0], progname, usage_string);
133*0Sstevel@tonic-gate 				exit(1);
134*0Sstevel@tonic-gate 		}
135*0Sstevel@tonic-gate 	    } else if (argflag) {
136*0Sstevel@tonic-gate 		fprintf(stderr, "%s: Too many file names.  Usage is:\n\t%s\n",
137*0Sstevel@tonic-gate 		    argv[0], usage_string);
138*0Sstevel@tonic-gate 			exit(1);
139*0Sstevel@tonic-gate 	    } else {
140*0Sstevel@tonic-gate 		argflag = TRUE;
141*0Sstevel@tonic-gate 		source_file = argv[i];
142*0Sstevel@tonic-gate 	    }
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	init();
146*0Sstevel@tonic-gate 	make_hash_table();
147*0Sstevel@tonic-gate 	compile();
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	exit(0);
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	return(0);
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate /*
155*0Sstevel@tonic-gate  *	init()
156*0Sstevel@tonic-gate  *
157*0Sstevel@tonic-gate  *	Miscellaneous initializations
158*0Sstevel@tonic-gate  *
159*0Sstevel@tonic-gate  *	Open source file as standard input
160*0Sstevel@tonic-gate  *	Change directory to working terminfo directory.
161*0Sstevel@tonic-gate  *
162*0Sstevel@tonic-gate  */
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate static void
165*0Sstevel@tonic-gate init()
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate 	char		*env = getenv("TERMINFO");
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	start_time = time((time_t *) 0);
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	curr_line = 0;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	if (freopen(source_file, "r", stdin) == NULL) {
174*0Sstevel@tonic-gate 	    fprintf(stderr, "%s: Can't open %s\n", progname, source_file);
175*0Sstevel@tonic-gate 	    exit(1);
176*0Sstevel@tonic-gate 	}
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	if (env && *env)
179*0Sstevel@tonic-gate 	    destination = env;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	if (check_only) {
182*0Sstevel@tonic-gate 		DEBUG(1, "Would be working in %s\n", destination);
183*0Sstevel@tonic-gate 	} else {
184*0Sstevel@tonic-gate 		DEBUG(1, "Working in %s\n", destination);
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	if (access(destination, 7) < 0) {
188*0Sstevel@tonic-gate 		fprintf(stderr, "%s: %s nonexistent or permission denied\n",
189*0Sstevel@tonic-gate 		    progname, destination);
190*0Sstevel@tonic-gate 		exit(1);
191*0Sstevel@tonic-gate 	}
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	if (chdir(destination) < 0) {
194*0Sstevel@tonic-gate 		fprintf(stderr, "%s: %s is not a directory\n",
195*0Sstevel@tonic-gate 		    progname, destination);
196*0Sstevel@tonic-gate 		exit(1);
197*0Sstevel@tonic-gate 	}
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate /*
202*0Sstevel@tonic-gate  *
203*0Sstevel@tonic-gate  *	check_dir(dirletter)
204*0Sstevel@tonic-gate  *
205*0Sstevel@tonic-gate  *	Check for access rights to the destination directory.
206*0Sstevel@tonic-gate  *	Create any directories which don't exist.
207*0Sstevel@tonic-gate  *
208*0Sstevel@tonic-gate  */
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate void
211*0Sstevel@tonic-gate check_dir(char dirletter)
212*0Sstevel@tonic-gate {
213*0Sstevel@tonic-gate 	struct stat64	statbuf;
214*0Sstevel@tonic-gate 	static char	dirnames[128];
215*0Sstevel@tonic-gate 	static char	dir[2] = " ";
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	if (dirnames[dirletter] == 0) {
218*0Sstevel@tonic-gate 	    dir[0] = dirletter;
219*0Sstevel@tonic-gate 	    if (stat64(dir, &statbuf) < 0) {
220*0Sstevel@tonic-gate 		if (mkdir(dir, 0755) < 0)
221*0Sstevel@tonic-gate 			syserr_abort("mkdir %s returned bad status", dir);
222*0Sstevel@tonic-gate 			dirnames[dirletter] = 1;
223*0Sstevel@tonic-gate 	    } else if (access(dir, 7) < 0) {
224*0Sstevel@tonic-gate 			fprintf(stderr, "%s: %s/%s: Permission denied\n",
225*0Sstevel@tonic-gate 			    progname, destination, dir);
226*0Sstevel@tonic-gate 			perror(dir);
227*0Sstevel@tonic-gate 			exit(1);
228*0Sstevel@tonic-gate 	    } else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
229*0Sstevel@tonic-gate 			fprintf(stderr, "%s: %s/%s: Not a directory\n",
230*0Sstevel@tonic-gate 			    progname, destination, dir);
231*0Sstevel@tonic-gate 			perror(dir);
232*0Sstevel@tonic-gate 			exit(1);
233*0Sstevel@tonic-gate 	    }
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 	return;
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate #include <curses.h>
239*0Sstevel@tonic-gate #if (defined(SYSV) || defined(USG)) && !defined(SIGPOLL)
240*0Sstevel@tonic-gate /*
241*0Sstevel@tonic-gate  *	mkdir(dirname, mode)
242*0Sstevel@tonic-gate  *
243*0Sstevel@tonic-gate  *	forks and execs the mkdir program to create the given directory
244*0Sstevel@tonic-gate  *
245*0Sstevel@tonic-gate  */
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate mkdir(dirname, mode)
248*0Sstevel@tonic-gate #ifdef __STDC__
249*0Sstevel@tonic-gate const
250*0Sstevel@tonic-gate #endif
251*0Sstevel@tonic-gate char	*dirname;
252*0Sstevel@tonic-gate int mode;
253*0Sstevel@tonic-gate {
254*0Sstevel@tonic-gate     int	fork_rtn;
255*0Sstevel@tonic-gate     int	status;
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate     fork_rtn = fork();
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate     switch (fork_rtn) {
260*0Sstevel@tonic-gate 	case 0:		/* Child */
261*0Sstevel@tonic-gate 		(void) execl("/bin/mkdir", "mkdir", dirname, (char *)0);
262*0Sstevel@tonic-gate 		_exit(1);
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	case -1:	/* Error */
265*0Sstevel@tonic-gate 		fprintf(stderr, "%s: SYSTEM ERROR!! Fork failed!!!\n",
266*0Sstevel@tonic-gate 		    progname);
267*0Sstevel@tonic-gate 		exit(1);
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	default:
270*0Sstevel@tonic-gate 		(void) wait(&status);
271*0Sstevel@tonic-gate 		if ((status != 0) || (chmod(dirname, mode) == -1))
272*0Sstevel@tonic-gate 			return (-1);
273*0Sstevel@tonic-gate 		return (0);
274*0Sstevel@tonic-gate 	}
275*0Sstevel@tonic-gate }
276*0Sstevel@tonic-gate #endif
277