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

Annotation of src/usr.bin/tmux/cmd.c, Revision 1.108

1.108   ! nicm        1: /* $OpenBSD: cmd.c,v 1.107 2015/12/08 01:10:31 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 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: #include <sys/time.h>
                     21:
1.5       nicm       22: #include <fnmatch.h>
                     23: #include <paths.h>
1.61      nicm       24: #include <pwd.h>
1.1       nicm       25: #include <stdlib.h>
                     26: #include <string.h>
                     27: #include <unistd.h>
                     28:
                     29: #include "tmux.h"
1.105     nicm       30:
                     31: extern const struct cmd_entry cmd_attach_session_entry;
                     32: extern const struct cmd_entry cmd_bind_key_entry;
                     33: extern const struct cmd_entry cmd_break_pane_entry;
                     34: extern const struct cmd_entry cmd_capture_pane_entry;
                     35: extern const struct cmd_entry cmd_choose_buffer_entry;
                     36: extern const struct cmd_entry cmd_choose_client_entry;
                     37: extern const struct cmd_entry cmd_choose_session_entry;
                     38: extern const struct cmd_entry cmd_choose_tree_entry;
                     39: extern const struct cmd_entry cmd_choose_window_entry;
                     40: extern const struct cmd_entry cmd_clear_history_entry;
                     41: extern const struct cmd_entry cmd_clock_mode_entry;
                     42: extern const struct cmd_entry cmd_command_prompt_entry;
                     43: extern const struct cmd_entry cmd_confirm_before_entry;
                     44: extern const struct cmd_entry cmd_copy_mode_entry;
                     45: extern const struct cmd_entry cmd_delete_buffer_entry;
                     46: extern const struct cmd_entry cmd_detach_client_entry;
                     47: extern const struct cmd_entry cmd_display_message_entry;
                     48: extern const struct cmd_entry cmd_display_panes_entry;
                     49: extern const struct cmd_entry cmd_down_pane_entry;
                     50: extern const struct cmd_entry cmd_find_window_entry;
                     51: extern const struct cmd_entry cmd_has_session_entry;
                     52: extern const struct cmd_entry cmd_if_shell_entry;
                     53: extern const struct cmd_entry cmd_join_pane_entry;
                     54: extern const struct cmd_entry cmd_kill_pane_entry;
                     55: extern const struct cmd_entry cmd_kill_server_entry;
                     56: extern const struct cmd_entry cmd_kill_session_entry;
                     57: extern const struct cmd_entry cmd_kill_window_entry;
                     58: extern const struct cmd_entry cmd_last_pane_entry;
                     59: extern const struct cmd_entry cmd_last_window_entry;
                     60: extern const struct cmd_entry cmd_link_window_entry;
                     61: extern const struct cmd_entry cmd_list_buffers_entry;
                     62: extern const struct cmd_entry cmd_list_clients_entry;
                     63: extern const struct cmd_entry cmd_list_commands_entry;
                     64: extern const struct cmd_entry cmd_list_keys_entry;
                     65: extern const struct cmd_entry cmd_list_panes_entry;
                     66: extern const struct cmd_entry cmd_list_sessions_entry;
                     67: extern const struct cmd_entry cmd_list_windows_entry;
                     68: extern const struct cmd_entry cmd_load_buffer_entry;
                     69: extern const struct cmd_entry cmd_lock_client_entry;
                     70: extern const struct cmd_entry cmd_lock_server_entry;
                     71: extern const struct cmd_entry cmd_lock_session_entry;
                     72: extern const struct cmd_entry cmd_move_pane_entry;
                     73: extern const struct cmd_entry cmd_move_window_entry;
                     74: extern const struct cmd_entry cmd_new_session_entry;
                     75: extern const struct cmd_entry cmd_new_window_entry;
                     76: extern const struct cmd_entry cmd_next_layout_entry;
                     77: extern const struct cmd_entry cmd_next_window_entry;
                     78: extern const struct cmd_entry cmd_paste_buffer_entry;
                     79: extern const struct cmd_entry cmd_pipe_pane_entry;
                     80: extern const struct cmd_entry cmd_previous_layout_entry;
                     81: extern const struct cmd_entry cmd_previous_window_entry;
                     82: extern const struct cmd_entry cmd_refresh_client_entry;
                     83: extern const struct cmd_entry cmd_rename_session_entry;
                     84: extern const struct cmd_entry cmd_rename_window_entry;
                     85: extern const struct cmd_entry cmd_resize_pane_entry;
                     86: extern const struct cmd_entry cmd_respawn_pane_entry;
                     87: extern const struct cmd_entry cmd_respawn_window_entry;
                     88: extern const struct cmd_entry cmd_rotate_window_entry;
                     89: extern const struct cmd_entry cmd_run_shell_entry;
                     90: extern const struct cmd_entry cmd_save_buffer_entry;
                     91: extern const struct cmd_entry cmd_select_layout_entry;
                     92: extern const struct cmd_entry cmd_select_pane_entry;
                     93: extern const struct cmd_entry cmd_select_window_entry;
                     94: extern const struct cmd_entry cmd_send_keys_entry;
                     95: extern const struct cmd_entry cmd_send_prefix_entry;
                     96: extern const struct cmd_entry cmd_server_info_entry;
                     97: extern const struct cmd_entry cmd_set_buffer_entry;
                     98: extern const struct cmd_entry cmd_set_environment_entry;
1.107     nicm       99: extern const struct cmd_entry cmd_set_hook_entry;
1.105     nicm      100: extern const struct cmd_entry cmd_set_option_entry;
                    101: extern const struct cmd_entry cmd_set_window_option_entry;
                    102: extern const struct cmd_entry cmd_show_buffer_entry;
                    103: extern const struct cmd_entry cmd_show_environment_entry;
1.107     nicm      104: extern const struct cmd_entry cmd_show_hooks_entry;
1.105     nicm      105: extern const struct cmd_entry cmd_show_messages_entry;
                    106: extern const struct cmd_entry cmd_show_options_entry;
                    107: extern const struct cmd_entry cmd_show_window_options_entry;
                    108: extern const struct cmd_entry cmd_source_file_entry;
                    109: extern const struct cmd_entry cmd_split_window_entry;
                    110: extern const struct cmd_entry cmd_start_server_entry;
                    111: extern const struct cmd_entry cmd_suspend_client_entry;
                    112: extern const struct cmd_entry cmd_swap_pane_entry;
                    113: extern const struct cmd_entry cmd_swap_window_entry;
                    114: extern const struct cmd_entry cmd_switch_client_entry;
                    115: extern const struct cmd_entry cmd_unbind_key_entry;
                    116: extern const struct cmd_entry cmd_unlink_window_entry;
                    117: extern const struct cmd_entry cmd_up_pane_entry;
                    118: extern const struct cmd_entry cmd_wait_for_entry;
1.1       nicm      119:
                    120: const struct cmd_entry *cmd_table[] = {
                    121:        &cmd_attach_session_entry,
                    122:        &cmd_bind_key_entry,
                    123:        &cmd_break_pane_entry,
1.34      nicm      124:        &cmd_capture_pane_entry,
1.42      nicm      125:        &cmd_choose_buffer_entry,
1.15      nicm      126:        &cmd_choose_client_entry,
1.1       nicm      127:        &cmd_choose_session_entry,
1.67      nicm      128:        &cmd_choose_tree_entry,
1.1       nicm      129:        &cmd_choose_window_entry,
                    130:        &cmd_clear_history_entry,
                    131:        &cmd_clock_mode_entry,
                    132:        &cmd_command_prompt_entry,
                    133:        &cmd_confirm_before_entry,
                    134:        &cmd_copy_mode_entry,
                    135:        &cmd_delete_buffer_entry,
                    136:        &cmd_detach_client_entry,
1.7       nicm      137:        &cmd_display_message_entry,
1.16      nicm      138:        &cmd_display_panes_entry,
1.1       nicm      139:        &cmd_find_window_entry,
                    140:        &cmd_has_session_entry,
1.4       nicm      141:        &cmd_if_shell_entry,
1.37      nicm      142:        &cmd_join_pane_entry,
1.1       nicm      143:        &cmd_kill_pane_entry,
                    144:        &cmd_kill_server_entry,
                    145:        &cmd_kill_session_entry,
                    146:        &cmd_kill_window_entry,
1.45      nicm      147:        &cmd_last_pane_entry,
1.1       nicm      148:        &cmd_last_window_entry,
                    149:        &cmd_link_window_entry,
                    150:        &cmd_list_buffers_entry,
                    151:        &cmd_list_clients_entry,
                    152:        &cmd_list_commands_entry,
                    153:        &cmd_list_keys_entry,
1.23      nicm      154:        &cmd_list_panes_entry,
1.1       nicm      155:        &cmd_list_sessions_entry,
                    156:        &cmd_list_windows_entry,
                    157:        &cmd_load_buffer_entry,
1.19      nicm      158:        &cmd_lock_client_entry,
1.1       nicm      159:        &cmd_lock_server_entry,
1.19      nicm      160:        &cmd_lock_session_entry,
1.63      nicm      161:        &cmd_move_pane_entry,
1.1       nicm      162:        &cmd_move_window_entry,
                    163:        &cmd_new_session_entry,
                    164:        &cmd_new_window_entry,
                    165:        &cmd_next_layout_entry,
                    166:        &cmd_next_window_entry,
                    167:        &cmd_paste_buffer_entry,
1.24      nicm      168:        &cmd_pipe_pane_entry,
1.1       nicm      169:        &cmd_previous_layout_entry,
                    170:        &cmd_previous_window_entry,
                    171:        &cmd_refresh_client_entry,
                    172:        &cmd_rename_session_entry,
                    173:        &cmd_rename_window_entry,
                    174:        &cmd_resize_pane_entry,
1.56      nicm      175:        &cmd_respawn_pane_entry,
1.1       nicm      176:        &cmd_respawn_window_entry,
                    177:        &cmd_rotate_window_entry,
1.17      nicm      178:        &cmd_run_shell_entry,
1.1       nicm      179:        &cmd_save_buffer_entry,
                    180:        &cmd_select_layout_entry,
                    181:        &cmd_select_pane_entry,
                    182:        &cmd_select_window_entry,
                    183:        &cmd_send_keys_entry,
                    184:        &cmd_send_prefix_entry,
                    185:        &cmd_server_info_entry,
                    186:        &cmd_set_buffer_entry,
1.13      nicm      187:        &cmd_set_environment_entry,
1.107     nicm      188:        &cmd_set_hook_entry,
1.1       nicm      189:        &cmd_set_option_entry,
                    190:        &cmd_set_window_option_entry,
                    191:        &cmd_show_buffer_entry,
1.13      nicm      192:        &cmd_show_environment_entry,
1.107     nicm      193:        &cmd_show_hooks_entry,
1.32      nicm      194:        &cmd_show_messages_entry,
1.1       nicm      195:        &cmd_show_options_entry,
                    196:        &cmd_show_window_options_entry,
                    197:        &cmd_source_file_entry,
                    198:        &cmd_split_window_entry,
                    199:        &cmd_start_server_entry,
                    200:        &cmd_suspend_client_entry,
                    201:        &cmd_swap_pane_entry,
                    202:        &cmd_swap_window_entry,
                    203:        &cmd_switch_client_entry,
                    204:        &cmd_unbind_key_entry,
                    205:        &cmd_unlink_window_entry,
1.82      nicm      206:        &cmd_wait_for_entry,
1.1       nicm      207:        NULL
                    208: };
                    209:
1.108   ! nicm      210: static void             cmd_clear_state(struct cmd_state *);
        !           211: static struct client   *cmd_get_state_client(struct cmd_q *, int);
        !           212: static int              cmd_set_state_flag(struct cmd *, struct cmd_q *, char);
        !           213:
1.10      nicm      214: int
                    215: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
                    216: {
                    217:        size_t  arglen;
                    218:        int     i;
1.91      nicm      219:
                    220:        if (argc == 0)
                    221:                return (0);
1.10      nicm      222:
                    223:        *buf = '\0';
                    224:        for (i = 0; i < argc; i++) {
                    225:                if (strlcpy(buf, argv[i], len) >= len)
                    226:                        return (-1);
                    227:                arglen = strlen(argv[i]) + 1;
                    228:                buf += arglen;
                    229:                len -= arglen;
                    230:        }
                    231:
                    232:        return (0);
                    233: }
                    234:
                    235: int
                    236: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
                    237: {
                    238:        int     i;
                    239:        size_t  arglen;
                    240:
                    241:        if (argc == 0)
                    242:                return (0);
                    243:        *argv = xcalloc(argc, sizeof **argv);
                    244:
                    245:        buf[len - 1] = '\0';
                    246:        for (i = 0; i < argc; i++) {
                    247:                if (len == 0) {
                    248:                        cmd_free_argv(argc, *argv);
                    249:                        return (-1);
                    250:                }
                    251:
                    252:                arglen = strlen(buf) + 1;
                    253:                (*argv)[i] = xstrdup(buf);
                    254:                buf += arglen;
                    255:                len -= arglen;
                    256:        }
                    257:
                    258:        return (0);
1.46      nicm      259: }
                    260:
                    261: char **
1.93      nicm      262: cmd_copy_argv(int argc, char **argv)
1.46      nicm      263: {
                    264:        char    **new_argv;
                    265:        int       i;
                    266:
                    267:        if (argc == 0)
                    268:                return (NULL);
1.94      nicm      269:        new_argv = xcalloc(argc + 1, sizeof *new_argv);
1.46      nicm      270:        for (i = 0; i < argc; i++) {
                    271:                if (argv[i] != NULL)
                    272:                        new_argv[i] = xstrdup(argv[i]);
                    273:        }
                    274:        return (new_argv);
1.10      nicm      275: }
                    276:
                    277: void
                    278: cmd_free_argv(int argc, char **argv)
                    279: {
                    280:        int     i;
                    281:
                    282:        if (argc == 0)
1.35      nicm      283:                return;
1.68      nicm      284:        for (i = 0; i < argc; i++)
                    285:                free(argv[i]);
                    286:        free(argv);
1.94      nicm      287: }
                    288:
                    289: char *
                    290: cmd_stringify_argv(int argc, char **argv)
                    291: {
                    292:        char    *buf;
                    293:        int      i;
                    294:        size_t   len;
                    295:
                    296:        if (argc == 0)
                    297:                return (xstrdup(""));
                    298:
                    299:        len = 0;
                    300:        buf = NULL;
                    301:
                    302:        for (i = 0; i < argc; i++) {
                    303:                len += strlen(argv[i]) + 1;
1.98      nicm      304:                buf = xrealloc(buf, len);
1.94      nicm      305:
                    306:                if (i == 0)
                    307:                        *buf = '\0';
                    308:                else
                    309:                        strlcat(buf, " ", len);
                    310:                strlcat(buf, argv[i], len);
                    311:        }
                    312:        return (buf);
1.10      nicm      313: }
                    314:
1.1       nicm      315: struct cmd *
1.80      nicm      316: cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
1.1       nicm      317: {
                    318:        const struct cmd_entry **entryp, *entry;
1.27      deraadt   319:        struct cmd              *cmd;
1.49      nicm      320:        struct args             *args;
1.1       nicm      321:        char                     s[BUFSIZ];
1.49      nicm      322:        int                      ambiguous = 0;
1.1       nicm      323:
                    324:        *cause = NULL;
1.2       nicm      325:        if (argc == 0) {
                    326:                xasprintf(cause, "no command");
1.1       nicm      327:                return (NULL);
1.2       nicm      328:        }
1.1       nicm      329:
                    330:        entry = NULL;
                    331:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    332:                if ((*entryp)->alias != NULL &&
                    333:                    strcmp((*entryp)->alias, argv[0]) == 0) {
1.3       nicm      334:                        ambiguous = 0;
1.1       nicm      335:                        entry = *entryp;
                    336:                        break;
                    337:                }
                    338:
                    339:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    340:                        continue;
                    341:                if (entry != NULL)
1.3       nicm      342:                        ambiguous = 1;
1.1       nicm      343:                entry = *entryp;
                    344:
                    345:                /* Bail now if an exact match. */
                    346:                if (strcmp(entry->name, argv[0]) == 0)
                    347:                        break;
                    348:        }
