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

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