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