[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.86

1.86    ! nicm        1: /* $OpenBSD: cfg.c,v 1.85 2022/05/30 13:00:18 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: int                      cfg_finished;
                     32: static char            **cfg_causes;
                     33: static u_int             cfg_ncauses;
                     34: static struct cmdq_item         *cfg_item;
                     35:
1.82      nicm       36: int                       cfg_quiet = 1;
                     37: char                    **cfg_files;
                     38: u_int                     cfg_nfiles;
                     39:
1.58      nicm       40: static enum cmd_retval
                     41: cfg_client_done(__unused struct cmdq_item *item, __unused void *data)
                     42: {
                     43:        if (!cfg_finished)
                     44:                return (CMD_RETURN_WAIT);
                     45:        return (CMD_RETURN_NORMAL);
                     46: }
1.40      nicm       47:
1.49      nicm       48: static enum cmd_retval
1.50      nicm       49: cfg_done(__unused struct cmdq_item *item, __unused void *data)
1.49      nicm       50: {
                     51:        if (cfg_finished)
                     52:                return (CMD_RETURN_NORMAL);
                     53:        cfg_finished = 1;
                     54:
1.86    ! nicm       55:        cfg_show_causes(NULL);
1.57      nicm       56:
1.58      nicm       57:        if (cfg_item != NULL)
1.74      nicm       58:                cmdq_continue(cfg_item);
1.58      nicm       59:
1.57      nicm       60:        status_prompt_load_history();
1.49      nicm       61:
                     62:        return (CMD_RETURN_NORMAL);
                     63: }
1.40      nicm       64:
                     65: void
                     66: start_cfg(void)
                     67: {
1.81      nicm       68:        struct client    *c;
1.82      nicm       69:        u_int             i;
1.40      nicm       70:
1.56      nicm       71:        /*
1.71      nicm       72:         * Configuration files are loaded without a client, so commands are run
                     73:         * in the global queue with item->client NULL.
1.58      nicm       74:         *
                     75:         * However, we must block the initial client (but just the initial
                     76:         * client) so that its command runs after the configuration is loaded.
                     77:         * Because start_cfg() is called so early, we can be sure the client's
                     78:         * command queue is currently empty and our callback will be at the
                     79:         * front - we need to get in before MSG_COMMAND.
1.56      nicm       80:         */
1.63      nicm       81:        cfg_client = c = TAILQ_FIRST(&clients);
1.58      nicm       82:        if (c != NULL) {
                     83:                cfg_item = cmdq_get_callback(cfg_client_done, NULL);
                     84:                cmdq_append(c, cfg_item);
                     85:        }
1.40      nicm       86:
1.82      nicm       87:        for (i = 0; i < cfg_nfiles; i++) {
                     88:                if (cfg_quiet)
                     89:                        load_cfg(cfg_files[i], c, NULL, CMD_PARSE_QUIET, NULL);
                     90:                else
                     91:                        load_cfg(cfg_files[i], c, NULL, 0, NULL);
                     92:        }
1.40      nicm       93:
1.56      nicm       94:        cmdq_append(NULL, cmdq_get_callback(cfg_done, NULL));
1.40      nicm       95: }
1.1       nicm       96:
1.28      nicm       97: int
1.70      nicm       98: load_cfg(const char *path, struct client *c, struct cmdq_item *item, int flags,
                     99:     struct cmdq_item **new_item)
