Annotation of src/usr.bin/tmux/cmd-command-prompt.c, Revision 1.54
1.54 ! nicm 1: /* $OpenBSD: cmd-command-prompt.c,v 1.53 2020/05/16 16:16:07 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.38 nicm 32: static enum cmd_retval cmd_command_prompt_exec(struct cmd *,
33: struct cmdq_item *);
1.8 nicm 34:
1.44 nicm 35: static int cmd_command_prompt_callback(struct client *, void *,
36: const char *, int);
1.34 nicm 37: static void cmd_command_prompt_free(void *);
1.1 nicm 38:
39: const struct cmd_entry cmd_command_prompt_entry = {
1.31 nicm 40: .name = "command-prompt",
41: .alias = NULL,
42:
1.54 ! nicm 43: .args = { "1kiI:Np:t:T:", 0, 1 },
! 44: .usage = "[-1kiN] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE
! 45: " [-T type] [template]",
1.31 nicm 46:
1.51 nicm 47: .flags = CMD_CLIENT_TFLAG,
1.31 nicm 48: .exec = cmd_command_prompt_exec
1.8 nicm 49: };
50:
51: struct cmd_command_prompt_cdata {
1.54 ! nicm 52: int flags;
! 53: enum prompt_type prompt_type;
1.39 nicm 54:
1.54 ! nicm 55: char *inputs;
! 56: char *next_input;
1.39 nicm 57:
1.54 ! nicm 58: char *prompts;
! 59: char *next_prompt;
1.39 nicm 60:
1.54 ! nicm 61: char *template;
! 62: int idx;
1.1 nicm 63: };
64:
1.34 nicm 65: static enum cmd_retval
1.38 nicm 66: cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 67: {
1.48 nicm 68: struct args *args = cmd_get_args(self);
1.51 nicm 69: struct client *tc = cmdq_get_target_client(item);
1.53 nicm 70: struct cmd_find_state *target = cmdq_get_target(item);
1.54 ! nicm 71: const char *inputs, *prompts, *type;
1.8 nicm 72: struct cmd_command_prompt_cdata *cdata;
1.19 nicm 73: char *prompt, *ptr, *input = NULL;
1.8 nicm 74: size_t n;
1.43 nicm 75:
1.51 nicm 76: if (tc->prompt_string != NULL)
1.21 nicm 77: return (CMD_RETURN_NORMAL);
1.1 nicm 78:
1.39 nicm 79: cdata = xcalloc(1, sizeof *cdata);
80:
1.17 nicm 81: cdata->inputs = NULL;
82: cdata->next_input = NULL;
1.39 nicm 83:
84: cdata->prompts = NULL;
1.8 nicm 85: cdata->next_prompt = NULL;
1.39 nicm 86:
1.8 nicm 87: cdata->template = NULL;
1.39 nicm 88: cdata->idx = 1;
1.8 nicm 89:
1.14 nicm 90: if (args->argc != 0)
91: cdata->template = xstrdup(args->argv[0]);
1.8 nicm 92: else
93: cdata->template = xstrdup("%1");
1.14 nicm 94:
1.17 nicm 95: if ((prompts = args_get(args, 'p')) != NULL)
1.14 nicm 96: cdata->prompts = xstrdup(prompts);
97: else if (args->argc != 0) {
98: n = strcspn(cdata->template, " ,");
99: xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
1.8 nicm 100: } else
101: cdata->prompts = xstrdup(":");
102:
1.17 nicm 103: /* Get first prompt. */
1.8 nicm 104: cdata->next_prompt = cdata->prompts;
105: ptr = strsep(&cdata->next_prompt, ",");
1.14 nicm 106: if (prompts == NULL)
1.8 nicm 107: prompt = xstrdup(ptr);
1.19 nicm 108: else
109: xasprintf(&prompt, "%s ", ptr);
1.17 nicm 110:
111: /* Get initial prompt input. */
112: if ((inputs = args_get(args, 'I')) != NULL) {
113: cdata->inputs = xstrdup(inputs);
114: cdata->next_input = cdata->inputs;
1.19 nicm 115: input = strsep(&cdata->next_input, ",");
1.17 nicm 116: }
117:
1.54 ! nicm 118: /* Get prompt type. */
! 119: if ((type = args_get(args, 'T')) != NULL) {
! 120: cdata->prompt_type = status_prompt_type(type);
! 121: if (cdata->prompt_type == PROMPT_TYPE_INVALID) {
! 122: cmdq_error(item, "unknown type: %s", type);
! 123: return (CMD_RETURN_ERROR);
! 124: }
! 125: } else
! 126: cdata->prompt_type = PROMPT_TYPE_COMMAND;
! 127:
1.35 nicm 128: if (args_has(args, '1'))
1.39 nicm 129: cdata->flags |= PROMPT_SINGLE;
1.36 nicm 130: else if (args_has(args, 'N'))
1.39 nicm 131: cdata->flags |= PROMPT_NUMERIC;
132: else if (args_has(args, 'i'))
133: cdata->flags |= PROMPT_INCREMENTAL;
1.47 nicm 134: else if (args_has(args, 'k'))
135: cdata->flags |= PROMPT_KEY;
1.53 nicm 136: status_prompt_set(tc, target, prompt, input,
137: cmd_command_prompt_callback, cmd_command_prompt_free, cdata,
1.54 ! nicm 138: cdata->flags, cdata->prompt_type);
1.20 nicm 139: free(prompt);
1.1 nicm 140:
1.21 nicm 141: return (CMD_RETURN_NORMAL);
1.1 nicm 142: }
143:
1.34 nicm 144: static int
1.44 nicm 145: cmd_command_prompt_callback(struct client *c, void *data, const char *s,
146: int done)
1.1 nicm 147: {
1.8 nicm 148: struct cmd_command_prompt_cdata *cdata = data;
1.50 nicm 149: char *new_template, *prompt, *ptr, *error;
1.19 nicm 150: char *input = NULL;
1.50 nicm 151: enum cmd_parse_status status;
1.1 nicm 152:
1.8 nicm 153: if (s == NULL)
1.1 nicm 154: return (0);
1.39 nicm 155: if (done && (cdata->flags & PROMPT_INCREMENTAL))
156: return (0);
1.1 nicm 157:
1.17 nicm 158: new_template = cmd_template_replace(cdata->template, s, cdata->idx);
1.39 nicm 159: if (done) {
160: free(cdata->template);
161: cdata->template = new_template;
162: }
1.8 nicm 163:
1.17 nicm 164: /*
165: * Check if there are more prompts; if so, get its respective input
166: * and update the prompt data.
167: */
1.39 nicm 168: if (done && (ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
1.19 nicm 169: xasprintf(&prompt, "%s ", ptr);
170: input = strsep(&cdata->next_input, ",");
1.17 nicm 171: status_prompt_update(c, prompt, input);
172:
1.20 nicm 173: free(prompt);
1.8 nicm 174: cdata->idx++;
175: return (1);
176: }
1.1 nicm 177:
1.50 nicm 178: status = cmd_parse_and_append(new_template, NULL, c, NULL, &error);
179: if (status == CMD_PARSE_ERROR) {
180: cmdq_append(c, cmdq_get_error(error));
181: free(error);
1.1 nicm 182: }
183:
1.39 nicm 184: if (!done)
185: free(new_template);
1.44 nicm 186: if (c->prompt_inputcb != cmd_command_prompt_callback)
1.1 nicm 187: return (1);
188: return (0);
1.4 nicm 189: }
190:
1.34 nicm 191: static void
1.14 nicm 192: cmd_command_prompt_free(void *data)
1.4 nicm 193: {
1.8 nicm 194: struct cmd_command_prompt_cdata *cdata = data;
1.4 nicm 195:
1.20 nicm 196: free(cdata->inputs);
197: free(cdata->prompts);
198: free(cdata->template);
199: free(cdata);
1.1 nicm 200: }