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

1.45    ! tim         1: /* $OpenBSD: cfg.c,v 1.44 2016/01/19 15:59:12 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.40      nicm       26: #include <unistd.h>
1.32      tobias     27: #include <util.h>
1.1       nicm       28:
                     29: #include "tmux.h"
                     30:
1.41      nicm       31: char            *cfg_file;
1.40      nicm       32: struct cmd_q    *cfg_cmd_q;
                     33: int              cfg_finished;
                     34: int              cfg_references;
                     35: char           **cfg_causes;
                     36: u_int            cfg_ncauses;
                     37: struct client   *cfg_client;
                     38:
                     39: void   cfg_default_done(struct cmd_q *);
                     40:
                     41: void
1.41      nicm       42: set_cfg_file(const char *path)
                     43: {
                     44:        free(cfg_file);
                     45:        cfg_file = xstrdup(path);
                     46: }
                     47:
                     48: void
1.40      nicm       49: start_cfg(void)
                     50: {
1.41      nicm       51:        const char      *home;
1.45    ! tim        52:        int              quiet = 0;
1.40      nicm       53:
                     54:        cfg_cmd_q = cmdq_new(NULL);
                     55:        cfg_cmd_q->emptyfn = cfg_default_done;
                     56:
                     57:        cfg_finished = 0;
                     58:        cfg_references = 1;
                     59:
                     60:        cfg_client = TAILQ_FIRST(&clients);
                     61:        if (cfg_client != NULL)
                     62:                cfg_client->references++;
                     63:
1.45    ! tim        64:        load_cfg(TMUX_CONF, cfg_cmd_q, 1);
1.40      nicm       65:
1.41      nicm       66:        if (cfg_file == NULL && (home = find_home()) != NULL) {
                     67:                xasprintf(&cfg_file, "%s/.tmux.conf", home);
1.45    ! tim        68:                quiet = 1;
1.41      nicm       69:        }
1.45    ! tim        70:        if (cfg_file != NULL)
        !            71:                load_cfg(cfg_file, cfg_cmd_q, quiet);
1.40      nicm       72:
                     73:        cmdq_continue(cfg_cmd_q);
                     74: }
1.1       nicm       75:
1.28      nicm       76: int
1.45    ! tim        77: load_cfg(const char *path, struct cmd_q *cmdq, int quiet)
1.1       nicm       78: {
1.7       deraadt    79:        FILE            *f;
1.32      tobias     80:        char             delim[3] = { '\\', '\\', '\0' };
                     81:        u_int            found;
                     82:        size_t           line = 0;
1.33      nicm       83:        char            *buf, *cause1, *p;
1.1       nicm       84:        struct cmd_list *cmdlist;
                     85:
1.29      nicm       86:        log_debug("loading %s", path);
1.1       nicm       87:        if ((f = fopen(path, "rb")) == NULL) {
1.45    ! tim        88:                if (errno == ENOENT && quiet)
        !            89:                        return (0);
        !            90:                cfg_add_cause("%s: %s", path, strerror(errno));
1.28      nicm       91:                return (-1);
1.25      nicm       92:        }
                     93:
1.32      tobias     94:        found = 0;
1.42      nicm       95:        while ((buf = fparseln(f, NULL, &line, delim, 0)) != NULL) {
1.32      tobias     96:                log_debug("%s: %s", path, buf);
1.13      nicm       97:
1.21      nicm       98:                /* Skip empty lines. */
1.32      tobias     99:                p = buf;
                    100:                while (isspace((u_char) *p))
                    101:                        p++;
                    102:                if (*p == '\0') {
                    103:                        free(buf);
1.21      nicm      104:                        continue;
1.22      nicm      105:                }
1.21      nicm      106:
1.28      nicm      107:                /* Parse and run the command. */
1.32      tobias    108:                if (cmd_string_parse(p, &cmdlist, path, line, &cause1) != 0) {
                    109:                        free(buf);
1.28      nicm      110:                        if (cause1 == NULL)
1.1       nicm      111:                                continue;
1.33      nicm      112:                        cfg_add_cause("%s:%zu: %s", path, line, cause1);
1.28      nicm      113:                        free(cause1);
1.9       nicm      114:                        continue;
1.20      nicm      115:                }
1.32      tobias    116:                free(buf);
1.28      nicm      117:
1.1       nicm      118:                if (cmdlist == NULL)
                    119:                        continue;