1.1       nicm      100: {
1.50      nicm      101:        FILE                    *f;
1.71      nicm      102:        struct cmd_parse_input   pi;
                    103:        struct cmd_parse_result *pr;
1.70      nicm      104:        struct cmdq_item        *new_item0;
1.83      nicm      105:        struct cmdq_state       *state;
1.61      nicm      106:
1.71      nicm      107:        if (new_item != NULL)
                    108:                *new_item = NULL;
1.1       nicm      109:
1.29      nicm      110:        log_debug("loading %s", path);
1.1       nicm      111:        if ((f = fopen(path, "rb")) == NULL) {
1.71      nicm      112:                if (errno == ENOENT && (flags & CMD_PARSE_QUIET))
1.45      tim       113:                        return (0);
                    114:                cfg_add_cause("%s: %s", path, strerror(errno));
1.28      nicm      115:                return (-1);
1.25      nicm      116:        }
                    117:
1.71      nicm      118:        memset(&pi, 0, sizeof pi);
                    119:        pi.flags = flags;
                    120:        pi.file = path;
1.72      nicm      121:        pi.line = 1;
1.73      nicm      122:        pi.item = item;
1.75      nicm      123:        pi.c = c;
1.13      nicm      124:
1.71      nicm      125:        pr = cmd_parse_from_file(f, &pi);
                    126:        fclose(f);
1.77      nicm      127:        if (pr->status == CMD_PARSE_ERROR) {
                    128:                cfg_add_cause("%s", pr->error);
                    129:                free(pr->error);
                    130:                return (-1);
                    131:        }
                    132:        if (flags & CMD_PARSE_PARSEONLY) {
                    133:                cmd_list_free(pr->cmdlist);
                    134:                return (0);
                    135:        }
                    136:
1.83      nicm      137:        if (item != NULL)
                    138:                state = cmdq_copy_state(cmdq_get_state(item));
                    139:        else
                    140:                state = cmdq_new_state(NULL, NULL, 0);
                    141:        cmdq_add_format(state, "current_file", "%s", pi.file);
                    142:
                    143:        new_item0 = cmdq_get_command(pr->cmdlist, state);
1.77      nicm      144:        if (item != NULL)
1.78      nicm      145:                new_item0 = cmdq_insert_after(item, new_item0);
1.77      nicm      146:        else
1.78      nicm      147:                new_item0 = cmdq_append(NULL, new_item0);
1.77      nicm      148:        cmd_list_free(pr->cmdlist);
1.83      nicm      149:        cmdq_free_state(state);
1.77      nicm      150:
                    151:        if (new_item != NULL)
                    152:                *new_item = new_item0;
                    153:        return (0);
                    154: }
                    155:
                    156: int
                    157: load_cfg_from_buffer(const void *buf, size_t len, const char *path,
                    158:     struct client *c, struct cmdq_item *item, int flags,
                    159:     struct cmdq_item **new_item)
                    160: {
                    161:        struct cmd_parse_input   pi;
                    162:        struct cmd_parse_result *pr;
                    163:        struct cmdq_item        *new_item0;
1.83      nicm      164:        struct cmdq_state       *state;
1.77      nicm      165:
                    166:        if (new_item != NULL)
                    167:                *new_item = NULL;
                    168:
                    169:        log_debug("loading %s", path);
                    170:
                    171:        memset(&pi, 0, sizeof pi);
                    172:        pi.flags = flags;
                    173:        pi.file = path;
                    174:        pi.line = 1;
                    175:        pi.item = item;
                    176:        pi.c = c;
                    177:
                    178:        pr = cmd_parse_from_buffer(buf, len, &pi);
1.71      nicm      179:        if (pr->status == CMD_PARSE_ERROR) {
                    180:                cfg_add_cause("%s", pr->error);
                    181:                free(pr->error);
                    182:                return (-1);
                    183:        }
                    184:        if (flags & CMD_PARSE_PARSEONLY) {
                    185:                cmd_list_free(pr->cmdlist);
                    186:                return (0);
1.1       nicm      187:        }
1.61      nicm      188:
1.83      nicm      189:        if (item != NULL)
                    190:                state = cmdq_copy_state(cmdq_get_state(item));
                    191:        else
                    192:                state = cmdq_new_state(NULL, NULL, 0);
                    193:        cmdq_add_format(state, "current_file", "%s", pi.file);
                    194:
                    195:        new_item0 = cmdq_get_command(pr->cmdlist, state);
1.71      nicm      196:        if (item != NULL)
1.78      nicm      197:                new_item0 = cmdq_insert_after(item, new_item0);
1.71      nicm      198:        else
1.78      nicm      199:                new_item0 = cmdq_append(NULL, new_item0);
1.71      nicm      200:        cmd_list_free(pr->cmdlist);
1.83      nicm      201:        cmdq_free_state(state);
1.25      nicm      202:
1.70      nicm      203:        if (new_item != NULL)
1.71      nicm      204:                *new_item = new_item0;
                    205:        return (0);
1.33      nicm      206: }
                    207:
                    208: void
