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