1b528cefcSMark Murray /*
2c19800e8SDoug Rabson * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray * All rights reserved.
5b528cefcSMark Murray *
6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray * modification, are permitted provided that the following conditions
8b528cefcSMark Murray * are met:
9b528cefcSMark Murray *
10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray *
13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray * documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray *
17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray * may be used to endorse or promote products derived from this software
19b528cefcSMark Murray * without specific prior written permission.
20b528cefcSMark Murray *
21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray * SUCH DAMAGE.
32b528cefcSMark Murray */
33b528cefcSMark Murray
34b528cefcSMark Murray #include "gen_locl.h"
35b528cefcSMark Murray #include <getarg.h>
36c19800e8SDoug Rabson #include "lex.h"
37b528cefcSMark Murray
38c19800e8SDoug Rabson RCSID("$Id$");
39b528cefcSMark Murray
40b528cefcSMark Murray extern FILE *yyin;
41b528cefcSMark Murray
42c19800e8SDoug Rabson static getarg_strings preserve;
43c19800e8SDoug Rabson static getarg_strings seq;
44c19800e8SDoug Rabson
45c19800e8SDoug Rabson int
preserve_type(const char * p)46c19800e8SDoug Rabson preserve_type(const char *p)
47c19800e8SDoug Rabson {
48c19800e8SDoug Rabson int i;
49c19800e8SDoug Rabson for (i = 0; i < preserve.num_strings; i++)
50c19800e8SDoug Rabson if (strcmp(preserve.strings[i], p) == 0)
51c19800e8SDoug Rabson return 1;
52c19800e8SDoug Rabson return 0;
53c19800e8SDoug Rabson }
54c19800e8SDoug Rabson
55c19800e8SDoug Rabson int
seq_type(const char * p)56c19800e8SDoug Rabson seq_type(const char *p)
57c19800e8SDoug Rabson {
58c19800e8SDoug Rabson int i;
59c19800e8SDoug Rabson for (i = 0; i < seq.num_strings; i++)
60c19800e8SDoug Rabson if (strcmp(seq.strings[i], p) == 0)
61c19800e8SDoug Rabson return 1;
62c19800e8SDoug Rabson return 0;
63c19800e8SDoug Rabson }
64c19800e8SDoug Rabson
65c19800e8SDoug Rabson int support_ber;
66c19800e8SDoug Rabson int template_flag;
67b528cefcSMark Murray int rfc1510_bitstring;
68b528cefcSMark Murray int one_code_file;
69b528cefcSMark Murray char *option_file;
70c19800e8SDoug Rabson int version_flag;
71c19800e8SDoug Rabson int help_flag;
72c19800e8SDoug Rabson struct getargs args[] = {
73c19800e8SDoug Rabson { "template", 0, arg_flag, &template_flag },
74b528cefcSMark Murray { "encode-rfc1510-bit-string", 0, arg_flag, &rfc1510_bitstring },
75b528cefcSMark Murray { "decode-dce-ber", 0, arg_flag, &support_ber },
76b528cefcSMark Murray { "support-ber", 0, arg_flag, &support_ber },
77b528cefcSMark Murray { "preserve-binary", 0, arg_strings, &preserve },
78b528cefcSMark Murray { "sequence", 0, arg_strings, &seq },
79b528cefcSMark Murray { "one-code-file", 0, arg_flag, &one_code_file },
80b528cefcSMark Murray { "option-file", 0, arg_string, &option_file },
81b528cefcSMark Murray { "version", 0, arg_flag, &version_flag },
82b528cefcSMark Murray { "help", 0, arg_flag, &help_flag }
83b528cefcSMark Murray };
84b528cefcSMark Murray int num_args = sizeof(args) / sizeof(args[0]);
85b528cefcSMark Murray
86c19800e8SDoug Rabson static void
usage(int code)87c19800e8SDoug Rabson usage(int code)
88b528cefcSMark Murray {
89b528cefcSMark Murray arg_printusage(args, num_args, NULL, "[asn1-file [name]]");
90b528cefcSMark Murray exit(code);
91b528cefcSMark Murray }
92c19800e8SDoug Rabson
93c19800e8SDoug Rabson int error_flag;
94c19800e8SDoug Rabson
95b528cefcSMark Murray int
main(int argc,char ** argv)96adb0ddaeSAssar Westerlund main(int argc, char **argv)
97c19800e8SDoug Rabson {
98b528cefcSMark Murray int ret;
99b528cefcSMark Murray const char *file;
100b528cefcSMark Murray const char *name = NULL;
101b528cefcSMark Murray int optidx = 0;
102b528cefcSMark Murray char **arg = NULL;
103b528cefcSMark Murray size_t len = 0, i;
104b528cefcSMark Murray
105c19800e8SDoug Rabson setprogname(argv[0]);
106b528cefcSMark Murray if(getarg(args, num_args, argc, argv, &optidx))
107b528cefcSMark Murray usage(1);
108b528cefcSMark Murray if(help_flag)
109b528cefcSMark Murray usage(0);
110c19800e8SDoug Rabson if(version_flag) {
111b528cefcSMark Murray print_version(NULL);
112b528cefcSMark Murray exit(0);
113b528cefcSMark Murray }
114c19800e8SDoug Rabson if (argc == optidx) {
115c19800e8SDoug Rabson file = "stdin";
116c19800e8SDoug Rabson name = "stdin";
117c19800e8SDoug Rabson yyin = stdin;
118c19800e8SDoug Rabson } else {
119c19800e8SDoug Rabson file = argv[optidx];
120c19800e8SDoug Rabson yyin = fopen (file, "r");
121c19800e8SDoug Rabson if (yyin == NULL)
122b528cefcSMark Murray err (1, "open %s", file);
123b528cefcSMark Murray if (argc == optidx + 1) {
124b528cefcSMark Murray char *p;
125b528cefcSMark Murray name = estrdup(file);
126b528cefcSMark Murray p = strrchr(name, '.');
127c19800e8SDoug Rabson if (p)
128c19800e8SDoug Rabson *p = '\0';
129b528cefcSMark Murray } else
130c19800e8SDoug Rabson name = argv[optidx + 1];
131c19800e8SDoug Rabson }
132c19800e8SDoug Rabson
133b528cefcSMark Murray /*
134 * Parse extra options file
135 */
136 if (option_file) {
137 char buf[1024];
138 FILE *opt;
139
140 opt = fopen(option_file, "r");
141 if (opt == NULL) {
142 perror("open");
143 exit(1);
144 }
145
146 arg = calloc(2, sizeof(arg[0]));
147 if (arg == NULL) {
148 perror("calloc");
149 exit(1);
150 }
151 arg[0] = option_file;
152 arg[1] = NULL;
153 len = 1;
154
155 while (fgets(buf, sizeof(buf), opt) != NULL) {
156 buf[strcspn(buf, "\n\r")] = '\0';
157
158 arg = realloc(arg, (len + 2) * sizeof(arg[0]));
159 if (arg == NULL) {
160 perror("malloc");
161 exit(1);
162 }
163 arg[len] = strdup(buf);
164 if (arg[len] == NULL) {
165 perror("strdup");
166 exit(1);
167 }
168 arg[len + 1] = NULL;
169 len++;
170 }
171 fclose(opt);
172
173 optidx = 0;
174 if(getarg(args, num_args, len, arg, &optidx))
175 usage(1);
176
177 if (len != optidx) {
178 fprintf(stderr, "extra args");
179 exit(1);
180 }
181 }
182
183
184 init_generate (file, name);
185
186 if (one_code_file)
187 generate_header_of_codefile(name);
188
189 initsym ();
190 ret = yyparse ();
191 if(ret != 0 || error_flag != 0)
192 exit(1);
193 close_generate ();
194 if (argc != optidx)
195 fclose(yyin);
196
197 if (one_code_file)
198 close_codefile();
199
200 if (arg) {
201 for (i = 1; i < len; i++)
202 free(arg[i]);
203 free(arg);
204 }
205
206 return 0;
207 }
208