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