4 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
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>
27 * Prompt for command in client.
30 void cmd_command_prompt_init(struct cmd
*, int);
31 int cmd_command_prompt_parse(struct cmd
*, int, char **, char **);
32 int cmd_command_prompt_exec(struct cmd
*, struct cmd_ctx
*);
33 void cmd_command_prompt_free(struct cmd
*);
34 size_t cmd_command_prompt_print(struct cmd
*, char *, size_t);
36 int cmd_command_prompt_callback(void *, const char *);
37 void cmd_command_prompt_cfree(void *);
39 const struct cmd_entry cmd_command_prompt_entry
= {
40 "command-prompt", NULL
,
41 CMD_TARGET_CLIENT_USAGE
" [-p prompts] [template]",
43 cmd_command_prompt_init
,
44 cmd_command_prompt_parse
,
45 cmd_command_prompt_exec
,
46 cmd_command_prompt_free
,
47 cmd_command_prompt_print
50 struct cmd_command_prompt_data
{
56 struct cmd_command_prompt_cdata
{
65 cmd_command_prompt_init(struct cmd
*self
, int key
)
67 struct cmd_command_prompt_data
*data
;
69 self
->data
= data
= xmalloc(sizeof *data
);
72 data
->template = NULL
;
76 data
->template = xstrdup("rename-window '%%'");
79 data
->template = xstrdup("move-window -t '%%'");
82 data
->template = xstrdup("find-window '%%'");
85 data
->template = xstrdup("select-window -t ':%%'");
86 data
->prompts
= xstrdup("index");
92 cmd_command_prompt_parse(struct cmd
*self
, int argc
, char **argv
, char **cause
)
94 struct cmd_command_prompt_data
*data
;
97 self
->entry
->init(self
, KEYC_NONE
);
100 while ((opt
= getopt(argc
, argv
, "p:t:")) != -1) {
103 if (data
->prompts
== NULL
)
104 data
->prompts
= xstrdup(optarg
);
107 if (data
->target
== NULL
)
108 data
->target
= xstrdup(optarg
);
116 if (argc
!= 0 && argc
!= 1)
120 data
->template = xstrdup(argv
[0]);
125 xasprintf(cause
, "usage: %s %s", self
->entry
->name
, self
->entry
->usage
);
127 self
->entry
->free(self
);
132 cmd_command_prompt_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
134 struct cmd_command_prompt_data
*data
= self
->data
;
135 struct cmd_command_prompt_cdata
*cdata
;
140 if ((c
= cmd_find_client(ctx
, data
->target
)) == NULL
)
143 if (c
->prompt_string
!= NULL
)
146 cdata
= xmalloc(sizeof *cdata
);
149 cdata
->next_prompt
= NULL
;
150 cdata
->prompts
= NULL
;
151 cdata
->template = NULL
;
153 if (data
->template != NULL
)
154 cdata
->template = xstrdup(data
->template);
156 cdata
->template = xstrdup("%1");
157 if (data
->prompts
!= NULL
)
158 cdata
->prompts
= xstrdup(data
->prompts
);
159 else if (data
->template != NULL
) {
160 n
= strcspn(data
->template, " ,");
161 xasprintf(&cdata
->prompts
, "(%.*s) ", (int) n
, data
->template);
163 cdata
->prompts
= xstrdup(":");
165 cdata
->next_prompt
= cdata
->prompts
;
166 ptr
= strsep(&cdata
->next_prompt
, ",");
167 if (data
->prompts
== NULL
)
168 prompt
= xstrdup(ptr
);
170 xasprintf(&prompt
, "%s ", ptr
);
171 status_prompt_set(c
, prompt
, cmd_command_prompt_callback
,
172 cmd_command_prompt_cfree
, cdata
, 0);
179 cmd_command_prompt_free(struct cmd
*self
)
181 struct cmd_command_prompt_data
*data
= self
->data
;
183 if (data
->prompts
!= NULL
)
184 xfree(data
->prompts
);
185 if (data
->target
!= NULL
)
187 if (data
->template != NULL
)
188 xfree(data
->template);
193 cmd_command_prompt_print(struct cmd
*self
, char *buf
, size_t len
)
195 struct cmd_command_prompt_data
*data
= self
->data
;
198 off
+= xsnprintf(buf
, len
, "%s", self
->entry
->name
);
201 if (off
< len
&& data
->prompts
!= NULL
)
202 off
+= cmd_prarg(buf
+ off
, len
- off
, " -p ", data
->prompts
);
203 if (off
< len
&& data
->target
!= NULL
)
204 off
+= cmd_prarg(buf
+ off
, len
- off
, " -t ", data
->target
);
205 if (off
< len
&& data
->template != NULL
)
206 off
+= cmd_prarg(buf
+ off
, len
- off
, " ", data
->template);
211 cmd_command_prompt_callback(void *data
, const char *s
)
213 struct cmd_command_prompt_cdata
*cdata
= data
;
214 struct client
*c
= cdata
->c
;
215 struct cmd_list
*cmdlist
;
217 char *cause
, *newtempl
, *prompt
, *ptr
;
222 newtempl
= cmd_template_replace(cdata
->template, s
, cdata
->idx
);
223 xfree(cdata
->template);
224 cdata
->template = newtempl
;
226 if ((ptr
= strsep(&cdata
->next_prompt
, ",")) != NULL
) {
227 xasprintf(&prompt
, "%s ", ptr
);
228 status_prompt_update(c
, prompt
);
234 if (cmd_string_parse(newtempl
, &cmdlist
, &cause
) != 0) {
236 *cause
= toupper((u_char
) *cause
);
237 status_message_set(c
, "%s", cause
);
246 ctx
.error
= key_bindings_error
;
247 ctx
.print
= key_bindings_print
;
248 ctx
.info
= key_bindings_info
;
250 ctx
.cmdclient
= NULL
;
252 cmd_list_exec(cmdlist
, &ctx
);
253 cmd_list_free(cmdlist
);
255 if (c
->prompt_callbackfn
!= (void *) &cmd_command_prompt_callback
)
261 cmd_command_prompt_cfree(void *data
)
263 struct cmd_command_prompt_cdata
*cdata
= data
;
265 if (cdata
->prompts
!= NULL
)
266 xfree(cdata
->prompts
);
267 if (cdata
->template != NULL
)
268 xfree(cdata
->template);