Annotation of src/usr.bin/tmux/cmd-command-prompt.c, Revision 1.37
1.37 ! nicm 1: /* $OpenBSD: cmd-command-prompt.c,v 1.36 2016/10/12 13:03:27 nicm Exp $ */
1.1 nicm 2:
3: /*
1.33 nicm 4: * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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>
1.20 nicm 22: #include <stdlib.h>
1.1 nicm 23: #include <string.h>
1.17 nicm 24: #include <time.h>
1.1 nicm 25:
26: #include "tmux.h"
27:
28: /*
29: * Prompt for command in client.
30: */
31:
1.34 nicm 32: static enum cmd_retval cmd_command_prompt_exec(struct cmd *, struct cmd_q *);
1.8 nicm 33:
1.34 nicm 34: static int cmd_command_prompt_callback(void *, const char *);
35: static void cmd_command_prompt_free(void *);
1.1 nicm 36:
37: const struct cmd_entry cmd_command_prompt_entry = {
1.31 nicm 38: .name = "command-prompt",
39: .alias = NULL,
40:
1.36 nicm 41: .args = { "1I:Np:t:", 0, 1 },
42: .usage = "[-1N] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " "
1.31 nicm 43: "[template]",
44:
1.32 nicm 45: .tflag = CMD_CLIENT,
46:
47: .flags = 0,
1.31 nicm 48: .exec = cmd_command_prompt_exec
1.8 nicm 49: };
50:
51: struct cmd_command_prompt_cdata {
1.1 nicm 52: struct client *c;
1.17 nicm 53: char *inputs;
54: char *next_input;
1.8 nicm 55: char *next_prompt;
56: char *prompts;
1.1 nicm 57: char *template;
1.8 nicm 58: int idx;
1.1 nicm 59: };
60:
1.34 nicm 61: static enum cmd_retval
1.26 nicm 62: cmd_command_prompt_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 63: {
1.14 nicm 64: struct args *args = self->args;
1.17 nicm 65: const char *inputs, *prompts;
1.8 nicm 66: struct cmd_command_prompt_cdata *cdata;
1.30 nicm 67: struct client *c = cmdq->state.c;
1.19 nicm 68: char *prompt, *ptr, *input = NULL;
1.8 nicm 69: size_t n;
1.35 nicm 70: int flags;
1.1 nicm 71:
72: if (c->prompt_string != NULL)
1.21 nicm 73: return (CMD_RETURN_NORMAL);
1.1 nicm 74:
75: cdata = xmalloc(sizeof *cdata);
76: cdata->c = c;
1.8 nicm 77: cdata->idx = 1;
1.17 nicm 78: cdata->inputs = NULL;
79: cdata->next_input = NULL;
1.8 nicm 80: cdata->next_prompt = NULL;
81: cdata->prompts = NULL;
82: cdata->template = NULL;
83:
1.14 nicm 84: if (args->argc != 0)
85: cdata->template = xstrdup(args->argv[0]);
1.8 nicm 86: else
87: cdata->template = xstrdup("%1");
1.14 nicm 88:
1.17 nicm 89: if ((prompts = args_get(args, 'p')) != NULL)
1.14 nicm 90: cdata->prompts = xstrdup(prompts);
91: else if (args->argc != 0) {
92: n = strcspn(cdata->template, " ,");
93: xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
1.8 nicm 94: } else
95: cdata->prompts = xstrdup(":");
96:
1.17 nicm 97: /* Get first prompt. */
1.8 nicm 98: cdata->next_prompt = cdata->prompts;
99: ptr = strsep(&cdata->next_prompt, ",");
1.14 nicm 100: if (prompts == NULL)
1.8 nicm 101: prompt = xstrdup(ptr);
1.19 nicm 102: else
103: xasprintf(&prompt, "%s ", ptr);
1.17 nicm 104:
105: /* Get initial prompt input. */
106: if ((inputs = args_get(args, 'I')) != NULL) {
107: cdata->inputs = xstrdup(inputs);
108: cdata->next_input = cdata->inputs;
1.19 nicm 109: input = strsep(&cdata->next_input, ",");
1.17 nicm 110: }
111:
1.35 nicm 112: flags = 0;
113: if (args_has(args, '1'))
114: flags |= PROMPT_SINGLE;
1.36 nicm 115: else if (args_has(args, 'N'))
116: flags |= PROMPT_NUMERIC;
1.17 nicm 117: status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
1.35 nicm 118: cmd_command_prompt_free, cdata, flags);
1.20 nicm 119: free(prompt);
1.1 nicm 120:
1.21 nicm 121: return (CMD_RETURN_NORMAL);
1.1 nicm 122: }
123:
1.37 ! nicm 124: static enum cmd_retval
! 125: cmd_command_prompt_error(struct cmd_q *cmdq, void *data)
! 126: {
! 127: char *error = data;
! 128:
! 129: cmdq_error(cmdq, "%s", error);
! 130: free(error);
! 131:
! 132: return (CMD_RETURN_NORMAL);
! 133: }
! 134:
1.34 nicm 135: static int
1.1 nicm 136: cmd_command_prompt_callback(void *data, const char *s)
137: {
1.8 nicm 138: struct cmd_command_prompt_cdata *cdata = data;
1.1 nicm 139: struct client *c = cdata->c;
140: struct cmd_list *cmdlist;
1.37 ! nicm 141: struct cmd_q *new_cmdq;
1.19 nicm 142: char *cause, *new_template, *prompt, *ptr;
143: char *input = NULL;
1.1 nicm 144:
1.8 nicm 145: if (s == NULL)
1.1 nicm 146: return (0);
147:
1.17 nicm 148: new_template = cmd_template_replace(cdata->template, s, cdata->idx);
1.20 nicm 149: free(cdata->template);
1.17 nicm 150: cdata->template = new_template;
1.8 nicm 151:
1.17 nicm 152: /*
153: * Check if there are more prompts; if so, get its respective input
154: * and update the prompt data.
155: */
1.8 nicm 156: if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
1.19 nicm 157: xasprintf(&prompt, "%s ", ptr);
158: input = strsep(&cdata->next_input, ",");
1.17 nicm 159: status_prompt_update(c, prompt, input);
160:
1.20 nicm 161: free(prompt);
1.8 nicm 162: cdata->idx++;
163: return (1);
164: }
1.1 nicm 165:
1.26 nicm 166: if (cmd_string_parse(new_template, &cmdlist, NULL, 0, &cause) != 0) {
1.8 nicm 167: if (cause != NULL) {
1.37 ! nicm 168: new_cmdq = cmdq_get_callback(cmd_command_prompt_error,
! 169: cause);
! 170: } else
! 171: new_cmdq = NULL;
! 172: } else {
! 173: new_cmdq = cmdq_get_command(cmdlist, NULL, NULL, 0);
! 174: cmd_list_free(cmdlist);
1.1 nicm 175: }
176:
1.37 ! nicm 177: if (new_cmdq != NULL)
! 178: cmdq_append(c, new_cmdq);
1.1 nicm 179:
1.37 ! nicm 180: if (c->prompt_callbackfn != (void *)&cmd_command_prompt_callback)
1.1 nicm 181: return (1);
182: return (0);
1.4 nicm 183: }
184:
1.34 nicm 185: static void
1.14 nicm 186: cmd_command_prompt_free(void *data)
1.4 nicm 187: {
1.8 nicm 188: struct cmd_command_prompt_cdata *cdata = data;
1.4 nicm 189:
1.20 nicm 190: free(cdata->inputs);
191: free(cdata->prompts);
192: free(cdata->template);
193: free(cdata);
1.1 nicm 194: }