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

Annotation of src/usr.bin/tmux/cmd-show-environment.c, Revision 1.25

1.25    ! nicm        1: /* $OpenBSD: cmd-show-environment.c,v 1.24 2020/03/31 17:14:40 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.18      nicm        4:  * Copyright (c) 2009 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:
                     21: #include <stdlib.h>
                     22: #include <string.h>
                     23:
                     24: #include "tmux.h"
                     25:
                     26: /*
                     27:  * Show environment.
                     28:  */
                     29:
1.22      nicm       30: static enum cmd_retval cmd_show_environment_exec(struct cmd *,
                     31:                            struct cmdq_item *);
1.10      nicm       32:
1.20      nicm       33: static char    *cmd_show_environment_escape(struct environ_entry *);
1.22      nicm       34: static void     cmd_show_environment_print(struct cmd *, struct cmdq_item *,
1.20      nicm       35:                     struct environ_entry *);
1.1       nicm       36:
                     37: const struct cmd_entry cmd_show_environment_entry = {
1.15      nicm       38:        .name = "show-environment",
                     39:        .alias = "showenv",
                     40:
1.24      nicm       41:        .args = { "hgst:", 0, 1 },
                     42:        .usage = "[-hgs] " CMD_TARGET_SESSION_USAGE " [name]",
1.15      nicm       43:
1.23      nicm       44:        .target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL },
1.16      nicm       45:
1.21      nicm       46:        .flags = CMD_AFTERHOOK,
1.15      nicm       47:        .exec = cmd_show_environment_exec
1.1       nicm       48: };
                     49:
1.20      nicm       50: static char *
1.10      nicm       51: cmd_show_environment_escape(struct environ_entry *envent)
                     52: {
                     53:        const char      *value = envent->value;
                     54:        char             c, *out, *ret;
                     55:
                     56:        out = ret = xmalloc(strlen(value) * 2 + 1); /* at most twice the size */
                     57:        while ((c = *value++) != '\0') {
                     58:                /* POSIX interprets $ ` " and \ in double quotes. */
                     59:                if (c == '$' || c == '`' || c == '"' || c == '\\')
                     60:                        *out++ = '\\';
                     61:                *out++ = c;
                     62:        }
                     63:        *out = '\0';
                     64:
1.11      nicm       65:        return (ret);
1.10      nicm       66: }
                     67:
1.20      nicm       68: static void
1.22      nicm       69: cmd_show_environment_print(struct cmd *self, struct cmdq_item *item,
1.10      nicm       70:     struct environ_entry *envent)
                     71: {
1.25    ! nicm       72:        struct args     *args = cmd_get_args(self);
1.24      nicm       73:        char            *escaped;
                     74:
                     75:        if (!args_has(args, 'h') && (envent->flags & ENVIRON_HIDDEN))
                     76:                return;
                     77:        if (args_has(args, 'h') && (~envent->flags & ENVIRON_HIDDEN))
                     78:                return;
1.10      nicm       79:
1.25    ! nicm       80:        if (!args_has(args, 's')) {
1.10      nicm       81:                if (envent->value != NULL)
1.22      nicm       82:                        cmdq_print(item, "%s=%s", envent->name, envent->value);
1.10      nicm       83:                else
1.22      nicm       84:                        cmdq_print(item, "-%s", envent->name);
1.10      nicm       85:                return;
                     86:        }
                     87:
                     88:        if (envent->value != NULL) {
                     89:                escaped = cmd_show_environment_escape(envent);
1.22      nicm       90:                cmdq_print(item, "%s=\"%s\"; export %s;", envent->name, escaped,
1.10      nicm       91:                    envent->name);
                     92:                free(escaped);
                     93:        } else
1.22      nicm       94:                cmdq_print(item, "unset %s;", envent->name);
1.10      nicm       95: }
                     96:
1.20      nicm       97: static enum cmd_retval
1.22      nicm       98: cmd_show_environment_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       99: {
1.25    ! nicm      100:        struct args             *args = cmd_get_args(self);
1.3       nicm      101:        struct environ          *env;
                    102:        struct environ_entry    *envent;
1.19      nicm      103:        const char              *target;
1.1       nicm      104:
1.19      nicm      105:        if ((target = args_get(args, 't')) != NULL) {
1.23      nicm      106:                if (item->target.s == NULL) {
1.22      nicm      107:                        cmdq_error(item, "no such session: %s", target);
1.19      nicm      108:                        return (CMD_RETURN_ERROR);
                    109:                }
                    110:        }
                    111:
1.25    ! nicm      112:        if (args_has(args, 'g'))
1.12      nicm      113:                env = global_environ;
1.19      nicm      114:        else {
1.23      nicm      115:                if (item->target.s == NULL) {
1.19      nicm      116:                        target = args_get(args, 't');
                    117:                        if (target != NULL)
1.22      nicm      118:                                cmdq_error(item, "no such session: %s", target);
1.19      nicm      119:                        else
1.22      nicm      120:                                cmdq_error(item, "no current session");
1.19      nicm      121:                        return (CMD_RETURN_ERROR);
                    122:                }
1.23      nicm      123:                env = item->target.s->environ;
1.19      nicm      124:        }
1.5       nicm      125:
                    126:        if (args->argc != 0) {
                    127:                envent = environ_find(env, args->argv[0]);
                    128:                if (envent == NULL) {
1.22      nicm      129:                        cmdq_error(item, "unknown variable: %s", args->argv[0]);
1.6       nicm      130:                        return (CMD_RETURN_ERROR);
1.5       nicm      131:                }
1.22      nicm      132:                cmd_show_environment_print(self, item, envent);
1.6       nicm      133:                return (CMD_RETURN_NORMAL);
1.1       nicm      134:        }
                    135:
1.12      nicm      136:        envent = environ_first(env);
                    137:        while (envent != NULL) {
1.22      nicm      138:                cmd_show_environment_print(self, item, envent);
1.12      nicm      139:                envent = environ_next(envent);
                    140:        }
1.6       nicm      141:        return (CMD_RETURN_NORMAL);
1.1       nicm      142: }