1.3       nicm      349:        if (ambiguous)
                    350:                goto ambiguous;
1.1       nicm      351:        if (entry == NULL) {
                    352:                xasprintf(cause, "unknown command: %s", argv[0]);
                    353:                return (NULL);
                    354:        }
                    355:
1.49      nicm      356:        args = args_parse(entry->args_template, argc, argv);
                    357:        if (args == NULL)
                    358:                goto usage;
                    359:        if (entry->args_lower != -1 && args->argc < entry->args_lower)
                    360:                goto usage;
                    361:        if (entry->args_upper != -1 && args->argc > entry->args_upper)
                    362:                goto usage;
1.1       nicm      363:
1.80      nicm      364:        cmd = xcalloc(1, sizeof *cmd);
1.1       nicm      365:        cmd->entry = entry;
1.49      nicm      366:        cmd->args = args;
1.80      nicm      367:
                    368:        if (file != NULL)
                    369:                cmd->file = xstrdup(file);
                    370:        cmd->line = line;
                    371:
1.1       nicm      372:        return (cmd);
                    373:
                    374: ambiguous:
                    375:        *s = '\0';
                    376:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    377:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    378:                        continue;
                    379:                if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
                    380:                        break;
                    381:                if (strlcat(s, ", ", sizeof s) >= sizeof s)
                    382:                        break;
                    383:        }
                    384:        s[strlen(s) - 2] = '\0';
                    385:        xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
                    386:        return (NULL);
                    387:
                    388: usage:
