Annotation of src/usr.bin/tmux/cmd.c, Revision 1.149
1.149 ! nicm 1: /* $OpenBSD: cmd.c,v 1.148 2019/05/10 18:04:06 nicm Exp $ */
1.1 nicm 2:
3: /*
1.120 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20: #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_tree_entry;
38: extern const struct cmd_entry cmd_clear_history_entry;
39: extern const struct cmd_entry cmd_clock_mode_entry;
40: extern const struct cmd_entry cmd_command_prompt_entry;
41: extern const struct cmd_entry cmd_confirm_before_entry;
42: extern const struct cmd_entry cmd_copy_mode_entry;
43: extern const struct cmd_entry cmd_delete_buffer_entry;
44: extern const struct cmd_entry cmd_detach_client_entry;
1.148 nicm 45: extern const struct cmd_entry cmd_display_menu_entry;
1.105 nicm 46: extern const struct cmd_entry cmd_display_message_entry;
47: extern const struct cmd_entry cmd_display_panes_entry;
48: extern const struct cmd_entry cmd_down_pane_entry;
49: extern const struct cmd_entry cmd_find_window_entry;
50: extern const struct cmd_entry cmd_has_session_entry;
51: extern const struct cmd_entry cmd_if_shell_entry;
52: extern const struct cmd_entry cmd_join_pane_entry;
53: extern const struct cmd_entry cmd_kill_pane_entry;
54: extern const struct cmd_entry cmd_kill_server_entry;
55: extern const struct cmd_entry cmd_kill_session_entry;
56: extern const struct cmd_entry cmd_kill_window_entry;
57: extern const struct cmd_entry cmd_last_pane_entry;
58: extern const struct cmd_entry cmd_last_window_entry;
59: extern const struct cmd_entry cmd_link_window_entry;
60: extern const struct cmd_entry cmd_list_buffers_entry;
61: extern const struct cmd_entry cmd_list_clients_entry;
62: extern const struct cmd_entry cmd_list_commands_entry;
63: extern const struct cmd_entry cmd_list_keys_entry;
64: extern const struct cmd_entry cmd_list_panes_entry;
65: extern const struct cmd_entry cmd_list_sessions_entry;
66: extern const struct cmd_entry cmd_list_windows_entry;
67: extern const struct cmd_entry cmd_load_buffer_entry;
68: extern const struct cmd_entry cmd_lock_client_entry;
69: extern const struct cmd_entry cmd_lock_server_entry;
70: extern const struct cmd_entry cmd_lock_session_entry;
71: extern const struct cmd_entry cmd_move_pane_entry;
72: extern const struct cmd_entry cmd_move_window_entry;
73: extern const struct cmd_entry cmd_new_session_entry;
74: extern const struct cmd_entry cmd_new_window_entry;
75: extern const struct cmd_entry cmd_next_layout_entry;
76: extern const struct cmd_entry cmd_next_window_entry;
77: extern const struct cmd_entry cmd_paste_buffer_entry;
78: extern const struct cmd_entry cmd_pipe_pane_entry;
79: extern const struct cmd_entry cmd_previous_layout_entry;
80: extern const struct cmd_entry cmd_previous_window_entry;
81: extern const struct cmd_entry cmd_refresh_client_entry;
82: extern const struct cmd_entry cmd_rename_session_entry;
83: extern const struct cmd_entry cmd_rename_window_entry;
84: extern const struct cmd_entry cmd_resize_pane_entry;
1.141 nicm 85: extern const struct cmd_entry cmd_resize_window_entry;
1.105 nicm 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_set_buffer_entry;
97: extern const struct cmd_entry cmd_set_environment_entry;
1.107 nicm 98: extern const struct cmd_entry cmd_set_hook_entry;
1.105 nicm 99: extern const struct cmd_entry cmd_set_option_entry;
100: extern const struct cmd_entry cmd_set_window_option_entry;
101: extern const struct cmd_entry cmd_show_buffer_entry;
102: extern const struct cmd_entry cmd_show_environment_entry;
1.107 nicm 103: extern const struct cmd_entry cmd_show_hooks_entry;
1.105 nicm 104: extern const struct cmd_entry cmd_show_messages_entry;
105: extern const struct cmd_entry cmd_show_options_entry;
106: extern const struct cmd_entry cmd_show_window_options_entry;
107: extern const struct cmd_entry cmd_source_file_entry;
108: extern const struct cmd_entry cmd_split_window_entry;
109: extern const struct cmd_entry cmd_start_server_entry;
110: extern const struct cmd_entry cmd_suspend_client_entry;
111: extern const struct cmd_entry cmd_swap_pane_entry;
112: extern const struct cmd_entry cmd_swap_window_entry;
113: extern const struct cmd_entry cmd_switch_client_entry;
114: extern const struct cmd_entry cmd_unbind_key_entry;
115: extern const struct cmd_entry cmd_unlink_window_entry;
116: extern const struct cmd_entry cmd_up_pane_entry;
117: extern const struct cmd_entry cmd_wait_for_entry;
1.1 nicm 118:
119: const struct cmd_entry *cmd_table[] = {
120: &cmd_attach_session_entry,
121: &cmd_bind_key_entry,
122: &cmd_break_pane_entry,
1.34 nicm 123: &cmd_capture_pane_entry,
1.42 nicm 124: &cmd_choose_buffer_entry,
1.15 nicm 125: &cmd_choose_client_entry,
1.67 nicm 126: &cmd_choose_tree_entry,
1.1 nicm 127: &cmd_clear_history_entry,
128: &cmd_clock_mode_entry,
129: &cmd_command_prompt_entry,
130: &cmd_confirm_before_entry,
131: &cmd_copy_mode_entry,
132: &cmd_delete_buffer_entry,
133: &cmd_detach_client_entry,
1.148 nicm 134: &cmd_display_menu_entry,
1.7 nicm 135: &cmd_display_message_entry,
1.16 nicm 136: &cmd_display_panes_entry,
1.1 nicm 137: &cmd_find_window_entry,
138: &cmd_has_session_entry,
1.4 nicm 139: &cmd_if_shell_entry,
1.37 nicm 140: &cmd_join_pane_entry,
1.1 nicm 141: &cmd_kill_pane_entry,
142: &cmd_kill_server_entry,
143: &cmd_kill_session_entry,
144: &cmd_kill_window_entry,
1.45 nicm 145: &cmd_last_pane_entry,
1.1 nicm 146: &cmd_last_window_entry,
147: &cmd_link_window_entry,
148: &cmd_list_buffers_entry,
149: &cmd_list_clients_entry,
150: &cmd_list_commands_entry,
151: &cmd_list_keys_entry,
1.23 nicm 152: &cmd_list_panes_entry,
1.1 nicm 153: &cmd_list_sessions_entry,
154: &cmd_list_windows_entry,
155: &cmd_load_buffer_entry,
1.19 nicm 156: &cmd_lock_client_entry,
1.1 nicm 157: &cmd_lock_server_entry,
1.19 nicm 158: &cmd_lock_session_entry,
1.63 nicm 159: &cmd_move_pane_entry,
1.1 nicm 160: &cmd_move_window_entry,
161: &cmd_new_session_entry,
162: &cmd_new_window_entry,
163: &cmd_next_layout_entry,
164: &cmd_next_window_entry,
165: &cmd_paste_buffer_entry,
1.24 nicm 166: &cmd_pipe_pane_entry,
1.1 nicm 167: &cmd_previous_layout_entry,
168: &cmd_previous_window_entry,
169: &cmd_refresh_client_entry,
170: &cmd_rename_session_entry,
171: &cmd_rename_window_entry,
172: &cmd_resize_pane_entry,
1.141 nicm 173: &cmd_resize_window_entry,
1.56 nicm 174: &cmd_respawn_pane_entry,
1.1 nicm 175: &cmd_respawn_window_entry,
176: &cmd_rotate_window_entry,
1.17 nicm 177: &cmd_run_shell_entry,
1.1 nicm 178: &cmd_save_buffer_entry,
179: &cmd_select_layout_entry,
180: &cmd_select_pane_entry,
181: &cmd_select_window_entry,
182: &cmd_send_keys_entry,
183: &cmd_send_prefix_entry,
184: &cmd_set_buffer_entry,
1.13 nicm 185: &cmd_set_environment_entry,
1.107 nicm 186: &cmd_set_hook_entry,
1.1 nicm 187: &cmd_set_option_entry,
188: &cmd_set_window_option_entry,
189: &cmd_show_buffer_entry,
1.13 nicm 190: &cmd_show_environment_entry,
1.107 nicm 191: &cmd_show_hooks_entry,
1.32 nicm 192: &cmd_show_messages_entry,
1.1 nicm 193: &cmd_show_options_entry,
194: &cmd_show_window_options_entry,
195: &cmd_source_file_entry,
196: &cmd_split_window_entry,
197: &cmd_start_server_entry,
198: &cmd_suspend_client_entry,
199: &cmd_swap_pane_entry,
200: &cmd_swap_window_entry,
201: &cmd_switch_client_entry,
202: &cmd_unbind_key_entry,
203: &cmd_unlink_window_entry,
1.82 nicm 204: &cmd_wait_for_entry,
1.1 nicm 205: NULL
206: };
207:
1.140 nicm 208: void
209: cmd_log_argv(int argc, char **argv, const char *prefix)
210: {
211: int i;
212:
213: for (i = 0; i < argc; i++)
214: log_debug("%s: argv[%d]=%s", prefix, i, argv[i]);
215: }
216:
1.149 ! nicm 217: void
! 218: cmd_prepend_argv(int *argc, char ***argv, char *arg)
! 219: {
! 220: char **new_argv;
! 221: int i;
! 222:
! 223: new_argv = xreallocarray(NULL, (*argc) + 1, sizeof *new_argv);
! 224: new_argv[0] = xstrdup(arg);
! 225: for (i = 0; i < *argc; i++)
! 226: new_argv[1 + i] = (*argv)[i];
! 227:
! 228: free(*argv);
! 229: *argv = new_argv;
! 230: (*argc)++;
! 231: }
! 232:
! 233: void
! 234: cmd_append_argv(int *argc, char ***argv, char *arg)
! 235: {
! 236: *argv = xreallocarray(*argv, (*argc) + 1, sizeof **argv);
! 237: (*argv)[(*argc)++] = xstrdup(arg);
! 238: }
! 239:
1.10 nicm 240: int
241: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
242: {
243: size_t arglen;
244: int i;
1.91 nicm 245:
246: if (argc == 0)
247: return (0);
1.140 nicm 248: cmd_log_argv(argc, argv, __func__);
1.10 nicm 249:
250: *buf = '\0';
251: for (i = 0; i < argc; i++) {
252: if (strlcpy(buf, argv[i], len) >= len)
253: return (-1);
254: arglen = strlen(argv[i]) + 1;
255: buf += arglen;
256: len -= arglen;
257: }
258:
259: return (0);
260: }
261:
262: int
263: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
264: {
265: int i;
266: size_t arglen;
267:
268: if (argc == 0)
269: return (0);
270: *argv = xcalloc(argc, sizeof **argv);
271:
272: buf[len - 1] = '\0';
273: for (i = 0; i < argc; i++) {
274: if (len == 0) {
275: cmd_free_argv(argc, *argv);
276: return (-1);
277: }
278:
279: arglen = strlen(buf) + 1;
280: (*argv)[i] = xstrdup(buf);
1.140 nicm 281:
1.10 nicm 282: buf += arglen;
283: len -= arglen;
284: }
1.140 nicm 285: cmd_log_argv(argc, *argv, __func__);
1.10 nicm 286:
287: return (0);
1.46 nicm 288: }
289:
290: char **
1.93 nicm 291: cmd_copy_argv(int argc, char **argv)
1.46 nicm 292: {
293: char **new_argv;
294: int i;
295:
296: if (argc == 0)
297: return (NULL);
1.94 nicm 298: new_argv = xcalloc(argc + 1, sizeof *new_argv);
1.46 nicm 299: for (i = 0; i < argc; i++) {
300: if (argv[i] != NULL)
301: new_argv[i] = xstrdup(argv[i]);
302: }
303: return (new_argv);
1.10 nicm 304: }
305:
306: void
307: cmd_free_argv(int argc, char **argv)
308: {
309: int i;
310:
311: if (argc == 0)
1.35 nicm 312: return;
1.68 nicm 313: for (i = 0; i < argc; i++)
314: free(argv[i]);
315: free(argv);
1.94 nicm 316: }
317:
318: char *
319: cmd_stringify_argv(int argc, char **argv)
320: {
321: char *buf;
322: int i;
323: size_t len;
324:
325: if (argc == 0)
326: return (xstrdup(""));
327:
328: len = 0;
329: buf = NULL;
330:
331: for (i = 0; i < argc; i++) {
332: len += strlen(argv[i]) + 1;
1.98 nicm 333: buf = xrealloc(buf, len);
1.94 nicm 334:
335: if (i == 0)
336: *buf = '\0';
337: else
338: strlcat(buf, " ", len);
339: strlcat(buf, argv[i], len);
340: }
341: return (buf);
1.10 nicm 342: }
343:
1.149 ! nicm 344: char *
! 345: cmd_get_alias(const char *name)
1.135 nicm 346: {
1.149 ! nicm 347: struct options_entry *o;
! 348: struct options_array_item *a;
! 349: union options_value *ov;
! 350: size_t wanted, n;
! 351: const char *equals;
1.135 nicm 352:
353: o = options_get_only(global_options, "command-alias");
1.142 nicm 354: if (o == NULL)
1.149 ! nicm 355: return (NULL);
! 356: wanted = strlen(name);
1.135 nicm 357:
1.142 nicm 358: a = options_array_first(o);
359: while (a != NULL) {
1.143 nicm 360: ov = options_array_item_value(a);
1.149 ! nicm 361:
! 362: equals = strchr(ov->string, '=');
! 363: if (equals != NULL) {
! 364: n = equals - ov->string;
! 365: if (n == wanted && strncmp(name, ov->string, n) == 0)
! 366: return (xstrdup(equals + 1));
! 367: }
! 368:
1.142 nicm 369: a = options_array_next(a);
1.135 nicm 370: }
1.149 ! nicm 371: return (NULL);
! 372: }
1.135 nicm 373:
1.149 ! nicm 374: static const struct cmd_entry *
! 375: cmd_find(const char *name, char **cause)
! 376: {
! 377: const struct cmd_entry **loop, *entry, *found = NULL;
! 378: int ambiguous;
! 379: char s[BUFSIZ];
1.135 nicm 380:
1.149 ! nicm 381: ambiguous = 0;
! 382: for (loop = cmd_table; *loop != NULL; loop++) {
! 383: entry = *loop;
! 384: if (entry->alias != NULL && strcmp(entry->alias, name) == 0) {
! 385: ambiguous = 0;
! 386: found = entry;
! 387: break;
! 388: }
1.135 nicm 389:
1.149 ! nicm 390: if (strncmp(entry->name, name, strlen(name)) != 0)
! 391: continue;
! 392: if (found != NULL)
! 393: ambiguous = 1;
! 394: found = entry;
1.135 nicm 395:
1.149 ! nicm 396: if (strcmp(entry->name, name) == 0)
! 397: break;
! 398: }
! 399: if (ambiguous)
! 400: goto ambiguous;
! 401: if (found == NULL) {
! 402: xasprintf(cause, "unknown command: %s", name);
! 403: return (NULL);
! 404: }
! 405: return (found);
1.135 nicm 406:
1.149 ! nicm 407: ambiguous:
! 408: *s = '\0';
! 409: for (loop = cmd_table; *loop != NULL; loop++) {
! 410: entry = *loop;
! 411: if (strncmp(entry->name, name, strlen(name)) != 0)
! 412: continue;
! 413: if (strlcat(s, entry->name, sizeof s) >= sizeof s)
! 414: break;
! 415: if (strlcat(s, ", ", sizeof s) >= sizeof s)
! 416: break;
! 417: }
! 418: s[strlen(s) - 2] = '\0';
! 419: xasprintf(cause, "ambiguous command: %s, could be: %s", name, s);
! 420: return (NULL);
1.135 nicm 421: }
422:
1.1 nicm 423: struct cmd *
1.80 nicm 424: cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
1.1 nicm 425: {
1.149 ! nicm 426: const struct cmd_entry *entry;
1.135 nicm 427: const char *name;
1.27 deraadt 428: struct cmd *cmd;
1.49 nicm 429: struct args *args;
1.1 nicm 430:
1.2 nicm 431: if (argc == 0) {
432: xasprintf(cause, "no command");
1.1 nicm 433: return (NULL);
1.2 nicm 434: }
1.135 nicm 435: name = argv[0];
1.1 nicm 436:
1.149 ! nicm 437: entry = cmd_find(name, cause);
! 438: if (entry == NULL)
1.1 nicm 439: return (NULL);
1.140 nicm 440: cmd_log_argv(argc, argv, entry->name);
1.1 nicm 441:
1.117 nicm 442: args = args_parse(entry->args.template, argc, argv);
1.49 nicm 443: if (args == NULL)
444: goto usage;
1.117 nicm 445: if (entry->args.lower != -1 && args->argc < entry->args.lower)
1.49 nicm 446: goto usage;
1.117 nicm 447: if (entry->args.upper != -1 && args->argc > entry->args.upper)
1.49 nicm 448: goto usage;
1.1 nicm 449:
1.80 nicm 450: cmd = xcalloc(1, sizeof *cmd);
1.1 nicm 451: cmd->entry = entry;
1.49 nicm 452: cmd->args = args;
1.80 nicm 453:
454: if (file != NULL)
455: cmd->file = xstrdup(file);
456: cmd->line = line;
457:
1.149 ! nicm 458: cmd->alias = NULL;
! 459: cmd->argc = argc;
! 460: cmd->argv = cmd_copy_argv(argc, argv);
! 461:
1.1 nicm 462: return (cmd);
463:
464: usage:
1.49 nicm 465: if (args != NULL)
466: args_free(args);
1.1 nicm 467: xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
468: return (NULL);
1.149 ! nicm 469: }
! 470:
! 471: void
! 472: cmd_free(struct cmd *cmd)
! 473: {
! 474: free(cmd->alias);
! 475: cmd_free_argv(cmd->argc, cmd->argv);
! 476:
! 477: free(cmd->file);
! 478:
! 479: args_free(cmd->args);
! 480: free(cmd);
1.1 nicm 481: }
482:
1.106 nicm 483: char *
484: cmd_print(struct cmd *cmd)
1.1 nicm 485: {
1.106 nicm 486: char *out, *s;
487:
488: s = args_print(cmd->args);
489: if (*s != '\0')
490: xasprintf(&out, "%s %s", cmd->entry->name, s);
491: else
492: out = xstrdup(cmd->entry->name);
493: free(s);
1.49 nicm 494:
1.106 nicm 495: return (out);
1.1 nicm 496: }
497:
1.99 nicm 498: /* Adjust current mouse position for a pane. */
499: int
500: cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
501: u_int *yp, int last)
502: {
503: u_int x, y;
504:
505: if (last) {
1.145 nicm 506: x = m->lx + m->ox;
507: y = m->ly + m->oy;
1.99 nicm 508: } else {
1.146 nicm 509: x = m->x + m->ox;
510: y = m->y + m->oy;
1.99 nicm 511: }
1.145 nicm 512: log_debug("%s: x=%u, y=%u%s", __func__, x, y, last ? " (last)" : "");
1.99 nicm 513:
514: if (m->statusat == 0 && y > 0)
515: y--;
516:
517: if (x < wp->xoff || x >= wp->xoff + wp->sx)
518: return (-1);
519: if (y < wp->yoff || y >= wp->yoff + wp->sy)
520: return (-1);
521:
1.123 nicm 522: if (xp != NULL)
523: *xp = x - wp->xoff;
524: if (yp != NULL)
525: *yp = y - wp->yoff;
1.99 nicm 526: return (0);
527: }
528:
529: /* Get current mouse window if any. */
530: struct winlink *
531: cmd_mouse_window(struct mouse_event *m, struct session **sp)
532: {
533: struct session *s;
534: struct window *w;
1.147 nicm 535: struct winlink *wl;
1.99 nicm 536:
1.147 nicm 537: if (!m->valid)
1.99 nicm 538: return (NULL);
1.147 nicm 539: if (m->s == -1 || (s = session_find_by_id(m->s)) == NULL)
1.99 nicm 540: return (NULL);
1.147 nicm 541: if (m->w == -1)
542: wl = s->curw;
543: else {
544: if ((w = window_find_by_id(m->w)) == NULL)
545: return (NULL);
546: wl = winlink_find_by_window(&s->windows, w);
547: }
1.99 nicm 548: if (sp != NULL)
549: *sp = s;
1.147 nicm 550: return (wl);
1.99 nicm 551: }
552:
553: /* Get current mouse pane if any. */
554: struct window_pane *
1.104 nicm 555: cmd_mouse_pane(struct mouse_event *m, struct session **sp,
556: struct winlink **wlp)
1.99 nicm 557: {
558: struct winlink *wl;
559: struct window_pane *wp;
560:
561: if ((wl = cmd_mouse_window(m, sp)) == NULL)
562: return (NULL);
563: if ((wp = window_pane_find_by_id(m->wp)) == NULL)
564: return (NULL);
565: if (!window_has_pane(wl->window, wp))
566: return (NULL);
567:
568: if (wlp != NULL)
569: *wlp = wl;
1.43 nicm 570: return (wp);
1.15 nicm 571: }
572:
573: /* Replace the first %% or %idx in template by s. */
574: char *
1.75 nicm 575: cmd_template_replace(const char *template, const char *s, int idx)
1.15 nicm 576: {
1.75 nicm 577: char ch, *buf;
1.131 nicm 578: const char *ptr, *cp, quote[] = "\"\\$";
1.127 nicm 579: int replaced, quoted;
1.84 nicm 580: size_t len;
1.15 nicm 581:
1.76 nicm 582: if (strchr(template, '%') == NULL)
1.15 nicm 583: return (xstrdup(template));
584:
585: buf = xmalloc(1);
586: *buf = '\0';
587: len = 0;
588: replaced = 0;
589:
590: ptr = template;
591: while (*ptr != '\0') {
592: switch (ch = *ptr++) {
593: case '%':
594: if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
595: if (*ptr != '%' || replaced)
596: break;
597: replaced = 1;
598: }
599: ptr++;
600:
1.127 nicm 601: quoted = (*ptr == '%');
602: if (quoted)
603: ptr++;
604:
1.132 nicm 605: buf = xrealloc(buf, len + (strlen(s) * 3) + 1);
1.127 nicm 606: for (cp = s; *cp != '\0'; cp++) {
1.131 nicm 607: if (quoted && strchr(quote, *cp) != NULL)
1.127 nicm 608: buf[len++] = '\\';
1.132 nicm 609: if (quoted && *cp == ';') {
610: buf[len++] = '\\';
611: buf[len++] = '\\';
612: }
1.127 nicm 613: buf[len++] = *cp;
614: }
615: buf[len] = '\0';
1.15 nicm 616: continue;
617: }
1.98 nicm 618: buf = xrealloc(buf, len + 2);
1.15 nicm 619: buf[len++] = ch;
620: buf[len] = '\0';
621: }
622:
623: return (buf);
1.1 nicm 624: }