1.36      nicm      209: cfg_add_cause(const char *fmt, ...)
1.33      nicm      210: {
1.36      nicm      211:        va_list  ap;
                    212:        char    *msg;
1.33      nicm      213:
                    214:        va_start(ap, fmt);
                    215:        xvasprintf(&msg, fmt, ap);
1.38      nicm      216:        va_end(ap);
1.33      nicm      217:
1.35      nicm      218:        cfg_ncauses++;
                    219:        cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes);
                    220:        cfg_causes[cfg_ncauses - 1] = msg;
1.33      nicm      221: }
                    222:
                    223: void
1.50      nicm      224: cfg_print_causes(struct cmdq_item *item)
1.33      nicm      225: {
                    226:        u_int    i;
                    227:
1.35      nicm      228:        for (i = 0; i < cfg_ncauses; i++) {
1.50      nicm      229:                cmdq_print(item, "%s", cfg_causes[i]);
1.35      nicm      230:                free(cfg_causes[i]);
1.33      nicm      231:        }
1.35      nicm      232:
                    233:        free(cfg_causes);
                    234:        cfg_causes = NULL;
1.37      nicm      235:        cfg_ncauses = 0;
1.17      nicm      236: }
                    237:
                    238: void
1.28      nicm      239: cfg_show_causes(struct session *s)
1.17      nicm      240: {
1.86    ! nicm      241:        struct client                   *c = TAILQ_FIRST(&clients);
1.66      nicm      242:        struct window_pane              *wp;
                    243:        struct window_mode_entry        *wme;
                    244:        u_int                            i;
1.17      nicm      245:
1.86    ! nicm      246:        if (cfg_ncauses == 0)
        !           247:                return;
        !           248:
        !           249:        if (c != NULL && (c->flags & CLIENT_CONTROL)) {
        !           250:                for (i = 0; i < cfg_ncauses; i++) {
        !           251:                        control_write(c, "%%config-error %s", cfg_causes[i]);
        !           252:                        free(cfg_causes[i]);
        !           253:                }
        !           254:                goto out;
        !           255:        }
        !           256:
        !           257:        if (s == NULL) {
        !           258:                if (c != NULL && c->session != NULL)
        !           259:                        s = c->session;
        !           260:                else
        !           261:                        s = RB_MIN(sessions, &sessions);
        !           262:        }
        !           263:        if (s == NULL || s->attached == 0) /* wait for an attached session */
1.17      nicm      264:                return;
                    265:        wp = s->curw->window->active;
                    266:
1.66      nicm      267:        wme = TAILQ_FIRST(&wp->modes);
                    268:        if (wme == NULL || wme->mode != &window_view_mode)
1.79      nicm      269:                window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
1.35      nicm      270:        for (i = 0; i < cfg_ncauses; i++) {
1.85      nicm      271:                window_copy_add(wp, 0, "%s", cfg_causes[i]);
1.35      nicm      272:                free(cfg_causes[i]);
1.17      nicm      273:        }
1.35      nicm      274:
1.86    ! nicm      275: out:
1.35      nicm      276:        free(cfg_causes);
                    277:        cfg_causes = NULL;
1.37      nicm      278:        cfg_ncauses = 0;
1.1       nicm      279: }