1.49      nicm      389:        if (args != NULL)
                    390:                args_free(args);
1.1       nicm      391:        xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
                    392:        return (NULL);
1.108   ! nicm      393: }
        !           394:
        !           395: static void
        !           396: cmd_clear_state(struct cmd_state *state)
        !           397: {
        !           398:        state->c = NULL;
        !           399:
        !           400:        state->tflag.s = NULL;
        !           401:        state->tflag.wl = NULL;
        !           402:        state->tflag.wp = NULL;
        !           403:        state->tflag.idx = -1;
        !           404:
        !           405:        state->sflag.s = NULL;
        !           406:        state->sflag.wl = NULL;
        !           407:        state->sflag.wp = NULL;
        !           408:        state->sflag.idx = -1;
        !           409: }
        !           410:
        !           411: static struct client *
        !           412: cmd_get_state_client(struct cmd_q *cmdq, int quiet)
        !           413: {
        !           414:        struct cmd      *cmd = cmdq->cmd;
        !           415:        struct args     *args = cmd->args;
        !           416:
        !           417:        switch (cmd->entry->flags & (CMD_CLIENT_C|CMD_CLIENT_T)) {
        !           418:        case 0:
        !           419:                return (cmd_find_client(cmdq, NULL, 1));
        !           420:        case CMD_CLIENT_C:
        !           421:                return (cmd_find_client(cmdq, args_get(args, 'c'), quiet));
        !           422:        case CMD_CLIENT_T:
        !           423:                return (cmd_find_client(cmdq, args_get(args, 't'), quiet));
        !           424:        default:
        !           425:                fatalx("both -t and -c for %s", cmd->entry->name);
        !           426:        }
        !           427: }
        !           428:
        !           429: static int
        !           430: cmd_set_state_flag(struct cmd *cmd, struct cmd_q *cmdq, char c)
        !           431: {
        !           432:        struct cmd_state        *state = &cmdq->state;
        !           433:        struct cmd_state_flag   *statef = NULL;
        !           434:        const char              *flag;
        !           435:        int                      flags = cmd->entry->flags, everything = 0;
        !           436:        int                      allflags = 0;
        !           437:        int                      prefer = !!(flags & CMD_PREFERUNATTACHED);
        !           438:        struct session          *s;
        !           439:        struct window           *w;
        !           440:        struct winlink          *wl;
        !           441:        struct window_pane      *wp;
        !           442:
        !           443:        if (c == 't') {
        !           444:                statef = &cmdq->state.tflag;
        !           445:                allflags = CMD_ALL_T;
        !           446:        } else if (c == 's') {
        !           447:                statef = &cmdq->state.sflag;
        !           448:                allflags = CMD_ALL_S;
        !           449:        }
        !           450:
        !           451:        /*
        !           452:         * If the command wants something and no argument is present, use the
        !           453:         * base command instead.
        !           454:         */
        !           455:        flag = args_get(cmd->args, c);
        !           456:        if (flag == NULL) {
        !           457:                if ((flags & allflags) == 0)
        !           458:                        return (0); /* doesn't care about flag */
        !           459:                cmd = cmdq->cmd;
        !           460:                everything = 1;
        !           461:                flag = args_get(cmd->args, c);
        !           462:        }
        !           463:
        !           464:        /*
        !           465:         * If no flag and the current command is allowed to fail, just skip to
        !           466:         * fill in as much we can, otherwise continue and fail later if needed.
        !           467:         */
        !           468:        if (flag == NULL && (flags & CMD_CANFAIL))
        !           469:                goto complete_everything;
        !           470:
        !           471:        /* Fill in state using command (current or base) flags. */
        !           472:        switch (cmd->entry->flags & allflags) {
        !           473:        case 0:
        !           474:                break;
        !           475:        case CMD_SESSION_T|CMD_PANE_T:
        !           476:        case CMD_SESSION_S|CMD_PANE_S:
        !           477:                if (flag != NULL && flag[strcspn(flag, ":.")] != '\0') {
        !           478:                        statef->wl = cmd_find_pane(cmdq, flag, &statef->s,
        !           479:                            &statef->wp);
        !           480:                        if (statef->wl == NULL)
        !           481:                                return (-1);
        !           482:                } else {
        !           483:                        statef->s = cmd_find_session(cmdq, flag, prefer);
        !           484:                        if (statef->s == NULL)
        !           485:                                return (-1);
        !           486:
        !           487:                        s = statef->s;
        !           488:                        if (flag == NULL) {
        !           489:                                statef->wl = s->curw;
        !           490:                                statef->wp = s->curw->window->active;
        !           491:                        } else {
        !           492:                                if ((w = window_find_by_id_str(flag)) != NULL)
        !           493:                                        wp = w->active;
        !           494:                                else {
        !           495:                                        wp = window_pane_find_by_id_str(flag);
        !           496:                                        if (wp != NULL)
        !           497:                                                w = wp->window;
        !           498:                                }
        !           499:                                wl = winlink_find_by_window(&s->windows, w);
        !           500:                                if (wl != NULL) {
        !           501:                                        statef->wl = wl;
        !           502:                                        statef->wp = wp;
        !           503:                                }
        !           504:                        }
        !           505:                }
        !           506:                break;
        !           507:        case CMD_MOVEW_R|CMD_INDEX_T:
        !           508:        case CMD_MOVEW_R|CMD_INDEX_S:
        !           509:                statef->s = cmd_find_session(cmdq, flag, prefer);
        !           510:                if (statef->s == NULL) {
        !           511:                        statef->idx = cmd_find_index(cmdq, flag, &statef->s);
        !           512:                        if (statef->idx == -2)
        !           513:                                return (-1);
        !           514:                }
        !           515:                break;
        !           516:        case CMD_SESSION_T:
        !           517:        case CMD_SESSION_S:
        !           518:                statef->s = cmd_find_session(cmdq, flag, prefer);
        !           519:                if (statef->s == NULL)
        !           520:                        return (-1);
        !           521:                break;
        !           522:        case CMD_WINDOW_T:
        !           523:        case CMD_WINDOW_S:
        !           524:                statef->wl = cmd_find_window(cmdq, flag, &statef->s);
        !           525:                if (statef->wl == NULL)
        !           526:                        return (-1);
        !           527:                break;
        !           528:        case CMD_WINDOW_MARKED_T:
        !           529:        case CMD_WINDOW_MARKED_S:
        !           530:                statef->wl = cmd_find_window_marked(cmdq, flag, &statef->s);
        !           531:                if (statef->wl == NULL)
        !           532:                        return (-1);
        !           533:                break;
        !           534:        case CMD_PANE_T:
        !           535:        case CMD_PANE_S:
        !           536:                statef->wl = cmd_find_pane(cmdq, flag, &statef->s,
        !           537:                    &statef->wp);
        !           538:                if (statef->wl == NULL)
        !           539:                        return (-1);
        !           540:                break;
        !           541:        case CMD_PANE_MARKED_S:
        !           542:        case CMD_PANE_MARKED_T:
        !           543:                statef->wl = cmd_find_pane_marked(cmdq, flag, &statef->s,
        !           544:                    &statef->wp);
        !           545:                if (statef->wl == NULL)
        !           546:                        return (-1);
        !           547:                break;
        !           548:        case CMD_INDEX_T:
        !           549:        case CMD_INDEX_S:
        !           550:                statef->idx = cmd_find_index(cmdq, flag, &statef->s);
        !           551:                if (statef->idx == -2)
        !           552:                        return (-1);
        !           553:                break;
        !           554:        default:
        !           555:                fatalx("too many -%c for %s", c, cmd->entry->name);
        !           556:        }
        !           557:
        !           558:        /*
        !           559:         * If this is still the current command, it wants what it asked for and
        !           560:         * nothing more. If it's the base command, fill in as much as possible
        !           561:         * because the current command may have different flags.
        !           562:         */
        !           563:        if (!everything)
        !           564:                return (0);
        !           565:
        !           566: complete_everything:
        !           567:        if (statef->s == NULL) {
        !           568:                if (state->c != NULL)
        !           569:                        statef->s = state->c->session;
        !           570:                if (statef->s == NULL)
        !           571:                        statef->s = cmd_find_current(cmdq);
        !           572:                if (statef->s == NULL) {
        !           573:                        if (flags & CMD_CANFAIL)
        !           574:                                return (0);
        !           575:
        !           576:                        cmdq_error(cmdq, "no current session");
        !           577:                        return (-1);
        !           578:                }
        !           579:        }
        !           580:        if (statef->wl == NULL)
        !           581:                statef->wl = cmd_find_window(cmdq, flag, &statef->s);
        !           582:        if (statef->wp == NULL)
        !           583:                statef->wl = cmd_find_pane(cmdq, flag, &statef->s, &statef->wp);
        !           584:
        !           585:        return (0);
        !           586: }
        !           587:
        !           588: int
        !           589: cmd_prepare_state(struct cmd *cmd, struct cmd_q *cmdq)
        !           590: {
        !           591:        struct cmd_state        *state = &cmdq->state;
        !           592:        struct args             *args = cmd->args;
        !           593:        const char              *cflag, *tflag;
        !           594:        char                    *tmp;
        !           595:        int                      error;
        !           596:
        !           597:        tmp = cmd_print(cmd);
        !           598:        log_debug("preparing state for: %s (client %d)", tmp,
        !           599:            cmdq->client != NULL ? cmdq->client->fd : -1);
        !           600:        free(tmp);
        !           601:
        !           602:        /* Start with an empty state. */
        !           603:        cmd_clear_state(state);
        !           604:
        !           605:        /*
        !           606:         * If the command wants a client and provides -c or -t, use it. If not,
        !           607:         * try the base command instead via cmd_get_state_client. No client is
        !           608:         * allowed if no flags, otherwise it must be available.
        !           609:         */
        !           610:        switch (cmd->entry->flags & (CMD_CLIENT_C|CMD_CLIENT_T)) {
        !           611:        case 0:
        !           612:                state->c = cmd_get_state_client(cmdq, 1);
        !           613:                break;
        !           614:        case CMD_CLIENT_C:
        !           615:                cflag = args_get(args, 'c');
        !           616:                if (cflag == NULL)
        !           617:                        state->c = cmd_get_state_client(cmdq, 0);
        !           618:                else
        !           619:                        state->c = cmd_find_client(cmdq, cflag, 0);
        !           620:                if (state->c == NULL)
        !           621:                        return (-1);
        !           622:                break;
        !           623:        case CMD_CLIENT_T:
        !           624:                tflag = args_get(args, 't');
        !           625:                if (tflag == NULL)
        !           626:                        state->c = cmd_get_state_client(cmdq, 0);
        !           627:                else
        !           628:                        state->c = cmd_find_client(cmdq, tflag, 0);
        !           629:                if (state->c == NULL)
        !           630:                        return (-1);
        !           631:                break;
        !           632:        default:
        !           633:                fatalx("both -c and -t for %s", cmd->entry->name);
        !           634:        }
        !           635:
        !           636:        error = cmd_set_state_flag(cmd, cmdq, 't');
        !           637:        if (error == 0)
        !           638:                error = cmd_set_state_flag(cmd, cmdq, 's');
        !           639:        return (error);
1.1       nicm      640: }
                    641:
