[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.21

1.21    ! nicm        1: /* $OpenBSD: cmd-command-prompt.c,v 1.20 2012/07/10 11:53:01 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);
                     33: int    cmd_command_prompt_check(struct args *);
1.21    ! nicm       34: enum cmd_retval        cmd_command_prompt_exec(struct cmd *, struct cmd_ctx *);
1.8       nicm       35:
1.14      nicm       36: int    cmd_command_prompt_callback(void *, const char *);
                     37: void   cmd_command_prompt_free(void *);
1.1       nicm       38:
                     39: const struct cmd_entry cmd_command_prompt_entry = {
                     40:        "command-prompt", NULL,
1.17      nicm       41:        "I:p:t:", 0, 1,
                     42:        "[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " [template]",
1.14      nicm       43:        0,
                     44:        cmd_command_prompt_key_binding,
                     45:        NULL,
                     46:        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:
                     59: void
1.14      nicm       60: cmd_command_prompt_key_binding(struct cmd *self, int key)
1.1       nicm       61: {
                     62:        switch (key) {
1.18      nicm       63:        case '$':
                     64:                self->args = args_create(1, "rename-session '%%'");
                     65:                args_set(self->args, 'I', "#S");
                     66:                break;
1.1       nicm       67:        case ',':
1.14      nicm       68:                self->args = args_create(1, "rename-window '%%'");
1.18      nicm       69:                args_set(self->args, 'I', "#W");
1.1       nicm       70:                break;
                     71:        case '.':
1.14      nicm       72:                self->args = args_create(1, "move-window -t '%%'");
1.1       nicm       73:                break;
                     74:        case 'f':
1.14      nicm       75:                self->args = args_create(1, "find-window '%%'");
1.13      nicm       76:                break;
                     77:        case '\'':
1.14      nicm       78:                self->args = args_create(1, "select-window -t ':%%'");
                     79:                args_set(self->args, 'p', "index");
                     80:                break;
                     81:        default:
                     82:                self->args = args_create(0);
1.1       nicm       83:                break;
                     84:        }
                     85: }
                     86:
1.21    ! nicm       87: enum cmd_retval
1.1       nicm       88: cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
                     89: {
1.14      nicm       90:        struct args                     *args = self->args;
1.17      nicm       91:        const char                      *inputs, *prompts;
1.8       nicm       92:        struct cmd_command_prompt_cdata *cdata;
1.1       nicm       93:        struct client                   *c;
1.19      nicm       94:        char                            *prompt, *ptr, *input = NULL;
1.8       nicm       95:        size_t                           n;
1.1       nicm       96:
1.14      nicm       97:        if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
1.21    ! nicm       98:                return (CMD_RETURN_ERROR);
1.1       nicm       99:
                    100:        if (c->prompt_string != NULL)
1.21    ! nicm      101:                return (CMD_RETURN_NORMAL);
1.1       nicm      102:
                    103:        cdata = xmalloc(sizeof *cdata);
                    104:        cdata->c = c;
1.8       nicm      105:        cdata->idx = 1;
1.17      nicm      106:        cdata->inputs = NULL;
                    107:        cdata->next_input = NULL;
1.8       nicm      108:        cdata->next_prompt = NULL;
                    109:        cdata->prompts = NULL;
                    110:        cdata->template = NULL;
                    111:
1.14      nicm      112:        if (args->argc != 0)
                    113:                cdata->template = xstrdup(args->argv[0]);
1.8       nicm      114:        else
                    115:                cdata->template = xstrdup("%1");
1.14      nicm      116:
1.17      nicm      117:        if ((prompts = args_get(args, 'p')) != NULL)
1.14      nicm      118:                cdata->prompts = xstrdup(prompts);
                    119:        else if (args->argc != 0) {
                    120:                n = strcspn(cdata->template, " ,");
                    121:                xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
1.8       nicm      122:        } else
                    123:                cdata->prompts = xstrdup(":");
                    124:
1.17      nicm      125:        /* Get first prompt. */
1.8       nicm      126:        cdata->next_prompt = cdata->prompts;
                    127:        ptr = strsep(&cdata->next_prompt, ",");
1.14      nicm      128:        if (prompts == NULL)
1.8       nicm      129:                prompt = xstrdup(ptr);
1.19      nicm      130:        else
                    131:                xasprintf(&prompt, "%s ", ptr);
1.17      nicm      132:
                    133:        /* Get initial prompt input. */
                    134:        if ((inputs = args_get(args, 'I')) != NULL) {
                    135:                cdata->inputs = xstrdup(inputs);
                    136:                cdata->next_input = cdata->inputs;
1.19      nicm      137:                input = strsep(&cdata->next_input, ",");
1.17      nicm      138:        }
                    139:
                    140:        status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
1.14      nicm      141:            cmd_command_prompt_free, cdata, 0);
1.20      nicm      142:        free(prompt);
1.1       nicm      143:
1.21    ! nicm      144:        return (CMD_RETURN_NORMAL);
1.1       nicm      145: }
                    146:
                    147: int
                    148: cmd_command_prompt_callback(void *data, const char *s)
                    149: {
1.8       nicm      150:        struct cmd_command_prompt_cdata *cdata = data;
1.1       nicm      151:        struct client                   *c = cdata->c;
                    152:        struct cmd_list                 *cmdlist;
1.8       nicm      153:        struct cmd_ctx                   ctx;
1.19      nicm      154:        char                            *cause, *new_template, *prompt, *ptr;
                    155:        char                            *input = NULL;
1.1       nicm      156:
1.8       nicm      157:        if (s == NULL)
1.1       nicm      158:                return (0);
                    159:
1.17      nicm      160:        new_template = cmd_template_replace(cdata->template, s, cdata->idx);
1.20      nicm      161:        free(cdata->template);
1.17      nicm      162:        cdata->template = new_template;
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.8       nicm      168:        if ((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.17      nicm      178:        if (cmd_string_parse(new_template, &cmdlist, &cause) != 0) {
1.8       nicm      179:                if (cause != NULL) {
                    180:                        *cause = toupper((u_char) *cause);
                    181:                        status_message_set(c, "%s", cause);
1.20      nicm      182:                        free(cause);
1.1       nicm      183:                }
1.8       nicm      184:                return (0);
1.1       nicm      185:        }
                    186:
                    187:        ctx.msgdata = NULL;
                    188:        ctx.curclient = c;
                    189:
                    190:        ctx.error = key_bindings_error;
                    191:        ctx.print = key_bindings_print;
                    192:        ctx.info = key_bindings_info;
                    193:
                    194:        ctx.cmdclient = NULL;
                    195:
                    196:        cmd_list_exec(cmdlist, &ctx);
                    197:        cmd_list_free(cmdlist);
                    198:
1.4       nicm      199:        if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback)
1.1       nicm      200:                return (1);
                    201:        return (0);
1.4       nicm      202: }
                    203:
                    204: void
1.14      nicm      205: cmd_command_prompt_free(void *data)
1.4       nicm      206: {
1.8       nicm      207:        struct cmd_command_prompt_cdata *cdata = data;
1.4       nicm      208:
1.20      nicm      209:        free(cdata->inputs);
                    210:        free(cdata->prompts);
                    211:        free(cdata->template);
                    212:        free(cdata);
1.1       nicm      213: }