[BACK]Return to cmd-command-prompt.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-command-prompt.c, Revision 1.27

1.27    ! nicm        1: /* $OpenBSD: cmd-command-prompt.c,v 1.26 2013/03/24 09:54: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>
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.14      nicm       32: void   cmd_command_prompt_key_binding(struct cmd *, int);
1.26      nicm       33: enum cmd_retval        cmd_command_prompt_exec(struct cmd *, struct cmd_q *);
1.8       nicm       34:
1.14      nicm       35: int    cmd_command_prompt_callback(void *, const char *);
                     36: void   cmd_command_prompt_free(void *);
1.1       nicm       37:
                     38: const struct cmd_entry cmd_command_prompt_entry = {
                     39:        "command-prompt", NULL,
1.17      nicm       40:        "I:p:t:", 0, 1,
                     41:        "[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " [template]",
1.14      nicm       42:        0,
                     43:        cmd_command_prompt_key_binding,
                     44:        cmd_command_prompt_exec
1.8       nicm       45: };
                     46:
                     47: struct cmd_command_prompt_cdata {
1.1       nicm       48:        struct client   *c;
1.17      nicm       49:        char            *inputs;
                     50:        char            *next_input;
1.8       nicm       51:        char            *next_prompt;
                     52:        char            *prompts;
1.1       nicm       53:        char            *template;
1.8       nicm       54:        int              idx;
1.1       nicm       55: };
                     56:
                     57: void
1.14      nicm       58: cmd_command_prompt_key_binding(struct cmd *self, int key)
1.1       nicm       59: {
                     60:        switch (key) {
1.18      nicm       61:        case '$':
                     62:                self->args = args_create(1, "rename-session '%%'");
                     63:                args_set(self->args, 'I', "#S");
                     64:                break;
1.1       nicm       65:        case ',':
1.14      nicm       66:                self->args = args_create(1, "rename-window '%%'");
1.18      nicm       67:                args_set(self->args, 'I', "#W");
1.1       nicm       68:                break;
                     69:        case '.':
1.14      nicm       70:                self->args = args_create(1, "move-window -t '%%'");
1.1       nicm       71:                break;
                     72:        case 'f':
1.14      nicm       73:                self->args = args_create(1, "find-window '%%'");
1.13      nicm       74:                break;
                     75:        case '\'':
1.14      nicm       76:                self->args = args_create(1, "select-window -t ':%%'");
                     77:                args_set(self->args, 'p', "index");
                     78:                break;
                     79:        default:
                     80:                self->args = args_create(0);
1.1       nicm       81:                break;
                     82:        }
                     83: }
                     84:
1.21      nicm       85: enum cmd_retval
1.26      nicm       86: cmd_command_prompt_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       87: {
1.14      nicm       88:        struct args                     *args = self->args;
1.17      nicm       89:        const char                      *inputs, *prompts;
1.8       nicm       90:        struct cmd_command_prompt_cdata *cdata;
1.1       nicm       91:        struct client                   *c;
1.19      nicm       92:        char                            *prompt, *ptr, *input = NULL;
1.8       nicm       93:        size_t                           n;
1.1       nicm       94:
1.26      nicm       95:        if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL)
1.21      nicm       96:                return (CMD_RETURN_ERROR);
1.1       nicm       97:
                     98:        if (c->prompt_string != NULL)
1.21      nicm       99:                return (CMD_RETURN_NORMAL);
1.1       nicm      100:
                    101:        cdata = xmalloc(sizeof *cdata);
                    102:        cdata->c = c;
1.8       nicm      103:        cdata->idx = 1;
1.17      nicm      104:        cdata->inputs = NULL;
                    105:        cdata->next_input = NULL;
1.8       nicm      106:        cdata->next_prompt = NULL;
                    107:        cdata->prompts = NULL;
                    108:        cdata->template = NULL;
                    109:
1.14      nicm      110:        if (args->argc != 0)
                    111:                cdata->template = xstrdup(args->argv[0]);
1.8       nicm      112:        else
                    113:                cdata->template = xstrdup("%1");
1.14      nicm      114:
1.17      nicm      115:        if ((prompts = args_get(args, 'p')) != NULL)
1.14      nicm      116:                cdata->prompts = xstrdup(prompts);
                    117:        else if (args->argc != 0) {
                    118:                n = strcspn(cdata->template, " ,");
                    119:                xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
1.8       nicm      120:        } else
                    121:                cdata->prompts = xstrdup(":");
                    122:
1.17      nicm      123:        /* Get first prompt. */
1.8       nicm      124:        cdata->next_prompt = cdata->prompts;
                    125:        ptr = strsep(&cdata->next_prompt, ",");
