Implement include config directive (#4420)
The implementation uses wordexp(3) just like sway:
https://github.com/i3/i3/issues/1197#issuecomment-226844106
Thanks to jajm for their implementation at
bb55709d0a
This required refactoring the config parser to be re-entrant
(no more global state) and to return an error instead of dying.
In case a file cannot be opened, i3 reports an error but proceeds with the
remaining configuration.
Key bindings can be overwritten or removed using the new --remove flag of the
bindsym/bindcode directive.
All files that were successfully included are displayed in i3 --moreversion.
One caveat is i3 config file variable expansion, see the note in the userguide.
fixes #4192
This commit is contained in:
committed by
GitHub
parent
4c93f61353
commit
eaa5e636f9
@@ -39,6 +39,7 @@ CFGFUN(criteria_init, int _state);
|
||||
CFGFUN(criteria_add, const char *ctype, const char *cvalue);
|
||||
CFGFUN(criteria_pop_state);
|
||||
|
||||
CFGFUN(include, const char *pattern);
|
||||
CFGFUN(font, const char *font);
|
||||
CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command);
|
||||
CFGFUN(for_window, const char *command);
|
||||
|
@@ -16,6 +16,50 @@
|
||||
SLIST_HEAD(variables_head, Variable);
|
||||
extern pid_t config_error_nagbar_pid;
|
||||
|
||||
struct stack_entry {
|
||||
/* Just a pointer, not dynamically allocated. */
|
||||
const char *identifier;
|
||||
enum {
|
||||
STACK_STR = 0,
|
||||
STACK_LONG = 1,
|
||||
} type;
|
||||
union {
|
||||
char *str;
|
||||
long num;
|
||||
} val;
|
||||
};
|
||||
|
||||
struct stack {
|
||||
struct stack_entry stack[10];
|
||||
};
|
||||
|
||||
struct parser_ctx {
|
||||
bool use_nagbar;
|
||||
bool assume_v4;
|
||||
|
||||
int state;
|
||||
Match current_match;
|
||||
|
||||
/* A list which contains the states that lead to the current state, e.g.
|
||||
* INITIAL, WORKSPACE_LAYOUT.
|
||||
* When jumping back to INITIAL, statelist_idx will simply be set to 1
|
||||
* (likewise for other states, e.g. MODE or BAR).
|
||||
* This list is used to process the nearest error token. */
|
||||
int statelist[10];
|
||||
/* NB: statelist_idx points to where the next entry will be inserted */
|
||||
int statelist_idx;
|
||||
|
||||
/*******************************************************************************
|
||||
* The (small) stack where identified literals are stored during the parsing
|
||||
* of a single config directive (like $workspace).
|
||||
******************************************************************************/
|
||||
struct stack *stack;
|
||||
|
||||
struct variables_head variables;
|
||||
|
||||
bool has_errors;
|
||||
};
|
||||
|
||||
/**
|
||||
* An intermediate reprsentation of the result of a parse_config call.
|
||||
* Currently unused, but the JSON output will be useful in the future when we
|
||||
@@ -23,22 +67,34 @@ extern pid_t config_error_nagbar_pid;
|
||||
*
|
||||
*/
|
||||
struct ConfigResultIR {
|
||||
/* The JSON generator to append a reply to. */
|
||||
yajl_gen json_gen;
|
||||
struct parser_ctx *ctx;
|
||||
|
||||
/* The next state to transition to. Passed to the function so that we can
|
||||
* determine the next state as a result of a function call, like
|
||||
* cfg_criteria_pop_state() does. */
|
||||
int next_state;
|
||||
};
|
||||
|
||||
struct ConfigResultIR *parse_config(const char *input, struct context *context);
|
||||
/* Whether any error happened while processing this config directive. */
|
||||
bool has_errors;
|
||||
};
|
||||
|
||||
/**
|
||||
* launch nagbar to indicate errors in the configuration file.
|
||||
*/
|
||||
void start_config_error_nagbar(const char *configpath, bool has_errors);
|
||||
|
||||
/**
|
||||
* Releases the memory of all variables in ctx.
|
||||
*
|
||||
*/
|
||||
void free_variables(struct parser_ctx *ctx);
|
||||
|
||||
typedef enum {
|
||||
PARSE_FILE_FAILED = -1,
|
||||
PARSE_FILE_SUCCESS = 0,
|
||||
PARSE_FILE_CONFIG_ERRORS = 1,
|
||||
} parse_file_result_t;
|
||||
|
||||
/**
|
||||
* Parses the given file by first replacing the variables, then calling
|
||||
* parse_config and launching i3-nagbar if use_nagbar is true.
|
||||
@@ -47,4 +103,4 @@ void start_config_error_nagbar(const char *configpath, bool has_errors);
|
||||
* parsing.
|
||||
*
|
||||
*/
|
||||
bool parse_file(const char *f, bool use_nagbar);
|
||||
parse_file_result_t parse_file(struct parser_ctx *ctx, const char *f);
|
||||
|
@@ -15,6 +15,7 @@
|
||||
#include "queue.h"
|
||||
#include "i3.h"
|
||||
|
||||
typedef struct IncludedFile IncludedFile;
|
||||
typedef struct Config Config;
|
||||
typedef struct Barconfig Barconfig;
|
||||
extern char *current_configpath;
|
||||
@@ -22,6 +23,7 @@ extern char *current_config;
|
||||
extern Config config;
|
||||
extern SLIST_HEAD(modes_head, Mode) modes;
|
||||
extern TAILQ_HEAD(barconfig_head, Barconfig) barconfigs;
|
||||
extern TAILQ_HEAD(includedfiles_head, IncludedFile) included_files;
|
||||
|
||||
/**
|
||||
* Used during the config file lexing/parsing to keep the state of the lexer
|
||||
@@ -69,6 +71,16 @@ struct Variable {
|
||||
SLIST_ENTRY(Variable) variables;
|
||||
};
|
||||
|
||||
/**
|
||||
* List entry struct for an included file.
|
||||
*
|
||||
*/
|
||||
struct IncludedFile {
|
||||
char *path;
|
||||
|
||||
TAILQ_ENTRY(IncludedFile) files;
|
||||
};
|
||||
|
||||
/**
|
||||
* The configuration file can contain multiple sets of bindings. Apart from the
|
||||
* default set (name == "default"), you can specify other sets and change the
|
||||
|
Reference in New Issue
Block a user