xref: /illumos-gate/usr/src/cmd/tic/tic_main.c (revision f86c22fe)
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 /*
23d2117003Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24d2117003Sdp  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate *************************************************************************
427c478bd9Sstevel@tonic-gate *			COPYRIGHT NOTICE				*
437c478bd9Sstevel@tonic-gate *************************************************************************
447c478bd9Sstevel@tonic-gate *	This software is copyright(C) 1982 by Pavel Curtis		*
457c478bd9Sstevel@tonic-gate *									*
467c478bd9Sstevel@tonic-gate *	Permission is granted to reproduce and distribute		*
477c478bd9Sstevel@tonic-gate *	this file by any means so long as no fee is charged		*
487c478bd9Sstevel@tonic-gate *	above a nominal handling fee and so long as this		*
497c478bd9Sstevel@tonic-gate *	notice is always included in the copies.			*
507c478bd9Sstevel@tonic-gate *									*
517c478bd9Sstevel@tonic-gate *	Other rights are reserved except as explicitly granted		*
527c478bd9Sstevel@tonic-gate *	by written permission of the author.				*
537c478bd9Sstevel@tonic-gate *		Pavel Curtis						*
547c478bd9Sstevel@tonic-gate *		Computer Science Dept.					*
557c478bd9Sstevel@tonic-gate *		405 Upson Halli						*
567c478bd9Sstevel@tonic-gate *		Cornell Universityi					*
577c478bd9Sstevel@tonic-gate *		Ithaca, NY 14853					*
587c478bd9Sstevel@tonic-gate *									*
597c478bd9Sstevel@tonic-gate *		Ph- (607) 256-4934					*
607c478bd9Sstevel@tonic-gate *									*
617c478bd9Sstevel@tonic-gate *		Pavel.Cornell@Udel-Relay(ARPAnet)			*
627c478bd9Sstevel@tonic-gate *		decvax!cornell!pavel(UUCPnet)				*
637c478bd9Sstevel@tonic-gate *********************************************************************** */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  *	comp_main.c --- Main program for terminfo compiler
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  *  $Log:	RCS/comp_main.v $
697c478bd9Sstevel@tonic-gate  * Revision 2.1  82/10/25  14:45:37  pavel
707c478bd9Sstevel@tonic-gate  * Added Copyright Notice
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  * Revision 2.0  82/10/24  15:16:37  pavel
737c478bd9Sstevel@tonic-gate  * Beta-one Test Release
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  * Revision 1.3  82/08/23  22:29:36  pavel
767c478bd9Sstevel@tonic-gate  * The REAL Alpha-one Release Version
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  * Revision 1.2  82/08/19  19:09:49  pavel
797c478bd9Sstevel@tonic-gate  * Alpha Test Release One
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  * Revision 1.1  82/08/12  18:36:55  pavel
827c478bd9Sstevel@tonic-gate  * Initial revision
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define	EXTERN		/* EXTERN=extern in other .c files */
897c478bd9Sstevel@tonic-gate #include <sys/types.h>
907c478bd9Sstevel@tonic-gate #include <sys/stat.h>
917c478bd9Sstevel@tonic-gate #include <stdlib.h>
927c478bd9Sstevel@tonic-gate #include <unistd.h>
937c478bd9Sstevel@tonic-gate #include "compiler.h"
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate char	*source_file = "./terminfo.src";
967c478bd9Sstevel@tonic-gate char	*destination = SRCDIR;
977c478bd9Sstevel@tonic-gate char	*usage_string = "[-v[n]] [-c] source-file\n";
987c478bd9Sstevel@tonic-gate char	check_only = 0;
997c478bd9Sstevel@tonic-gate char	*progname;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate extern void make_hash_table();	/* should be in a header file :-( */
1027c478bd9Sstevel@tonic-gate extern void compile();		/* should be in a header file :-( */
1037c478bd9Sstevel@tonic-gate extern void syserr_abort();		/* should be in a header file :-( */
1047c478bd9Sstevel@tonic-gate static void init();
1057c478bd9Sstevel@tonic-gate 
106d2117003Sdp int
main(int argc,char * argv[])1077c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	int	i;
1107c478bd9Sstevel@tonic-gate 	int	argflag = FALSE;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	debug_level = 0;
1137c478bd9Sstevel@tonic-gate 	progname = argv[0];
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	umask(022);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
1187c478bd9Sstevel@tonic-gate 	    if (argv[i][0] == '-') {
1197c478bd9Sstevel@tonic-gate 		switch (argv[i][1]) {
1207c478bd9Sstevel@tonic-gate 		    case 'c':
1217c478bd9Sstevel@tonic-gate 			check_only = 1;
1227c478bd9Sstevel@tonic-gate 			break;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		    case 'v':
1257c478bd9Sstevel@tonic-gate 			debug_level = argv[i][2]  ?  atoi(&argv[i][2])  :  1;
1267c478bd9Sstevel@tonic-gate 			break;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		    default:
1297c478bd9Sstevel@tonic-gate 			fprintf(stderr,
1307c478bd9Sstevel@tonic-gate 			    "%s: Unknown option. Usage is:\n\t%s: %s\n",
1317c478bd9Sstevel@tonic-gate 			    argv[0], progname, usage_string);
1327c478bd9Sstevel@tonic-gate 				exit(1);
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 	    } else if (argflag) {
1357c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Too many file names.  Usage is:\n\t%s\n",
1367c478bd9Sstevel@tonic-gate 		    argv[0], usage_string);
1377c478bd9Sstevel@tonic-gate 			exit(1);
1387c478bd9Sstevel@tonic-gate 	    } else {
1397c478bd9Sstevel@tonic-gate 		argflag = TRUE;
1407c478bd9Sstevel@tonic-gate 		source_file = argv[i];
1417c478bd9Sstevel@tonic-gate 	    }
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	init();
1457c478bd9Sstevel@tonic-gate 	make_hash_table();
1467c478bd9Sstevel@tonic-gate 	compile();
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	exit(0);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	return(0);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  *	init()
1557c478bd9Sstevel@tonic-gate  *
1567c478bd9Sstevel@tonic-gate  *	Miscellaneous initializations
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  *	Open source file as standard input
1597c478bd9Sstevel@tonic-gate  *	Change directory to working terminfo directory.
1607c478bd9Sstevel@tonic-gate  *
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static void
init()1647c478bd9Sstevel@tonic-gate init()
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	char		*env = getenv("TERMINFO");
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	start_time = time((time_t *) 0);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	curr_line = 0;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	if (freopen(source_file, "r", stdin) == NULL) {
1737c478bd9Sstevel@tonic-gate 	    fprintf(stderr, "%s: Can't open %s\n", progname, source_file);
1747c478bd9Sstevel@tonic-gate 	    exit(1);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (env && *env)
1787c478bd9Sstevel@tonic-gate 	    destination = env;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (check_only) {
1817c478bd9Sstevel@tonic-gate 		DEBUG(1, "Would be working in %s\n", destination);
1827c478bd9Sstevel@tonic-gate 	} else {
1837c478bd9Sstevel@tonic-gate 		DEBUG(1, "Working in %s\n", destination);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (access(destination, 7) < 0) {
1877c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: %s nonexistent or permission denied\n",
1887c478bd9Sstevel@tonic-gate 		    progname, destination);
1897c478bd9Sstevel@tonic-gate 		exit(1);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (chdir(destination) < 0) {
1937c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: %s is not a directory\n",
1947c478bd9Sstevel@tonic-gate 		    progname, destination);
1957c478bd9Sstevel@tonic-gate 		exit(1);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate  *
2027c478bd9Sstevel@tonic-gate  *	check_dir(dirletter)
2037c478bd9Sstevel@tonic-gate  *
2047c478bd9Sstevel@tonic-gate  *	Check for access rights to the destination directory.
2057c478bd9Sstevel@tonic-gate  *	Create any directories which don't exist.
2067c478bd9Sstevel@tonic-gate  *
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate void
check_dir(char dirletter)2107c478bd9Sstevel@tonic-gate check_dir(char dirletter)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	struct stat64	statbuf;
2137c478bd9Sstevel@tonic-gate 	static char	dirnames[128];
2147c478bd9Sstevel@tonic-gate 	static char	dir[2] = " ";
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (dirnames[dirletter] == 0) {
2177c478bd9Sstevel@tonic-gate 	    dir[0] = dirletter;
2187c478bd9Sstevel@tonic-gate 	    if (stat64(dir, &statbuf) < 0) {
2197c478bd9Sstevel@tonic-gate 		if (mkdir(dir, 0755) < 0)
2207c478bd9Sstevel@tonic-gate 			syserr_abort("mkdir %s returned bad status", dir);
221*f86c22feSToomas Soome 		dirnames[dirletter] = 1;
2227c478bd9Sstevel@tonic-gate 	    } else if (access(dir, 7) < 0) {
2237c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: %s/%s: Permission denied\n",
2247c478bd9Sstevel@tonic-gate 			    progname, destination, dir);
2257c478bd9Sstevel@tonic-gate 			perror(dir);
2267c478bd9Sstevel@tonic-gate 			exit(1);
2277c478bd9Sstevel@tonic-gate 	    } else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
2287c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: %s/%s: Not a directory\n",
2297c478bd9Sstevel@tonic-gate 			    progname, destination, dir);
2307c478bd9Sstevel@tonic-gate 			perror(dir);
2317c478bd9Sstevel@tonic-gate 			exit(1);
2327c478bd9Sstevel@tonic-gate 	    }
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	return;
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate #include <curses.h>
2387c478bd9Sstevel@tonic-gate #if (defined(SYSV) || defined(USG)) && !defined(SIGPOLL)
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate  *	mkdir(dirname, mode)
2417c478bd9Sstevel@tonic-gate  *
2427c478bd9Sstevel@tonic-gate  *	forks and execs the mkdir program to create the given directory
2437c478bd9Sstevel@tonic-gate  *
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate 
mkdir(dirname,mode)2467c478bd9Sstevel@tonic-gate mkdir(dirname, mode)
2477c478bd9Sstevel@tonic-gate #ifdef __STDC__
2487c478bd9Sstevel@tonic-gate const
2497c478bd9Sstevel@tonic-gate #endif
2507c478bd9Sstevel@tonic-gate char	*dirname;
2517c478bd9Sstevel@tonic-gate int mode;
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate     int	fork_rtn;
2547c478bd9Sstevel@tonic-gate     int	status;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate     fork_rtn = fork();
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate     switch (fork_rtn) {
2597c478bd9Sstevel@tonic-gate 	case 0:		/* Child */
2607c478bd9Sstevel@tonic-gate 		(void) execl("/bin/mkdir", "mkdir", dirname, (char *)0);
2617c478bd9Sstevel@tonic-gate 		_exit(1);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	case -1:	/* Error */
2647c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: SYSTEM ERROR!! Fork failed!!!\n",
2657c478bd9Sstevel@tonic-gate 		    progname);
2667c478bd9Sstevel@tonic-gate 		exit(1);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	default:
2697c478bd9Sstevel@tonic-gate 		(void) wait(&status);
2707c478bd9Sstevel@tonic-gate 		if ((status != 0) || (chmod(dirname, mode) == -1))
2717c478bd9Sstevel@tonic-gate 			return (-1);
2727c478bd9Sstevel@tonic-gate 		return (0);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate #endif
276