Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 216 lines (182 sloc) 5.314 kB
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
1 /* $Id: cmd-command-prompt.c 2553 2011-07-09 09:42:33Z tcunha $ */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
2
3 /*
4 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
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 <string.h>
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
23 #include <time.h>
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
24
25 #include "tmux.h"
26
27 /*
28 * Prompt for command in client.
29 */
30
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
31 void cmd_command_prompt_key_binding(struct cmd *, int);
32 int cmd_command_prompt_check(struct args *);
33 int cmd_command_prompt_exec(struct cmd *, struct cmd_ctx *);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
34
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
35 int cmd_command_prompt_callback(void *, const char *);
36 void cmd_command_prompt_free(void *);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
37
38 const struct cmd_entry cmd_command_prompt_entry = {
39 "command-prompt", NULL,
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
40 "I:p:t:", 0, 1,
41 "[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " [template]",
42 0,
43 cmd_command_prompt_key_binding,
44 NULL,
45 cmd_command_prompt_exec
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
46 };
47
48 struct cmd_command_prompt_cdata {
49 struct client *c;
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
50 char *inputs;
51 char *next_input;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
52 char *next_prompt;
53 char *prompts;
54 char *template;
55 int idx;
56 };
57
58 void
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
59 cmd_command_prompt_key_binding(struct cmd *self, int key)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
60 {
61 switch (key) {
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
62 case '$':
63 self->args = args_create(1, "rename-session '%%'");
64 args_set(self->args, 'I', "#S");
65 break;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
66 case ',':
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
67 self->args = args_create(1, "rename-window '%%'");
68 args_set(self->args, 'I', "#W");
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
69 break;
70 case '.':
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
71 self->args = args_create(1, "move-window -t '%%'");
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
72 break;
73 case 'f':
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
74 self->args = args_create(1, "find-window '%%'");
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
75 break;
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
76 case '\'':
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
77 self->args = args_create(1, "select-window -t ':%%'");
78 args_set(self->args, 'p', "index");
79 break;
80 default:
81 self->args = args_create(0);
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
82 break;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
83 }
84 }
85
86 int
87 cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
88 {
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
89 struct args *args = self->args;
90 const char *inputs, *prompts;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
91 struct cmd_command_prompt_cdata *cdata;
92 struct client *c;
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
93 char *prompt, *ptr, *input = NULL;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
94 size_t n;
95
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
96 if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
97 return (-1);
98
99 if (c->prompt_string != NULL)
100 return (0);
101
102 cdata = xmalloc(sizeof *cdata);
103 cdata->c = c;
104 cdata->idx = 1;
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
105 cdata->inputs = NULL;
106 cdata->next_input = NULL;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
107 cdata->next_prompt = NULL;
108 cdata->prompts = NULL;
109 cdata->template = NULL;
110
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
111 if (args->argc != 0)
112 cdata->template = xstrdup(args->argv[0]);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
113 else
114 cdata->template = xstrdup("%1");
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
115
116 if ((prompts = args_get(args, 'p')) != NULL)
117 cdata->prompts = xstrdup(prompts);
118 else if (args->argc != 0) {
119 n = strcspn(cdata->template, " ,");
120 xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
121 } else
122 cdata->prompts = xstrdup(":");
123
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
124 /* Get first prompt. */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
125 cdata->next_prompt = cdata->prompts;
126 ptr = strsep(&cdata->next_prompt, ",");
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
127 if (prompts == NULL)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
128 prompt = xstrdup(ptr);
129 else
130 xasprintf(&prompt, "%s ", ptr);
131
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
132 /* Get initial prompt input. */
133 if ((inputs = args_get(args, 'I')) != NULL) {
134 cdata->inputs = xstrdup(inputs);
135 cdata->next_input = cdata->inputs;
136 input = strsep(&cdata->next_input, ",");
137 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
138
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
139 status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
140 cmd_command_prompt_free, cdata, 0);
141 xfree(prompt);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
142
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
143 return (0);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
144 }
145
146 int
147 cmd_command_prompt_callback(void *data, const char *s)
148 {
149 struct cmd_command_prompt_cdata *cdata = data;
150 struct client *c = cdata->c;
151 struct cmd_list *cmdlist;
152 struct cmd_ctx ctx;
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
153 char *cause, *new_template, *prompt, *ptr;
154 char *input = NULL;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
155
156 if (s == NULL)
157 return (0);
158
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
159 new_template = cmd_template_replace(cdata->template, s, cdata->idx);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
160 xfree(cdata->template);
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
161 cdata->template = new_template;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
162
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
163 /*
164 * Check if there are more prompts; if so, get its respective input
165 * and update the prompt data.
166 */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
167 if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
168 xasprintf(&prompt, "%s ", ptr);
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
169 input = strsep(&cdata->next_input, ",");
170 status_prompt_update(c, prompt, input);
171
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
172 xfree(prompt);
173 cdata->idx++;
174 return (1);
175 }
176
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
177 if (cmd_string_parse(new_template, &cmdlist, &cause) != 0) {
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
178 if (cause != NULL) {
179 *cause = toupper((u_char) *cause);
180 status_message_set(c, "%s", cause);
181 xfree(cause);
182 }
183 return (0);
184 }
185
186 ctx.msgdata = NULL;
187 ctx.curclient = c;
188
189 ctx.error = key_bindings_error;
190 ctx.print = key_bindings_print;
191 ctx.info = key_bindings_info;
192
193 ctx.cmdclient = NULL;
194
195 cmd_list_exec(cmdlist, &ctx);
196 cmd_list_free(cmdlist);
197
198 if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback)
199 return (1);
200 return (0);
201 }
202
203 void
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
204 cmd_command_prompt_free(void *data)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
205 {
206 struct cmd_command_prompt_cdata *cdata = data;
207
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
208 if (cdata->inputs != NULL)
209 xfree(cdata->inputs);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
210 if (cdata->prompts != NULL)
211 xfree(cdata->prompts);
212 if (cdata->template != NULL)
213 xfree(cdata->template);
214 xfree(cdata);
215 }
Something went wrong with that request. Please try again.