Annotation of src/usr.bin/tmux/cmd-show-environment.c, Revision 1.19
1.19 ! nicm 1: /* $OpenBSD: cmd-show-environment.c,v 1.18 2016/01/19 15:59:12 nicm Exp $ */
1.1 nicm 2:
3: /*
1.18 nicm 4: * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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 = {
1.15 nicm 37: .name = "show-environment",
38: .alias = "showenv",
39:
40: .args = { "gst:", 0, 1 },
41: .usage = "[-gs] " CMD_TARGET_SESSION_USAGE " [name]",
42:
1.16 nicm 43: .tflag = CMD_SESSION_CANFAIL,
44:
45: .flags = 0,
1.15 nicm 46: .exec = cmd_show_environment_exec
1.1 nicm 47: };
48:
1.10 nicm 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:
1.11 nicm 64: return (ret);
1.10 nicm 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:
1.6 nicm 90: enum cmd_retval
1.7 nicm 91: cmd_show_environment_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 92: {
1.3 nicm 93: struct args *args = self->args;
94: struct environ *env;
95: struct environ_entry *envent;
1.19 ! nicm 96: const char *target;
1.1 nicm 97:
1.19 ! nicm 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'))
1.12 nicm 106: env = global_environ;
1.19 ! nicm 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: }
1.13 nicm 116: env = cmdq->state.tflag.s->environ;
1.19 ! nicm 117: }
1.5 nicm 118:
119: if (args->argc != 0) {
120: envent = environ_find(env, args->argv[0]);
121: if (envent == NULL) {
1.7 nicm 122: cmdq_error(cmdq, "unknown variable: %s", args->argv[0]);
1.6 nicm 123: return (CMD_RETURN_ERROR);
1.5 nicm 124: }
1.10 nicm 125: cmd_show_environment_print(self, cmdq, envent);
1.6 nicm 126: return (CMD_RETURN_NORMAL);
1.1 nicm 127: }
128:
1.12 nicm 129: envent = environ_first(env);
130: while (envent != NULL) {
1.10 nicm 131: cmd_show_environment_print(self, cmdq, envent);
1.12 nicm 132: envent = environ_next(envent);
133: }
1.6 nicm 134: return (CMD_RETURN_NORMAL);
1.1 nicm 135: }