[BACK]Return to cfg.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cfg.c, Revision 1.76

1.76    ! nicm        1: /* $OpenBSD: cfg.c,v 1.75 2019/06/20 06:51:36 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.44      nicm        4:  * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
1.21      nicm       21: #include <ctype.h>
1.1       nicm       22: #include <errno.h>
                     23: #include <stdio.h>
1.15      nicm       24: #include <stdlib.h>
1.1       nicm       25: #include <string.h>
1.32      tobias     26: #include <util.h>
1.1       nicm       27:
                     28: #include "tmux.h"
                     29:
1.63      nicm       30: struct client           *cfg_client;
1.58      nicm       31: static char             *cfg_file;
                     32: int                      cfg_finished;
                     33: static char            **cfg_causes;
                     34: static u_int             cfg_ncauses;
                     35: static struct cmdq_item         *cfg_item;
                     36:
                     37: static enum cmd_retval
                     38: cfg_client_done(__unused struct cmdq_item *item, __unused void *data)
                     39: {
                     40:        if (!cfg_finished)
                     41:                return (CMD_RETURN_WAIT);
                     42:        return (CMD_RETURN_NORMAL);
                     43: }
1.40      nicm       44:
1.49      nicm       45: static enum cmd_retval
1.50      nicm       46: cfg_done(__unused struct cmdq_item *item, __unused void *data)
1.49      nicm       47: {
                     48:        if (cfg_finished)
                     49:                return (CMD_RETURN_NORMAL);
                     50:        cfg_finished = 1;
                     51:
                     52:        if (!RB_EMPTY(&sessions))
                     53:                cfg_show_causes(RB_MIN(sessions, &sessions));
1.57      nicm       54:
1.58      nicm       55:        if (cfg_item != NULL)
1.74      nicm       56:                cmdq_continue(cfg_item);
1.58      nicm       57:
1.57      nicm       58:        status_prompt_load_history();
1.49      nicm       59:
                     60:        return (CMD_RETURN_NORMAL);
                     61: }
1.40      nicm       62:
                     63: void
1.41      nicm       64: set_cfg_file(const char *path)
                     65: {
                     66:        free(cfg_file);
                     67:        cfg_file = xstrdup(path);
                     68: }
                     69:
1.76    ! nicm       70: static char *
        !            71: expand_cfg_file(const char *path, const char *home)
        !            72: {
        !            73:        char                    *expanded, *name;
        !            74:        const char              *end;
        !            75:        struct environ_entry    *value;
        !            76:
        !            77:        if (strncmp(path, "~/", 2) == 0) {
        !            78:                if (home == NULL)
        !            79:                        return (NULL);
        !            80:                xasprintf(&expanded, "%s%s", home, path + 1);
        !            81:                return (expanded);
        !            82:        }
        !            83:
        !            84:        if (*path == '$') {
        !            85:                end = strchr(path, '/');
        !            86:                if (end == NULL)
        !            87:                        name = xstrdup(path + 1);
        !            88:                else
        !            89:                        name = xstrndup(path + 1, end - path - 1);
        !            90:                value = environ_find(global_environ, name);
        !            91:                free(name);
        !            92:                if (value == NULL)
        !            93:                        return (NULL);
        !            94:                if (end == NULL)
        !            95:                        end = "";
        !            96:                xasprintf(&expanded, "%s%s", value->value, end);
        !            97:                return (expanded);
        !            98:        }
        !            99:
        !           100:        return (xstrdup(path));
        !           101: }
        !           102:
1.41      nicm      103: void
1.40      nicm      104: start_cfg(void)
                    105: {
1.76    ! nicm      106:        const char      *home = find_home();
1.58      nicm      107:        struct client   *c;
1.76    ! nicm      108:        char            *path, *copy, *next, *expanded;
1.40      nicm      109:
1.56      nicm      110:        /*
1.71      nicm      111:         * Configuration files are loaded without a client, so commands are run
                    112:         * in the global queue with item->client NULL.
1.58      nicm      113:         *
                    114:         * However, we must block the initial client (but just the initial
                    115:         * client) so that its command runs after the configuration is loaded.
                    116:         * Because start_cfg() is called so early, we can be sure the client's
                    117:         * command queue is currently empty and our callback will be at the
                    118:         * front - we need to get in before MSG_COMMAND.
1.56      nicm      119:         */
1.63      nicm      120:        cfg_client = c = TAILQ_FIRST(&clients);
1.58      nicm      121:        if (c != NULL) {
                    122:                cfg_item = cmdq_get_callback(cfg_client_done, NULL);
                    123:                cmdq_append(c, cfg_item);
                    124:        }
1.40      nicm      125:
1.76    ! nicm      126:        if (cfg_file == NULL) {
        !           127:                path = copy = xstrdup(TMUX_CONF);
        !           128:                while ((next = strsep(&path, ":")) != NULL) {
        !           129:                        expanded = expand_cfg_file(next, home);
        !           130:                        if (expanded == NULL) {
        !           131:                                log_debug("couldn't expand %s", next);
        !           132:                                continue;
        !           133:                        }
        !           134:                        log_debug("expanded %s to %s", next, expanded);
        !           135:                        load_cfg(expanded, c, NULL, CMD_PARSE_QUIET, NULL);
        !           136:                        free(expanded);
        !           137:                }
        !           138:                free(copy);
        !           139:        } else
        !           140:                load_cfg(cfg_file, c, NULL, 0, NULL);
