2 #define I3__FILE__ "config_parser.c"
34 #include <sys/types.h>
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))
60 typedef struct token {
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) {
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");
132 for (
int c = 0; c < 10; c++) {
133 if (
stack[c].identifier != NULL)
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");
153 for (
int c = 0; c < 10; c++) {
154 if (
stack[c].identifier == NULL)
156 if (strcmp(identifier,
stack[c].identifier) == 0)
163 for (
int c = 0; c < 10; c++) {
164 if (
stack[c].identifier == NULL)
166 if (strcmp(identifier,
stack[c].identifier) == 0)
173 for (
int c = 0; c < 10; c++) {
189 typedef struct criterion {
196 static
TAILQ_HEAD(criteria_head, criterion) criteria =
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);
217 static void clear_criteria(
void *unused_criteria) {
218 struct criterion *criterion;
221 free(criterion->type);
222 free(criterion->value);
277 statelist[statelist_idx++] = _next_state;
286 while (*walk !=
'\n' && *walk !=
'\r' && walk >= beginning) {
301 char *end = strchr(result,
'\n');
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);
335 const char *walk = input;
336 const size_t len = strlen(input);
349 while ((walk - input) <= len) {
352 while ((*walk ==
' ' || *walk ==
'\t') && *walk !=
'\0')
358 token_handled =
false;
359 for (c = 0; c < ptr->
n; c++) {
360 token = &(ptr->
array[c]);
363 if (token->
name[0] ==
'\'') {
364 if (strncasecmp(walk, token->
name + 1, strlen(token->
name) - 1) == 0) {
367 walk += strlen(token->
name) - 1;
369 token_handled =
true;
375 if (strcmp(token->
name,
"number") == 0) {
379 long int num = strtol(walk, &end, 10);
380 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
381 (errno != 0 && num == 0))
394 token_handled =
true;
398 if (strcmp(token->
name,
"string") == 0 ||
399 strcmp(token->
name,
"word") == 0) {
400 const char *beginning = walk;
405 while (*walk !=
'\0' && (*walk !=
'"' || *(walk-1) ==
'\\'))
408 if (token->
name[0] ==
's') {
409 while (*walk !=
'\0' && *walk !=
'\r' && *walk !=
'\n')
415 while (*walk !=
' ' && *walk !=
'\t' &&
416 *walk !=
']' && *walk !=
',' &&
417 *walk !=
';' && *walk !=
'\r' &&
418 *walk !=
'\n' && *walk !=
'\0')
422 if (walk != beginning) {
423 char *str =
scalloc(walk-beginning + 1);
426 for (inpos = 0, outpos = 0;
427 inpos < (walk-beginning);
432 if (beginning[inpos] ==
'\\' && beginning[inpos+1] ==
'"')
434 str[outpos] = beginning[inpos];
444 token_handled =
true;
449 if (strcmp(token->
name,
"end") == 0) {
451 if (*walk ==
'\0' || *walk ==
'\n' || *walk ==
'\r') {
453 token_handled =
true;
469 if (!token_handled) {
473 for (c = 0; c < ptr->
n; c++)
474 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
480 char *possible_tokens =
smalloc(tokenlen + 1);
481 char *tokenwalk = possible_tokens;
482 for (c = 0; c < ptr->
n; c++) {
483 token = &(ptr->
array[c]);
484 if (token->
name[0] ==
'\'') {
488 strcpy(tokenwalk, token->
name + 1);
489 tokenwalk += strlen(token->
name + 1);
494 if (strcmp(token->
name,
"error") == 0)
499 strcpy(tokenwalk, token->
name);
500 tokenwalk += strlen(token->
name);
503 if (c < (ptr->
n - 1)) {
509 sasprintf(&errormessage,
"Expected one of these tokens: %s",
511 free(possible_tokens);
519 char *position =
scalloc(strlen(error_line) + 1);
520 const char *copywalk;
521 for (copywalk = error_line;
522 *copywalk !=
'\n' && *copywalk !=
'\r' && *copywalk !=
'\0';
524 position[(copywalk - error_line)] = (copywalk >= walk ?
'^' : (*copywalk ==
'\t' ?
'\t' :
' '));
525 position[(copywalk - error_line)] =
'\0';
527 ELOG(
"CONFIG: %s\n", errormessage);
533 const char *context_p1_start =
start_of_line(error_line-2, input);
534 char *context_p1_line =
single_line(context_p1_start);
536 const char *context_p2_start =
start_of_line(context_p1_start-2, input);
537 char *context_p2_line =
single_line(context_p2_start);
538 ELOG(
"CONFIG: Line %3d: %s\n", linecnt - 2, context_p2_line);
539 free(context_p2_line);
541 ELOG(
"CONFIG: Line %3d: %s\n", linecnt - 1, context_p1_line);
542 free(context_p1_line);
544 ELOG(
"CONFIG: Line %3d: %s\n", linecnt, error_copy);
545 ELOG(
"CONFIG: %s\n", position);
548 for (
int i = 0; i < 2; i++) {
549 char *error_line_end = strchr(error_line,
'\n');
550 if (error_line_end != NULL && *(error_line_end + 1) !=
'\0') {
551 error_line = error_line_end + 1;
553 ELOG(
"CONFIG: Line %3d: %s\n", linecnt + i + 1, error_copy);
573 ystr(
"errorposition");
578 while ((walk - input) <= len && *walk !=
'\n')
588 bool error_token_found =
false;
589 for (
int i =
statelist_idx-1; (i >= 0) && !error_token_found; i--) {
591 for (
int j = 0; j < errptr->
n; j++) {
592 if (strcmp(errptr->
array[j].
name,
"error") != 0)
595 error_token_found =
true;
600 assert(error_token_found);
626 fprintf(stdout,
"# ");
627 vfprintf(stdout, fmt, args);
635 vfprintf(stderr, fmt, args);
641 void cfg_criteria_init(
I3_CFG,
int _state) {
645 void cfg_criteria_add(
I3_CFG,
const char *ctype,
const char *cvalue) {
648 void cfg_criteria_pop_state(
I3_CFG) {
652 int main(
int argc,
char *argv[]) {
654 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);
657 struct context context;
673 while (*walk !=
'\0') {
680 if (strncasecmp(line,
"bindcode", strlen(
"bindcode")) == 0 ||
681 strncasecmp(line,
"force_focus_wrapping", strlen(
"force_focus_wrapping")) == 0 ||
682 strncasecmp(line,
"# i3 config file (v4)", strlen(
"# i3 config file (v4)")) == 0 ||
683 strncasecmp(line,
"workspace_layout", strlen(
"workspace_layout")) == 0) {
684 printf(
"deciding for version 4 due to this line: %.*s\n", (
int)(walk-line), line);
689 if (strncasecmp(line,
"bind", strlen(
"bind")) == 0) {
690 char *bind = strchr(line,
' ');
693 while ((*bind ==
' ' || *bind ==
'\t') && *bind !=
'\0')
697 if ((bind = strchr(bind,
' ')) == NULL)
699 while ((*bind ==
' ' || *bind ==
'\t') && *bind !=
'\0')
703 if (strncasecmp(bind,
"layout", strlen(
"layout")) == 0 ||
704 strncasecmp(bind,
"floating", strlen(
"floating")) == 0 ||
705 strncasecmp(bind,
"workspace", strlen(
"workspace")) == 0 ||
706 strncasecmp(bind,
"focus left", strlen(
"focus left")) == 0 ||
707 strncasecmp(bind,
"focus right", strlen(
"focus right")) == 0 ||
708 strncasecmp(bind,
"focus up", strlen(
"focus up")) == 0 ||
709 strncasecmp(bind,
"focus down", strlen(
"focus down")) == 0 ||
710 strncasecmp(bind,
"border normal", strlen(
"border normal")) == 0 ||
711 strncasecmp(bind,
"border 1pixel", strlen(
"border 1pixel")) == 0 ||
712 strncasecmp(bind,
"border pixel", strlen(
"border pixel")) == 0 ||
713 strncasecmp(bind,
"border borderless", strlen(
"border borderless")) == 0 ||
714 strncasecmp(bind,
"--no-startup-id", strlen(
"--no-startup-id")) == 0 ||
715 strncasecmp(bind,
"bar", strlen(
"bar")) == 0) {
716 printf(
"deciding for version 4 due to this line: %.*s\n", (
int)(walk-line), line);
743 if (pipe(writepipe) != 0 ||
744 pipe(readpipe) != 0) {
745 warn(
"migrate_config: Could not create pipes");
751 warn(
"Could not fork()");
759 dup2(writepipe[0], 0);
763 dup2(readpipe[1], 1);
765 static char *argv[] = {
781 while (written < size) {
782 if ((ret = write(writepipe[1], input + written, size - written)) < 0) {
783 warn(
"Could not write to pipe");
794 int conv_size = 65535;
795 char *converted = malloc(conv_size);
798 if (read_bytes == conv_size) {
800 converted = realloc(converted, conv_size);
802 ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
804 warn(
"Cannot read from pipe");
814 if (!WIFEXITED(status)) {
815 fprintf(stderr,
"Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
819 int returncode = WEXITSTATUS(status);
820 if (returncode != 0) {
821 fprintf(stderr,
"Migration process exit code was != 0\n");
822 if (returncode == 2) {
823 fprintf(stderr,
"could not start the migration script\n");
825 }
else if (returncode == 1) {
826 fprintf(stderr,
"This already was a v4 config. Please add the following line to your config file:\n");
827 fprintf(stderr,
"# i3 config file (v4)\n");
856 if ((bind->
symbol == NULL && current->
symbol != NULL) ||
862 if (bind->
symbol != NULL &&
875 ELOG(
"Duplicate keybinding in config file:\n modmask %d with keycode %d, command \"%s\"\n",
878 ELOG(
"Duplicate keybinding in config file:\n modmask %d with keysym %s, command \"%s\"\n",
892 int fd, ret, read_bytes = 0;
896 char buffer[1026], key[512], value[512];
898 if ((fd = open(f, O_RDONLY)) == -1)
899 die(
"Could not open configuration file: %s\n", strerror(errno));
901 if (fstat(fd, &stbuf) == -1)
902 die(
"Could not fstat file: %s\n", strerror(errno));
904 buf =
scalloc((stbuf.st_size + 1) *
sizeof(
char));
905 while (read_bytes < stbuf.st_size) {
906 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
907 die(
"Could not read(): %s\n", strerror(errno));
911 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
912 die(
"Could not lseek: %s\n", strerror(errno));
914 if ((fstr = fdopen(fd,
"r")) == NULL)
915 die(
"Could not fdopen: %s\n", strerror(errno));
917 while (!feof(fstr)) {
918 if (fgets(buffer, 1024, fstr) == NULL) {
921 die(
"Could not read configuration file\n");
925 if (sscanf(buffer,
"%s %[^\n]", key, value) < 1 ||
926 key[0] ==
'#' || strlen(key) < 3)
929 if (strcasecmp(key,
"set") == 0) {
930 if (value[0] !=
'$') {
931 ELOG(
"Malformed variable assignment, name has to start with $\n");
936 char *v_key = value, *v_value;
937 if (strstr(value,
" ") == NULL && strstr(value,
"\t") == NULL) {
938 ELOG(
"Malformed variable assignment, need a value\n");
942 if (!(v_value = strstr(value,
" ")))
943 v_value = strstr(value,
"\t");
946 while (*v_value ==
'\t' || *v_value ==
' ')
953 DLOG(
"Got new variable %s = %s\n", v_key, v_value);
968 int extra = (strlen(current->
value) - strlen(current->
key));
971 next < (bufcopy + stbuf.st_size) &&
972 (next = strcasestr(next, current->
key)) != NULL;
973 next += strlen(current->
key)) {
975 extra_bytes += extra;
982 char *walk = buf, *destwalk;
983 char *
new =
smalloc((stbuf.st_size + extra_bytes + 1) *
sizeof(
char));
985 while (walk < (buf + stbuf.st_size)) {
990 int distance = stbuf.st_size;
994 if ((current->
next_match - walk) < distance) {
999 if (nearest == NULL) {
1001 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
1002 destwalk += (buf + stbuf.st_size) - walk;
1007 strncpy(destwalk, walk, distance);
1008 strncpy(destwalk + distance, nearest->
value, strlen(nearest->
value));
1009 walk += distance + strlen(nearest->
key);
1010 destwalk += distance + strlen(nearest->
value);
1020 if (converted != NULL) {
1022 ELOG(
"****************************************************************\n");
1023 ELOG(
"NOTE: Automatically converted configuration file from v3 to v4.\n");
1025 ELOG(
"Please convert your config file to v4. You can use this command:\n");
1026 ELOG(
" mv %s %s.O\n", f, f);
1027 ELOG(
" i3-migrate-config-to-v4 %s.O > %s\n", f, f);
1028 ELOG(
"****************************************************************\n");
1034 printf(
"**********************************************************************\n");
1035 printf(
"ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
1036 printf(
"was not correctly installed on your system?\n");
1037 printf(
"**********************************************************************\n");
1043 context =
scalloc(
sizeof(
struct context));
1047 yajl_gen_free(config_output->
json_gen);
1052 ELOG(
"FYI: You are using i3 version " I3_VERSION
"\n");
1054 ELOG(
"Please convert your configfile first, then fix any remaining errors (see above).\n");
1058 sasprintf(&editaction,
"i3-sensible-editor \"%s\" && i3-msg reload\n", f);
1068 "You have an error in your i3 config file!" :
1069 "Your config is outdated. Please fix the warnings to make sure everything works."),
1074 (context->
has_errors ?
"show errors" :
"show warnings"),