1 /* $OpenBSD$ */ 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 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 "show-environment", "showenv", 38 "gst:", 0, 1, 39 "[-gs] " CMD_TARGET_SESSION_USAGE " [name]", 40 0, 41 cmd_show_environment_exec 42 }; 43 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 85 enum cmd_retval 86 cmd_show_environment_exec(struct cmd *self, struct cmd_q *cmdq) 87 { 88 struct args *args = self->args; 89 struct session *s; 90 struct environ *env; 91 struct environ_entry *envent; 92 93 if (args_has(self->args, 'g')) 94 env = &global_environ; 95 else { 96 s = cmd_find_session(cmdq, args_get(args, 't'), 0); 97 if (s == NULL) 98 return (CMD_RETURN_ERROR); 99 env = &s->environ; 100 } 101 102 if (args->argc != 0) { 103 envent = environ_find(env, args->argv[0]); 104 if (envent == NULL) { 105 cmdq_error(cmdq, "unknown variable: %s", args->argv[0]); 106 return (CMD_RETURN_ERROR); 107 } 108 cmd_show_environment_print(self, cmdq, envent); 109 return (CMD_RETURN_NORMAL); 110 } 111 112 RB_FOREACH(envent, environ, env) 113 cmd_show_environment_print(self, cmdq, envent); 114 return (CMD_RETURN_NORMAL); 115 } 116