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

1.49    ! nicm        1: /* $OpenBSD: cfg.c,v 1.48 2016/10/14 18:41:53 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.47      nicm       31: char                    *cfg_file;
                     32: int                      cfg_finished;
                     33: static char            **cfg_causes;
                     34: static u_int             cfg_ncauses;
                     35: struct client           *cfg_client;
1.40      nicm       36:
1.49    ! nicm       37: static enum cmd_retval
        !            38: cfg_done(__unused struct cmd_q *cmdq, __unused void *data)
        !            39: {
        !            40:        if (cfg_finished)
        !            41:                return (CMD_RETURN_NORMAL);
        !            42:        cfg_finished = 1;
        !            43:
        !            44:        if (!RB_EMPTY(&sessions))
        !            45:                cfg_show_causes(RB_MIN(sessions, &sessions));
        !            46:
        !            47:        if (cfg_client != NULL)
        !            48:                server_client_unref(cfg_client);
        !            49:        return (CMD_RETURN_NORMAL);
        !            50: }
1.40      nicm       51:
                     52: void
1.41      nicm       53: set_cfg_file(const char *path)
                     54: {
                     55:        free(cfg_file);
                     56:        cfg_file = xstrdup(path);
                     57: }
                     58:
                     59: void
1.40      nicm       60: start_cfg(void)
                     61: {
1.41      nicm       62:        const char      *home;
1.45      tim        63:        int              quiet = 0;
1.40      nicm       64:
                     65:        cfg_client = TAILQ_FIRST(&clients);
                     66:        if (cfg_client != NULL)
                     67:                cfg_client->references++;
                     68:
1.49    ! nicm       69:        load_cfg(TMUX_CONF, cfg_client, NULL, 1);
1.40      nicm       70:
1.41      nicm       71:        if (cfg_file == NULL && (home = find_home()) != NULL) {
                     72:                xasprintf(&cfg_file, "%s/.tmux.conf", home);
1.45      tim        73:                quiet = 1;
1.41      nicm       74:        }
1.45      tim        75:        if (cfg_file != NULL)
1.49    ! nicm       76:                load_cfg(cfg_file, cfg_client, NULL, quiet);
1.40      nicm       77:
1.49    ! nicm       78:        cmdq_append(cfg_client, cmdq_get_callback(cfg_done, NULL));
1.40      nicm       79: }
1.1       nicm       80:
1.28      nicm       81: int
1.49    ! nicm       82: load_cfg(const char *path, struct client *c, struct cmd_q *cmdq, int quiet)
1.1       nicm       83: {
1.7       deraadt    84:        FILE            *f;
1.32      tobias     85:        char             delim[3] = { '\\', '\\', '\0' };
                     86:        u_int            found;
                     87:        size_t           line = 0;
1.33      nicm       88:        char            *buf, *cause1, *p;
1.1       nicm       89:        struct cmd_list *cmdlist;
1.49    ! nicm       90:        struct cmd_q    *new_cmdq;
1.1       nicm       91:
1.29      nicm       92:        log_debug("loading %s", path);
1.1       nicm       93:        if ((f = fopen(path, "rb")) == NULL) {
1.45      tim        94:                if (errno == ENOENT && quiet)
                     95:                        return (0);
                     96:                cfg_add_cause("%s: %s", path, strerror(errno));
1.28      nicm       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.49    ! nicm      126:                new_cmdq = cmdq_get_command(cmdlist, NULL, NULL, 0);
        !           127:                if (cmdq != NULL)
        !           128:                        cmdq_insert_after(cmdq, new_cmdq);
        !           129:                else
        !           130:                        cmdq_append(c, new_cmdq);
1.1       nicm      131:                cmd_list_free(cmdlist);
1.49    ! nicm      132:
1.28      nicm      133:                found++;
1.1       nicm      134:        }
                    135:        fclose(f);
1.25      nicm      136:
1.28      nicm      137:        return (found);
1.33      nicm      138: }
                    139:
                    140: void
1.36      nicm      141: cfg_add_cause(const char *fmt, ...)
1.33      nicm      142: {
1.36      nicm      143:        va_list  ap;
                    144:        char    *msg;
1.33      nicm      145:
                    146:        va_start(ap, fmt);
                    147:        xvasprintf(&msg, fmt, ap);
1.38      nicm      148:        va_end(ap);
1.33      nicm      149:
1.35      nicm      150:        cfg_ncauses++;
                    151:        cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes);
                    152:        cfg_causes[cfg_ncauses - 1] = msg;
1.33      nicm      153: }
                    154:
                    155: void
                    156: cfg_print_causes(struct cmd_q *cmdq)
                    157: {
                    158:        u_int    i;
                    159:
1.35      nicm      160:        for (i = 0; i < cfg_ncauses; i++) {
                    161:                cmdq_print(cmdq, "%s", cfg_causes[i]);
                    162:                free(cfg_causes[i]);
1.33      nicm      163:        }
1.35      nicm      164:
                    165:        free(cfg_causes);
                    166:        cfg_causes = NULL;
1.37      nicm      167:        cfg_ncauses = 0;
1.17      nicm      168: }
                    169:
                    170: void
1.28      nicm      171: cfg_show_causes(struct session *s)
1.17      nicm      172: {
                    173:        struct window_pane      *wp;
                    174:        u_int                    i;
                    175:
1.35      nicm      176:        if (s == NULL || cfg_ncauses == 0)
1.17      nicm      177:                return;
                    178:        wp = s->curw->window->active;
                    179:
                    180:        window_pane_set_mode(wp, &window_copy_mode);
                    181:        window_copy_init_for_output(wp);
1.35      nicm      182:        for (i = 0; i < cfg_ncauses; i++) {
                    183:                window_copy_add(wp, "%s", cfg_causes[i]);
                    184:                free(cfg_causes[i]);
1.17      nicm      185:        }
1.35      nicm      186:
                    187:        free(cfg_causes);
                    188:        cfg_causes = NULL;
1.37      nicm      189:        cfg_ncauses = 0;
1.1       nicm      190: }