X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcommands_parser.c;h=f0459220fe3ef4c4a3115dbea62637dcaea2526a;hb=e114b3dba2485606cf5a3d044c0de8430c9f80b4;hp=af0afe02a69b80cb13d3eb0d50e2dc30661333ac;hpb=8d72a77c7a56fa788d29a54455c806f18e2a3fd5;p=i3%2Fi3 diff --git a/src/commands_parser.c b/src/commands_parser.c index af0afe02..f0459220 100644 --- a/src/commands_parser.c +++ b/src/commands_parser.c @@ -31,7 +31,6 @@ #include #include "all.h" -#include "queue.h" /******************************************************************************* * The data structures used for parsing. Essentially the current state and a @@ -174,7 +173,8 @@ static cmdp_state state; #ifndef TEST_PARSER static Match current_match; #endif -static char *json_output; +static struct CommandResult subcommand_output; +static struct CommandResult command_output; #include "GENERATED_call.h" @@ -183,7 +183,24 @@ static void next_state(const cmdp_token *token) { if (token->next_state == __CALL) { DLOG("should call stuff, yay. call_id = %d\n", token->extra.call_identifier); - json_output = GENERATED_call(token->extra.call_identifier); + subcommand_output.json_output = NULL; + subcommand_output.needs_tree_render = false; + GENERATED_call(token->extra.call_identifier, &subcommand_output); + if (subcommand_output.json_output) { + DLOG("Subcommand JSON output: %s\n", subcommand_output.json_output); + char *buffer; + /* In the beginning, the contents of json_output are "[\0". */ + if (command_output.json_output[1] == '\0') + sasprintf(&buffer, "%s%s", command_output.json_output, subcommand_output.json_output); + else sasprintf(&buffer, "%s, %s", command_output.json_output, subcommand_output.json_output); + free(command_output.json_output); + command_output.json_output = buffer; + DLOG("merged command JSON output: %s\n", command_output.json_output); + } + /* If any subcommand requires a tree_render(), we need to make the + * whole parser result request a tree_render(). */ + if (subcommand_output.needs_tree_render) + command_output.needs_tree_render = true; clear_stack(); return; } @@ -195,10 +212,11 @@ static void next_state(const cmdp_token *token) { } /* TODO: Return parsing errors via JSON. */ -char *parse_command(const char *input) { +struct CommandResult *parse_command(const char *input) { DLOG("new parser handling: %s\n", input); state = INITIAL; - json_output = NULL; + command_output.json_output = sstrdup("["); + command_output.needs_tree_render = false; const char *walk = input; const size_t len = strlen(input); @@ -208,14 +226,15 @@ char *parse_command(const char *input) { // TODO: make this testable #ifndef TEST_PARSER - cmd_criteria_init(¤t_match); + cmd_criteria_init(¤t_match, &subcommand_output); #endif /* The "<=" operator is intentional: We also handle the terminating 0-byte * explicitly by looking for an 'end' token. */ while ((walk - input) <= len) { - /* skip whitespace before every token */ - while ((*walk == ' ' || *walk == '\t') && *walk != '\0') + /* skip whitespace and newlines before every token */ + while ((*walk == ' ' || *walk == '\t' || + *walk == '\r' || *walk == '\n') && *walk != '\0') walk++; DLOG("remaining input = %s\n", walk); @@ -232,7 +251,7 @@ char *parse_command(const char *input) { if (strncasecmp(walk, token->name + 1, strlen(token->name) - 1) == 0) { DLOG("found literal, moving to next state\n"); if (token->identifier != NULL) - push_string(token->identifier, strdup(token->name + 1)); + push_string(token->identifier, sstrdup(token->name + 1)); walk += strlen(token->name) - 1; next_state(token); token_handled = true; @@ -249,27 +268,43 @@ char *parse_command(const char *input) { if (*walk == '"') { beginning++; walk++; - while (*walk != '"' || *(walk-1) == '\\') + while (*walk != '\0' && (*walk != '"' || *(walk-1) == '\\')) walk++; } else { if (token->name[0] == 's') { /* For a string (starting with 's'), the delimiters are * comma (,) and semicolon (;) which introduce a new - * operation or command, respectively. */ - while (*walk != ';' && *walk != ',' && *walk != '\0') + * operation or command, respectively. Also, newlines + * end a command. */ + while (*walk != ';' && *walk != ',' && + *walk != '\0' && *walk != '\r' && + *walk != '\n') walk++; } else { /* For a word, the delimiters are white space (' ' or * '\t'), closing square bracket (]), comma (,) and * semicolon (;). */ - while (*walk != ' ' && *walk != '\t' && *walk != ']' && - *walk != ',' && *walk != ';' && *walk != '\0') + while (*walk != ' ' && *walk != '\t' && + *walk != ']' && *walk != ',' && + *walk != ';' && *walk != '\r' && + *walk != '\n' && *walk != '\0') walk++; } } if (walk != beginning) { - char *str = calloc(walk-beginning + 1, 1); - strncpy(str, beginning, walk-beginning); + char *str = scalloc(walk-beginning + 1); + /* We copy manually to handle escaping of characters. */ + int inpos, outpos; + for (inpos = 0, outpos = 0; + inpos < (walk-beginning); + inpos++, outpos++) { + /* We only handle escaped double quotes to not break + * backwards compatibility with people using \w in + * regular expressions etc. */ + if (beginning[inpos] == '\\' && beginning[inpos+1] == '"') + inpos++; + str[outpos] = beginning[inpos]; + } if (token->identifier) push_string(token->identifier, str); DLOG("str is \"%s\"\n", str); @@ -296,7 +331,7 @@ char *parse_command(const char *input) { // TODO: make this testable #ifndef TEST_PARSER if (*walk == '\0' || *walk == ';') - cmd_criteria_init(¤t_match); + cmd_criteria_init(¤t_match, &subcommand_output); #endif walk++; break; @@ -315,7 +350,7 @@ char *parse_command(const char *input) { * full input, and underline the position where the parser * currently is. */ char *errormessage; - char *possible_tokens = malloc(tokenlen + 1); + char *possible_tokens = smalloc(tokenlen + 1); char *tokenwalk = possible_tokens; for (c = 0; c < ptr->n; c++) { token = &(ptr->array[c]); @@ -340,13 +375,13 @@ char *parse_command(const char *input) { } } *tokenwalk = '\0'; - asprintf(&errormessage, "Expected one of these tokens: %s", - possible_tokens); + sasprintf(&errormessage, "Expected one of these tokens: %s", + possible_tokens); free(possible_tokens); /* Contains the same amount of characters as 'input' has, but with * the unparseable part highlighted using ^ characters. */ - char *position = malloc(len + 1); + char *position = smalloc(len + 1); for (const char *copywalk = input; *copywalk != '\0'; copywalk++) position[(copywalk - input)] = (copywalk >= walk ? '^' : ' '); position[len] = '\0'; @@ -361,8 +396,13 @@ char *parse_command(const char *input) { } } - DLOG("json_output = %s\n", json_output); - return json_output; + char *buffer; + sasprintf(&buffer, "%s]", command_output.json_output); + free(command_output.json_output); + command_output.json_output = buffer; + DLOG("command_output.json_output = %s\n", command_output.json_output); + DLOG("command_output.needs_tree_render = %d\n", command_output.needs_tree_render); + return &command_output; } /******************************************************************************* @@ -382,8 +422,8 @@ void debuglog(uint64_t lev, char *fmt, ...) { va_list args; va_start(args, fmt); - fprintf(stdout, "# "); - vfprintf(stdout, fmt, args); + fprintf(stderr, "# "); + vfprintf(stderr, fmt, args); va_end(args); }