1.14      nicm      126:        if (prompts == NULL)
1.8       nicm      127:                prompt = xstrdup(ptr);
1.19      nicm      128:        else
                    129:                xasprintf(&prompt, "%s ", ptr);
1.17      nicm      130:
                    131:        /* Get initial prompt input. */
                    132:        if ((inputs = args_get(args, 'I')) != NULL) {
                    133:                cdata->inputs = xstrdup(inputs);
                    134:                cdata->next_input = cdata->inputs;
1.19      nicm      135:                input = strsep(&cdata->next_input, ",");
1.17      nicm      136:        }
                    137:
                    138:        status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
1.14      nicm      139:            cmd_command_prompt_free, cdata, 0);
1.20      nicm      140:        free(prompt);
1.1       nicm      141:
1.21      nicm      142:        return (CMD_RETURN_NORMAL);
1.1       nicm      143: }
                    144:
                    145: int
                    146: cmd_command_prompt_callback(void *data, const char *s)
                    147: {
1.8       nicm      148:        struct cmd_command_prompt_cdata *cdata = data;
1.1       nicm      149:        struct client                   *c = cdata->c;
                    150:        struct cmd_list                 *cmdlist;
1.19      nicm      151:        char                            *cause, *new_template, *prompt, *ptr;
                    152:        char                            *input = NULL;
1.1       nicm      153:
1.8       nicm      154:        if (s == NULL)
1.1       nicm      155:                return (0);
                    156:
1.17      nicm      157:        new_template = cmd_template_replace(cdata->template, s, cdata->idx);
1.20      nicm      158:        free(cdata->template);
1.17      nicm      159:        cdata->template = new_template;
1.8       nicm      160:
1.17      nicm      161:        /*
                    162:         * Check if there are more prompts; if so, get its respective input
                    163:         * and update the prompt data.
                    164:         */
1.8       nicm      165:        if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
1.19      nicm      166:                xasprintf(&prompt, "%s ", ptr);
                    167:                input = strsep(&cdata->next_input, ",");
1.17      nicm      168:                status_prompt_update(c, prompt, input);
                    169:
1.20      nicm      170:                free(prompt);
1.8       nicm      171:                cdata->idx++;
                    172:                return (1);
                    173:        }
1.1       nicm      174:
1.26      nicm      175:        if (cmd_string_parse(new_template, &cmdlist, NULL, 0, &cause) != 0) {
1.8       nicm      176:                if (cause != NULL) {
                    177:                        *cause = toupper((u_char) *cause);
                    178:                        status_message_set(c, "%s", cause);
1.20      nicm      179:                        free(cause);
1.1       nicm      180:                }
1.8       nicm      181:                return (0);
1.1       nicm      182:        }
                    183:
1.26      nicm      184:        cmdq_run(c->cmdq, cmdlist);
1.1       nicm      185:        cmd_list_free(cmdlist);
                    186:
1.4       nicm      187:        if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback)
1.1       nicm      188:                return (1);
                    189:        return (0);
1.4       nicm      190: }
                    191:
                    192: void
1.14      nicm      193: cmd_command_prompt_free(void *data)
1.4       nicm      194: {
1.8       nicm      195:        struct cmd_command_prompt_cdata *cdata = data;
1.4       nicm      196:
1.20      nicm      197:        free(cdata->inputs);
                    198:        free(cdata->prompts);
                    199:        free(cdata->template);
                    200:        free(cdata);
1.1       nicm      201: }