1.106     nicm      642: char *
                    643: cmd_print(struct cmd *cmd)
1.1       nicm      644: {
1.106     nicm      645:        char    *out, *s;
                    646:
                    647:        s = args_print(cmd->args);
                    648:        if (*s != '\0')
                    649:                xasprintf(&out, "%s %s", cmd->entry->name, s);
                    650:        else
                    651:                out = xstrdup(cmd->entry->name);
                    652:        free(s);
1.49      nicm      653:
1.106     nicm      654:        return (out);
1.1       nicm      655: }
                    656:
1.99      nicm      657: /* Adjust current mouse position for a pane. */
                    658: int
                    659: cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
                    660:     u_int *yp, int last)
                    661: {
                    662:        u_int   x, y;
                    663:
                    664:        if (last) {
                    665:                x = m->lx;
                    666:                y = m->ly;
                    667:        } else {
                    668:                x = m->x;
                    669:                y = m->y;
                    670:        }
                    671:
                    672:        if (m->statusat == 0 && y > 0)
                    673:                y--;
                    674:        else if (m->statusat > 0 && y >= (u_int)m->statusat)
                    675:                y = m->statusat - 1;
                    676:
                    677:        if (x < wp->xoff || x >= wp->xoff + wp->sx)
                    678:                return (-1);
                    679:        if (y < wp->yoff || y >= wp->yoff + wp->sy)
                    680:                return (-1);
                    681:
                    682:        *xp = x - wp->xoff;
                    683:        *yp = y - wp->yoff;
                    684:        return (0);
                    685: }
                    686:
                    687: /* Get current mouse window if any. */
                    688: struct winlink *
                    689: cmd_mouse_window(struct mouse_event *m, struct session **sp)
                    690: {
                    691:        struct session  *s;
                    692:        struct window   *w;
                    693:
                    694:        if (!m->valid || m->s == -1 || m->w == -1)
                    695:                return (NULL);
                    696:        if ((s = session_find_by_id(m->s)) == NULL)
                    697:                return (NULL);
                    698:        if ((w = window_find_by_id(m->w)) == NULL)
                    699:                return (NULL);
                    700:
                    701:        if (sp != NULL)
                    702:                *sp = s;
                    703:        return (winlink_find_by_window(&s->windows, w));
                    704: }
                    705:
                    706: /* Get current mouse pane if any. */
                    707: struct window_pane *
