2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Copyright (c) 2004-2006
10 * Author: Harti Brandt <harti@freebsd.org>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $Begemot: gensnmptree.c 383 2006-05-30 07:40:49Z brandt_h $
35 * Generate OID table from table description.
39 * file := top | top file
41 * top := tree | typedef | include
43 * tree := head elements ')'
45 * entry := head ':' index STRING elements ')'
47 * leaf := head type STRING ACCESS ')'
49 * column := head type ACCESS ')'
51 * type := BASETYPE | BASETYPE '|' subtype | enum | bits
55 * enum := ENUM '(' value ')'
57 * bits := BITS '(' value ')'
59 * value := optminus INT STRING | optminus INT STRING value
61 * optminus := '-' | EMPTY
63 * head := '(' INT STRING
65 * elements := EMPTY | elements element
67 * element := tree | leaf | column
69 * index := type | index type
71 * typedef := 'typedef' STRING type
73 * include := 'include' filespec
75 * filespec := '"' STRING '"' | '<' STRING '>'
77 #include <sys/types.h>
78 #include <sys/param.h>
90 #include <sys/queue.h>
94 #include "snmpagent.h"
97 * Constant prefix for all OIDs
99 static const asn_subid_t prefix[] = { 1, 3, 6 };
100 #define PREFIX_LEN (sizeof(prefix) / sizeof(prefix[0]))
103 static const char *file_prefix = "";
105 /* if true generate local include paths */
106 static int localincs = 0;
108 /* if true print tokens */
111 static const char usgtxt[] = "\
112 Generate SNMP tables.\n\
113 usage: gensnmptree [-dEehlt] [-I directory] [-i infile] [-p prefix]\n\
117 -E extract the named enums and bits only\n\
118 -e extract the named oids or enums\n\
119 -h print this info\n\
120 -I directory add directory to include path\n\
121 -i ifile read from the named file instead of stdin\n\
122 -l generate local include directives\n\
123 -p prefix prepend prefix to file and variable names\n\
124 -t generated a .def file\n\
128 * A node in the OID tree
143 TAILQ_HEAD(node_list, node);
147 asn_subid_t id; /* last element of OID */
148 char *name; /* name of node */
149 TAILQ_ENTRY(node) link;
150 u_int lno; /* starting line number */
151 u_int flags; /* allowed operations */
155 struct node_list subs;
159 uint32_t index; /* index for table entry */
160 char *func; /* function for tables */
161 struct node_list subs;
165 enum snmp_syntax syntax; /* syntax for this leaf */
166 char *func; /* function name */
170 enum snmp_syntax syntax; /* syntax for this column */
177 LIST_ENTRY(func) link;
180 static LIST_HEAD(, func) funcs = LIST_HEAD_INITIALIZER(funcs);
185 TAILQ_ENTRY(enums) link;
190 const char *from_fname;
195 TAILQ_HEAD(, enums) enums;
196 LIST_ENTRY(type) link;
199 static LIST_HEAD(, type) types = LIST_HEAD_INITIALIZER(types);
201 static void report(const char *, ...) __dead2 __printflike(1, 2);
202 static void report_node(const struct node *, const char *, ...)
203 __dead2 __printflike(2, 3);
205 /************************************************************
207 * Allocate memory and panic just in the case...
214 if ((ptr = malloc(size)) == NULL)
215 err(1, "allocing %zu bytes", size);
221 savestr(const char *s)
226 return (strcpy(xalloc(strlen(s) + 1), s));
229 /************************************************************
238 LIST_ENTRY(input) link;
240 static LIST_HEAD(, input) inputs = LIST_HEAD_INITIALIZER(inputs);
241 static struct input *input = NULL;
243 #define MAX_PATHS 100
244 static u_int npaths = 2;
245 static u_int stdpaths = 2;
246 static const char *paths[MAX_PATHS + 1] = {
247 "/usr/share/snmp/defs",
248 "/usr/local/share/snmp/defs",
252 static int pbchar = -1;
255 path_new(const char *path)
257 if (npaths >= MAX_PATHS)
258 report("too many -I directives");
259 memmove(&paths[npaths - stdpaths + 1], &paths[npaths - stdpaths],
260 sizeof(path[0]) * stdpaths);
261 paths[npaths - stdpaths] = savestr(path);
266 input_new(FILE *fp, const char *path, const char *fname)
270 ip = xalloc(sizeof(*ip));
273 ip->fname = savestr(fname);
274 ip->path = savestr(path);
275 LIST_INSERT_HEAD(&inputs, ip, link);
289 LIST_REMOVE(input, link);
292 input = LIST_FIRST(&inputs);
296 tryopen(const char *path, const char *fname)
304 fn = xalloc(strlen(path) + strlen(fname) + 2);
305 sprintf(fn, "%s/%s", path, fname);
313 input_fopen(const char *fname, int loc)
319 if (fname[0] == '/') {
320 if ((fp = tryopen(NULL, fname)) != NULL) {
321 input_new(fp, NULL, fname);
332 if ((fp = tryopen(path, fname)) != NULL) {
333 input_new(fp, NULL, fname);
338 for (p = 0; paths[p] != NULL; p++)
339 if ((fp = tryopen(paths[p], fname)) != NULL) {
340 input_new(fp, paths[p], fname);
344 report("cannot open '%s'", fname);
362 if ((c = getc(input->fp)) != EOF)
378 /************************************************************
383 TOK_EOF = 0200, /* end-of-file seen */
384 TOK_NUM, /* number */
385 TOK_STR, /* string */
386 TOK_ACCESS, /* access operator */
387 TOK_TYPE, /* type operator */
388 TOK_ENUM, /* enum token (kind of a type) */
389 TOK_TYPEDEF, /* typedef directive */
390 TOK_DEFTYPE, /* defined type */
391 TOK_INCLUDE, /* include directive */
392 TOK_FILENAME, /* filename ("foo.bar" or <foo.bar>) */
393 TOK_BITS, /* bits token (kind of a type) */
396 static const struct {
401 { "GET", TOK_ACCESS, FL_GET },
402 { "SET", TOK_ACCESS, FL_SET },
403 { "NULL", TOK_TYPE, SNMP_SYNTAX_NULL },
404 { "INTEGER", TOK_TYPE, SNMP_SYNTAX_INTEGER },
405 { "INTEGER32", TOK_TYPE, SNMP_SYNTAX_INTEGER },
406 { "UNSIGNED32", TOK_TYPE, SNMP_SYNTAX_GAUGE },
407 { "OCTETSTRING", TOK_TYPE, SNMP_SYNTAX_OCTETSTRING },
408 { "IPADDRESS", TOK_TYPE, SNMP_SYNTAX_IPADDRESS },
409 { "OID", TOK_TYPE, SNMP_SYNTAX_OID },
410 { "TIMETICKS", TOK_TYPE, SNMP_SYNTAX_TIMETICKS },
411 { "COUNTER", TOK_TYPE, SNMP_SYNTAX_COUNTER },
412 { "GAUGE", TOK_TYPE, SNMP_SYNTAX_GAUGE },
413 { "COUNTER64", TOK_TYPE, SNMP_SYNTAX_COUNTER64 },
414 { "ENUM", TOK_ENUM, SNMP_SYNTAX_INTEGER },
415 { "BITS", TOK_BITS, SNMP_SYNTAX_OCTETSTRING },
416 { "typedef", TOK_TYPEDEF, 0 },
417 { "include", TOK_INCLUDE, 0 },
421 /* arbitrary upper limit on node names and function names */
424 u_long val; /* integer values */
425 int all_cond; /* all conditions are true */
426 int saved_token = -1;
429 * Report an error and exit.
432 report(const char *fmt, ...)
438 fprintf(stderr, "line %u: ", input->lno);
439 vfprintf(stderr, fmt, ap);
440 fprintf(stderr, "\n");
441 fprintf(stderr, "context: \"");
442 while ((c = tgetc()) != EOF && c != '\n')
443 fprintf(stderr, "%c", c);
444 fprintf(stderr, "\n");
449 report_node(const struct node *np, const char *fmt, ...)
454 fprintf(stderr, "line %u, node %s: ", np->lno, np->name);
455 vfprintf(stderr, fmt, ap);
456 fprintf(stderr, "\n");
462 * Return a fresh copy of the string constituting the current token.
467 return (savestr(str));
471 * Get the next token from input.
474 gettoken_internal(void)
479 if (saved_token != -1) {
487 * Skip any whitespace before the next token
489 while ((c = tgetc()) != EOF) {
498 report("unexpected character %#2x", (u_int)c);
504 while ((c = tgetc()) != EOF) {
510 report("unexpected EOF in comment");
514 * Single character tokens
516 if (strchr("():|-", c) != NULL)
519 if (c == '"' || c == '<') {
529 while ((c = tgetc()) != EOF) {
532 if (n == sizeof(str) - 1) {
534 report("filename too long '%s...'", str);
539 return (TOK_FILENAME);
548 while ((c = tgetc()) != EOF) {
553 if (n == sizeof(str) - 1) {
555 report("number too long '%s...'", str);
560 sscanf(str, "%lu", &val);
565 * So that has to be a string.
567 if (isalpha(c) || c == '_') {
570 while ((c = tgetc()) != EOF) {
571 if (!isalnum(c) && c != '_' && c != '-') {
575 if (n == sizeof(str) - 1) {
577 report("string too long '%s...'", str);
586 for (c = 0; keywords[c].str != NULL; c++)
587 if (strcmp(keywords[c].str, str) == 0) {
588 val = keywords[c].val;
589 return (keywords[c].tok);
592 LIST_FOREACH(t, &types, link) {
593 if (strcmp(t->name, str) == 0) {
595 return (TOK_DEFTYPE);
601 errx(1, "%u: unexpected character '%c'", input->lno, c);
603 errx(1, "%u: unexpected character 0x%02x", input->lno,
609 int tok = gettoken_internal();
615 fprintf(stderr, "EOF ");
619 fprintf(stderr, "NUM(%lu) ", val);
623 fprintf(stderr, "STR(%s) ", str);
627 fprintf(stderr, "ACCESS(%lu) ", val);
631 fprintf(stderr, "TYPE(%lu) ", val);
635 fprintf(stderr, "ENUM ");
639 fprintf(stderr, "BITS ");
643 fprintf(stderr, "TYPEDEF ");
647 fprintf(stderr, "DEFTYPE(%s,%lu) ", str, val);
651 fprintf(stderr, "INCLUDE ");
655 fprintf(stderr, "FILENAME ");
661 fprintf(stderr, "'%c' ", tok);
662 else if (tok == '\n')
663 fprintf(stderr, "\n");
665 fprintf(stderr, "%02x ", tok);
678 pushback(enum tok tok)
681 if (saved_token != -1)
690 make_type(const char *s)
694 t = xalloc(sizeof(*t));
695 t->name = savestr(s);
697 t->syntax = SNMP_SYNTAX_NULL;
698 t->from_fname = savestr(input->fname);
699 t->from_lno = input->lno;
700 TAILQ_INIT(&t->enums);
701 LIST_INSERT_HEAD(&types, t, link);
707 * Parse a type. We've seen the ENUM or type keyword already. Leave next
711 parse_type(enum tok *tok, struct type *t, const char *vname)
718 if (*tok == TOK_ENUM || *tok == TOK_BITS) {
719 if (t == NULL && vname != NULL) {
720 t = make_type(vname);
721 t->is_enum = (*tok == TOK_ENUM);
722 t->is_bits = (*tok == TOK_BITS);
725 if (gettoken() != '(')
726 report("'(' expected after ENUM");
728 if ((*tok = gettoken()) == TOK_EOF)
729 report("unexpected EOF in ENUM");
733 e = xalloc(sizeof(*e));
736 if ((*tok = gettoken()) == TOK_EOF)
737 report("unexpected EOF in ENUM");
738 e->value = -(long)val;
743 report("need value for ENUM/BITS");
744 if (gettoken() != TOK_STR)
745 report("need string in ENUM/BITS");
748 TAILQ_INSERT_TAIL(&t->enums, e, link);
750 if ((*tok = gettoken()) == TOK_EOF)
751 report("unexpected EOF in ENUM/BITS");
752 } while (*tok != ')');
755 } else if (*tok == TOK_DEFTYPE) {
759 if ((*tok = gettoken()) == '|') {
760 if (gettoken() != TOK_STR)
761 report("subtype expected after '|'");
770 * Parse the next node (complete with all subnodes)
779 node = xalloc(sizeof(struct node));
780 node->lno = input->lno;
784 report("'(' expected at begin of node");
785 if (gettoken() != TOK_NUM)
786 report("node id expected after opening '('");
788 report("subid too large '%lu'", val);
789 node->id = (asn_subid_t)val;
790 if (gettoken() != TOK_STR)
791 report("node name expected after '(' ID");
792 node->name = savetok();
794 if ((tok = gettoken()) == TOK_TYPE || tok == TOK_DEFTYPE ||
795 tok == TOK_ENUM || tok == TOK_BITS) {
797 u_int syntax = parse_type(&tok, NULL, node->name);
799 if (tok == TOK_STR) {
801 node->type = NODE_LEAF;
802 node->u.leaf.func = savetok();
803 node->u.leaf.syntax = syntax;
807 node->type = NODE_COLUMN;
808 node->u.column.syntax = syntax;
812 if (tok != TOK_ACCESS)
813 report("access keyword or ')' expected");
814 node->flags |= (u_int)val;
818 } else if (tok == ':') {
820 node->type = NODE_ENTRY;
821 TAILQ_INIT(&node->u.entry.subs);
824 node->u.entry.index = 0;
826 while (tok == TOK_TYPE || tok == TOK_DEFTYPE ||
827 tok == TOK_ENUM || tok == TOK_BITS) {
828 u_int syntax = parse_type(&tok, NULL, node->name);
829 if (index_count++ == SNMP_INDEXES_MAX)
830 report("too many table indexes");
831 node->u.entry.index |=
832 syntax << (SNMP_INDEX_SHIFT * index_count);
834 node->u.entry.index |= index_count;
835 if (index_count == 0)
836 report("need at least one index");
838 report("function name expected");
840 node->u.entry.func = savetok();
846 TAILQ_INSERT_TAIL(&node->u.entry.subs, sub, link);
852 node->type = NODE_TREE;
853 TAILQ_INIT(&node->u.tree.subs);
857 TAILQ_INSERT_TAIL(&node->u.tree.subs, sub, link);
865 * Parse a top level element. Return the tree if it was a tree, NULL
869 parse_top(enum tok tok)
876 if (tok == TOK_TYPEDEF) {
877 if (gettoken() != TOK_STR)
878 report("type name expected after typedef");
883 t->is_enum = (tok == TOK_ENUM);
884 t->is_bits = (tok == TOK_BITS);
885 t->syntax = parse_type(&tok, t, NULL);
891 if (tok == TOK_INCLUDE) {
892 if (gettoken() != TOK_FILENAME)
893 report("filename expected in include directive");
895 input_fopen(str, val);
899 report("'(' or 'typedef' expected");
903 * Generate the C-code table part for one node.
906 gen_node(FILE *fp, struct node *np, struct asn_oid *oid, u_int idx,
913 if (oid->len == ASN_MAXOIDLEN)
914 report_node(np, "OID too long");
915 oid->subs[oid->len++] = np->id;
917 if (np->type == NODE_TREE) {
918 TAILQ_FOREACH(sub, &np->u.tree.subs, link)
919 gen_node(fp, sub, oid, 0, NULL);
923 if (np->type == NODE_ENTRY) {
924 TAILQ_FOREACH(sub, &np->u.entry.subs, link)
925 gen_node(fp, sub, oid, np->u.entry.index,
932 if ((np->flags & (FL_GET|FL_SET)) == 0) {
937 fprintf(fp, " {{ %u, {", oid->len);
938 for (n = 0; n < oid->len; n++)
939 fprintf(fp, " %u,", oid->subs[n]);
940 fprintf(fp, " }}, \"%s\", ", np->name);
942 if (np->type == NODE_COLUMN) {
943 syntax = np->u.column.syntax;
944 fprintf(fp, "SNMP_NODE_COLUMN, ");
946 syntax = np->u.leaf.syntax;
947 fprintf(fp, "SNMP_NODE_LEAF, ");
952 case SNMP_SYNTAX_NULL:
953 fprintf(fp, "SNMP_SYNTAX_NULL, ");
956 case SNMP_SYNTAX_INTEGER:
957 fprintf(fp, "SNMP_SYNTAX_INTEGER, ");
960 case SNMP_SYNTAX_OCTETSTRING:
961 fprintf(fp, "SNMP_SYNTAX_OCTETSTRING, ");
964 case SNMP_SYNTAX_IPADDRESS:
965 fprintf(fp, "SNMP_SYNTAX_IPADDRESS, ");
968 case SNMP_SYNTAX_OID:
969 fprintf(fp, "SNMP_SYNTAX_OID, ");
972 case SNMP_SYNTAX_TIMETICKS:
973 fprintf(fp, "SNMP_SYNTAX_TIMETICKS, ");
976 case SNMP_SYNTAX_COUNTER:
977 fprintf(fp, "SNMP_SYNTAX_COUNTER, ");
980 case SNMP_SYNTAX_GAUGE:
981 fprintf(fp, "SNMP_SYNTAX_GAUGE, ");
984 case SNMP_SYNTAX_COUNTER64:
985 fprintf(fp, "SNMP_SYNTAX_COUNTER64, ");
988 case SNMP_SYNTAX_NOSUCHOBJECT:
989 case SNMP_SYNTAX_NOSUCHINSTANCE:
990 case SNMP_SYNTAX_ENDOFMIBVIEW:
994 if (np->type == NODE_COLUMN)
995 fprintf(fp, "%s, ", func);
997 fprintf(fp, "%s, ", np->u.leaf.func);
1000 if (np->flags & FL_SET)
1001 fprintf(fp, "|SNMP_NODE_CANSET");
1002 fprintf(fp, ", %#x, NULL, NULL },\n", idx);
1008 * Generate the header file with the function declarations.
1011 gen_header(FILE *fp, struct node *np, u_int oidlen, const char *func)
1018 if (np->type == NODE_TREE) {
1019 TAILQ_FOREACH(sub, &np->u.tree.subs, link)
1020 gen_header(fp, sub, oidlen, NULL);
1023 if (np->type == NODE_ENTRY) {
1024 TAILQ_FOREACH(sub, &np->u.entry.subs, link)
1025 gen_header(fp, sub, oidlen, np->u.entry.func);
1029 if((np->flags & (FL_GET|FL_SET)) == 0)
1032 if (np->type == NODE_COLUMN) {
1034 errx(1, "column without function (%s) - probably "
1035 "outside of a table", np->name);
1036 sprintf(f, "%s", func);
1038 sprintf(f, "%s", np->u.leaf.func);
1040 LIST_FOREACH(ptr, &funcs, link)
1041 if (strcmp(ptr->name, f) == 0)
1045 ptr = xalloc(sizeof(*ptr));
1046 ptr->name = savestr(f);
1047 LIST_INSERT_HEAD(&funcs, ptr, link);
1049 fprintf(fp, "int %s(struct snmp_context *, "
1050 "struct snmp_value *, u_int, u_int, "
1051 "enum snmp_op);\n", f);
1054 fprintf(fp, "# define LEAF_%s %u\n", np->name, np->id);
1058 * Generate the OID table.
1061 gen_table(FILE *fp, struct node *node)
1065 fprintf(fp, "#include <sys/types.h>\n");
1066 fprintf(fp, "#include <stdio.h>\n");
1067 #ifdef HAVE_STDINT_H
1068 fprintf(fp, "#include <stdint.h>\n");
1071 fprintf(fp, "#include \"asn1.h\"\n");
1072 fprintf(fp, "#include \"snmp.h\"\n");
1073 fprintf(fp, "#include \"snmpagent.h\"\n");
1075 fprintf(fp, "#include <bsnmp/asn1.h>\n");
1076 fprintf(fp, "#include <bsnmp/snmp.h>\n");
1077 fprintf(fp, "#include <bsnmp/snmpagent.h>\n");
1079 fprintf(fp, "#include \"%stree.h\"\n", file_prefix);
1082 fprintf(fp, "const struct snmp_node %sctree[] = {\n", file_prefix);
1084 oid.len = PREFIX_LEN;
1085 memcpy(oid.subs, prefix, sizeof(prefix));
1086 gen_node(fp, node, &oid, 0, NULL);
1088 fprintf(fp, "};\n\n");
1092 print_syntax(u_int syntax)
1096 for (i = 0; keywords[i].str != NULL; i++)
1097 if (keywords[i].tok == TOK_TYPE &&
1098 keywords[i].val == syntax) {
1099 printf(" %s", keywords[i].str);
1106 * Generate a tree definition file
1109 gen_tree(const struct node *np, int level)
1111 const struct node *sp;
1114 printf("%*s(%u %s", 2 * level, "", np->id, np->name);
1119 print_syntax(np->u.leaf.syntax);
1120 printf(" %s%s%s)\n", np->u.leaf.func,
1121 (np->flags & FL_GET) ? " GET" : "",
1122 (np->flags & FL_SET) ? " SET" : "");
1126 if (TAILQ_EMPTY(&np->u.tree.subs)) {
1130 TAILQ_FOREACH(sp, &np->u.tree.subs, link)
1131 gen_tree(sp, level + 1);
1132 printf("%*s)\n", 2 * level, "");
1139 for (i = 0; i < SNMP_INDEX_COUNT(np->u.entry.index); i++)
1140 print_syntax(SNMP_INDEX(np->u.entry.index, i));
1141 printf(" %s\n", np->u.entry.func);
1142 TAILQ_FOREACH(sp, &np->u.entry.subs, link)
1143 gen_tree(sp, level + 1);
1144 printf("%*s)\n", 2 * level, "");
1148 print_syntax(np->u.column.syntax);
1149 printf("%s%s)\n", (np->flags & FL_GET) ? " GET" : "",
1150 (np->flags & FL_SET) ? " SET" : "");
1156 extract(FILE *fp, const struct node *np, struct asn_oid *oid, const char *obj,
1157 const struct asn_oid *idx, const char *iname)
1162 if (oid->len == ASN_MAXOIDLEN)
1163 report_node(np, "OID too long");
1164 oid->subs[oid->len++] = np->id;
1166 if (strcmp(obj, np->name) == 0) {
1167 if (oid->len + idx->len >= ASN_MAXOIDLEN)
1168 report_node(np, "OID too long");
1169 fprintf(fp, "#define OID_%s%s\t%u\n", np->name,
1170 iname ? iname : "", np->id);
1171 fprintf(fp, "#define OIDLEN_%s%s\t%u\n", np->name,
1172 iname ? iname : "", oid->len + idx->len);
1173 fprintf(fp, "#define OIDX_%s%s\t{ %u, {", np->name,
1174 iname ? iname : "", oid->len + idx->len);
1175 for (n = 0; n < oid->len; n++)
1176 fprintf(fp, " %u,", oid->subs[n]);
1177 for (n = 0; n < idx->len; n++)
1178 fprintf(fp, " %u,", idx->subs[n]);
1179 fprintf(fp, " } }\n");
1183 if (np->type == NODE_TREE) {
1184 TAILQ_FOREACH(sub, &np->u.tree.subs, link)
1185 if (!extract(fp, sub, oid, obj, idx, iname))
1187 } else if (np->type == NODE_ENTRY) {
1188 TAILQ_FOREACH(sub, &np->u.entry.subs, link)
1189 if (!extract(fp, sub, oid, obj, idx, iname))
1197 gen_extract(FILE *fp, const struct node *root, char *object)
1201 char *s, *e, *end, *iname;
1205 /* look whether the object to extract has an index part */
1208 s = strchr(object, '.');
1210 iname = malloc(strlen(s) + 1);
1212 err(1, "cannot allocated index");
1215 for (e = iname; *e != '\0'; e++)
1222 errx(1, "bad index syntax");
1223 if ((e = strchr(s, '.')) != NULL)
1227 ul = strtoul(s, &end, 0);
1229 errx(1, "bad index syntax '%s'", end);
1231 err(1, "bad index syntax");
1233 if (idx.len == ASN_MAXOIDLEN)
1234 errx(1, "index oid too large");
1235 idx.subs[idx.len++] = ul;
1241 oid.len = PREFIX_LEN;
1242 memcpy(oid.subs, prefix, sizeof(prefix));
1243 ret = extract(fp, root, &oid, object, &idx, iname);
1252 check_sub_order(const struct node *np, const struct node_list *subs)
1255 const struct node *sub;
1256 asn_subid_t maxid = 0;
1258 /* ensure, that subids are ordered */
1260 TAILQ_FOREACH(sub, subs, link) {
1261 if (!first && sub->id <= maxid)
1262 report_node(np, "subids not ordered at %s", sub->name);
1269 * Do some sanity checks on the tree definition and do some computations.
1272 check_tree(struct node *np)
1276 if (np->type == NODE_LEAF || np->type == NODE_COLUMN) {
1277 if ((np->flags & (FL_GET|FL_SET)) != 0)
1282 if (np->type == NODE_ENTRY) {
1283 check_sub_order(np, &np->u.entry.subs);
1285 /* ensure all subnodes are columns */
1286 TAILQ_FOREACH(sub, &np->u.entry.subs, link) {
1287 if (sub->type != NODE_COLUMN)
1288 report_node(np, "entry subnode '%s' is not "
1289 "a column", sub->name);
1293 check_sub_order(np, &np->u.tree.subs);
1295 TAILQ_FOREACH(sub, &np->u.tree.subs, link)
1301 merge_subs(struct node_list *s1, struct node_list *s2)
1303 struct node *n1, *n2;
1305 while (!TAILQ_EMPTY(s2)) {
1306 n2 = TAILQ_FIRST(s2);
1307 TAILQ_REMOVE(s2, n2, link);
1309 TAILQ_FOREACH(n1, s1, link)
1310 if (n1->id >= n2->id)
1313 TAILQ_INSERT_TAIL(s1, n2, link);
1314 else if (n1->id > n2->id)
1315 TAILQ_INSERT_BEFORE(n1, n2, link);
1317 if (n1->type == NODE_TREE && n2->type == NODE_TREE) {
1318 if (strcmp(n1->name, n2->name) != 0)
1319 errx(1, "trees to merge must have "
1320 "same name '%s' '%s'", n1->name,
1322 merge_subs(&n1->u.tree.subs, &n2->u.tree.subs);
1324 } else if (n1->type == NODE_ENTRY &&
1325 n2->type == NODE_ENTRY) {
1326 if (strcmp(n1->name, n2->name) != 0)
1327 errx(1, "entries to merge must have "
1328 "same name '%s' '%s'", n1->name,
1330 if (n1->u.entry.index != n2->u.entry.index)
1331 errx(1, "entries to merge must have "
1332 "same index '%s'", n1->name);
1333 if (strcmp(n1->u.entry.func,
1334 n2->u.entry.func) != 0)
1335 errx(1, "entries to merge must have "
1336 "same op '%s'", n1->name);
1337 merge_subs(&n1->u.entry.subs,
1341 errx(1, "entities to merge must be both "
1342 "trees or both entries: %s, %s",
1343 n1->name, n2->name);
1349 merge(struct node **root, struct node *t)
1352 if (*root == NULL) {
1359 /* both must be trees */
1360 if ((*root)->type != NODE_TREE)
1361 errx(1, "root is not a tree");
1362 if (t->type != NODE_TREE)
1363 errx(1, "can merge only with tree");
1364 if ((*root)->id != t->id)
1365 errx(1, "trees to merge must have same id");
1367 merge_subs(&(*root)->u.tree.subs, &t->u.tree.subs);
1371 unminus(FILE *fp, const char *s)
1374 while (*s != '\0') {
1378 fprintf(fp, "%c", *s);
1384 gen_enum(FILE *fp, const struct type *t)
1386 const struct enums *e;
1387 long min = LONG_MAX;
1390 fprintf(fp, "#ifndef %s_defined__\n", t->name);
1391 fprintf(fp, "#define %s_defined__\n", t->name);
1392 fprintf(fp, "/*\n");
1393 fprintf(fp, " * From %s:%u\n", t->from_fname, t->from_lno);
1394 fprintf(fp, " */\n");
1395 fprintf(fp, "enum %s {\n", t->name);
1396 TAILQ_FOREACH(e, &t->enums, link) {
1397 fprintf(fp, "\t%s_", t->name);
1398 unminus(fp, e->name);
1399 fprintf(fp, " = %ld,\n", e->value);
1403 fprintf(fp, "};\n");
1404 fprintf(fp, "#define STROFF_%s %ld\n", t->name, min);
1405 fprintf(fp, "#define STRING_%s \\\n", t->name);
1406 TAILQ_FOREACH(e, &t->enums, link) {
1407 fprintf(fp, "\t[%ld] \"%s_", e->value - min, t->name);
1408 unminus(fp, e->name);
1409 fprintf(fp, "\",\\\n");
1412 fprintf(fp, "#endif /* %s_defined__ */\n", t->name);
1418 const struct type *t;
1420 LIST_FOREACH(t, &types, link)
1421 if (t->is_enum || t->is_bits)
1426 extract_enum(FILE *fp, const char *name)
1428 const struct type *t;
1430 LIST_FOREACH(t, &types, link)
1431 if ((t->is_enum || t->is_bits) && strcmp(t->name, name) == 0) {
1439 main(int argc, char *argv[])
1446 char fname[MAXPATHLEN + 1];
1449 char *infile = NULL;
1451 while ((opt = getopt(argc, argv, "dEehI:i:lp:t")) != EOF)
1459 fprintf(stderr, "%s", usgtxt);
1483 file_prefix = optarg;
1484 if (strlen(file_prefix) + strlen("tree.c") >
1486 errx(1, "prefix too long");
1494 if (do_extract + do_tree + do_enums > 1)
1495 errx(1, "conflicting options -e/-t/-E");
1496 if (!do_extract && !do_enums && argc != optind)
1497 errx(1, "no arguments allowed");
1498 if ((do_extract || do_enums) && argc == optind)
1499 errx(1, "no objects specified");
1501 if (infile == NULL) {
1502 input_new(stdin, NULL, "<stdin>");
1504 if ((fp = fopen(infile, "r")) == NULL)
1505 err(1, "%s", infile);
1506 input_new(fp, NULL, infile);
1509 root = parse_top(gettoken());
1510 while ((tok = gettoken()) != TOK_EOF)
1511 merge(&root, parse_top(tok));
1516 while (optind < argc) {
1517 if (gen_extract(stdout, root, argv[optind]))
1518 errx(1, "object not found: %s", argv[optind]);
1524 while (optind < argc) {
1525 if (extract_enum(stdout, argv[optind]))
1526 errx(1, "enum not found: %s", argv[optind]);
1535 sprintf(fname, "%stree.h", file_prefix);
1536 if ((fp = fopen(fname, "w")) == NULL)
1537 err(1, "%s: ", fname);
1538 gen_header(fp, root, PREFIX_LEN, NULL);
1540 fprintf(fp, "\n#ifdef SNMPTREE_TYPES\n");
1542 fprintf(fp, "\n#endif /* SNMPTREE_TYPES */\n\n");
1544 fprintf(fp, "#define %sCTREE_SIZE %u\n", file_prefix, tree_size);
1545 fprintf(fp, "extern const struct snmp_node %sctree[];\n", file_prefix);
1549 sprintf(fname, "%stree.c", file_prefix);
1550 if ((fp = fopen(fname, "w")) == NULL)
1551 err(1, "%s: ", fname);
1552 gen_table(fp, root);