2 #define I3__FILE__ "config_parser.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2013 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.
34 #include <sys/types.h>
41 // Macros to make the YAJL API a bit easier to use.
42 #define y(x, ...) yajl_gen_ ## x (command_output.json_gen, ##__VA_ARGS__)
43 #define ystr(str) yajl_gen_string(command_output.json_gen, (unsigned char*)str, strlen(str))
46 pid_t config_error_nagbar_pid = -1;
47 static struct context *context;
50 /*******************************************************************************
51 * The data structures used for parsing. Essentially the current state and a
52 * list of tokens for that state.
54 * The GENERATED_* files are generated by generate-commands-parser.pl with the
55 * input parser-specs/configs.spec.
56 ******************************************************************************/
58 #include "GENERATED_config_enums.h"
60 typedef struct token {
63 /* This might be __CALL */
64 cmdp_state next_state;
66 uint16_t call_identifier;
70 typedef struct tokenptr {
75 #include "GENERATED_config_tokens.h"
77 /*******************************************************************************
78 * The (small) stack where identified literals are stored during the parsing
79 * of a single command (like $workspace).
80 ******************************************************************************/
83 /* Just a pointer, not dynamically allocated. */
84 const char *identifier;
95 /* 10 entries should be enough for everybody. */
96 static struct stack_entry stack[10];
99 * Pushes a string (identified by 'identifier') on the stack. We simply use a
100 * single array, since the number of entries we have to store is very small.
103 static void push_string(const char *identifier, const char *str) {
104 for (int c = 0; c < 10; c++) {
105 if (stack[c].identifier != NULL &&
106 strcmp(stack[c].identifier, identifier) != 0)
108 if (stack[c].identifier == NULL) {
109 /* Found a free slot, let’s store it here. */
110 stack[c].identifier = identifier;
111 stack[c].val.str = sstrdup(str);
112 stack[c].type = STACK_STR;
114 /* Append the value. */
115 char *prev = stack[c].val.str;
116 sasprintf(&(stack[c].val.str), "%s,%s", prev, str);
122 /* When we arrive here, the stack is full. This should not happen and
123 * means there’s either a bug in this parser or the specification
124 * contains a command with more than 10 identified tokens. */
125 fprintf(stderr, "BUG: commands_parser stack full. This means either a bug "
126 "in the code, or a new command which contains more than "
127 "10 identified tokens.\n");
131 static void push_long(const char *identifier, long num) {
132 for (int c = 0; c < 10; c++) {
133 if (stack[c].identifier != NULL)
135 /* Found a free slot, let’s store it here. */
136 stack[c].identifier = identifier;
137 stack[c].val.num = num;
138 stack[c].type = STACK_LONG;
142 /* When we arrive here, the stack is full. This should not happen and
143 * means there’s either a bug in this parser or the specification
144 * contains a command with more than 10 identified tokens. */
145 fprintf(stderr, "BUG: commands_parser stack full. This means either a bug "
146 "in the code, or a new command which contains more than "
147 "10 identified tokens.\n");
152 static const char *get_string(const char *identifier) {
153 for (int c = 0; c < 10; c++) {
154 if (stack[c].identifier == NULL)
156 if (strcmp(identifier, stack[c].identifier) == 0)
157 return stack[c].val.str;
162 static long get_long(const char *identifier) {
163 for (int c = 0; c < 10; c++) {
164 if (stack[c].identifier == NULL)
166 if (strcmp(identifier, stack[c].identifier) == 0)
167 return stack[c].val.num;
172 static void clear_stack(void) {
173 for (int c = 0; c < 10; c++) {
174 if (stack[c].type == STACK_STR && stack[c].val.str != NULL)
175 free(stack[c].val.str);
176 stack[c].identifier = NULL;
177 stack[c].val.str = NULL;
178 stack[c].val.num = 0;
182 // TODO: remove this if it turns out we don’t need it for testing.
184 /*******************************************************************************
185 * A dynamically growing linked list which holds the criteria for the current
187 ******************************************************************************/
189 typedef struct criterion {
193 TAILQ_ENTRY(criterion) criteria;
196 static TAILQ_HEAD(criteria_head, criterion) criteria =
197 TAILQ_HEAD_INITIALIZER(criteria);
200 * Stores the given type/value in the list of criteria.
201 * Accepts a pointer as first argument, since it is 'call'ed by the parser.
204 static void push_criterion(void *unused_criteria, const char *type,
206 struct criterion *criterion = malloc(sizeof(struct criterion));
207 criterion->type = strdup(type);
208 criterion->value = strdup(value);
209 TAILQ_INSERT_TAIL(&criteria, criterion, criteria);
213 * Clears the criteria linked list.
214 * Accepts a pointer as first argument, since it is 'call'ed by the parser.
217 static void clear_criteria(void *unused_criteria) {
218 struct criterion *criterion;
219 while (!TAILQ_EMPTY(&criteria)) {
220 criterion = TAILQ_FIRST(&criteria);
221 free(criterion->type);
222 free(criterion->value);
223 TAILQ_REMOVE(&criteria, criterion, criteria);
229 /*******************************************************************************
231 ******************************************************************************/
233 static cmdp_state state;
234 static Match current_match;
235 static struct ConfigResult subcommand_output;
236 static struct ConfigResult command_output;
238 /* A list which contains the states that lead to the current state, e.g.
239 * INITIAL, WORKSPACE_LAYOUT.
240 * When jumping back to INITIAL, statelist_idx will simply be set to 1
241 * (likewise for other states, e.g. MODE or BAR).
242 * This list is used to process the nearest error token. */
243 static cmdp_state statelist[10] = { INITIAL };
244 /* NB: statelist_idx points to where the next entry will be inserted */
245 static int statelist_idx = 1;
247 #include "GENERATED_config_call.h"
250 static void next_state(const cmdp_token *token) {
251 cmdp_state _next_state = token->next_state;
253 //printf("token = name %s identifier %s\n", token->name, token->identifier);
254 //printf("next_state = %d\n", token->next_state);
255 if (token->next_state == __CALL) {
256 subcommand_output.json_gen = command_output.json_gen;
257 GENERATED_call(token->extra.call_identifier, &subcommand_output);
258 _next_state = subcommand_output.next_state;
263 if (state == INITIAL) {
267 /* See if we are jumping back to a state in which we were in previously
268 * (statelist contains INITIAL) and just move statelist_idx accordingly. */
269 for (int i = 0; i < statelist_idx; i++) {
270 if (statelist[i] != _next_state)
276 /* Otherwise, the state is new and we add it to the list */
277 statelist[statelist_idx++] = _next_state;
281 * Returns a pointer to the start of the line (one byte after the previous \r,
282 * \n) or the start of the input, if this is the first line.
285 static const char *start_of_line(const char *walk, const char *beginning) {
286 while (*walk != '\n' && *walk != '\r' && walk >= beginning) {
294 * Copies the line and terminates it at the next \n, if any.
296 * The caller has to free() the result.
299 static char *single_line(const char *start) {
300 char *result = sstrdup(start);
301 char *end = strchr(result, '\n');
307 struct ConfigResult *parse_config(const char *input, struct context *context) {
308 /* Dump the entire config file into the debug log. We cannot just use
309 * DLOG("%s", input); because one log message must not exceed 4 KiB. */
310 const char *dumpwalk = input;
312 while (*dumpwalk != '\0') {
313 char *next_nl = strchr(dumpwalk, '\n');
314 if (next_nl != NULL) {
315 DLOG("CONFIG(line %3d): %.*s\n", linecnt, (int)(next_nl - dumpwalk), dumpwalk);
316 dumpwalk = next_nl + 1;
318 DLOG("CONFIG(line %3d): %s\n", linecnt, dumpwalk);
326 /* A YAJL JSON generator used for formatting replies. */
327 command_output.json_gen = yajl_gen_alloc(NULL);
331 const char *walk = input;
332 const size_t len = strlen(input);
334 const cmdp_token *token;
338 // TODO: make this testable
340 cfg_criteria_init(¤t_match, &subcommand_output, INITIAL);
343 /* The "<=" operator is intentional: We also handle the terminating 0-byte
344 * explicitly by looking for an 'end' token. */
345 while ((size_t)(walk - input) <= len) {
346 /* Skip whitespace before every token, newlines are relevant since they
347 * separate configuration directives. */
348 while ((*walk == ' ' || *walk == '\t') && *walk != '\0')
351 //printf("remaining input: %s\n", walk);
353 cmdp_token_ptr *ptr = &(tokens[state]);
354 token_handled = false;
355 for (c = 0; c < ptr->n; c++) {
356 token = &(ptr->array[c]);
359 if (token->name[0] == '\'') {
360 if (strncasecmp(walk, token->name + 1, strlen(token->name) - 1) == 0) {
361 if (token->identifier != NULL)
362 push_string(token->identifier, token->name + 1);
363 walk += strlen(token->name) - 1;
365 token_handled = true;
371 if (strcmp(token->name, "number") == 0) {
372 /* Handle numbers. We only accept decimal numbers for now. */
375 long int num = strtol(walk, &end, 10);
376 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
377 (errno != 0 && num == 0))
380 /* No valid numbers found */
384 if (token->identifier != NULL)
385 push_long(token->identifier, num);
387 /* Set walk to the first non-number character */
390 token_handled = true;
394 if (strcmp(token->name, "string") == 0 ||
395 strcmp(token->name, "word") == 0) {
396 const char *beginning = walk;
397 /* Handle quoted strings (or words). */
401 while (*walk != '\0' && (*walk != '"' || *(walk-1) == '\\'))
404 if (token->name[0] == 's') {
405 while (*walk != '\0' && *walk != '\r' && *walk != '\n')
408 /* For a word, the delimiters are white space (' ' or
409 * '\t'), closing square bracket (]), comma (,) and
411 while (*walk != ' ' && *walk != '\t' &&
412 *walk != ']' && *walk != ',' &&
413 *walk != ';' && *walk != '\r' &&
414 *walk != '\n' && *walk != '\0')
418 if (walk != beginning) {
419 char *str = scalloc(walk-beginning + 1);
420 /* We copy manually to handle escaping of characters. */
422 for (inpos = 0, outpos = 0;
423 inpos < (walk-beginning);
425 /* We only handle escaped double quotes to not break
426 * backwards compatibility with people using \w in
427 * regular expressions etc. */
428 if (beginning[inpos] == '\\' && beginning[inpos+1] == '"')
430 str[outpos] = beginning[inpos];
432 if (token->identifier)
433 push_string(token->identifier, str);
435 /* If we are at the end of a quoted string, skip the ending
440 token_handled = true;
445 if (strcmp(token->name, "line") == 0) {
446 while (*walk != '\0' && *walk != '\n' && *walk != '\r')
449 token_handled = true;
455 if (strcmp(token->name, "end") == 0) {
456 //printf("checking for end: *%s*\n", walk);
457 if (*walk == '\0' || *walk == '\n' || *walk == '\r') {
459 token_handled = true;
460 /* To make sure we start with an appropriate matching
461 * datastructure for commands which do *not* specify any
462 * criteria, we re-initialize the criteria system after
464 // TODO: make this testable
466 cfg_criteria_init(¤t_match, &subcommand_output, INITIAL);
475 if (!token_handled) {
476 /* Figure out how much memory we will need to fill in the names of
477 * all tokens afterwards. */
479 for (c = 0; c < ptr->n; c++)
480 tokenlen += strlen(ptr->array[c].name) + strlen("'', ");
482 /* Build up a decent error message. We include the problem, the
483 * full input, and underline the position where the parser
486 char *possible_tokens = smalloc(tokenlen + 1);
487 char *tokenwalk = possible_tokens;
488 for (c = 0; c < ptr->n; c++) {
489 token = &(ptr->array[c]);
490 if (token->name[0] == '\'') {
491 /* A literal is copied to the error message enclosed with
494 strcpy(tokenwalk, token->name + 1);
495 tokenwalk += strlen(token->name + 1);
498 /* Skip error tokens in error messages, they are used
499 * internally only and might confuse users. */
500 if (strcmp(token->name, "error") == 0)
502 /* Any other token is copied to the error message enclosed
503 * with angle brackets. */
505 strcpy(tokenwalk, token->name);
506 tokenwalk += strlen(token->name);
509 if (c < (ptr->n - 1)) {
515 sasprintf(&errormessage, "Expected one of these tokens: %s",
517 free(possible_tokens);
520 /* Go back to the beginning of the line */
521 const char *error_line = start_of_line(walk, input);
523 /* Contains the same amount of characters as 'input' has, but with
524 * the unparseable part highlighted using ^ characters. */
525 char *position = scalloc(strlen(error_line) + 1);
526 const char *copywalk;
527 for (copywalk = error_line;
528 *copywalk != '\n' && *copywalk != '\r' && *copywalk != '\0';
530 position[(copywalk - error_line)] = (copywalk >= walk ? '^' : (*copywalk == '\t' ? '\t' : ' '));
531 position[(copywalk - error_line)] = '\0';
533 ELOG("CONFIG: %s\n", errormessage);
534 ELOG("CONFIG: (in file %s)\n", context->filename);
535 char *error_copy = single_line(error_line);
537 /* Print context lines *before* the error, if any. */
539 const char *context_p1_start = start_of_line(error_line-2, input);
540 char *context_p1_line = single_line(context_p1_start);
542 const char *context_p2_start = start_of_line(context_p1_start-2, input);
543 char *context_p2_line = single_line(context_p2_start);
544 ELOG("CONFIG: Line %3d: %s\n", linecnt - 2, context_p2_line);
545 free(context_p2_line);
547 ELOG("CONFIG: Line %3d: %s\n", linecnt - 1, context_p1_line);
548 free(context_p1_line);
550 ELOG("CONFIG: Line %3d: %s\n", linecnt, error_copy);
551 ELOG("CONFIG: %s\n", position);
553 /* Print context lines *after* the error, if any. */
554 for (int i = 0; i < 2; i++) {
555 char *error_line_end = strchr(error_line, '\n');
556 if (error_line_end != NULL && *(error_line_end + 1) != '\0') {
557 error_line = error_line_end + 1;
558 error_copy = single_line(error_line);
559 ELOG("CONFIG: Line %3d: %s\n", linecnt + i + 1, error_copy);
564 context->has_errors = true;
566 /* Format this error message as a JSON reply. */
570 /* We set parse_error to true to distinguish this from other
571 * errors. i3-nagbar is spawned upon keypresses only for parser
579 ystr("errorposition");
583 /* Skip the rest of this line, but continue parsing. */
584 while ((size_t)(walk - input) <= len && *walk != '\n')
591 /* To figure out in which state to go (e.g. MODE or INITIAL),
592 * we find the nearest state which contains an <error> token
593 * and follow that one. */
594 bool error_token_found = false;
595 for (int i = statelist_idx-1; (i >= 0) && !error_token_found; i--) {
596 cmdp_token_ptr *errptr = &(tokens[statelist[i]]);
597 for (int j = 0; j < errptr->n; j++) {
598 if (strcmp(errptr->array[j].name, "error") != 0)
600 next_state(&(errptr->array[j]));
601 error_token_found = true;
606 assert(error_token_found);
612 return &command_output;
615 /*******************************************************************************
616 * Code for building the stand-alone binary test.commands_parser which is used
617 * by t/187-commands-parser.t.
618 ******************************************************************************/
623 * Logs the given message to stdout while prefixing the current time to it,
624 * but only if debug logging was activated.
625 * This is to be called by DLOG() which includes filename/linenumber
628 void debuglog(char *fmt, ...) {
632 fprintf(stdout, "# ");
633 vfprintf(stdout, fmt, args);
637 void errorlog(char *fmt, ...) {
641 vfprintf(stderr, fmt, args);
645 static int criteria_next_state;
647 void cfg_criteria_init(I3_CFG, int _state) {
648 criteria_next_state = _state;
651 void cfg_criteria_add(I3_CFG, const char *ctype, const char *cvalue) {
654 void cfg_criteria_pop_state(I3_CFG) {
655 result->next_state = criteria_next_state;
658 int main(int argc, char *argv[]) {
660 fprintf(stderr, "Syntax: %s <command>\n", argv[0]);
663 struct context context;
664 context.filename = "<stdin>";
665 parse_config(argv[1], &context);
671 * Goes through each line of buf (separated by \n) and checks for statements /
672 * commands which only occur in i3 v4 configuration files. If it finds any, it
673 * returns version 4, otherwise it returns version 3.
676 static int detect_version(char *buf) {
679 while (*walk != '\0') {
685 /* check for some v4-only statements */
686 if (strncasecmp(line, "bindcode", strlen("bindcode")) == 0 ||
687 strncasecmp(line, "force_focus_wrapping", strlen("force_focus_wrapping")) == 0 ||
688 strncasecmp(line, "# i3 config file (v4)", strlen("# i3 config file (v4)")) == 0 ||
689 strncasecmp(line, "workspace_layout", strlen("workspace_layout")) == 0) {
690 printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
694 /* if this is a bind statement, we can check the command */
695 if (strncasecmp(line, "bind", strlen("bind")) == 0) {
696 char *bind = strchr(line, ' ');
699 while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
703 if ((bind = strchr(bind, ' ')) == NULL)
705 while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
709 if (strncasecmp(bind, "layout", strlen("layout")) == 0 ||
710 strncasecmp(bind, "floating", strlen("floating")) == 0 ||
711 strncasecmp(bind, "workspace", strlen("workspace")) == 0 ||
712 strncasecmp(bind, "focus left", strlen("focus left")) == 0 ||
713 strncasecmp(bind, "focus right", strlen("focus right")) == 0 ||
714 strncasecmp(bind, "focus up", strlen("focus up")) == 0 ||
715 strncasecmp(bind, "focus down", strlen("focus down")) == 0 ||
716 strncasecmp(bind, "border normal", strlen("border normal")) == 0 ||
717 strncasecmp(bind, "border 1pixel", strlen("border 1pixel")) == 0 ||
718 strncasecmp(bind, "border pixel", strlen("border pixel")) == 0 ||
719 strncasecmp(bind, "border borderless", strlen("border borderless")) == 0 ||
720 strncasecmp(bind, "--no-startup-id", strlen("--no-startup-id")) == 0 ||
721 strncasecmp(bind, "bar", strlen("bar")) == 0) {
722 printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
728 /* advance to the next line */
737 * Calls i3-migrate-config-to-v4 to migrate a configuration file (input
740 * Returns the converted config file or NULL if there was an error (for
741 * example the script could not be found in $PATH or the i3 executable’s
745 static char *migrate_config(char *input, off_t size) {
749 if (pipe(writepipe) != 0 ||
750 pipe(readpipe) != 0) {
751 warn("migrate_config: Could not create pipes");
757 warn("Could not fork()");
763 /* close writing end of writepipe, connect reading side to stdin */
765 dup2(writepipe[0], 0);
767 /* close reading end of readpipe, connect writing side to stdout */
769 dup2(readpipe[1], 1);
771 static char *argv[] = {
772 NULL, /* will be replaced by the executable path */
775 exec_i3_utility("i3-migrate-config-to-v4", argv);
780 /* close reading end of the writepipe (connected to the script’s stdin) */
783 /* write the whole config file to the pipe, the script will read everything
787 while (written < size) {
788 if ((ret = write(writepipe[1], input + written, size - written)) < 0) {
789 warn("Could not write to pipe");
796 /* close writing end of the readpipe (connected to the script’s stdout) */
799 /* read the script’s output */
800 int conv_size = 65535;
801 char *converted = malloc(conv_size);
804 if (read_bytes == conv_size) {
806 converted = realloc(converted, conv_size);
808 ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
810 warn("Cannot read from pipe");
817 /* get the returncode */
820 if (!WIFEXITED(status)) {
821 fprintf(stderr, "Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
825 int returncode = WEXITSTATUS(status);
826 if (returncode != 0) {
827 fprintf(stderr, "Migration process exit code was != 0\n");
828 if (returncode == 2) {
829 fprintf(stderr, "could not start the migration script\n");
830 /* TODO: script was not found. tell the user to fix his system or create a v4 config */
831 } else if (returncode == 1) {
832 fprintf(stderr, "This already was a v4 config. Please add the following line to your config file:\n");
833 fprintf(stderr, "# i3 config file (v4)\n");
834 /* TODO: nag the user with a message to include a hint for i3 in his config file */
843 * Parses the given file by first replacing the variables, then calling
844 * parse_config and possibly launching i3-nagbar.
847 void parse_file(const char *f) {
848 SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
849 int fd, ret, read_bytes = 0;
853 char buffer[1026], key[512], value[512];
855 if ((fd = open(f, O_RDONLY)) == -1)
856 die("Could not open configuration file: %s\n", strerror(errno));
858 if (fstat(fd, &stbuf) == -1)
859 die("Could not fstat file: %s\n", strerror(errno));
861 buf = scalloc((stbuf.st_size + 1) * sizeof(char));
862 while (read_bytes < stbuf.st_size) {
863 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
864 die("Could not read(): %s\n", strerror(errno));
868 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
869 die("Could not lseek: %s\n", strerror(errno));
871 if ((fstr = fdopen(fd, "r")) == NULL)
872 die("Could not fdopen: %s\n", strerror(errno));
874 while (!feof(fstr)) {
875 if (fgets(buffer, 1024, fstr) == NULL) {
878 die("Could not read configuration file\n");
881 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
882 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
883 key[0] == '#' || strlen(key) < 3)
886 if (strcasecmp(key, "set") == 0) {
887 if (value[0] != '$') {
888 ELOG("Malformed variable assignment, name has to start with $\n");
892 /* get key/value for this variable */
893 char *v_key = value, *v_value;
894 if (strstr(value, " ") == NULL && strstr(value, "\t") == NULL) {
895 ELOG("Malformed variable assignment, need a value\n");
899 if (!(v_value = strstr(value, " ")))
900 v_value = strstr(value, "\t");
903 while (*v_value == '\t' || *v_value == ' ')
906 struct Variable *new = scalloc(sizeof(struct Variable));
907 new->key = sstrdup(v_key);
908 new->value = sstrdup(v_value);
909 SLIST_INSERT_HEAD(&variables, new, variables);
910 DLOG("Got new variable %s = %s\n", v_key, v_value);
916 /* For every custom variable, see how often it occurs in the file and
917 * how much extra bytes it requires when replaced. */
918 struct Variable *current, *nearest;
920 /* We need to copy the buffer because we need to invalidate the
921 * variables (otherwise we will count them twice, which is bad when
922 * 'extra' is negative) */
923 char *bufcopy = sstrdup(buf);
924 SLIST_FOREACH(current, &variables, variables) {
925 int extra = (strlen(current->value) - strlen(current->key));
928 next < (bufcopy + stbuf.st_size) &&
929 (next = strcasestr(next, current->key)) != NULL;
930 next += strlen(current->key)) {
932 extra_bytes += extra;
937 /* Then, allocate a new buffer and copy the file over to the new one,
938 * but replace occurences of our variables */
939 char *walk = buf, *destwalk;
940 char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
942 while (walk < (buf + stbuf.st_size)) {
943 /* Find the next variable */
944 SLIST_FOREACH(current, &variables, variables)
945 current->next_match = strcasestr(walk, current->key);
947 int distance = stbuf.st_size;
948 SLIST_FOREACH(current, &variables, variables) {
949 if (current->next_match == NULL)
951 if ((current->next_match - walk) < distance) {
952 distance = (current->next_match - walk);
956 if (nearest == NULL) {
957 /* If there are no more variables, we just copy the rest */
958 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
959 destwalk += (buf + stbuf.st_size) - walk;
963 /* Copy until the next variable, then copy its value */
964 strncpy(destwalk, walk, distance);
965 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
966 walk += distance + strlen(nearest->key);
967 destwalk += distance + strlen(nearest->value);
971 /* analyze the string to find out whether this is an old config file (3.x)
972 * or a new config file (4.x). If it’s old, we run the converter script. */
973 int version = detect_version(buf);
975 /* We need to convert this v3 configuration */
976 char *converted = migrate_config(new, stbuf.st_size);
977 if (converted != NULL) {
979 ELOG("****************************************************************\n");
980 ELOG("NOTE: Automatically converted configuration file from v3 to v4.\n");
982 ELOG("Please convert your config file to v4. You can use this command:\n");
983 ELOG(" mv %s %s.O\n", f, f);
984 ELOG(" i3-migrate-config-to-v4 %s.O > %s\n", f, f);
985 ELOG("****************************************************************\n");
991 printf("**********************************************************************\n");
992 printf("ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
993 printf("was not correctly installed on your system?\n");
994 printf("**********************************************************************\n");
1000 context = scalloc(sizeof(struct context));
1001 context->filename = f;
1003 struct ConfigResult *config_output = parse_config(new, context);
1004 yajl_gen_free(config_output->json_gen);
1006 check_for_duplicate_bindings(context);
1008 if (context->has_errors || context->has_warnings) {
1009 ELOG("FYI: You are using i3 version " I3_VERSION "\n");
1011 ELOG("Please convert your configfile first, then fix any remaining errors (see above).\n");
1015 sasprintf(&editaction, "i3-sensible-editor \"%s\" && i3-msg reload\n", f);
1016 sasprintf(&pageraction, "i3-sensible-pager \"%s\"\n", errorfilename);
1018 NULL, /* will be replaced by the executable path */
1020 (config.font.pattern ? config.font.pattern : "fixed"),
1022 (context->has_errors ? "error" : "warning"),
1024 (context->has_errors ?
1025 "You have an error in your i3 config file!" :
1026 "Your config is outdated. Please fix the warnings to make sure everything works."),
1030 (errorfilename ? "-b" : NULL),
1031 (context->has_errors ? "show errors" : "show warnings"),
1036 start_nagbar(&config_error_nagbar_pid, argv);
1041 FREE(context->line_copy);
1046 while (!SLIST_EMPTY(&variables)) {
1047 current = SLIST_FIRST(&variables);
1049 FREE(current->value);
1050 SLIST_REMOVE_HEAD(&variables, variables);