xref: /titanic_51/usr/src/lib/libxcurses/src/tic/ticmain.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  *	ticmain.c
31  *
32  *	Terminal Information Compiler
33  *
34  *	Copyright 1990, 1992 by Mortice Kern Systems Inc.  All rights reserved.
35  *
36  *	Portions of this code Copyright 1982 by Pavel Curtis.
37  *
38  */
39 
40 #ifdef M_RCSID
41 #ifndef lint
42 static char const rcsID[] = "$Header: /rd/src/tic/rcs/ticmain.c 1.11 1995/06/22 18:40:30 ross Exp $";
43 #endif
44 #endif
45 
46 #include "tic.h"
47 #include <ctype.h>
48 #include <sys/stat.h>
49 
50 int curr_line;
51 int check_only = 0;
52 int debug_level = 0;
53 time_t start_time;
54 char *source_file = M_TERMINFO_DIR;
55 char *destination = M_TERMINFO_DIR;
56 
57 char boolean[BOOLCOUNT];
58 short number[NUMCOUNT];
59 short string[STRCOUNT];
60 
61 static char const usage[] = m_textstr(3103, "usage:  %s [-v[n]] [-c] <file>\n", "U _");
62 static char const src_err[] = m_textstr(3104, "terminfo definition file \"%s\" ", "E filename");
63 static char const dst_err[] = m_textstr(3105, "terminfo database \"%s\" ", "E filename");
64 static char const dstdir_err[] = m_textstr(3106, "terminfo database directory \"%s/%s\" ", "E pathname");
65 
66 static void init(void);
67 
68 int
69 main(int argc, char **argv)
70 {
71 	char *ap;
72 	setlocale(LC_ALL, "");
73 	_cmdname = m_cmdname(*argv);
74 	for (--argc, ++argv; 0 < argc && **argv == '-'; --argc, ++argv) {
75 		ap = &argv[0][1];
76 		if (*ap == '-' && ap[1] == '\0') {
77 			--argc;
78 			++argv;
79 			break;
80 		}
81 		while (*ap != '\0') {
82 			switch (*ap++) {
83 			case 'c':
84 				check_only = 1;
85 				continue;
86 			case 'v':
87 				debug_level = 1;
88 				if (isdigit(*ap))
89 					debug_level = (int) strtol(ap, &ap, 0);
90 				break;
91 			default:
92 				(void) fprintf(stderr, m_strmsg(usage), _cmdname);
93 				return (USAGE);
94 			}
95 			break;
96 		}
97 	}
98 	/* There must be only one source file. */
99 	if (argc != 1) {
100 		(void) fprintf(stderr, m_strmsg(usage), _cmdname);
101 		return (USAGE);
102 	}
103 	source_file = *argv;
104 	init();
105 	compile();
106 	if (warnings > 0) {
107 		return 1;
108 	}
109 	return (0);
110 }
111 
112 /*f
113  *	Miscellaneous initializations
114  *
115  *	Open source file as standard input
116  *	Check for access rights to destination directories
117  *	Create any directories which don't exist.
118  */
119 static void
120 init(void)
121 {
122 	char *s;
123 	char const *p;
124 	char dir[2];
125 	struct stat statbuf;
126 	static char const dirnames[] = "abcdefghijklmnopqrstuvwxyz0123456789";
127 
128 	curr_line = 0;
129 	start_time = time(NULL);
130 	if (freopen(source_file, "r", stdin) == NULL) {
131 		(void) eprintf(m_strmsg(src_err), source_file);
132 		exit(ERROR);
133 	}
134 	if ((s = getenv("TERMINFO")) != NULL)
135 		destination = s;
136 	if (access(destination, 7) < 0) {
137 		(void) eprintf(m_strmsg(dst_err), destination);
138 		exit(ERROR);
139 	}
140 	if (chdir(destination) < 0) {
141 		(void) eprintf(m_strmsg(dst_err), destination);
142 		exit(ERROR);
143 	}
144 	dir[1] = '\0';
145 	for (p = dirnames; *p != '\0'; ++p) {
146 		*dir = *p;
147 		if (stat(dir, &statbuf) < 0) {
148 			(void) mkdir(dir, M_DIRMODE);
149 		} else if (access(dir, 7) < 0) {
150 			(void) eprintf(m_strmsg(dstdir_err), destination, dir);
151 			exit(1);
152 		} else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
153 			(void) eprintf(m_strmsg(dstdir_err), destination, dir);
154 			exit(1);
155 		}
156 	}
157 }
158 
159 /*f
160  *	Reset boolean[], number[], and string[].
161  */
162 void
163 reset(void)
164 {
165 	int i;
166 	for (i = 0; i < BOOLCOUNT; ++i)
167 		boolean[i] = 0;
168 	for (i = 0; i < NUMCOUNT; ++i)
169 		number[i] = -1;
170 	for (i = 0; i < STRCOUNT; ++i)
171 		string[i] = -1;
172 }
173 
174 /*f
175  *	Return a linear index value.
176  *
177  *	Search in the following order boolnames[], numnames[], and strnames[]
178  *	for the matching capability name.  Then map the array and index into
179  *	a linear index.  Return -1 if capname is not valid.
180  *
181  *	While this linear approach is slow, TIC is seldom used once the
182  *	database is created, therefore we don't bother spending extra
183  *	effort to speed it up.
184  */
185 int
186 find(char const *capname, void **arrayp, int *indexp)
187 {
188 	char **p;
189 	for (p = boolnames; *p != NULL; ++p)
190 		if (strcmp(*p, capname) == 0) {
191 			*arrayp = (void*) boolean;
192 			*indexp = (int)(p - boolnames);
193 			return (0);
194 		}
195 	for (p = numnames; *p != NULL; ++p)
196 		if (strcmp(*p, capname) == 0) {
197 			*arrayp = (void*) number;
198 			*indexp = (int)(p - numnames);
199 			return (0);
200 		}
201 	for (p = strnames; *p != NULL; ++p)
202 		if (strcmp(*p, capname) == 0) {
203 			*arrayp = (void*) string;
204 			*indexp = (int)(p - strnames);
205 			return (0);
206 		}
207 	return (-1);
208 }
209