1 |
|
|
/* $OpenBSD: cmd-command-prompt.c,v 1.33 2016/01/19 15:59:12 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 |
|
|
enum cmd_retval cmd_command_prompt_exec(struct cmd *, struct cmd_q *); |
33 |
|
|
|
34 |
|
|
int cmd_command_prompt_callback(void *, const char *); |
35 |
|
|
void cmd_command_prompt_free(void *); |
36 |
|
|
|
37 |
|
|
const struct cmd_entry cmd_command_prompt_entry = { |
38 |
|
|
.name = "command-prompt", |
39 |
|
|
.alias = NULL, |
40 |
|
|
|
41 |
|
|
.args = { "I:p:t:", 0, 1 }, |
42 |
|
|
.usage = "[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " " |
43 |
|
|
"[template]", |
44 |
|
|
|
45 |
|
|
.tflag = CMD_CLIENT, |
46 |
|
|
|
47 |
|
|
.flags = 0, |
48 |
|
|
.exec = cmd_command_prompt_exec |
49 |
|
|
}; |
50 |
|
|
|
51 |
|
|
struct cmd_command_prompt_cdata { |
52 |
|
|
struct client *c; |
53 |
|
|
char *inputs; |
54 |
|
|
char *next_input; |
55 |
|
|
char *next_prompt; |
56 |
|
|
char *prompts; |
57 |
|
|
char *template; |
58 |
|
|
int idx; |
59 |
|
|
}; |
60 |
|
|
|
61 |
|
|
enum cmd_retval |
62 |
|
|
cmd_command_prompt_exec(struct cmd *self, struct cmd_q *cmdq) |
63 |
|
|
{ |
64 |
|
|
struct args *args = self->args; |
65 |
|
|
const char *inputs, *prompts; |
66 |
|
|
struct cmd_command_prompt_cdata *cdata; |
67 |
|
|
struct client *c = cmdq->state.c; |
68 |
|
|
char *prompt, *ptr, *input = NULL; |
69 |
|
|
size_t n; |
70 |
|
|
|
71 |
|
|
if (c->prompt_string != NULL) |
72 |
|
|
return (CMD_RETURN_NORMAL); |
73 |
|
|
|
74 |
|
|
cdata = xmalloc(sizeof *cdata); |
75 |
|
|
cdata->c = c; |
76 |
|
|
cdata->idx = 1; |
77 |
|
|
cdata->inputs = NULL; |
78 |
|
|
cdata->next_input = NULL; |
79 |
|
|
cdata->next_prompt = NULL; |
80 |
|
|
cdata->prompts = NULL; |
81 |
|
|
cdata->template = NULL; |
82 |
|
|
|
83 |
|
|
if (args->argc != 0) |
84 |
|
|
cdata->template = xstrdup(args->argv[0]); |
85 |
|
|
else |
86 |
|
|
cdata->template = xstrdup("%1"); |
87 |
|
|
|
88 |
|
|
if ((prompts = args_get(args, 'p')) != NULL) |
89 |
|
|
cdata->prompts = xstrdup(prompts); |
90 |
|
|
else if (args->argc != 0) { |
91 |
|
|
n = strcspn(cdata->template, " ,"); |
92 |
|
|
xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template); |
93 |
|
|
} else |
94 |
|
|
cdata->prompts = xstrdup(":"); |
95 |
|
|
|
96 |
|
|
/* Get first prompt. */ |
97 |
|
|
cdata->next_prompt = cdata->prompts; |
98 |
|
|
ptr = strsep(&cdata->next_prompt, ","); |
99 |
|
|
if (prompts == NULL) |
100 |
|
|
prompt = xstrdup(ptr); |
101 |
|
|
else |
102 |
|
|
xasprintf(&prompt, "%s ", ptr); |
103 |
|
|
|
104 |
|
|
/* Get initial prompt input. */ |
105 |
|
|
if ((inputs = args_get(args, 'I')) != NULL) { |
106 |
|
|
cdata->inputs = xstrdup(inputs); |
107 |
|
|
cdata->next_input = cdata->inputs; |
108 |
|
|
input = strsep(&cdata->next_input, ","); |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
status_prompt_set(c, prompt, input, cmd_command_prompt_callback, |
112 |
|
|
cmd_command_prompt_free, cdata, 0); |
113 |
|
|
free(prompt); |
114 |
|
|
|
115 |
|
|
return (CMD_RETURN_NORMAL); |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
int |
119 |
|
|
cmd_command_prompt_callback(void *data, const char *s) |
120 |
|
|
{ |
121 |
|
|
struct cmd_command_prompt_cdata *cdata = data; |
122 |
|
|
struct client *c = cdata->c; |
123 |
|
|
struct cmd_list *cmdlist; |
124 |
|
|
char *cause, *new_template, *prompt, *ptr; |
125 |
|
|
char *input = NULL; |
126 |
|
|
|
127 |
|
|
if (s == NULL) |
128 |
|
|
return (0); |
129 |
|
|
|
130 |
|
|
new_template = cmd_template_replace(cdata->template, s, cdata->idx); |
131 |
|
|
free(cdata->template); |
132 |
|
|
cdata->template = new_template; |
133 |
|
|
|
134 |
|
|
/* |
135 |
|
|
* Check if there are more prompts; if so, get its respective input |
136 |
|
|
* and update the prompt data. |
137 |
|
|
*/ |
138 |
|
|
if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) { |
139 |
|
|
xasprintf(&prompt, "%s ", ptr); |
140 |
|
|
input = strsep(&cdata->next_input, ","); |
141 |
|
|
status_prompt_update(c, prompt, input); |
142 |
|
|
|
143 |
|
|
free(prompt); |
144 |
|
|
cdata->idx++; |
145 |
|
|
return (1); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
if (cmd_string_parse(new_template, &cmdlist, NULL, 0, &cause) != 0) { |
149 |
|
|
if (cause != NULL) { |
150 |
|
|
*cause = toupper((u_char) *cause); |
151 |
|
|
status_message_set(c, "%s", cause); |
152 |
|
|
free(cause); |
153 |
|
|
} |
154 |
|
|
return (0); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
cmdq_run(c->cmdq, cmdlist, NULL); |
158 |
|
|
cmd_list_free(cmdlist); |
159 |
|
|
|
160 |
|
|
if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback) |
161 |
|
|
return (1); |
162 |
|
|
return (0); |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
void |
166 |
|
|
cmd_command_prompt_free(void *data) |
167 |
|
|
{ |
168 |
|
|
struct cmd_command_prompt_cdata *cdata = data; |
169 |
|
|
|
170 |
|
|
free(cdata->inputs); |
171 |
|
|
free(cdata->prompts); |
172 |
|
|
free(cdata->template); |
173 |
|
|
free(cdata); |
174 |
|
|
} |