const char * cvt_number(const char *buf, u_int64_t *nump) { int base; unsigned char c; base = 10; *nump = 0; c = *buf; if (c == '0') { c = *(++buf); if (c == 'x' || c == 'X') { base = 16; buf++; } else { base = 8; } } for (c = *buf; c != '\0' && !isspace(c); c = *(++buf)) { switch (base) { case 10: if (c < '0' || c > '9') goto done; } } done: } int dispatch_cmd(const char *buf, const struct cmdtab *cmds) { const struct cmdtab *try, *winner; size_t nonwhitespace, i; unsigned int nmatches; const char *pre, *post; int rv; /* advance past white space. */ buf = advance_past_space(buf); if (buf == NULL) return (DISPATCH_CMD_NOCMD); /* find how much non-white space there is. */ nonwhitespace = 0; while ((buf[nonwhitespace] != '\0') && !isspace(buf[nonwhitespace])) nonwhitespace++; /* at this point, nonwhitespace should always be non-zero */ if (nonwhitespace == 0) { printf("assertion failed: dispatch_cmd: nonwhitespace == 0\n"); halt(); } /* see how many matches there were. */ for (nmatches = 0, try = cmds; try != NULL && try->cmd != NULL; try++) { if (strncmp(buf, try->cmd, nonwhitespace) == 0) { winner = try; nmatches++; } } if (nmatches == 1) { (*winner->fn)(buf + nonwhitespace); return (DISPATCH_CMD_MATCHED); } else if (nmatches == 0) { pre = "invalid command word"; post = "allowed words"; rv = DISPATCH_CMD_NOMATCH; } else { pre = "ambiguous command word"; post = "matches"; rv = DISPATCH_CMD_AMBIGUOUS; } printf("%s \"", pre); print_stringarray(buf, nonwhitespace); printf("\", %s:\n", post); /* print commands. if no match, print all commands. */ print_cmds(cmds, buf, rv == DISPATCH_CMD_NOMATCH ? 0 : nonwhitespace); return (rv); } void print_cmds(const struct cmdtab *cmds, const char *match, size_t matchlen) { const struct cmdtab *try; printf(" "); for (try = cmds; try != NULL && try->cmd != NULL; try++) { if (strncmp(match, try->cmd, matchlen) == 0) printf("%s%s", try != cmds ? ", " : "", try->cmd); } printf("\n"); } void print_stringarray(const char *s, size_t maxlen) { size_t i; for (i = 0; (i < maxlen) && (*s != '\0'); i++, s++) putchar(*s); } void warn_ignored_args(const char *buf, const char *cmd) { if (advance_past_space(buf) != NULL) printf("WARNING: extra arguments to \"%s\" command ignored\n", cmd); } /* * Top-level Commands */ void toplevel_dpb(const char *buf) { u_int64_t startaddr, count = 1; buf = advance_past_space(buf); if (buf == NULL) { printf("\"dpb\" must be given starting address\n"); return; } buf = cvt_number(buf, &startaddr); if (*buf != '\0' && !isspace(*buf)) { printf("bad character '%c' in starting address\n"); return; } buf = advance_past_space(buf); if (buf != NULL) { buf = cvt_number(buf, &count); if (*buf != '\0' && !isspace(*buf)) { printf("bad character '%c' in count\n"); return; } buf = advance_past_space(buf); if (buf != NULL) { printf("extra args at end of \"dpb\" command\n"); return; } } printf("startaddr = 0x%lx, count = 0x%lx\n", startaddr, count); printf("\"dpb\" not yet implemented\n"); } void toplevel_dpl(const char *buf) { printf("\"dpl\" not yet implemented\n"); } void toplevel_dpq(const char *buf) { printf("\"dpq\" not yet implemented\n"); }
int read_item( int parent, void (*func)(void)) { /** Function: General function to read an item and fill in appropriate global variables Input: File descriptor for input Outputs: Sets option flags Fills in tag, type, min, max, itemname, subclass, subtype, defaultname and numstring Procedure: 1. WHILE token is neither ',' NOR '}' IF token is '[', get tag ELSE IF token is '(' IF enumerated flag set, get material for tag or sub_val ELSE Get min-max ELSE IF token is CHOICE, set constructed bit in type ELSE IF token is COMPONENTS, do components stuff ELSE IF token is DEFAULT, make defaultname and set default flag ELSE IF token is DEFINED, do the defined thing to fill in defined_by ELSE IF token is EMPTY, do nothing ELSE IF token is EXPLICIT, set temporary explicit flag ELSE IF token is FUNCTION Set type Get all tokens up to comma or right brace ELSE IF token is IMPLICIT, clear temporary explicit flag ELSE IF token is OF, error ELSE IF token is OPTIONAL, set OPTIONAL flag in options ELSE IF token is SIZE, get min-max ELSE IF token is TABLE Get table name to skip it Set table variable ELSE IF token is TAGS OR UNIQUE, swallow it ELSE IF token is a defined type IF this is a table AND (there's a type OR a subclass already) append token to alt_subclasses ELSE IF have a subclass already, syntax error ELSE IF have no type yet, use that ELSE IF explicit tagging, set subtype ELSE 'Or' the constructed bit into type Get expected sequel to token, if any ELSE IF token begins with a number IF TABLE bit is set, convert number ELSE IF enumerated flag is set Put token into itemname prefixed with e Set enumerated flag ELSE IF in a table AND (token is TRUE OR FALSE) Make type boolean Put token in subclass ELSE IF name begins with a capital letter IF this is a table item AND (there is already a subclass OR a type) Append this to the alt_subclasses ELSE IF there is already a subclass OR a type, syntax error ELSE set the subclass and option from the token ELSE IF name begins with a lower-case letter IF this is a table item, increment the array count IF no itemname so far, set token in itemname with options IF no next token, return -1 2. IF token is '}' Peek at the next token IF it's '(', set the constrained flag Return 0 **/ char *c; long tmp; struct name_table *ntbp; struct id_table *idp = (struct id_table *)0; int parens; struct alt_subclass *altscp = (struct alt_subclass *)0; while (*token != ',' && *token != '}') { if (*token == '[') { tmp = get_tag(token); if (tag >= 0) tag |= tmp; else tag = tmp; } else if (*token == '(') { if ((flags & ASN_ENUM_FLAG)) { if (!get_token(0, token)) syntax(itemname); if (*(c = token) == '_') c++; if (find_name(classname)->type == ASN_OBJ_ID) { if (*c > '2') { if (!(idp = find_id(c))) syntax(c); strcpy(c, idp->val); idp = (struct id_table *)0; } sub_val = (char *)calloc(1, strlen(c) + 1); strcpy(sub_val, c); } else if (*c >= '0' && *c <= '9') { for (integer_val = 0; *c; integer_val = (integer_val * 10) + *c++ - '0'); if (*token == '_') integer_val = -integer_val; } else { integer_val = find_ub(token); add_child(token, parent, 0, (ulong) - 1, 0); } if (!get_known(0, token, ")")) syntax(itemname); } else get_paren(token, &min, &max, parent); } else if (!strcmp(token, choice_w)) { if (tag >= 0) tag |= ASN_CONSTRUCTED; else if (type >= 0) type |= ASN_CONSTRUCTED; else type = ASN_CONSTRUCTED; } else if (!strcmp(token, components_w)) do_components(func); else if (!strcmp(token, default_w)) { option |= (ASN_OPTIONAL_FLAG | ASN_DEFAULT_FLAG); get_token(0, token); c = defaultname; if (*token >= '0' && *token <= '9') *c++ = '0'; else if (!strcmp(token, empty_w) || !strcmp(token, null_w) || (*token == '"' && token[1] == '"')) cat(token, "{"); strcpy(c, token); } else if (!strcmp(token, defined_w)) { do_defined(); } else if (!strcmp(token, empty_w)); else if (!strcmp(token, explicit_w)) explicit1 |= 1; else if (!strcmp(token, function_w)) { type = ASN_FUNCTION; for (c = itemname, parens = 0; get_must(0, token) && (parens || (*token != ',' && *token != '}'));) { if (*token == '(') parens++; else if (*token == ')') parens--; c = cat(cat(c, token), " "); } break; } else if (!strcmp(token, implicit_w)) { explicit1 &= ~1; if (*subclass && tag < ASN_APPL_SPEC && (ntbp = find_name(subclass)) && ntbp->name) { if (type < 0) type = ntbp->type; else if (ntbp->type != -1) type |= (ntbp->type & ASN_CONSTRUCTED); } } else if (!strcmp(token, in_w)) { if (!*definer) syntax(token); if (!get_token(0, inclass)) syntax(definer); } else if (!strcmp(token, of_w)) syntax(token); else if (!strcmp(token, optional_w)) { option |= ASN_OPTIONAL_FLAG; if (altscp) { altscp->options = option; option &= ~(ASN_OPTIONAL_FLAG); } } else if (!strcmp(token, size_w)) { get_size(token, &min, &max, parent); // if (type == ASN_UTCTIME || type == ASN_GENTIME) min = max = 0; } else if (!strcmp(token, table_w)) { if (!get_token(0, tablename)) syntax(table_w); if (*tablename < 'A' || *tablename > 'Z') syntax(tablename); option |= ASN_TABLE_FLAG; } else if (!strcmp(token, tags_w) || !strcmp(token, unique_w)); else if ((tmp = (int)find_type(token)) != ASN_NOTYPE) { if ((flags & ASN_TABLE_FLAG) && (*subclass || type >= 0)) altscp = append_subclasses(token); else if (*subclass) syntax(token); if (type < 0) type = (ulong) tmp; else if ((explicit1 & 1)) subtype = (short)tmp; else if (tmp > 0) type |= (tmp & ASN_CONSTRUCTED); get_expected(0, tmp, token); } else if ((*token >= '0' && *token <= '9') || (*itemname && (idp = find_id(token))) || is_ub(token)) { if (*token > '9') { if (idp) cat(token, idp->val); else { add_name(token, -1, 0); /* keep it off the 'Defined * but not used' list */ sprintf(token, "%ld", find_ub(token)); } } if ((flags & ASN_TABLE_FLAG)) { if (*token >= '0' && *token <= '9') cvt_number(numstring, token); } else if ((flags & ASN_ENUM_FLAG)) { if (!*itemname) cat(cat(itemname, "e"), token); else { if (*(c = token) == '_') c++; for (integer_val = 0; *c; integer_val = (integer_val * 10) + *c++ - '0'); if (*token == '_') integer_val = -integer_val; } } idp = (struct id_table *)0; } else if ((flags & ASN_TABLE_FLAG) && (!strcmp(token, false_w) || !strcmp(token, true_w) || !strcmp(token, either_w))) { type = ASN_BOOLEAN; cat(subclass, token); } else if ((*token >= 'A' && *token <= 'Z') || *token == '*') { if ((flags & ASN_TABLE_FLAG)) { if (!strcmp(token, "NONE")) cat(token, "AsnNone"); if (*subclass || type > 0) altscp = append_subclasses(token); else option |= set_name_option(subclass, token); } else if ((type > 0 && type < ASN_CHOICE) || *subclass) syntax(token); else option |= set_name_option(subclass, token); } else if (*token >= 'a' && *token <= 'z') { if ((flags & ASN_TABLE_FLAG)) array++; if (*itemname) warn(MSG_SYNTAX_ERR, token); else cat(itemname, token); } if (!get_token(0, token)) return -1; } if (*definer && !*inclass) cat(inclass, classname); if (*token == '}' && *peek_token(0) == '(') { get_known(0, &token[2], "("); get_def_paren(&token[2]); } return 0; }