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