xref: /openbsd-src/usr.bin/tmux/cmd-command-prompt.c (revision 37a3aee2ece7d35250a09a2ec42437d3655c5cf4)
1 /* $OpenBSD: cmd-command-prompt.c,v 1.52 2020/05/16 15:16:36 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 = { "1kiI:Np:Tt:W", 0, 1 },
44 	.usage = "[-1kiNTW] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " "
45 		 "[template]",
46 
47 	.flags = CMD_CLIENT_TFLAG,
48 	.exec = cmd_command_prompt_exec
49 };
50 
51 struct cmd_command_prompt_cdata {
52 	int	 flags;
53 
54 	char	*inputs;
55 	char	*next_input;
56 
57 	char	*prompts;
58 	char	*next_prompt;
59 
60 	char	*template;
61 	int	 idx;
62 };
63 
64 static enum cmd_retval
65 cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item)
66 {
67 	struct args			*args = cmd_get_args(self);
68 	struct client			*tc = cmdq_get_target_client(item);
69 	const char			*inputs, *prompts;
70 	struct cmd_command_prompt_cdata	*cdata;
71 	char				*prompt, *ptr, *input = NULL;
72 	size_t				 n;
73 
74 	if (tc->prompt_string != NULL)
75 		return (CMD_RETURN_NORMAL);
76 
77 	cdata = xcalloc(1, sizeof *cdata);
78 
79 	cdata->inputs = NULL;
80 	cdata->next_input = NULL;
81 
82 	cdata->prompts = NULL;
83 	cdata->next_prompt = NULL;
84 
85 	cdata->template = NULL;
86 	cdata->idx = 1;
87 
88 	if (args->argc != 0)
89 		cdata->template = xstrdup(args->argv[0]);
90 	else
91 		cdata->template = xstrdup("%1");
92 
93 	if ((prompts = args_get(args, 'p')) != NULL)
94 		cdata->prompts = xstrdup(prompts);
95 	else if (args->argc != 0) {
96 		n = strcspn(cdata->template, " ,");
97 		xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
98 	} else
99 		cdata->prompts = xstrdup(":");
100 
101 	/* Get first prompt. */
102 	cdata->next_prompt = cdata->prompts;
103 	ptr = strsep(&cdata->next_prompt, ",");
104 	if (prompts == NULL)
105 		prompt = xstrdup(ptr);
106 	else
107 		xasprintf(&prompt, "%s ", ptr);
108 
109 	/* Get initial prompt input. */
110 	if ((inputs = args_get(args, 'I')) != NULL) {
111 		cdata->inputs = xstrdup(inputs);
112 		cdata->next_input = cdata->inputs;
113 		input = strsep(&cdata->next_input, ",");
114 	}
115 
116 	if (args_has(args, '1'))
117 		cdata->flags |= PROMPT_SINGLE;
118 	else if (args_has(args, 'N'))
119 		cdata->flags |= PROMPT_NUMERIC;
120 	else if (args_has(args, 'i'))
121 		cdata->flags |= PROMPT_INCREMENTAL;
122 	else if (args_has(args, 'k'))
123 		cdata->flags |= PROMPT_KEY;
124 	else if (args_has(args, 'W'))
125 		cdata->flags |= PROMPT_WINDOW;
126 	else if (args_has(args, 'T'))
127 		cdata->flags |= PROMPT_TARGET;
128 	status_prompt_set(tc, prompt, input, cmd_command_prompt_callback,
129 	    cmd_command_prompt_free, cdata, cdata->flags);
130 	free(prompt);
131 
132 	return (CMD_RETURN_NORMAL);
133 }
134 
135 static int
136 cmd_command_prompt_callback(struct client *c, void *data, const char *s,
137     int done)
138 {
139 	struct cmd_command_prompt_cdata	*cdata = data;
140 	char				*new_template, *prompt, *ptr, *error;
141 	char				*input = NULL;
142 	enum cmd_parse_status		 status;
143 
144 	if (s == NULL)
145 		return (0);
146 	if (done && (cdata->flags & PROMPT_INCREMENTAL))
147 		return (0);
148 
149 	new_template = cmd_template_replace(cdata->template, s, cdata->idx);
150 	if (done) {
151 		free(cdata->template);
152 		cdata->template = new_template;
153 	}
154 
155 	/*
156 	 * Check if there are more prompts; if so, get its respective input
157 	 * and update the prompt data.
158 	 */
159 	if (done && (ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
160 		xasprintf(&prompt, "%s ", ptr);
161 		input = strsep(&cdata->next_input, ",");
162 		status_prompt_update(c, prompt, input);
163 
164 		free(prompt);
165 		cdata->idx++;
166 		return (1);
167 	}
168 
169 	status = cmd_parse_and_append(new_template, NULL, c, NULL, &error);
170 	if (status == CMD_PARSE_ERROR) {
171 		cmdq_append(c, cmdq_get_error(error));
172 		free(error);
173 	}
174 
175 	if (!done)
176 		free(new_template);
177 	if (c->prompt_inputcb != cmd_command_prompt_callback)
178 		return (1);
179 	return (0);
180 }
181 
182 static void
183 cmd_command_prompt_free(void *data)
184 {
185 	struct cmd_command_prompt_cdata	*cdata = data;
186 
187 	free(cdata->inputs);
188 	free(cdata->prompts);
189 	free(cdata->template);
190 	free(cdata);
191 }
192