summaryrefslogtreecommitdiff
path: root/usr/src/cmd/abi/spectrans/parser/main.c
blob: 1e3046ddc692a32903aa653a9626cf7420e53ae4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <libgen.h>
#include <ctype.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include "parser.h"
#include "errlog.h"

#define	SPEC_PARSER_VERSION "2.1"

char **filelist;

static char *prog;

static void usage(void);

int
main(int argc, char **argv)
{
	int c, retval, size = 0;
	int lflag = 0,
	    iflag = 0,
	    aflag = 0,
	    vflag = 0;
	char *tmpptr;

	Translator_info T_info;

	prog = basename(argv[0]);

	T_info.ti_verbosity = 0;
	T_info.ti_flags = 0;
	T_info.ti_dash_I = NULL;
	T_info.ti_output_file = NULL;
	T_info.ti_libtype = NORMALLIB;
	T_info.ti_versfile = "version";

	while ((c = getopt(argc, argv, "FVpd:v:l:o:I:a:")) != EOF) {
		switch (c) {
		case 'F':
			/* Library is a filter */
			T_info.ti_libtype = FILTERLIB;
			break;
		case 'a':
			/* set the target architecture */
			if ((T_info.ti_archtoken = arch_strtoi(optarg)) == 0) {
				errlog(ERROR,
				    "Error: architecture specified must "
				    "be one of: i386, sparc, sparcv9, "
				    "ia64 or amd64\n");
				usage();
			}
			T_info.ti_arch = optarg;
			++aflag;
			break;
		case 'V':
			/* print the version info */
			(void) fprintf(stderr,
			    "%s Version %s\n", prog, SPEC_PARSER_VERSION);
			return (0);
			break;
		case 'd':
			/* set debugging level */
			if (!isdigit(*optarg) ||
			    (T_info.ti_verbosity = atoi(optarg)) < 0) {
				errlog(ERROR,
				    "Error: -d option must be given a "
				    "positive integer argument\n");
				usage();
			}
			break;
		case 'l':
			/* set library name */
			++lflag;
			if (!isalnum(optarg[0])) {
				errlog(ERROR,
				    "Error: -l must be given the name of "
				    "a library as an argument\n");
				usage();
			}
			T_info.ti_liblist = optarg;
			break;
		case 'I':
			/* set path to spec files */
			++iflag;
			if (iflag == 1) {
				size = 1;
			} else {
				(void) strcat(T_info.ti_dash_I, ":");
				size = strlen(T_info.ti_dash_I);
			}
			tmpptr = realloc(T_info.ti_dash_I,
			    sizeof (char) * (size + strlen(optarg) + 3));
			if (tmpptr == NULL) {
				errlog(ERROR | FATAL,
				    "Error: Unable to allocate memory "
				    "for command line arguments\n");
			}
			T_info.ti_dash_I = tmpptr;
			if (iflag == 1) {
				(void) strcpy(T_info.ti_dash_I, optarg);
			} else {
				(void) strcat(T_info.ti_dash_I, optarg);
			}
			break;
		case 'v':
			/* set version filename */
			if (vflag != 0) {
				errlog(ERROR, "Error: Multiple -v options "
				    "in command line\n");
				usage();
			}
			T_info.ti_versfile = optarg;
			++vflag;
			break;
		case 'o':
			/* set name of output file */
			T_info.ti_output_file = optarg;
			break;
		case 'p':
			/* set picky flag */
			T_info.ti_flags = T_info.ti_flags | XLATOR_PICKY_FLAG;
			break;
		case '?':
		default:
			usage();
		}
	}

	if (lflag == 0) {
		errlog(ERROR,
		    "Error: -l library argument must be specified\n");
		usage();
	}
	if (aflag == 0) {
		errlog(ERROR, "Error: -a i386|sparc|sparcv9|ia64|amd64 "
			"argument must be specified\n");
		usage();
	}

	if (optind < argc) {
		filelist = &argv[optind];
	} else {
		filelist = NULL;
		errlog(ERROR, "Error: Must specify at least one spec "
			"file to process\n");
		usage();
	}

	T_info.ti_nfiles = argc-optind;
	seterrseverity(T_info.ti_verbosity);

	if (T_info.ti_dash_I == NULL) {
		T_info.ti_dash_I = ".";
	} else {
		(void) strcat(T_info.ti_dash_I, ":.");
	}

	errlog(STATUS, "using %s for spec path\n", T_info.ti_dash_I);

	if ((retval = frontend(&T_info)) != 0) {
		errlog(ERROR, "%d Error(s) occurred\n", retval);
		return (1);
	}
	return (0);
}

/*
 * usage()
 * prints the usage string and exits
 */
static void
usage(void)
{
	(void) fprintf(stderr, "Usage:\n\t%s [-d n] [-V] [ -v version_file] "
	    "-a i386|sparc|sparcv9|ia64|amd64 -l lib [-I path_to_spec ] "
	    "[-o outfile ] [-p] [-F] file.spec [ ... ]\n"
	    "Command line arguments:\n"
	    "  -d n             n is an integer specifying "
	    "the level of verbosity\n"
	    "  -V               Print the Version info\n"
	    "  -a arch          Target cpu architecture. "
	    "Must be one of\n"
	    "                   i386, sparc, sparcv9, ia64 or amd64\n"
	    "  -v version_file  Name of version file\n"
	    "  -o file          Name of output file\n"
	    "                   this option can only be used when "
	    "processing single\n                   spec files.  "
	    "Using this with multiple source .spec\n"
	    "                   filenames will cause "
	    "unpredictable results\n"
	    "  -l lib           library to process\n"
	    "  -I path_to_spec  path to spec files\n"
	    "  -p               be picky with interface versioning\n"
	    "  -F               library is a filter library\n", prog);
	exit(1);
}