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