=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/tmux/cmd.c,v retrieving revision 1.79 retrieving revision 1.80 diff -c -r1.79 -r1.80 *** src/usr.bin/tmux/cmd.c 2013/03/24 09:27:20 1.79 --- src/usr.bin/tmux/cmd.c 2013/03/24 09:54:10 1.80 *************** *** 1,4 **** ! /* $OpenBSD: cmd.c,v 1.79 2013/03/24 09:27:20 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott --- 1,4 ---- ! /* $OpenBSD: cmd.c,v 1.80 2013/03/24 09:54:10 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott *************** *** 127,174 **** struct window_pane *cmd_lookup_paneid(const char *); struct winlink *cmd_lookup_winlink_windowid(struct session *, const char *); struct window *cmd_lookup_windowid(const char *); ! struct session *cmd_window_session(struct cmd_ctx *, ! struct window *, struct winlink **); struct winlink *cmd_find_window_offset(const char *, struct session *, int *); int cmd_find_index_offset(const char *, struct session *, int *); struct window_pane *cmd_find_pane_offset(const char *, struct winlink *); - struct cmd_ctx * - cmd_get_ctx(struct client *cmdclient, struct client *curclient) - { - struct cmd_ctx *ctx; - - ctx = xcalloc(1, sizeof *ctx); - ctx->references = 0; - - ctx->cmdclient = cmdclient; - ctx->curclient = curclient; - - cmd_ref_ctx(ctx); - return (ctx); - } - - void - cmd_free_ctx(struct cmd_ctx *ctx) - { - if (ctx->cmdclient != NULL) - ctx->cmdclient->references--; - if (ctx->curclient != NULL) - ctx->curclient->references--; - if (--ctx->references == 0) - free(ctx); - } - - void - cmd_ref_ctx(struct cmd_ctx *ctx) - { - ctx->references++; - if (ctx->cmdclient != NULL) - ctx->cmdclient->references++; - if (ctx->curclient != NULL) - ctx->curclient->references++; - } - int cmd_pack_argv(int argc, char **argv, char *buf, size_t len) { --- 127,138 ---- struct window_pane *cmd_lookup_paneid(const char *); struct winlink *cmd_lookup_winlink_windowid(struct session *, const char *); struct window *cmd_lookup_windowid(const char *); ! struct session *cmd_window_session(struct cmd_q *, struct window *, ! struct winlink **); struct winlink *cmd_find_window_offset(const char *, struct session *, int *); int cmd_find_index_offset(const char *, struct session *, int *); struct window_pane *cmd_find_pane_offset(const char *, struct winlink *); int cmd_pack_argv(int argc, char **argv, char *buf, size_t len) { *************** *** 242,248 **** } struct cmd * ! cmd_parse(int argc, char **argv, char **cause) { const struct cmd_entry **entryp, *entry; struct cmd *cmd; --- 206,212 ---- } struct cmd * ! cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause) { const struct cmd_entry **entryp, *entry; struct cmd *cmd; *************** *** 292,300 **** if (entry->check != NULL && entry->check(args) != 0) goto usage; ! cmd = xmalloc(sizeof *cmd); cmd->entry = entry; cmd->args = args; return (cmd); ambiguous: --- 256,269 ---- if (entry->check != NULL && entry->check(args) != 0) goto usage; ! cmd = xcalloc(1, sizeof *cmd); cmd->entry = entry; cmd->args = args; + + if (file != NULL) + cmd->file = xstrdup(file); + cmd->line = line; + return (cmd); ambiguous: *************** *** 343,373 **** * session from all sessions. */ struct session * ! cmd_current_session(struct cmd_ctx *ctx, int prefer_unattached) { ! struct msg_command_data *data = ctx->msgdata; ! struct client *c = ctx->cmdclient; struct session *s; struct sessionslist ss; struct winlink *wl; struct window_pane *wp; int found; ! if (ctx->curclient != NULL && ctx->curclient->session != NULL) ! return (ctx->curclient->session); /* * If the name of the calling client's pty is know, build a list of the * sessions that contain it and if any choose either the first or the * newest. */ ! if (c != NULL && c->tty.path != NULL) { ARRAY_INIT(&ss); RB_FOREACH(s, sessions, &sessions) { found = 0; RB_FOREACH(wl, winlinks, &s->windows) { TAILQ_FOREACH(wp, &wl->window->panes, entry) { ! if (strcmp(wp->tty, c->tty.path) == 0) { found = 1; break; } --- 312,344 ---- * session from all sessions. */ struct session * ! cmd_current_session(struct cmd_q *cmdq, int prefer_unattached) { ! struct msg_command_data *data = cmdq->msgdata; ! struct client *c = cmdq->client; struct session *s; struct sessionslist ss; struct winlink *wl; struct window_pane *wp; + const char *path; int found; ! if (c != NULL && c->session != NULL) ! return (c->session); /* * If the name of the calling client's pty is know, build a list of the * sessions that contain it and if any choose either the first or the * newest. */ ! path = c == NULL ? NULL : c->tty.path; ! if (path != NULL) { ARRAY_INIT(&ss); RB_FOREACH(s, sessions, &sessions) { found = 0; RB_FOREACH(wl, winlinks, &s->windows) { TAILQ_FOREACH(wp, &wl->window->panes, entry) { ! if (strcmp(wp->tty, path) == 0) { found = 1; break; } *************** *** 458,478 **** * then of all clients. */ struct client * ! cmd_current_client(struct cmd_ctx *ctx) { struct session *s; struct client *c; struct clients cc; u_int i; ! if (ctx->curclient != NULL) ! return (ctx->curclient); /* * No current client set. Find the current session and return the * newest of its clients. */ ! s = cmd_current_session(ctx, 0); if (s != NULL && !(s->flags & SESSION_UNATTACHED)) { ARRAY_INIT(&cc); for (i = 0; i < ARRAY_LENGTH(&clients); i++) { --- 429,449 ---- * then of all clients. */ struct client * ! cmd_current_client(struct cmd_q *cmdq) { struct session *s; struct client *c; struct clients cc; u_int i; ! if (cmdq->client != NULL && cmdq->client->session != NULL) ! return (cmdq->client); /* * No current client set. Find the current session and return the * newest of its clients. */ ! s = cmd_current_session(cmdq, 0); if (s != NULL && !(s->flags & SESSION_UNATTACHED)) { ARRAY_INIT(&cc); for (i = 0; i < ARRAY_LENGTH(&clients); i++) { *************** *** 517,523 **** /* Find the target client or report an error and return NULL. */ struct client * ! cmd_find_client(struct cmd_ctx *ctx, const char *arg, int quiet) { struct client *c; char *tmparg; --- 488,494 ---- /* Find the target client or report an error and return NULL. */ struct client * ! cmd_find_client(struct cmd_q *cmdq, const char *arg, int quiet) { struct client *c; char *tmparg; *************** *** 525,533 **** /* A NULL argument means the current client. */ if (arg == NULL) { ! c = cmd_current_client(ctx); if (c == NULL && !quiet) ! ctx->error(ctx, "no clients"); return (c); } tmparg = xstrdup(arg); --- 496,504 ---- /* A NULL argument means the current client. */ if (arg == NULL) { ! c = cmd_current_client(cmdq); if (c == NULL && !quiet) ! cmdq_error(cmdq, "no clients"); return (c); } tmparg = xstrdup(arg); *************** *** 542,548 **** /* If no client found, report an error. */ if (c == NULL && !quiet) ! ctx->error(ctx, "client not found: %s", tmparg); free(tmparg); return (c); --- 513,519 ---- /* If no client found, report an error. */ if (c == NULL && !quiet) ! cmdq_error(cmdq, "client not found: %s", tmparg); free(tmparg); return (c); *************** *** 744,757 **** /* Find session and winlink for window. */ struct session * ! cmd_window_session(struct cmd_ctx *ctx, struct window *w, struct winlink **wlp) { struct session *s; struct sessionslist ss; struct winlink *wl; /* If this window is in the current session, return that winlink. */ ! s = cmd_current_session(ctx, 0); if (s != NULL) { wl = winlink_find_by_window(&s->windows, w); if (wl != NULL) { --- 715,728 ---- /* Find session and winlink for window. */ struct session * ! cmd_window_session(struct cmd_q *cmdq, struct window *w, struct winlink **wlp) { struct session *s; struct sessionslist ss; struct winlink *wl; /* If this window is in the current session, return that winlink. */ ! s = cmd_current_session(cmdq, 0); if (s != NULL) { wl = winlink_find_by_window(&s->windows, w); if (wl != NULL) { *************** *** 776,782 **** /* Find the target session or report an error and return NULL. */ struct session * ! cmd_find_session(struct cmd_ctx *ctx, const char *arg, int prefer_unattached) { struct session *s; struct window_pane *wp; --- 747,753 ---- /* Find the target session or report an error and return NULL. */ struct session * ! cmd_find_session(struct cmd_q *cmdq, const char *arg, int prefer_unattached) { struct session *s; struct window_pane *wp; *************** *** 788,800 **** /* A NULL argument means the current session. */ if (arg == NULL) ! return (cmd_current_session(ctx, prefer_unattached)); /* Lookup as pane id or window id. */ if ((wp = cmd_lookup_paneid(arg)) != NULL) ! return (cmd_window_session(ctx, wp->window, NULL)); if ((w = cmd_lookup_windowid(arg)) != NULL) ! return (cmd_window_session(ctx, w, NULL)); /* Trim a single trailing colon if any. */ tmparg = xstrdup(arg); --- 759,771 ---- /* A NULL argument means the current session. */ if (arg == NULL) ! return (cmd_current_session(cmdq, prefer_unattached)); /* Lookup as pane id or window id. */ if ((wp = cmd_lookup_paneid(arg)) != NULL) ! return (cmd_window_session(cmdq, wp->window, NULL)); if ((w = cmd_lookup_windowid(arg)) != NULL) ! return (cmd_window_session(cmdq, w, NULL)); /* Trim a single trailing colon if any. */ tmparg = xstrdup(arg); *************** *** 805,811 **** /* An empty session name is the current session. */ if (*tmparg == '\0') { free(tmparg); ! return (cmd_current_session(ctx, prefer_unattached)); } /* Find the session, if any. */ --- 776,782 ---- /* An empty session name is the current session. */ if (*tmparg == '\0') { free(tmparg); ! return (cmd_current_session(cmdq, prefer_unattached)); } /* Find the session, if any. */ *************** *** 818,826 **** /* If no session found, report an error. */ if (s == NULL) { if (ambiguous) ! ctx->error(ctx, "more than one session: %s", tmparg); else ! ctx->error(ctx, "session not found: %s", tmparg); } free(tmparg); --- 789,797 ---- /* If no session found, report an error. */ if (s == NULL) { if (ambiguous) ! cmdq_error(cmdq, "more than one session: %s", tmparg); else ! cmdq_error(cmdq, "session not found: %s", tmparg); } free(tmparg); *************** *** 829,835 **** /* Find the target session and window or report an error and return NULL. */ struct winlink * ! cmd_find_window(struct cmd_ctx *ctx, const char *arg, struct session **sp) { struct session *s; struct winlink *wl; --- 800,806 ---- /* Find the target session and window or report an error and return NULL. */ struct winlink * ! cmd_find_window(struct cmd_q *cmdq, const char *arg, struct session **sp) { struct session *s; struct winlink *wl; *************** *** 842,849 **** * Find the current session. There must always be a current session, if * it can't be found, report an error. */ ! if ((s = cmd_current_session(ctx, 0)) == NULL) { ! ctx->error(ctx, "can't establish current session"); return (NULL); } --- 813,820 ---- * Find the current session. There must always be a current session, if * it can't be found, report an error. */ ! if ((s = cmd_current_session(cmdq, 0)) == NULL) { ! cmdq_error(cmdq, "can't establish current session"); return (NULL); } *************** *** 856,862 **** /* Lookup as pane id. */ if ((wp = cmd_lookup_paneid(arg)) != NULL) { ! s = cmd_window_session(ctx, wp->window, &wl); if (sp != NULL) *sp = s; return (wl); --- 827,833 ---- /* Lookup as pane id. */ if ((wp = cmd_lookup_paneid(arg)) != NULL) { ! s = cmd_window_session(cmdq, wp->window, &wl); if (sp != NULL) *sp = s; return (wl); *************** *** 937,953 **** no_session: if (ambiguous) ! ctx->error(ctx, "multiple sessions: %s", arg); else ! ctx->error(ctx, "session not found: %s", arg); free(sessptr); return (NULL); not_found: if (ambiguous) ! ctx->error(ctx, "multiple windows: %s", arg); else ! ctx->error(ctx, "window not found: %s", arg); free(sessptr); return (NULL); } --- 908,924 ---- no_session: if (ambiguous) ! cmdq_error(cmdq, "multiple sessions: %s", arg); else ! cmdq_error(cmdq, "session not found: %s", arg); free(sessptr); return (NULL); not_found: if (ambiguous) ! cmdq_error(cmdq, "multiple windows: %s", arg); else ! cmdq_error(cmdq, "window not found: %s", arg); free(sessptr); return (NULL); } *************** *** 979,985 **** * example if it is going to be created). */ int ! cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp) { struct session *s; struct winlink *wl; --- 950,956 ---- * example if it is going to be created). */ int ! cmd_find_index(struct cmd_q *cmdq, const char *arg, struct session **sp) { struct session *s; struct winlink *wl; *************** *** 991,998 **** * Find the current session. There must always be a current session, if * it can't be found, report an error. */ ! if ((s = cmd_current_session(ctx, 0)) == NULL) { ! ctx->error(ctx, "can't establish current session"); return (-2); } --- 962,969 ---- * Find the current session. There must always be a current session, if * it can't be found, report an error. */ ! if ((s = cmd_current_session(cmdq, 0)) == NULL) { ! cmdq_error(cmdq, "can't establish current session"); return (-2); } *************** *** 1075,1099 **** no_session: if (ambiguous) ! ctx->error(ctx, "multiple sessions: %s", arg); else ! ctx->error(ctx, "session not found: %s", arg); free(sessptr); return (-2); invalid_index: if (ambiguous) goto not_found; ! ctx->error(ctx, "invalid index: %s", arg); free(sessptr); return (-2); not_found: if (ambiguous) ! ctx->error(ctx, "multiple windows: %s", arg); else ! ctx->error(ctx, "window not found: %s", arg); free(sessptr); return (-2); } --- 1046,1070 ---- no_session: if (ambiguous) ! cmdq_error(cmdq, "multiple sessions: %s", arg); else ! cmdq_error(cmdq, "session not found: %s", arg); free(sessptr); return (-2); invalid_index: if (ambiguous) goto not_found; ! cmdq_error(cmdq, "invalid index: %s", arg); free(sessptr); return (-2); not_found: if (ambiguous) ! cmdq_error(cmdq, "multiple windows: %s", arg); else ! cmdq_error(cmdq, "window not found: %s", arg); free(sessptr); return (-2); } *************** *** 1130,1136 **** * such as mysession:mywindow.0. */ struct winlink * ! cmd_find_pane(struct cmd_ctx *ctx, const char *arg, struct session **sp, struct window_pane **wpp) { struct session *s; --- 1101,1107 ---- * such as mysession:mywindow.0. */ struct winlink * ! cmd_find_pane(struct cmd_q *cmdq, const char *arg, struct session **sp, struct window_pane **wpp) { struct session *s; *************** *** 1140,1147 **** u_int idx; /* Get the current session. */ ! if ((s = cmd_current_session(ctx, 0)) == NULL) { ! ctx->error(ctx, "can't establish current session"); return (NULL); } if (sp != NULL) --- 1111,1118 ---- u_int idx; /* Get the current session. */ ! if ((s = cmd_current_session(cmdq, 0)) == NULL) { ! cmdq_error(cmdq, "can't establish current session"); return (NULL); } if (sp != NULL) *************** *** 1155,1161 **** /* Lookup as pane id. */ if ((*wpp = cmd_lookup_paneid(arg)) != NULL) { ! s = cmd_window_session(ctx, (*wpp)->window, &wl); if (sp != NULL) *sp = s; return (wl); --- 1126,1132 ---- /* Lookup as pane id. */ if ((*wpp = cmd_lookup_paneid(arg)) != NULL) { ! s = cmd_window_session(cmdq, (*wpp)->window, &wl); if (sp != NULL) *sp = s; return (wl); *************** *** 1170,1176 **** winptr[period - arg] = '\0'; if (*winptr == '\0') wl = s->curw; ! else if ((wl = cmd_find_window(ctx, winptr, sp)) == NULL) goto error; /* Find the pane section and look it up. */ --- 1141,1147 ---- winptr[period - arg] = '\0'; if (*winptr == '\0') wl = s->curw; ! else if ((wl = cmd_find_window(cmdq, winptr, sp)) == NULL) goto error; /* Find the pane section and look it up. */ *************** *** 1194,1200 **** lookup_string: /* Try pane string description. */ if ((*wpp = window_find_string(wl->window, paneptr)) == NULL) { ! ctx->error(ctx, "can't find pane: %s", paneptr); goto error; } --- 1165,1171 ---- lookup_string: /* Try pane string description. */ if ((*wpp = window_find_string(wl->window, paneptr)) == NULL) { ! cmdq_error(cmdq, "can't find pane: %s", paneptr); goto error; } *************** *** 1219,1225 **** return (s->curw); /* Try as a window and use the active pane. */ ! if ((wl = cmd_find_window(ctx, arg, sp)) != NULL) *wpp = wl->window->active; return (wl); --- 1190,1196 ---- return (s->curw); /* Try as a window and use the active pane. */ ! if ((wl = cmd_find_window(cmdq, arg, sp)) != NULL) *wpp = wl->window->active; return (wl); *************** *** 1297,1304 **** * directory. */ const char * ! cmd_get_default_path(struct cmd_ctx *ctx, const char *cwd) { struct session *s; struct environ_entry *envent; const char *root; --- 1268,1276 ---- * directory. */ const char * ! cmd_get_default_path(struct cmd_q *cmdq, const char *cwd) { + struct client *c = cmdq->client; struct session *s; struct environ_entry *envent; const char *root; *************** *** 1308,1314 **** size_t skip; static char path[MAXPATHLEN]; ! if ((s = cmd_current_session(ctx, 0)) == NULL) return (NULL); if (cwd == NULL) --- 1280,1286 ---- size_t skip; static char path[MAXPATHLEN]; ! if ((s = cmd_current_session(cmdq, 0)) == NULL) return (NULL); if (cwd == NULL) *************** *** 1338,1346 **** return (cwd); } else { /* Empty or relative path. */ ! if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL) ! root = ctx->cmdclient->cwd; ! else if (ctx->curclient != NULL && s->curw != NULL) root = get_proc_cwd(s->curw->window->active->fd); else return (s->cwd); --- 1310,1318 ---- return (cwd); } else { /* Empty or relative path. */ ! if (c != NULL && c->session == NULL && c->cwd != NULL) ! root = c->cwd; ! else if (s->curw != NULL) root = get_proc_cwd(s->curw->window->active->fd); else return (s->cwd);