[BACK]Return to cmd-source-file.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-source-file.c, Revision 1.37

1.37    ! nicm        1: /* $OpenBSD: cmd-source-file.c,v 1.36 2018/05/24 09:42:49 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2008 Tiago Cunha <me@tiagocunha.org>
                      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.31      nicm       21: #include <errno.h>
                     22: #include <glob.h>
1.12      nicm       23: #include <stdlib.h>
1.31      nicm       24: #include <string.h>
1.34      nicm       25: #include <vis.h>
1.12      nicm       26:
1.1       nicm       27: #include "tmux.h"
                     28:
                     29: /*
                     30:  * Sources a configuration file.
                     31:  */
                     32:
1.30      nicm       33: static enum cmd_retval cmd_source_file_exec(struct cmd *, struct cmdq_item *);
1.15      nicm       34:
1.30      nicm       35: static enum cmd_retval cmd_source_file_done(struct cmdq_item *, void *);
1.1       nicm       36:
                     37: const struct cmd_entry cmd_source_file_entry = {
1.23      nicm       38:        .name = "source-file",
                     39:        .alias = "source",
                     40:
1.25      tim        41:        .args = { "q", 1, 1 },
                     42:        .usage = "[-q] path",
1.23      nicm       43:
                     44:        .flags = 0,
                     45:        .exec = cmd_source_file_exec
1.1       nicm       46: };
                     47:
1.26      nicm       48: static enum cmd_retval
1.30      nicm       49: cmd_source_file_exec(struct cmd *self, struct cmdq_item *item)
1.15      nicm       50: {
1.30      nicm       51:        struct args             *args = self->args;
1.37    ! nicm       52:        int                      flags = 0;
1.30      nicm       53:        struct client           *c = item->client;
1.37    ! nicm       54:        struct cmdq_item        *new_item, *after;
1.31      nicm       55:        enum cmd_retval          retval;
1.34      nicm       56:        char                    *pattern, *tmp;
                     57:        const char              *path = args->argv[0];
1.31      nicm       58:        glob_t                   g;
1.33      nicm       59:        u_int                    i;
1.15      nicm       60:
1.37    ! nicm       61:        if (args_has(args, 'q'))
        !            62:                flags |= CFG_QUIET;
        !            63:
1.34      nicm       64:        if (*path == '/')
                     65:                pattern = xstrdup(path);
                     66:        else {
1.36      nicm       67:                utf8_stravis(&tmp, server_client_get_cwd(c, NULL), VIS_GLOB);
1.34      nicm       68:                xasprintf(&pattern, "%s/%s", tmp, path);
                     69:                free(tmp);
1.31      nicm       70:        }
1.34      nicm       71:        log_debug("%s: %s", __func__, pattern);
1.31      nicm       72:
                     73:        retval = CMD_RETURN_NORMAL;
1.34      nicm       74:        if (glob(pattern, 0, NULL, &g) != 0) {
1.37    ! nicm       75:                if (errno != ENOENT || (~flags & CFG_QUIET)) {
1.34      nicm       76:                        cmdq_error(item, "%s: %s", path, strerror(errno));
                     77:                        retval = CMD_RETURN_ERROR;
                     78:                }
                     79:                free(pattern);
                     80:                return (retval);
                     81:        }
                     82:        free(pattern);
                     83:
1.37    ! nicm       84:        after = item;
1.33      nicm       85:        for (i = 0; i < (u_int)g.gl_pathc; i++) {
1.37    ! nicm       86:                if (load_cfg(g.gl_pathv[i], c, after, flags, &new_item) < 0)
1.31      nicm       87:                        retval = CMD_RETURN_ERROR;
1.37    ! nicm       88:                else if (new_item != NULL)
        !            89:                        after = new_item;
1.15      nicm       90:        }
1.29      nicm       91:        if (cfg_finished) {
1.30      nicm       92:                new_item = cmdq_get_callback(cmd_source_file_done, NULL);
                     93:                cmdq_insert_after(item, new_item);
1.29      nicm       94:        }
1.31      nicm       95:
                     96:        globfree(&g);
                     97:        return (retval);
1.15      nicm       98: }
                     99:
1.29      nicm      100: static enum cmd_retval
1.30      nicm      101: cmd_source_file_done(struct cmdq_item *item, __unused void *data)
1.15      nicm      102: {
1.30      nicm      103:        cfg_print_causes(item);
1.29      nicm      104:        return (CMD_RETURN_NORMAL);
1.1       nicm      105: }