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

1.57    ! nicm        1: /* $OpenBSD: cfg.c,v 1.56 2017/04/21 13:15:43 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.55      nicm       31: static char     *cfg_file;
1.51      nicm       32: int              cfg_finished;
                     33: static char    **cfg_causes;
                     34: static u_int     cfg_ncauses;
1.40      nicm       35:
1.49      nicm       36: static enum cmd_retval
1.50      nicm       37: cfg_done(__unused struct cmdq_item *item, __unused void *data)
1.49      nicm       38: {
                     39:        if (cfg_finished)
                     40:                return (CMD_RETURN_NORMAL);
                     41:        cfg_finished = 1;
                     42:
                     43:        if (!RB_EMPTY(&sessions))
                     44:                cfg_show_causes(RB_MIN(sessions, &sessions));
1.57    ! nicm       45:
        !            46:        status_prompt_load_history();
1.49      nicm       47:
                     48:        return (CMD_RETURN_NORMAL);
                     49: }
1.40      nicm       50:
                     51: void
1.41      nicm       52: set_cfg_file(const char *path)
                     53: {
                     54:        free(cfg_file);
                     55:        cfg_file = xstrdup(path);
                     56: }
                     57:
                     58: void
1.40      nicm       59: start_cfg(void)
                     60: {
1.41      nicm       61:        const char      *home;
1.45      tim        62:        int              quiet = 0;
1.40      nicm       63:
1.56      nicm       64:        /*
                     65:         * Note that the configuration files are loaded without a client, so
                     66:         * NULL is passed into load_cfg() which means that commands run in the
                     67:         * global queue and item->client is NULL for all commands.
                     68:         */
1.40      nicm       69:
1.56      nicm       70:        load_cfg(TMUX_CONF, NULL, NULL, 1);
1.40      nicm       71:
1.41      nicm       72:        if (cfg_file == NULL && (home = find_home()) != NULL) {
                     73:                xasprintf(&cfg_file, "%s/.tmux.conf", home);
1.45      tim        74:                quiet = 1;
1.41      nicm       75:        }
1.45      tim        76:        if (cfg_file != NULL)
1.56      nicm       77:                load_cfg(cfg_file, NULL, NULL, quiet);
1.40      nicm       78:
1.56      nicm       79:        cmdq_append(NULL, cmdq_get_callback(cfg_done, NULL));
1.40      nicm       80: }
1.1       nicm       81:
1.28      nicm       82: int
1.50      nicm       83: load_cfg(const char *path, struct client *c, struct cmdq_item *item, int quiet)
1.1       nicm       84: {
1.50      nicm       85:        FILE                    *f;
1.52      nicm       86:        const char               delim[3] = { '\\', '\\', '\0' };
                     87:        u_int                    found = 0;
1.50      nicm       88:        size_t                   line = 0;
1.52      nicm       89:        char                    *buf, *cause1, *p, *q, *s;
1.50      nicm       90:        struct cmd_list         *cmdlist;
                     91:        struct cmdq_item        *new_item;
1.52      nicm       92:        int                      condition = 0;
                     93:        struct format_tree      *ft;
1.1       nicm       94:
1.29      nicm       95:        log_debug("loading %s", path);
1.1       nicm       96:        if ((f = fopen(path, "rb")) == NULL) {
1.45      tim        97:                if (errno == ENOENT && quiet)
                     98:                        return (0);
                     99:                cfg_add_cause("%s: %s", path, strerror(errno));
1.28      nicm      100:                return (-1);
1.25      nicm      101:        }
                    102:
1.42      nicm      103:        while ((buf = fparseln(f, NULL, &line, delim, 0)) != NULL) {
1.32      tobias    104:                log_debug("%s: %s", path, buf);
1.13      nicm      105:
1.32      tobias    106:                p = buf;
1.52      nicm      107:                while (isspace((u_char)*p))
1.32      tobias    108:                        p++;
                    109:                if (*p == '\0') {
                    110:                        free(buf);
1.21      nicm      111:                        continue;
1.22      nicm      112:                }
1.52      nicm      113:                q = p + strlen(p) - 1;
                    114:                while (q != p && isspace((u_char)*q))
                    115:                        *q-- = '\0';
                    116:
                    117:                if (condition != 0 && strcmp(p, "%endif") == 0) {
                    118:                        condition = 0;
                    119:                        continue;
                    120:                }
                    121:                if (strncmp(p, "%if ", 4) == 0) {
                    122:                        if (condition != 0) {
                    123:                                cfg_add_cause("%s:%zu: nested %%if", path,
                    124:                                    line);
                    125:                                continue;
                    126:                        }
1.54      nicm      127:                        ft = format_create(NULL, FORMAT_NONE, FORMAT_NOJOBS);
1.52      nicm      128:
                    129:                        s = p + 3;
                    130:                        while (isspace((u_char)*s))
                    131:                                s++;
                    132:                        s = format_expand(ft, s);
                    133:                        if (*s != '\0' && (s[0] != '0' || s[1] != '\0'))
                    134:                                condition = 1;
                    135:                        else
                    136:                                condition = -1;
                    137:                        free(s);
                    138:
                    139:                        format_free(ft);
                    140:                        continue;
                    141:                }
                    142:                if (condition == -1)
                    143:                        continue;
1.21      nicm      144:
1.53      nicm      145:                cmdlist = cmd_string_parse(p, path, line, &cause1);
                    146:                if (cmdlist == NULL) {
1.32      tobias    147:                        free(buf);
1.28      nicm      148:                        if (cause1 == NULL)
1.1       nicm      149:                                continue;
1.33      nicm      150:                        cfg_add_cause("%s:%zu: %s", path, line, cause1);
1.28      nicm      151:                        free(cause1);
1.9       nicm      152:                        continue;
1.20      nicm      153:                }
1.32      tobias    154:                free(buf);
1.28      nicm      155:
1.1       nicm      156:                if (cmdlist == NULL)
                    157:                        continue;
1.50      nicm      158:                new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
                    159:                if (item != NULL)
                    160:                        cmdq_insert_after(item, new_item);
1.49      nicm      161:                else
1.50      nicm      162:                        cmdq_append(c, new_item);
1.1       nicm      163:                cmd_list_free(cmdlist);
1.49      nicm      164:
1.28      nicm      165:                found++;
1.1       nicm      166:        }
                    167:        fclose(f);
1.25      nicm      168:
1.28      nicm      169:        return (found);
1.33      nicm      170: }
                    171:
                    172: void
