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

1.17    ! nicm        1: /* $OpenBSD: cmd-show-environment.c,v 1.16 2015/12/14 00:31:54 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
                      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.10      nicm       30: enum cmd_retval        cmd_show_environment_exec(struct cmd *, struct cmd_q *);
                     31:
                     32: char   *cmd_show_environment_escape(struct environ_entry *);
                     33: void    cmd_show_environment_print(struct cmd *, struct cmd_q *,
                     34:             struct environ_entry *);
1.1       nicm       35:
                     36: const struct cmd_entry cmd_show_environment_entry = {
1.15      nicm       37:        .name = "show-environment",
                     38:        .alias = "showenv",
                     39:
                     40:        .args = { "gst:", 0, 1 },
                     41:        .usage = "[-gs] " CMD_TARGET_SESSION_USAGE " [name]",
                     42:
1.16      nicm       43:        .tflag = CMD_SESSION_CANFAIL,
                     44:
                     45:        .flags = 0,
1.15      nicm       46:        .exec = cmd_show_environment_exec
1.1       nicm       47: };
                     48:
1.10      nicm       49: char *
                     50: cmd_show_environment_escape(struct environ_entry *envent)
                     51: {
                     52:        const char      *value = envent->value;
                     53:        char             c, *out, *ret;
                     54:
                     55:        out = ret = xmalloc(strlen(value) * 2 + 1); /* at most twice the size */
                     56:        while ((c = *value++) != '\0') {
                     57:                /* POSIX interprets $ ` " and \ in double quotes. */
                     58:                if (c == '$' || c == '`' || c == '"' || c == '\\')
                     59:                        *out++ = '\\';
                     60:                *out++ = c;
                     61:        }
                     62:        *out = '\0';
                     63:
1.11      nicm       64:        return (ret);
1.10      nicm       65: }
                     66:
                     67: void
                     68: cmd_show_environment_print(struct cmd *self, struct cmd_q *cmdq,
                     69:     struct environ_entry *envent)
                     70: {
                     71:        char    *escaped;
                     72:
                     73:        if (!args_has(self->args, 's')) {
                     74:                if (envent->value != NULL)
                     75:                        cmdq_print(cmdq, "%s=%s", envent->name, envent->value);
                     76:                else
                     77:                        cmdq_print(cmdq, "-%s", envent->name);
                     78:                return;
                     79:        }
                     80:
                     81:        if (envent->value != NULL) {
                     82:                escaped = cmd_show_environment_escape(envent);
                     83:                cmdq_print(cmdq, "%s=\"%s\"; export %s;", envent->name, escaped,
                     84:                    envent->name);
                     85:                free(escaped);
                     86:        } else
                     87:                cmdq_print(cmdq, "unset %s;", envent->name);
                     88: }
                     89:
1.6       nicm       90: enum cmd_retval
1.7       nicm       91: cmd_show_environment_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       92: {
1.3       nicm       93:        struct args             *args = self->args;
                     94:        struct environ          *env;
                     95:        struct environ_entry    *envent;
1.1       nicm       96:
1.17    ! nicm       97:        if (args_has(self->args, 'g') || cmdq->state.tflag.s == NULL)
1.12      nicm       98:                env = global_environ;
1.13      nicm       99:        else
                    100:                env = cmdq->state.tflag.s->environ;
1.5       nicm      101:
                    102:        if (args->argc != 0) {
                    103:                envent = environ_find(env, args->argv[0]);
                    104:                if (envent == NULL) {
1.7       nicm      105:                        cmdq_error(cmdq, "unknown variable: %s", args->argv[0]);
1.6       nicm      106:                        return (CMD_RETURN_ERROR);
1.5       nicm      107:                }
1.10      nicm      108:                cmd_show_environment_print(self, cmdq, envent);
1.6       nicm      109:                return (CMD_RETURN_NORMAL);
1.1       nicm      110:        }
                    111:
1.12      nicm      112:        envent = environ_first(env);
                    113:        while (envent != NULL) {
1.10      nicm      114:                cmd_show_environment_print(self, cmdq, envent);
1.12      nicm      115:                envent = environ_next(envent);
                    116:        }
1.6       nicm      117:        return (CMD_RETURN_NORMAL);
1.1       nicm      118: }