Annotation of embedaddon/tmux/cmd-show-environment.c, revision 1.1.1.1
1.1 misho 1: /* $OpenBSD$ */
2:
3: /*
4: * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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:
30: static enum cmd_retval cmd_show_environment_exec(struct cmd *,
31: struct cmdq_item *);
32:
33: static char *cmd_show_environment_escape(struct environ_entry *);
34: static void cmd_show_environment_print(struct cmd *, struct cmdq_item *,
35: struct environ_entry *);
36:
37: const struct cmd_entry cmd_show_environment_entry = {
38: .name = "show-environment",
39: .alias = "showenv",
40:
41: .args = { "gst:", 0, 1 },
42: .usage = "[-gs] " CMD_TARGET_SESSION_USAGE " [name]",
43:
44: .tflag = CMD_SESSION_CANFAIL,
45:
46: .flags = CMD_AFTERHOOK,
47: .exec = cmd_show_environment_exec
48: };
49:
50: static char *
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:
65: return (ret);
66: }
67:
68: static void
69: cmd_show_environment_print(struct cmd *self, struct cmdq_item *item,
70: struct environ_entry *envent)
71: {
72: char *escaped;
73:
74: if (!args_has(self->args, 's')) {
75: if (envent->value != NULL)
76: cmdq_print(item, "%s=%s", envent->name, envent->value);
77: else
78: cmdq_print(item, "-%s", envent->name);
79: return;
80: }
81:
82: if (envent->value != NULL) {
83: escaped = cmd_show_environment_escape(envent);
84: cmdq_print(item, "%s=\"%s\"; export %s;", envent->name, escaped,
85: envent->name);
86: free(escaped);
87: } else
88: cmdq_print(item, "unset %s;", envent->name);
89: }
90:
91: static enum cmd_retval
92: cmd_show_environment_exec(struct cmd *self, struct cmdq_item *item)
93: {
94: struct args *args = self->args;
95: struct environ *env;
96: struct environ_entry *envent;
97: const char *target;
98:
99: if ((target = args_get(args, 't')) != NULL) {
100: if (item->state.tflag.s == NULL) {
101: cmdq_error(item, "no such session: %s", target);
102: return (CMD_RETURN_ERROR);
103: }
104: }
105:
106: if (args_has(self->args, 'g'))
107: env = global_environ;
108: else {
109: if (item->state.tflag.s == NULL) {
110: target = args_get(args, 't');
111: if (target != NULL)
112: cmdq_error(item, "no such session: %s", target);
113: else
114: cmdq_error(item, "no current session");
115: return (CMD_RETURN_ERROR);
116: }
117: env = item->state.tflag.s->environ;
118: }
119:
120: if (args->argc != 0) {
121: envent = environ_find(env, args->argv[0]);
122: if (envent == NULL) {
123: cmdq_error(item, "unknown variable: %s", args->argv[0]);
124: return (CMD_RETURN_ERROR);
125: }
126: cmd_show_environment_print(self, item, envent);
127: return (CMD_RETURN_NORMAL);
128: }
129:
130: envent = environ_first(env);
131: while (envent != NULL) {
132: cmd_show_environment_print(self, item, envent);
133: envent = environ_next(envent);
134: }
135: return (CMD_RETURN_NORMAL);
136: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>