/src/tmux/cmd-show-environment.c
Line | Count | Source |
1 | | /* $OpenBSD: cmd-show-environment.c,v 1.30 2026/10/09 13:12:14 nicm Exp $ */ |
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 | | .description = "Show environment variables.", |
40 | | .alias = "showenv", |
41 | | |
42 | | .args = { "hgst:", 0, 1, NULL }, |
43 | | .usage = "[-hgs] " CMD_TARGET_SESSION_USAGE " [variable]", |
44 | | |
45 | | .target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL }, |
46 | | |
47 | | .flags = CMD_AFTERHOOK, |
48 | | .exec = cmd_show_environment_exec |
49 | | }; |
50 | | |
51 | | static char * |
52 | | cmd_show_environment_escape(struct environ_entry *envent) |
53 | 0 | { |
54 | 0 | const char *value = envent->value; |
55 | 0 | char c, *out, *ret; |
56 | |
|
57 | 0 | out = ret = xmalloc(strlen(value) * 2 + 1); /* at most twice the size */ |
58 | 0 | while ((c = *value++) != '\0') { |
59 | | /* POSIX interprets $ ` " and \ in double quotes. */ |
60 | 0 | if (c == '$' || c == '`' || c == '"' || c == '\\') |
61 | 0 | *out++ = '\\'; |
62 | 0 | *out++ = c; |
63 | 0 | } |
64 | 0 | *out = '\0'; |
65 | |
|
66 | 0 | return (ret); |
67 | 0 | } |
68 | | |
69 | | static void |
70 | | cmd_show_environment_print(struct cmd *self, struct cmdq_item *item, |
71 | | struct environ_entry *envent) |
72 | 0 | { |
73 | 0 | struct args *args = cmd_get_args(self); |
74 | 0 | char *escaped; |
75 | |
|
76 | 0 | if (!args_has(args, 'h') && (envent->flags & ENVIRON_HIDDEN)) |
77 | 0 | return; |
78 | 0 | if (args_has(args, 'h') && (~envent->flags & ENVIRON_HIDDEN)) |
79 | 0 | return; |
80 | | |
81 | 0 | if (!args_has(args, 's')) { |
82 | 0 | if (envent->value != NULL) |
83 | 0 | cmdq_print(item, "%s=%s", envent->name, envent->value); |
84 | 0 | else |
85 | 0 | cmdq_print(item, "-%s", envent->name); |
86 | 0 | return; |
87 | 0 | } |
88 | | |
89 | 0 | if (envent->value != NULL) { |
90 | 0 | escaped = cmd_show_environment_escape(envent); |
91 | 0 | cmdq_print(item, "%s=\"%s\"; export %s;", envent->name, escaped, |
92 | 0 | envent->name); |
93 | 0 | free(escaped); |
94 | 0 | } else |
95 | 0 | cmdq_print(item, "unset %s;", envent->name); |
96 | 0 | } |
97 | | |
98 | | static enum cmd_retval |
99 | | cmd_show_environment_exec(struct cmd *self, struct cmdq_item *item) |
100 | 0 | { |
101 | 0 | struct args *args = cmd_get_args(self); |
102 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
103 | 0 | struct environ *env; |
104 | 0 | struct environ_entry *envent; |
105 | 0 | const char *tflag, *name = args_string(args, 0); |
106 | |
|
107 | 0 | if ((tflag = args_get(args, 't')) != NULL) { |
108 | 0 | if (target->s == NULL) { |
109 | 0 | cmdq_error(item, "no such session: %s", tflag); |
110 | 0 | return (CMD_RETURN_ERROR); |
111 | 0 | } |
112 | 0 | } |
113 | | |
114 | 0 | if (args_has(args, 'g')) |
115 | 0 | env = global_environ; |
116 | 0 | else { |
117 | 0 | if (target->s == NULL) { |
118 | 0 | tflag = args_get(args, 't'); |
119 | 0 | if (tflag != NULL) |
120 | 0 | cmdq_error(item, "no such session: %s", tflag); |
121 | 0 | else |
122 | 0 | cmdq_error(item, "no current session"); |
123 | 0 | return (CMD_RETURN_ERROR); |
124 | 0 | } |
125 | 0 | env = target->s->environ; |
126 | 0 | } |
127 | | |
128 | 0 | if (name != NULL) { |
129 | 0 | envent = environ_find(env, name); |
130 | 0 | if (envent == NULL) { |
131 | 0 | cmdq_error(item, "unknown variable: %s", name); |
132 | 0 | return (CMD_RETURN_ERROR); |
133 | 0 | } |
134 | 0 | cmd_show_environment_print(self, item, envent); |
135 | 0 | return (CMD_RETURN_NORMAL); |
136 | 0 | } |
137 | | |
138 | 0 | envent = environ_first(env); |
139 | 0 | while (envent != NULL) { |
140 | 0 | cmd_show_environment_print(self, item, envent); |
141 | 0 | envent = environ_next(envent); |
142 | 0 | } |
143 | 0 | return (CMD_RETURN_NORMAL); |
144 | 0 | } |