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

1.45    ! nicm        1: /* $OpenBSD: cmd-command-prompt.c,v 1.44 2017/05/17 15:20:23 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.39      nicm       43:        .args = { "1iI:Np:t:", 0, 1 },
                     44:        .usage = "[-1Ni] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " "
1.31      nicm       45:                 "[template]",
                     46:
1.32      nicm       47:        .flags = 0,
1.31      nicm       48:        .exec = cmd_command_prompt_exec
1.8       nicm       49: };
                     50:
                     51: struct cmd_command_prompt_cdata {
1.44      nicm       52:        int      flags;
1.39      nicm       53:
1.44      nicm       54:        char    *inputs;
                     55:        char    *next_input;
1.39      nicm       56:
1.44      nicm       57:        char    *prompts;
                     58:        char    *next_prompt;
1.39      nicm       59:
1.44      nicm       60:        char    *template;
                     61:        int      idx;
1.1       nicm       62: };
                     63:
1.34      nicm       64: static enum cmd_retval
1.38      nicm       65: cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       66: {
1.14      nicm       67:        struct args                     *args = self->args;
1.17      nicm       68:        const char                      *inputs, *prompts;
1.8       nicm       69:        struct cmd_command_prompt_cdata *cdata;
1.43      nicm       70:        struct client                   *c;
1.19      nicm       71:        char                            *prompt, *ptr, *input = NULL;
1.8       nicm       72:        size_t                           n;
1.43      nicm       73:
                     74:        if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
                     75:                return (CMD_RETURN_ERROR);
1.1       nicm       76:
                     77:        if (c->prompt_string != NULL)
1.21      nicm       78:                return (CMD_RETURN_NORMAL);
1.1       nicm       79:
1.39      nicm       80:        cdata = xcalloc(1, sizeof *cdata);
                     81:
1.17      nicm       82:        cdata->inputs = NULL;
                     83:        cdata->next_input = NULL;
1.39      nicm       84:
                     85:        cdata->prompts = NULL;
1.8       nicm       86:        cdata->next_prompt = NULL;
1.39      nicm       87:
1.8       nicm       88:        cdata->template = NULL;
1.39      nicm       89:        cdata->idx = 1;
1.8       nicm       90:
1.14      nicm       91:        if (args->argc != 0)
                     92:                cdata->template = xstrdup(args->argv[0]);
1.8       nicm       93:        else
                     94:                cdata->template = xstrdup("%1");
1.14      nicm       95:
1.17      nicm       96:        if ((prompts = args_get(args, 'p')) != NULL)
1.14      nicm       97:                cdata->prompts = xstrdup(prompts);
                     98:        else if (args->argc != 0) {
                     99:                n = strcspn(cdata->template, " ,");
                    100:                xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
1.8       nicm      101:        } else
                    102:                cdata->prompts = xstrdup(":");
                    103:
1.17      nicm      104:        /* Get first prompt. */
1.8       nicm      105:        cdata->next_prompt = cdata->prompts;
                    106:        ptr = strsep(&cdata->next_prompt, ",");
1.14      nicm      107:        if (prompts == NULL)
1.8       nicm      108:                prompt = xstrdup(ptr);
1.19      nicm      109:        else
                    110:                xasprintf(&prompt, "%s ", ptr);
1.17      nicm      111:
                    112:        /* Get initial prompt input. */
                    113:        if ((inputs = args_get(args, 'I')) != NULL) {
                    114:                cdata->inputs = xstrdup(inputs);
                    115:                cdata->next_input = cdata->inputs;
1.19      nicm      116:                input = strsep(&cdata->next_input, ",");
1.17      nicm      117:        }
                    118:
1.35      nicm      119:        if (args_has(args, '1'))
1.39      nicm      120:                cdata->flags |= PROMPT_SINGLE;
1.36      nicm      121:        else if (args_has(args, 'N'))
1.39      nicm      122:                cdata->flags |= PROMPT_NUMERIC;
                    123:        else if (args_has(args, 'i'))
                    124:                cdata->flags |= PROMPT_INCREMENTAL;
1.17      nicm      125:        status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
1.39      nicm      126:            cmd_command_prompt_free, cdata, cdata->flags);
1.20      nicm      127:        free(prompt);
1.1       nicm      128:
1.21      nicm      129:        return (CMD_RETURN_NORMAL);
1.1       nicm      130: }
                    131:
1.34      nicm      132: static int
1.44      nicm      133: cmd_command_prompt_callback(struct client *c, void *data, const char *s,
                    134:     int done)
1.1       nicm      135: {
1.8       nicm      136:        struct cmd_command_prompt_cdata *cdata = data;
1.1       nicm      137:        struct cmd_list                 *cmdlist;
1.38      nicm      138:        struct cmdq_item                *new_item;
1.19      nicm      139:        char                            *cause, *new_template, *prompt, *ptr;
                    140:        char                            *input = NULL;
1.1       nicm      141:
1.8       nicm      142:        if (s == NULL)
1.1       nicm      143:                return (0);
1.39      nicm      144:        if (done && (cdata->flags & PROMPT_INCREMENTAL))
                    145:                return (0);
1.1       nicm      146:
1.17      nicm      147:        new_template = cmd_template_replace(cdata->template, s, cdata->idx);
1.39      nicm      148:        if (done) {
                    149:                free(cdata->template);
                    150:                cdata->template = new_template;
                    151:        }
1.8       nicm      152:
1.17      nicm      153:        /*
                    154:         * Check if there are more prompts; if so, get its respective input
                    155:         * and update the prompt data.
                    156:         */
1.39      nicm      157:        if (done && (ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
1.19      nicm      158:                xasprintf(&prompt, "%s ", ptr);
                    159:                input = strsep(&cdata->next_input, ",");
1.17      nicm      160:                status_prompt_update(c, prompt, input);
                    161:
1.20      nicm      162:                free(prompt);
1.8       nicm      163:                cdata->idx++;
                    164:                return (1);
                    165:        }
1.1       nicm      166:
1.41      nicm      167:        cmdlist = cmd_string_parse(new_template, NULL, 0, &cause);
                    168:        if (cmdlist == NULL) {
1.45    ! nicm      169:                if (cause != NULL)
        !           170:                        new_item = cmdq_get_error(cause);
        !           171:                else
1.38      nicm      172:                        new_item = NULL;
1.45    ! nicm      173:                free(cause);
1.37      nicm      174:        } else {
1.38      nicm      175:                new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
1.37      nicm      176:                cmd_list_free(cmdlist);
1.1       nicm      177:        }
                    178:
1.38      nicm      179:        if (new_item != NULL)
                    180:                cmdq_append(c, new_item);
1.1       nicm      181:
1.39      nicm      182:        if (!done)
                    183:                free(new_template);
1.44      nicm      184:        if (c->prompt_inputcb != cmd_command_prompt_callback)
1.1       nicm      185:                return (1);
                    186:        return (0);
1.4       nicm      187: }
                    188:
1.34      nicm      189: static void
1.14      nicm      190: cmd_command_prompt_free(void *data)
1.4       nicm      191: {
1.8       nicm      192:        struct cmd_command_prompt_cdata *cdata = data;
1.4       nicm      193:
1.20      nicm      194:        free(cdata->inputs);
                    195:        free(cdata->prompts);
                    196:        free(cdata->template);
                    197:        free(cdata);
1.1       nicm      198: }