summaryrefslogtreecommitdiffstats
path: root/cfg.c
diff options
context:
space:
mode:
authornicm <nicm>2019-05-23 11:13:30 +0000
committernicm <nicm>2019-05-23 11:13:30 +0000
commit723010ba72e337832402f8e44981c02caa30b476 (patch)
treea7fa1089f4d5dc368a44702489cf176afbcfec55 /cfg.c
parent5571d7a21c5dc920eb9d25336b5bb7b04d72fc9d (diff)
Replace the split parser code (cfg.c and cmd-string.c) with a single
parser using yacc(1). This is a major change but is clearer and simpler and allows some edge cases to be made more consistent, as well as tidying up how aliases are handled. It will also allow some further improvements later. Entirely the same parser is now used for parsing the configuration file and for string commands. This means that constructs previously only available in .tmux.conf, such as %if, can now be used in string commands (for example, those given to if-shell - not commands invoked from the shell, they are still parsed by the shell itself). The only syntax change I am aware of is that #{} outside quotes or a comment is now considered a format and not a comment, so #{ is now a syntax error (notably, if it is at the start of a line). This also adds two new sections to the man page documenting the syntax and outlining how parsing and command execution works. Thanks to everyone who sent me test configs (they still all parse without errors - but this doesn't mean they still work as intended!). Thanks to Avi Halachmi for testing and man page improvements, also to jmc@ for reviewing the man page changes.
Diffstat (limited to 'cfg.c')
-rw-r--r--cfg.c238
1 files changed, 33 insertions, 205 deletions
diff --git a/cfg.c b/cfg.c
index 0b74ec35..aa5c13b7 100644
--- a/cfg.c
+++ b/cfg.c
@@ -27,17 +27,6 @@
#include "tmux.h"
-/* Condition for %if, %elif, %else and %endif. */
-struct cfg_cond {
- size_t line; /* line number of %if */
- int met; /* condition was met */
- int skip; /* skip later %elif/%else */
- int saw_else; /* saw a %else */
-
- TAILQ_ENTRY(cfg_cond) entry;
-};
-TAILQ_HEAD(cfg_conds, cfg_cond);
-
struct client *cfg_client;
static char *cfg_file;
int cfg_finished;
@@ -86,9 +75,8 @@ start_cfg(void)
struct client *c;
/*
- * Configuration files are loaded without a client, so NULL is passed
- * into load_cfg() and commands run in the global queue with
- * item->client NULL.
+ * Configuration files are loaded without a client, so commands are run
+ * in the global queue with item->client NULL.
*
* However, we must block the initial client (but just the initial
* client) so that its command runs after the configuration is loaded.
@@ -103,11 +91,11 @@ start_cfg(void)
}
if (cfg_file == NULL)
- load_cfg(TMUX_CONF, NULL, NULL, CFG_QUIET, NULL);
+ load_cfg(TMUX_CONF, NULL, NULL, CMD_PARSE_QUIET, NULL);
if (cfg_file == NULL && (home = find_home()) != NULL) {
xasprintf(&cfg_file, "%s/.tmux.conf", home);
- flags = CFG_QUIET;
+ flags = CMD_PARSE_QUIET;
}
if (cfg_file != NULL)
load_cfg(cfg_file, NULL, NULL, flags, NULL);
@@ -115,214 +103,54 @@ start_cfg(void)
cmdq_append(NULL, cmdq_get_callback(cfg_done, NULL));
}
-static int
-cfg_check_cond(const char *path, size_t line, const char *p, int *skip,
- struct client *c, struct cmd_find_state *fs)
-{
- struct format_tree *ft;
- char *s;
- int result;
-
- while (isspace((u_char)*p))
- p++;
- if (p[0] == '\0') {
- cfg_add_cause("%s:%zu: invalid condition", path, line);
- *skip = 1;
- return (0);
- }
-
- ft = format_create(NULL, NULL, FORMAT_NONE, FORMAT_NOJOBS);
- if (fs != NULL)
- format_defaults(ft, c, fs->s, fs->wl, fs->wp);
- else
- format_defaults(ft, c, NULL, NULL, NULL);
- s = format_expand(ft, p);
- result = format_true(s);
- free(s);
- format_free(ft);
-
- *skip = result;
- return (result);
-}
-
-static void
-cfg_handle_if(const char *path, size_t line, struct cfg_conds *conds,
- const char *p, struct client *c, struct cmd_find_state *fs)
-{
- struct cfg_cond *cond;
- struct cfg_cond *parent = TAILQ_FIRST(conds);
-
- /*
- * Add a new condition. If a previous condition exists and isn't
- * currently met, this new one also can't be met.
- */
- cond = xcalloc(1, sizeof *cond);
- cond->line = line;
- if (parent == NULL || parent->met)
- cond->met = cfg_check_cond(path, line, p, &cond->skip, c, fs);
- else
- cond->skip = 1;
- cond->saw_else = 0;
- TAILQ_INSERT_HEAD(conds, cond, entry);
-}
-
-static void
-cfg_handle_elif(const char *path, size_t line, struct cfg_conds *conds,
- const char *p, struct client *c, struct cmd_find_state *fs)
-{
- struct cfg_cond *cond = TAILQ_FIRST(conds);
-
- /*
- * If a previous condition exists and wasn't met, check this
- * one instead and change the state.
- */
- if (cond == NULL || cond->saw_else)
- cfg_add_cause("%s:%zu: unexpected %%elif", path, line);
- else if (!cond->skip)
- cond->met = cfg_check_cond(path, line, p, &cond->skip, c, fs);
- else
- cond->met = 0;
-}
-
-static void
-cfg_handle_else(const char *path, size_t line, struct cfg_conds *conds)
-{
- struct cfg_cond *cond = TAILQ_FIRST(conds);
-
- /*
- * If a previous condition exists and wasn't met and wasn't already
- * %else, use this one instead.
- */
- if (cond == NULL || cond->saw_else) {
- cfg_add_cause("%s:%zu: unexpected %%else", path, line);
- return;
- }
- cond->saw_else = 1;
- cond->met = !cond->skip;
- cond->skip = 1;
-}
-
-static void
-cfg_handle_endif(const char *path, size_t line, struct cfg_conds *conds)
-{
- struct cfg_cond *cond = TAILQ_FIRST(conds);
-
- /*
- * Remove previous condition if one exists.
- */
- if (cond == NULL) {
- cfg_add_cause("%s:%zu: unexpected %%endif", path, line);
- return;
- }
- TAILQ_REMOVE(conds, cond, entry);
- free(cond);
-}
-
-static void
-cfg_handle_directive(const char *p, const char *path, size_t line,
- struct cfg_conds *conds, struct client *c, struct cmd_find_state *fs)
-{
- int n = 0;
-
- while (p[n] != '\0' && !isspace((u_char)p[n]))
- n++;
- if (strncmp(p, "%if", n) == 0)
- cfg_handle_if(path, line, conds, p + n, c, fs);
- else if (strncmp(p, "%elif", n) == 0)
- cfg_handle_elif(path, line, conds, p + n, c, fs);
- else if (strcmp(p, "%else") == 0)
- cfg_handle_else(path, line, conds);
- else if (strcmp(p, "%endif") == 0)
- cfg_handle_endif(path, line, conds);
- else
- cfg_add_cause("%s:%zu: invalid directive: %s", path, line, p);
-}
-
int
load_cfg(const char *path, struct client *c, struct cmdq_item *item, int flags,
struct cmdq_item **new_item)
{
FILE *f;
- const char delim[3] = { '\\', '\\', '\0' };
- u_int found = 0;
- size_t line = 0;
- char *buf, *cause1, *p, *q;
- struct cmd_list *cmdlist;
+ struct cmd_parse_input pi;
+ struct cmd_parse_result *pr;
struct cmdq_item *new_item0;
- struct cfg_cond *cond, *cond1;
- struct cfg_conds conds;
- struct cmd_find_state *fs = NULL;
- struct client *fc = NULL;
-
- if (item != NULL) {
- fs = &item->target;
- fc = cmd_find_client(item, NULL, 1);
- }
- TAILQ_INIT(&conds);
+ if (new_item != NULL)
+ *new_item = NULL;
log_debug("loading %s", path);
if ((f = fopen(path, "rb")) == NULL) {
- if (errno == ENOENT && (flags & CFG_QUIET))
+ if (errno == ENOENT && (flags & CMD_PARSE_QUIET))
return (0);
cfg_add_cause("%s: %s", path, strerror(errno));
return (-1);
}
- while ((buf = fparseln(f, NULL, &line, delim, 0)) != NULL) {
- log_debug("%s: %s", path, buf);
-
- p = buf;
- while (isspace((u_char)*p))
- p++;
- if (*p == '\0') {
- free(buf);
- continue;
- }
- q = p + strlen(p) - 1;
- while (q != p && isspace((u_char)*q))
- *q-- = '\0';
-
- if (*p == '%') {
- cfg_handle_directive(p, path, line, &conds, fc, fs);
- continue;
- }
- cond = TAILQ_FIRST(&conds);
- if (cond != NULL && !cond->met)
- continue;
-
- cmdlist = cmd_string_parse(p, path, line, &cause1);
- if (cmdlist == NULL) {
- free(buf);
- if (cause1 == NULL)
- continue;
- cfg_add_cause("%s:%zu: %s", path, line, cause1);
- free(cause1);
- continue;
- }
- free(buf);
-
- new_item0 = cmdq_get_command(cmdlist, NULL, NULL, 0);
- if (item != NULL) {
- cmdq_insert_after(item, new_item0);
- item = new_item0;
- } else
- cmdq_append(c, new_item0);
- cmd_list_free(cmdlist);
-
- found++;
- }
- fclose(f);
+ memset(&pi, 0, sizeof pi);
+ pi.flags = flags;
+ pi.file = path;
- TAILQ_FOREACH_REVERSE_SAFE(cond, &conds, cfg_conds, entry, cond1) {
- cfg_add_cause("%s:%zu: unterminated %%if", path, cond->line);
- TAILQ_REMOVE(&conds, cond, entry);
- free(cond);
+ pr = cmd_parse_from_file(f, &pi);
+ fclose(f);
+ if (pr->status == CMD_PARSE_EMPTY)
+ return (0);
+ if (pr->status == CMD_PARSE_ERROR) {
+ cfg_add_cause("%s", pr->error);
+ free(pr->error);
+ return (-1);
+ }
+ if (flags & CMD_PARSE_PARSEONLY) {
+ cmd_list_free(pr->cmdlist);
+ return (0);
}
+ new_item0 = cmdq_get_command(pr->cmdlist, NULL, NULL, 0);
+ if (item != NULL)
+ cmdq_insert_after(item, new_item0);
+ else
+ cmdq_append(c, new_item0);
+ cmd_list_free(pr->cmdlist);
+
if (new_item != NULL)
- *new_item = item;
- return (found);
+ *new_item = new_item0;
+ return (0);
}
void