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