Annotation of src/usr.bin/tmux/cmd-set-environment.c, Revision 1.24
1.24 ! nicm 1: /* $OpenBSD: cmd-set-environment.c,v 1.23 2020/03/31 17:14:40 nicm Exp $ */
1.1 nicm 2:
3: /*
1.17 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: * Set an environment variable.
28: */
29:
1.21 nicm 30: static enum cmd_retval cmd_set_environment_exec(struct cmd *,
31: struct cmdq_item *);
1.1 nicm 32:
33: const struct cmd_entry cmd_set_environment_entry = {
1.14 nicm 34: .name = "set-environment",
35: .alias = "setenv",
36:
1.23 nicm 37: .args = { "hgrt:u", 1, 2 },
38: .usage = "[-hgru] " CMD_TARGET_SESSION_USAGE " name [value]",
1.14 nicm 39:
1.22 nicm 40: .target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL },
1.15 nicm 41:
1.20 nicm 42: .flags = CMD_AFTERHOOK,
1.14 nicm 43: .exec = cmd_set_environment_exec
1.1 nicm 44: };
45:
1.19 nicm 46: static enum cmd_retval
1.21 nicm 47: cmd_set_environment_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 48: {
1.24 ! nicm 49: struct args *args = cmd_get_args(self);
1.4 nicm 50: struct environ *env;
1.18 nicm 51: const char *name, *value, *target;
1.1 nicm 52:
1.4 nicm 53: name = args->argv[0];
54: if (*name == '\0') {
1.21 nicm 55: cmdq_error(item, "empty variable name");
1.6 nicm 56: return (CMD_RETURN_ERROR);
1.1 nicm 57: }
1.4 nicm 58: if (strchr(name, '=') != NULL) {
1.21 nicm 59: cmdq_error(item, "variable name contains =");
1.6 nicm 60: return (CMD_RETURN_ERROR);
1.1 nicm 61: }
62:
1.7 okan 63: if (args->argc < 2)
1.4 nicm 64: value = NULL;
65: else
66: value = args->argv[1];
67:
1.24 ! nicm 68: if (args_has(args, 'g'))
1.11 nicm 69: env = global_environ;
1.18 nicm 70: else {
1.22 nicm 71: if (item->target.s == NULL) {
1.18 nicm 72: target = args_get(args, 't');
73: if (target != NULL)
1.21 nicm 74: cmdq_error(item, "no such session: %s", target);
1.18 nicm 75: else
1.21 nicm 76: cmdq_error(item, "no current session");
1.18 nicm 77: return (CMD_RETURN_ERROR);
78: }
1.22 nicm 79: env = item->target.s->environ;
1.18 nicm 80: }
1.1 nicm 81:
1.24 ! nicm 82: if (args_has(args, 'u')) {
1.4 nicm 83: if (value != NULL) {
1.21 nicm 84: cmdq_error(item, "can't specify a value with -u");
1.6 nicm 85: return (CMD_RETURN_ERROR);
1.1 nicm 86: }
1.4 nicm 87: environ_unset(env, name);
1.24 ! nicm 88: } else if (args_has(args, 'r')) {
1.4 nicm 89: if (value != NULL) {
1.21 nicm 90: cmdq_error(item, "can't specify a value with -r");
1.6 nicm 91: return (CMD_RETURN_ERROR);
1.1 nicm 92: }
1.12 nicm 93: environ_clear(env, name);
1.1 nicm 94: } else {
1.4 nicm 95: if (value == NULL) {
1.21 nicm 96: cmdq_error(item, "no value specified");
1.6 nicm 97: return (CMD_RETURN_ERROR);
1.1 nicm 98: }
1.23 nicm 99: if (args_has(args, 'h'))
100: environ_set(env, name, ENVIRON_HIDDEN, "%s", value);
101: else
102: environ_set(env, name, 0, "%s", value);
1.1 nicm 103: }
104:
1.6 nicm 105: return (CMD_RETURN_NORMAL);
1.1 nicm 106: }