Annotation of src/usr.bin/tmux/cmd.c, Revision 1.91
1.91 ! nicm 1: /* $OpenBSD: cmd.c,v 1.90 2014/01/09 14:20:55 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"
30:
31: const struct cmd_entry *cmd_table[] = {
32: &cmd_attach_session_entry,
33: &cmd_bind_key_entry,
34: &cmd_break_pane_entry,
1.34 nicm 35: &cmd_capture_pane_entry,
1.42 nicm 36: &cmd_choose_buffer_entry,
1.15 nicm 37: &cmd_choose_client_entry,
1.70 nicm 38: &cmd_choose_list_entry,
1.1 nicm 39: &cmd_choose_session_entry,
1.67 nicm 40: &cmd_choose_tree_entry,
1.1 nicm 41: &cmd_choose_window_entry,
42: &cmd_clear_history_entry,
43: &cmd_clock_mode_entry,
44: &cmd_command_prompt_entry,
45: &cmd_confirm_before_entry,
46: &cmd_copy_mode_entry,
47: &cmd_delete_buffer_entry,
48: &cmd_detach_client_entry,
1.7 nicm 49: &cmd_display_message_entry,
1.16 nicm 50: &cmd_display_panes_entry,
1.1 nicm 51: &cmd_find_window_entry,
52: &cmd_has_session_entry,
1.4 nicm 53: &cmd_if_shell_entry,
1.37 nicm 54: &cmd_join_pane_entry,
1.1 nicm 55: &cmd_kill_pane_entry,
56: &cmd_kill_server_entry,
57: &cmd_kill_session_entry,
58: &cmd_kill_window_entry,
1.45 nicm 59: &cmd_last_pane_entry,
1.1 nicm 60: &cmd_last_window_entry,
61: &cmd_link_window_entry,
62: &cmd_list_buffers_entry,
63: &cmd_list_clients_entry,
64: &cmd_list_commands_entry,
65: &cmd_list_keys_entry,
1.23 nicm 66: &cmd_list_panes_entry,
1.1 nicm 67: &cmd_list_sessions_entry,
68: &cmd_list_windows_entry,
69: &cmd_load_buffer_entry,
1.19 nicm 70: &cmd_lock_client_entry,
1.1 nicm 71: &cmd_lock_server_entry,
1.19 nicm 72: &cmd_lock_session_entry,
1.63 nicm 73: &cmd_move_pane_entry,
1.1 nicm 74: &cmd_move_window_entry,
75: &cmd_new_session_entry,
76: &cmd_new_window_entry,
77: &cmd_next_layout_entry,
78: &cmd_next_window_entry,
79: &cmd_paste_buffer_entry,
1.24 nicm 80: &cmd_pipe_pane_entry,
1.1 nicm 81: &cmd_previous_layout_entry,
82: &cmd_previous_window_entry,
83: &cmd_refresh_client_entry,
84: &cmd_rename_session_entry,
85: &cmd_rename_window_entry,
86: &cmd_resize_pane_entry,
1.56 nicm 87: &cmd_respawn_pane_entry,
1.1 nicm 88: &cmd_respawn_window_entry,
89: &cmd_rotate_window_entry,
1.17 nicm 90: &cmd_run_shell_entry,
1.1 nicm 91: &cmd_save_buffer_entry,
92: &cmd_select_layout_entry,
93: &cmd_select_pane_entry,
94: &cmd_select_window_entry,
95: &cmd_send_keys_entry,
96: &cmd_send_prefix_entry,
97: &cmd_server_info_entry,
98: &cmd_set_buffer_entry,
1.13 nicm 99: &cmd_set_environment_entry,
1.1 nicm 100: &cmd_set_option_entry,
101: &cmd_set_window_option_entry,
102: &cmd_show_buffer_entry,
1.13 nicm 103: &cmd_show_environment_entry,
1.32 nicm 104: &cmd_show_messages_entry,
1.1 nicm 105: &cmd_show_options_entry,
106: &cmd_show_window_options_entry,
107: &cmd_source_file_entry,
108: &cmd_split_window_entry,
109: &cmd_start_server_entry,
110: &cmd_suspend_client_entry,
111: &cmd_swap_pane_entry,
112: &cmd_swap_window_entry,
113: &cmd_switch_client_entry,
114: &cmd_unbind_key_entry,
115: &cmd_unlink_window_entry,
1.82 nicm 116: &cmd_wait_for_entry,
1.1 nicm 117: NULL
118: };
119:
1.72 nicm 120: int cmd_session_better(struct session *, struct session *, int);
1.47 nicm 121: struct session *cmd_choose_session_list(struct sessionslist *);
1.52 nicm 122: struct session *cmd_choose_session(int);
1.31 nicm 123: struct client *cmd_choose_client(struct clients *);
1.5 nicm 124: struct client *cmd_lookup_client(const char *);
125: struct session *cmd_lookup_session(const char *, int *);
1.83 nicm 126: struct session *cmd_lookup_session_id(const char *);
1.5 nicm 127: struct winlink *cmd_lookup_window(struct session *, const char *, int *);
1.8 nicm 128: int cmd_lookup_index(struct session *, const char *, int *);
1.60 nicm 129: struct winlink *cmd_lookup_winlink_windowid(struct session *, const char *);
1.80 nicm 130: struct session *cmd_window_session(struct cmd_q *, struct window *,
131: struct winlink **);
1.43 nicm 132: struct winlink *cmd_find_window_offset(const char *, struct session *, int *);
133: int cmd_find_index_offset(const char *, struct session *, int *);
1.51 nicm 134: struct window_pane *cmd_find_pane_offset(const char *, struct winlink *);
1.5 nicm 135:
1.10 nicm 136: int
137: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
138: {
139: size_t arglen;
140: int i;
1.91 ! nicm 141:
! 142: if (argc == 0)
! 143: return (0);
1.10 nicm 144:
145: *buf = '\0';
146: for (i = 0; i < argc; i++) {
147: if (strlcpy(buf, argv[i], len) >= len)
148: return (-1);
149: arglen = strlen(argv[i]) + 1;
150: buf += arglen;
151: len -= arglen;
152: }
153:
154: return (0);
155: }
156:
157: int
158: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
159: {
160: int i;
161: size_t arglen;
162:
163: if (argc == 0)
164: return (0);
165: *argv = xcalloc(argc, sizeof **argv);
166:
167: buf[len - 1] = '\0';
168: for (i = 0; i < argc; i++) {
169: if (len == 0) {
170: cmd_free_argv(argc, *argv);
171: return (-1);
172: }
173:
174: arglen = strlen(buf) + 1;
175: (*argv)[i] = xstrdup(buf);
176: buf += arglen;
177: len -= arglen;
178: }
179:
180: return (0);
1.46 nicm 181: }
182:
183: char **
1.49 nicm 184: cmd_copy_argv(int argc, char *const *argv)
1.46 nicm 185: {
186: char **new_argv;
187: int i;
188:
189: if (argc == 0)
190: return (NULL);
191: new_argv = xcalloc(argc, sizeof *new_argv);
192: for (i = 0; i < argc; i++) {
193: if (argv[i] != NULL)
194: new_argv[i] = xstrdup(argv[i]);
195: }
196: return (new_argv);
1.10 nicm 197: }
198:
199: void
200: cmd_free_argv(int argc, char **argv)
201: {
202: int i;
203:
204: if (argc == 0)
1.35 nicm 205: return;
1.68 nicm 206: for (i = 0; i < argc; i++)
207: free(argv[i]);
208: free(argv);
1.10 nicm 209: }
210:
1.1 nicm 211: struct cmd *
1.80 nicm 212: cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
1.1 nicm 213: {
214: const struct cmd_entry **entryp, *entry;
1.27 deraadt 215: struct cmd *cmd;
1.49 nicm 216: struct args *args;
1.1 nicm 217: char s[BUFSIZ];
1.49 nicm 218: int ambiguous = 0;
1.1 nicm 219:
220: *cause = NULL;
1.2 nicm 221: if (argc == 0) {
222: xasprintf(cause, "no command");
1.1 nicm 223: return (NULL);
1.2 nicm 224: }
1.1 nicm 225:
226: entry = NULL;
227: for (entryp = cmd_table; *entryp != NULL; entryp++) {
228: if ((*entryp)->alias != NULL &&
229: strcmp((*entryp)->alias, argv[0]) == 0) {
1.3 nicm 230: ambiguous = 0;
1.1 nicm 231: entry = *entryp;
232: break;
233: }
234:
235: if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
236: continue;
237: if (entry != NULL)
1.3 nicm 238: ambiguous = 1;
1.1 nicm 239: entry = *entryp;
240:
241: /* Bail now if an exact match. */
242: if (strcmp(entry->name, argv[0]) == 0)
243: break;
244: }
1.3 nicm 245: if (ambiguous)
246: goto ambiguous;
1.1 nicm 247: if (entry == NULL) {
248: xasprintf(cause, "unknown command: %s", argv[0]);
249: return (NULL);
250: }
251:
1.49 nicm 252: args = args_parse(entry->args_template, argc, argv);
253: if (args == NULL)
254: goto usage;
255: if (entry->args_lower != -1 && args->argc < entry->args_lower)
256: goto usage;
257: if (entry->args_upper != -1 && args->argc > entry->args_upper)
258: goto usage;
1.1 nicm 259:
1.80 nicm 260: cmd = xcalloc(1, sizeof *cmd);
1.1 nicm 261: cmd->entry = entry;
1.49 nicm 262: cmd->args = args;
1.80 nicm 263:
264: if (file != NULL)
265: cmd->file = xstrdup(file);
266: cmd->line = line;
267:
1.1 nicm 268: return (cmd);
269:
270: ambiguous:
271: *s = '\0';
272: for (entryp = cmd_table; *entryp != NULL; entryp++) {
273: if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
274: continue;
275: if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
276: break;
277: if (strlcat(s, ", ", sizeof s) >= sizeof s)
278: break;
279: }
280: s[strlen(s) - 2] = '\0';
281: xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
282: return (NULL);
283:
284: usage:
1.49 nicm 285: if (args != NULL)
286: args_free(args);
1.1 nicm 287: xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
288: return (NULL);
289: }
290:
291: size_t
292: cmd_print(struct cmd *cmd, char *buf, size_t len)
293: {
1.49 nicm 294: size_t off, used;
295:
296: off = xsnprintf(buf, len, "%s ", cmd->entry->name);
1.85 nicm 297: if (off + 1 < len) {
298: used = args_print(cmd->args, buf + off, len - off - 1);
1.49 nicm 299: if (used == 0)
1.64 nicm 300: off--;
301: else
1.49 nicm 302: off += used;
1.64 nicm 303: buf[off] = '\0';
1.49 nicm 304: }
305: return (off);
1.1 nicm 306: }
307:
1.5 nicm 308: /*
309: * Figure out the current session. Use: 1) the current session, if the command
1.31 nicm 310: * context has one; 2) the most recently used session containing the pty of the
311: * calling client, if any; 3) the session specified in the TMUX variable from
312: * the environment (as passed from the client); 4) the most recently used
313: * session from all sessions.
1.5 nicm 314: */
1.1 nicm 315: struct session *
1.80 nicm 316: cmd_current_session(struct cmd_q *cmdq, int prefer_unattached)
1.1 nicm 317: {
1.80 nicm 318: struct client *c = cmdq->client;
1.5 nicm 319: struct session *s;
1.47 nicm 320: struct sessionslist ss;
1.11 nicm 321: struct winlink *wl;
322: struct window_pane *wp;
1.80 nicm 323: const char *path;
1.11 nicm 324: int found;
1.1 nicm 325:
1.80 nicm 326: if (c != NULL && c->session != NULL)
327: return (c->session);
1.1 nicm 328:
1.11 nicm 329: /*
1.81 nicm 330: * If the name of the calling client's pty is known, build a list of
331: * the sessions that contain it and if any choose either the first or
332: * the newest.
1.11 nicm 333: */
1.80 nicm 334: path = c == NULL ? NULL : c->tty.path;
335: if (path != NULL) {
1.11 nicm 336: ARRAY_INIT(&ss);
1.47 nicm 337: RB_FOREACH(s, sessions, &sessions) {
1.11 nicm 338: found = 0;
339: RB_FOREACH(wl, winlinks, &s->windows) {
340: TAILQ_FOREACH(wp, &wl->window->panes, entry) {
1.80 nicm 341: if (strcmp(wp->tty, path) == 0) {
1.11 nicm 342: found = 1;
343: break;
344: }
345: }
346: if (found)
347: break;
348: }
349: if (found)
350: ARRAY_ADD(&ss, s);
351: }
352:
1.47 nicm 353: s = cmd_choose_session_list(&ss);
1.11 nicm 354: ARRAY_FREE(&ss);
1.47 nicm 355: if (s != NULL)
356: return (s);
357: }
358:
1.52 nicm 359: return (cmd_choose_session(prefer_unattached));
1.47 nicm 360: }
361:
1.72 nicm 362: /* Is this session better? */
363: int
364: cmd_session_better(struct session *s, struct session *best,
365: int prefer_unattached)
366: {
367: if (best == NULL)
1.74 nicm 368: return (1);
1.72 nicm 369: if (prefer_unattached) {
370: if (!(best->flags & SESSION_UNATTACHED) &&
371: (s->flags & SESSION_UNATTACHED))
1.74 nicm 372: return (1);
1.72 nicm 373: else if ((best->flags & SESSION_UNATTACHED) &&
374: !(s->flags & SESSION_UNATTACHED))
1.74 nicm 375: return (0);
1.72 nicm 376: }
377: return (timercmp(&s->activity_time, &best->activity_time, >));
378: }
379:
1.52 nicm 380: /*
381: * Find the most recently used session, preferring unattached if the flag is
382: * set.
383: */
1.47 nicm 384: struct session *
1.52 nicm 385: cmd_choose_session(int prefer_unattached)
1.47 nicm 386: {
1.72 nicm 387: struct session *s, *best;
1.47 nicm 388:
1.72 nicm 389: best = NULL;
1.47 nicm 390: RB_FOREACH(s, sessions, &sessions) {
1.72 nicm 391: if (cmd_session_better(s, best, prefer_unattached))
392: best = s;
1.47 nicm 393: }
1.72 nicm 394: return (best);
1.5 nicm 395: }
396:
1.31 nicm 397: /* Find the most recently used session from a list. */
1.5 nicm 398: struct session *
1.47 nicm 399: cmd_choose_session_list(struct sessionslist *ss)
1.5 nicm 400: {
1.31 nicm 401: struct session *s, *sbest;
1.5 nicm 402: struct timeval *tv = NULL;
403: u_int i;
404:
1.31 nicm 405: sbest = NULL;
1.11 nicm 406: for (i = 0; i < ARRAY_LENGTH(ss); i++) {
407: if ((s = ARRAY_ITEM(ss, i)) == NULL)
1.5 nicm 408: continue;
409:
1.31 nicm 410: if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
411: sbest = s;
412: tv = &s->activity_time;
1.1 nicm 413: }
414: }
1.5 nicm 415:
1.31 nicm 416: return (sbest);
1.1 nicm 417: }
418:
1.30 nicm 419: /*
420: * Find the current client. First try the current client if set, then pick the
1.31 nicm 421: * most recently used of the clients attached to the current session if any,
422: * then of all clients.
1.30 nicm 423: */
424: struct client *
1.80 nicm 425: cmd_current_client(struct cmd_q *cmdq)
1.30 nicm 426: {
427: struct session *s;
428: struct client *c;
429: struct clients cc;
430: u_int i;
431:
1.80 nicm 432: if (cmdq->client != NULL && cmdq->client->session != NULL)
433: return (cmdq->client);
1.30 nicm 434:
435: /*
436: * No current client set. Find the current session and return the
437: * newest of its clients.
438: */
1.80 nicm 439: s = cmd_current_session(cmdq, 0);
1.30 nicm 440: if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
441: ARRAY_INIT(&cc);
442: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
443: if ((c = ARRAY_ITEM(&clients, i)) == NULL)
444: continue;
445: if (s == c->session)
446: ARRAY_ADD(&cc, c);
447: }
448:
1.31 nicm 449: c = cmd_choose_client(&cc);
1.30 nicm 450: ARRAY_FREE(&cc);
451: if (c != NULL)
452: return (c);
453: }
454:
1.31 nicm 455: return (cmd_choose_client(&clients));
1.30 nicm 456: }
457:
1.31 nicm 458: /* Choose the most recently used client from a list. */
1.20 nicm 459: struct client *
1.31 nicm 460: cmd_choose_client(struct clients *cc)
1.20 nicm 461: {
1.31 nicm 462: struct client *c, *cbest;
1.20 nicm 463: struct timeval *tv = NULL;
464: u_int i;
465:
1.31 nicm 466: cbest = NULL;
1.30 nicm 467: for (i = 0; i < ARRAY_LENGTH(cc); i++) {
468: if ((c = ARRAY_ITEM(cc, i)) == NULL)
1.20 nicm 469: continue;
470: if (c->session == NULL)
471: continue;
472:
1.31 nicm 473: if (tv == NULL || timercmp(&c->activity_time, tv, >)) {
474: cbest = c;
475: tv = &c->activity_time;
1.20 nicm 476: }
477: }
478:
1.31 nicm 479: return (cbest);
1.20 nicm 480: }
481:
1.5 nicm 482: /* Find the target client or report an error and return NULL. */
1.1 nicm 483: struct client *
1.80 nicm 484: cmd_find_client(struct cmd_q *cmdq, const char *arg, int quiet)
1.1 nicm 485: {
1.30 nicm 486: struct client *c;
1.5 nicm 487: char *tmparg;
488: size_t arglen;
1.1 nicm 489:
1.5 nicm 490: /* A NULL argument means the current client. */
1.79 nicm 491: if (arg == NULL) {
1.80 nicm 492: c = cmd_current_client(cmdq);
1.79 nicm 493: if (c == NULL && !quiet)
1.80 nicm 494: cmdq_error(cmdq, "no clients");
1.79 nicm 495: return (c);
496: }
1.5 nicm 497: tmparg = xstrdup(arg);
498:
499: /* Trim a single trailing colon if any. */
500: arglen = strlen(tmparg);
501: if (arglen != 0 && tmparg[arglen - 1] == ':')
502: tmparg[arglen - 1] = '\0';
503:
504: /* Find the client, if any. */
505: c = cmd_lookup_client(tmparg);
506:
507: /* If no client found, report an error. */
1.79 nicm 508: if (c == NULL && !quiet)
1.80 nicm 509: cmdq_error(cmdq, "client not found: %s", tmparg);
1.5 nicm 510:
1.68 nicm 511: free(tmparg);
1.5 nicm 512: return (c);
513: }
514:
515: /*
516: * Lookup a client by device path. Either of a full match and a match without a
517: * leading _PATH_DEV ("/dev/") is accepted.
518: */
519: struct client *
520: cmd_lookup_client(const char *name)
521: {
522: struct client *c;
523: const char *path;
524: u_int i;
525:
526: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1.25 nicm 527: c = ARRAY_ITEM(&clients, i);
1.81 nicm 528: if (c == NULL || c->session == NULL || c->tty.path == NULL)
1.5 nicm 529: continue;
530: path = c->tty.path;
531:
532: /* Check for exact matches. */
533: if (strcmp(name, path) == 0)
534: return (c);
535:
536: /* Check without leading /dev if present. */
537: if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
538: continue;
539: if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
540: return (c);
541: }
542:
543: return (NULL);
544: }
545:
1.83 nicm 546: /* Find the target session or report an error and return NULL. */
547: struct session *
548: cmd_lookup_session_id(const char *arg)
549: {
550: char *endptr;
551: long id;
552:
553: if (arg[0] != '$')
554: return (NULL);
555: id = strtol(arg + 1, &endptr, 10);
556: if (arg[1] != '\0' && *endptr == '\0')
557: return (session_find_by_id(id));
558: return (NULL);
559: }
560:
1.5 nicm 561: /* Lookup a session by name. If no session is found, NULL is returned. */
562: struct session *
563: cmd_lookup_session(const char *name, int *ambiguous)
564: {
565: struct session *s, *sfound;
566:
567: *ambiguous = 0;
1.83 nicm 568:
569: /* Look for $id first. */
570: if ((s = cmd_lookup_session_id(name)) != NULL)
571: return (s);
1.5 nicm 572:
573: /*
1.28 nicm 574: * Look for matches. First look for exact matches - session names must
575: * be unique so an exact match can't be ambigious and can just be
576: * returned.
1.5 nicm 577: */
1.47 nicm 578: if ((s = session_find(name)) != NULL)
579: return (s);
1.28 nicm 580:
581: /*
582: * Otherwise look for partial matches, returning early if it is found to
583: * be ambiguous.
584: */
585: sfound = NULL;
1.47 nicm 586: RB_FOREACH(s, sessions, &sessions) {
1.5 nicm 587: if (strncmp(name, s->name, strlen(name)) == 0 ||
588: fnmatch(name, s->name, 0) == 0) {
589: if (sfound != NULL) {
590: *ambiguous = 1;
591: return (NULL);
592: }
593: sfound = s;
594: }
595: }
1.35 nicm 596: return (sfound);
1.5 nicm 597: }
598:
599: /*
1.8 nicm 600: * Lookup a window or return -1 if not found or ambigious. First try as an
601: * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
1.41 nicm 602: * idx if the window index is a valid number but there is no window with that
1.8 nicm 603: * index.
1.5 nicm 604: */
605: struct winlink *
606: cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
607: {
608: struct winlink *wl, *wlfound;
609: const char *errstr;
610: u_int idx;
611:
612: *ambiguous = 0;
613:
1.60 nicm 614: /* Try as a window id. */
615: if ((wl = cmd_lookup_winlink_windowid(s, name)) != NULL)
616: return (wl);
617:
1.5 nicm 618: /* First see if this is a valid window index in this session. */
619: idx = strtonum(name, 0, INT_MAX, &errstr);
620: if (errstr == NULL) {
621: if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
622: return (wl);
623: }
1.35 nicm 624:
1.5 nicm 625: /* Look for exact matches, error if more than one. */
626: wlfound = NULL;
627: RB_FOREACH(wl, winlinks, &s->windows) {
1.8 nicm 628: if (strcmp(name, wl->window->name) == 0) {
1.5 nicm 629: if (wlfound != NULL) {
630: *ambiguous = 1;
631: return (NULL);
632: }
633: wlfound = wl;
1.1 nicm 634: }
635: }
1.5 nicm 636: if (wlfound != NULL)
637: return (wlfound);
638:
639: /* Now look for pattern matches, again error if multiple. */
640: wlfound = NULL;
641: RB_FOREACH(wl, winlinks, &s->windows) {
1.8 nicm 642: if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
643: fnmatch(name, wl->window->name, 0) == 0) {
1.5 nicm 644: if (wlfound != NULL) {
645: *ambiguous = 1;
646: return (NULL);
647: }
648: wlfound = wl;
649: }
1.35 nicm 650: }
1.5 nicm 651: if (wlfound != NULL)
652: return (wlfound);
653:
654: return (NULL);
1.1 nicm 655: }
656:
1.8 nicm 657: /*
658: * Find a window index - if the window doesn't exist, check if it is a
659: * potential index and return it anyway.
660: */
661: int
662: cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
663: {
664: struct winlink *wl;
665: const char *errstr;
666: u_int idx;
667:
668: if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
669: return (wl->idx);
670: if (*ambiguous)
671: return (-1);
672:
673: idx = strtonum(name, 0, INT_MAX, &errstr);
674: if (errstr == NULL)
675: return (idx);
676:
677: return (-1);
678: }
679:
1.60 nicm 680: /* Lookup pane id. An initial % means a pane id. */
1.51 nicm 681: struct window_pane *
682: cmd_lookup_paneid(const char *arg)
683: {
684: const char *errstr;
685: u_int paneid;
686:
687: if (*arg != '%')
688: return (NULL);
689:
690: paneid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
691: if (errstr != NULL)
692: return (NULL);
693: return (window_pane_find_by_id(paneid));
694: }
695:
1.60 nicm 696: /* Lookup window id in a session. An initial @ means a window id. */
697: struct winlink *
698: cmd_lookup_winlink_windowid(struct session *s, const char *arg)
699: {
700: const char *errstr;
701: u_int windowid;
702:
703: if (*arg != '@')
704: return (NULL);
705:
706: windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
707: if (errstr != NULL)
708: return (NULL);
709: return (winlink_find_by_window_id(&s->windows, windowid));
710: }
711:
712: /* Lookup window id. An initial @ means a window id. */
713: struct window *
714: cmd_lookup_windowid(const char *arg)
715: {
716: const char *errstr;
717: u_int windowid;
718:
719: if (*arg != '@')
720: return (NULL);
721:
722: windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
723: if (errstr != NULL)
724: return (NULL);
725: return (window_find_by_id(windowid));
726: }
727:
728: /* Find session and winlink for window. */
1.51 nicm 729: struct session *
1.80 nicm 730: cmd_window_session(struct cmd_q *cmdq, struct window *w, struct winlink **wlp)
1.51 nicm 731: {
732: struct session *s;
733: struct sessionslist ss;
734: struct winlink *wl;
735:
1.60 nicm 736: /* If this window is in the current session, return that winlink. */
1.80 nicm 737: s = cmd_current_session(cmdq, 0);
1.51 nicm 738: if (s != NULL) {
1.60 nicm 739: wl = winlink_find_by_window(&s->windows, w);
1.51 nicm 740: if (wl != NULL) {
741: if (wlp != NULL)
742: *wlp = wl;
743: return (s);
744: }
745: }
746:
1.60 nicm 747: /* Otherwise choose from all sessions with this window. */
1.51 nicm 748: ARRAY_INIT(&ss);
749: RB_FOREACH(s, sessions, &sessions) {
1.60 nicm 750: if (winlink_find_by_window(&s->windows, w) != NULL)
1.51 nicm 751: ARRAY_ADD(&ss, s);
752: }
753: s = cmd_choose_session_list(&ss);
754: ARRAY_FREE(&ss);
755: if (wlp != NULL)
1.60 nicm 756: *wlp = winlink_find_by_window(&s->windows, w);
1.51 nicm 757: return (s);
758: }
759:
1.5 nicm 760: /* Find the target session or report an error and return NULL. */
1.1 nicm 761: struct session *
1.80 nicm 762: cmd_find_session(struct cmd_q *cmdq, const char *arg, int prefer_unattached)
1.1 nicm 763: {
1.51 nicm 764: struct session *s;
765: struct window_pane *wp;
1.60 nicm 766: struct window *w;
1.51 nicm 767: struct client *c;
768: char *tmparg;
769: size_t arglen;
770: int ambiguous;
1.1 nicm 771:
1.5 nicm 772: /* A NULL argument means the current session. */
1.1 nicm 773: if (arg == NULL)
1.80 nicm 774: return (cmd_current_session(cmdq, prefer_unattached));
1.5 nicm 775:
1.60 nicm 776: /* Lookup as pane id or window id. */
1.51 nicm 777: if ((wp = cmd_lookup_paneid(arg)) != NULL)
1.80 nicm 778: return (cmd_window_session(cmdq, wp->window, NULL));
1.60 nicm 779: if ((w = cmd_lookup_windowid(arg)) != NULL)
1.80 nicm 780: return (cmd_window_session(cmdq, w, NULL));
1.51 nicm 781:
1.5 nicm 782: /* Trim a single trailing colon if any. */
1.54 nicm 783: tmparg = xstrdup(arg);
1.5 nicm 784: arglen = strlen(tmparg);
785: if (arglen != 0 && tmparg[arglen - 1] == ':')
786: tmparg[arglen - 1] = '\0';
787:
1.53 nicm 788: /* An empty session name is the current session. */
789: if (*tmparg == '\0') {
1.68 nicm 790: free(tmparg);
1.80 nicm 791: return (cmd_current_session(cmdq, prefer_unattached));
1.53 nicm 792: }
793:
1.5 nicm 794: /* Find the session, if any. */
795: s = cmd_lookup_session(tmparg, &ambiguous);
796:
797: /* If it doesn't, try to match it as a client. */
798: if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
799: s = c->session;
800:
801: /* If no session found, report an error. */
802: if (s == NULL) {
803: if (ambiguous)
1.80 nicm 804: cmdq_error(cmdq, "more than one session: %s", tmparg);
1.5 nicm 805: else
1.80 nicm 806: cmdq_error(cmdq, "session not found: %s", tmparg);
1.1 nicm 807: }
1.5 nicm 808:
1.68 nicm 809: free(tmparg);
1.1 nicm 810: return (s);
811: }
812:
1.5 nicm 813: /* Find the target session and window or report an error and return NULL. */
1.1 nicm 814: struct winlink *
1.80 nicm 815: cmd_find_window(struct cmd_q *cmdq, const char *arg, struct session **sp)
1.1 nicm 816: {
1.51 nicm 817: struct session *s;
818: struct winlink *wl;
819: struct window_pane *wp;
820: const char *winptr;
821: char *sessptr = NULL;
822: int ambiguous = 0;
1.5 nicm 823:
824: /*
825: * Find the current session. There must always be a current session, if
826: * it can't be found, report an error.
827: */
1.80 nicm 828: if ((s = cmd_current_session(cmdq, 0)) == NULL) {
829: cmdq_error(cmdq, "can't establish current session");
1.5 nicm 830: return (NULL);
831: }
832:
833: /* A NULL argument means the current session and window. */
834: if (arg == NULL) {
835: if (sp != NULL)
836: *sp = s;
837: return (s->curw);
838: }
839:
1.51 nicm 840: /* Lookup as pane id. */
841: if ((wp = cmd_lookup_paneid(arg)) != NULL) {
1.80 nicm 842: s = cmd_window_session(cmdq, wp->window, &wl);
1.51 nicm 843: if (sp != NULL)
844: *sp = s;
845: return (wl);
846: }
847:
1.5 nicm 848: /* Time to look at the argument. If it is empty, that is an error. */
849: if (*arg == '\0')
850: goto not_found;
851:
1.8 nicm 852: /* Find the separating colon and split into window and session. */
1.5 nicm 853: winptr = strchr(arg, ':');
854: if (winptr == NULL)
1.8 nicm 855: goto no_colon;
856: winptr++; /* skip : */
857: sessptr = xstrdup(arg);
858: *strchr(sessptr, ':') = '\0';
1.5 nicm 859:
860: /* Try to lookup the session if present. */
1.8 nicm 861: if (*sessptr != '\0') {
862: if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5 nicm 863: goto no_session;
864: }
865: if (sp != NULL)
866: *sp = s;
867:
868: /*
869: * Then work out the window. An empty string is the current window,
1.38 nicm 870: * otherwise try special cases then to look it up in the session.
1.5 nicm 871: */
1.8 nicm 872: if (*winptr == '\0')
1.5 nicm 873: wl = s->curw;
1.38 nicm 874: else if (winptr[0] == '!' && winptr[1] == '\0')
875: wl = TAILQ_FIRST(&s->lastw);
1.73 nicm 876: else if (winptr[0] == '^' && winptr[1] == '\0')
877: wl = RB_MIN(winlinks, &s->windows);
878: else if (winptr[0] == '$' && winptr[1] == '\0')
879: wl = RB_MAX(winlinks, &s->windows);
1.43 nicm 880: else if (winptr[0] == '+' || winptr[0] == '-')
881: wl = cmd_find_window_offset(winptr, s, &ambiguous);
882: else
1.38 nicm 883: wl = cmd_lookup_window(s, winptr, &ambiguous);
884: if (wl == NULL)
1.5 nicm 885: goto not_found;
1.35 nicm 886:
1.5 nicm 887: if (sessptr != NULL)
1.68 nicm 888: free(sessptr);
1.5 nicm 889: return (wl);
890:
1.8 nicm 891: no_colon:
1.38 nicm 892: /*
893: * No colon in the string, first try special cases, then as a window
894: * and lastly as a session.
895: */
896: if (arg[0] == '!' && arg[1] == '\0') {
897: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
898: goto not_found;
1.41 nicm 899: } else if (arg[0] == '+' || arg[0] == '-') {
1.43 nicm 900: if ((wl = cmd_find_window_offset(arg, s, &ambiguous)) == NULL)
1.41 nicm 901: goto lookup_session;
902: } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL)
903: goto lookup_session;
1.8 nicm 904:
905: if (sp != NULL)
906: *sp = s;
907:
908: return (wl);
909:
1.41 nicm 910: lookup_session:
911: if (ambiguous)
912: goto not_found;
1.53 nicm 913: if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
1.41 nicm 914: goto no_session;
915:
916: if (sp != NULL)
917: *sp = s;
918:
919: return (s->curw);
920:
1.5 nicm 921: no_session:
922: if (ambiguous)
1.80 nicm 923: cmdq_error(cmdq, "multiple sessions: %s", arg);
1.5 nicm 924: else
1.80 nicm 925: cmdq_error(cmdq, "session not found: %s", arg);
1.68 nicm 926: free(sessptr);
1.5 nicm 927: return (NULL);
928:
929: not_found:
930: if (ambiguous)
1.80 nicm 931: cmdq_error(cmdq, "multiple windows: %s", arg);
1.5 nicm 932: else
1.80 nicm 933: cmdq_error(cmdq, "window not found: %s", arg);
1.68 nicm 934: free(sessptr);
1.5 nicm 935: return (NULL);
936: }
1.1 nicm 937:
1.43 nicm 938: struct winlink *
939: cmd_find_window_offset(const char *winptr, struct session *s, int *ambiguous)
940: {
941: struct winlink *wl;
942: int offset = 1;
943:
944: if (winptr[1] != '\0')
945: offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
946: if (offset == 0)
947: wl = cmd_lookup_window(s, winptr, ambiguous);
948: else {
949: if (winptr[0] == '+')
950: wl = winlink_next_by_number(s->curw, s, offset);
951: else
952: wl = winlink_previous_by_number(s->curw, s, offset);
953: }
954:
955: return (wl);
956: }
957:
1.5 nicm 958: /*
959: * Find the target session and window index, whether or not it exists in the
960: * session. Return -2 on error or -1 if no window index is specified. This is
1.8 nicm 961: * used when parsing an argument for a window target that may not exist (for
962: * example if it is going to be created).
1.5 nicm 963: */
964: int
1.80 nicm 965: cmd_find_index(struct cmd_q *cmdq, const char *arg, struct session **sp)
1.5 nicm 966: {
967: struct session *s;
1.38 nicm 968: struct winlink *wl;
1.8 nicm 969: const char *winptr;
1.5 nicm 970: char *sessptr = NULL;
971: int idx, ambiguous = 0;
972:
973: /*
974: * Find the current session. There must always be a current session, if
975: * it can't be found, report an error.
976: */
1.80 nicm 977: if ((s = cmd_current_session(cmdq, 0)) == NULL) {
978: cmdq_error(cmdq, "can't establish current session");
1.9 nicm 979: return (-2);
1.1 nicm 980: }
1.5 nicm 981:
982: /* A NULL argument means the current session and "no window" (-1). */
983: if (arg == NULL) {
984: if (sp != NULL)
985: *sp = s;
986: return (-1);
987: }
988:
989: /* Time to look at the argument. If it is empty, that is an error. */
990: if (*arg == '\0')
991: goto not_found;
992:
993: /* Find the separating colon. If none, assume the current session. */
994: winptr = strchr(arg, ':');
995: if (winptr == NULL)
1.8 nicm 996: goto no_colon;
997: winptr++; /* skip : */
998: sessptr = xstrdup(arg);
999: *strchr(sessptr, ':') = '\0';
1.5 nicm 1000:
1001: /* Try to lookup the session if present. */
1002: if (sessptr != NULL && *sessptr != '\0') {
1.8 nicm 1003: if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5 nicm 1004: goto no_session;
1005: }
1.1 nicm 1006: if (sp != NULL)
1007: *sp = s;
1008:
1.5 nicm 1009: /*
1.8 nicm 1010: * Then work out the window. An empty string is a new window otherwise
1011: * try to look it up in the session.
1.5 nicm 1012: */
1.8 nicm 1013: if (*winptr == '\0')
1.38 nicm 1014: idx = -1;
1015: else if (winptr[0] == '!' && winptr[1] == '\0') {
1016: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1017: goto not_found;
1018: idx = wl->idx;
1.41 nicm 1019: } else if (winptr[0] == '+' || winptr[0] == '-') {
1.43 nicm 1020: if ((idx = cmd_find_index_offset(winptr, s, &ambiguous)) < 0)
1.41 nicm 1021: goto invalid_index;
1022: } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
1023: goto invalid_index;
1.35 nicm 1024:
1.68 nicm 1025: free(sessptr);
1.5 nicm 1026: return (idx);
1027:
1.8 nicm 1028: no_colon:
1.38 nicm 1029: /*
1030: * No colon in the string, first try special cases, then as a window
1031: * and lastly as a session.
1032: */
1033: if (arg[0] == '!' && arg[1] == '\0') {
1034: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1035: goto not_found;
1036: idx = wl->idx;
1.41 nicm 1037: } else if (arg[0] == '+' || arg[0] == '-') {
1.43 nicm 1038: if ((idx = cmd_find_index_offset(arg, s, &ambiguous)) < 0)
1.41 nicm 1039: goto lookup_session;
1040: } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1)
1041: goto lookup_session;
1.8 nicm 1042:
1043: if (sp != NULL)
1044: *sp = s;
1045:
1046: return (idx);
1047:
1.41 nicm 1048: lookup_session:
1049: if (ambiguous)
1050: goto not_found;
1.53 nicm 1051: if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
1.41 nicm 1052: goto no_session;
1053:
1054: if (sp != NULL)
1055: *sp = s;
1056:
1057: return (-1);
1058:
1.5 nicm 1059: no_session:
1060: if (ambiguous)
1.80 nicm 1061: cmdq_error(cmdq, "multiple sessions: %s", arg);
1.5 nicm 1062: else
1.80 nicm 1063: cmdq_error(cmdq, "session not found: %s", arg);
1.68 nicm 1064: free(sessptr);
1.5 nicm 1065: return (-2);
1066:
1.41 nicm 1067: invalid_index:
1068: if (ambiguous)
1069: goto not_found;
1.80 nicm 1070: cmdq_error(cmdq, "invalid index: %s", arg);
1.41 nicm 1071:
1.68 nicm 1072: free(sessptr);
1.41 nicm 1073: return (-2);
1074:
1.5 nicm 1075: not_found:
1076: if (ambiguous)
1.80 nicm 1077: cmdq_error(cmdq, "multiple windows: %s", arg);
1.1 nicm 1078: else
1.80 nicm 1079: cmdq_error(cmdq, "window not found: %s", arg);
1.68 nicm 1080: free(sessptr);
1.5 nicm 1081: return (-2);
1.12 nicm 1082: }
1083:
1.43 nicm 1084: int
1085: cmd_find_index_offset(const char *winptr, struct session *s, int *ambiguous)
1086: {
1087: int idx, offset = 1;
1088:
1089: if (winptr[1] != '\0')
1090: offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
1091: if (offset == 0)
1092: idx = cmd_lookup_index(s, winptr, ambiguous);
1093: else {
1094: if (winptr[0] == '+') {
1095: if (s->curw->idx == INT_MAX)
1096: idx = cmd_lookup_index(s, winptr, ambiguous);
1097: else
1098: idx = s->curw->idx + offset;
1099: } else {
1100: if (s->curw->idx == 0)
1101: idx = cmd_lookup_index(s, winptr, ambiguous);
1102: else
1103: idx = s->curw->idx - offset;
1104: }
1105: }
1106:
1107: return (idx);
1108: }
1109:
1.12 nicm 1110: /*
1111: * Find the target session, window and pane number or report an error and
1112: * return NULL. The pane number is separated from the session:window by a .,
1113: * such as mysession:mywindow.0.
1114: */
1115: struct winlink *
1.80 nicm 1116: cmd_find_pane(struct cmd_q *cmdq,
1.12 nicm 1117: const char *arg, struct session **sp, struct window_pane **wpp)
1118: {
1.55 nicm 1119: struct session *s;
1120: struct winlink *wl;
1121: const char *period, *errstr;
1122: char *winptr, *paneptr;
1123: u_int idx;
1.12 nicm 1124:
1125: /* Get the current session. */
1.80 nicm 1126: if ((s = cmd_current_session(cmdq, 0)) == NULL) {
1127: cmdq_error(cmdq, "can't establish current session");
1.12 nicm 1128: return (NULL);
1129: }
1130: if (sp != NULL)
1131: *sp = s;
1132:
1133: /* A NULL argument means the current session, window and pane. */
1134: if (arg == NULL) {
1135: *wpp = s->curw->window->active;
1136: return (s->curw);
1.51 nicm 1137: }
1138:
1139: /* Lookup as pane id. */
1140: if ((*wpp = cmd_lookup_paneid(arg)) != NULL) {
1.80 nicm 1141: s = cmd_window_session(cmdq, (*wpp)->window, &wl);
1.51 nicm 1142: if (sp != NULL)
1143: *sp = s;
1144: return (wl);
1.12 nicm 1145: }
1146:
1147: /* Look for a separating period. */
1148: if ((period = strrchr(arg, '.')) == NULL)
1149: goto no_period;
1150:
1151: /* Pull out the window part and parse it. */
1152: winptr = xstrdup(arg);
1153: winptr[period - arg] = '\0';
1154: if (*winptr == '\0')
1155: wl = s->curw;
1.80 nicm 1156: else if ((wl = cmd_find_window(cmdq, winptr, sp)) == NULL)
1.12 nicm 1157: goto error;
1158:
1159: /* Find the pane section and look it up. */
1160: paneptr = winptr + (period - arg) + 1;
1161: if (*paneptr == '\0')
1162: *wpp = wl->window->active;
1.43 nicm 1163: else if (paneptr[0] == '+' || paneptr[0] == '-')
1164: *wpp = cmd_find_pane_offset(paneptr, wl);
1165: else {
1.12 nicm 1166: idx = strtonum(paneptr, 0, INT_MAX, &errstr);
1.36 nicm 1167: if (errstr != NULL)
1168: goto lookup_string;
1.12 nicm 1169: *wpp = window_pane_at_index(wl->window, idx);
1.36 nicm 1170: if (*wpp == NULL)
1171: goto lookup_string;
1.12 nicm 1172: }
1173:
1.68 nicm 1174: free(winptr);
1.12 nicm 1175: return (wl);
1176:
1.36 nicm 1177: lookup_string:
1178: /* Try pane string description. */
1.55 nicm 1179: if ((*wpp = window_find_string(wl->window, paneptr)) == NULL) {
1.80 nicm 1180: cmdq_error(cmdq, "can't find pane: %s", paneptr);
1.36 nicm 1181: goto error;
1182: }
1183:
1.68 nicm 1184: free(winptr);
1.39 nicm 1185: return (wl);
1.36 nicm 1186:
1.12 nicm 1187: no_period:
1188: /* Try as a pane number alone. */
1189: idx = strtonum(arg, 0, INT_MAX, &errstr);
1190: if (errstr != NULL)
1191: goto lookup_window;
1192:
1193: /* Try index in the current session and window. */
1194: if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
1195: goto lookup_window;
1.35 nicm 1196:
1.12 nicm 1197: return (s->curw);
1198:
1199: lookup_window:
1.36 nicm 1200: /* Try pane string description. */
1.55 nicm 1201: if ((*wpp = window_find_string(s->curw->window, arg)) != NULL)
1.36 nicm 1202: return (s->curw);
1203:
1.12 nicm 1204: /* Try as a window and use the active pane. */
1.80 nicm 1205: if ((wl = cmd_find_window(cmdq, arg, sp)) != NULL)
1.12 nicm 1206: *wpp = wl->window->active;
1207: return (wl);
1.35 nicm 1208:
1.12 nicm 1209: error:
1.68 nicm 1210: free(winptr);
1.12 nicm 1211: return (NULL);
1.43 nicm 1212: }
1213:
1214: struct window_pane *
1215: cmd_find_pane_offset(const char *paneptr, struct winlink *wl)
1216: {
1217: struct window *w = wl->window;
1218: struct window_pane *wp = w->active;
1219: u_int offset = 1;
1220:
1221: if (paneptr[1] != '\0')
1222: offset = strtonum(paneptr + 1, 1, INT_MAX, NULL);
1223: if (offset > 0) {
1224: if (paneptr[0] == '+')
1225: wp = window_pane_next_by_number(w, wp, offset);
1226: else
1227: wp = window_pane_previous_by_number(w, wp, offset);
1228: }
1229:
1230: return (wp);
1.15 nicm 1231: }
1232:
1233: /* Replace the first %% or %idx in template by s. */
1234: char *
1.75 nicm 1235: cmd_template_replace(const char *template, const char *s, int idx)
1.15 nicm 1236: {
1.75 nicm 1237: char ch, *buf;
1238: const char *ptr;
1.84 nicm 1239: int replaced;
1240: size_t len;
1.15 nicm 1241:
1.76 nicm 1242: if (strchr(template, '%') == NULL)
1.15 nicm 1243: return (xstrdup(template));
1244:
1245: buf = xmalloc(1);
1246: *buf = '\0';
1247: len = 0;
1248: replaced = 0;
1249:
1250: ptr = template;
1251: while (*ptr != '\0') {
1252: switch (ch = *ptr++) {
1253: case '%':
1254: if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
1255: if (*ptr != '%' || replaced)
1256: break;
1257: replaced = 1;
1258: }
1259: ptr++;
1260:
1261: len += strlen(s);
1262: buf = xrealloc(buf, 1, len + 1);
1263: strlcat(buf, s, len + 1);
1264: continue;
1265: }
1266: buf = xrealloc(buf, 1, len + 2);
1267: buf[len++] = ch;
1268: buf[len] = '\0';
1269: }
1270:
1271: return (buf);
1.1 nicm 1272: }