Coverage Report

Created: 2026-10-17 06:19

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