Coverage Report

Created: 2025-07-11 06:20

/src/tmux/cmd-command-prompt.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2008 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 <ctype.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <time.h>
25
26
#include "tmux.h"
27
28
/*
29
 * Prompt for command in client.
30
 */
31
32
static enum args_parse_type cmd_command_prompt_args_parse(struct args *,
33
            u_int, char **);
34
static enum cmd_retval    cmd_command_prompt_exec(struct cmd *,
35
            struct cmdq_item *);
36
37
static int  cmd_command_prompt_callback(struct client *, void *,
38
        const char *, int);
39
static void cmd_command_prompt_free(void *);
40
41
const struct cmd_entry cmd_command_prompt_entry = {
42
  .name = "command-prompt",
43
  .alias = NULL,
44
45
  .args = { "1bFkiI:Np:t:T:", 0, 1, cmd_command_prompt_args_parse },
46
  .usage = "[-1bFkiN] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE
47
     " [-T prompt-type] [template]",
48
49
  .flags = CMD_CLIENT_TFLAG,
50
  .exec = cmd_command_prompt_exec
51
};
52
53
struct cmd_command_prompt_prompt {
54
  char  *input;
55
  char  *prompt;
56
};
57
58
struct cmd_command_prompt_cdata {
59
  struct cmdq_item     *item;
60
  struct args_command_state  *state;
61
62
  int         flags;
63
  enum prompt_type      prompt_type;
64
65
  struct cmd_command_prompt_prompt *prompts;
66
  u_int         count;
67
  u_int         current;
68
69
  int         argc;
70
  char        **argv;
71
};
72
73
static enum args_parse_type
74
cmd_command_prompt_args_parse(__unused struct args *args, __unused u_int idx,
75
    __unused char **cause)
76
0
{
77
0
  return (ARGS_PARSE_COMMANDS_OR_STRING);
78
0
}
79
80
static enum cmd_retval
81
cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item)
82
0
{
83
0
  struct args     *args = cmd_get_args(self);
84
0
  struct client     *tc = cmdq_get_target_client(item);
85
0
  struct cmd_find_state   *target = cmdq_get_target(item);
86
0
  const char      *type, *s, *input;
87
0
  struct cmd_command_prompt_cdata *cdata;
88
0
  char        *tmp, *prompts, *prompt, *next_prompt;
89
0
  char        *inputs = NULL, *next_input;
90
0
  u_int        count = args_count(args);
91
0
  int        wait = !args_has(args, 'b'), space = 1;
92
93
0
  if (tc->prompt_string != NULL)
94
0
    return (CMD_RETURN_NORMAL);
95
0
  if (args_has(args, 'i'))
96
0
    wait = 0;
97
98
0
  cdata = xcalloc(1, sizeof *cdata);
99
0
  if (wait)
100
0
    cdata->item = item;
101
0
  cdata->state = args_make_commands_prepare(self, item, 0, "%1", wait,
102
0
      args_has(args, 'F'));
103
104
0
  if ((s = args_get(args, 'p')) == NULL) {
105
0
    if (count != 0) {
106
0
      tmp = args_make_commands_get_command(cdata->state);
107
0
      xasprintf(&prompts, "(%s)", tmp);
108
0
      free(tmp);
109
0
    } else {
110
0
      prompts = xstrdup(":");
111
0
      space = 0;
112
0
    }
113
0
    next_prompt = prompts;
114
0
  } else
115
0
    next_prompt = prompts = xstrdup(s);
116
0
  if ((s = args_get(args, 'I')) != NULL)
117
0
    next_input = inputs = xstrdup(s);
118
0
  else
119
0
    next_input = NULL;
120
0
  while ((prompt = strsep(&next_prompt, ",")) != NULL) {
121
0
    cdata->prompts = xreallocarray(cdata->prompts, cdata->count + 1,
122
0
        sizeof *cdata->prompts);
123
0
    if (!space)
124
0
      tmp = xstrdup(prompt);
125
0
    else
126
0
      xasprintf(&tmp, "%s ", prompt);
127
0
    cdata->prompts[cdata->count].prompt = tmp;
128
129
0
    if (next_input != NULL) {
130
0
      input = strsep(&next_input, ",");
131
0
      if (input == NULL)
132
0
        input = "";
133
0
    } else
134
0
      input = "";
135
0
    cdata->prompts[cdata->count].input = xstrdup(input);
136
137
0
    cdata->count++;
138
0
  }
139
0
  free(inputs);
140
0
  free(prompts);
141
142
0
  if ((type = args_get(args, 'T')) != NULL) {
143
0
    cdata->prompt_type = status_prompt_type(type);
144
0
    if (cdata->prompt_type == PROMPT_TYPE_INVALID) {
145
0
      cmdq_error(item, "unknown type: %s", type);
146
0
      cmd_command_prompt_free(cdata);
147
0
      return (CMD_RETURN_ERROR);
148
0
    }
149
0
  } else
150
0
    cdata->prompt_type = PROMPT_TYPE_COMMAND;
151
152
0
  if (args_has(args, '1'))
153
0
    cdata->flags |= PROMPT_SINGLE;
154
0
  else if (args_has(args, 'N'))
155
0
    cdata->flags |= PROMPT_NUMERIC;
156
0
  else if (args_has(args, 'i'))
157
0
    cdata->flags |= PROMPT_INCREMENTAL;
158
0
  else if (args_has(args, 'k'))
159
0
    cdata->flags |= PROMPT_KEY;
160
0
  status_prompt_set(tc, target, cdata->prompts[0].prompt,
161
0
      cdata->prompts[0].input, cmd_command_prompt_callback,
162
0
      cmd_command_prompt_free, cdata, cdata->flags, cdata->prompt_type);
163
164
0
  if (!wait)
165
0
    return (CMD_RETURN_NORMAL);
166
0
  return (CMD_RETURN_WAIT);
167
0
}
168
169
static int
170
cmd_command_prompt_callback(struct client *c, void *data, const char *s,
171
    int done)
