1 /* $OpenBSD: cmd-show-environment.c,v 1.19 2016/03/03 14:15:22 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 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 *);
35 
36 const struct cmd_entry cmd_show_environment_entry = {
37 	.name = "show-environment",
38 	.alias = "showenv",
39 
40 	.args = { "gst:", 0, 1 },
41 	.usage = "[-gs] " CMD_TARGET_SESSION_USAGE " [name]",
42 
43 	.tflag = CMD_SESSION_CANFAIL,
44 
45 	.flags = 0,
46 	.exec = cmd_show_environment_exec
47 };
48 
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 
64 	return (ret);
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 
90 enum cmd_retval
91 cmd_show_environment_exec(struct cmd *self, struct cmd_q *cmdq)
92 {
93 	struct args		*args = self->args;
94 	struct environ		*env;
95 	struct environ_entry	*envent;
96 	const char		*target;
97 
98 	if ((target = args_get(args, 't')) != NULL) {
99 		if (cmdq->state.tflag.s == NULL) {
100 			cmdq_error(cmdq, "no such session: %s", target);
101 			return (CMD_RETURN_ERROR);
102 		}
103 	}
104 
105 	if (args_has(self->args, 'g'))
106 		env = global_environ;
107 	else {
108 		if (cmdq->state.tflag.s == NULL) {
109 			target = args_get(args, 't');
110 			if (target != NULL)
111 				cmdq_error(cmdq, "no such session: %s", target);
112 			else
113 				cmdq_error(cmdq, "no current session");
114 			return (CMD_RETURN_ERROR);
115 		}
116 		env = cmdq->state.tflag.s->environ;
117 	}
118 
119 	if (args->argc != 0) {
120 		envent = environ_find(env, args->argv[0]);
121 		if (envent == NULL) {
122 			cmdq_error(cmdq, "unknown variable: %s", args->argv[0]);
123 			return (CMD_RETURN_ERROR);
124 		}
125 		cmd_show_environment_print(self, cmdq, envent);
126 		return (CMD_RETURN_NORMAL);
127 	}
128 
129 	envent = environ_first(env);
130 	while (envent != NULL) {
131 		cmd_show_environment_print(self, cmdq, envent);
132 		envent = environ_next(envent);
133 	}
134 	return (CMD_RETURN_NORMAL);
135 }
136