35 #include <sys/types.h>
40 #include <xcb/xcb_xrm.h>
56 #include "GENERATED_config_enums.h"
58 typedef struct token {
73 #include "GENERATED_config_tokens.h"
81 for (
int c = 0; c < 10; c++) {
82 if (
ctx->stack[c].identifier != NULL &&
83 strcmp(
ctx->stack[c].identifier, identifier) != 0)
85 if (
ctx->stack[c].identifier == NULL) {
87 ctx->stack[c].identifier = identifier;
89 ctx->stack[c].type = STACK_STR;
92 char *prev =
ctx->stack[c].val.str;
102 fprintf(stderr,
"BUG: config_parser stack full. This means either a bug "
103 "in the code, or a new command which contains more than "
104 "10 identified tokens.\n");
109 for (
int c = 0; c < 10; c++) {
110 if (
ctx->stack[c].identifier != NULL) {
114 ctx->stack[c].identifier = identifier;
115 ctx->stack[c].val.num = num;
116 ctx->stack[c].type = STACK_LONG;
123 fprintf(stderr,
"BUG: config_parser stack full. This means either a bug "
124 "in the code, or a new command which contains more than "
125 "10 identified tokens.\n");
130 for (
int c = 0; c < 10; c++) {
131 if (
ctx->stack[c].identifier == NULL)
133 if (strcmp(identifier,
ctx->stack[c].identifier) == 0)
134 return ctx->stack[c].val.str;
140 for (
int c = 0; c < 10; c++) {
141 if (
ctx->stack[c].identifier == NULL)
143 if (strcmp(identifier,
ctx->stack[c].identifier) == 0)
144 return ctx->stack[c].val.num;
150 for (
int c = 0; c < 10; c++) {
151 if (
ctx->stack[c].type == STACK_STR)
152 free(
ctx->stack[c].val.str);
153 ctx->stack[c].identifier = NULL;
154 ctx->stack[c].val.str = NULL;
155 ctx->stack[c].val.num = 0;
163 #include "GENERATED_config_call.h"
176 ctx->has_errors =
true;
182 ctx->state = _next_state;
183 if (
ctx->state == INITIAL) {
189 for (
int i = 0; i <
ctx->statelist_idx; i++) {
190 if ((cmdp_state)(
ctx->statelist[i]) != _next_state) {
193 ctx->statelist_idx = i + 1;
198 ctx->statelist[
ctx->statelist_idx++] = _next_state;
207 while (walk >= beginning && *walk !=
'\n' && *walk !=
'\r') {
222 char *end = strchr(result,
'\n');
231 const char *dumpwalk = input;
233 while (*dumpwalk !=
'\0') {
234 char *next_nl = strchr(dumpwalk,
'\n');
235 if (next_nl != NULL) {
236 DLOG(
"CONFIG(line %3d): %.*s\n", linecnt, (
int)(next_nl - dumpwalk), dumpwalk);
237 dumpwalk = next_nl + 1;
239 DLOG(
"CONFIG(line %3d): %s\n", linecnt, dumpwalk);
244 ctx->state = INITIAL;
245 for (
int i = 0; i < 10; i++) {
246 ctx->statelist[i] = INITIAL;
248 ctx->statelist_idx = 1;
250 const char *walk = input;
251 const size_t len = strlen(input);
267 while ((
size_t)(walk - input) <= len) {
270 while ((*walk ==
' ' || *walk ==
'\t') && *walk !=
'\0')
276 token_handled =
false;
277 for (c = 0; c < ptr->
n; c++) {
288 token_handled =
true;
294 if (strcmp(
token->
name,
"number") == 0) {
298 long int num = strtol(walk, &end, 10);
299 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
300 (errno != 0 && num == 0))
314 token_handled =
true;
318 if (strcmp(
token->
name,
"string") == 0 ||
320 const char *beginning = walk;
325 while (*walk !=
'\0' && (*walk !=
'"' || *(walk - 1) ==
'\\'))
329 while (*walk !=
'\0' && *walk !=
'\r' && *walk !=
'\n')
335 while (*walk !=
' ' && *walk !=
'\t' &&
336 *walk !=
']' && *walk !=
',' &&
337 *walk !=
';' && *walk !=
'\r' &&
338 *walk !=
'\n' && *walk !=
'\0')
342 if (walk != beginning) {
343 char *str =
scalloc(walk - beginning + 1, 1);
346 for (inpos = 0, outpos = 0;
347 inpos < (walk - beginning);
352 if (beginning[inpos] ==
'\\' && beginning[inpos + 1] ==
'"')
354 str[outpos] = beginning[inpos];
365 token_handled =
true;
371 while (*walk !=
'\0' && *walk !=
'\n' && *walk !=
'\r')
374 token_handled =
true;
382 if (*walk ==
'\0' || *walk ==
'\n' || *walk ==
'\r') {
384 token_handled =
true;
400 if (!token_handled) {
404 for (c = 0; c < ptr->
n; c++)
405 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
411 char *possible_tokens =
smalloc(tokenlen + 1);
412 char *tokenwalk = possible_tokens;
413 for (c = 0; c < ptr->
n; c++) {
434 if (c < (ptr->
n - 1)) {
440 sasprintf(&errormessage,
"Expected one of these tokens: %s",
442 free(possible_tokens);
449 char *position =
scalloc(strlen(error_line) + 1, 1);
450 const char *copywalk;
451 for (copywalk = error_line;
452 *copywalk !=
'\n' && *copywalk !=
'\r' && *copywalk !=
'\0';
454 position[(copywalk - error_line)] = (copywalk >= walk ?
'^' : (*copywalk ==
'\t' ?
'\t' :
' '));
455 position[(copywalk - error_line)] =
'\0';
457 ELOG(
"CONFIG: %s\n", errormessage);
463 const char *context_p1_start =
start_of_line(error_line - 2, input);
464 char *context_p1_line =
single_line(context_p1_start);
466 const char *context_p2_start =
start_of_line(context_p1_start - 2, input);
467 char *context_p2_line =
single_line(context_p2_start);
468 ELOG(
"CONFIG: Line %3d: %s\n", linecnt - 2, context_p2_line);
469 free(context_p2_line);
471 ELOG(
"CONFIG: Line %3d: %s\n", linecnt - 1, context_p1_line);
472 free(context_p1_line);
474 ELOG(
"CONFIG: Line %3d: %s\n", linecnt, error_copy);
475 ELOG(
"CONFIG: %s\n", position);
478 for (
int i = 0; i < 2; i++) {
479 char *error_line_end = strchr(error_line,
'\n');
480 if (error_line_end != NULL && *(error_line_end + 1) !=
'\0') {
481 error_line = error_line_end + 1;
483 ELOG(
"CONFIG: Line %3d: %s\n", linecnt + i + 1, error_copy);
491 while ((
size_t)(walk - input) <= len && *walk !=
'\n')
501 bool error_token_found =
false;
502 for (
int i =
ctx->statelist_idx - 1; (i >= 0) && !error_token_found; i--) {
504 for (
int j = 0; j < errptr->
n; j++) {
505 if (strcmp(errptr->
array[j].
name,
"error") != 0)
508 error_token_found =
true;
513 assert(error_token_found);
535 fprintf(stdout,
"# ");
536 vfprintf(stdout, fmt, args);
544 vfprintf(stderr, fmt, args);
550 void cfg_criteria_init(
I3_CFG,
int _state) {
554 void cfg_criteria_add(
I3_CFG,
const char *ctype,
const char *cvalue) {
557 void cfg_criteria_pop_state(
I3_CFG) {
561 int main(
int argc,
char *argv[]) {
563 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);
590 while (*walk !=
'\0') {
597 if (strncasecmp(line,
"bindcode", strlen(
"bindcode")) == 0 ||
598 strncasecmp(line,
"include", strlen(
"include")) == 0 ||
599 strncasecmp(line,
"force_focus_wrapping", strlen(
"force_focus_wrapping")) == 0 ||
600 strncasecmp(line,
"# i3 config file (v4)", strlen(
"# i3 config file (v4)")) == 0 ||
601 strncasecmp(line,
"workspace_layout", strlen(
"workspace_layout")) == 0) {
602 LOG(
"deciding for version 4 due to this line: %.*s\n", (
int)(walk - line), line);
607 if (strncasecmp(line,
"bind", strlen(
"bind")) == 0) {
608 char *bind = strchr(line,
' ');
611 while ((*bind ==
' ' || *bind ==
'\t') && *bind !=
'\0')
615 if ((bind = strchr(bind,
' ')) == NULL)
617 while ((*bind ==
' ' || *bind ==
'\t') && *bind !=
'\0')
621 if (strncasecmp(bind,
"layout", strlen(
"layout")) == 0 ||
622 strncasecmp(bind,
"floating", strlen(
"floating")) == 0 ||
623 strncasecmp(bind,
"workspace", strlen(
"workspace")) == 0 ||
624 strncasecmp(bind,
"focus left", strlen(
"focus left")) == 0 ||
625 strncasecmp(bind,
"focus right", strlen(
"focus right")) == 0 ||
626 strncasecmp(bind,
"focus up", strlen(
"focus up")) == 0 ||
627 strncasecmp(bind,
"focus down", strlen(
"focus down")) == 0 ||
628 strncasecmp(bind,
"border normal", strlen(
"border normal")) == 0 ||
629 strncasecmp(bind,
"border 1pixel", strlen(
"border 1pixel")) == 0 ||
630 strncasecmp(bind,
"border pixel", strlen(
"border pixel")) == 0 ||
631 strncasecmp(bind,
"border borderless", strlen(
"border borderless")) == 0 ||
632 strncasecmp(bind,
"--no-startup-id", strlen(
"--no-startup-id")) == 0 ||
633 strncasecmp(bind,
"bar", strlen(
"bar")) == 0) {
634 LOG(
"deciding for version 4 due to this line: %.*s\n", (
int)(walk - line), line);
661 if (pipe(writepipe) != 0 ||
662 pipe(readpipe) != 0) {
663 warn(
"migrate_config: Could not create pipes");
669 warn(
"Could not fork()");
677 dup2(writepipe[0], 0);
681 dup2(readpipe[1], 1);
683 static char *argv[] = {
696 if (
writeall(writepipe[1], input, size) == -1) {
697 warn(
"Could not write to pipe");
706 int conv_size = 65535;
707 char *converted =
scalloc(conv_size, 1);
708 int read_bytes = 0, ret;
710 if (read_bytes == conv_size) {
712 converted =
srealloc(converted, conv_size);
714 ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
716 warn(
"Cannot read from pipe");
726 if (!WIFEXITED(status)) {
727 fprintf(stderr,
"Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
732 int returncode = WEXITSTATUS(status);
733 if (returncode != 0) {
734 fprintf(stderr,
"Migration process exit code was != 0\n");
735 if (returncode == 2) {
736 fprintf(stderr,
"could not start the migration script\n");
738 }
else if (returncode == 1) {
739 fprintf(stderr,
"This already was a v4 config. Please add the following line to your config file:\n");
740 fprintf(stderr,
"# i3 config file (v4)\n");
754 char *editaction, *pageraction;
755 sasprintf(&editaction,
"i3-sensible-editor \"%s\" && i3-msg reload\n", configpath);
764 (
has_errors ?
"You have an error in your i3 config file!" :
"Your config is outdated. Please fix the warnings to make sure everything works."),
769 (
has_errors ?
"show errors" :
"show warnings"),
782 static void upsert_variable(
struct variables_head *variables,
char *key,
char *value) {
785 if (strcmp(current->
key,
key) != 0) {
797 struct Variable *test = NULL, *loc = NULL;
803 if (strlen(new->key) >= strlen(test->
key))
825 ELOG(
"Failed to open the resource database.\n");
829 database = xcb_xrm_database_from_string(
"");
836 xcb_xrm_resource_get_string(
database, name, NULL, &resource);
865 char buffer[4096], key[512], value[4096], *continuation = NULL;
867 char *old_dir = getcwd(NULL, 0);
871 if ((dir = dirname(dirbuf)) != NULL) {
872 LOG(
"Changing working directory to config file directory %s\n", dir);
873 if (chdir(dir) == -1) {
874 ELOG(
"chdir(%s) failed: %s\n", dir, strerror(errno));
880 if ((fd = open(f, O_RDONLY)) == -1) {
884 if (fstat(fd, &stbuf) == -1) {
888 buf =
scalloc(stbuf.st_size + 1, 1);
890 if ((fstr = fdopen(fd,
"r")) == NULL) {
895 if ((ssize_t)fread(included_file->
raw_contents, 1, stbuf.st_size, fstr) != stbuf.st_size) {
900 bool invalid_sets =
false;
902 while (!feof(fstr)) {
904 continuation = buffer;
905 if (fgets(continuation,
sizeof(buffer) - (continuation - buffer), fstr) == NULL) {
910 if (buffer[strlen(buffer) - 1] !=
'\n' && !feof(fstr)) {
911 ELOG(
"Your line continuation is too long, it exceeds %zd bytes\n",
sizeof(buffer));
916 const bool skip_line = (sscanf(buffer,
"%511s %4095[^\n]", key, value) < 1 || strlen(key) < 3);
917 const bool comment = (key[0] ==
'#');
920 continuation = strstr(buffer,
"\\\n");
925 DLOG(
"line continuation in comment is ignored: \"%.*s\"\n", (
int)strlen(buffer) - 1, buffer);
929 strncpy(buf + strlen(buf), buffer, strlen(buffer) + 1);
932 if (skip_line || comment) {
936 if (strcasecmp(key,
"set") == 0 && *value !=
'\0') {
938 char v_value[4096] = {
'\0'};
940 if (sscanf(value,
"%511s %4095[^\n]", v_key, v_value) < 1) {
941 ELOG(
"Failed to parse variable specification '%s', skipping it.\n", value);
946 if (v_key[0] !=
'$') {
947 ELOG(
"Malformed variable assignment, name has to start with $\n");
954 }
else if (strcasecmp(key,
"set_from_resource") == 0) {
955 char res_name[512] = {
'\0'};
957 char fallback[4096] = {
'\0'};
967 if (sscanf(value,
"%511s %511s %4095[^\n]", v_key, res_name, fallback) < 1) {
968 ELOG(
"Failed to parse resource specification '%s', skipping it.\n", value);
973 if (v_key[0] !=
'$') {
974 ELOG(
"Malformed variable assignment, name has to start with $\n");
980 if (res_value == NULL) {
981 DLOG(
"Could not get resource '%s', using fallback '%s'.\n", res_name, fallback);
1000 struct Variable *current, *nearest;
1001 int extra_bytes = 0;
1007 int extra = (strlen(current->
value) - strlen(current->
key));
1009 for (next = bufcopy;
1010 next < (bufcopy + stbuf.st_size) &&
1011 (next = strcasestr(next, current->
key)) != NULL;
1012 next += strlen(current->
key)) {
1014 extra_bytes += extra;
1021 char *walk = buf, *destwalk;
1022 char *
new =
scalloc(stbuf.st_size + extra_bytes + 1, 1);
1024 while (walk < (buf + stbuf.st_size)) {
1030 int distance = stbuf.st_size;
1034 if ((current->
next_match - walk) < distance) {
1039 if (nearest == NULL) {
1041 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
1042 destwalk += (buf + stbuf.st_size) - walk;
1047 strncpy(destwalk, walk, distance);
1048 strncpy(destwalk + distance, nearest->
value, strlen(nearest->
value));
1049 walk += distance + strlen(nearest->
key);
1050 destwalk += distance + strlen(nearest->
value);
1057 if (!
ctx->assume_v4) {
1063 if (converted != NULL) {
1065 ELOG(
"****************************************************************\n");
1066 ELOG(
"NOTE: Automatically converted configuration file from v3 to v4.\n");
1068 ELOG(
"Please convert your config file to v4. You can use this command:\n");
1069 ELOG(
" mv %s %s.O\n", f, f);
1070 ELOG(
" i3-migrate-config-to-v4 %s.O > %s\n", f, f);
1071 ELOG(
"****************************************************************\n");
1077 LOG(
"**********************************************************************\n");
1078 LOG(
"ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
1079 LOG(
"was not correctly installed on your system?\n");
1080 LOG(
"**********************************************************************\n");
1090 if (
ctx->has_errors) {
1099 ELOG(
"Please convert your configfile first, then fix any remaining errors (see above).\n");
1111 if (chdir(old_dir) == -1) {
1112 ELOG(
"chdir(%s) failed: %s\n", old_dir, strerror(errno));
void check_for_duplicate_bindings(struct context *context)
Checks for duplicate key bindings (the same keycode or keysym is configured more than once).
static struct stack stack
static struct CommandResultIR subcommand_output
static int criteria_next_state
static char * single_line(const char *start)
struct tokenptr cmdp_token_ptr
static void next_state(const cmdp_token *token, struct parser_ctx *ctx)
pid_t config_error_nagbar_pid
static const char * start_of_line(const char *walk, const char *beginning)
static void push_string(struct stack *ctx, const char *identifier, const char *str)
static void clear_stack(struct stack *ctx)
static void parse_config(struct parser_ctx *ctx, const char *input, struct context *context)
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(struct stack *ctx, const char *identifier)
parse_file_result_t parse_file(struct parser_ctx *ctx, const char *f, IncludedFile *included_file)
Parses the given file by first replacing the variables, then calling parse_config and launching i3-na...
static void upsert_variable(struct variables_head *variables, char *key, char *value)
static long get_long(struct stack *ctx, const char *identifier)
static void push_long(struct stack *ctx, const char *identifier, long num)
void free_variables(struct parser_ctx *ctx)
Releases the memory of all variables in ctx.
void start_config_error_nagbar(const char *configpath, bool has_errors)
Launch nagbar to indicate errors in the configuration file.
static int detect_version(char *buf)
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.
static xcb_cursor_context_t * ctx
#define I3_CFG
The beginning of the prototype for every cfg_ function.
@ PARSE_FILE_CONFIG_ERRORS
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_REMOVE_HEAD(head, field)
#define SLIST_INSERT_AFTER(slistelm, elm, field)
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.
List entry struct for an included file.
char * variable_replaced_contents
char * pattern
The pattern/name used to load the font.