Annotation of src/usr.bin/tmux/cmd-command-prompt.c, Revision 1.6
1.6 ! nicm 1: /* $OpenBSD: cmd-command-prompt.c,v 1.5 2009/07/20 07:31:10 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>
22: #include <string.h>
23:
24: #include "tmux.h"
25:
26: /*
27: * Prompt for command in client.
28: */
29:
30: void cmd_command_prompt_init(struct cmd *, int);
31: int cmd_command_prompt_exec(struct cmd *, struct cmd_ctx *);
32:
33: int cmd_command_prompt_callback(void *, const char *);
1.4 nicm 34: void cmd_command_prompt_free(void *);
1.1 nicm 35:
36: const struct cmd_entry cmd_command_prompt_entry = {
37: "command-prompt", NULL,
38: CMD_TARGET_CLIENT_USAGE " [template]",
1.2 nicm 39: CMD_ARG01, 0,
1.1 nicm 40: cmd_command_prompt_init,
41: cmd_target_parse,
42: cmd_command_prompt_exec,
43: cmd_target_free,
44: cmd_target_print
45: };
46:
47: struct cmd_command_prompt_data {
48: struct client *c;
49: char *template;
50: };
51:
52: void
53: cmd_command_prompt_init(struct cmd *self, int key)
54: {
55: struct cmd_target_data *data;
56:
57: cmd_target_init(self, key);
58: data = self->data;
59:
60: switch (key) {
61: case ',':
62: data->arg = xstrdup("rename-window '%%'");
63: break;
64: case '.':
65: data->arg = xstrdup("move-window -t '%%'");
66: break;
67: case 'f':
68: data->arg = xstrdup("find-window '%%'");
69: break;
70: }
71: }
72:
73: int
74: cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
75: {
76: struct cmd_target_data *data = self->data;
77: struct cmd_command_prompt_data *cdata;
78: struct client *c;
79: char *hdr, *ptr;
80:
81: if ((c = cmd_find_client(ctx, data->target)) == NULL)
82: return (-1);
83:
84: if (c->prompt_string != NULL)
85: return (0);
86:
87: cdata = xmalloc(sizeof *cdata);
88: cdata->c = c;
89: if (data->arg != NULL) {
90: cdata->template = xstrdup(data->arg);
91: if ((ptr = strchr(data->arg, ' ')) == NULL)
92: ptr = strchr(data->arg, '\0');
93: xasprintf(&hdr, "(%.*s) ", (int) (ptr - data->arg), data->arg);
94: } else {
95: cdata->template = NULL;
96: hdr = xstrdup(":");
97: }
1.4 nicm 98: status_prompt_set(c, hdr,
99: cmd_command_prompt_callback, cmd_command_prompt_free, cdata, 0);
1.1 nicm 100: xfree(hdr);
101:
102: return (0);
103: }
104:
105: int
106: cmd_command_prompt_callback(void *data, const char *s)
107: {
108: struct cmd_command_prompt_data *cdata = data;
109: struct client *c = cdata->c;
110: struct cmd_list *cmdlist;
111: struct cmd_ctx ctx;
112: char *cause, *ptr, *buf, ch;
113: size_t len, slen;
114:
1.4 nicm 115: if (s == NULL)
1.1 nicm 116: return (0);
117: slen = strlen(s);
118:
119: len = 0;
120: buf = NULL;
121: if (cdata->template != NULL) {
122: ptr = cdata->template;
123: while (*ptr != '\0') {
124: switch (ch = *ptr++) {
125: case '%':
126: if (*ptr != '%')
127: break;
128: ptr++;
129:
130: buf = xrealloc(buf, 1, len + slen + 1);
131: memcpy(buf + len, s, slen);
132: len += slen;
133: break;
134: default:
135: buf = xrealloc(buf, 1, len + 2);
136: buf[len++] = ch;
137: break;
138: }
139: }
140:
1.5 nicm 141: if (buf == NULL)
142: return (0);
1.1 nicm 143: buf[len] = '\0';
144: s = buf;
145: }
146:
147: if (cmd_string_parse(s, &cmdlist, &cause) != 0) {
148: if (cause == NULL)
149: return (0);
150: *cause = toupper((u_char) *cause);
1.3 nicm 151: status_message_set(c, "%s", cause);
1.1 nicm 152: xfree(cause);
153: cmdlist = NULL;
154: }
155: if (buf != NULL)
156: xfree(buf);
157: if (cmdlist == NULL)
158: return (0);
159:
160: ctx.msgdata = NULL;
161: ctx.cursession = c->session;
162: ctx.curclient = c;
163:
164: ctx.error = key_bindings_error;
165: ctx.print = key_bindings_print;
166: ctx.info = key_bindings_info;
167:
168: ctx.cmdclient = NULL;
169:
170: cmd_list_exec(cmdlist, &ctx);
171: cmd_list_free(cmdlist);
172:
1.4 nicm 173: if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback)
1.1 nicm 174: return (1);
175: return (0);
1.4 nicm 176: }
177:
178: void
179: cmd_command_prompt_free(void *data)
180: {
181: struct cmd_command_prompt_data *cdata = data;
182:
183: if (cdata->template != NULL)
184: xfree(cdata->template);
185: xfree(cdata);
1.1 nicm 186: }