1.40      nicm      141:
1.56      nicm      142:        cmdq_append(NULL, cmdq_get_callback(cfg_done, NULL));
1.40      nicm      143: }
1.1       nicm      144:
1.28      nicm      145: int
1.70      nicm      146: load_cfg(const char *path, struct client *c, struct cmdq_item *item, int flags,
                    147:     struct cmdq_item **new_item)
1.1       nicm      148: {
1.50      nicm      149:        FILE                    *f;
1.71      nicm      150:        struct cmd_parse_input   pi;
                    151:        struct cmd_parse_result *pr;
1.70      nicm      152:        struct cmdq_item        *new_item0;
1.61      nicm      153:
1.71      nicm      154:        if (new_item != NULL)
                    155:                *new_item = NULL;
1.1       nicm      156:
1.29      nicm      157:        log_debug("loading %s", path);
1.1       nicm      158:        if ((f = fopen(path, "rb")) == NULL) {
1.71      nicm      159:                if (errno == ENOENT && (flags & CMD_PARSE_QUIET))
1.45      tim       160:                        return (0);
                    161:                cfg_add_cause("%s: %s", path, strerror(errno));
1.28      nicm      162:                return (-1);
1.25      nicm      163:        }
                    164:
1.71      nicm      165:        memset(&pi, 0, sizeof pi);
                    166:        pi.flags = flags;
                    167:        pi.file = path;
1.72      nicm      168:        pi.line = 1;
1.73      nicm      169:        pi.item = item;
1.75      nicm      170:        pi.c = c;
1.13      nicm      171:
1.71      nicm      172:        pr = cmd_parse_from_file(f, &pi);
                    173:        fclose(f);
                    174:        if (pr->status == CMD_PARSE_EMPTY)
                    175:                return (0);
                    176:        if (pr->status == CMD_PARSE_ERROR) {
                    177:                cfg_add_cause("%s", pr->error);
                    178:                free(pr->error);
                    179:                return (-1);
                    180:        }
                    181:        if (flags & CMD_PARSE_PARSEONLY) {
                    182:                cmd_list_free(pr->cmdlist);
                    183:                return (0);
1.1       nicm      184:        }
1.61      nicm      185:
1.71      nicm      186:        new_item0 = cmdq_get_command(pr->cmdlist, NULL, NULL, 0);
                    187:        if (item != NULL)
                    188:                cmdq_insert_after(item, new_item0);
                    189:        else
1.75      nicm      190:                cmdq_append(NULL, new_item0);
1.71      nicm      191:        cmd_list_free(pr->cmdlist);
1.25      nicm      192:
1.70      nicm      193:        if (new_item != NULL)
1.71      nicm      194:                *new_item = new_item0;
                    195:        return (0);
1.33      nicm      196: }
                    197:
                    198: void
1.36      nicm      199: cfg_add_cause(const char *fmt, ...)
1.33      nicm      200: {
1.36      nicm      201:        va_list  ap;
                    202:        char    *msg;
1.33      nicm      203:
                    204:        va_start(ap, fmt);
                    205:        xvasprintf(&msg, fmt, ap);
1.38      nicm      206:        va_end(ap);
1.33      nicm      207:
1.35      nicm      208:        cfg_ncauses++;
                    209:        cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes);
                    210:        cfg_causes[cfg_ncauses - 1] = msg;
1.33      nicm      211: }
                    212:
                    213: void
1.50      nicm      214: cfg_print_causes(struct cmdq_item *item)
1.33      nicm      215: {
                    216:        u_int    i;
                    217:
1.35      nicm      218:        for (i = 0; i < cfg_ncauses; i++) {
1.50      nicm      219:                cmdq_print(item, "%s", cfg_causes[i]);
1.35      nicm      220:                free(cfg_causes[i]);
1.33      nicm      221:        }
1.35      nicm      222:
                    223:        free(cfg_causes);
                    224:        cfg_causes = NULL;
1.37      nicm      225:        cfg_ncauses = 0;
1.17      nicm      226: }
                    227:
                    228: void
1.28      nicm      229: cfg_show_causes(struct session *s)
1.17      nicm      230: {
1.66      nicm      231:        struct window_pane              *wp;
                    232:        struct window_mode_entry        *wme;
                    233:        u_int                            i;
1.17      nicm      234:
1.35      nicm      235:        if (s == NULL || cfg_ncauses == 0)
1.17      nicm      236:                return;
                    237:        wp = s->curw->window->active;
                    238:
1.66      nicm      239:        wme = TAILQ_FIRST(&wp->modes);
                    240:        if (wme == NULL || wme->mode != &window_view_mode)
1.65      nicm      241:                window_pane_set_mode(wp, &window_view_mode, NULL, NULL);
1.35      nicm      242:        for (i = 0; i < cfg_ncauses; i++) {
                    243:                window_copy_add(wp, "%s", cfg_causes[i]);
                    244:                free(cfg_causes[i]);
1.17      nicm      245:        }
1.35      nicm      246:
                    247:        free(cfg_causes);
                    248:        cfg_causes = NULL;
1.37      nicm      249:        cfg_ncauses = 0;
1.1       nicm      250: }