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");
152 for (
int c = 0; c < 10; c++) {
153 if (
stack[c].identifier == NULL)
155 if (strcmp(identifier,
stack[c].identifier) == 0)
162 for (
int c = 0; c < 10; c++) {
163 if (
stack[c].identifier == NULL)
165 if (strcmp(identifier,
stack[c].identifier) == 0)
172 for (
int c = 0; c < 10; c++) {
188 typedef struct criterion {
195 static
TAILQ_HEAD(criteria_head, criterion) criteria =
203 static
void push_criterion(
void *unused_criteria, const
char *type,
205 struct criterion *criterion = malloc(
sizeof(
struct criterion));
206 criterion->type = strdup(type);
207 criterion->value = strdup(value);
216 static void clear_criteria(
void *unused_criteria) {
217 struct criterion *criterion;
220 free(criterion->type);
221 free(criterion->value);
270 statelist_idx = i + 1;
275 statelist[statelist_idx++] = _next_state;
284 while (*walk !=
'\n' && *walk !=
'\r' && walk >= beginning) {
299 char *end = strchr(result,
'\n');
308 const char *dumpwalk = input;
310 while (*dumpwalk !=
'\0') {
311 char *next_nl = strchr(dumpwalk,
'\n');
312 if (next_nl != NULL) {
313 DLOG(
"CONFIG(line %3d): %.*s\n", linecnt, (
int)(next_nl - dumpwalk), dumpwalk);
314 dumpwalk = next_nl + 1;
316 DLOG(
"CONFIG(line %3d): %s\n", linecnt, dumpwalk);
329 const char *walk = input;
330 const size_t len = strlen(input);
343 while ((
size_t)(walk - input) <= len) {
346 while ((*walk ==
' ' || *walk ==
'\t') && *walk !=
'\0')
352 token_handled =
false;
353 for (c = 0; c < ptr->
n; c++) {
354 token = &(ptr->
array[c]);
357 if (token->
name[0] ==
'\'') {
358 if (strncasecmp(walk, token->
name + 1, strlen(token->
name) - 1) == 0) {
361 walk += strlen(token->
name) - 1;
363 token_handled =
true;
369 if (strcmp(token->
name,
"number") == 0) {
373 long int num = strtol(walk, &end, 10);
374 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
375 (errno != 0 && num == 0))
388 token_handled =
true;
392 if (strcmp(token->
name,
"string") == 0 ||
393 strcmp(token->
name,
"word") == 0) {
394 const char *beginning = walk;
399 while (*walk !=
'\0' && (*walk !=
'"' || *(walk - 1) ==
'\\'))
402 if (token->
name[0] ==
's') {
403 while (*walk !=
'\0' && *walk !=
'\r' && *walk !=
'\n')
409 while (*walk !=
' ' && *walk !=
'\t' &&
410 *walk !=
']' && *walk !=
',' &&
411 *walk !=
';' && *walk !=
'\r' &&
412 *walk !=
'\n' && *walk !=
'\0')
416 if (walk != beginning) {
417 char *str =
scalloc(walk - beginning + 1);
420 for (inpos = 0, outpos = 0;
421 inpos < (walk - beginning);
426 if (beginning[inpos] ==
'\\' && beginning[inpos + 1] ==
'"')
428 str[outpos] = beginning[inpos];
438 token_handled =
true;
443 if (strcmp(token->
name,
"line") == 0) {
444 while (*walk !=
'\0' && *walk !=
'\n' && *walk !=
'\r')
447 token_handled =
true;
453 if (strcmp(token->
name,
"end") == 0) {
455 if (*walk ==
'\0' || *walk ==
'\n' || *walk ==
'\r') {
457 token_handled =
true;
473 if (!token_handled) {
477 for (c = 0; c < ptr->
n; c++)
478 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
484 char *possible_tokens =
smalloc(tokenlen + 1);
485 char *tokenwalk = possible_tokens;
486 for (c = 0; c < ptr->
n; c++) {
487 token = &(ptr->
array[c]);
488 if (token->
name[0] ==
'\'') {
492 strcpy(tokenwalk, token->
name + 1);
493 tokenwalk += strlen(token->
name + 1);
498 if (strcmp(token->
name,
"error") == 0)
503 strcpy(tokenwalk, token->
name);
504 tokenwalk += strlen(token->
name);
507 if (c < (ptr->
n - 1)) {
513 sasprintf(&errormessage,
"Expected one of these tokens: %s",
515 free(possible_tokens);
522 char *position =
scalloc(strlen(error_line) + 1);
523 const char *copywalk;
524 for (copywalk = error_line;
525 *copywalk !=
'\n' && *copywalk !=
'\r' && *copywalk !=
'\0';
527 position[(copywalk - error_line)] = (copywalk >= walk ?
'^' : (*copywalk ==
'\t' ?
'\t' :
' '));
528 position[(copywalk - error_line)] =
'\0';
530 ELOG(
"CONFIG: %s\n", errormessage);
536 const char *context_p1_start =
start_of_line(error_line - 2, input);
537 char *context_p1_line =
single_line(context_p1_start);
539 const char *context_p2_start =
start_of_line(context_p1_start - 2, input);
540 char *context_p2_line =
single_line(context_p2_start);
541 ELOG(
"CONFIG: Line %3d: %s\n", linecnt - 2, context_p2_line);
542 free(context_p2_line);
544 ELOG(
"CONFIG: Line %3d: %s\n", linecnt - 1, context_p1_line);
545 free(context_p1_line);
547 ELOG(
"CONFIG: Line %3d: %s\n", linecnt, error_copy);
548 ELOG(
"CONFIG: %s\n", position);
551 for (
int i = 0; i < 2; i++) {
552 char *error_line_end = strchr(error_line,
'\n');
553 if (error_line_end != NULL && *(error_line_end + 1) !=
'\0') {
554 error_line = error_line_end + 1;
556 ELOG(
"CONFIG: Line %3d: %s\n", linecnt + i + 1, error_copy);
576 ystr(
"errorposition");
581 while ((
size_t)(walk - input) <= len && *walk !=
'\n')
591 bool error_token_found =
false;
592 for (
int i =
statelist_idx - 1; (i >= 0) && !error_token_found; i--) {
594 for (
int j = 0; j < errptr->
n; j++) {
595 if (strcmp(errptr->
array[j].
name,
"error") != 0)
598 error_token_found =
true;
603 assert(error_token_found);
629 fprintf(stdout,
"# ");
630 vfprintf(stdout, fmt, args);
638 vfprintf(stderr, fmt, args);
644 void cfg_criteria_init(
I3_CFG,
int _state) {
648 void cfg_criteria_add(
I3_CFG,
const char *ctype,
const char *cvalue) {
651 void cfg_criteria_pop_state(
I3_CFG) {
655 int main(
int argc,
char *argv[]) {
657 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);
660 struct context context;
676 while (*walk !=
'\0') {
683 if (strncasecmp(line,
"bindcode", strlen(
"bindcode")) == 0 ||
684 strncasecmp(line,
"force_focus_wrapping", strlen(
"force_focus_wrapping")) == 0 ||
685 strncasecmp(line,
"# i3 config file (v4)", strlen(
"# i3 config file (v4)")) == 0 ||
686 strncasecmp(line,
"workspace_layout", strlen(
"workspace_layout")) == 0) {
687 LOG(
"deciding for version 4 due to this line: %.*s\n", (
int)(walk - line), line);
692 if (strncasecmp(line,
"bind", strlen(
"bind")) == 0) {
693 char *bind = strchr(line,
' ');
696 while ((*bind ==
' ' || *bind ==
'\t') && *bind !=
'\0')
700 if ((bind = strchr(bind,
' ')) == NULL)
702 while ((*bind ==
' ' || *bind ==
'\t') && *bind !=
'\0')
706 if (strncasecmp(bind,
"layout", strlen(
"layout")) == 0 ||
707 strncasecmp(bind,
"floating", strlen(
"floating")) == 0 ||
708 strncasecmp(bind,
"workspace", strlen(
"workspace")) == 0 ||
709 strncasecmp(bind,
"focus left", strlen(
"focus left")) == 0 ||
710 strncasecmp(bind,
"focus right", strlen(
"focus right")) == 0 ||
711 strncasecmp(bind,
"focus up", strlen(
"focus up")) == 0 ||
712 strncasecmp(bind,
"focus down", strlen(
"focus down")) == 0 ||
713 strncasecmp(bind,
"border normal", strlen(
"border normal")) == 0 ||
714 strncasecmp(bind,
"border 1pixel", strlen(
"border 1pixel")) == 0 ||
715 strncasecmp(bind,
"border pixel", strlen(
"border pixel")) == 0 ||
716 strncasecmp(bind,
"border borderless", strlen(
"border borderless")) == 0 ||
717 strncasecmp(bind,
"--no-startup-id", strlen(
"--no-startup-id")) == 0 ||
718 strncasecmp(bind,
"bar", strlen(
"bar")) == 0) {
719 LOG(
"deciding for version 4 due to this line: %.*s\n", (
int)(walk - line), line);
746 if (pipe(writepipe) != 0 ||
747 pipe(readpipe) != 0) {
748 warn(
"migrate_config: Could not create pipes");
754 warn(
"Could not fork()");
762 dup2(writepipe[0], 0);
766 dup2(readpipe[1], 1);
768 static char *argv[] = {
783 while (written < size) {
784 if ((ret = write(writepipe[1], input + written, size - written)) < 0) {
785 warn(
"Could not write to pipe");
796 int conv_size = 65535;
797 char *converted = malloc(conv_size);
800 if (read_bytes == conv_size) {
802 converted = realloc(converted, conv_size);
804 ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
806 warn(
"Cannot read from pipe");
816 if (!WIFEXITED(status)) {
817 fprintf(stderr,
"Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
821 int returncode = WEXITSTATUS(status);
822 if (returncode != 0) {
823 fprintf(stderr,
"Migration process exit code was != 0\n");
824 if (returncode == 2) {
825 fprintf(stderr,
"could not start the migration script\n");
827 }
else if (returncode == 1) {
828 fprintf(stderr,
"This already was a v4 config. Please add the following line to your config file:\n");
829 fprintf(stderr,
"# i3 config file (v4)\n");
845 int fd, ret, read_bytes = 0;
849 char buffer[1026], key[512], value[512];
851 if ((fd = open(f, O_RDONLY)) == -1)
852 die(
"Could not open configuration file: %s\n", strerror(errno));
854 if (fstat(fd, &stbuf) == -1)
855 die(
"Could not fstat file: %s\n", strerror(errno));
857 buf =
scalloc((stbuf.st_size + 1) *
sizeof(
char));
858 while (read_bytes < stbuf.st_size) {
859 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
860 die(
"Could not read(): %s\n", strerror(errno));
864 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
865 die(
"Could not lseek: %s\n", strerror(errno));
867 if ((fstr = fdopen(fd,
"r")) == NULL)
868 die(
"Could not fdopen: %s\n", strerror(errno));
870 while (!feof(fstr)) {
871 if (fgets(buffer, 1024, fstr) == NULL) {
874 die(
"Could not read configuration file\n");
878 if (sscanf(buffer,
"%s %[^\n]", key, value) < 1 ||
879 key[0] ==
'#' || strlen(key) < 3)
882 if (strcasecmp(key,
"set") == 0) {
883 if (value[0] !=
'$') {
884 ELOG(
"Malformed variable assignment, name has to start with $\n");
889 char *v_key = value, *v_value;
890 if (strstr(value,
" ") == NULL && strstr(value,
"\t") == NULL) {
891 ELOG(
"Malformed variable assignment, need a value\n");
895 if (!(v_value = strstr(value,
" ")))
896 v_value = strstr(value,
"\t");
899 while (*v_value ==
'\t' || *v_value ==
' ')
906 DLOG(
"Got new variable %s = %s\n", v_key, v_value);
921 int extra = (strlen(current->
value) - strlen(current->
key));
924 next < (bufcopy + stbuf.st_size) &&
925 (next = strcasestr(next, current->
key)) != NULL;
926 next += strlen(current->
key)) {
928 extra_bytes += extra;
935 char *walk = buf, *destwalk;
936 char *
new =
smalloc((stbuf.st_size + extra_bytes + 1) *
sizeof(
char));
938 while (walk < (buf + stbuf.st_size)) {
943 int distance = stbuf.st_size;
947 if ((current->
next_match - walk) < distance) {
952 if (nearest == NULL) {
954 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
955 destwalk += (buf + stbuf.st_size) - walk;
960 strncpy(destwalk, walk, distance);
961 strncpy(destwalk + distance, nearest->
value, strlen(nearest->
value));
962 walk += distance + strlen(nearest->
key);
963 destwalk += distance + strlen(nearest->
value);
973 if (converted != NULL) {
975 ELOG(
"****************************************************************\n");
976 ELOG(
"NOTE: Automatically converted configuration file from v3 to v4.\n");
978 ELOG(
"Please convert your config file to v4. You can use this command:\n");
979 ELOG(
" mv %s %s.O\n", f, f);
980 ELOG(
" i3-migrate-config-to-v4 %s.O > %s\n", f, f);
981 ELOG(
"****************************************************************\n");
987 LOG(
"**********************************************************************\n");
988 LOG(
"ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
989 LOG(
"was not correctly installed on your system?\n");
990 LOG(
"**********************************************************************\n");
995 context =
scalloc(
sizeof(
struct context));
999 yajl_gen_free(config_output->
json_gen);
1004 ELOG(
"FYI: You are using i3 version " I3_VERSION
"\n");
1006 ELOG(
"Please convert your configfile first, then fix any remaining errors (see above).\n");
1010 sasprintf(&editaction,
"i3-sensible-editor \"%s\" && i3-msg reload\n", f);
1019 (context->
has_errors ?
"You have an error in your i3 config file!" :
"Your config is outdated. Please fix the warnings to make sure everything works."),
1024 (context->
has_errors ?
"show errors" :
"show warnings"),
static char * migrate_config(char *input, off_t size)
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
#define SLIST_FOREACH(var, head, field)
static void GENERATED_call(const int call_identifier, struct CommandResultIR *result)
Holds a user-assigned variable for parsing the configuration file.
void check_for_duplicate_bindings(struct context *context)
Checks for duplicate key bindings (the same keycode or keysym is configured more than once)...
#define TAILQ_REMOVE(head, elm, field)
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 struct context * context
static void clear_stack(void)
static struct ConfigResultIR subcommand_output
static void push_string(const char *identifier, const char *str)
struct tokenptr cmdp_token_ptr
#define SLIST_INSERT_HEAD(head, elm, field)
static char * single_line(const char *start)
#define TAILQ_FIRST(head)
static int detect_version(char *buf)
static struct stack_entry stack[10]
static Match current_match
pid_t config_error_nagbar_pid
static int criteria_next_state
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...
#define SLIST_REMOVE_HEAD(head, field)
void errorlog(char *fmt,...)
#define SLIST_EMPTY(head)
static const char * get_string(const char *identifier)
void start_nagbar(pid_t *nagbar_pid, char *argv[])
Starts an i3-nagbar instance with the given parameters.
Used during the config file lexing/parsing to keep the state of the lexer in order to provide useful ...
char * pattern
The pattern/name used to load the font.
#define TAILQ_EMPTY(head)
#define TAILQ_ENTRY(type)
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
struct ConfigResultIR * parse_config(const char *input, struct context *context)
void debuglog(char *fmt,...)
#define SLIST_HEAD_INITIALIZER(head)
static cmdp_token_ptr tokens[51]
void * scalloc(size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
void parse_file(const char *f)
Parses the given file by first replacing the variables, then calling parse_config and possibly launch...
static void next_state(const cmdp_token *token)
static long get_long(const char *identifier)
#define TAILQ_INSERT_TAIL(head, elm, field)
static struct ConfigResultIR command_output
static void push_long(const char *identifier, long num)
#define TAILQ_HEAD_INITIALIZER(head)
#define SLIST_HEAD(name, type)
int main(int argc, char *argv[])
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
static cmdp_state statelist[10]
#define I3_CFG
The beginning of the prototype for every cfg_ function.
union stack_entry::@3 val
enum stack_entry::@2 type
#define TAILQ_HEAD(name, type)
#define SLIST_FIRST(head)