xref: /openbsd-src/usr.bin/tmux/cmd-command-prompt.c (revision 3e68b6f46147884e15b8f978cc0313b39c20d681)
1 /* $OpenBSD: cmd-command-prompt.c,v 1.57 2021/08/17 19:26:42 nicm Exp $ */
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 cmd_retval	cmd_command_prompt_exec(struct cmd *,
33 			    struct cmdq_item *);
34 
35 static int	cmd_command_prompt_callback(struct client *, void *,
36 		    const char *, int);
37 static void	cmd_command_prompt_free(void *);
38 
39 const struct cmd_entry cmd_command_prompt_entry = {
40 	.name = "command-prompt",
41 	.alias = NULL,
42 
43 	.args = { "1bFkiI:Np:t:T:", 0, 1 },
44 	.usage = "[-1bFkiN] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE
45 		 " [-T type] [template]",
46 
47 	.flags = CMD_CLIENT_TFLAG,
48 	.exec = cmd_command_prompt_exec
49 };
50 
51 struct cmd_command_prompt_cdata {
52 	struct cmdq_item	*item;
53 	struct cmd_parse_input	 pi;
54 
55 	int			 flags;
56 	enum prompt_type	 prompt_type;
57 
58 	char			*inputs;
59 	char			*next_input;
60 
61 	char			*prompts;
62 	char			*next_prompt;
63 
64 	char			*template;
65 	int	 		 idx;
66 };
67 
68 static enum cmd_retval
69 cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item)
70 {
71 	struct args			*args = cmd_get_args(self);
72 	struct client			*tc = cmdq_get_target_client(item);
73 	struct cmd_find_state		*target = cmdq_get_target(item);
74 	const char			*inputs, *prompts, *type;
75 	struct cmd_command_prompt_cdata	*cdata;
76 	char				*prompt, *ptr, *input = NULL;
77 	size_t				 n;
78 	int				 wait = !args_has(args, 'b');
79 
80 	if (tc->prompt_string != NULL)
81 		return (CMD_RETURN_NORMAL);
82 	if (args_has(args, 'i'))
83 		wait = 0;
84 
85 	cdata = xcalloc(1, sizeof *cdata);
86 	cdata->idx = 1;
87 
88 	cmd_get_source(self, &cdata->pi.file, &cdata->pi.line);
89 	if (wait)
90 		cdata->pi.item = item;
91 	cdata->pi.c = tc;
92 	cmd_find_copy_state(&cdata->pi.fs, target);
93 
94 	if (wait)
95 		cdata->item = item;
96 
97 	if (args->argc != 0 && args_has(args, 'F'))
98 	    cdata->template = format_single_from_target(item, args->argv[0]);
99 	else if (args->argc != 0)
100 		cdata->template = xstrdup(args->argv[0]);
101 	else
102 		cdata->template = xstrdup("%1");
103 
104 	if ((prompts = args_get(args, 'p')) != NULL)
105 		cdata->prompts = xstrdup(prompts);
106 	else if (args->argc != 0) {
107 		n = strcspn(cdata->template, " ,");
108 		xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
109 	} else
110 		cdata->prompts = xstrdup(":");
111 
112 	/* Get first prompt. */
113 	cdata->next_prompt = cdata->prompts;
114 	ptr = strsep(&cdata->next_prompt, ",");
115 	if (prompts == NULL)
116 		prompt = xstrdup(ptr);
117 	else
118 		xasprintf(&prompt, "%s ", ptr);
119 
120 	/* Get initial prompt input. */
121 	if ((inputs = args_get(args, 'I')) != NULL) {
122 		cdata->inputs = xstrdup(inputs);
123 		cdata->next_input = cdata->inputs;
124 		input = strsep(&cdata->next_input, ",");
125 	}
126 
127 	/* Get prompt type. */
128 	if ((type = args_get(args, 'T')) != NULL) {
129 		cdata->prompt_type = status_prompt_type(type);
130 		if (cdata->prompt_type == PROMPT_TYPE_INVALID) {
131 			cmdq_error(item, "unknown type: %s", type);
132 			return (CMD_RETURN_ERROR);
133 		}
134 	} else
135 		cdata->prompt_type = PROMPT_TYPE_COMMAND;
136 
137 	if (args_has(args, '1'))
138 		cdata->flags |= PROMPT_SINGLE;
139 	else if (args_has(args, 'N'))
140 		cdata->flags |= PROMPT_NUMERIC;
141 	else if (args_has(args, 'i'))
142 		cdata->flags |= PROMPT_INCREMENTAL;
143 	else if (args_has(args, 'k'))
144 		cdata->flags |= PROMPT_KEY;
145 	status_prompt_set(tc, target, prompt, input,
146 	    cmd_command_prompt_callback, cmd_command_prompt_free, cdata,
147 	    cdata->flags, cdata->prompt_type);
148 	free(prompt);
149 
150 	if (!wait)
151 		return (CMD_RETURN_NORMAL);
152 	return (CMD_RETURN_WAIT);
153 }
154 
155 static int
156 cmd_command_prompt_callback(struct client *c, void *data, const char *s,
157     int done)
158 {
159 	struct cmd_command_prompt_cdata	*cdata = data;
160 	char				*new_template, *prompt, *ptr, *error;
161 	char				*input = NULL;
162 	struct cmdq_item		*item = cdata->item;
163 	enum cmd_parse_status		 status;
164 
165 	if (s == NULL)
166 		goto out;
167 	if (done && (cdata->flags & PROMPT_INCREMENTAL))
168 		goto out;
169 
170 	new_template = cmd_template_replace(cdata->template, s, cdata->idx);
171 	if (done) {
172 		free(cdata->template);
173 		cdata->template = new_template;
174 	}
175 
176 	/*
177 	 * Check if there are more prompts; if so, get its respective input
178 	 * and update the prompt data.
179 	 */
180 	if (done && (ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
181 		xasprintf(&prompt, "%s ", ptr);
182 		input = strsep(&cdata->next_input, ",");
183 		status_prompt_update(c, prompt, input);
184 
185 		free(prompt);
186 		cdata->idx++;
187 		return (1);
188 	}
189 
190 	if (item != NULL) {
191 		status = cmd_parse_and_insert(new_template, &cdata->pi, item,
192 		    cmdq_get_state(item), &error);
193 	} else {
194 		status = cmd_parse_and_append(new_template, &cdata->pi, c, NULL,
195 		    &error);
196 	}
197 	if (status == CMD_PARSE_ERROR) {
198 		cmdq_append(c, cmdq_get_error(error));
199 		free(error);
200 	}
201 
202 	if (!done)
203 		free(new_template);
204 	if (c->prompt_inputcb != cmd_command_prompt_callback)
205 		return (1);
206 
207 out:
208         if (item != NULL)
209                 cmdq_continue(item);
210 	return (0);
211 }
212 
213 static void
214 cmd_command_prompt_free(void *data)
215 {
216 	struct cmd_command_prompt_cdata	*cdata = data;
217 
218 	free(cdata->inputs);
219 	free(cdata->prompts);
220 	free(cdata->template);
221 	free(cdata);
222 }
223