xref: /openbsd-src/usr.bin/tmux/cmd-set-environment.c (revision a51dead1c4d3ed85038c588391e643b117324e5a)
1*a51dead1Snicm /* $OpenBSD: cmd-set-environment.c,v 1.28 2021/08/21 10:22:39 nicm Exp $ */
26f7d62ebSnicm 
36f7d62ebSnicm /*
498ca8272Snicm  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
56f7d62ebSnicm  *
66f7d62ebSnicm  * Permission to use, copy, modify, and distribute this software for any
76f7d62ebSnicm  * purpose with or without fee is hereby granted, provided that the above
86f7d62ebSnicm  * copyright notice and this permission notice appear in all copies.
96f7d62ebSnicm  *
106f7d62ebSnicm  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
116f7d62ebSnicm  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
126f7d62ebSnicm  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
136f7d62ebSnicm  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
146f7d62ebSnicm  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
156f7d62ebSnicm  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
166f7d62ebSnicm  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
176f7d62ebSnicm  */
186f7d62ebSnicm 
196f7d62ebSnicm #include <sys/types.h>
206f7d62ebSnicm 
216f7d62ebSnicm #include <stdlib.h>
226f7d62ebSnicm #include <string.h>
236f7d62ebSnicm 
246f7d62ebSnicm #include "tmux.h"
256f7d62ebSnicm 
266f7d62ebSnicm /*
276f7d62ebSnicm  * Set an environment variable.
286f7d62ebSnicm  */
296f7d62ebSnicm 
3068e0a7f2Snicm static enum cmd_retval	cmd_set_environment_exec(struct cmd *,
3168e0a7f2Snicm 			    struct cmdq_item *);
326f7d62ebSnicm 
336f7d62ebSnicm const struct cmd_entry cmd_set_environment_entry = {
34c057646bSnicm 	.name = "set-environment",
35c057646bSnicm 	.alias = "setenv",
36c057646bSnicm 
37*a51dead1Snicm 	.args = { "Fhgrt:u", 1, 2, NULL },
389f04068aSnicm 	.usage = "[-Fhgru] " CMD_TARGET_SESSION_USAGE " name [value]",
39c057646bSnicm 
40bf0d297eSnicm 	.target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL },
418d471e80Snicm 
427a61a8ddSnicm 	.flags = CMD_AFTERHOOK,
43c057646bSnicm 	.exec = cmd_set_environment_exec
446f7d62ebSnicm };
456f7d62ebSnicm 
46dc1f0f5fSnicm static enum cmd_retval
cmd_set_environment_exec(struct cmd * self,struct cmdq_item * item)4768e0a7f2Snicm cmd_set_environment_exec(struct cmd *self, struct cmdq_item *item)
486f7d62ebSnicm {
4990d7ba38Snicm 	struct args		*args = cmd_get_args(self);
50040343aeSnicm 	struct cmd_find_state	*target = cmdq_get_target(item);
516f7d62ebSnicm 	struct environ		*env;
521693b10bSnicm 	const char		*name = args_string(args, 0), *value;
531693b10bSnicm 	const char		*tflag;
541693b10bSnicm 	char			*expanded = NULL;
559f04068aSnicm 	enum cmd_retval		 retval = CMD_RETURN_NORMAL;
566f7d62ebSnicm 
57ca7befccSnicm 	if (*name == '\0') {
5868e0a7f2Snicm 		cmdq_error(item, "empty variable name");
59a224d0d3Snicm 		return (CMD_RETURN_ERROR);
606f7d62ebSnicm 	}
61ca7befccSnicm 	if (strchr(name, '=') != NULL) {
6268e0a7f2Snicm 		cmdq_error(item, "variable name contains =");
63a224d0d3Snicm 		return (CMD_RETURN_ERROR);
646f7d62ebSnicm 	}
656f7d62ebSnicm 
661693b10bSnicm 	if (args_count(args) < 2)
67ca7befccSnicm 		value = NULL;
68ca7befccSnicm 	else
691693b10bSnicm 		value = args_string(args, 1);
701693b10bSnicm 	if (value != NULL && args_has(args, 'F')) {
711693b10bSnicm 		expanded = format_single_from_target(item, value);
721693b10bSnicm 		value = expanded;
731693b10bSnicm 	}
7490d7ba38Snicm 	if (args_has(args, 'g'))
75fb46cb3dSnicm 		env = global_environ;
768864da59Snicm 	else {
77040343aeSnicm 		if (target->s == NULL) {
78040343aeSnicm 			tflag = args_get(args, 't');
79040343aeSnicm 			if (tflag != NULL)
80040343aeSnicm 				cmdq_error(item, "no such session: %s", tflag);
813447b427Snicm 			else
8268e0a7f2Snicm 				cmdq_error(item, "no current session");
839f04068aSnicm 			retval = CMD_RETURN_ERROR;
849f04068aSnicm 			goto out;
858864da59Snicm 		}
86040343aeSnicm 		env = target->s->environ;
878864da59Snicm 	}
886f7d62ebSnicm 
8990d7ba38Snicm 	if (args_has(args, 'u')) {
90ca7befccSnicm 		if (value != NULL) {
9168e0a7f2Snicm 			cmdq_error(item, "can't specify a value with -u");
929f04068aSnicm 			retval = CMD_RETURN_ERROR;
939f04068aSnicm 			goto out;
946f7d62ebSnicm 		}
95ca7befccSnicm 		environ_unset(env, name);
9690d7ba38Snicm 	} else if (args_has(args, 'r')) {
97ca7befccSnicm 		if (value != NULL) {
9868e0a7f2Snicm 			cmdq_error(item, "can't specify a value with -r");
999f04068aSnicm 			retval = CMD_RETURN_ERROR;
1009f04068aSnicm 			goto out;
1016f7d62ebSnicm 		}
102c4ce9d7aSnicm 		environ_clear(env, name);
1036f7d62ebSnicm 	} else {
104ca7befccSnicm 		if (value == NULL) {
10568e0a7f2Snicm 			cmdq_error(item, "no value specified");
1069f04068aSnicm 			retval = CMD_RETURN_ERROR;
1079f04068aSnicm 			goto out;
1086f7d62ebSnicm 		}
1099f04068aSnicm 
110d6f6a5d2Snicm 		if (args_has(args, 'h'))
111d6f6a5d2Snicm 			environ_set(env, name, ENVIRON_HIDDEN, "%s", value);
112d6f6a5d2Snicm 		else
113d6f6a5d2Snicm 			environ_set(env, name, 0, "%s", value);
1146f7d62ebSnicm 	}
1156f7d62ebSnicm 
1169f04068aSnicm out:
1171693b10bSnicm 	free(expanded);
1189f04068aSnicm 	return (retval);
1196f7d62ebSnicm }
120