/* >> TOK_STRING | TOK_INTEGER | TOK_BOOLEAN | TOK_KEYWORD | '{' */ static int parse_operand (section_t **section,scan_t *sc,char **keyword) { int result; SCAN(); if (!result) { parse_error (sc,"operand",0); return (ERR); } if (result == '{') return (parse_array_or_section (section,sc,keyword)); if (result == TOK_STRING || result == TOK_INTEGER || result == TOK_BOOLEAN || result == TOK_KEYWORD) { stmt_t *nw; if ((nw = stmt_init (keyword,result)) == NULL) return (ERR); if (stmt_add (nw,sc) < 0) { stmt_destroy (&nw); return (ERR); } stmt_save (*section,&nw); return (OK); } parse_error (sc,"TOK_STRING, TOK_INTEGER, TOK_BOOLEAN, TOK_ENUM, or '{'",result); return (ERR); }
/* * free all memory occupied by the specified parse structure */ void parse_destroy (section_t **section) { if (section != NULL && *section != NULL) { int i; if ((*section)->name != NULL) mem_free ((*section)->name); if ((*section)->stmt != NULL) { stmt_t *tmp,*prev = (*section)->stmt; do { tmp = (*section)->stmt->next; stmt_destroy (&(*section)->stmt); (*section)->stmt = tmp; } while (prev != (*section)->stmt); (*section)->stmt = NULL; } for (i = 0; i < (*section)->n; i++) if ((*section)->child[i] != NULL) parse_destroy (&(*section)->child[i]); if ((*section)->n) mem_free ((*section)->child); mem_free (*section); *section = NULL; } }
/* * value = TOK_STRING | TOK_INTEGER | TOK_BOOLEAN | TOK_KEYWORD * >> value (',' value)* '}' */ static int parse_array_or_section (section_t **section,scan_t *sc,char **keyword) { int result,saved = 0; stmt_t *nw = NULL; SCAN(); if (result == TOK_KEYWORD) { char *tmp = sc->token.keyword; if ((result = scan (sc)) < 0) { mem_free (tmp); return (ERR); } saved = result; if (result == '=') { if ((*section)->child == NULL) { if (((*section)->child = (section_t **) mem_alloc (sizeof (section_t *))) == NULL) { out_of_memory: log_printf (LOG_ERROR,"Out of memory\n"); mem_free (tmp); return (ERR); } if (((*section)->child[0] = (section_t *) mem_alloc (sizeof (section_t))) == NULL) { mem_free ((*section)->child); (*section)->child = NULL; goto out_of_memory; } } else { section_t **ptr; if ((ptr = (section_t **) mem_realloc ((*section)->child,((*section)->n + 1) * sizeof (section_t *))) == NULL) goto out_of_memory; (*section)->child = ptr; if (((*section)->child[(*section)->n] = (section_t *) mem_alloc (sizeof (section_t))) == NULL) goto out_of_memory; } (*section)->child[(*section)->n]->n = 0; (*section)->child[(*section)->n]->name = *keyword; (*section)->child[(*section)->n]->stmt = NULL; (*section)->child[(*section)->n]->child = NULL; ((*section)->n)++; return (parse_section (&(*section)->child[(*section)->n - 1],sc,&tmp)); } result = TOK_KEYWORD; sc->token.keyword = tmp; } if (result != TOK_STRING && result != TOK_INTEGER && result != TOK_BOOLEAN && result != TOK_KEYWORD) { mem_free (*keyword); missing_value: parse_error (sc,"TOK_STRING, TOK_INTEGER, TOK_BOOLEAN, or TOK_ENUM",result); goto error; } if ((nw = stmt_init (keyword,result)) == NULL) { unable_to_add: if (result == TOK_STRING) mem_free (sc->token.string); else if (result == TOK_KEYWORD) mem_free (sc->token.keyword); goto error; } if (stmt_add (nw,sc) < 0) goto unable_to_add; if (result == TOK_KEYWORD) result = saved; else if ((result = scan (sc)) < 0) goto error; while (result == ',') { if ((result = scan (sc)) < 0) goto error; if (result != TOK_STRING && result != TOK_INTEGER && result != TOK_BOOLEAN && result != TOK_KEYWORD) goto missing_value; if (result != nw->type && (result != TOK_KEYWORD || nw->type != TOK_ENUM)) { type_mismatch (sc,nw->type,result); goto error; } if (stmt_add (nw,sc) < 0) goto unable_to_add; if ((result = scan (sc)) < 0) goto error; } if (result != '}') { parse_error (sc,"'}'",result); goto error; } stmt_save (*section,&nw); return (OK); error: if (nw != NULL) stmt_destroy (&nw); return (ERR); }
int main(int argc, char *argv[]) { int s; struct Stmt *stmt = 0; s = vos_init(argc, argv); if (s) goto err; s = vos_parsing(&stmt, _vos.script); if (s) goto err; if (! (_vos.debug & DBG_SCRIPT)) s = vos_process(stmt); if (! s) goto out; err: switch (s) { case E_VOS_PARAM: case E_FILE_NOT_OPEN: case E_MEM: vos_error0(s); break; case E_FILE_OPEN: case E_FILE_EXIST: case E_FILE_NOT_EXIST: case E_FILE_SEEK: case E_FILE_READ: case E_FILE_WRITE: vos_error1(s, _vos.e_sparm0); break; case E_PARSER_INV_POS: vos_error1(s, _vos.e_nparm0); break; case E_PARSER_UNX_CHAR: vos_error2(s, _vos.e_nparm0, _vos.e_nparm1); break; case E_PARSER_UNX_TOKEN: case E_PARSER_UNK_TOKEN: case E_PARSER_UNK_FIELDNAME: case E_PARSER_INV_FIELDNAME: case E_PARSER_INV_VALUE: case E_PARSER_INV_STMT: case E_PARSER_AMB_FIELDNAME: vos_error2(s, _vos.e_nparm0, _vos.e_sparm0); break; case E_FILE_END: break; default: fprintf(stderr, "vos error number : %d\n", s); } if (_vos.e_sparm0) free(_vos.e_sparm0); if (_vos.e_sparm1) free(_vos.e_sparm1); out: if (_vos.proc_tmp_dir) ll_destroy(&_vos.proc_tmp_dir); pthread_mutex_destroy(&_vos.proc_tmp_dir_lock); stmt_destroy(&stmt); return s; }