172
0
{
173
0
  struct cmd_command_prompt_cdata    *cdata = data;
174
0
  char           *error;
175
0
  struct cmdq_item       *item = cdata->item, *new_item;
176
0
  struct cmd_list        *cmdlist;
177
0
  struct cmd_command_prompt_prompt   *prompt;
178
0
  int           argc = 0;
179
0
  char          **argv = NULL;
180
181
0
  if (s == NULL)
182
0
    goto out;
183
184
0
  if (done) {
185
0
    if (cdata->flags & PROMPT_INCREMENTAL)
186
0
      goto out;
187
0
    cmd_append_argv(&cdata->argc, &cdata->argv, s);
188
0
    if (++cdata->current != cdata->count) {
189
0
      prompt = &cdata->prompts[cdata->current];
190
0
      status_prompt_update(c, prompt->prompt, prompt->input);
191
0
      return (1);
192
0
    }
193
0
  }
194
195
0
  argc = cdata->argc;
196
0
  argv = cmd_copy_argv(cdata->argc, cdata->argv);
197
0
  if (!done)
198
0
    cmd_append_argv(&argc, &argv, s);
199
200
0
  if (done) {
201
0
    cmd_free_argv(cdata->argc, cdata->argv);
202
0
    cdata->argc = argc;
203
0
    cdata->argv = cmd_copy_argv(argc, argv);
204
0
  }
205
206
0
  cmdlist = args_make_commands(cdata->state, argc, argv, &error);
207
0
  if (cmdlist == NULL) {
208
0
    cmdq_append(c, cmdq_get_error(error));
209
0
    free(error);
210
0
  } else if (item == NULL) {
211
0
    new_item = cmdq_get_command(cmdlist, NULL);
212
0
    cmdq_append(c, new_item);
213
0
  } else {
214
0
    new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
215
0
    cmdq_insert_after(item, new_item);
216
0
  }
217
0
  cmd_free_argv(argc, argv);
218
219
0
  if (c->prompt_inputcb != cmd_command_prompt_callback)
220
0
    return (1);
221
222
0
out:
223
0
  if (item != NULL)
224
0
    cmdq_continue(item);
225
0
  return (0);
226
0
}
227
228
static void
229
cmd_command_prompt_free(void *data)
230
0
{
231
0
  struct cmd_command_prompt_cdata *cdata = data;
232
0
  u_int        i;
233
234
0
  for (i = 0; i < cdata->count; i++) {
235
0
    free(cdata->prompts[i].prompt);
236
0
    free(cdata->prompts[i].input);
237
0
  }
238
0
  free(cdata->prompts);
239
0
  cmd_free_argv(cdata->argc, cdata->argv);
240
0
  args_make_commands_free(cdata->state);
241
0
  free(cdata);
242
0
}