/* * The following function sends a new window event, which consists * of fields "change" and "container", the latter containing a dump * of the window's container. * */ static void ipc_send_window_new_event(Con *con) { setlocale(LC_NUMERIC, "C"); yajl_gen gen = ygenalloc(); y(map_open); ystr("change"); ystr("new"); ystr("container"); dump_node(gen, con, false); y(map_close); const unsigned char *payload; ylength length; y(get_buf, &payload, &length); ipc_send_event("window", I3_IPC_EVENT_WINDOW, (const char *)payload); y(free); setlocale(LC_NUMERIC, ""); }
struct ConfigResultIR *parse_config(const char *input, struct context *context) { /* Dump the entire config file into the debug log. We cannot just use * DLOG("%s", input); because one log message must not exceed 4 KiB. */ const char *dumpwalk = input; int linecnt = 1; while (*dumpwalk != '\0') { char *next_nl = strchr(dumpwalk, '\n'); if (next_nl != NULL) { DLOG("CONFIG(line %3d): %.*s\n", linecnt, (int)(next_nl - dumpwalk), dumpwalk); dumpwalk = next_nl + 1; } else { DLOG("CONFIG(line %3d): %s\n", linecnt, dumpwalk); break; } linecnt++; } state = INITIAL; statelist_idx = 1; /* A YAJL JSON generator used for formatting replies. */ command_output.json_gen = yajl_gen_alloc(NULL); y(array_open); const char *walk = input; const size_t len = strlen(input); int c; const cmdp_token *token; bool token_handled; linecnt = 1; // TODO: make this testable #ifndef TEST_PARSER cfg_criteria_init(¤t_match, &subcommand_output, INITIAL); #endif /* The "<=" operator is intentional: We also handle the terminating 0-byte * explicitly by looking for an 'end' token. */ while ((size_t)(walk - input) <= len) { /* Skip whitespace before every token, newlines are relevant since they * separate configuration directives. */ while ((*walk == ' ' || *walk == '\t') && *walk != '\0') walk++; //printf("remaining input: %s\n", walk); cmdp_token_ptr *ptr = &(tokens[state]); token_handled = false; for (c = 0; c < ptr->n; c++) { token = &(ptr->array[c]); /* A literal. */ if (token->name[0] == '\'') { if (strncasecmp(walk, token->name + 1, strlen(token->name) - 1) == 0) { if (token->identifier != NULL) push_string(token->identifier, token->name + 1); walk += strlen(token->name) - 1; next_state(token); token_handled = true; break; } continue; } if (strcmp(token->name, "number") == 0) { /* Handle numbers. We only accept decimal numbers for now. */ char *end = NULL; errno = 0; long int num = strtol(walk, &end, 10); if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) || (errno != 0 && num == 0)) continue; /* No valid numbers found */ if (end == walk) continue; if (token->identifier != NULL) push_long(token->identifier, num); /* Set walk to the first non-number character */ walk = end; next_state(token); token_handled = true; break; } if (strcmp(token->name, "string") == 0 || strcmp(token->name, "word") == 0) { const char *beginning = walk; /* Handle quoted strings (or words). */ if (*walk == '"') { beginning++; walk++; while (*walk != '\0' && (*walk != '"' || *(walk - 1) == '\\')) walk++; } else { if (token->name[0] == 's') { while (*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 != '\r' && *walk != '\n' && *walk != '\0') walk++; } } if (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); free(str); /* If we are at the end of a quoted string, skip the ending * double quote. */ if (*walk == '"') walk++; next_state(token); token_handled = true; break; } } if (strcmp(token->name, "line") == 0) { while (*walk != '\0' && *walk != '\n' && *walk != '\r') walk++; next_state(token); token_handled = true; linecnt++; walk++; break; } if (strcmp(token->name, "end") == 0) { //printf("checking for end: *%s*\n", walk); if (*walk == '\0' || *walk == '\n' || *walk == '\r') { next_state(token); token_handled = true; /* To make sure we start with an appropriate matching * datastructure for commands which do *not* specify any * criteria, we re-initialize the criteria system after * every command. */ // TODO: make this testable #ifndef TEST_PARSER cfg_criteria_init(¤t_match, &subcommand_output, INITIAL); #endif linecnt++; walk++; break; } } } if (!token_handled) { /* Figure out how much memory we will need to fill in the names of * all tokens afterwards. */ int tokenlen = 0; for (c = 0; c < ptr->n; c++) tokenlen += strlen(ptr->array[c].name) + strlen("'', "); /* Build up a decent error message. We include the problem, the * full input, and underline the position where the parser * currently is. */ char *errormessage; char *possible_tokens = smalloc(tokenlen + 1); char *tokenwalk = possible_tokens; for (c = 0; c < ptr->n; c++) { token = &(ptr->array[c]); if (token->name[0] == '\'') { /* A literal is copied to the error message enclosed with * single quotes. */ *tokenwalk++ = '\''; strcpy(tokenwalk, token->name + 1); tokenwalk += strlen(token->name + 1); *tokenwalk++ = '\''; } else { /* Skip error tokens in error messages, they are used * internally only and might confuse users. */ if (strcmp(token->name, "error") == 0) continue; /* Any other token is copied to the error message enclosed * with angle brackets. */ *tokenwalk++ = '<'; strcpy(tokenwalk, token->name); tokenwalk += strlen(token->name); *tokenwalk++ = '>'; } if (c < (ptr->n - 1)) { *tokenwalk++ = ','; *tokenwalk++ = ' '; } } *tokenwalk = '\0'; sasprintf(&errormessage, "Expected one of these tokens: %s", possible_tokens); free(possible_tokens); /* Go back to the beginning of the line */ const char *error_line = start_of_line(walk, input); /* Contains the same amount of characters as 'input' has, but with * the unparseable part highlighted using ^ characters. */ char *position = scalloc(strlen(error_line) + 1); const char *copywalk; for (copywalk = error_line; *copywalk != '\n' && *copywalk != '\r' && *copywalk != '\0'; copywalk++) position[(copywalk - error_line)] = (copywalk >= walk ? '^' : (*copywalk == '\t' ? '\t' : ' ')); position[(copywalk - error_line)] = '\0'; ELOG("CONFIG: %s\n", errormessage); ELOG("CONFIG: (in file %s)\n", context->filename); char *error_copy = single_line(error_line); /* Print context lines *before* the error, if any. */ if (linecnt > 1) { const char *context_p1_start = start_of_line(error_line - 2, input); char *context_p1_line = single_line(context_p1_start); if (linecnt > 2) { const char *context_p2_start = start_of_line(context_p1_start - 2, input); char *context_p2_line = single_line(context_p2_start); ELOG("CONFIG: Line %3d: %s\n", linecnt - 2, context_p2_line); free(context_p2_line); } ELOG("CONFIG: Line %3d: %s\n", linecnt - 1, context_p1_line); free(context_p1_line); } ELOG("CONFIG: Line %3d: %s\n", linecnt, error_copy); ELOG("CONFIG: %s\n", position); free(error_copy); /* Print context lines *after* the error, if any. */ for (int i = 0; i < 2; i++) { char *error_line_end = strchr(error_line, '\n'); if (error_line_end != NULL && *(error_line_end + 1) != '\0') { error_line = error_line_end + 1; error_copy = single_line(error_line); ELOG("CONFIG: Line %3d: %s\n", linecnt + i + 1, error_copy); free(error_copy); } } context->has_errors = true; /* Format this error message as a JSON reply. */ y(map_open); ystr("success"); y(bool, false); /* We set parse_error to true to distinguish this from other * errors. i3-nagbar is spawned upon keypresses only for parser * errors. */ ystr("parse_error"); y(bool, true); ystr("error"); ystr(errormessage); ystr("input"); ystr(input); ystr("errorposition"); ystr(position); y(map_close); /* Skip the rest of this line, but continue parsing. */ while ((size_t)(walk - input) <= len && *walk != '\n') walk++; free(position); free(errormessage); clear_stack(); /* To figure out in which state to go (e.g. MODE or INITIAL), * we find the nearest state which contains an <error> token * and follow that one. */ bool error_token_found = false; for (int i = statelist_idx - 1; (i >= 0) && !error_token_found; i--) { cmdp_token_ptr *errptr = &(tokens[statelist[i]]); for (int j = 0; j < errptr->n; j++) { if (strcmp(errptr->array[j].name, "error") != 0) continue; next_state(&(errptr->array[j])); error_token_found = true; break; } } assert(error_token_found); } }