Instead of eating 1024 bytes or so for the arguments of each command,
[tmux-openbsd.git] / cmd-command-prompt.c
blob3b773316dbdb2645320665438482a5c362f8922e
1 /* $OpenBSD$ */
3 /*
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>
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 void cmd_command_prompt_key_binding(struct cmd *, int);
33 int cmd_command_prompt_check(struct args *);
34 enum cmd_retval cmd_command_prompt_exec(struct cmd *, struct cmd_q *);
36 int cmd_command_prompt_callback(void *, const char *);
37 void cmd_command_prompt_free(void *);
39 const struct cmd_entry cmd_command_prompt_entry = {
40 "command-prompt", NULL,
41 "I:p:t:", 0, 1,
42 "[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " [template]",
44 cmd_command_prompt_key_binding,
45 NULL,
46 cmd_command_prompt_exec
49 struct cmd_command_prompt_cdata {
50 struct client *c;
51 char *inputs;
52 char *next_input;
53 char *next_prompt;
54 char *prompts;
55 char *template;
56 int idx;
59 void
60 cmd_command_prompt_key_binding(struct cmd *self, int key)
62 switch (key) {
63 case '$':
64 self->args = args_create(1, "rename-session '%%'");
65 args_set(self->args, 'I', "#S");
66 break;
67 case ',':
68 self->args = args_create(1, "rename-window '%%'");
69 args_set(self->args, 'I', "#W");
70 break;
71 case '.':
72 self->args = args_create(1, "move-window -t '%%'");
73 break;
74 case 'f':
75 self->args = args_create(1, "find-window '%%'");
76 break;
77 case '\'':
78 self->args = args_create(1, "select-window -t ':%%'");
79 args_set(self->args, 'p', "index");
80 break;
81 default:
82 self->args = args_create(0);
83 break;
87 enum cmd_retval
88 cmd_command_prompt_exec(struct cmd *self, struct cmd_q *cmdq)
90 struct args *args = self->args;
91 const char *inputs, *prompts;
92 struct cmd_command_prompt_cdata *cdata;
93 struct client *c;
94 char *prompt, *ptr, *input = NULL;
95 size_t n;
97 if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL)
98 return (CMD_RETURN_ERROR);
100 if (c->prompt_string != NULL)
101 return (CMD_RETURN_NORMAL);
103 cdata = xmalloc(sizeof *cdata);
104 cdata->c = c;
105 cdata->idx = 1;
106 cdata->inputs = NULL;
107 cdata->next_input = NULL;
108 cdata->next_prompt = NULL;
109 cdata->prompts = NULL;
110 cdata->template = NULL;
112 if (args->argc != 0)
113 cdata->template = xstrdup(args->argv[0]);
114 else
115 cdata->template = xstrdup("%1");
117 if ((prompts = args_get(args, 'p')) != NULL)
118 cdata->prompts = xstrdup(prompts);
119 else if (args->argc != 0) {
120 n = strcspn(cdata->template, " ,");
121 xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
122 } else
123 cdata->prompts = xstrdup(":");
125 /* Get first prompt. */
126 cdata->next_prompt = cdata->prompts;
127 ptr = strsep(&cdata->next_prompt, ",");
128 if (prompts == NULL)
129 prompt = xstrdup(ptr);
130 else
131 xasprintf(&prompt, "%s ", ptr);
133 /* Get initial prompt input. */
134 if ((inputs = args_get(args, 'I')) != NULL) {
135 cdata->inputs = xstrdup(inputs);
136 cdata->next_input = cdata->inputs;
137 input = strsep(&cdata->next_input, ",");
140 status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
141 cmd_command_prompt_free, cdata, 0);
142 free(prompt);
144 return (CMD_RETURN_NORMAL);
148 cmd_command_prompt_callback(void *data, const char *s)
150 struct cmd_command_prompt_cdata *cdata = data;
151 struct client *c = cdata->c;
152 struct cmd_list *cmdlist;
153 char *cause, *new_template, *prompt, *ptr;
154 char *input = NULL;
156 if (s == NULL)
157 return (0);
159 new_template = cmd_template_replace(cdata->template, s, cdata->idx);
160 free(cdata->template);
161 cdata->template = new_template;
164 * Check if there are more prompts; if so, get its respective input
165 * and update the prompt data.
167 if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
168 xasprintf(&prompt, "%s ", ptr);
169 input = strsep(&cdata->next_input, ",");
170 status_prompt_update(c, prompt, input);
172 free(prompt);
173 cdata->idx++;
174 return (1);
177 if (cmd_string_parse(new_template, &cmdlist, NULL, 0, &cause) != 0) {
178 if (cause != NULL) {
179 *cause = toupper((u_char) *cause);
180 status_message_set(c, "%s", cause);
181 free(cause);
183 return (0);
186 cmdq_run(c->cmdq, cmdlist);
187 cmd_list_free(cmdlist);
189 if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback)
190 return (1);
191 return (0);
194 void
195 cmd_command_prompt_free(void *data)
197 struct cmd_command_prompt_cdata *cdata = data;
199 free(cdata->inputs);
200 free(cdata->prompts);
201 free(cdata->template);
202 free(cdata);