1.34      nicm      120:                cmdq_append(cmdq, cmdlist, NULL);
1.1       nicm      121:                cmd_list_free(cmdlist);
1.28      nicm      122:                found++;
1.1       nicm      123:        }
                    124:        fclose(f);
1.25      nicm      125:
1.28      nicm      126:        return (found);
                    127: }
                    128:
                    129: void
1.43      nicm      130: cfg_default_done(__unused struct cmd_q *cmdq)
1.28      nicm      131: {
                    132:        if (--cfg_references != 0)
                    133:                return;
                    134:        cfg_finished = 1;
1.18      nicm      135:
1.28      nicm      136:        if (!RB_EMPTY(&sessions))
                    137:                cfg_show_causes(RB_MIN(sessions, &sessions));
1.1       nicm      138:
1.28      nicm      139:        cmdq_free(cfg_cmd_q);
                    140:        cfg_cmd_q = NULL;
1.30      nicm      141:
                    142:        if (cfg_client != NULL) {
                    143:                /*
                    144:                 * The client command queue starts with client_exit set to 1 so
                    145:                 * only continue if not empty (that is, we have been delayed
                    146:                 * during configuration parsing for long enough that the
                    147:                 * MSG_COMMAND has arrived), else the client will exit before
                    148:                 * the MSG_COMMAND which might tell it not to.
                    149:                 */
                    150:                if (!TAILQ_EMPTY(&cfg_client->cmdq->queue))
                    151:                        cmdq_continue(cfg_client->cmdq);
1.39      nicm      152:                server_client_unref(cfg_client);
1.30      nicm      153:                cfg_client = NULL;
                    154:        }
1.33      nicm      155: }
                    156:
                    157: void
1.36      nicm      158: cfg_add_cause(const char *fmt, ...)
1.33      nicm      159: {
1.36      nicm      160:        va_list  ap;
                    161:        char    *msg;
1.33      nicm      162:
                    163:        va_start(ap, fmt);
                    164:        xvasprintf(&msg, fmt, ap);
1.38      nicm      165:        va_end(ap);
1.33      nicm      166:
1.35      nicm      167:        cfg_ncauses++;
                    168:        cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes);
                    169:        cfg_causes[cfg_ncauses - 1] = msg;
1.33      nicm      170: }
                    171:
                    172: void
                    173: cfg_print_causes(struct cmd_q *cmdq)
                    174: {
                    175:        u_int    i;
                    176:
1.35      nicm      177:        for (i = 0; i < cfg_ncauses; i++) {
                    178:                cmdq_print(cmdq, "%s", cfg_causes[i]);
                    179:                free(cfg_causes[i]);
1.33      nicm      180:        }
1.35      nicm      181:
                    182:        free(cfg_causes);
                    183:        cfg_causes = NULL;
1.37      nicm      184:        cfg_ncauses = 0;
1.17      nicm      185: }
                    186:
                    187: void
1.28      nicm      188: cfg_show_causes(struct session *s)
1.17      nicm      189: {
                    190:        struct window_pane      *wp;
                    191:        u_int                    i;
                    192:
1.35      nicm      193:        if (s == NULL || cfg_ncauses == 0)
1.17      nicm      194:                return;
                    195:        wp = s->curw->window->active;
                    196:
                    197:        window_pane_set_mode(wp, &window_copy_mode);
                    198:        window_copy_init_for_output(wp);
1.35      nicm      199:        for (i = 0; i < cfg_ncauses; i++) {
                    200:                window_copy_add(wp, "%s", cfg_causes[i]);
                    201:                free(cfg_causes[i]);
1.17      nicm      202:        }
1.35      nicm      203:
                    204:        free(cfg_causes);
                    205:        cfg_causes = NULL;
1.37      nicm      206:        cfg_ncauses = 0;
1.1       nicm      207: }