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

1.10    ! nicm        1: /* $OpenBSD: cmd-show-environment.c,v 1.9 2014/10/20 22:29:25 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 = {
                     37:        "show-environment", "showenv",
1.10    ! nicm       38:        "gst:", 0, 1,
        !            39:        "[-gs] " CMD_TARGET_SESSION_USAGE " [name]",
1.3       nicm       40:        0,
                     41:        cmd_show_environment_exec
1.1       nicm       42: };
                     43:
1.10    ! nicm       44: char *
        !            45: cmd_show_environment_escape(struct environ_entry *envent)
        !            46: {
        !            47:        const char      *value = envent->value;
        !            48:        char             c, *out, *ret;
        !            49:
        !            50:        out = ret = xmalloc(strlen(value) * 2 + 1); /* at most twice the size */
        !            51:        while ((c = *value++) != '\0') {
        !            52:                /* POSIX interprets $ ` " and \ in double quotes. */
        !            53:                if (c == '$' || c == '`' || c == '"' || c == '\\')
        !            54:                        *out++ = '\\';
        !            55:                *out++ = c;
        !            56:        }
        !            57:        *out = '\0';
        !            58:
        !            59:        return ret;
        !            60: }
        !            61:
        !            62: void
        !            63: cmd_show_environment_print(struct cmd *self, struct cmd_q *cmdq,
        !            64:     struct environ_entry *envent)
        !            65: {
        !            66:        char    *escaped;
        !            67:
        !            68:        if (!args_has(self->args, 's')) {
        !            69:                if (envent->value != NULL)
        !            70:                        cmdq_print(cmdq, "%s=%s", envent->name, envent->value);
        !            71:                else
        !            72:                        cmdq_print(cmdq, "-%s", envent->name);
        !            73:                return;
        !            74:        }
        !            75:
        !            76:        if (envent->value != NULL) {
        !            77:                escaped = cmd_show_environment_escape(envent);
        !            78:                cmdq_print(cmdq, "%s=\"%s\"; export %s;", envent->name, escaped,
        !            79:                    envent->name);
        !            80:                free(escaped);
        !            81:        } else
        !            82:                cmdq_print(cmdq, "unset %s;", envent->name);
        !            83: }
        !            84:
1.6       nicm       85: enum cmd_retval
1.7       nicm       86: cmd_show_environment_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       87: {
1.3       nicm       88:        struct args             *args = self->args;
                     89:        struct session          *s;
                     90:        struct environ          *env;
                     91:        struct environ_entry    *envent;
1.1       nicm       92:
1.3       nicm       93:        if (args_has(self->args, 'g'))
1.1       nicm       94:                env = &global_environ;
                     95:        else {
1.10    ! nicm       96:                s = cmd_find_session(cmdq, args_get(args, 't'), 0);
        !            97:                if (s == NULL)
1.6       nicm       98:                        return (CMD_RETURN_ERROR);
1.1       nicm       99:                env = &s->environ;
1.5       nicm      100:        }
                    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.10    ! nicm      112:        RB_FOREACH(envent, environ, env)
        !           113:                cmd_show_environment_print(self, cmdq, envent);
1.6       nicm      114:        return (CMD_RETURN_NORMAL);
1.1       nicm      115: }