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

1.52    ! nicm        1: /* $OpenBSD: cmd-source-file.c,v 1.51 2021/08/21 10:22:39 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.1       nicm       35: const struct cmd_entry cmd_source_file_entry = {
1.23      nicm       36:        .name = "source-file",
                     37:        .alias = "source",
                     38:
1.51      nicm       39:        .args = { "Fnqv", 1, -1, NULL },
1.48      nicm       40:        .usage = "[-Fnqv] path ...",
1.23      nicm       41:
                     42:        .flags = 0,
                     43:        .exec = cmd_source_file_exec
1.1       nicm       44: };
                     45:
1.42      nicm       46: struct cmd_source_file_data {
                     47:        struct cmdq_item         *item;
                     48:        int                       flags;
                     49:
                     50:        struct cmdq_item         *after;
                     51:        enum cmd_retval           retval;
                     52:
                     53:        u_int                     current;
                     54:        char                    **files;
                     55:        u_int                     nfiles;
                     56: };
                     57:
                     58: static enum cmd_retval
                     59: cmd_source_file_complete_cb(struct cmdq_item *item, __unused void *data)
                     60: {
                     61:        cfg_print_causes(item);
                     62:        return (CMD_RETURN_NORMAL);
                     63: }
                     64:
                     65: static void
                     66: cmd_source_file_complete(struct client *c, struct cmd_source_file_data *cdata)
                     67: {
                     68:        struct cmdq_item        *new_item;
                     69:
                     70:        if (cfg_finished) {
1.49      nicm       71:                if (cdata->retval == CMD_RETURN_ERROR &&
                     72:                    c != NULL &&
                     73:                    c->session == NULL)
1.42      nicm       74:                        c->retval = 1;
                     75:                new_item = cmdq_get_callback(cmd_source_file_complete_cb, NULL);
                     76:                cmdq_insert_after(cdata->after, new_item);
                     77:        }
                     78:
                     79:        free(cdata->files);
                     80:        free(cdata);
                     81: }
                     82:
                     83: static void
                     84: cmd_source_file_done(struct client *c, const char *path, int error,
                     85:     int closed, struct evbuffer *buffer, void *data)
                     86: {
                     87:        struct cmd_source_file_data     *cdata = data;
                     88:        struct cmdq_item                *item = cdata->item;
                     89:        void                            *bdata = EVBUFFER_DATA(buffer);
                     90:        size_t                           bsize = EVBUFFER_LENGTH(buffer);
                     91:        u_int                            n;
                     92:        struct cmdq_item                *new_item;
                     93:
                     94:        if (!closed)
                     95:                return;
                     96:
                     97:        if (error != 0)
                     98:                cmdq_error(item, "%s: %s", path, strerror(error));
                     99:        else if (bsize != 0) {
                    100:                if (load_cfg_from_buffer(bdata, bsize, path, c, cdata->after,
                    101:                    cdata->flags, &new_item) < 0)
                    102:                        cdata->retval = CMD_RETURN_ERROR;
                    103:                else if (new_item != NULL)
                    104:                        cdata->after = new_item;
                    105:        }
                    106:
                    107:        n = ++cdata->current;
                    108:        if (n < cdata->nfiles)
                    109:                file_read(c, cdata->files[n], cmd_source_file_done, cdata);
                    110:        else {
                    111:                cmd_source_file_complete(c, cdata);
                    112:                cmdq_continue(item);
                    113:        }
                    114: }
                    115:
                    116: static void
                    117: cmd_source_file_add(struct cmd_source_file_data *cdata, const char *path)
                    118: {
1.44      nicm      119:        log_debug("%s: %s", __func__, path);
1.42      nicm      120:        cdata->files = xreallocarray(cdata->files, cdata->nfiles + 1,
                    121:            sizeof *cdata->files);
                    122:        cdata->files[cdata->nfiles++] = xstrdup(path);
                    123: }
                    124:
1.26      nicm      125: static enum cmd_retval
1.30      nicm      126: cmd_source_file_exec(struct cmd *self, struct cmdq_item *item)
1.15      nicm      127: {
1.46      nicm      128:        struct args                     *args = cmd_get_args(self);
1.42      nicm      129:        struct cmd_source_file_data     *cdata;
1.47      nicm      130:        struct client                   *c = cmdq_get_client(item);
1.42      nicm      131:        enum cmd_retval                  retval = CMD_RETURN_NORMAL;
1.50      nicm      132:        char                            *pattern, *cwd, *expanded = NULL;
1.42      nicm      133:        const char                      *path, *error;
                    134:        glob_t                           g;
1.50      nicm      135:        int                              result;
                    136:        u_int                            i, j;
1.42      nicm      137:
                    138:        cdata = xcalloc(1, sizeof *cdata);
                    139:        cdata->item = item;
1.15      nicm      140:
1.37      nicm      141:        if (args_has(args, 'q'))
1.42      nicm      142:                cdata->flags |= CMD_PARSE_QUIET;
1.38      nicm      143:        if (args_has(args, 'n'))
1.42      nicm      144:                cdata->flags |= CMD_PARSE_PARSEONLY;
1.41      nicm      145:        if (args_has(args, 'v'))
1.42      nicm      146:                cdata->flags |= CMD_PARSE_VERBOSE;
                    147:
1.39      nicm      148:        utf8_stravis(&cwd, server_client_get_cwd(c, NULL), VIS_GLOB);
1.31      nicm      149:
1.50      nicm      150:        for (i = 0; i < args_count(args); i++) {
                    151:                path = args_string(args, i);
1.48      nicm      152:                if (args_has(args, 'F')) {
1.50      nicm      153:                        free(expanded);
                    154:                        expanded = format_single_from_target(item, path);
                    155:                        path = expanded;
                    156:                }
1.42      nicm      157:                if (strcmp(path, "-") == 0) {
                    158:                        cmd_source_file_add(cdata, "-");
                    159:                        continue;
                    160:                }
                    161:
1.39      nicm      162:                if (*path == '/')
                    163:                        pattern = xstrdup(path);
                    164:                else
                    165:                        xasprintf(&pattern, "%s/%s", cwd, path);
                    166:                log_debug("%s: %s", __func__, pattern);
                    167:
1.43      nicm      168:                if ((result = glob(pattern, 0, NULL, &g)) != 0) {
                    169:                        if (result != GLOB_NOMATCH ||
1.45      tim       170:                            (~cdata->flags & CMD_PARSE_QUIET)) {
1.43      nicm      171:                                if (result == GLOB_NOMATCH)
                    172:                                        error = strerror(ENOENT);
                    173:                                else if (result == GLOB_NOSPACE)
                    174:                                        error = strerror(ENOMEM);
                    175:                                else
                    176:                                        error = strerror(EINVAL);
1.39      nicm      177:                                cmdq_error(item, "%s: %s", path, error);
                    178:                                retval = CMD_RETURN_ERROR;
                    179:                        }
                    180:                        free(pattern);
                    181:                        continue;
1.34      nicm      182:                }
                    183:                free(pattern);
                    184:
1.42      nicm      185:                for (j = 0; j < g.gl_pathc; j++)
                    186:                        cmd_source_file_add(cdata, g.gl_pathv[j]);
1.29      nicm      187:        }
1.52    ! nicm      188:        free(expanded);
1.42      nicm      189:
                    190:        cdata->after = item;
                    191:        cdata->retval = retval;
                    192:
                    193:        if (cdata->nfiles != 0) {
                    194:                file_read(c, cdata->files[0], cmd_source_file_done, cdata);
                    195:                retval = CMD_RETURN_WAIT;
                    196:        } else
                    197:                cmd_source_file_complete(c, cdata);
1.31      nicm      198:
1.39      nicm      199:        free(cwd);
1.31      nicm      200:        return (retval);
1.1       nicm      201: }