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

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