34 #include <sys/types.h> 38 #include <xcb/xcb_xrm.h> 41 #define y(x, ...) yajl_gen_##x(command_output.json_gen, ##__VA_ARGS__) 42 #define ystr(str) yajl_gen_string(command_output.json_gen, (unsigned char *)str, strlen(str)) 59 #include "GENERATED_config_enums.h" 61 typedef struct token {
76 #include "GENERATED_config_tokens.h" 105 for (
int c = 0; c < 10; c++) {
106 if (
stack[c].identifier != NULL &&
107 strcmp(
stack[c].identifier, identifier) != 0)
109 if (
stack[c].identifier == NULL) {
126 fprintf(stderr,
"BUG: config_parser stack full. This means either a bug " 127 "in the code, or a new command which contains more than " 128 "10 identified tokens.\n");
133 for (
int c = 0; c < 10; c++) {
134 if (
stack[c].identifier != NULL)
146 fprintf(stderr,
"BUG: config_parser stack full. This means either a bug " 147 "in the code, or a new command which contains more than " 148 "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++) {
200 #include "GENERATED_config_call.h" 215 if (
state == INITIAL) {
224 statelist_idx = i + 1;
229 statelist[statelist_idx++] = _next_state;
238 while (walk >= beginning && *walk !=
'\n' && *walk !=
'\r') {
253 char *end = strchr(result,
'\n');
262 const char *dumpwalk = input;
264 while (*dumpwalk !=
'\0') {
265 char *next_nl = strchr(dumpwalk,
'\n');
266 if (next_nl != NULL) {
267 DLOG(
"CONFIG(line %3d): %.*s\n", linecnt, (
int)(next_nl - dumpwalk), dumpwalk);
268 dumpwalk = next_nl + 1;
270 DLOG(
"CONFIG(line %3d): %s\n", linecnt, dumpwalk);
283 const char *walk = input;
284 const size_t len = strlen(input);
297 while ((
size_t)(walk - input) <= len) {
300 while ((*walk ==
' ' || *walk ==
'\t') && *walk !=
'\0')
306 token_handled =
false;
307 for (c = 0; c < ptr->
n; c++) {
308 token = &(ptr->
array[c]);
311 if (token->
name[0] ==
'\'') {
312 if (strncasecmp(walk, token->
name + 1, strlen(token->
name) - 1) == 0) {
315 walk += strlen(token->
name) - 1;
317 token_handled =
true;
323 if (strcmp(token->
name,
"number") == 0) {
327 long int num = strtol(walk, &end, 10);
328 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
329 (errno != 0 && num == 0))
342 token_handled =
true;
346 if (strcmp(token->
name,
"string") == 0 ||
347 strcmp(token->
name,
"word") == 0) {
348 const char *beginning = walk;
353 while (*walk !=
'\0' && (*walk !=
'"' || *(walk - 1) ==
'\\'))
356 if (token->
name[0] ==
's') {
357 while (*walk !=
'\0' && *walk !=
'\r' && *walk !=
'\n')
363 while (*walk !=
' ' && *walk !=
'\t' &&
364 *walk !=
']' && *walk !=
',' &&
365 *walk !=
';' && *walk !=
'\r' &&
366 *walk !=
'\n' && *walk !=
'\0')
370 if (walk != beginning) {
371 char *str =
scalloc(walk - beginning + 1, 1);
374 for (inpos = 0, outpos = 0;
375 inpos < (walk - beginning);
380 if (beginning[inpos] ==
'\\' && beginning[inpos + 1] ==
'"')
382 str[outpos] = beginning[inpos];
392 token_handled =
true;
397 if (strcmp(token->
name,
"line") == 0) {
398 while (*walk !=
'\0' && *walk !=
'\n' && *walk !=
'\r')
401 token_handled =
true;
407 if (strcmp(token->
name,
"end") == 0) {
409 if (*walk ==
'\0' || *walk ==
'\n' || *walk ==
'\r') {
411 token_handled =
true;
427 if (!token_handled) {
431 for (c = 0; c < ptr->
n; c++)
432 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
438 char *possible_tokens =
smalloc(tokenlen + 1);
439 char *tokenwalk = possible_tokens;
440 for (c = 0; c < ptr->
n; c++) {
441 token = &(ptr->
array[c]);
442 if (token->
name[0] ==
'\'') {
446 strcpy(tokenwalk, token->
name + 1);
447 tokenwalk += strlen(token->
name + 1);
452 if (strcmp(token->
name,
"error") == 0)
457 strcpy(tokenwalk, token->
name);
458 tokenwalk += strlen(token->
name);
461 if (c < (ptr->
n - 1)) {
467 sasprintf(&errormessage,
"Expected one of these tokens: %s",
469 free(possible_tokens);
476 char *position =
scalloc(strlen(error_line) + 1, 1);
477 const char *copywalk;
478 for (copywalk = error_line;
479 *copywalk !=
'\n' && *copywalk !=
'\r' && *copywalk !=
'\0';
481 position[(copywalk - error_line)] = (copywalk >= walk ?
'^' : (*copywalk ==
'\t' ?
'\t' :
' '));
482 position[(copywalk - error_line)] =
'\0';
484 ELOG(
"CONFIG: %s\n", errormessage);
490 const char *context_p1_start =
start_of_line(error_line - 2, input);
491 char *context_p1_line =
single_line(context_p1_start);
493 const char *context_p2_start =
start_of_line(context_p1_start - 2, input);
494 char *context_p2_line =
single_line(context_p2_start);
495 ELOG(
"CONFIG: Line %3d: %s\n", linecnt - 2, context_p2_line);
496 free(context_p2_line);
498 ELOG(
"CONFIG: Line %3d: %s\n", linecnt - 1, context_p1_line);
499 free(context_p1_line);
501 ELOG(
"CONFIG: Line %3d: %s\n", linecnt, error_copy);
502 ELOG(
"CONFIG: %s\n", position);
505 for (
int i = 0; i < 2; i++) {
506 char *error_line_end = strchr(error_line,
'\n');
507 if (error_line_end != NULL && *(error_line_end + 1) !=
'\0') {
508 error_line = error_line_end + 1;
510 ELOG(
"CONFIG: Line %3d: %s\n", linecnt + i + 1, error_copy);
530 ystr(
"errorposition");
535 while ((
size_t)(walk - input) <= len && *walk !=
'\n')
545 bool error_token_found =
false;
546 for (
int i =
statelist_idx - 1; (i >= 0) && !error_token_found; i--) {
548 for (
int j = 0; j < errptr->
n; j++) {
549 if (strcmp(errptr->
array[j].
name,
"error") != 0)
552 error_token_found =
true;
557 assert(error_token_found);
583 fprintf(stdout,
"# ");
584 vfprintf(stdout, fmt, args);
592 vfprintf(stderr, fmt, args);
598 void cfg_criteria_init(
I3_CFG,
int _state) {
602 void cfg_criteria_add(
I3_CFG,
const char *ctype,
const char *cvalue) {
605 void cfg_criteria_pop_state(
I3_CFG) {
609 int main(
int argc,
char *argv[]) {
611 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);
614 struct context context;
630 while (*walk !=
'\0') {
637 if (strncasecmp(line,
"bindcode", strlen(
"bindcode")) == 0 ||
638 strncasecmp(line,
"force_focus_wrapping", strlen(
"force_focus_wrapping")) == 0 ||
639 strncasecmp(line,
"# i3 config file (v4)", strlen(
"# i3 config file (v4)")) == 0 ||
640 strncasecmp(line,
"workspace_layout", strlen(
"workspace_layout")) == 0) {
641 LOG(
"deciding for version 4 due to this line: %.*s\n", (
int)(walk - line), line);
646 if (strncasecmp(line,
"bind", strlen(
"bind")) == 0) {
647 char *bind = strchr(line,
' ');
650 while ((*bind ==
' ' || *bind ==
'\t') && *bind !=
'\0')
654 if ((bind = strchr(bind,
' ')) == NULL)
656 while ((*bind ==
' ' || *bind ==
'\t') && *bind !=
'\0')
660 if (strncasecmp(bind,
"layout", strlen(
"layout")) == 0 ||
661 strncasecmp(bind,
"floating", strlen(
"floating")) == 0 ||
662 strncasecmp(bind,
"workspace", strlen(
"workspace")) == 0 ||
663 strncasecmp(bind,
"focus left", strlen(
"focus left")) == 0 ||
664 strncasecmp(bind,
"focus right", strlen(
"focus right")) == 0 ||
665 strncasecmp(bind,
"focus up", strlen(
"focus up")) == 0 ||
666 strncasecmp(bind,
"focus down", strlen(
"focus down")) == 0 ||
667 strncasecmp(bind,
"border normal", strlen(
"border normal")) == 0 ||
668 strncasecmp(bind,
"border 1pixel", strlen(
"border 1pixel")) == 0 ||
669 strncasecmp(bind,
"border pixel", strlen(
"border pixel")) == 0 ||
670 strncasecmp(bind,
"border borderless", strlen(
"border borderless")) == 0 ||
671 strncasecmp(bind,
"--no-startup-id", strlen(
"--no-startup-id")) == 0 ||
672 strncasecmp(bind,
"bar", strlen(
"bar")) == 0) {
673 LOG(
"deciding for version 4 due to this line: %.*s\n", (
int)(walk - line), line);
700 if (pipe(writepipe) != 0 ||
701 pipe(readpipe) != 0) {
702 warn(
"migrate_config: Could not create pipes");
708 warn(
"Could not fork()");
716 dup2(writepipe[0], 0);
720 dup2(readpipe[1], 1);
722 static char *argv[] = {
735 if (
writeall(writepipe[1], input, size) == -1) {
736 warn(
"Could not write to pipe");
745 int conv_size = 65535;
746 char *converted =
smalloc(conv_size);
747 int read_bytes = 0, ret;
749 if (read_bytes == conv_size) {
751 converted =
srealloc(converted, conv_size);
753 ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
755 warn(
"Cannot read from pipe");
765 if (!WIFEXITED(status)) {
766 fprintf(stderr,
"Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
770 int returncode = WEXITSTATUS(status);
771 if (returncode != 0) {
772 fprintf(stderr,
"Migration process exit code was != 0\n");
773 if (returncode == 2) {
774 fprintf(stderr,
"could not start the migration script\n");
776 }
else if (returncode == 1) {
777 fprintf(stderr,
"This already was a v4 config. Please add the following line to your config file:\n");
778 fprintf(stderr,
"# i3 config file (v4)\n");
791 char *editaction, *pageraction;
792 sasprintf(&editaction,
"i3-sensible-editor \"%s\" && i3-msg reload\n", configpath);
799 (has_errors ?
"error" :
"warning"),
801 (has_errors ?
"You have an error in your i3 config file!" :
"Your config is outdated. Please fix the warnings to make sure everything works."),
806 (has_errors ?
"show errors" :
"show warnings"),
819 static void upsert_variable(
struct variables_head *variables,
char *key,
char *value) {
822 if (strcmp(current->
key, key) != 0) {
826 DLOG(
"Updated variable: %s = %s -> %s\n", key, current->
value, value);
832 DLOG(
"Defined new variable: %s = %s\n", key, value);
834 struct Variable *test = NULL, *loc = NULL;
840 if (strlen(new->key) >= strlen(test->
key))
862 ELOG(
"Failed to open the resource database.\n");
866 database = xcb_xrm_database_from_string(
"");
873 xcb_xrm_resource_get_string(
database, name, NULL, &resource);
888 char buffer[4096], key[512], value[4096], *continuation = NULL;
890 if ((fd = open(f, O_RDONLY)) == -1)
891 die(
"Could not open configuration file: %s\n", strerror(errno));
893 if (fstat(fd, &stbuf) == -1)
894 die(
"Could not fstat file: %s\n", strerror(errno));
896 buf =
scalloc(stbuf.st_size + 1, 1);
898 if ((fstr = fdopen(fd,
"r")) == NULL)
899 die(
"Could not fdopen: %s\n", strerror(errno));
906 bool invalid_sets =
false;
908 while (!feof(fstr)) {
910 continuation = buffer;
911 if (fgets(continuation,
sizeof(buffer) - (continuation - buffer), fstr) == NULL) {
914 die(
"Could not read configuration file\n");
916 if (buffer[strlen(buffer) - 1] !=
'\n' && !feof(fstr)) {
917 ELOG(
"Your line continuation is too long, it exceeds %zd bytes\n",
sizeof(buffer));
922 const bool skip_line = (sscanf(buffer,
"%511s %4095[^\n]", key, value) < 1 || strlen(key) < 3);
923 const bool comment = (key[0] ==
'#');
926 continuation = strstr(buffer,
"\\\n");
931 DLOG(
"line continuation in comment is ignored: \"%.*s\"\n", (
int)strlen(buffer) - 1, buffer);
935 strncpy(buf + strlen(buf), buffer, strlen(buffer) + 1);
938 if (skip_line || comment) {
942 if (strcasecmp(key,
"set") == 0 && *value !=
'\0') {
944 char v_value[4096] = {
'\0'};
946 if (sscanf(value,
"%511s %4095[^\n]", v_key, v_value) < 1) {
947 ELOG(
"Failed to parse variable specification '%s', skipping it.\n", value);
952 if (v_key[0] !=
'$') {
953 ELOG(
"Malformed variable assignment, name has to start with $\n");
960 }
else if (strcasecmp(key,
"set_from_resource") == 0) {
961 char res_name[512] = {
'\0'};
963 char fallback[4096] = {
'\0'};
973 if (sscanf(value,
"%511s %511s %4095[^\n]", v_key, res_name, fallback) < 1) {
974 ELOG(
"Failed to parse resource specification '%s', skipping it.\n", value);
979 if (v_key[0] !=
'$') {
980 ELOG(
"Malformed variable assignment, name has to start with $\n");
986 if (res_value == NULL) {
987 DLOG(
"Could not get resource '%s', using fallback '%s'.\n", res_name, fallback);
1006 struct Variable *current, *nearest;
1007 int extra_bytes = 0;
1013 int extra = (strlen(current->
value) - strlen(current->
key));
1015 for (next = bufcopy;
1016 next < (bufcopy + stbuf.st_size) &&
1017 (next = strcasestr(next, current->
key)) != NULL;
1018 next += strlen(current->
key)) {
1020 extra_bytes +=
extra;
1027 char *walk = buf, *destwalk;
1028 char *
new =
scalloc(stbuf.st_size + extra_bytes + 1, 1);
1030 while (walk < (buf + stbuf.st_size)) {
1035 int distance = stbuf.st_size;
1039 if ((current->
next_match - walk) < distance) {
1044 if (nearest == NULL) {
1046 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
1047 destwalk += (buf + stbuf.st_size) - walk;
1052 strncpy(destwalk, walk, distance);
1053 strncpy(destwalk + distance, nearest->
value, strlen(nearest->
value));
1054 walk += distance + strlen(nearest->
key);
1055 destwalk += distance + strlen(nearest->
value);
1065 if (converted != NULL) {
1067 ELOG(
"****************************************************************\n");
1068 ELOG(
"NOTE: Automatically converted configuration file from v3 to v4.\n");
1070 ELOG(
"Please convert your config file to v4. You can use this command:\n");
1071 ELOG(
" mv %s %s.O\n", f, f);
1072 ELOG(
" i3-migrate-config-to-v4 %s.O > %s\n", f, f);
1073 ELOG(
"****************************************************************\n");
1079 LOG(
"**********************************************************************\n");
1080 LOG(
"ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
1081 LOG(
"was not correctly installed on your system?\n");
1082 LOG(
"**********************************************************************\n");
1087 context =
scalloc(1,
sizeof(
struct context));
1091 yajl_gen_free(config_output->
json_gen);
1100 ELOG(
"Please convert your configfile first, then fix any remaining errors (see above).\n");
union stack_entry::@2 val
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
static char * migrate_config(char *input, off_t size)
Holds a user-assigned variable for parsing the configuration file.
static char * single_line(const char *start)
static char * get_resource(char *name)
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
ssize_t writeall(int fd, const void *buf, size_t count)
Wrapper around correct write which returns -1 (meaning that write failed) or count (meaning that all ...
#define SLIST_INSERT_AFTER(slistelm, elm, field)
static struct ConfigResultIR command_output
struct tokenptr cmdp_token_ptr
static void upsert_variable(struct variables_head *variables, char *key, char *value)
#define SLIST_EMPTY(head)
xcb_connection_t * conn
XCB connection and root screen.
static struct stack_entry stack[10]
static int detect_version(char *buf)
const char * i3_version
Git commit identifier, from version.c.
static long get_long(const char *identifier)
static Match current_match
void debuglog(char *fmt,...)
static int criteria_next_state
enum stack_entry::@1 type
static cmdp_state statelist[10]
void reorder_bindings(void)
Reorders bindings by event_state_mask descendingly so that get_binding() correctly matches more speci...
static const char * get_string(const char *identifier)
void check_for_duplicate_bindings(struct context *context)
Checks for duplicate key bindings (the same keycode or keysym is configured more than once)...
void errorlog(char *fmt,...)
void start_config_error_nagbar(const char *configpath, bool has_errors)
Launch nagbar to indicate errors in the configuration file.
char * pattern
The pattern/name used to load the font.
static struct ConfigResultIR subcommand_output
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
struct ConfigResultIR * parse_config(const char *input, struct context *context)
#define I3_CFG
The beginning of the prototype for every cfg_ function.
#define SLIST_INSERT_HEAD(head, elm, field)
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
int main(int argc, char *argv[])
void exec_i3_utility(char *name, char *argv[])
exec()s an i3 utility, for example the config file migration script or i3-nagbar. ...
static const char * start_of_line(const char *walk, const char *beginning)
static void push_string(const char *identifier, const char *str)
bool parse_file(const char *f, bool use_nagbar)
Parses the given file by first replacing the variables, then calling parse_config and launching i3-na...
static void clear_stack(void)
#define SLIST_FOREACH(var, head, field)
void extract_workspace_names_from_bindings(void)
Extracts workspace names from keybindings (e.g.
static void push_long(const char *identifier, long num)
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
Used during the config file lexing/parsing to keep the state of the lexer in order to provide useful ...
#define SLIST_FIRST(head)
#define SLIST_HEAD_INITIALIZER(head)
xcb_xrm_database_t * database
#define SLIST_REMOVE_HEAD(head, field)
static struct context * context
void start_nagbar(pid_t *nagbar_pid, char *argv[])
Starts an i3-nagbar instance with the given parameters.
pid_t config_error_nagbar_pid