[HEIMDAL-646] malloc(0) checks for AIX
[heimdal.git] / lib / sl / slc-gram.y
blob0cba8fa3b1e110b45574d83a1fcd4b2a4fe65b36
1 %{
2 /*
3 * Copyright (c) 2004-2006 Kungliga Tekniska Högskolan
4 * (Royal Institute of Technology, Stockholm, Sweden).
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the Institute nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include <config.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <err.h>
40 #include <ctype.h>
41 #include <limits.h>
42 #include <getarg.h>
43 #include <vers.h>
44 #include <roken.h>
46 #include "slc.h"
47 extern FILE *yyin;
48 extern struct assignment *assignment;
51 %union {
52 char *string;
53 struct assignment *assignment;
56 %token <string> LITERAL
57 %token <string> STRING
58 %type <assignment> assignment assignments
60 %start start
64 start : assignments
66 assignment = $1;
70 assignments : assignment assignments
72 $1->next = $2;
73 $$ = $1;
75 | assignment
78 assignment : LITERAL '=' STRING
80 $$ = malloc(sizeof(*$$));
81 $$->name = $1;
82 $$->type = a_value;
83 $$->lineno = lineno;
84 $$->u.value = $3;
85 $$->next = NULL;
87 | LITERAL '=' '{' assignments '}'
89 $$ = malloc(sizeof(*$$));
90 $$->name = $1;
91 $$->type = a_assignment;
92 $$->lineno = lineno;
93 $$->u.assignment = $4;
94 $$->next = NULL;
99 char *filename;
100 FILE *cfile, *hfile;
101 int error_flag;
102 struct assignment *assignment;
105 static void
106 ex(struct assignment *a, const char *fmt, ...)
108 va_list ap;
109 fprintf(stderr, "%s:%d: ", a->name, a->lineno);
110 va_start(ap, fmt);
111 vfprintf(stderr, fmt, ap);
112 va_end(ap);
113 fprintf(stderr, "\n");
118 static int
119 check_option(struct assignment *as)
121 struct assignment *a;
122 int seen_long = 0;
123 int seen_short = 0;
124 int seen_type = 0;
125 int seen_argument = 0;
126 int seen_help = 0;
127 int seen_default = 0;
128 int ret = 0;
130 for(a = as; a != NULL; a = a->next) {
131 if(strcmp(a->name, "long") == 0)
132 seen_long++;
133 else if(strcmp(a->name, "short") == 0)
134 seen_short++;
135 else if(strcmp(a->name, "type") == 0)
136 seen_type++;
137 else if(strcmp(a->name, "argument") == 0)
138 seen_argument++;
139 else if(strcmp(a->name, "help") == 0)
140 seen_help++;
141 else if(strcmp(a->name, "default") == 0)
142 seen_default++;
143 else {
144 ex(a, "unknown name");
145 ret++;
148 if(seen_long == 0 && seen_short == 0) {
149 ex(as, "neither long nor short option");
150 ret++;
152 if(seen_long > 1) {
153 ex(as, "multiple long options");
154 ret++;
156 if(seen_short > 1) {
157 ex(as, "multiple short options");
158 ret++;
160 if(seen_type > 1) {
161 ex(as, "multiple types");
162 ret++;
164 if(seen_argument > 1) {
165 ex(as, "multiple arguments");
166 ret++;
168 if(seen_help > 1) {
169 ex(as, "multiple help strings");
170 ret++;
172 if(seen_default > 1) {
173 ex(as, "multiple default values");
174 ret++;
176 return ret;
179 static int
180 check_command(struct assignment *as)
182 struct assignment *a;
183 int seen_name = 0;
184 int seen_function = 0;
185 int seen_help = 0;
186 int seen_argument = 0;
187 int seen_minargs = 0;
188 int seen_maxargs = 0;
189 int ret = 0;
190 for(a = as; a != NULL; a = a->next) {
191 if(strcmp(a->name, "name") == 0)
192 seen_name++;
193 else if(strcmp(a->name, "function") == 0) {
194 seen_function++;
195 } else if(strcmp(a->name, "option") == 0)
196 ret += check_option(a->u.assignment);
197 else if(strcmp(a->name, "help") == 0) {
198 seen_help++;
199 } else if(strcmp(a->name, "argument") == 0) {
200 seen_argument++;
201 } else if(strcmp(a->name, "min_args") == 0) {
202 seen_minargs++;
203 } else if(strcmp(a->name, "max_args") == 0) {
204 seen_maxargs++;
205 } else {
206 ex(a, "unknown name");
207 ret++;
210 if(seen_name == 0) {
211 ex(as, "no command name");
212 ret++;
214 if(seen_function > 1) {
215 ex(as, "multiple function names");
216 ret++;
218 if(seen_help > 1) {
219 ex(as, "multiple help strings");
220 ret++;
222 if(seen_argument > 1) {
223 ex(as, "multiple argument strings");
224 ret++;
226 if(seen_minargs > 1) {
227 ex(as, "multiple min_args strings");
228 ret++;
230 if(seen_maxargs > 1) {
231 ex(as, "multiple max_args strings");
232 ret++;
235 return ret;
238 static int
239 check(struct assignment *as)
241 struct assignment *a;
242 int ret = 0;
243 for(a = as; a != NULL; a = a->next) {
244 if(strcmp(a->name, "command")) {
245 fprintf(stderr, "unknown type %s line %d\n", a->name, a->lineno);
246 ret++;
247 continue;
249 if(a->type != a_assignment) {
250 fprintf(stderr, "bad command definition %s line %d\n", a->name, a->lineno);
251 ret++;
252 continue;
254 ret += check_command(a->u.assignment);
256 return ret;
259 static struct assignment *
260 find_next(struct assignment *as, const char *name)
262 for(as = as->next; as != NULL; as = as->next) {
263 if(strcmp(as->name, name) == 0)
264 return as;
266 return NULL;
269 static struct assignment *
270 find(struct assignment *as, const char *name)
272 for(; as != NULL; as = as->next) {
273 if(strcmp(as->name, name) == 0)
274 return as;
276 return NULL;
279 static void
280 space(FILE *f, int level)
282 fprintf(f, "%*.*s", level * 4, level * 4, " ");
285 static void
286 cprint(int level, const char *fmt, ...)
288 va_list ap;
289 va_start(ap, fmt);
290 space(cfile, level);
291 vfprintf(cfile, fmt, ap);
292 va_end(ap);
295 static void
296 hprint(int level, const char *fmt, ...)
298 va_list ap;
299 va_start(ap, fmt);
300 space(hfile, level);
301 vfprintf(hfile, fmt, ap);
302 va_end(ap);
305 static void gen_name(char *str);
307 static void
308 gen_command(struct assignment *as)
310 struct assignment *a, *b;
311 char *f;
312 a = find(as, "name");
313 f = strdup(a->u.value);
314 gen_name(f);
315 cprint(1, " { ");
316 fprintf(cfile, "\"%s\", ", a->u.value);
317 fprintf(cfile, "%s_wrap, ", f);
318 b = find(as, "argument");
319 if(b)
320 fprintf(cfile, "\"%s %s\", ", a->u.value, b->u.value);
321 else
322 fprintf(cfile, "\"%s\", ", a->u.value);
323 b = find(as, "help");
324 if(b)
325 fprintf(cfile, "\"%s\"", b->u.value);
326 else
327 fprintf(cfile, "NULL");
328 fprintf(cfile, " },\n");
329 for(a = a->next; a != NULL; a = a->next)
330 if(strcmp(a->name, "name") == 0)
331 cprint(1, " { \"%s\" },\n", a->u.value);
332 cprint(0, "\n");
335 static void
336 gen_name(char *str)
338 char *p;
339 for(p = str; *p != '\0'; p++)
340 if(!isalnum((unsigned char)*p))
341 *p = '_';
344 static char *
345 make_name(struct assignment *as)
347 struct assignment *lopt;
348 struct assignment *type;
349 char *s;
351 lopt = find(as, "long");
352 if(lopt == NULL)
353 lopt = find(as, "name");
354 if(lopt == NULL)
355 return NULL;
357 type = find(as, "type");
358 if(strcmp(type->u.value, "-flag") == 0)
359 asprintf(&s, "%s_flag", lopt->u.value);
360 else
361 asprintf(&s, "%s_%s", lopt->u.value, type->u.value);
362 gen_name(s);
363 return s;
367 static void defval_int(const char *name, struct assignment *defval)
369 if(defval != NULL)
370 cprint(1, "opt.%s = %s;\n", name, defval->u.value);
371 else
372 cprint(1, "opt.%s = 0;\n", name);
374 static void defval_neg_flag(const char *name, struct assignment *defval)
376 if(defval != NULL)
377 cprint(1, "opt.%s = %s;\n", name, defval->u.value);
378 else
379 cprint(1, "opt.%s = 1;\n", name);
381 static void defval_string(const char *name, struct assignment *defval)
383 if(defval != NULL)
384 cprint(1, "opt.%s = \"%s\";\n", name, defval->u.value);
385 else
386 cprint(1, "opt.%s = NULL;\n", name);
388 static void defval_strings(const char *name, struct assignment *defval)
390 cprint(1, "opt.%s.num_strings = 0;\n", name);
391 cprint(1, "opt.%s.strings = NULL;\n", name);
394 static void free_strings(const char *name)
396 cprint(1, "free_getarg_strings (&opt.%s);\n", name);
399 struct type_handler {
400 const char *typename;
401 const char *c_type;
402 const char *getarg_type;
403 void (*defval)(const char*, struct assignment*);
404 void (*free)(const char*);
405 } type_handlers[] = {
406 { "integer",
407 "int",
408 "arg_integer",
409 defval_int,
410 NULL
412 { "string",
413 "char*",
414 "arg_string",
415 defval_string,
416 NULL
418 { "strings",
419 "struct getarg_strings",
420 "arg_strings",
421 defval_strings,
422 free_strings
424 { "flag",
425 "int",
426 "arg_flag",
427 defval_int,
428 NULL
430 { "-flag",
431 "int",
432 "arg_negative_flag",
433 defval_neg_flag,
434 NULL
436 { NULL }
439 static struct type_handler *find_handler(struct assignment *type)
441 struct type_handler *th;
442 for(th = type_handlers; th->typename != NULL; th++)
443 if(strcmp(type->u.value, th->typename) == 0)
444 return th;
445 ex(type, "unknown type \"%s\"", type->u.value);
446 exit(1);
449 static void
450 gen_options(struct assignment *opt1, const char *name)
452 struct assignment *tmp;
454 hprint(0, "struct %s_options {\n", name);
456 for(tmp = opt1;
457 tmp != NULL;
458 tmp = find_next(tmp, "option")) {
459 struct assignment *type;
460 struct type_handler *th;
461 char *s;
463 s = make_name(tmp->u.assignment);
464 type = find(tmp->u.assignment, "type");
465 th = find_handler(type);
466 hprint(1, "%s %s;\n", th->c_type, s);
467 free(s);
469 hprint(0, "};\n");
472 static void
473 gen_wrapper(struct assignment *as)
475 struct assignment *name;
476 struct assignment *arg;
477 struct assignment *opt1;
478 struct assignment *function;
479 struct assignment *tmp;
480 char *n, *f;
481 int nargs = 0;
483 name = find(as, "name");
484 n = strdup(name->u.value);
485 gen_name(n);
486 arg = find(as, "argument");
487 opt1 = find(as, "option");
488 function = find(as, "function");
489 if(function)
490 f = function->u.value;
491 else
492 f = n;
495 if(opt1 != NULL) {
496 gen_options(opt1, n);
497 hprint(0, "int %s(struct %s_options*, int, char **);\n", f, n);
498 } else {
499 hprint(0, "int %s(void*, int, char **);\n", f);
502 fprintf(cfile, "static int\n");
503 fprintf(cfile, "%s_wrap(int argc, char **argv)\n", n);
504 fprintf(cfile, "{\n");
505 if(opt1 != NULL)
506 cprint(1, "struct %s_options opt;\n", n);
507 cprint(1, "int ret;\n");
508 cprint(1, "int optidx = 0;\n");
509 cprint(1, "struct getargs args[] = {\n");
510 for(tmp = find(as, "option");
511 tmp != NULL;
512 tmp = find_next(tmp, "option")) {
513 struct assignment *type = find(tmp->u.assignment, "type");
514 struct assignment *lopt = find(tmp->u.assignment, "long");
515 struct assignment *sopt = find(tmp->u.assignment, "short");
516 struct assignment *aarg = find(tmp->u.assignment, "argument");
517 struct assignment *help = find(tmp->u.assignment, "help");
519 struct type_handler *th;
521 cprint(2, "{ ");
522 if(lopt)
523 fprintf(cfile, "\"%s\", ", lopt->u.value);
524 else
525 fprintf(cfile, "NULL, ");
526 if(sopt)
527 fprintf(cfile, "'%c', ", *sopt->u.value);
528 else
529 fprintf(cfile, "0, ");
530 th = find_handler(type);
531 fprintf(cfile, "%s, ", th->getarg_type);
532 fprintf(cfile, "NULL, ");
533 if(help)
534 fprintf(cfile, "\"%s\", ", help->u.value);
535 else
536 fprintf(cfile, "NULL, ");
537 if(aarg)
538 fprintf(cfile, "\"%s\"", aarg->u.value);
539 else
540 fprintf(cfile, "NULL");
541 fprintf(cfile, " },\n");
543 cprint(2, "{ \"help\", 'h', arg_flag, NULL, NULL, NULL }\n");
544 cprint(1, "};\n");
545 cprint(1, "int help_flag = 0;\n");
547 for(tmp = find(as, "option");
548 tmp != NULL;
549 tmp = find_next(tmp, "option")) {
550 char *s;
551 struct assignment *type = find(tmp->u.assignment, "type");
553 struct assignment *defval = find(tmp->u.assignment, "default");
555 struct type_handler *th;
557 s = make_name(tmp->u.assignment);
558 th = find_handler(type);
559 (*th->defval)(s, defval);
560 free(s);
563 for(tmp = find(as, "option");
564 tmp != NULL;
565 tmp = find_next(tmp, "option")) {
566 char *s;
567 s = make_name(tmp->u.assignment);
568 cprint(1, "args[%d].value = &opt.%s;\n", nargs++, s);
569 free(s);
571 cprint(1, "args[%d].value = &help_flag;\n", nargs++);
572 cprint(1, "if(getarg(args, %d, argc, argv, &optidx))\n", nargs);
573 cprint(2, "goto usage;\n");
576 int min_args = -1;
577 int max_args = -1;
578 char *end;
579 if(arg == NULL) {
580 max_args = 0;
581 } else {
582 if((tmp = find(as, "min_args")) != NULL) {
583 min_args = strtol(tmp->u.value, &end, 0);
584 if(*end != '\0') {
585 ex(tmp, "min_args is not numeric");
586 exit(1);
588 if(min_args < 0) {
589 ex(tmp, "min_args must be non-negative");
590 exit(1);
593 if((tmp = find(as, "max_args")) != NULL) {
594 max_args = strtol(tmp->u.value, &end, 0);
595 if(*end != '\0') {
596 ex(tmp, "max_args is not numeric");
597 exit(1);
599 if(max_args < 0) {
600 ex(tmp, "max_args must be non-negative");
601 exit(1);
605 if(min_args != -1 || max_args != -1) {
606 if(min_args == max_args) {
607 cprint(1, "if(argc - optidx != %d) {\n",
608 min_args);
609 cprint(2, "fprintf(stderr, \"Need exactly %u parameters (%%u given).\\n\\n\", argc - optidx);\n", min_args);
610 cprint(2, "goto usage;\n");
611 cprint(1, "}\n");
612 } else {
613 if(max_args != -1) {
614 cprint(1, "if(argc - optidx > %d) {\n", max_args);
615 cprint(2, "fprintf(stderr, \"Arguments given (%%u) are more than expected (%u).\\n\\n\", argc - optidx);\n", max_args);
616 cprint(2, "goto usage;\n");
617 cprint(1, "}\n");
619 if(min_args != -1) {
620 cprint(1, "if(argc - optidx < %d) {\n", min_args);
621 cprint(2, "fprintf(stderr, \"Arguments given (%%u) are less than expected (%u).\\n\\n\", argc - optidx);\n", min_args);
622 cprint(2, "goto usage;\n");
623 cprint(1, "}\n");
629 cprint(1, "if(help_flag)\n");
630 cprint(2, "goto usage;\n");
632 cprint(1, "ret = %s(%s, argc - optidx, argv + optidx);\n",
633 f, opt1 ? "&opt": "NULL");
635 /* free allocated data */
636 for(tmp = find(as, "option");
637 tmp != NULL;
638 tmp = find_next(tmp, "option")) {
639 char *s;
640 struct assignment *type = find(tmp->u.assignment, "type");
641 struct type_handler *th;
642 th = find_handler(type);
643 if(th->free == NULL)
644 continue;
645 s = make_name(tmp->u.assignment);
646 (*th->free)(s);
647 free(s);
649 cprint(1, "return ret;\n");
651 cprint(0, "usage:\n");
652 cprint(1, "arg_printusage (args, %d, \"%s\", \"%s\");\n", nargs,
653 name->u.value, arg ? arg->u.value : "");
654 /* free allocated data */
655 for(tmp = find(as, "option");
656 tmp != NULL;
657 tmp = find_next(tmp, "option")) {
658 char *s;
659 struct assignment *type = find(tmp->u.assignment, "type");
660 struct type_handler *th;
661 th = find_handler(type);
662 if(th->free == NULL)
663 continue;
664 s = make_name(tmp->u.assignment);
665 (*th->free)(s);
666 free(s);
668 cprint(1, "return 0;\n");
669 cprint(0, "}\n");
670 cprint(0, "\n");
673 char cname[PATH_MAX];
674 char hname[PATH_MAX];
676 static void
677 gen(struct assignment *as)
679 struct assignment *a;
680 cprint(0, "#include <stdio.h>\n");
681 cprint(0, "#include <getarg.h>\n");
682 cprint(0, "#include <sl.h>\n");
683 cprint(0, "#include \"%s\"\n\n", hname);
685 hprint(0, "#include <stdio.h>\n");
686 hprint(0, "#include <sl.h>\n");
687 hprint(0, "\n");
690 for(a = as; a != NULL; a = a->next)
691 gen_wrapper(a->u.assignment);
693 cprint(0, "SL_cmd commands[] = {\n");
694 for(a = as; a != NULL; a = a->next)
695 gen_command(a->u.assignment);
696 cprint(1, "{ NULL }\n");
697 cprint(0, "};\n");
699 hprint(0, "extern SL_cmd commands[];\n");
702 int version_flag;
703 int help_flag;
704 struct getargs args[] = {
705 { "version", 0, arg_flag, &version_flag },
706 { "help", 0, arg_flag, &help_flag }
708 int num_args = sizeof(args) / sizeof(args[0]);
710 static void
711 usage(int code)
713 arg_printusage(args, num_args, NULL, "command-table");
714 exit(code);
718 main(int argc, char **argv)
720 char *p;
722 int optidx = 0;
724 setprogname(argv[0]);
725 if(getarg(args, num_args, argc, argv, &optidx))
726 usage(1);
727 if(help_flag)
728 usage(0);
729 if(version_flag) {
730 print_version(NULL);
731 exit(0);
734 if(argc == optidx)
735 usage(1);
737 filename = argv[optidx];
738 yyin = fopen(filename, "r");
739 if(yyin == NULL)
740 err(1, "%s", filename);
741 p = strrchr(filename, '/');
742 if(p)
743 strlcpy(cname, p + 1, sizeof(cname));
744 else
745 strlcpy(cname, filename, sizeof(cname));
746 p = strrchr(cname, '.');
747 if(p)
748 *p = '\0';
749 strlcpy(hname, cname, sizeof(hname));
750 strlcat(cname, ".c", sizeof(cname));
751 strlcat(hname, ".h", sizeof(hname));
752 yyparse();
753 if(error_flag)
754 exit(1);
755 if(check(assignment) == 0) {
756 cfile = fopen(cname, "w");
757 if(cfile == NULL)
758 err(1, "%s", cname);
759 hfile = fopen(hname, "w");
760 if(hfile == NULL)
761 err(1, "%s", hname);
762 gen(assignment);
763 fclose(cfile);
764 fclose(hfile);
766 fclose(yyin);
767 return 0;