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

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