1.36      nicm      173: cfg_add_cause(const char *fmt, ...)
1.33      nicm      174: {
1.36      nicm      175:        va_list  ap;
                    176:        char    *msg;
1.33      nicm      177:
                    178:        va_start(ap, fmt);
                    179:        xvasprintf(&msg, fmt, ap);
1.38      nicm      180:        va_end(ap);
1.33      nicm      181:
1.35      nicm      182:        cfg_ncauses++;
                    183:        cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes);
                    184:        cfg_causes[cfg_ncauses - 1] = msg;
1.33      nicm      185: }
                    186:
                    187: void
1.50      nicm      188: cfg_print_causes(struct cmdq_item *item)
1.33      nicm      189: {
                    190:        u_int    i;
                    191:
1.35      nicm      192:        for (i = 0; i < cfg_ncauses; i++) {
1.50      nicm      193:                cmdq_print(item, "%s", cfg_causes[i]);
1.35      nicm      194:                free(cfg_causes[i]);
1.33      nicm      195:        }
1.35      nicm      196:
                    197:        free(cfg_causes);
                    198:        cfg_causes = NULL;
1.37      nicm      199:        cfg_ncauses = 0;
1.17      nicm      200: }
                    201:
                    202: void
1.28      nicm      203: cfg_show_causes(struct session *s)
1.17      nicm      204: {
                    205:        struct window_pane      *wp;
                    206:        u_int                    i;
                    207:
1.35      nicm      208:        if (s == NULL || cfg_ncauses == 0)
1.17      nicm      209:                return;
                    210:        wp = s->curw->window->active;
                    211:
                    212:        window_pane_set_mode(wp, &window_copy_mode);
                    213:        window_copy_init_for_output(wp);
1.35      nicm      214:        for (i = 0; i < cfg_ncauses; i++) {
                    215:                window_copy_add(wp, "%s", cfg_causes[i]);
                    216:                free(cfg_causes[i]);
1.17      nicm      217:        }
1.35      nicm      218:
                    219:        free(cfg_causes);
                    220:        cfg_causes = NULL;
1.37      nicm      221:        cfg_ncauses = 0;
1.1       nicm      222: }