1.104     nicm      708: cmd_mouse_pane(struct mouse_event *m, struct session **sp,
                    709:     struct winlink **wlp)
1.99      nicm      710: {
                    711:        struct winlink          *wl;
                    712:        struct window_pane      *wp;
                    713:
                    714:        if ((wl = cmd_mouse_window(m, sp)) == NULL)
                    715:                return (NULL);
                    716:        if ((wp = window_pane_find_by_id(m->wp)) == NULL)
                    717:                return (NULL);
                    718:        if (!window_has_pane(wl->window, wp))
                    719:                return (NULL);
                    720:
                    721:        if (wlp != NULL)
                    722:                *wlp = wl;
1.43      nicm      723:        return (wp);
1.15      nicm      724: }
                    725:
                    726: /* Replace the first %% or %idx in template by s. */
                    727: char *
1.75      nicm      728: cmd_template_replace(const char *template, const char *s, int idx)
1.15      nicm      729: {
1.75      nicm      730:        char             ch, *buf;
                    731:        const char      *ptr;
1.84      nicm      732:        int              replaced;
                    733:        size_t           len;
1.15      nicm      734:
1.76      nicm      735:        if (strchr(template, '%') == NULL)
1.15      nicm      736:                return (xstrdup(template));
                    737:
                    738:        buf = xmalloc(1);
                    739:        *buf = '\0';
                    740:        len = 0;
                    741:        replaced = 0;
                    742:
                    743:        ptr = template;
                    744:        while (*ptr != '\0') {
                    745:                switch (ch = *ptr++) {
                    746:                case '%':
                    747:                        if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
                    748:                                if (*ptr != '%' || replaced)
                    749:                                        break;
                    750:                                replaced = 1;
                    751:                        }
                    752:                        ptr++;
                    753:
                    754:                        len += strlen(s);
1.98      nicm      755:                        buf = xrealloc(buf, len + 1);
1.15      nicm      756:                        strlcat(buf, s, len + 1);
                    757:                        continue;
                    758:                }
1.98      nicm      759:                buf = xrealloc(buf, len + 2);
1.15      nicm      760:                buf[len++] = ch;
                    761:                buf[len] = '\0';
                    762:        }
                    763:
                    764:        return (buf);
1.1       nicm      765: }