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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <libgen.h>
34 #include <ctype.h>
35 #include <limits.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <dirent.h>
39 #include <errno.h>
40 #include "parser.h"
41 #include "errlog.h"
42 
43 #define	SPEC_PARSER_VERSION "2.1"
44 
45 char **filelist;
46 
47 static char *prog;
48 
49 static void usage(void);
50 
51 int
52 main(int argc, char **argv)
53 {
54 	int c, retval, size = 0;
55 	int lflag = 0,
56 	    iflag = 0,
57 	    aflag = 0,
58 	    vflag = 0;
59 	char *tmpptr;
60 
61 	Translator_info T_info;
62 
63 	prog = basename(argv[0]);
64 
65 	T_info.ti_verbosity = 0;
66 	T_info.ti_flags = 0;
67 	T_info.ti_dash_I = NULL;
68 	T_info.ti_output_file = NULL;
69 	T_info.ti_libtype = NORMALLIB;
70 	T_info.ti_versfile = "version";
71 
72 	while ((c = getopt(argc, argv, "FVpd:v:l:o:I:a:")) != EOF) {
73 		switch (c) {
74 		case 'F':
75 			/* Library is a filter */
76 			T_info.ti_libtype = FILTERLIB;
77 			break;
78 		case 'a':
79 			/* set the target architecture */
80 			if ((T_info.ti_archtoken = arch_strtoi(optarg)) == 0) {
81 				errlog(ERROR,
82 				    "Error: architecture specified must "
83 				    "be one of: i386, sparc, sparcv9, "
84 				    "ia64 or amd64\n");
85 				usage();
86 			}
87 			T_info.ti_arch = optarg;
88 			++aflag;
89 			break;
90 		case 'V':
91 			/* print the version info */
92 			(void) fprintf(stderr,
93 			    "%s Version %s\n", prog, SPEC_PARSER_VERSION);
94 			return (0);
95 			break;
96 		case 'd':
97 			/* set debugging level */
98 			if (!isdigit(*optarg) ||
99 			    (T_info.ti_verbosity = atoi(optarg)) < 0) {
100 				errlog(ERROR,
101 				    "Error: -d option must be given a "
102 				    "positive integer argument\n");
103 				usage();
104 			}
105 			break;
106 		case 'l':
107 			/* set library name */
108 			++lflag;
109 			if (!isalnum(optarg[0])) {
110 				errlog(ERROR,
111 				    "Error: -l must be given the name of "
112 				    "a library as an argument\n");
113 				usage();
114 			}
115 			T_info.ti_liblist = optarg;
116 			break;
117 		case 'I':
118 			/* set path to spec files */
119 			++iflag;
120 			if (iflag == 1) {
121 				size = 1;
122 			} else {
123 				(void) strcat(T_info.ti_dash_I, ":");
124 				size = strlen(T_info.ti_dash_I);
125 			}
126 			tmpptr = realloc(T_info.ti_dash_I,
127 			    sizeof (char) * (size + strlen(optarg) + 3));
128 			if (tmpptr == NULL) {
129 				errlog(ERROR | FATAL,
130 				    "Error: Unable to allocate memory "
131 				    "for command line arguments\n");
132 			}
133 			T_info.ti_dash_I = tmpptr;
134 			if (iflag == 1) {
135 				(void) strcpy(T_info.ti_dash_I, optarg);
136 			} else {
137 				(void) strcat(T_info.ti_dash_I, optarg);
138 			}
139 			break;
140 		case 'v':
141 			/* set version filename */
142 			if (vflag != 0) {
143 				errlog(ERROR, "Error: Multiple -v options "
144 				    "in command line\n");
145 				usage();
146 			}
147 			T_info.ti_versfile = optarg;
148 			++vflag;
149 			break;
150 		case 'o':
151 			/* set name of output file */
152 			T_info.ti_output_file = optarg;
153 			break;
154 		case 'p':
155 			/* set picky flag */
156 			T_info.ti_flags = T_info.ti_flags | XLATOR_PICKY_FLAG;
157 			break;
158 		case '?':
159 		default:
160 			usage();
161 		}
162 	}
163 
164 	if (lflag == 0) {
165 		errlog(ERROR,
166 		    "Error: -l library argument must be specified\n");
167 		usage();
168 	}
169 	if (aflag == 0) {
170 		errlog(ERROR, "Error: -a i386|sparc|sparcv9|ia64|amd64 "
171 			"argument must be specified\n");
172 		usage();
173 	}
174 
175 	if (optind < argc) {
176 		filelist = &argv[optind];
177 	} else {
178 		filelist = NULL;
179 		errlog(ERROR, "Error: Must specify at least one spec "
180 			"file to process\n");
181 		usage();
182 	}
183 
184 	T_info.ti_nfiles = argc-optind;
185 	seterrseverity(T_info.ti_verbosity);
186 
187 	if (T_info.ti_dash_I == NULL) {
188 		T_info.ti_dash_I = ".";
189 	} else {
190 		(void) strcat(T_info.ti_dash_I, ":.");
191 	}
192 
193 	errlog(STATUS, "using %s for spec path\n", T_info.ti_dash_I);
194 
195 	if ((retval = frontend(&T_info)) != 0) {
196 		errlog(ERROR, "%d Error(s) occurred\n", retval);
197 		return (1);
198 	}
199 	return (0);
200 }
201 
202 /*
203  * usage()
204  * prints the usage string and exits
205  */
206 static void
207 usage(void)
208 {
209 	(void) fprintf(stderr, "Usage:\n\t%s [-d n] [-V] [ -v version_file] "
210 	    "-a i386|sparc|sparcv9|ia64|amd64 -l lib [-I path_to_spec ] "
211 	    "[-o outfile ] [-p] [-F] file.spec [ ... ]\n"
212 	    "Command line arguments:\n"
213 	    "  -d n             n is an integer specifying "
214 	    "the level of verbosity\n"
215 	    "  -V               Print the Version info\n"
216 	    "  -a arch          Target cpu architecture. "
217 	    "Must be one of\n"
218 	    "                   i386, sparc, sparcv9, ia64 or amd64\n"
219 	    "  -v version_file  Name of version file\n"
220 	    "  -o file          Name of output file\n"
221 	    "                   this option can only be used when "
222 	    "processing single\n                   spec files.  "
223 	    "Using this with multiple source .spec\n"
224 	    "                   filenames will cause "
225 	    "unpredictable results\n"
226 	    "  -l lib           library to process\n"
227 	    "  -I path_to_spec  path to spec files\n"
228 	    "  -p               be picky with interface versioning\n"
229 	    "  -F               library is a filter library\n", prog);
230 	exit(1);
231 }
232