Fix a few memory leaks.
[tmux-openbsd.git] / cmd-command-prompt.c
blobf80760ce20807e6aa7e8fd85aa41f25138746052
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
26 #include "tmux.h"
29 * Prompt for command in client.
32 static enum cmd_retval cmd_command_prompt_exec(struct cmd *,
33 struct cmdq_item *);
35 static int cmd_command_prompt_callback(struct client *, void *,
36 const char *, int);
37 static void cmd_command_prompt_free(void *);
39 const struct cmd_entry cmd_command_prompt_entry = {
40 .name = "command-prompt",
41 .alias = NULL,
43 .args = { "1bFkiI:Np:t:T:", 0, 1, NULL },
44 .usage = "[-1bFkiN] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE
45 " [-T type] [template]",
47 .flags = CMD_CLIENT_TFLAG,
48 .exec = cmd_command_prompt_exec
51 struct cmd_command_prompt_cdata {
52 struct cmdq_item *item;
53 struct cmd_parse_input pi;
55 int flags;
56 enum prompt_type prompt_type;
58 char *inputs;
59 char *next_input;
61 char *prompts;
62 char *next_prompt;
64 char *template;
65 int idx;
68 static enum cmd_retval
69 cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item)
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, *s;
75 struct cmd_command_prompt_cdata *cdata;
76 char *prompt, *comma, *input = NULL;
77 u_int count = args_count(args);
78 size_t n;
79 int wait = !args_has(args, 'b');
81 if (tc->prompt_string != NULL)
82 return (CMD_RETURN_NORMAL);
83 if (args_has(args, 'i'))
84 wait = 0;
86 cdata = xcalloc(1, sizeof *cdata);
87 cdata->idx = 1;
89 cmd_get_source(self, &cdata->pi.file, &cdata->pi.line);
90 if (wait)
91 cdata->pi.item = item;
92 cdata->pi.c = tc;
93 cmd_find_copy_state(&cdata->pi.fs, target);
95 if (wait)
96 cdata->item = item;
98 if (count != 0) {
99 s = args_string(args, 0);
100 if (args_has(args, 'F'))
101 cdata->template = format_single_from_target(item, s);
102 else
103 cdata->template = xstrdup(s);
104 } else
105 cdata->template = xstrdup("%1");
107 if ((prompts = args_get(args, 'p')) != NULL)
108 cdata->prompts = xstrdup(prompts);
109 else if (count != 0) {
110 n = strcspn(cdata->template, " ,");
111 xasprintf(&cdata->prompts, "(%.*s) ", (int)n, cdata->template);
112 } else
113 cdata->prompts = xstrdup(":");
115 /* Get first prompt. */
116 cdata->next_prompt = cdata->prompts;
117 comma = strsep(&cdata->next_prompt, ",");
118 if (prompts == NULL)
119 prompt = xstrdup(comma);
120 else
121 xasprintf(&prompt, "%s ", comma);
123 /* Get initial prompt input. */
124 if ((inputs = args_get(args, 'I')) != NULL) {
125 cdata->inputs = xstrdup(inputs);
126 cdata->next_input = cdata->inputs;
127 input = strsep(&cdata->next_input, ",");
130 /* Get prompt type. */
131 if ((type = args_get(args, 'T')) != NULL) {
132 cdata->prompt_type = status_prompt_type(type);
133 if (cdata->prompt_type == PROMPT_TYPE_INVALID) {
134 cmdq_error(item, "unknown type: %s", type);
135 return (CMD_RETURN_ERROR);
137 } else
138 cdata->prompt_type = PROMPT_TYPE_COMMAND;
140 if (args_has(args, '1'))
141 cdata->flags |= PROMPT_SINGLE;
142 else if (args_has(args, 'N'))
143 cdata->flags |= PROMPT_NUMERIC;
144 else if (args_has(args, 'i'))
145 cdata->flags |= PROMPT_INCREMENTAL;
146 else if (args_has(args, 'k'))
147 cdata->flags |= PROMPT_KEY;
148 status_prompt_set(tc, target, prompt, input,
149 cmd_command_prompt_callback, cmd_command_prompt_free, cdata,
150 cdata->flags, cdata->prompt_type);
151 free(prompt);
153 if (!wait)
154 return (CMD_RETURN_NORMAL);
155 return (CMD_RETURN_WAIT);
158 static int
159 cmd_command_prompt_callback(struct client *c, void *data, const char *s,
160 int done)
162 struct cmd_command_prompt_cdata *cdata = data;
163 char *new_template, *prompt, *comma, *error;
164 char *input = NULL;
165 struct cmdq_item *item = cdata->item;
166 enum cmd_parse_status status;
168 if (s == NULL)
169 goto out;
170 if (done && (cdata->flags & PROMPT_INCREMENTAL))
171 goto out;
173 new_template = cmd_template_replace(cdata->template, s, cdata->idx);
174 if (done) {
175 free(cdata->template);
176 cdata->template = new_template;
180 * Check if there are more prompts; if so, get its respective input
181 * and update the prompt data.
183 if (done && (comma = strsep(&cdata->next_prompt, ",")) != NULL) {
184 xasprintf(&prompt, "%s ", comma);
185 input = strsep(&cdata->next_input, ",");
186 status_prompt_update(c, prompt, input);
188 free(prompt);
189 cdata->idx++;
190 return (1);
193 if (item != NULL) {
194 status = cmd_parse_and_insert(new_template, &cdata->pi, item,
195 cmdq_get_state(item), &error);
196 } else {
197 status = cmd_parse_and_append(new_template, &cdata->pi, c, NULL,
198 &error);
200 if (status == CMD_PARSE_ERROR) {
201 cmdq_append(c, cmdq_get_error(error));
202 free(error);
205 if (!done)
206 free(new_template);
207 if (c->prompt_inputcb != cmd_command_prompt_callback)
208 return (1);
210 out:
211 if (item != NULL)
212 cmdq_continue(item);
213 return (0);
216 static void
217 cmd_command_prompt_free(void *data)
219 struct cmd_command_prompt_cdata *cdata = data;
221 free(cdata->inputs);
222 free(cdata->prompts);
223 free(cdata->template);
224 free(cdata);