cmd-command-prompt.c revision 1.1.1.6 1 1.1.1.5 christos /* $OpenBSD$ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.1.1.6 christos * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott (at) gmail.com>
5 1.1 jmmv *
6 1.1 jmmv * Permission to use, copy, modify, and distribute this software for any
7 1.1 jmmv * purpose with or without fee is hereby granted, provided that the above
8 1.1 jmmv * copyright notice and this permission notice appear in all copies.
9 1.1 jmmv *
10 1.1 jmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 jmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 jmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 jmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 jmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 1.1 jmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 1.1 jmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 jmmv */
18 1.1 jmmv
19 1.1 jmmv #include <sys/types.h>
20 1.1 jmmv
21 1.1 jmmv #include <ctype.h>
22 1.1.1.3 christos #include <stdlib.h>
23 1.1 jmmv #include <string.h>
24 1.1.1.2 jmmv #include <time.h>
25 1.1 jmmv
26 1.1 jmmv #include "tmux.h"
27 1.1 jmmv
28 1.1 jmmv /*
29 1.1 jmmv * Prompt for command in client.
30 1.1 jmmv */
31 1.1 jmmv
32 1.1.1.3 christos enum cmd_retval cmd_command_prompt_exec(struct cmd *, struct cmd_q *);
33 1.1 jmmv
34 1.1.1.2 jmmv int cmd_command_prompt_callback(void *, const char *);
35 1.1.1.2 jmmv void cmd_command_prompt_free(void *);
36 1.1 jmmv
37 1.1 jmmv const struct cmd_entry cmd_command_prompt_entry = {
38 1.1.1.6 christos .name = "command-prompt",
39 1.1.1.6 christos .alias = NULL,
40 1.1.1.6 christos
41 1.1.1.6 christos .args = { "I:p:t:", 0, 1 },
42 1.1.1.6 christos .usage = "[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " "
43 1.1.1.6 christos "[template]",
44 1.1.1.6 christos
45 1.1.1.6 christos .tflag = CMD_CLIENT,
46 1.1.1.6 christos
47 1.1.1.6 christos .flags = 0,
48 1.1.1.6 christos .exec = cmd_command_prompt_exec
49 1.1 jmmv };
50 1.1 jmmv
51 1.1 jmmv struct cmd_command_prompt_cdata {
52 1.1 jmmv struct client *c;
53 1.1.1.2 jmmv char *inputs;
54 1.1.1.2 jmmv char *next_input;
55 1.1 jmmv char *next_prompt;
56 1.1 jmmv char *prompts;
57 1.1 jmmv char *template;
58 1.1 jmmv int idx;
59 1.1 jmmv };
60 1.1 jmmv
61 1.1.1.3 christos enum cmd_retval
62 1.1.1.3 christos cmd_command_prompt_exec(struct cmd *self, struct cmd_q *cmdq)
63 1.1 jmmv {
64 1.1.1.2 jmmv struct args *args = self->args;
65 1.1.1.2 jmmv const char *inputs, *prompts;
66 1.1 jmmv struct cmd_command_prompt_cdata *cdata;
67 1.1.1.6 christos struct client *c = cmdq->state.c;
68 1.1.1.2 jmmv char *prompt, *ptr, *input = NULL;
69 1.1 jmmv size_t n;
70 1.1 jmmv
71 1.1 jmmv if (c->prompt_string != NULL)
72 1.1.1.3 christos return (CMD_RETURN_NORMAL);
73 1.1 jmmv
74 1.1 jmmv cdata = xmalloc(sizeof *cdata);
75 1.1 jmmv cdata->c = c;
76 1.1 jmmv cdata->idx = 1;
77 1.1.1.2 jmmv cdata->inputs = NULL;
78 1.1.1.2 jmmv cdata->next_input = NULL;
79 1.1 jmmv cdata->next_prompt = NULL;
80 1.1 jmmv cdata->prompts = NULL;
81 1.1 jmmv cdata->template = NULL;
82 1.1 jmmv
83 1.1.1.2 jmmv if (args->argc != 0)
84 1.1.1.2 jmmv cdata->template = xstrdup(args->argv[0]);
85 1.1 jmmv else
86 1.1 jmmv cdata->template = xstrdup("%1");
87 1.1.1.2 jmmv
88 1.1.1.2 jmmv if ((prompts = args_get(args, 'p')) != NULL)
89 1.1.1.2 jmmv cdata->prompts = xstrdup(prompts);
90 1.1.1.2 jmmv else if (args->argc != 0) {
91 1.1.1.2 jmmv n = strcspn(cdata->template, " ,");
92 1.1.1.2 jmmv xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
93 1.1 jmmv } else
94 1.1 jmmv cdata->prompts = xstrdup(":");
95 1.1 jmmv
96 1.1.1.2 jmmv /* Get first prompt. */
97 1.1 jmmv cdata->next_prompt = cdata->prompts;
98 1.1 jmmv ptr = strsep(&cdata->next_prompt, ",");
99 1.1.1.2 jmmv if (prompts == NULL)
100 1.1 jmmv prompt = xstrdup(ptr);
101 1.1 jmmv else
102 1.1 jmmv xasprintf(&prompt, "%s ", ptr);
103 1.1 jmmv
104 1.1.1.2 jmmv /* Get initial prompt input. */
105 1.1.1.2 jmmv if ((inputs = args_get(args, 'I')) != NULL) {
106 1.1.1.2 jmmv cdata->inputs = xstrdup(inputs);
107 1.1.1.2 jmmv cdata->next_input = cdata->inputs;
108 1.1.1.2 jmmv input = strsep(&cdata->next_input, ",");
109 1.1.1.2 jmmv }
110 1.1 jmmv
111 1.1.1.2 jmmv status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
112 1.1.1.2 jmmv cmd_command_prompt_free, cdata, 0);
113 1.1.1.3 christos free(prompt);
114 1.1 jmmv
115 1.1.1.3 christos return (CMD_RETURN_NORMAL);
116 1.1 jmmv }
117 1.1 jmmv
118 1.1 jmmv int
119 1.1 jmmv cmd_command_prompt_callback(void *data, const char *s)
120 1.1 jmmv {
121 1.1 jmmv struct cmd_command_prompt_cdata *cdata = data;
122 1.1 jmmv struct client *c = cdata->c;
123 1.1 jmmv struct cmd_list *cmdlist;
124 1.1.1.2 jmmv char *cause, *new_template, *prompt, *ptr;
125 1.1.1.2 jmmv char *input = NULL;
126 1.1 jmmv
127 1.1 jmmv if (s == NULL)
128 1.1 jmmv return (0);
129 1.1 jmmv
130 1.1.1.2 jmmv new_template = cmd_template_replace(cdata->template, s, cdata->idx);
131 1.1.1.3 christos free(cdata->template);
132 1.1.1.2 jmmv cdata->template = new_template;
133 1.1 jmmv
134 1.1.1.2 jmmv /*
135 1.1.1.2 jmmv * Check if there are more prompts; if so, get its respective input
136 1.1.1.2 jmmv * and update the prompt data.
137 1.1.1.2 jmmv */
138 1.1 jmmv if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
139 1.1 jmmv xasprintf(&prompt, "%s ", ptr);
140 1.1.1.2 jmmv input = strsep(&cdata->next_input, ",");
141 1.1.1.2 jmmv status_prompt_update(c, prompt, input);
142 1.1.1.2 jmmv
143 1.1.1.3 christos free(prompt);
144 1.1 jmmv cdata->idx++;
145 1.1 jmmv return (1);
146 1.1 jmmv }
147 1.1 jmmv
148 1.1.1.3 christos if (cmd_string_parse(new_template, &cmdlist, NULL, 0, &cause) != 0) {
149 1.1 jmmv if (cause != NULL) {
150 1.1 jmmv *cause = toupper((u_char) *cause);
151 1.1 jmmv status_message_set(c, "%s", cause);
152 1.1.1.3 christos free(cause);
153 1.1 jmmv }
154 1.1 jmmv return (0);
155 1.1 jmmv }
156 1.1 jmmv
157 1.1.1.5 christos cmdq_run(c->cmdq, cmdlist, NULL);
158 1.1 jmmv cmd_list_free(cmdlist);
159 1.1 jmmv
160 1.1 jmmv if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback)
161 1.1 jmmv return (1);
162 1.1 jmmv return (0);
163 1.1 jmmv }
164 1.1 jmmv
165 1.1 jmmv void
166 1.1.1.2 jmmv cmd_command_prompt_free(void *data)
167 1.1 jmmv {
168 1.1 jmmv struct cmd_command_prompt_cdata *cdata = data;
169 1.1 jmmv
170 1.1.1.3 christos free(cdata->inputs);
171 1.1.1.3 christos free(cdata->prompts);
172 1.1.1.3 christos free(cdata->template);
173 1.1.1.3 christos free(cdata);
174 1.1 jmmv }
175