xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/sl/slc-gram.y (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1 /*	$NetBSD: slc-gram.y,v 1.2 2017/01/28 21:31:50 christos Exp $	*/
2 
3 %{
4 /*
5  * Copyright (c) 2004-2006 Kungliga Tekniska Högskolan
6  * (Royal Institute of Technology, Stockholm, Sweden).
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * 3. Neither the name of the Institute nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <config.h>
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <err.h>
42 #include <ctype.h>
43 #include <limits.h>
44 #include <getarg.h>
45 #include <vers.h>
46 #include <roken.h>
47 
48 #include "slc.h"
49 extern FILE *yyin;
50 extern struct assignment *assignment;
51 
52 /* Declarations for Bison:
53  */
54 #define YYMALLOC        malloc
55 #define YYFREE          free
56 
57 %}
58 
59 %union {
60 	char *string;
61 	struct assignment *assignment;
62 }
63 
64 %token <string> LITERAL
65 %token <string> STRING
66 %type <assignment> assignment assignments
67 
68 %start start
69 
70 %%
71 
72 start		: assignments
73 		{
74 			assignment = $1;
75 		}
76 		;
77 
78 assignments	: assignment assignments
79 		{
80 			$1->next = $2;
81 			$$ = $1;
82 		}
83 		| assignment
84 		;
85 
86 assignment	: LITERAL '=' STRING
87 		{
88 			$$ = malloc(sizeof(*$$));
89 			$$->name = $1;
90 			$$->type = a_value;
91 			$$->lineno = lineno;
92 			$$->u.value = $3;
93 			$$->next = NULL;
94 		}
95 		| LITERAL '=' '{' assignments '}'
96 		{
97 			$$ = malloc(sizeof(*$$));
98 			$$->name = $1;
99 			$$->type = a_assignment;
100 			$$->lineno = lineno;
101 			$$->u.assignment = $4;
102 			$$->next = NULL;
103 		}
104 		;
105 
106 %%
107 char *filename;
108 FILE *cfile, *hfile;
109 int error_flag;
110 struct assignment *assignment;
111 
112 
113 static void
ex(struct assignment * a,const char * fmt,...)114 ex(struct assignment *a, const char *fmt, ...)
115 {
116     va_list ap;
117     fprintf(stderr, "%s:%d: ", a->name, a->lineno);
118     va_start(ap, fmt);
119     vfprintf(stderr, fmt, ap);
120     va_end(ap);
121     fprintf(stderr, "\n");
122 }
123 
124 
125 
126 static int
check_option(struct assignment * as)127 check_option(struct assignment *as)
128 {
129     struct assignment *a;
130     int seen_long = 0;
131     int seen_name = 0;
132     int seen_short = 0;
133     int seen_type = 0;
134     int seen_argument = 0;
135     int seen_help = 0;
136     int seen_default = 0;
137     int ret = 0;
138 
139     for(a = as; a != NULL; a = a->next) {
140 	if(strcmp(a->name, "long") == 0)
141 	    seen_long++;
142 	else if(strcmp(a->name, "short") == 0)
143 	    seen_short++;
144 	else if(strcmp(a->name, "name") == 0)
145 	    seen_name++;
146 	else if(strcmp(a->name, "type") == 0)
147 	    seen_type++;
148 	else if(strcmp(a->name, "argument") == 0)
149 	    seen_argument++;
150 	else if(strcmp(a->name, "help") == 0)
151 	    seen_help++;
152 	else if(strcmp(a->name, "default") == 0)
153 	    seen_default++;
154 	else {
155 	    ex(a, "unknown name %s", a->name);
156 	    ret++;
157 	}
158     }
159     if(seen_long == 0 && seen_short == 0) {
160 	ex(as, "neither long nor short option");
161 	ret++;
162     }
163     if (seen_long == 0 && seen_name == 0) {
164 	ex(as, "either of long or name option must be used");
165 	ret++;
166     }
167     if(seen_long > 1) {
168 	ex(as, "multiple long options");
169 	ret++;
170     }
171     if(seen_short > 1) {
172 	ex(as, "multiple short options");
173 	ret++;
174     }
175     if(seen_type > 1) {
176 	ex(as, "multiple types");
177 	ret++;
178     }
179     if(seen_argument > 1) {
180 	ex(as, "multiple arguments");
181 	ret++;
182     }
183     if(seen_help > 1) {
184 	ex(as, "multiple help strings");
185 	ret++;
186     }
187     if(seen_default > 1) {
188 	ex(as, "multiple default values");
189 	ret++;
190     }
191     return ret;
192 }
193 
194 static int
check_command(struct assignment * as)195 check_command(struct assignment *as)
196 {
197 	struct assignment *a;
198 	int seen_name = 0;
199 	int seen_function = 0;
200 	int seen_help = 0;
201 	int seen_argument = 0;
202 	int seen_minargs = 0;
203 	int seen_maxargs = 0;
204 	int ret = 0;
205 	for(a = as; a != NULL; a = a->next) {
206 		if(strcmp(a->name, "name") == 0)
207 			seen_name++;
208 		else if(strcmp(a->name, "function") == 0) {
209 			seen_function++;
210 		} else if(strcmp(a->name, "option") == 0)
211 			ret += check_option(a->u.assignment);
212 		else if(strcmp(a->name, "help") == 0) {
213 			seen_help++;
214 		} else if(strcmp(a->name, "argument") == 0) {
215 			seen_argument++;
216 		} else if(strcmp(a->name, "min_args") == 0) {
217 			seen_minargs++;
218 		} else if(strcmp(a->name, "max_args") == 0) {
219 			seen_maxargs++;
220 		} else {
221 			ex(a, "unknown name: %s", a->name);
222 			ret++;
223 		}
224 	}
225 	if(seen_name == 0) {
226 		ex(as, "no command name");
227 		ret++;
228 	}
229 	if(seen_function > 1) {
230 		ex(as, "multiple function names");
231 		ret++;
232 	}
233 	if(seen_help > 1) {
234 		ex(as, "multiple help strings");
235 		ret++;
236 	}
237 	if(seen_argument > 1) {
238 		ex(as, "multiple argument strings");
239 		ret++;
240 	}
241 	if(seen_minargs > 1) {
242 		ex(as, "multiple min_args strings");
243 		ret++;
244 	}
245 	if(seen_maxargs > 1) {
246 		ex(as, "multiple max_args strings");
247 		ret++;
248 	}
249 
250 	return ret;
251 }
252 
253 static int
check(struct assignment * as)254 check(struct assignment *as)
255 {
256     struct assignment *a;
257     int ret = 0;
258     for(a = as; a != NULL; a = a->next) {
259 	if(strcmp(a->name, "command")) {
260 	    fprintf(stderr, "unknown type %s line %d\n", a->name, a->lineno);
261 	    ret++;
262 	    continue;
263 	}
264 	if(a->type != a_assignment) {
265 	    fprintf(stderr, "bad command definition %s line %d\n", a->name, a->lineno);
266 	    ret++;
267 	    continue;
268 	}
269 	ret += check_command(a->u.assignment);
270     }
271     return ret;
272 }
273 
274 static struct assignment *
find_next(struct assignment * as,const char * name)275 find_next(struct assignment *as, const char *name)
276 {
277     for(as = as->next; as != NULL; as = as->next) {
278 	if(strcmp(as->name, name) == 0)
279 	    return as;
280     }
281     return NULL;
282 }
283 
284 static struct assignment *
find(struct assignment * as,const char * name)285 find(struct assignment *as, const char *name)
286 {
287     for(; as != NULL; as = as->next) {
288 	if(strcmp(as->name, name) == 0)
289 	    return as;
290     }
291     return NULL;
292 }
293 
294 static void
space(FILE * f,int level)295 space(FILE *f, int level)
296 {
297     fprintf(f, "%*.*s", level * 4, level * 4, " ");
298 }
299 
300 static void
cprint(int level,const char * fmt,...)301 cprint(int level, const char *fmt, ...)
302 {
303     va_list ap;
304     va_start(ap, fmt);
305     space(cfile, level);
306     vfprintf(cfile, fmt, ap);
307     va_end(ap);
308 }
309 
310 static void
hprint(int level,const char * fmt,...)311 hprint(int level, const char *fmt, ...)
312 {
313     va_list ap;
314     va_start(ap, fmt);
315     space(hfile, level);
316     vfprintf(hfile, fmt, ap);
317     va_end(ap);
318 }
319 
320 static void gen_name(char *str);
321 
322 static void
gen_command(struct assignment * as)323 gen_command(struct assignment *as)
324 {
325     struct assignment *a, *b;
326     char *f;
327     a = find(as, "name");
328     f = strdup(a->u.value);
329     gen_name(f);
330     cprint(1, "    { ");
331     fprintf(cfile, "\"%s\", ", a->u.value);
332     fprintf(cfile, "%s_wrap, ", f);
333     free(f);
334     b = find(as, "argument");
335     if(b)
336 	fprintf(cfile, "\"%s %s\", ", a->u.value, b->u.value);
337     else
338 	fprintf(cfile, "\"%s\", ", a->u.value);
339     b = find(as, "help");
340     if(b)
341 	fprintf(cfile, "\"%s\"", b->u.value);
342     else
343 	fprintf(cfile, "NULL");
344     fprintf(cfile, " },\n");
345     for(a = a->next; a != NULL; a = a->next)
346 	if(strcmp(a->name, "name") == 0)
347 	    cprint(1, "    { \"%s\", NULL, NULL, NULL },\n", a->u.value);
348     cprint(0, "\n");
349 }
350 
351 static void
gen_name(char * str)352 gen_name(char *str)
353 {
354     char *p;
355     for(p = str; *p != '\0'; p++)
356 	if(!isalnum((unsigned char)*p))
357 	    *p = '_';
358 }
359 
360 static char *
make_name(struct assignment * as)361 make_name(struct assignment *as)
362 {
363     struct assignment *lopt;
364     struct assignment *type;
365     char *s;
366     int ret;
367 
368     lopt = find(as, "long");
369     if(lopt == NULL)
370 	lopt = find(as, "name");
371     if(lopt == NULL)
372 	return NULL;
373 
374     type = find(as, "type");
375     if(strcmp(type->u.value, "-flag") == 0)
376 	ret = asprintf(&s, "%s_flag", lopt->u.value);
377     else
378 	ret = asprintf(&s, "%s_%s", lopt->u.value, type->u.value);
379     if (ret == -1)
380 	return NULL;
381     gen_name(s);
382     return s;
383 }
384 
385 
defval_int(const char * name,struct assignment * defval)386 static void defval_int(const char *name, struct assignment *defval)
387 {
388     if(defval != NULL)
389 	cprint(1, "opt.%s = %s;\n", name, defval->u.value);
390     else
391 	cprint(1, "opt.%s = 0;\n", name);
392 }
defval_neg_flag(const char * name,struct assignment * defval)393 static void defval_neg_flag(const char *name, struct assignment *defval)
394 {
395     if(defval != NULL)
396 	cprint(1, "opt.%s = %s;\n", name, defval->u.value);
397     else
398 	cprint(1, "opt.%s = 1;\n", name);
399 }
defval_string(const char * name,struct assignment * defval)400 static void defval_string(const char *name, struct assignment *defval)
401 {
402     if(defval != NULL)
403 	cprint(1, "opt.%s = (char *)(unsigned long)\"%s\";\n", name, defval->u.value);
404     else
405 	cprint(1, "opt.%s = NULL;\n", name);
406 }
defval_strings(const char * name,struct assignment * defval)407 static void defval_strings(const char *name, struct assignment *defval)
408 {
409     cprint(1, "opt.%s.num_strings = 0;\n", name);
410     cprint(1, "opt.%s.strings = NULL;\n", name);
411 }
412 
free_strings(const char * name)413 static void free_strings(const char *name)
414 {
415     cprint(1, "free_getarg_strings (&opt.%s);\n", name);
416 }
417 
418 struct type_handler {
419     const char *typename;
420     const char *c_type;
421     const char *getarg_type;
422     void (*defval)(const char*, struct assignment*);
423     void (*free)(const char*);
424 } type_handlers[] = {
425 	{ "integer",
426 	  "int",
427 	  "arg_integer",
428 	  defval_int,
429 	  NULL
430 	},
431 	{ "string",
432 	  "char*",
433 	  "arg_string",
434 	  defval_string,
435 	  NULL
436 	},
437 	{ "strings",
438 	  "struct getarg_strings",
439 	  "arg_strings",
440 	  defval_strings,
441 	  free_strings
442 	},
443 	{ "flag",
444 	  "int",
445 	  "arg_flag",
446 	  defval_int,
447 	  NULL
448 	},
449 	{ "-flag",
450 	  "int",
451 	  "arg_negative_flag",
452 	  defval_neg_flag,
453 	  NULL
454 	},
455 	{ NULL, NULL, NULL, NULL, NULL }
456 };
457 
find_handler(struct assignment * type)458 static struct type_handler *find_handler(struct assignment *type)
459 {
460     struct type_handler *th;
461     for(th = type_handlers; th->typename != NULL; th++)
462 	if(strcmp(type->u.value, th->typename) == 0)
463 	    return th;
464     ex(type, "unknown type \"%s\"", type->u.value);
465     exit(1);
466 }
467 
468 static void
gen_options(struct assignment * opt1,const char * name)469 gen_options(struct assignment *opt1, const char *name)
470 {
471     struct assignment *tmp;
472 
473     hprint(0, "struct %s_options {\n", name);
474 
475     for(tmp = opt1;
476 	tmp != NULL;
477 	tmp = find_next(tmp, "option")) {
478 	struct assignment *type;
479 	struct type_handler *th;
480 	char *s;
481 
482 	s = make_name(tmp->u.assignment);
483 	type = find(tmp->u.assignment, "type");
484 	th = find_handler(type);
485 	hprint(1, "%s %s;\n", th->c_type, s);
486 	free(s);
487     }
488     hprint(0, "};\n");
489 }
490 
491 static void
gen_wrapper(struct assignment * as)492 gen_wrapper(struct assignment *as)
493 {
494     struct assignment *name;
495     struct assignment *arg;
496     struct assignment *opt1;
497     struct assignment *function;
498     struct assignment *tmp;
499     char *n, *f;
500     int nargs = 0;
501     int narguments = 0;
502 
503     name = find(as, "name");
504     n = strdup(name->u.value);
505     gen_name(n);
506     arg = find(as, "argument");
507     if (arg)
508         narguments++;
509     opt1 = find(as, "option");
510     function = find(as, "function");
511     if(function)
512 	f = function->u.value;
513     else
514 	f = n;
515 
516 
517     if(opt1 != NULL) {
518 	gen_options(opt1, n);
519 	hprint(0, "int %s(struct %s_options*, int, char **);\n", f, n);
520     } else {
521 	hprint(0, "int %s(void*, int, char **);\n", f);
522     }
523 
524     fprintf(cfile, "static int\n");
525     fprintf(cfile, "%s_wrap(int argc, char **argv)\n", n);
526     fprintf(cfile, "{\n");
527     if(opt1 != NULL)
528 	cprint(1, "struct %s_options opt;\n", n);
529     cprint(1, "int ret;\n");
530     cprint(1, "int optidx = 0;\n");
531     cprint(1, "struct getargs args[] = {\n");
532     for(tmp = find(as, "option");
533 	tmp != NULL;
534 	tmp = find_next(tmp, "option")) {
535 	struct assignment *type = find(tmp->u.assignment, "type");
536 	struct assignment *lopt = find(tmp->u.assignment, "long");
537 	struct assignment *sopt = find(tmp->u.assignment, "short");
538 	struct assignment *aarg = find(tmp->u.assignment, "argument");
539 	struct assignment *help = find(tmp->u.assignment, "help");
540 
541 	struct type_handler *th;
542 
543 	cprint(2, "{ ");
544 	if(lopt)
545 	    fprintf(cfile, "\"%s\", ", lopt->u.value);
546 	else
547 	    fprintf(cfile, "NULL, ");
548 	if(sopt)
549 	    fprintf(cfile, "'%c', ", *sopt->u.value);
550 	else
551 	    fprintf(cfile, "0, ");
552 	th = find_handler(type);
553 	fprintf(cfile, "%s, ", th->getarg_type);
554 	fprintf(cfile, "NULL, ");
555 	if(help)
556 	    fprintf(cfile, "\"%s\", ", help->u.value);
557 	else
558 	    fprintf(cfile, "NULL, ");
559 	if(aarg) {
560 	    fprintf(cfile, "\"%s\"", aarg->u.value);
561             narguments++;
562 	} else
563 	    fprintf(cfile, "NULL");
564 	fprintf(cfile, " },\n");
565     }
566     cprint(2, "{ \"help\", 'h', arg_flag, NULL, NULL, NULL }\n");
567     cprint(1, "};\n");
568     cprint(1, "int help_flag = 0;\n");
569 
570     for(tmp = find(as, "option");
571 	tmp != NULL;
572 	tmp = find_next(tmp, "option")) {
573 	char *s;
574 	struct assignment *type = find(tmp->u.assignment, "type");
575 
576 	struct assignment *defval = find(tmp->u.assignment, "default");
577 
578 	struct type_handler *th;
579 
580 	s = make_name(tmp->u.assignment);
581 	th = find_handler(type);
582 	(*th->defval)(s, defval);
583 	free(s);
584     }
585 
586     for(tmp = find(as, "option");
587 	tmp != NULL;
588 	tmp = find_next(tmp, "option")) {
589 	char *s;
590 	s = make_name(tmp->u.assignment);
591 	cprint(1, "args[%d].value = &opt.%s;\n", nargs++, s);
592 	free(s);
593     }
594     cprint(1, "args[%d].value = &help_flag;\n", nargs++);
595     cprint(1, "if(getarg(args, %d, argc, argv, &optidx))\n", nargs);
596     cprint(2, "goto usage;\n");
597 
598     {
599 	int min_args = -1;
600 	int max_args = -1;
601 	char *end;
602 	if(narguments == 0) {
603 	    max_args = 0;
604 	} else {
605 	    if((tmp = find(as, "min_args")) != NULL) {
606 		min_args = strtol(tmp->u.value, &end, 0);
607 		if(*end != '\0') {
608 		    ex(tmp, "min_args is not numeric");
609 		    exit(1);
610 		}
611 		if(min_args < 0) {
612 		    ex(tmp, "min_args must be non-negative");
613 		    exit(1);
614 		}
615 	    }
616 	    if((tmp = find(as, "max_args")) != NULL) {
617 		max_args = strtol(tmp->u.value, &end, 0);
618 		if(*end != '\0') {
619 		    ex(tmp, "max_args is not numeric");
620 		    exit(1);
621 		}
622 		if(max_args < 0) {
623 		    ex(tmp, "max_args must be non-negative");
624 		    exit(1);
625 		}
626 	    }
627 	}
628 	if(min_args != -1 || max_args != -1) {
629 	    if(min_args == max_args) {
630 		cprint(1, "if(argc - optidx != %d) {\n",
631 		       min_args);
632 		cprint(2, "fprintf(stderr, \"Need exactly %u parameters (%%u given).\\n\\n\", argc - optidx);\n", min_args);
633 		cprint(2, "goto usage;\n");
634 		cprint(1, "}\n");
635 	    } else {
636 		if(max_args != -1) {
637 		    cprint(1, "if(argc - optidx > %d) {\n", max_args);
638 		    cprint(2, "fprintf(stderr, \"Arguments given (%%u) are more than expected (%u).\\n\\n\", argc - optidx);\n", max_args);
639 		    cprint(2, "goto usage;\n");
640 		    cprint(1, "}\n");
641 		}
642 		if(min_args != -1) {
643 		    cprint(1, "if(argc - optidx < %d) {\n", min_args);
644 		    cprint(2, "fprintf(stderr, \"Arguments given (%%u) are less than expected (%u).\\n\\n\", argc - optidx);\n", min_args);
645 		    cprint(2, "goto usage;\n");
646 		    cprint(1, "}\n");
647 		}
648 	    }
649 	}
650     }
651 
652     cprint(1, "if(help_flag)\n");
653     cprint(2, "goto usage;\n");
654 
655     cprint(1, "ret = %s(%s, argc - optidx, argv + optidx);\n",
656 	   f, opt1 ? "&opt": "NULL");
657 
658     /* free allocated data */
659     for(tmp = find(as, "option");
660 	tmp != NULL;
661 	tmp = find_next(tmp, "option")) {
662 	char *s;
663 	struct assignment *type = find(tmp->u.assignment, "type");
664 	struct type_handler *th;
665 	th = find_handler(type);
666 	if(th->free == NULL)
667 	    continue;
668 	s = make_name(tmp->u.assignment);
669 	(*th->free)(s);
670 	free(s);
671     }
672     cprint(1, "return ret;\n");
673 
674     cprint(0, "usage:\n");
675     cprint(1, "arg_printusage (args, %d, \"%s\", \"%s\");\n", nargs,
676 	   name->u.value, arg ? arg->u.value : "");
677     /* free allocated data */
678     for(tmp = find(as, "option");
679 	tmp != NULL;
680 	tmp = find_next(tmp, "option")) {
681 	char *s;
682 	struct assignment *type = find(tmp->u.assignment, "type");
683 	struct type_handler *th;
684 	th = find_handler(type);
685 	if(th->free == NULL)
686 	    continue;
687 	s = make_name(tmp->u.assignment);
688 	(*th->free)(s);
689 	free(s);
690     }
691     cprint(1, "return 0;\n");
692     cprint(0, "}\n");
693     cprint(0, "\n");
694 }
695 
696 char cname[PATH_MAX];
697 char hname[PATH_MAX];
698 
699 static void
gen(struct assignment * as)700 gen(struct assignment *as)
701 {
702     struct assignment *a;
703     cprint(0, "#include <stdio.h>\n");
704     cprint(0, "#include <krb5/getarg.h>\n");
705     cprint(0, "#include <krb5/sl.h>\n");
706     cprint(0, "#include \"%s\"\n\n", hname);
707 
708     hprint(0, "#include <stdio.h>\n");
709     hprint(0, "#include <krb5/sl.h>\n");
710     hprint(0, "\n");
711 
712 
713     for(a = as; a != NULL; a = a->next)
714 	gen_wrapper(a->u.assignment);
715 
716     cprint(0, "SL_cmd commands[] = {\n");
717     for(a = as; a != NULL; a = a->next)
718 	gen_command(a->u.assignment);
719     cprint(1, "{ NULL, NULL, NULL, NULL }\n");
720     cprint(0, "};\n");
721 
722     hprint(0, "extern SL_cmd commands[];\n");
723 }
724 
725 int version_flag;
726 int help_flag;
727 struct getargs args[] = {
728     { "version", 0, arg_flag, &version_flag, NULL, NULL },
729     { "help", 0, arg_flag, &help_flag, NULL, NULL }
730 };
731 int num_args = sizeof(args) / sizeof(args[0]);
732 
733 static void
usage(int code)734 usage(int code)
735 {
736     arg_printusage(args, num_args, NULL, "command-table");
737     exit(code);
738 }
739 
740 int
main(int argc,char ** argv)741 main(int argc, char **argv)
742 {
743     char *p;
744 
745     int optidx = 0;
746 
747     setprogname(argv[0]);
748     if(getarg(args, num_args, argc, argv, &optidx))
749 	usage(1);
750     if(help_flag)
751 	usage(0);
752     if(version_flag) {
753 	print_version(NULL);
754 	exit(0);
755     }
756 
757     if(argc == optidx)
758 	usage(1);
759 
760     filename = argv[optidx];
761     yyin = fopen(filename, "r");
762     if(yyin == NULL)
763 	err(1, "%s", filename);
764     p = strrchr(filename, '/');
765     if(p)
766 	strlcpy(cname, p + 1, sizeof(cname));
767     else
768 	strlcpy(cname, filename, sizeof(cname));
769     p = strrchr(cname, '.');
770     if(p)
771 	*p = '\0';
772     strlcpy(hname, cname, sizeof(hname));
773     strlcat(cname, ".c", sizeof(cname));
774     strlcat(hname, ".h", sizeof(hname));
775     yyparse();
776     if(error_flag)
777 	exit(1);
778     if(check(assignment) == 0) {
779 	cfile = fopen(cname, "w");
780 	if(cfile == NULL)
781 	  err(1, "%s", cname);
782 	hfile = fopen(hname, "w");
783 	if(hfile == NULL)
784 	  err(1, "%s", hname);
785 	gen(assignment);
786 	fclose(cfile);
787 	fclose(hfile);
788     }
789     fclose(yyin);
790     return 0;
791 }
792