Lines Matching +full:sub +full:- +full:node
2 * Copyright (c) 2001-2003
6 * Copyright (c) 2004-2006,2018
33 * $Begemot: gensnmptree.c 383 2006-05-30 07:40:49Z brandt_h $
38 * ---------
61 * optminus := '-' | EMPTY
113 usage: gensnmptree [-dEeFfhlt] [-I directory] [-i infile] [-p prefix]\n\
116 -d debug mode\n\
117 -E extract the named or all enums and bits only\n\
118 -e extract the named oids or enums\n\
119 -F generate functions for -E into a .c file\n\
120 -f generate functions for -E into the header\n\
121 -h print this info\n\
122 -I directory add directory to include path\n\
123 -i ifile read from the named file instead of stdin\n\
124 -l generate local include directives\n\
125 -p prefix prepend prefix to file and variable names\n\
126 -t generate a .def file\n\
161 * A node in the OID tree
175 struct node;
176 TAILQ_HEAD(node_list, node);
178 struct node { struct
181 char *name; /* name of node */ argument
182 TAILQ_ENTRY(node) link;
238 static void report_node(const struct node *, const char *, ...)
288 static int pbchar = -1;
294 report("too many -I directives"); in path_new()
295 memmove(&paths[npaths - stdpaths + 1], &paths[npaths - stdpaths], in path_new()
297 paths[npaths - stdpaths] = savestr(path); in path_new()
307 ip->fp = fp; in input_new()
308 ip->lno = 1; in input_new()
309 ip->fname = savestr(fname); in input_new()
310 ip->path = savestr(path); in input_new()
322 fclose(input->fp); in input_close()
323 free(input->fname); in input_close()
324 free(input->path); in input_close()
366 path = input->path; in input_fopen()
388 if (pbchar != -1) { in tgetc()
390 pbchar = -1; in tgetc()
398 if ((c = getc(input->fp)) != EOF) in tgetc()
409 if (pbchar != -1) in tungetc()
419 TOK_EOF = 0200, /* end-of-file seen */
457 /* arbitrary upper limit on node names and function names */
461 static int saved_token = -1;
473 fprintf(stderr, "line %u: ", input->lno); in report()
484 report_node(const struct node *np, const char *fmt, ...) in report_node()
489 fprintf(stderr, "line %u, node %s: ", np->lno, np->name); in report_node()
514 if (saved_token != -1) { in gettoken_internal()
516 saved_token = -1; in gettoken_internal()
526 input->lno++; in gettoken_internal()
541 input->lno++; in gettoken_internal()
551 if (strchr("():|-", c) != NULL) in gettoken_internal()
567 if (n == sizeof(str) - 1) { in gettoken_internal()
588 if (n == sizeof(str) - 1) { in gettoken_internal()
606 if (!isalnum(c) && c != '_' && c != '-') { in gettoken_internal()
610 if (n == sizeof(str) - 1) { in gettoken_internal()
628 if (strcmp(t->name, str) == 0) { in gettoken_internal()
629 val = t->syntax; in gettoken_internal()
636 errx(1, "%u: unexpected character '%c'", input->lno, c); in gettoken_internal()
638 errx(1, "%u: unexpected character 0x%02x", input->lno, in gettoken_internal()
716 if (saved_token != -1) in pushback()
730 t->name = savestr(s); in make_type()
731 t->is_enum = 0; in make_type()
732 t->syntax = SNMP_SYNTAX_NULL; in make_type()
733 t->from_fname = savestr(input->fname); in make_type()
734 t->from_lno = input->lno; in make_type()
735 TAILQ_INIT(&t->enums); in make_type()
758 t->is_enum = (*tok == TOK_ENUM); in parse_type()
759 t->is_bits = (*tok == TOK_BITS); in parse_type()
760 t->syntax = syntax; in parse_type()
772 if (*tok == '-') { in parse_type()
775 e->value = -(long)val; in parse_type()
777 e->value = val; in parse_type()
783 e->name = savetok(); in parse_type()
784 TAILQ_INSERT_TAIL(&t->enums, e, link); in parse_type()
807 * Parse the next node (complete with all subnodes)
809 static struct node *
812 struct node *node; in parse() local
813 struct node *sub; in parse() local
816 node = xalloc(sizeof(struct node)); in parse()
817 node->lno = input->lno; in parse()
818 node->flags = 0; in parse()
821 report("'(' expected at begin of node"); in parse()
823 report("node id expected after opening '('"); in parse()
826 node->id = (asn_subid_t)val; in parse()
828 report("node name expected after '(' ID"); in parse()
829 node->name = savetok(); in parse()
835 u_int syntax = parse_type(&tok, NULL, node->name, &subtype); in parse()
839 node->type = NODE_LEAF; in parse()
840 node->u.leaf.func = savetok(); in parse()
841 node->u.leaf.syntax = syntax; in parse()
842 node->u.leaf.subtype = subtype; in parse()
846 node->type = NODE_COLUMN; in parse()
847 node->u.column.syntax = syntax; in parse()
848 node->u.column.subtype = subtype; in parse()
854 node->flags |= (u_int)val; in parse()
860 node->type = NODE_ENTRY; in parse()
861 TAILQ_INIT(&node->u.entry.subs); in parse()
864 node->u.entry.index = 0; in parse()
869 u_int syntax = parse_type(&tok, NULL, node->name, in parse()
873 node->u.entry.subtypes[index_count++] = subtype; in parse()
874 node->u.entry.index |= in parse()
877 node->u.entry.index |= index_count; in parse()
883 node->u.entry.func = savetok(); in parse()
888 sub = parse(tok); in parse()
889 TAILQ_INSERT_TAIL(&node->u.entry.subs, sub, link); in parse()
895 node->type = NODE_TREE; in parse()
896 TAILQ_INIT(&node->u.tree.subs); in parse()
899 sub = parse(tok); in parse()
900 TAILQ_INSERT_TAIL(&node->u.tree.subs, sub, link); in parse()
904 return (node); in parse()
911 static struct node *
926 t->is_enum = (tok == TOK_ENUM); in parse_top()
927 t->is_bits = (tok == TOK_BITS); in parse_top()
929 t->syntax = parse_type(&tok, t, NULL, NULL); in parse_top()
947 * Generate the C-code table part for one node.
950 gen_node(FILE *fp, const struct node *np, struct asn_oid *oid, u_int idx, in gen_node()
954 struct node *sub; in gen_node() local
957 if (oid->len == ASN_MAXOIDLEN) in gen_node()
959 oid->subs[oid->len++] = np->id; in gen_node()
961 if (np->type == NODE_TREE) { in gen_node()
962 TAILQ_FOREACH(sub, &np->u.tree.subs, link) in gen_node()
963 gen_node(fp, sub, oid, 0, NULL); in gen_node()
964 oid->len--; in gen_node()
967 if (np->type == NODE_ENTRY) { in gen_node()
968 TAILQ_FOREACH(sub, &np->u.entry.subs, link) in gen_node()
969 gen_node(fp, sub, oid, np->u.entry.index, in gen_node()
970 np->u.entry.func); in gen_node()
971 oid->len--; in gen_node()
976 if ((np->flags & (FL_GET|FL_SET)) == 0) { in gen_node()
977 oid->len--; in gen_node()
981 fprintf(fp, " {{ %u, {", oid->len); in gen_node()
982 for (n = 0; n < oid->len; n++) in gen_node()
983 fprintf(fp, " %u,", oid->subs[n]); in gen_node()
984 fprintf(fp, " }}, \"%s\", ", np->name); in gen_node()
986 if (np->type == NODE_COLUMN) { in gen_node()
987 syntax = np->u.column.syntax; in gen_node()
990 syntax = np->u.leaf.syntax; in gen_node()
1038 if (np->type == NODE_COLUMN) in gen_node()
1041 fprintf(fp, "%s, ", np->u.leaf.func); in gen_node()
1044 if (np->flags & FL_SET) in gen_node()
1047 oid->len--; in gen_node()
1055 gen_header(FILE *fp, const struct node *np, u_int oidlen, const char *func) in gen_header()
1058 struct node *sub; in gen_header() local
1062 if (np->type == NODE_TREE) { in gen_header()
1063 TAILQ_FOREACH(sub, &np->u.tree.subs, link) in gen_header()
1064 gen_header(fp, sub, oidlen, NULL); in gen_header()
1067 if (np->type == NODE_ENTRY) { in gen_header()
1068 TAILQ_FOREACH(sub, &np->u.entry.subs, link) in gen_header()
1069 gen_header(fp, sub, oidlen, np->u.entry.func); in gen_header()
1073 if((np->flags & (FL_GET|FL_SET)) == 0) in gen_header()
1076 if (np->type == NODE_COLUMN) { in gen_header()
1078 errx(1, "column without function (%s) - probably " in gen_header()
1079 "outside of a table", np->name); in gen_header()
1082 sprintf(f, "%s", np->u.leaf.func); in gen_header()
1085 if (strcmp(ptr->name, f) == 0) in gen_header()
1090 ptr->name = savestr(f); in gen_header()
1098 fprintf(fp, "# define LEAF_%s %u\n", np->name, np->id); in gen_header()
1105 gen_table(FILE *fp, const struct node *node) in gen_table() argument
1130 gen_node(fp, node, &oid, 0, NULL); in gen_table()
1153 gen_tree(const struct node *np, int level) in gen_tree()
1155 const struct node *sp; in gen_tree()
1158 printf("%*s(%u %s", 2 * level, "", np->id, np->name); in gen_tree()
1160 switch (np->type) { in gen_tree()
1163 print_syntax(np->u.leaf.syntax); in gen_tree()
1164 if (np->u.leaf.subtype != NULL) in gen_tree()
1165 printf(" | %s", np->u.leaf.subtype); in gen_tree()
1166 printf(" %s%s%s)\n", np->u.leaf.func, in gen_tree()
1167 (np->flags & FL_GET) ? " GET" : "", in gen_tree()
1168 (np->flags & FL_SET) ? " SET" : ""); in gen_tree()
1172 if (TAILQ_EMPTY(&np->u.tree.subs)) { in gen_tree()
1176 TAILQ_FOREACH(sp, &np->u.tree.subs, link) in gen_tree()
1185 for (i = 0; i < SNMP_INDEX_COUNT(np->u.entry.index); i++) { in gen_tree()
1186 print_syntax(SNMP_INDEX(np->u.entry.index, i)); in gen_tree()
1187 if (np->u.entry.subtypes[i] != NULL) in gen_tree()
1188 printf(" | %s", np->u.entry.subtypes[i]); in gen_tree()
1190 printf(" %s\n", np->u.entry.func); in gen_tree()
1191 TAILQ_FOREACH(sp, &np->u.entry.subs, link) in gen_tree()
1197 print_syntax(np->u.column.syntax); in gen_tree()
1198 if (np->u.column.subtype != NULL) in gen_tree()
1199 printf(" | %s", np->u.column.subtype); in gen_tree()
1200 printf("%s%s)\n", (np->flags & FL_GET) ? " GET" : "", in gen_tree()
1201 (np->flags & FL_SET) ? " SET" : ""); in gen_tree()
1207 extract(FILE *fp, const struct node *np, struct asn_oid *oid, const char *obj, in extract()
1210 struct node *sub; in extract() local
1213 if (oid->len == ASN_MAXOIDLEN) in extract()
1215 oid->subs[oid->len++] = np->id; in extract()
1217 if (strcmp(obj, np->name) == 0) { in extract()
1218 if (oid->len + idx->len >= ASN_MAXOIDLEN) in extract()
1220 fprintf(fp, "#define OID_%s%s\t%u\n", np->name, in extract()
1221 iname ? iname : "", np->id); in extract()
1222 fprintf(fp, "#define OIDLEN_%s%s\t%u\n", np->name, in extract()
1223 iname ? iname : "", oid->len + idx->len); in extract()
1224 fprintf(fp, "#define OIDX_%s%s\t{ %u, {", np->name, in extract()
1225 iname ? iname : "", oid->len + idx->len); in extract()
1226 for (n = 0; n < oid->len; n++) in extract()
1227 fprintf(fp, " %u,", oid->subs[n]); in extract()
1228 for (n = 0; n < idx->len; n++) in extract()
1229 fprintf(fp, " %u,", idx->subs[n]); in extract()
1234 if (np->type == NODE_TREE) { in extract()
1235 TAILQ_FOREACH(sub, &np->u.tree.subs, link) in extract()
1236 if (!extract(fp, sub, oid, obj, idx, iname)) in extract()
1238 } else if (np->type == NODE_ENTRY) { in extract()
1239 TAILQ_FOREACH(sub, &np->u.entry.subs, link) in extract()
1240 if (!extract(fp, sub, oid, obj, idx, iname)) in extract()
1243 oid->len--; in extract()
1248 gen_extract(FILE *fp, const struct node *root, char *object) in gen_extract()
1303 check_sub_order(const struct node *np, const struct node_list *subs) in check_sub_order()
1306 const struct node *sub; in check_sub_order() local
1311 TAILQ_FOREACH(sub, subs, link) { in check_sub_order()
1312 if (!first && sub->id <= maxid) in check_sub_order()
1313 report_node(np, "subids not ordered at %s", sub->name); in check_sub_order()
1314 maxid = sub->id; in check_sub_order()
1323 check_tree(struct node *np) in check_tree()
1325 struct node *sub; in check_tree() local
1327 if (np->type == NODE_LEAF || np->type == NODE_COLUMN) { in check_tree()
1328 if ((np->flags & (FL_GET|FL_SET)) != 0) in check_tree()
1333 if (np->type == NODE_ENTRY) { in check_tree()
1334 check_sub_order(np, &np->u.entry.subs); in check_tree()
1337 TAILQ_FOREACH(sub, &np->u.entry.subs, link) { in check_tree()
1338 if (sub->type != NODE_COLUMN) in check_tree()
1340 "a column", sub->name); in check_tree()
1341 check_tree(sub); in check_tree()
1344 check_sub_order(np, &np->u.tree.subs); in check_tree()
1346 TAILQ_FOREACH(sub, &np->u.tree.subs, link) in check_tree()
1347 check_tree(sub); in check_tree()
1354 struct node *n1, *n2; in merge_subs()
1361 if (n1->id >= n2->id) in merge_subs()
1365 else if (n1->id > n2->id) in merge_subs()
1368 if (n1->type == NODE_TREE && n2->type == NODE_TREE) { in merge_subs()
1369 if (strcmp(n1->name, n2->name) != 0) in merge_subs()
1371 "same name '%s' '%s'", n1->name, in merge_subs()
1372 n2->name); in merge_subs()
1373 merge_subs(&n1->u.tree.subs, &n2->u.tree.subs); in merge_subs()
1375 } else if (n1->type == NODE_ENTRY && in merge_subs()
1376 n2->type == NODE_ENTRY) { in merge_subs()
1377 if (strcmp(n1->name, n2->name) != 0) in merge_subs()
1379 "same name '%s' '%s'", n1->name, in merge_subs()
1380 n2->name); in merge_subs()
1381 if (n1->u.entry.index != n2->u.entry.index) in merge_subs()
1383 "same index '%s'", n1->name); in merge_subs()
1384 if (strcmp(n1->u.entry.func, in merge_subs()
1385 n2->u.entry.func) != 0) in merge_subs()
1387 "same op '%s'", n1->name); in merge_subs()
1388 merge_subs(&n1->u.entry.subs, in merge_subs()
1389 &n2->u.entry.subs); in merge_subs()
1394 n1->name, n2->name); in merge_subs()
1400 merge(struct node **root, struct node *t) in merge()
1411 if ((*root)->type != NODE_TREE) in merge()
1413 if (t->type != NODE_TREE) in merge()
1415 if ((*root)->id != t->id) in merge()
1418 merge_subs(&(*root)->u.tree.subs, &t->u.tree.subs); in merge()
1426 if (*s == '-') in unminus()
1442 * \param ccode generate externally visible non-inline functions
1452 fprintf(fp, "isok_%s(enum %s s)\n", t->name, t->name); in gen_enum_funcs()
1457 TAILQ_FOREACH(e, &t->enums, link) { in gen_enum_funcs()
1458 fprintf(fp, "\t case %s_", t->name); in gen_enum_funcs()
1459 unminus(fp, e->name); in gen_enum_funcs()
1471 fprintf(fp, "tostr_%s(enum %s s)\n", t->name, t->name); in gen_enum_funcs()
1473 fprintf(fp, " static const char *vals[] = { STRING_%s };\n", t->name); in gen_enum_funcs()
1475 fprintf(fp, " if (isok_%s(s))\n", t->name); in gen_enum_funcs()
1476 fprintf(fp, " return (vals[(int)s - STROFF_%s]);\n", t->name); in gen_enum_funcs()
1477 fprintf(fp, " return (\"%s???\");\n", t->name); in gen_enum_funcs()
1484 t->name, t->name); in gen_enum_funcs()
1486 fprintf(fp, " static const char *vals[] = { STRING_%s };\n", t->name); in gen_enum_funcs()
1490 fprintf(fp, " *s = i + STROFF_%s;\n", t->name); in gen_enum_funcs()
1513 fprintf(fp, "#ifndef %s_defined__\n", t->name); in gen_enum()
1514 fprintf(fp, "#define %s_defined__\n", t->name); in gen_enum()
1516 fprintf(fp, " * From %s:%u\n", t->from_fname, t->from_lno); in gen_enum()
1518 fprintf(fp, "enum %s {\n", t->name); in gen_enum()
1519 TAILQ_FOREACH(e, &t->enums, link) { in gen_enum()
1520 fprintf(fp, "\t%s_", t->name); in gen_enum()
1521 unminus(fp, e->name); in gen_enum()
1522 fprintf(fp, " = %ld,\n", e->value); in gen_enum()
1523 if (e->value < min) in gen_enum()
1524 min = e->value; in gen_enum()
1527 fprintf(fp, "#define STROFF_%s %ld\n", t->name, min); in gen_enum()
1528 fprintf(fp, "#define STRING_%s \\\n", t->name); in gen_enum()
1529 TAILQ_FOREACH(e, &t->enums, link) { in gen_enum()
1530 fprintf(fp, "\t[%ld] = \"%s_", e->value - min, t->name); in gen_enum()
1531 unminus(fp, e->name); in gen_enum()
1543 fprintf(fp, "#endif /* %s_defined__ */\n", t->name); in gen_enum()
1558 if ((t->is_enum || t->is_bits) && strcmp(t->name, name) == 0) { in gen_enum_funcs_str()
1563 return (-1); in gen_enum_funcs_str()
1570 * \param ccode generate externally visible non-inline functions
1578 if (t->is_enum || t->is_bits) in gen_all_enum_funcs()
1588 if (t->is_enum || t->is_bits) in gen_enums()
1600 * \return 0 if found, -1 otherwise
1608 if ((t->is_enum || t->is_bits) && strcmp(t->name, name) == 0) { in extract_enum()
1612 return (-1); in extract_enum()
1628 if (t->is_enum || t->is_bits) in extract_all_enums()
1669 make_table(const struct node *root, int gen_funcs) in make_table()
1713 errx(1, "-E conflicts with earlier options"); in main()
1719 errx(1, "-e conflicts with earlier options"); in main()
1726 errx(1, "-F conflicts with -f"); in main()
1733 errx(1, "-f conflicts with -F"); in main()
1762 errx(1, "-t conflicts with earlier options"); in main()
1767 argc -= optind; in main()
1781 struct node *root = parse_top(gettoken()); in main()
1795 errx(1, "-e requires arguments"); in main()
1808 errx(1, "-t allows no arguments"); in main()