1 /* $OpenBSD: cmd-set-environment.c,v 1.26 2020/09/01 09:19:01 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 * Set an environment variable. 28 */ 29 30 static enum cmd_retval cmd_set_environment_exec(struct cmd *, 31 struct cmdq_item *); 32 33 const struct cmd_entry cmd_set_environment_entry = { 34 .name = "set-environment", 35 .alias = "setenv", 36 37 .args = { "Fhgrt:u", 1, 2 }, 38 .usage = "[-Fhgru] " CMD_TARGET_SESSION_USAGE " name [value]", 39 40 .target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL }, 41 42 .flags = CMD_AFTERHOOK, 43 .exec = cmd_set_environment_exec 44 }; 45 46 static enum cmd_retval 47 cmd_set_environment_exec(struct cmd *self, struct cmdq_item *item) 48 { 49 struct args *args = cmd_get_args(self); 50 struct cmd_find_state *target = cmdq_get_target(item); 51 struct environ *env; 52 const char *name, *value, *tflag; 53 char *expand = NULL; 54 enum cmd_retval retval = CMD_RETURN_NORMAL; 55 56 name = args->argv[0]; 57 if (*name == '\0') { 58 cmdq_error(item, "empty variable name"); 59 return (CMD_RETURN_ERROR); 60 } 61 if (strchr(name, '=') != NULL) { 62 cmdq_error(item, "variable name contains ="); 63 return (CMD_RETURN_ERROR); 64 } 65 66 if (args->argc < 2) 67 value = NULL; 68 else if (args_has(args, 'F')) 69 value = expand = format_single_from_target(item, args->argv[1]); 70 else 71 value = args->argv[1]; 72 73 if (args_has(args, 'g')) 74 env = global_environ; 75 else { 76 if (target->s == NULL) { 77 tflag = args_get(args, 't'); 78 if (tflag != NULL) 79 cmdq_error(item, "no such session: %s", tflag); 80 else 81 cmdq_error(item, "no current session"); 82 retval = CMD_RETURN_ERROR; 83 goto out; 84 } 85 env = target->s->environ; 86 } 87 88 if (args_has(args, 'u')) { 89 if (value != NULL) { 90 cmdq_error(item, "can't specify a value with -u"); 91 retval = CMD_RETURN_ERROR; 92 goto out; 93 } 94 environ_unset(env, name); 95 } else if (args_has(args, 'r')) { 96 if (value != NULL) { 97 cmdq_error(item, "can't specify a value with -r"); 98 retval = CMD_RETURN_ERROR; 99 goto out; 100 } 101 environ_clear(env, name); 102 } else { 103 if (value == NULL) { 104 cmdq_error(item, "no value specified"); 105 retval = CMD_RETURN_ERROR; 106 goto out; 107 } 108 109 if (args_has(args, 'h')) 110 environ_set(env, name, ENVIRON_HIDDEN, "%s", value); 111 else 112 environ_set(env, name, 0, "%s", value); 113 } 114 115 out: 116 free(expand); 117 return (retval); 118 } 119