40#include <xcb/xcb_xrm.h>
56#include "GENERATED_config_enums.h"
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"
174 ctx->has_errors =
true;
180 ctx->state = _next_state;
181 if (
ctx->state == INITIAL) {
187 for (
int i = 0; i <
ctx->statelist_idx; i++) {
188 if ((cmdp_state)(
ctx->statelist[i]) != _next_state) {
191 ctx->statelist_idx = i + 1;
196 ctx->statelist[
ctx->statelist_idx++] = _next_state;
205 while (walk >= beginning && *walk !=
'\n' && *walk !=
'\r') {
220 char *end = strchr(result,
'\n');
229 const char *dumpwalk = input;
231 while (*dumpwalk !=
'\0') {
232 char *next_nl = strchr(dumpwalk,
'\n');
233 if (next_nl != NULL) {
234 DLOG(
"CONFIG(line %3d): %.*s\n", linecnt, (
int)(next_nl - dumpwalk), dumpwalk);
235 dumpwalk = next_nl + 1;
237 DLOG(
"CONFIG(line %3d): %s\n", linecnt, dumpwalk);
242 ctx->state = INITIAL;
243 for (
int i = 0; i < 10; i++) {
244 ctx->statelist[i] = INITIAL;
246 ctx->statelist_idx = 1;
248 const char *walk = input;
249 const size_t len = strlen(input);
264 while ((
size_t)(walk - input) <= len) {
267 while ((*walk ==
' ' || *walk ==
'\t') && *walk !=
'\0')
271 token_handled =
false;
272 for (c = 0; c < ptr->
n; c++) {
283 token_handled =
true;
289 if (strcmp(
token->
name,
"number") == 0) {
293 long int num = strtol(walk, &end, 10);
294 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
295 (errno != 0 && num == 0))
309 token_handled =
true;
313 if (strcmp(
token->
name,
"string") == 0 ||
315 const char *beginning = walk;
320 while (*walk !=
'\0' && (*walk !=
'"' || *(walk - 1) ==
'\\'))
324 while (*walk !=
'\0' && *walk !=
'\r' && *walk !=
'\n')
330 while (*walk !=
' ' && *walk !=
'\t' &&
331 *walk !=
']' && *walk !=
',' &&
332 *walk !=
';' && *walk !=
'\r' &&
333 *walk !=
'\n' && *walk !=
'\0')
337 if (walk != beginning) {
338 char *str =
scalloc(walk - beginning + 1, 1);
341 for (inpos = 0, outpos = 0;
342 inpos < (walk - beginning);
347 if (beginning[inpos] ==
'\\' && beginning[inpos + 1] ==
'"')
349 str[outpos] = beginning[inpos];
360 token_handled =
true;
366 while (*walk !=
'\0' && *walk !=
'\n' && *walk !=
'\r')
369 token_handled =
true;
376 if (*walk ==
'\0' || *walk ==
'\n' || *walk ==
'\r') {
378 token_handled =
true;
393 if (!token_handled) {
397 for (c = 0; c < ptr->
n; c++)
398 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
404 char *possible_tokens =
smalloc(tokenlen + 1);
405 char *tokenwalk = possible_tokens;
406 for (c = 0; c < ptr->
n; c++) {
427 if (c < (ptr->
n - 1)) {
433 sasprintf(&errormessage,
"Expected one of these tokens: %s",
435 free(possible_tokens);
442 char *position =
scalloc(strlen(error_line) + 1, 1);
443 const char *copywalk;
444 for (copywalk = error_line;
445 *copywalk !=
'\n' && *copywalk !=
'\r' && *copywalk !=
'\0';
447 position[(copywalk - error_line)] = (copywalk >= walk ?
'^' : (*copywalk ==
'\t' ?
'\t' :
' '));
448 position[(copywalk - error_line)] =
'\0';
450 ELOG(
"CONFIG: %s\n", errormessage);
456 const char *context_p1_start =
start_of_line(error_line - 2, input);
457 char *context_p1_line =
single_line(context_p1_start);
459 const char *context_p2_start =
start_of_line(context_p1_start - 2, input);
460 char *context_p2_line =
single_line(context_p2_start);
461 ELOG(
"CONFIG: Line %3d: %s\n", linecnt - 2, context_p2_line);
462 free(context_p2_line);
464 ELOG(
"CONFIG: Line %3d: %s\n", linecnt - 1, context_p1_line);
465 free(context_p1_line);
467 ELOG(
"CONFIG: Line %3d: %s\n", linecnt, error_copy);
468 ELOG(
"CONFIG: %s\n", position);
471 for (
int i = 0; i < 2; i++) {
472 char *error_line_end = strchr(error_line,
'\n');
473 if (error_line_end != NULL && *(error_line_end + 1) !=
'\0') {
474 error_line = error_line_end + 1;
476 ELOG(
"CONFIG: Line %3d: %s\n", linecnt + i + 1, error_copy);
484 while ((
size_t)(walk - input) <= len && *walk !=
'\n')
494 bool error_token_found =
false;
495 for (
int i =
ctx->statelist_idx - 1; (i >= 0) && !error_token_found; i--) {
497 for (
int j = 0; j < errptr->
n; j++) {
498 if (strcmp(errptr->
array[j].
name,
"error") != 0)
501 error_token_found =
true;
506 assert(error_token_found);
528 fprintf(stdout,
"# ");
529 vfprintf(stdout, fmt, args);
537 vfprintf(stderr, fmt, args);
543void cfg_criteria_init(
I3_CFG,
int _state) {
547void cfg_criteria_add(
I3_CFG,
const char *ctype,
const char *cvalue) {
550void cfg_criteria_pop_state(
I3_CFG) {
554int main(
int argc,
char *argv[]) {
556 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);
583 while (*walk !=
'\0') {
590 if (strncasecmp(line,
"bindcode", strlen(
"bindcode")) == 0 ||
591 strncasecmp(line,
"include", strlen(
"include")) == 0 ||
592 strncasecmp(line,
"force_focus_wrapping", strlen(
"force_focus_wrapping")) == 0 ||
593 strncasecmp(line,
"# i3 config file (v4)", strlen(
"# i3 config file (v4)")) == 0 ||
594 strncasecmp(line,
"workspace_layout", strlen(
"workspace_layout")) == 0) {
595 LOG(
"deciding for version 4 due to this line: %.*s\n", (
int)(walk - line), line);
600 if (strncasecmp(line,
"bind", strlen(
"bind")) == 0) {
601 char *bind = strchr(line,
' ');
604 while ((*bind ==
' ' || *bind ==
'\t') && *bind !=
'\0')
608 if ((bind = strchr(bind,
' ')) == NULL)
610 while ((*bind ==
' ' || *bind ==
'\t') && *bind !=
'\0')
614 if (strncasecmp(bind,
"layout", strlen(
"layout")) == 0 ||
615 strncasecmp(bind,
"floating", strlen(
"floating")) == 0 ||
616 strncasecmp(bind,
"workspace", strlen(
"workspace")) == 0 ||
617 strncasecmp(bind,
"focus left", strlen(
"focus left")) == 0 ||
618 strncasecmp(bind,
"focus right", strlen(
"focus right")) == 0 ||
619 strncasecmp(bind,
"focus up", strlen(
"focus up")) == 0 ||
620 strncasecmp(bind,
"focus down", strlen(
"focus down")) == 0 ||
621 strncasecmp(bind,
"border normal", strlen(
"border normal")) == 0 ||
622 strncasecmp(bind,
"border 1pixel", strlen(
"border 1pixel")) == 0 ||
623 strncasecmp(bind,
"border pixel", strlen(
"border pixel")) == 0 ||
624 strncasecmp(bind,
"border borderless", strlen(
"border borderless")) == 0 ||
625 strncasecmp(bind,
"--no-startup-id", strlen(
"--no-startup-id")) == 0 ||
626 strncasecmp(bind,
"bar", strlen(
"bar")) == 0) {
627 LOG(
"deciding for version 4 due to this line: %.*s\n", (
int)(walk - line), line);
654 if (pipe(writepipe) != 0 ||
655 pipe(readpipe) != 0) {
656 warn(
"migrate_config: Could not create pipes");
662 warn(
"Could not fork()");
670 dup2(writepipe[0], 0);
674 dup2(readpipe[1], 1);
676 static char *argv[] = {
689 if (
writeall(writepipe[1], input, size) == -1) {
690 warn(
"Could not write to pipe");
699 int conv_size = 65535;
700 char *converted =
scalloc(conv_size, 1);
701 int read_bytes = 0, ret;
703 if (read_bytes == conv_size) {
705 converted =
srealloc(converted, conv_size);
707 ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
709 warn(
"Cannot read from pipe");
719 if (!WIFEXITED(status)) {
720 fprintf(stderr,
"Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
725 int returncode = WEXITSTATUS(status);
726 if (returncode != 0) {
727 fprintf(stderr,
"Migration process exit code was != 0\n");
728 if (returncode == 2) {
729 fprintf(stderr,
"could not start the migration script\n");
731 }
else if (returncode == 1) {
732 fprintf(stderr,
"This already was a v4 config. Please add the following line to your config file:\n");
733 fprintf(stderr,
"# i3 config file (v4)\n");
747 char *editaction, *pageraction;
748 sasprintf(&editaction,
"i3-sensible-editor \"%s\" && i3-msg reload\n", configpath);
757 (
has_errors ?
"You have an error in your i3 config file!" :
"Your config is outdated. Please fix the warnings to make sure everything works."),
762 (
has_errors ?
"show errors" :
"show warnings"),
775static void upsert_variable(
struct variables_head *variables,
char *key,
char *value) {
778 if (strcmp(current->
key,
key) != 0) {
790 struct Variable *test = NULL, *loc = NULL;
796 if (strlen(new->key) >= strlen(test->
key))
818 ELOG(
"Failed to open the resource database.\n");
822 database = xcb_xrm_database_from_string(
"");
829 xcb_xrm_resource_get_string(
database, name, NULL, &resource);
858 char buffer[4096], key[512], value[4096], *continuation = NULL;
860 char *old_dir = getcwd(NULL, 0);
864 if ((dir = dirname(dirbuf)) != NULL) {
865 LOG(
"Changing working directory to config file directory %s\n", dir);
866 if (chdir(dir) == -1) {
867 ELOG(
"chdir(%s) failed: %s\n", dir, strerror(errno));
873 if ((fd = open(f, O_RDONLY)) == -1) {
877 if (fstat(fd, &stbuf) == -1) {
881 buf =
scalloc(stbuf.st_size + 1, 1);
883 if ((fstr = fdopen(fd,
"r")) == NULL) {
888 if ((ssize_t)fread(included_file->
raw_contents, 1, stbuf.st_size, fstr) != stbuf.st_size) {
893 bool invalid_sets =
false;
895 while (!feof(fstr)) {
897 continuation = buffer;
898 if (fgets(continuation,
sizeof(buffer) - (continuation - buffer), fstr) == NULL) {
903 if (buffer[strlen(buffer) - 1] !=
'\n' && !feof(fstr)) {
904 ELOG(
"Your line continuation is too long, it exceeds %zd bytes\n",
sizeof(buffer));
909 const bool skip_line = (sscanf(buffer,
"%511s %4095[^\n]", key, value) < 1 || strlen(key) < 3);
910 const bool comment = (key[0] ==
'#');
913 continuation = strstr(buffer,
"\\\n");
918 DLOG(
"line continuation in comment is ignored: \"%.*s\"\n", (
int)strlen(buffer) - 1, buffer);
922 strcpy(buf + strlen(buf), buffer);
925 if (skip_line || comment) {
929 if (strcasecmp(key,
"set") == 0 && *value !=
'\0') {
931 char v_value[4096] = {
'\0'};
933 if (sscanf(value,
"%511s %4095[^\n]", v_key, v_value) < 1) {
934 ELOG(
"Failed to parse variable specification '%s', skipping it.\n", value);
939 if (v_key[0] !=
'$') {
940 ELOG(
"Malformed variable assignment, name has to start with $\n");
947 }
else if (strcasecmp(key,
"set_from_resource") == 0) {
948 char res_name[512] = {
'\0'};
950 char fallback[4096] = {
'\0'};
960 if (sscanf(value,
"%511s %511s %4095[^\n]", v_key, res_name, fallback) < 1) {
961 ELOG(
"Failed to parse resource specification '%s', skipping it.\n", value);
966 if (v_key[0] !=
'$') {
967 ELOG(
"Malformed variable assignment, name has to start with $\n");
973 if (res_value == NULL) {
974 DLOG(
"Could not get resource '%s', using fallback '%s'.\n", res_name, fallback);
1000 int extra = (strlen(current->
value) - strlen(current->
key));
1002 for (next = bufcopy;
1003 next < (bufcopy + stbuf.st_size) &&
1004 (next = strcasestr(next, current->
key)) != NULL;) {
1008 char *end = next + strlen(current->
key);
1009 while (next < end) {
1012 extra_bytes += extra;
1019 char *walk = buf, *destwalk;
1020 char *
new =
scalloc(stbuf.st_size + extra_bytes + 1, 1);
1022 while (walk < (buf + stbuf.st_size)) {
1028 int distance = stbuf.st_size;
1032 if ((current->
next_match - walk) < distance) {
1037 if (nearest == NULL) {
1039 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
1040 destwalk += (buf + stbuf.st_size) - walk;
1045 strncpy(destwalk, walk, distance);
1046 strcpy(destwalk + distance, nearest->
value);
1047 walk += distance + strlen(nearest->
key);
1048 destwalk += distance + strlen(nearest->
value);
1055 if (!
ctx->assume_v4) {
1061 if (converted != NULL) {
1063 ELOG(
"****************************************************************\n");
1064 ELOG(
"NOTE: Automatically converted configuration file from v3 to v4.\n");
1066 ELOG(
"Please convert your config file to v4. You can use this command:\n");
1067 ELOG(
" mv %s %s.O\n", f, f);
1068 ELOG(
" i3-migrate-config-to-v4 %s.O > %s\n", f, f);
1069 ELOG(
"****************************************************************\n");
1075 LOG(
"**********************************************************************\n");
1076 LOG(
"ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
1077 LOG(
"was not correctly installed on your system?\n");
1078 LOG(
"**********************************************************************\n");
1088 if (
ctx->has_errors) {
1097 ELOG(
"Please convert your configfile first, then fix any remaining errors (see above).\n");
1109 if (chdir(old_dir) == -1) {
1110 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
struct tokenptr cmdp_token_ptr
static char * migrate_config(char *input, off_t size)
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 char * single_line(const char *start)
static void push_string(struct stack *ctx, const char *identifier, const char *str)
static void clear_stack(struct stack *ctx)
static char * get_resource(char *name)
static void parse_config(struct parser_ctx *ctx, const char *input, struct context *context)
xcb_xrm_database_t * database
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 const char * get_string(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 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
void errorlog(char *fmt,...)
void debuglog(char *fmt,...)
int main(int argc, char *argv[])
xcb_connection_t * conn
XCB connection and root screen.
#define I3_CFG
The beginning of the prototype for every cfg_ function.
@ PARSE_FILE_CONFIG_ERRORS
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 ...
char * sstrdup(const char *str)
Safe-wrapper around strdup 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...
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 * smalloc(size_t size)
Safe-wrapper around malloc 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 representation 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.