35 #include <sys/types.h>
39 #include <xcb/xcb_xrm.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 #include "GENERATED_config_enums.h"
62 typedef struct token {
77 #include "GENERATED_config_tokens.h"
106 for (
int c = 0; c < 10; c++) {
127 fprintf(stderr,
"BUG: config_parser stack full. This means either a bug "
128 "in the code, or a new command which contains more than "
129 "10 identified tokens.\n");
134 for (
int c = 0; c < 10; c++) {
147 fprintf(stderr,
"BUG: config_parser stack full. This means either a bug "
148 "in the code, or a new command which contains more than "
149 "10 identified tokens.\n");
154 for (
int c = 0; c < 10; c++) {
164 for (
int c = 0; c < 10; c++) {
174 for (
int c = 0; c < 10; c++) {
201 #include "GENERATED_config_call.h"
216 if (
state == INITIAL) {
239 while (walk >= beginning && *walk !=
'\n' && *walk !=
'\r') {
254 char *end = strchr(result,
'\n');
263 const char *dumpwalk = input;
265 while (*dumpwalk !=
'\0') {
266 char *next_nl = strchr(dumpwalk,
'\n');
267 if (next_nl != NULL) {
268 DLOG(
"CONFIG(line %3d): %.*s\n", linecnt, (
int)(next_nl - dumpwalk), dumpwalk);
269 dumpwalk = next_nl + 1;
271 DLOG(
"CONFIG(line %3d): %s\n", linecnt, dumpwalk);
284 const char *walk = input;
285 const size_t len = strlen(input);
298 while ((
size_t)(walk - input) <= len) {
301 while ((*walk ==
' ' || *walk ==
'\t') && *walk !=
'\0')
307 token_handled =
false;
308 for (c = 0; c < ptr->
n; c++) {
318 token_handled =
true;
324 if (strcmp(
token->
name,
"number") == 0) {
328 long int num = strtol(walk, &end, 10);
329 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
330 (errno != 0 && num == 0))
343 token_handled =
true;
347 if (strcmp(
token->
name,
"string") == 0 ||
349 const char *beginning = walk;
354 while (*walk !=
'\0' && (*walk !=
'"' || *(walk - 1) ==
'\\'))
358 while (*walk !=
'\0' && *walk !=
'\r' && *walk !=
'\n')
364 while (*walk !=
' ' && *walk !=
'\t' &&
365 *walk !=
']' && *walk !=
',' &&
366 *walk !=
';' && *walk !=
'\r' &&
367 *walk !=
'\n' && *walk !=
'\0')
371 if (walk != beginning) {
372 char *str =
scalloc(walk - beginning + 1, 1);
375 for (inpos = 0, outpos = 0;
376 inpos < (walk - beginning);
381 if (beginning[inpos] ==
'\\' && beginning[inpos + 1] ==
'"')
383 str[outpos] = beginning[inpos];
393 token_handled =
true;
399 while (*walk !=
'\0' && *walk !=
'\n' && *walk !=
'\r')
402 token_handled =
true;
410 if (*walk ==
'\0' || *walk ==
'\n' || *walk ==
'\r') {
412 token_handled =
true;
428 if (!token_handled) {
432 for (c = 0; c < ptr->
n; c++)
433 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
439 char *possible_tokens =
smalloc(tokenlen + 1);
440 char *tokenwalk = possible_tokens;
441 for (c = 0; c < ptr->
n; c++) {
462 if (c < (ptr->
n - 1)) {
468 sasprintf(&errormessage,
"Expected one of these tokens: %s",
470 free(possible_tokens);
477 char *position =
scalloc(strlen(error_line) + 1, 1);
478 const char *copywalk;
479 for (copywalk = error_line;
480 *copywalk !=
'\n' && *copywalk !=
'\r' && *copywalk !=
'\0';
482 position[(copywalk - error_line)] = (copywalk >= walk ?
'^' : (*copywalk ==
'\t' ?
'\t' :
' '));
483 position[(copywalk - error_line)] =
'\0';
485 ELOG(
"CONFIG: %s\n", errormessage);
491 const char *context_p1_start =
start_of_line(error_line - 2, input);
492 char *context_p1_line =
single_line(context_p1_start);
494 const char *context_p2_start =
start_of_line(context_p1_start - 2, input);
495 char *context_p2_line =
single_line(context_p2_start);
496 ELOG(
"CONFIG: Line %3d: %s\n", linecnt - 2, context_p2_line);
497 free(context_p2_line);
499 ELOG(
"CONFIG: Line %3d: %s\n", linecnt - 1, context_p1_line);
500 free(context_p1_line);
502 ELOG(
"CONFIG: Line %3d: %s\n", linecnt, error_copy);
503 ELOG(
"CONFIG: %s\n", position);
506 for (
int i = 0; i < 2; i++) {
507 char *error_line_end = strchr(error_line,
'\n');
508 if (error_line_end != NULL && *(error_line_end + 1) !=
'\0') {
509 error_line = error_line_end + 1;
511 ELOG(
"CONFIG: Line %3d: %s\n", linecnt + i + 1, error_copy);
531 ystr(
"errorposition");
536 while ((
size_t)(walk - input) <= len && *walk !=
'\n')
546 bool error_token_found =
false;
547 for (
int i =
statelist_idx - 1; (i >= 0) && !error_token_found; i--) {
549 for (
int j = 0; j < errptr->
n; j++) {
550 if (strcmp(errptr->
array[j].
name,
"error") != 0)
553 error_token_found =
true;
558 assert(error_token_found);
584 fprintf(stdout,
"# ");
585 vfprintf(stdout, fmt, args);
593 vfprintf(stderr, fmt, args);
599 void cfg_criteria_init(
I3_CFG,
int _state) {
603 void cfg_criteria_add(
I3_CFG,
const char *ctype,
const char *cvalue) {
606 void cfg_criteria_pop_state(
I3_CFG) {
610 int main(
int argc,
char *argv[]) {
612 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);
631 while (*walk !=
'\0') {
638 if (strncasecmp(line,
"bindcode", strlen(
"bindcode")) == 0 ||
639 strncasecmp(line,
"force_focus_wrapping", strlen(
"force_focus_wrapping")) == 0 ||
640 strncasecmp(line,
"# i3 config file (v4)", strlen(
"# i3 config file (v4)")) == 0 ||
641 strncasecmp(line,
"workspace_layout", strlen(
"workspace_layout")) == 0) {
642 LOG(
"deciding for version 4 due to this line: %.*s\n", (
int)(walk - line), line);
647 if (strncasecmp(line,
"bind", strlen(
"bind")) == 0) {
648 char *bind = strchr(line,
' ');
651 while ((*bind ==
' ' || *bind ==
'\t') && *bind !=
'\0')
655 if ((bind = strchr(bind,
' ')) == NULL)
657 while ((*bind ==
' ' || *bind ==
'\t') && *bind !=
'\0')
661 if (strncasecmp(bind,
"layout", strlen(
"layout")) == 0 ||
662 strncasecmp(bind,
"floating", strlen(
"floating")) == 0 ||
663 strncasecmp(bind,
"workspace", strlen(
"workspace")) == 0 ||
664 strncasecmp(bind,
"focus left", strlen(
"focus left")) == 0 ||
665 strncasecmp(bind,
"focus right", strlen(
"focus right")) == 0 ||
666 strncasecmp(bind,
"focus up", strlen(
"focus up")) == 0 ||
667 strncasecmp(bind,
"focus down", strlen(
"focus down")) == 0 ||
668 strncasecmp(bind,
"border normal", strlen(
"border normal")) == 0 ||
669 strncasecmp(bind,
"border 1pixel", strlen(
"border 1pixel")) == 0 ||
670 strncasecmp(bind,
"border pixel", strlen(
"border pixel")) == 0 ||
671 strncasecmp(bind,
"border borderless", strlen(
"border borderless")) == 0 ||
672 strncasecmp(bind,
"--no-startup-id", strlen(
"--no-startup-id")) == 0 ||
673 strncasecmp(bind,
"bar", strlen(
"bar")) == 0) {
674 LOG(
"deciding for version 4 due to this line: %.*s\n", (
int)(walk - line), line);
701 if (pipe(writepipe) != 0 ||
702 pipe(readpipe) != 0) {
703 warn(
"migrate_config: Could not create pipes");
709 warn(
"Could not fork()");
717 dup2(writepipe[0], 0);
721 dup2(readpipe[1], 1);
723 static char *argv[] = {
736 if (
writeall(writepipe[1], input, size) == -1) {
737 warn(
"Could not write to pipe");
746 int conv_size = 65535;
747 char *converted =
scalloc(conv_size, 1);
748 int read_bytes = 0, ret;
750 if (read_bytes == conv_size) {
752 converted =
srealloc(converted, conv_size);
754 ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
756 warn(
"Cannot read from pipe");
766 if (!WIFEXITED(status)) {
767 fprintf(stderr,
"Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
772 int returncode = WEXITSTATUS(status);
773 if (returncode != 0) {
774 fprintf(stderr,
"Migration process exit code was != 0\n");
775 if (returncode == 2) {
776 fprintf(stderr,
"could not start the migration script\n");
778 }
else if (returncode == 1) {
779 fprintf(stderr,
"This already was a v4 config. Please add the following line to your config file:\n");
780 fprintf(stderr,
"# i3 config file (v4)\n");
794 char *editaction, *pageraction;
795 sasprintf(&editaction,
"i3-sensible-editor \"%s\" && i3-msg reload\n", configpath);
804 (
has_errors ?
"You have an error in your i3 config file!" :
"Your config is outdated. Please fix the warnings to make sure everything works."),
809 (
has_errors ?
"show errors" :
"show warnings"),
822 static void upsert_variable(
struct variables_head *variables,
char *key,
char *value) {
825 if (strcmp(current->
key,
key) != 0) {
837 struct Variable *test = NULL, *loc = NULL;
843 if (strlen(new->key) >= strlen(test->
key))
865 ELOG(
"Failed to open the resource database.\n");
869 database = xcb_xrm_database_from_string(
"");
876 xcb_xrm_resource_get_string(
database, name, NULL, &resource);
891 char buffer[4096], key[512], value[4096], *continuation = NULL;
893 if ((fd = open(f, O_RDONLY)) == -1)
894 die(
"Could not open configuration file: %s\n", strerror(errno));
896 if (fstat(fd, &stbuf) == -1)
897 die(
"Could not fstat file: %s\n", strerror(errno));
899 buf =
scalloc(stbuf.st_size + 1, 1);
901 if ((fstr = fdopen(fd,
"r")) == NULL)
902 die(
"Could not fdopen: %s\n", strerror(errno));
906 if ((ssize_t)fread(
current_config, 1, stbuf.st_size, fstr) != stbuf.st_size) {
907 die(
"Could not fread: %s\n", strerror(errno));
911 bool invalid_sets =
false;
913 while (!feof(fstr)) {
915 continuation = buffer;
916 if (fgets(continuation,
sizeof(buffer) - (continuation - buffer), fstr) == NULL) {
919 die(
"Could not read configuration file\n");
921 if (buffer[strlen(buffer) - 1] !=
'\n' && !feof(fstr)) {
922 ELOG(
"Your line continuation is too long, it exceeds %zd bytes\n",
sizeof(buffer));
927 const bool skip_line = (sscanf(buffer,
"%511s %4095[^\n]", key, value) < 1 || strlen(key) < 3);
928 const bool comment = (key[0] ==
'#');
931 continuation = strstr(buffer,
"\\\n");
936 DLOG(
"line continuation in comment is ignored: \"%.*s\"\n", (
int)strlen(buffer) - 1, buffer);
940 strncpy(buf + strlen(buf), buffer, strlen(buffer) + 1);
943 if (skip_line || comment) {
947 if (strcasecmp(key,
"set") == 0 && *value !=
'\0') {
949 char v_value[4096] = {
'\0'};
951 if (sscanf(value,
"%511s %4095[^\n]", v_key, v_value) < 1) {
952 ELOG(
"Failed to parse variable specification '%s', skipping it.\n", value);
957 if (v_key[0] !=
'$') {
958 ELOG(
"Malformed variable assignment, name has to start with $\n");
965 }
else if (strcasecmp(key,
"set_from_resource") == 0) {
966 char res_name[512] = {
'\0'};
968 char fallback[4096] = {
'\0'};
978 if (sscanf(value,
"%511s %511s %4095[^\n]", v_key, res_name, fallback) < 1) {
979 ELOG(
"Failed to parse resource specification '%s', skipping it.\n", value);
984 if (v_key[0] !=
'$') {
985 ELOG(
"Malformed variable assignment, name has to start with $\n");
991 if (res_value == NULL) {
992 DLOG(
"Could not get resource '%s', using fallback '%s'.\n", res_name, fallback);
1011 struct Variable *current, *nearest;
1012 int extra_bytes = 0;
1018 int extra = (strlen(current->
value) - strlen(current->
key));
1020 for (next = bufcopy;
1021 next < (bufcopy + stbuf.st_size) &&
1022 (next = strcasestr(next, current->
key)) != NULL;
1023 next += strlen(current->
key)) {
1025 extra_bytes += extra;
1032 char *walk = buf, *destwalk;
1033 char *
new =
scalloc(stbuf.st_size + extra_bytes + 1, 1);
1035 while (walk < (buf + stbuf.st_size)) {
1041 int distance = stbuf.st_size;
1045 if ((current->
next_match - walk) < distance) {
1050 if (nearest == NULL) {
1052 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
1053 destwalk += (buf + stbuf.st_size) - walk;
1058 strncpy(destwalk, walk, distance);
1059 strncpy(destwalk + distance, nearest->
value, strlen(nearest->
value));
1060 walk += distance + strlen(nearest->
key);
1061 destwalk += distance + strlen(nearest->
value);
1071 if (converted != NULL) {
1073 ELOG(
"****************************************************************\n");
1074 ELOG(
"NOTE: Automatically converted configuration file from v3 to v4.\n");
1076 ELOG(
"Please convert your config file to v4. You can use this command:\n");
1077 ELOG(
" mv %s %s.O\n", f, f);
1078 ELOG(
" i3-migrate-config-to-v4 %s.O > %s\n", f, f);
1079 ELOG(
"****************************************************************\n");
1085 LOG(
"**********************************************************************\n");
1086 LOG(
"ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
1087 LOG(
"was not correctly installed on your system?\n");
1088 LOG(
"**********************************************************************\n");
1097 yajl_gen_free(config_output->
json_gen);
1106 ELOG(
"Please convert your configfile first, then fix any remaining errors (see above).\n");
void check_for_duplicate_bindings(struct context *context)
Checks for duplicate key bindings (the same keycode or keysym is configured more than once).
void reorder_bindings(void)
Reorders bindings by event_state_mask descendingly so that get_binding() correctly matches more speci...
static int criteria_next_state
static void clear_stack(void)
static char * single_line(const char *start)
static void push_long(const char *identifier, long num)
struct tokenptr cmdp_token_ptr
static struct context * context
pid_t config_error_nagbar_pid
static cmdp_state statelist[10]
static const char * start_of_line(const char *walk, const char *beginning)
struct ConfigResultIR * parse_config(const char *input, struct context *context)
static Match current_match
static struct ConfigResultIR command_output
xcb_xrm_database_t * database
static char * migrate_config(char *input, off_t size)
static char * get_resource(char *name)
static const char * get_string(const char *identifier)
static long get_long(const char *identifier)
static void upsert_variable(struct variables_head *variables, char *key, char *value)
static struct ConfigResultIR subcommand_output
static struct stack_entry stack[10]
static void push_string(const char *identifier, const char *str)
void start_config_error_nagbar(const char *configpath, bool has_errors)
Launch nagbar to indicate errors in the configuration file.
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 int detect_version(char *buf)
static void next_state(const cmdp_token *token)
void errorlog(char *fmt,...)
void debuglog(char *fmt,...)
int main(int argc, char *argv[])
xcb_connection_t * conn
XCB connection and root screen.
void exec_i3_utility(char *name, char *argv[])
exec()s an i3 utility, for example the config file migration script or i3-nagbar.
void start_nagbar(pid_t *nagbar_pid, char *argv[])
Starts an i3-nagbar instance with the given parameters.
const char * i3_version
Git commit identifier, from version.c.
void extract_workspace_names_from_bindings(void)
Extracts workspace names from keybindings (e.g.
#define I3_CFG
The beginning of the prototype for every cfg_ function.
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 ...
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
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...
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
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...
#define SLIST_FOREACH(var, head, field)
#define SLIST_INSERT_HEAD(head, elm, field)
#define SLIST_EMPTY(head)
#define SLIST_FIRST(head)
#define SLIST_HEAD_INITIALIZER(head)
#define SLIST_REMOVE_HEAD(head, field)
#define SLIST_INSERT_AFTER(slistelm, elm, field)
union stack_entry::@2 val
enum stack_entry::@1 type
An intermediate reprsentation of the result of a parse_config call.
Used during the config file lexing/parsing to keep the state of the lexer in order to provide useful ...
Holds a user-assigned variable for parsing the configuration file.
A "match" is a data structure which acts like a mask or expression to match certain windows or not.
char * pattern
The pattern/name used to load the font.