2 #define I3__FILE__ "config_parser.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
9 * config_parser.c: hand-written parser to parse configuration directives.
11 * See also src/commands_parser.c for rationale on why we use a custom parser.
13 * This parser works VERY MUCH like src/commands_parser.c, so read that first.
14 * The differences are:
16 * 1. config_parser supports the 'number' token type (in addition to 'word' and
17 * 'string'). Numbers are referred to using &num (like $str).
19 * 2. Criteria are not executed immediately, they are just stored.
21 * 3. config_parser recognizes \n and \r as 'end' token, while commands_parser
24 * 4. config_parser skips the current line on invalid inputs and follows the
25 * nearest <error> token.
37 // Macros to make the YAJL API a bit easier to use.
38 #define y(x, ...) yajl_gen_ ## x (command_output.json_gen, ##__VA_ARGS__)
39 #define ystr(str) yajl_gen_string(command_output.json_gen, (unsigned char*)str, strlen(str))
41 /*******************************************************************************
42 * The data structures used for parsing. Essentially the current state and a
43 * list of tokens for that state.
45 * The GENERATED_* files are generated by generate-commands-parser.pl with the
46 * input parser-specs/configs.spec.
47 ******************************************************************************/
49 #include "GENERATED_config_enums.h"
51 typedef struct token {
54 /* This might be __CALL */
55 cmdp_state next_state;
57 uint16_t call_identifier;
61 typedef struct tokenptr {
66 #include "GENERATED_config_tokens.h"
68 /*******************************************************************************
69 * The (small) stack where identified literals are stored during the parsing
70 * of a single command (like $workspace).
71 ******************************************************************************/
74 /* Just a pointer, not dynamically allocated. */
75 const char *identifier;
86 /* 10 entries should be enough for everybody. */
87 static struct stack_entry stack[10];
90 * Pushes a string (identified by 'identifier') on the stack. We simply use a
91 * single array, since the number of entries we have to store is very small.
94 static void push_string(const char *identifier, const char *str) {
95 for (int c = 0; c < 10; c++) {
96 if (stack[c].identifier != NULL &&
97 strcmp(stack[c].identifier, identifier) != 0)
99 if (stack[c].identifier == NULL) {
100 /* Found a free slot, let’s store it here. */
101 stack[c].identifier = identifier;
102 stack[c].val.str = sstrdup(str);
103 stack[c].type = STACK_STR;
105 /* Append the value. */
106 char *prev = stack[c].val.str;
107 sasprintf(&(stack[c].val.str), "%s,%s", prev, str);
113 /* When we arrive here, the stack is full. This should not happen and
114 * means there’s either a bug in this parser or the specification
115 * contains a command with more than 10 identified tokens. */
116 fprintf(stderr, "BUG: commands_parser stack full. This means either a bug "
117 "in the code, or a new command which contains more than "
118 "10 identified tokens.\n");
122 static void push_long(const char *identifier, long num) {
123 for (int c = 0; c < 10; c++) {
124 if (stack[c].identifier != NULL)
126 /* Found a free slot, let’s store it here. */
127 stack[c].identifier = identifier;
128 stack[c].val.num = num;
129 stack[c].type = STACK_LONG;
133 /* When we arrive here, the stack is full. This should not happen and
134 * means there’s either a bug in this parser or the specification
135 * contains a command with more than 10 identified tokens. */
136 fprintf(stderr, "BUG: commands_parser stack full. This means either a bug "
137 "in the code, or a new command which contains more than "
138 "10 identified tokens.\n");
143 static const char *get_string(const char *identifier) {
144 for (int c = 0; c < 10; c++) {
145 if (stack[c].identifier == NULL)
147 if (strcmp(identifier, stack[c].identifier) == 0)
148 return stack[c].val.str;
153 static const long get_long(const char *identifier) {
154 for (int c = 0; c < 10; c++) {
155 if (stack[c].identifier == NULL)
157 if (strcmp(identifier, stack[c].identifier) == 0)
158 return stack[c].val.num;
163 static void clear_stack(void) {
164 for (int c = 0; c < 10; c++) {
165 if (stack[c].type == STACK_STR && stack[c].val.str != NULL)
166 free(stack[c].val.str);
167 stack[c].identifier = NULL;
168 stack[c].val.str = NULL;
169 stack[c].val.num = 0;
173 // TODO: remove this if it turns out we don’t need it for testing.
175 /*******************************************************************************
176 * A dynamically growing linked list which holds the criteria for the current
178 ******************************************************************************/
180 typedef struct criterion {
184 TAILQ_ENTRY(criterion) criteria;
187 static TAILQ_HEAD(criteria_head, criterion) criteria =
188 TAILQ_HEAD_INITIALIZER(criteria);
191 * Stores the given type/value in the list of criteria.
192 * Accepts a pointer as first argument, since it is 'call'ed by the parser.
195 static void push_criterion(void *unused_criteria, const char *type,
197 struct criterion *criterion = malloc(sizeof(struct criterion));
198 criterion->type = strdup(type);
199 criterion->value = strdup(value);
200 TAILQ_INSERT_TAIL(&criteria, criterion, criteria);
204 * Clears the criteria linked list.
205 * Accepts a pointer as first argument, since it is 'call'ed by the parser.
208 static void clear_criteria(void *unused_criteria) {
209 struct criterion *criterion;
210 while (!TAILQ_EMPTY(&criteria)) {
211 criterion = TAILQ_FIRST(&criteria);
212 free(criterion->type);
213 free(criterion->value);
214 TAILQ_REMOVE(&criteria, criterion, criteria);
220 /*******************************************************************************
222 ******************************************************************************/
224 static cmdp_state state;
225 static Match current_match;
226 static struct ConfigResult subcommand_output;
227 static struct ConfigResult command_output;
229 /* A list which contains the states that lead to the current state, e.g.
230 * INITIAL, WORKSPACE_LAYOUT.
231 * When jumping back to INITIAL, statelist_idx will simply be set to 1
232 * (likewise for other states, e.g. MODE or BAR).
233 * This list is used to process the nearest error token. */
234 static cmdp_state statelist[10] = { INITIAL };
235 /* NB: statelist_idx points to where the next entry will be inserted */
236 static int statelist_idx = 1;
238 #include "GENERATED_config_call.h"
241 static void next_state(const cmdp_token *token) {
242 cmdp_state _next_state = token->next_state;
244 //printf("token = name %s identifier %s\n", token->name, token->identifier);
245 //printf("next_state = %d\n", token->next_state);
246 if (token->next_state == __CALL) {
247 subcommand_output.json_gen = command_output.json_gen;
248 GENERATED_call(token->extra.call_identifier, &subcommand_output);
249 _next_state = subcommand_output.next_state;
254 if (state == INITIAL) {
258 /* See if we are jumping back to a state in which we were in previously
259 * (statelist contains INITIAL) and just move statelist_idx accordingly. */
260 for (int i = 0; i < statelist_idx; i++) {
261 if (statelist[i] != _next_state)
267 /* Otherwise, the state is new and we add it to the list */
268 statelist[statelist_idx++] = _next_state;
272 * Returns a pointer to the start of the line (one byte after the previous \r,
273 * \n) or the start of the input, if this is the first line.
276 static const char *start_of_line(const char *walk, const char *beginning) {
277 while (*walk != '\n' && *walk != '\r' && walk >= beginning) {
285 * Copies the line and terminates it at the next \n, if any.
287 * The caller has to free() the result.
290 static char *single_line(const char *start) {
291 char *result = sstrdup(start);
292 char *end = strchr(result, '\n');
298 struct ConfigResult *parse_config(const char *input, struct context *context) {
299 /* Dump the entire config file into the debug log. We cannot just use
300 * DLOG("%s", input); because one log message must not exceed 4 KiB. */
301 const char *dumpwalk = input;
303 while (*dumpwalk != '\0') {
304 char *next_nl = strchr(dumpwalk, '\n');
305 if (next_nl != NULL) {
306 DLOG("CONFIG(line %3d): %.*s\n", linecnt, (int)(next_nl - dumpwalk), dumpwalk);
307 dumpwalk = next_nl + 1;
309 DLOG("CONFIG(line %3d): %s\n", linecnt, dumpwalk);
317 /* A YAJL JSON generator used for formatting replies. */
319 command_output.json_gen = yajl_gen_alloc(NULL);
321 command_output.json_gen = yajl_gen_alloc(NULL, NULL);
326 const char *walk = input;
327 const size_t len = strlen(input);
329 const cmdp_token *token;
333 // TODO: make this testable
335 cfg_criteria_init(¤t_match, &subcommand_output, INITIAL);
338 /* The "<=" operator is intentional: We also handle the terminating 0-byte
339 * explicitly by looking for an 'end' token. */
340 while ((walk - input) <= len) {
341 /* Skip whitespace before every token, newlines are relevant since they
342 * separate configuration directives. */
343 while ((*walk == ' ' || *walk == '\t') && *walk != '\0')
346 //printf("remaining input: %s\n", walk);
348 cmdp_token_ptr *ptr = &(tokens[state]);
349 token_handled = false;
350 for (c = 0; c < ptr->n; c++) {
351 token = &(ptr->array[c]);
354 if (token->name[0] == '\'') {
355 if (strncasecmp(walk, token->name + 1, strlen(token->name) - 1) == 0) {
356 if (token->identifier != NULL)
357 push_string(token->identifier, token->name + 1);
358 walk += strlen(token->name) - 1;
360 token_handled = true;
366 if (strcmp(token->name, "number") == 0) {
367 /* Handle numbers. We only accept decimal numbers for now. */
370 long int num = strtol(walk, &end, 10);
371 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
372 (errno != 0 && num == 0))
375 /* No valid numbers found */
379 if (token->identifier != NULL)
380 push_long(token->identifier, num);
382 /* Set walk to the first non-number character */
385 token_handled = true;
389 if (strcmp(token->name, "string") == 0 ||
390 strcmp(token->name, "word") == 0) {
391 const char *beginning = walk;
392 /* Handle quoted strings (or words). */
396 while (*walk != '\0' && (*walk != '"' || *(walk-1) == '\\'))
399 if (token->name[0] == 's') {
400 while (*walk != '\0' && *walk != '\r' && *walk != '\n')
403 /* For a word, the delimiters are white space (' ' or
404 * '\t'), closing square bracket (]), comma (,) and
406 while (*walk != ' ' && *walk != '\t' &&
407 *walk != ']' && *walk != ',' &&
408 *walk != ';' && *walk != '\r' &&
409 *walk != '\n' && *walk != '\0')
413 if (walk != beginning) {
414 char *str = scalloc(walk-beginning + 1);
415 /* We copy manually to handle escaping of characters. */
417 for (inpos = 0, outpos = 0;
418 inpos < (walk-beginning);
420 /* We only handle escaped double quotes to not break
421 * backwards compatibility with people using \w in
422 * regular expressions etc. */
423 if (beginning[inpos] == '\\' && beginning[inpos+1] == '"')
425 str[outpos] = beginning[inpos];
427 if (token->identifier)
428 push_string(token->identifier, str);
430 /* If we are at the end of a quoted string, skip the ending
435 token_handled = true;
440 if (strcmp(token->name, "end") == 0) {
441 //printf("checking for end: *%s*\n", walk);
442 if (*walk == '\0' || *walk == '\n' || *walk == '\r') {
444 token_handled = true;
445 /* To make sure we start with an appropriate matching
446 * datastructure for commands which do *not* specify any
447 * criteria, we re-initialize the criteria system after
449 // TODO: make this testable
451 cfg_criteria_init(¤t_match, &subcommand_output, INITIAL);
460 if (!token_handled) {
461 /* Figure out how much memory we will need to fill in the names of
462 * all tokens afterwards. */
464 for (c = 0; c < ptr->n; c++)
465 tokenlen += strlen(ptr->array[c].name) + strlen("'', ");
467 /* Build up a decent error message. We include the problem, the
468 * full input, and underline the position where the parser
471 char *possible_tokens = smalloc(tokenlen + 1);
472 char *tokenwalk = possible_tokens;
473 for (c = 0; c < ptr->n; c++) {
474 token = &(ptr->array[c]);
475 if (token->name[0] == '\'') {
476 /* A literal is copied to the error message enclosed with
479 strcpy(tokenwalk, token->name + 1);
480 tokenwalk += strlen(token->name + 1);
483 /* Skip error tokens in error messages, they are used
484 * internally only and might confuse users. */
485 if (strcmp(token->name, "error") == 0)
487 /* Any other token is copied to the error message enclosed
488 * with angle brackets. */
490 strcpy(tokenwalk, token->name);
491 tokenwalk += strlen(token->name);
494 if (c < (ptr->n - 1)) {
500 sasprintf(&errormessage, "Expected one of these tokens: %s",
502 free(possible_tokens);
505 /* Go back to the beginning of the line */
506 const char *error_line = start_of_line(walk, input);
508 /* Contains the same amount of characters as 'input' has, but with
509 * the unparseable part highlighted using ^ characters. */
510 char *position = scalloc(strlen(error_line) + 1);
511 const char *copywalk;
512 for (copywalk = error_line;
513 *copywalk != '\n' && *copywalk != '\r' && *copywalk != '\0';
515 position[(copywalk - error_line)] = (copywalk >= walk ? '^' : (*copywalk == '\t' ? '\t' : ' '));
516 position[(copywalk - error_line)] = '\0';
518 ELOG("CONFIG: %s\n", errormessage);
519 ELOG("CONFIG: (in file %s)\n", context->filename);
520 char *error_copy = single_line(error_line);
522 /* Print context lines *before* the error, if any. */
524 const char *context_p1_start = start_of_line(error_line-2, input);
525 char *context_p1_line = single_line(context_p1_start);
527 const char *context_p2_start = start_of_line(context_p1_start-2, input);
528 char *context_p2_line = single_line(context_p2_start);
529 ELOG("CONFIG: Line %3d: %s\n", linecnt - 2, context_p2_line);
530 free(context_p2_line);
532 ELOG("CONFIG: Line %3d: %s\n", linecnt - 1, context_p1_line);
533 free(context_p1_line);
535 ELOG("CONFIG: Line %3d: %s\n", linecnt, error_copy);
536 ELOG("CONFIG: %s\n", position);
538 /* Print context lines *after* the error, if any. */
539 for (int i = 0; i < 2; i++) {
540 char *error_line_end = strchr(error_line, '\n');
541 if (error_line_end != NULL && *(error_line_end + 1) != '\0') {
542 error_line = error_line_end + 1;
543 error_copy = single_line(error_line);
544 ELOG("CONFIG: Line %3d: %s\n", linecnt + i + 1, error_copy);
549 context->has_errors = true;
551 /* Format this error message as a JSON reply. */
555 /* We set parse_error to true to distinguish this from other
556 * errors. i3-nagbar is spawned upon keypresses only for parser
564 ystr("errorposition");
568 /* Skip the rest of this line, but continue parsing. */
569 while ((walk - input) <= len && *walk != '\n')
576 /* To figure out in which state to go (e.g. MODE or INITIAL),
577 * we find the nearest state which contains an <error> token
578 * and follow that one. */
579 bool error_token_found = false;
580 for (int i = statelist_idx-1; (i >= 0) && !error_token_found; i--) {
581 cmdp_token_ptr *errptr = &(tokens[statelist[i]]);
582 for (int j = 0; j < errptr->n; j++) {
583 if (strcmp(errptr->array[j].name, "error") != 0)
585 next_state(&(errptr->array[j]));
586 error_token_found = true;
591 assert(error_token_found);
597 return &command_output;
600 /*******************************************************************************
601 * Code for building the stand-alone binary test.commands_parser which is used
602 * by t/187-commands-parser.t.
603 ******************************************************************************/
608 * Logs the given message to stdout while prefixing the current time to it,
609 * but only if debug logging was activated.
610 * This is to be called by DLOG() which includes filename/linenumber
613 void debuglog(char *fmt, ...) {
617 fprintf(stdout, "# ");
618 vfprintf(stdout, fmt, args);
622 void errorlog(char *fmt, ...) {
626 vfprintf(stderr, fmt, args);
630 static int criteria_next_state;
632 void cfg_criteria_init(I3_CFG, int _state) {
633 criteria_next_state = _state;
636 void cfg_criteria_add(I3_CFG, const char *ctype, const char *cvalue) {
639 void cfg_criteria_pop_state(I3_CFG) {
640 result->next_state = criteria_next_state;
643 int main(int argc, char *argv[]) {
645 fprintf(stderr, "Syntax: %s <command>\n", argv[0]);
648 struct context context;
649 context.filename = "<stdin>";
650 parse_config(argv[1], &context);