Coverage Report

Created: 2025-07-18 06:48

/src/tmux/cmd-show-environment.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD$ */
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
 * Show environment.
28
 */
29
30
static enum cmd_retval  cmd_show_environment_exec(struct cmd *,
31
          struct cmdq_item *);
32
33
static char *cmd_show_environment_escape(struct environ_entry *);
34
static void  cmd_show_environment_print(struct cmd *, struct cmdq_item *,
35
         struct environ_entry *);
36
37
const struct cmd_entry cmd_show_environment_entry = {
38
  .name = "show-environment",
39
  .alias = "showenv",
40
41
  .args = { "hgst:", 0, 1, NULL },
42
  .usage = "[-hgs] " CMD_TARGET_SESSION_USAGE " [variable]",
43
44
  .target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL },
45
46
  .flags = CMD_AFTERHOOK,
47
  .exec = cmd_show_environment_exec
48
};
49
50
static char *
51
cmd_show_environment_escape(struct environ_entry *envent)
52
0
{
53
0
  const char  *value = envent->value;
54
0
  char     c, *out, *ret;
55
56
0
  out = ret = xmalloc(strlen(value) * 2 + 1); /* at most twice the size */
57
0
  while ((c = *value++) != '\0') {
58
    /* POSIX interprets $ ` " and \ in double quotes. */
59
0
    if (c == '$' || c == '`' || c == '"' || c == '\\')
60
0
      *out++ = '\\';
61
0
    *out++ = c;
62
0
  }
63
0
  *out = '\0';
64
65
0
  return (ret);
66
0
}
67
68
static void
69
cmd_show_environment_print(struct cmd *self, struct cmdq_item *item,
70
    struct environ_entry *envent)
71
0
{
72
0
  struct args *args = cmd_get_args(self);
73
0
  char    *escaped;
74
75
0
  if (!args_has(args, 'h') && (envent->flags & ENVIRON_HIDDEN))
76
0
    return;
77
0
  if (args_has(args, 'h') && (~envent->flags & ENVIRON_HIDDEN))
78
0
    return;
79
80
0
  if (!args_has(args, 's')) {
81
0
    if (envent->value != NULL)
82
0
      cmdq_print(item, "%s=%s", envent->name, envent->value);
83
0
    else
84
0
      cmdq_print(item, "-%s", envent->name);
85
0
    return;
86
0
  }
87
88
0
  if (envent->value != NULL) {
89
0
    escaped = cmd_show_environment_escape(envent);
90
0
    cmdq_print(item, "%s=\"%s\"; export %s;", envent->name, escaped,
91
0
        envent->name);
92
0
    free(escaped);
93
0
  } else
94
0
    cmdq_print(item, "unset %s;", envent->name);
95
0
}
96
97
static enum cmd_retval
98
cmd_show_environment_exec(struct cmd *self, struct cmdq_item *item)
99
0
{
100
0
  struct args   *args = cmd_get_args(self);
101
0
  struct cmd_find_state *target = cmdq_get_target(item);
102
0
  struct environ    *env;
103
0
  struct environ_entry  *envent;
104
0
  const char    *tflag, *name = args_string(args, 0);
105
106
0
  if ((tflag = args_get(args, 't')) != NULL) {
107
0
    if (target->s == NULL) {
108
0
      cmdq_error(item, "no such session: %s", tflag);
109
0
      return (CMD_RETURN_ERROR);
110
0
    }
111
0
  }
112
113
0
  if (args_has(args, 'g'))
114
0
    env = global_environ;
115
0
  else {
116
0
    if (target->s == NULL) {
117
0
      tflag = args_get(args, 't');
118
0
      if (tflag != NULL)
119
0
        cmdq_error(item, "no such session: %s", tflag);
120
0
      else
121
0
        cmdq_error(item, "no current session");
122
0
      return (CMD_RETURN_ERROR);
123
0
    }
124
0
    env = target->s->environ;
125
0
  }
126
127
0
  if (name != NULL) {
128
0
    envent = environ_find(env, name);
129
0
    if (envent == NULL) {
130
0
      cmdq_error(item, "unknown variable: %s", name);
131
0
      return (CMD_RETURN_ERROR);
132
0
    }
133
0
    cmd_show_environment_print(self, item, envent);
134
0
    return (CMD_RETURN_NORMAL);
135
0
  }
136
137
0
  envent = environ_first(env);
138
0
  while (envent != NULL) {
139
0
    cmd_show_environment_print(self, item, envent);
140
0
    envent = environ_next(envent);
141
0
  }
142
0
  return (CMD_RETURN_NORMAL);
143
0
}