Annotation of src/usr.bin/tmux/cmd-show-environment.c, Revision 1.13
1.13 ! nicm 1: /* $OpenBSD: cmd-show-environment.c,v 1.12 2015/10/28 09:51:55 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.13 ! nicm 40: CMD_SESSION_T,
1.3 nicm 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:
1.11 nicm 59: return (ret);
1.10 nicm 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 environ *env;
90: struct environ_entry *envent;
1.1 nicm 91:
1.3 nicm 92: if (args_has(self->args, 'g'))
1.12 nicm 93: env = global_environ;
1.13 ! nicm 94: else
! 95: env = cmdq->state.tflag.s->environ;
1.5 nicm 96:
97: if (args->argc != 0) {
98: envent = environ_find(env, args->argv[0]);
99: if (envent == NULL) {
1.7 nicm 100: cmdq_error(cmdq, "unknown variable: %s", args->argv[0]);
1.6 nicm 101: return (CMD_RETURN_ERROR);
1.5 nicm 102: }
1.10 nicm 103: cmd_show_environment_print(self, cmdq, envent);
1.6 nicm 104: return (CMD_RETURN_NORMAL);
1.1 nicm 105: }
106:
1.12 nicm 107: envent = environ_first(env);
108: while (envent != NULL) {
1.10 nicm 109: cmd_show_environment_print(self, cmdq, envent);
1.12 nicm 110: envent = environ_next(envent);
111: }
1.6 nicm 112: return (CMD_RETURN_NORMAL);
1.1 nicm 113: }