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