xref: /illumos-gate/usr/src/cmd/tic/tic_main.c (revision d2117003)
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 #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 int
109 main(int argc, char *argv[])
110 {
111 	int	i;
112 	int	argflag = FALSE;
113 
114 	debug_level = 0;
115 	progname = argv[0];
116 
117 	umask(022);
118 
119 	for (i = 1; i < argc; i++) {
120 	    if (argv[i][0] == '-') {
121 		switch (argv[i][1]) {
122 		    case 'c':
123 			check_only = 1;
124 			break;
125 
126 		    case 'v':
127 			debug_level = argv[i][2]  ?  atoi(&argv[i][2])  :  1;
128 			break;
129 
130 		    default:
131 			fprintf(stderr,
132 			    "%s: Unknown option. Usage is:\n\t%s: %s\n",
133 			    argv[0], progname, usage_string);
134 				exit(1);
135 		}
136 	    } else if (argflag) {
137 		fprintf(stderr, "%s: Too many file names.  Usage is:\n\t%s\n",
138 		    argv[0], usage_string);
139 			exit(1);
140 	    } else {
141 		argflag = TRUE;
142 		source_file = argv[i];
143 	    }
144 	}
145 
146 	init();
147 	make_hash_table();
148 	compile();
149 
150 	exit(0);
151 
152 	return(0);
153 }
154 
155 /*
156  *	init()
157  *
158  *	Miscellaneous initializations
159  *
160  *	Open source file as standard input
161  *	Change directory to working terminfo directory.
162  *
163  */
164 
165 static void
166 init()
167 {
168 	char		*env = getenv("TERMINFO");
169 
170 	start_time = time((time_t *) 0);
171 
172 	curr_line = 0;
173 
174 	if (freopen(source_file, "r", stdin) == NULL) {
175 	    fprintf(stderr, "%s: Can't open %s\n", progname, source_file);
176 	    exit(1);
177 	}
178 
179 	if (env && *env)
180 	    destination = env;
181 
182 	if (check_only) {
183 		DEBUG(1, "Would be working in %s\n", destination);
184 	} else {
185 		DEBUG(1, "Working in %s\n", destination);
186 	}
187 
188 	if (access(destination, 7) < 0) {
189 		fprintf(stderr, "%s: %s nonexistent or permission denied\n",
190 		    progname, destination);
191 		exit(1);
192 	}
193 
194 	if (chdir(destination) < 0) {
195 		fprintf(stderr, "%s: %s is not a directory\n",
196 		    progname, destination);
197 		exit(1);
198 	}
199 
200 }
201 
202 /*
203  *
204  *	check_dir(dirletter)
205  *
206  *	Check for access rights to the destination directory.
207  *	Create any directories which don't exist.
208  *
209  */
210 
211 void
212 check_dir(char dirletter)
213 {
214 	struct stat64	statbuf;
215 	static char	dirnames[128];
216 	static char	dir[2] = " ";
217 
218 	if (dirnames[dirletter] == 0) {
219 	    dir[0] = dirletter;
220 	    if (stat64(dir, &statbuf) < 0) {
221 		if (mkdir(dir, 0755) < 0)
222 			syserr_abort("mkdir %s returned bad status", dir);
223 			dirnames[dirletter] = 1;
224 	    } else if (access(dir, 7) < 0) {
225 			fprintf(stderr, "%s: %s/%s: Permission denied\n",
226 			    progname, destination, dir);
227 			perror(dir);
228 			exit(1);
229 	    } else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
230 			fprintf(stderr, "%s: %s/%s: Not a directory\n",
231 			    progname, destination, dir);
232 			perror(dir);
233 			exit(1);
234 	    }
235 	}
236 	return;
237 }
238 
239 #include <curses.h>
240 #if (defined(SYSV) || defined(USG)) && !defined(SIGPOLL)
241 /*
242  *	mkdir(dirname, mode)
243  *
244  *	forks and execs the mkdir program to create the given directory
245  *
246  */
247 
248 mkdir(dirname, mode)
249 #ifdef __STDC__
250 const
251 #endif
252 char	*dirname;
253 int mode;
254 {
255     int	fork_rtn;
256     int	status;
257 
258     fork_rtn = fork();
259 
260     switch (fork_rtn) {
261 	case 0:		/* Child */
262 		(void) execl("/bin/mkdir", "mkdir", dirname, (char *)0);
263 		_exit(1);
264 
265 	case -1:	/* Error */
266 		fprintf(stderr, "%s: SYSTEM ERROR!! Fork failed!!!\n",
267 		    progname);
268 		exit(1);
269 
270 	default:
271 		(void) wait(&status);
272 		if ((status != 0) || (chmod(dirname, mode) == -1))
273 			return (-1);
274 		return (0);
275 	}
276 }
277 #endif
278