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