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