Annotation of src/usr.bin/tmux/cmd.c, Revision 1.150
1.150 ! nicm 1: /* $OpenBSD: cmd.c,v 1.149 2019/05/23 11:13:30 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.150 ! nicm 208: void printflike(3, 4)
! 209: cmd_log_argv(int argc, char **argv, const char *fmt, ...)
1.140 nicm 210: {
1.150 ! nicm 211: char *prefix;
! 212: va_list ap;
! 213: int i;
! 214:
! 215: va_start(ap, fmt);
! 216: xvasprintf(&prefix, fmt, ap);
! 217: va_end(ap);
1.140 nicm 218:
219: for (i = 0; i < argc; i++)
220: log_debug("%s: argv[%d]=%s", prefix, i, argv[i]);
1.150 ! nicm 221: free(prefix);
1.140 nicm 222: }
223:
1.149 nicm 224: void
225: cmd_prepend_argv(int *argc, char ***argv, char *arg)
226: {
227: char **new_argv;
228: int i;
229:
230: new_argv = xreallocarray(NULL, (*argc) + 1, sizeof *new_argv);
231: new_argv[0] = xstrdup(arg);
232: for (i = 0; i < *argc; i++)
233: new_argv[1 + i] = (*argv)[i];
234:
235: free(*argv);
236: *argv = new_argv;
237: (*argc)++;
238: }
239:
240: void
241: cmd_append_argv(int *argc, char ***argv, char *arg)
242: {
243: *argv = xreallocarray(*argv, (*argc) + 1, sizeof **argv);
244: (*argv)[(*argc)++] = xstrdup(arg);
245: }
246:
1.10 nicm 247: int
248: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
249: {
250: size_t arglen;
251: int i;
1.91 nicm 252:
253: if (argc == 0)
254: return (0);
1.150 ! nicm 255: cmd_log_argv(argc, argv, "%s", __func__);
1.10 nicm 256:
257: *buf = '\0';
258: for (i = 0; i < argc; i++) {
259: if (strlcpy(buf, argv[i], len) >= len)
260: return (-1);
261: arglen = strlen(argv[i]) + 1;
262: buf += arglen;
263: len -= arglen;
264: }
265:
266: return (0);
267: }
268:
269: int
270: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
271: {
272: int i;
273: size_t arglen;
274:
275: if (argc == 0)
276: return (0);
277: *argv = xcalloc(argc, sizeof **argv);
278:
279: buf[len - 1] = '\0';
280: for (i = 0; i < argc; i++) {
281: if (len == 0) {
282: cmd_free_argv(argc, *argv);
283: return (-1);
284: }
285:
286: arglen = strlen(buf) + 1;
287: (*argv)[i] = xstrdup(buf);
1.140 nicm 288:
1.10 nicm 289: buf += arglen;
290: len -= arglen;
291: }
1.150 ! nicm 292: cmd_log_argv(argc, *argv, "%s", __func__);
1.10 nicm 293:
294: return (0);
1.46 nicm 295: }
296:
297: char **
1.93 nicm 298: cmd_copy_argv(int argc, char **argv)
1.46 nicm 299: {
300: char **new_argv;
301: int i;
302:
303: if (argc == 0)
304: return (NULL);
1.94 nicm 305: new_argv = xcalloc(argc + 1, sizeof *new_argv);
1.46 nicm 306: for (i = 0; i < argc; i++) {
307: if (argv[i] != NULL)
308: new_argv[i] = xstrdup(argv[i]);
309: }
310: return (new_argv);
1.10 nicm 311: }
312:
313: void
314: cmd_free_argv(int argc, char **argv)
315: {
316: int i;
317:
318: if (argc == 0)
1.35 nicm 319: return;
1.68 nicm 320: for (i = 0; i < argc; i++)
321: free(argv[i]);
322: free(argv);
1.94 nicm 323: }
324:
325: char *
326: cmd_stringify_argv(int argc, char **argv)
327: {
328: char *buf;
329: int i;
330: size_t len;
331:
332: if (argc == 0)
333: return (xstrdup(""));
334:
335: len = 0;
336: buf = NULL;
337:
338: for (i = 0; i < argc; i++) {
339: len += strlen(argv[i]) + 1;
1.98 nicm 340: buf = xrealloc(buf, len);
1.94 nicm 341:
342: if (i == 0)
343: *buf = '\0';
344: else
345: strlcat(buf, " ", len);
346: strlcat(buf, argv[i], len);
347: }
348: return (buf);
1.10 nicm 349: }
350:
1.149 nicm 351: char *
352: cmd_get_alias(const char *name)
1.135 nicm 353: {
1.149 nicm 354: struct options_entry *o;
355: struct options_array_item *a;
356: union options_value *ov;
357: size_t wanted, n;
358: const char *equals;
1.135 nicm 359:
360: o = options_get_only(global_options, "command-alias");
1.142 nicm 361: if (o == NULL)
1.149 nicm 362: return (NULL);
363: wanted = strlen(name);
1.135 nicm 364:
1.142 nicm 365: a = options_array_first(o);
366: while (a != NULL) {
1.143 nicm 367: ov = options_array_item_value(a);
1.149 nicm 368:
369: equals = strchr(ov->string, '=');
370: if (equals != NULL) {
371: n = equals - ov->string;
372: if (n == wanted && strncmp(name, ov->string, n) == 0)
373: return (xstrdup(equals + 1));
374: }
375:
1.142 nicm 376: a = options_array_next(a);
1.135 nicm 377: }
1.149 nicm 378: return (NULL);
379: }
1.135 nicm 380:
1.149 nicm 381: static const struct cmd_entry *
382: cmd_find(const char *name, char **cause)
383: {
384: const struct cmd_entry **loop, *entry, *found = NULL;
385: int ambiguous;
386: char s[BUFSIZ];
1.135 nicm 387:
1.149 nicm 388: ambiguous = 0;
389: for (loop = cmd_table; *loop != NULL; loop++) {
390: entry = *loop;
391: if (entry->alias != NULL && strcmp(entry->alias, name) == 0) {
392: ambiguous = 0;
393: found = entry;
394: break;
395: }
1.135 nicm 396:
1.149 nicm 397: if (strncmp(entry->name, name, strlen(name)) != 0)
398: continue;
399: if (found != NULL)
400: ambiguous = 1;
401: found = entry;
1.135 nicm 402:
1.149 nicm 403: if (strcmp(entry->name, name) == 0)
404: break;
405: }
406: if (ambiguous)
407: goto ambiguous;
408: if (found == NULL) {
409: xasprintf(cause, "unknown command: %s", name);
410: return (NULL);
411: }
412: return (found);
1.135 nicm 413:
1.149 nicm 414: ambiguous:
415: *s = '\0';
416: for (loop = cmd_table; *loop != NULL; loop++) {
417: entry = *loop;
418: if (strncmp(entry->name, name, strlen(name)) != 0)
419: continue;
420: if (strlcat(s, entry->name, sizeof s) >= sizeof s)
421: break;
422: if (strlcat(s, ", ", sizeof s) >= sizeof s)
423: break;
424: }
425: s[strlen(s) - 2] = '\0';
426: xasprintf(cause, "ambiguous command: %s, could be: %s", name, s);
427: return (NULL);
1.135 nicm 428: }
429:
1.1 nicm 430: struct cmd *
1.80 nicm 431: cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
1.1 nicm 432: {
1.149 nicm 433: const struct cmd_entry *entry;
1.135 nicm 434: const char *name;
1.27 deraadt 435: struct cmd *cmd;
1.49 nicm 436: struct args *args;
1.1 nicm 437:
1.2 nicm 438: if (argc == 0) {
439: xasprintf(cause, "no command");
1.1 nicm 440: return (NULL);
1.2 nicm 441: }
1.135 nicm 442: name = argv[0];
1.1 nicm 443:
1.149 nicm 444: entry = cmd_find(name, cause);
445: if (entry == NULL)
1.1 nicm 446: return (NULL);
1.150 ! nicm 447: cmd_log_argv(argc, argv, "%s: %s", __func__, entry->name);
1.1 nicm 448:
1.117 nicm 449: args = args_parse(entry->args.template, argc, argv);
1.49 nicm 450: if (args == NULL)
451: goto usage;
1.117 nicm 452: if (entry->args.lower != -1 && args->argc < entry->args.lower)
1.49 nicm 453: goto usage;
1.117 nicm 454: if (entry->args.upper != -1 && args->argc > entry->args.upper)
1.49 nicm 455: goto usage;
1.1 nicm 456:
1.80 nicm 457: cmd = xcalloc(1, sizeof *cmd);
1.1 nicm 458: cmd->entry = entry;
1.49 nicm 459: cmd->args = args;
1.80 nicm 460:
461: if (file != NULL)
462: cmd->file = xstrdup(file);
463: cmd->line = line;
464:
1.149 nicm 465: cmd->alias = NULL;
466: cmd->argc = argc;
467: cmd->argv = cmd_copy_argv(argc, argv);
468:
1.1 nicm 469: return (cmd);
470:
471: usage:
1.49 nicm 472: if (args != NULL)
473: args_free(args);
1.1 nicm 474: xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
475: return (NULL);
1.149 nicm 476: }
477:
478: void
479: cmd_free(struct cmd *cmd)
480: {
481: free(cmd->alias);
482: cmd_free_argv(cmd->argc, cmd->argv);
483:
484: free(cmd->file);
485:
486: args_free(cmd->args);
487: free(cmd);
1.1 nicm 488: }
489:
1.106 nicm 490: char *
491: cmd_print(struct cmd *cmd)
1.1 nicm 492: {
1.106 nicm 493: char *out, *s;
494:
495: s = args_print(cmd->args);
496: if (*s != '\0')
497: xasprintf(&out, "%s %s", cmd->entry->name, s);
498: else
499: out = xstrdup(cmd->entry->name);
500: free(s);
1.49 nicm 501:
1.106 nicm 502: return (out);
1.1 nicm 503: }
504:
1.99 nicm 505: /* Adjust current mouse position for a pane. */
506: int
507: cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
508: u_int *yp, int last)
509: {
510: u_int x, y;
511:
512: if (last) {
1.145 nicm 513: x = m->lx + m->ox;
514: y = m->ly + m->oy;
1.99 nicm 515: } else {
1.146 nicm 516: x = m->x + m->ox;
517: y = m->y + m->oy;
1.99 nicm 518: }
1.145 nicm 519: log_debug("%s: x=%u, y=%u%s", __func__, x, y, last ? " (last)" : "");
1.99 nicm 520:
521: if (m->statusat == 0 && y > 0)
522: y--;
523:
524: if (x < wp->xoff || x >= wp->xoff + wp->sx)
525: return (-1);
526: if (y < wp->yoff || y >= wp->yoff + wp->sy)
527: return (-1);
528:
1.123 nicm 529: if (xp != NULL)
530: *xp = x - wp->xoff;
531: if (yp != NULL)
532: *yp = y - wp->yoff;
1.99 nicm 533: return (0);
534: }
535:
536: /* Get current mouse window if any. */
537: struct winlink *
538: cmd_mouse_window(struct mouse_event *m, struct session **sp)
539: {
540: struct session *s;
541: struct window *w;
1.147 nicm 542: struct winlink *wl;
1.99 nicm 543:
1.147 nicm 544: if (!m->valid)
1.99 nicm 545: return (NULL);
1.147 nicm 546: if (m->s == -1 || (s = session_find_by_id(m->s)) == NULL)
1.99 nicm 547: return (NULL);
1.147 nicm 548: if (m->w == -1)
549: wl = s->curw;
550: else {
551: if ((w = window_find_by_id(m->w)) == NULL)
552: return (NULL);
553: wl = winlink_find_by_window(&s->windows, w);
554: }
1.99 nicm 555: if (sp != NULL)
556: *sp = s;
1.147 nicm 557: return (wl);
1.99 nicm 558: }
559:
560: /* Get current mouse pane if any. */
561: struct window_pane *
1.104 nicm 562: cmd_mouse_pane(struct mouse_event *m, struct session **sp,
563: struct winlink **wlp)
1.99 nicm 564: {
565: struct winlink *wl;
566: struct window_pane *wp;
567:
568: if ((wl = cmd_mouse_window(m, sp)) == NULL)
569: return (NULL);
570: if ((wp = window_pane_find_by_id(m->wp)) == NULL)
571: return (NULL);
572: if (!window_has_pane(wl->window, wp))
573: return (NULL);
574:
575: if (wlp != NULL)
576: *wlp = wl;
1.43 nicm 577: return (wp);
1.15 nicm 578: }
579:
580: /* Replace the first %% or %idx in template by s. */
581: char *
1.75 nicm 582: cmd_template_replace(const char *template, const char *s, int idx)
1.15 nicm 583: {
1.75 nicm 584: char ch, *buf;
1.131 nicm 585: const char *ptr, *cp, quote[] = "\"\\$";
1.127 nicm 586: int replaced, quoted;
1.84 nicm 587: size_t len;
1.15 nicm 588:
1.76 nicm 589: if (strchr(template, '%') == NULL)
1.15 nicm 590: return (xstrdup(template));
591:
592: buf = xmalloc(1);
593: *buf = '\0';
594: len = 0;
595: replaced = 0;
596:
597: ptr = template;
598: while (*ptr != '\0') {
599: switch (ch = *ptr++) {
600: case '%':
601: if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
602: if (*ptr != '%' || replaced)
603: break;
604: replaced = 1;
605: }
606: ptr++;
607:
1.127 nicm 608: quoted = (*ptr == '%');
609: if (quoted)
610: ptr++;
611:
1.132 nicm 612: buf = xrealloc(buf, len + (strlen(s) * 3) + 1);
1.127 nicm 613: for (cp = s; *cp != '\0'; cp++) {
1.131 nicm 614: if (quoted && strchr(quote, *cp) != NULL)
1.127 nicm 615: buf[len++] = '\\';
1.132 nicm 616: if (quoted && *cp == ';') {
617: buf[len++] = '\\';
618: buf[len++] = '\\';
619: }
1.127 nicm 620: buf[len++] = *cp;
621: }
622: buf[len] = '\0';
1.15 nicm 623: continue;
624: }
1.98 nicm 625: buf = xrealloc(buf, len + 2);
1.15 nicm 626: buf[len++] = ch;
627: buf[len] = '\0';
628: }
629:
630: return (buf);
1.1 nicm 631: }