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