Annotation of src/usr.bin/tmux/format.c, Revision 1.198
1.198 ! nicm 1: /* $OpenBSD: format.c,v 1.197 2019/05/26 17:34:45 nicm Exp $ */
1.1 nicm 2:
3: /*
1.104 nicm 4: * Copyright (c) 2011 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>
1.55 nicm 20: #include <sys/wait.h>
1.1 nicm 21:
1.157 nicm 22: #include <ctype.h>
1.30 nicm 23: #include <errno.h>
1.139 nicm 24: #include <fnmatch.h>
1.87 nicm 25: #include <libgen.h>
1.1 nicm 26: #include <stdarg.h>
1.9 nicm 27: #include <stdlib.h>
1.1 nicm 28: #include <string.h>
29: #include <time.h>
30: #include <unistd.h>
31:
32: #include "tmux.h"
33:
34: /*
35: * Build a list of key-value pairs and use them to expand #{key} entries in a
36: * string.
37: */
38:
1.79 nicm 39: struct format_entry;
40: typedef void (*format_cb)(struct format_tree *, struct format_entry *);
41:
1.108 nicm 42: static char *format_job_get(struct format_tree *, const char *);
43: static void format_job_timer(int, short, void *);
44:
45: static char *format_find(struct format_tree *, const char *, int);
46: static void format_add_cb(struct format_tree *, const char *, format_cb);
47: static void format_add_tv(struct format_tree *, const char *,
48: struct timeval *);
49: static int format_replace(struct format_tree *, const char *, size_t,
50: char **, size_t *, size_t *);
51:
52: static void format_defaults_session(struct format_tree *,
53: struct session *);
54: static void format_defaults_client(struct format_tree *, struct client *);
1.164 nicm 55: static void format_defaults_winlink(struct format_tree *, struct winlink *);
1.1 nicm 56:
1.192 nicm 57: /* Default menus. */
58: #define DEFAULT_CLIENT_MENU \
59: "Detach,d,detach-client|" \
60: "Detach & Kill,X,detach-client -P|" \
61: "Detach Others,o,detach-client -a|" \
62: "|" \
1.194 nicm 63: "Lock,l,lock-client"
1.192 nicm 64: #define DEFAULT_SESSION_MENU \
65: "Next,n,switch-client -n|" \
66: "Previous,p,switch-client -p|" \
67: "|" \
68: "Renumber,N,move-window -r|" \
69: "Rename,n,command-prompt -I \"#S\" \"rename-session -- '%%'\"|" \
70: "|" \
71: "New Session,s,new-session|" \
72: "New Window,w,new-window"
73: #define DEFAULT_WINDOW_MENU \
1.193 nicm 74: "Swap Left,l,swap-window -t:-1|" \
75: "Swap Right,r,swap-window -t:+1|" \
1.192 nicm 76: "#{?pane_marked_set,,#[dim]}Swap Marked,s,swap-window|" \
77: "|" \
78: "Kill,X,kill-window|" \
79: "Respawn,R,respawn-window -k|" \
80: "|" \
81: "#{?pane_marked,Unmark,Mark},m,select-pane -m|" \
82: "Rename,n,command-prompt -I \"#W\" \"rename-window -- '%%'\"|" \
83: "|" \
84: "New After,w,new-window -a|" \
85: "New At End,W,new-window"
86: #define DEFAULT_PANE_MENU \
1.198 ! nicm 87: "#{?mouse_word,Search For #[underscore]#{=/9/...:mouse_word},},C-r,copy-mode -t=; send -Xt= search-backward \"#{q:mouse_word}\"|" \
! 88: "#{?mouse_word,Type #[underscore]#{=/9/...:mouse_word},},C-y,send-keys -l \"#{q:mouse_word}\"|" \
1.197 nicm 89: "|" \
90: "#{?mouse_word,Copy #[underscore]#{=/9/...:mouse_word},},c,set-buffer \"#{q:mouse_word}\"|" \
91: "#{?mouse_line,Copy Line,},l,set-buffer \"#{q:mouse_line}\"|" \
92: "|" \
1.192 nicm 93: "Horizontal Split,h,split-window -h|" \
94: "Vertical Split,v,split-window -v|" \
95: "|" \
96: "Swap Up,u,swap-pane -U|" \
97: "Swap Down,d,swap-pane -D|" \
98: "#{?pane_marked_set,,#[dim]}Swap Marked,s,swap-pane|" \
99: "|" \
100: "Kill,X,kill-pane|" \
101: "Respawn,R,respawn-pane -k|" \
102: "|" \
103: "#{?pane_marked,Unmark,Mark},m,select-pane -m|" \
104: "#{?window_zoomed_flag,Unzoom,Zoom},z,resize-pane -Z"
105:
1.68 nicm 106: /* Entry in format job tree. */
107: struct format_job {
1.131 nicm 108: struct client *client;
1.120 nicm 109: u_int tag;
1.68 nicm 110: const char *cmd;
1.113 nicm 111: const char *expanded;
1.68 nicm 112:
113: time_t last;
114: char *out;
1.127 nicm 115: int updated;
1.68 nicm 116:
117: struct job *job;
118: int status;
119:
120: RB_ENTRY(format_job) entry;
121: };
122:
123: /* Format job tree. */
1.108 nicm 124: static struct event format_job_event;
125: static int format_job_cmp(struct format_job *, struct format_job *);
1.109 nicm 126: static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
1.108 nicm 127: RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp);
1.68 nicm 128:
129: /* Format job tree comparison function. */
1.108 nicm 130: static int
1.68 nicm 131: format_job_cmp(struct format_job *fj1, struct format_job *fj2)
132: {
1.120 nicm 133: if (fj1->tag < fj2->tag)
134: return (-1);
135: if (fj1->tag > fj2->tag)
136: return (1);
1.68 nicm 137: return (strcmp(fj1->cmd, fj2->cmd));
138: }
139:
1.87 nicm 140: /* Format modifiers. */
141: #define FORMAT_TIMESTRING 0x1
142: #define FORMAT_BASENAME 0x2
143: #define FORMAT_DIRNAME 0x4
1.169 nicm 144: #define FORMAT_QUOTE 0x8
145: #define FORMAT_LITERAL 0x10
1.170 nicm 146: #define FORMAT_EXPAND 0x20
1.175 nicm 147: #define FORMAT_EXPANDTIME 0x40
148: #define FORMAT_SESSIONS 0x80
149: #define FORMAT_WINDOWS 0x100
150: #define FORMAT_PANES 0x200
1.87 nicm 151:
1.178 nicm 152: /* Limit on recursion. */
153: #define FORMAT_LOOP_LIMIT 10
154:
1.54 nicm 155: /* Entry in format tree. */
156: struct format_entry {
1.79 nicm 157: char *key;
158: char *value;
1.87 nicm 159: time_t t;
1.79 nicm 160: format_cb cb;
161: RB_ENTRY(format_entry) entry;
1.54 nicm 162: };
163:
1.68 nicm 164: /* Format entry tree. */
1.54 nicm 165: struct format_tree {
1.164 nicm 166: struct client *c;
167: struct session *s;
168: struct winlink *wl;
1.79 nicm 169: struct window *w;
170: struct window_pane *wp;
1.54 nicm 171:
1.172 nicm 172: struct cmdq_item *item;
1.131 nicm 173: struct client *client;
1.120 nicm 174: u_int tag;
1.84 nicm 175: int flags;
1.177 nicm 176: time_t time;
1.178 nicm 177: u_int loop;
1.68 nicm 178:
1.197 nicm 179: struct mouse_event m;
180:
1.68 nicm 181: RB_HEAD(format_entry_tree, format_entry) tree;
1.54 nicm 182: };
1.108 nicm 183: static int format_entry_cmp(struct format_entry *, struct format_entry *);
184: RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
1.54 nicm 185:
1.180 nicm 186: /* Format modifier. */
1.169 nicm 187: struct format_modifier {
188: char modifier[3];
189: u_int size;
190:
191: char **argv;
192: int argc;
193: };
194:
1.68 nicm 195: /* Format entry tree comparison function. */
1.108 nicm 196: static int
1.68 nicm 197: format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
1.1 nicm 198: {
199: return (strcmp(fe1->key, fe2->key));
200: }
201:
1.25 nicm 202: /* Single-character uppercase aliases. */
1.108 nicm 203: static const char *format_upper[] = {
1.1 nicm 204: NULL, /* A */
205: NULL, /* B */
206: NULL, /* C */
207: "pane_id", /* D */
208: NULL, /* E */
209: "window_flags", /* F */
210: NULL, /* G */
211: "host", /* H */
212: "window_index", /* I */
213: NULL, /* J */
214: NULL, /* K */
215: NULL, /* L */
216: NULL, /* M */
217: NULL, /* N */
218: NULL, /* O */
219: "pane_index", /* P */
220: NULL, /* Q */
221: NULL, /* R */
222: "session_name", /* S */
223: "pane_title", /* T */
224: NULL, /* U */
225: NULL, /* V */
226: "window_name", /* W */
227: NULL, /* X */
228: NULL, /* Y */
229: NULL /* Z */
230: };
231:
1.25 nicm 232: /* Single-character lowercase aliases. */
1.108 nicm 233: static const char *format_lower[] = {
1.25 nicm 234: NULL, /* a */
235: NULL, /* b */
236: NULL, /* c */
237: NULL, /* d */
238: NULL, /* e */
239: NULL, /* f */
240: NULL, /* g */
241: "host_short", /* h */
242: NULL, /* i */
243: NULL, /* j */
244: NULL, /* k */
245: NULL, /* l */
246: NULL, /* m */
247: NULL, /* n */
248: NULL, /* o */
249: NULL, /* p */
250: NULL, /* q */
251: NULL, /* r */
252: NULL, /* s */
253: NULL, /* t */
254: NULL, /* u */
255: NULL, /* v */
256: NULL, /* w */
257: NULL, /* x */
258: NULL, /* y */
259: NULL /* z */
260: };
261:
1.179 nicm 262: /* Is logging enabled? */
263: static inline int
264: format_logging(struct format_tree *ft)
265: {
266: return (log_get_level() != 0 || (ft->flags & FORMAT_VERBOSE));
267: }
268:
269: /* Log a message if verbose. */
270: static void printflike(3, 4)
271: format_log1(struct format_tree *ft, const char *from, const char *fmt, ...)
272: {
273: va_list ap;
274: char *s;
275: static const char spaces[] = " ";
276:
277: if (!format_logging(ft))
278: return;
279:
280: va_start(ap, fmt);
281: vasprintf(&s, fmt, ap);
282: va_end(ap);
283:
284: log_debug("%s: %s", from, s);
1.181 nicm 285: if (ft->item != NULL && (ft->flags & FORMAT_VERBOSE))
1.179 nicm 286: cmdq_print(ft->item, "#%.*s%s", ft->loop, spaces, s);
287:
288: free(s);
289: }
290: #define format_log(ft, fmt, ...) format_log1(ft, __func__, fmt, ##__VA_ARGS__)
291:
1.127 nicm 292: /* Format job update callback. */
1.108 nicm 293: static void
1.127 nicm 294: format_job_update(struct job *job)
295: {
1.160 nicm 296: struct format_job *fj = job_get_data(job);
297: struct evbuffer *evb = job_get_event(job)->input;
1.151 nicm 298: char *line = NULL, *next;
1.127 nicm 299: time_t t;
300:
1.151 nicm 301: while ((next = evbuffer_readline(evb)) != NULL) {
302: free(line);
303: line = next;
304: }
305: if (line == NULL)
1.127 nicm 306: return;
307: fj->updated = 1;
308:
309: free(fj->out);
310: fj->out = line;
311:
1.136 nicm 312: log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out);
1.127 nicm 313:
1.142 nicm 314: t = time(NULL);
1.127 nicm 315: if (fj->status && fj->last != t) {
1.136 nicm 316: if (fj->client != NULL)
317: server_status_client(fj->client);
1.127 nicm 318: fj->last = t;
319: }
320: }
321:
322: /* Format job complete callback. */
323: static void
324: format_job_complete(struct job *job)
1.68 nicm 325: {
1.160 nicm 326: struct format_job *fj = job_get_data(job);
327: struct evbuffer *evb = job_get_event(job)->input;
1.68 nicm 328: char *line, *buf;
329: size_t len;
330:
331: fj->job = NULL;
332:
333: buf = NULL;
1.160 nicm 334: if ((line = evbuffer_readline(evb)) == NULL) {
335: len = EVBUFFER_LENGTH(evb);
1.68 nicm 336: buf = xmalloc(len + 1);
337: if (len != 0)
1.160 nicm 338: memcpy(buf, EVBUFFER_DATA(evb), len);
1.68 nicm 339: buf[len] = '\0';
340: } else
341: buf = line;
1.127 nicm 342:
1.136 nicm 343: log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf);
344:
1.127 nicm 345: if (*buf != '\0' || !fj->updated) {
346: free(fj->out);
347: fj->out = buf;
348: } else
349: free(buf);
1.68 nicm 350:
351: if (fj->status) {
1.131 nicm 352: if (fj->client != NULL)
353: server_status_client(fj->client);
1.68 nicm 354: fj->status = 0;
355: }
356: }
357:
358: /* Find a job. */
1.108 nicm 359: static char *
1.68 nicm 360: format_job_get(struct format_tree *ft, const char *cmd)
361: {
1.131 nicm 362: struct format_job_tree *jobs;
1.113 nicm 363: struct format_job fj0, *fj;
364: time_t t;
365: char *expanded;
366: int force;
1.68 nicm 367:
1.131 nicm 368: if (ft->client == NULL)
369: jobs = &format_jobs;
370: else if (ft->client->jobs != NULL)
371: jobs = ft->client->jobs;
372: else {
373: jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
374: RB_INIT(jobs);
375: }
376:
1.120 nicm 377: fj0.tag = ft->tag;
1.68 nicm 378: fj0.cmd = cmd;
1.131 nicm 379: if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
1.68 nicm 380: fj = xcalloc(1, sizeof *fj);
1.131 nicm 381: fj->client = ft->client;
1.120 nicm 382: fj->tag = ft->tag;
1.68 nicm 383: fj->cmd = xstrdup(cmd);
1.113 nicm 384: fj->expanded = NULL;
1.68 nicm 385:
386: xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
387:
1.131 nicm 388: RB_INSERT(format_job_tree, jobs, fj);
1.68 nicm 389: }
390:
1.113 nicm 391: expanded = format_expand(ft, cmd);
392: if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
393: free((void *)fj->expanded);
394: fj->expanded = xstrdup(expanded);
395: force = 1;
396: } else
397: force = (ft->flags & FORMAT_FORCE);
398:
1.84 nicm 399: t = time(NULL);
1.183 nicm 400: if (force && fj->job != NULL)
401: job_free(fj->job);
402: if (force || (fj->job == NULL && fj->last != t)) {
1.163 nicm 403: fj->job = job_run(expanded, NULL,
404: server_client_get_cwd(ft->client, NULL), format_job_update,
1.153 nicm 405: format_job_complete, NULL, fj, JOB_NOWAIT);
1.68 nicm 406: if (fj->job == NULL) {
407: free(fj->out);
408: xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
409: }
1.84 nicm 410: fj->last = t;
1.137 nicm 411: fj->updated = 0;
1.68 nicm 412: }
1.84 nicm 413:
414: if (ft->flags & FORMAT_STATUS)
415: fj->status = 1;
1.68 nicm 416:
1.113 nicm 417: free(expanded);
1.86 nicm 418: return (format_expand(ft, fj->out));
1.68 nicm 419: }
420:
421: /* Remove old jobs. */
1.108 nicm 422: static void
1.131 nicm 423: format_job_tidy(struct format_job_tree *jobs, int force)
1.68 nicm 424: {
425: struct format_job *fj, *fj1;
426: time_t now;
427:
428: now = time(NULL);
1.131 nicm 429: RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
430: if (!force && (fj->last > now || now - fj->last < 3600))
1.68 nicm 431: continue;
1.131 nicm 432: RB_REMOVE(format_job_tree, jobs, fj);
1.68 nicm 433:
1.77 nicm 434: log_debug("%s: %s", __func__, fj->cmd);
435:
1.68 nicm 436: if (fj->job != NULL)
437: job_free(fj->job);
438:
1.113 nicm 439: free((void *)fj->expanded);
1.78 nicm 440: free((void *)fj->cmd);
1.68 nicm 441: free(fj->out);
442:
443: free(fj);
444: }
1.131 nicm 445: }
446:
447: /* Remove old jobs for client. */
448: void
449: format_lost_client(struct client *c)
450: {
451: if (c->jobs != NULL)
452: format_job_tidy(c->jobs, 1);
453: free(c->jobs);
454: }
455:
456: /* Remove old jobs periodically. */
457: static void
458: format_job_timer(__unused int fd, __unused short events, __unused void *arg)
459: {
460: struct client *c;
461: struct timeval tv = { .tv_sec = 60 };
462:
463: format_job_tidy(&format_jobs, 0);
464: TAILQ_FOREACH(c, &clients, entry) {
465: if (c->jobs != NULL)
466: format_job_tidy(c->jobs, 0);
467: }
1.77 nicm 468:
469: evtimer_del(&format_job_event);
470: evtimer_add(&format_job_event, &tv);
1.68 nicm 471: }
472:
1.79 nicm 473: /* Callback for host. */
1.108 nicm 474: static void
1.99 nicm 475: format_cb_host(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 476: {
477: char host[HOST_NAME_MAX + 1];
478:
479: if (gethostname(host, sizeof host) != 0)
480: fe->value = xstrdup("");
481: else
482: fe->value = xstrdup(host);
483: }
484:
485: /* Callback for host_short. */
1.108 nicm 486: static void
1.99 nicm 487: format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 488: {
489: char host[HOST_NAME_MAX + 1], *cp;
490:
491: if (gethostname(host, sizeof host) != 0)
492: fe->value = xstrdup("");
493: else {
494: if ((cp = strchr(host, '.')) != NULL)
495: *cp = '\0';
496: fe->value = xstrdup(host);
497: }
498: }
499:
500: /* Callback for pid. */
1.108 nicm 501: static void
1.99 nicm 502: format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 503: {
504: xasprintf(&fe->value, "%ld", (long)getpid());
505: }
506:
1.80 nicm 507: /* Callback for session_alerts. */
1.108 nicm 508: static void
1.80 nicm 509: format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe)
510: {
511: struct session *s = ft->s;
512: struct winlink *wl;
1.133 nicm 513: char alerts[1024], tmp[16];
1.80 nicm 514:
515: if (s == NULL)
516: return;
517:
518: *alerts = '\0';
519: RB_FOREACH(wl, winlinks, &s->windows) {
520: if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
521: continue;
522: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
523:
524: if (*alerts != '\0')
525: strlcat(alerts, ",", sizeof alerts);
526: strlcat(alerts, tmp, sizeof alerts);
527: if (wl->flags & WINLINK_ACTIVITY)
528: strlcat(alerts, "#", sizeof alerts);
529: if (wl->flags & WINLINK_BELL)
530: strlcat(alerts, "!", sizeof alerts);
531: if (wl->flags & WINLINK_SILENCE)
532: strlcat(alerts, "~", sizeof alerts);
533: }
534: fe->value = xstrdup(alerts);
535: }
536:
1.133 nicm 537: /* Callback for session_stack. */
538: static void
539: format_cb_session_stack(struct format_tree *ft, struct format_entry *fe)
540: {
541: struct session *s = ft->s;
542: struct winlink *wl;
543: char result[1024], tmp[16];
544:
545: if (s == NULL)
546: return;
547:
548: xsnprintf(result, sizeof result, "%u", s->curw->idx);
549: TAILQ_FOREACH(wl, &s->lastw, sentry) {
550: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
551:
552: if (*result != '\0')
553: strlcat(result, ",", sizeof result);
554: strlcat(result, tmp, sizeof result);
555: }
556: fe->value = xstrdup(result);
557: }
558:
559: /* Callback for window_stack_index. */
560: static void
561: format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe)
562: {
563: struct session *s = ft->wl->session;
564: struct winlink *wl;
565: u_int idx;
566:
567: idx = 0;
568: TAILQ_FOREACH(wl, &s->lastw, sentry) {
569: idx++;
570: if (wl == ft->wl)
571: break;
572: }
573: if (wl != NULL)
574: xasprintf(&fe->value, "%u", idx);
575: else
576: fe->value = xstrdup("0");
577: }
578:
1.80 nicm 579: /* Callback for window_layout. */
1.108 nicm 580: static void
1.80 nicm 581: format_cb_window_layout(struct format_tree *ft, struct format_entry *fe)
582: {
583: struct window *w = ft->w;
584:
585: if (w == NULL)
586: return;
587:
588: if (w->saved_layout_root != NULL)
589: fe->value = layout_dump(w->saved_layout_root);
590: else
591: fe->value = layout_dump(w->layout_root);
592: }
593:
1.96 nicm 594: /* Callback for window_visible_layout. */
1.108 nicm 595: static void
1.96 nicm 596: format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe)
597: {
598: struct window *w = ft->w;
599:
600: if (w == NULL)
601: return;
602:
603: fe->value = layout_dump(w->layout_root);
604: }
605:
1.79 nicm 606: /* Callback for pane_start_command. */
1.108 nicm 607: static void
1.79 nicm 608: format_cb_start_command(struct format_tree *ft, struct format_entry *fe)
609: {
610: struct window_pane *wp = ft->wp;
611:
612: if (wp == NULL)
613: return;
614:
615: fe->value = cmd_stringify_argv(wp->argc, wp->argv);
616: }
617:
618: /* Callback for pane_current_command. */
1.108 nicm 619: static void
1.79 nicm 620: format_cb_current_command(struct format_tree *ft, struct format_entry *fe)
621: {
622: struct window_pane *wp = ft->wp;
623: char *cmd;
624:
625: if (wp == NULL)
626: return;
627:
628: cmd = get_proc_name(wp->fd, wp->tty);
629: if (cmd == NULL || *cmd == '\0') {
630: free(cmd);
631: cmd = cmd_stringify_argv(wp->argc, wp->argv);
632: if (cmd == NULL || *cmd == '\0') {
633: free(cmd);
634: cmd = xstrdup(wp->shell);
635: }
636: }
637: fe->value = parse_window_name(cmd);
638: free(cmd);
639: }
640:
1.80 nicm 641: /* Callback for history_bytes. */
1.108 nicm 642: static void
1.80 nicm 643: format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe)
644: {
645: struct window_pane *wp = ft->wp;
646: struct grid *gd;
647: struct grid_line *gl;
648: unsigned long long size;
649: u_int i;
650:
651: if (wp == NULL)
652: return;
653: gd = wp->base.grid;
654:
655: size = 0;
656: for (i = 0; i < gd->hsize; i++) {
1.158 nicm 657: gl = grid_get_line(gd, i);
1.80 nicm 658: size += gl->cellsize * sizeof *gl->celldata;
1.95 nicm 659: size += gl->extdsize * sizeof *gl->extddata;
1.80 nicm 660: }
1.158 nicm 661: size += gd->hsize * sizeof *gl;
1.80 nicm 662:
663: xasprintf(&fe->value, "%llu", size);
664: }
665:
666: /* Callback for pane_tabs. */
1.108 nicm 667: static void
1.80 nicm 668: format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe)
669: {
670: struct window_pane *wp = ft->wp;
671: struct evbuffer *buffer;
672: u_int i;
673: int size;
674:
675: if (wp == NULL)
676: return;
677:
678: buffer = evbuffer_new();
1.165 nicm 679: if (buffer == NULL)
680: fatalx("out of memory");
1.80 nicm 681: for (i = 0; i < wp->base.grid->sx; i++) {
682: if (!bit_test(wp->base.tabs, i))
683: continue;
684:
685: if (EVBUFFER_LENGTH(buffer) > 0)
686: evbuffer_add(buffer, ",", 1);
687: evbuffer_add_printf(buffer, "%u", i);
688: }
1.148 nicm 689: if ((size = EVBUFFER_LENGTH(buffer)) != 0)
690: xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
691: evbuffer_free(buffer);
692: }
693:
1.150 nicm 694: /* Callback for session_group_list. */
1.148 nicm 695: static void
1.150 nicm 696: format_cb_session_group_list(struct format_tree *ft, struct format_entry *fe)
1.148 nicm 697: {
698: struct session *s = ft->s;
699: struct session_group *sg;
700: struct session *loop;
701: struct evbuffer *buffer;
702: int size;
703:
704: if (s == NULL)
705: return;
706: sg = session_group_contains(s);
707: if (sg == NULL)
708: return;
709:
710: buffer = evbuffer_new();
1.165 nicm 711: if (buffer == NULL)
712: fatalx("out of memory");
1.148 nicm 713: TAILQ_FOREACH(loop, &sg->sessions, gentry) {
714: if (EVBUFFER_LENGTH(buffer) > 0)
715: evbuffer_add(buffer, ",", 1);
716: evbuffer_add_printf(buffer, "%s", loop->name);
717: }
718: if ((size = EVBUFFER_LENGTH(buffer)) != 0)
719: xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
1.80 nicm 720: evbuffer_free(buffer);
721: }
722:
1.168 nicm 723: /* Callback for pane_in_mode. */
724: static void
725: format_cb_pane_in_mode(struct format_tree *ft, struct format_entry *fe)
726: {
727: struct window_pane *wp = ft->wp;
728: u_int n = 0;
729: struct window_mode_entry *wme;
730:
731: if (wp == NULL)
732: return;
733:
734: TAILQ_FOREACH(wme, &wp->modes, entry)
735: n++;
736: xasprintf(&fe->value, "%u", n);
737: }
738:
1.186 nicm 739: /* Callback for cursor_character. */
740: static void
741: format_cb_cursor_character(struct format_tree *ft, struct format_entry *fe)
742: {
743: struct window_pane *wp = ft->wp;
744: struct grid_cell gc;
745:
746: if (wp == NULL)
747: return;
748:
749: grid_view_get_cell(wp->base.grid, wp->base.cx, wp->base.cy, &gc);
750: if (~gc.flags & GRID_FLAG_PADDING)
751: xasprintf(&fe->value, "%.*s", (int)gc.data.size, gc.data.data);
752: }
753:
1.197 nicm 754: /* Callback for mouse_word. */
755: static void
756: format_cb_mouse_word(struct format_tree *ft, struct format_entry *fe)
757: {
758: struct window_pane *wp;
759: u_int x, y, end;
760: struct grid *gd;
761: struct grid_line *gl;
762: struct grid_cell gc;
763: const char *ws;
764: struct utf8_data *ud = NULL;
765: size_t size = 0;
766: int found = 0;
767:
768: if (!ft->m.valid)
769: return;
770: wp = cmd_mouse_pane(&ft->m, NULL, NULL);
771: if (wp == NULL)
772: return;
773: if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
774: return;
775: gd = wp->base.grid;
776: ws = options_get_string(global_s_options, "word-separators");
777:
778: y = gd->hsize + y;
779: for (;;) {
780: grid_get_cell(gd, x, y, &gc);
781: if (gc.flags & GRID_FLAG_PADDING)
782: break;
783: if (utf8_cstrhas(ws, &gc.data)) {
784: found = 1;
785: break;
786: }
787:
788: if (x == 0) {
789: if (y == 0)
790: break;
791: gl = &gd->linedata[y - 1];
792: if (~gl->flags & GRID_LINE_WRAPPED)
793: break;
794: y--;
795: x = grid_line_length(gd, y);
796: if (x == 0)
797: break;
798: }
799: x--;
800: }
801: for (;;) {
802: if (found) {
803: end = grid_line_length(gd, y);
804: if (end == 0 || x == end - 1) {
805: if (y == gd->hsize + gd->sy - 1)
806: break;
807: gl = &gd->linedata[y];
808: if (~gl->flags & GRID_LINE_WRAPPED)
809: break;
810: y++;
811: x = 0;
812: } else
813: x++;
814: }
815: found = 1;
816:
817: grid_get_cell(gd, x, y, &gc);
818: if (gc.flags & GRID_FLAG_PADDING)
819: break;
820: if (utf8_cstrhas(ws, &gc.data))
821: break;
822:
823: ud = xreallocarray(ud, size + 2, sizeof *ud);
824: memcpy(&ud[size++], &gc.data, sizeof *ud);
825: }
826: if (size != 0) {
827: ud[size].size = 0;
828: fe->value = utf8_tocstr(ud);
829: free(ud);
830: }
831: }
832:
833: /* Callback for mouse_line. */
834: static void
835: format_cb_mouse_line(struct format_tree *ft, struct format_entry *fe)
836: {
837: struct window_pane *wp;
838: u_int x, y;
839: struct grid *gd;
840: struct grid_cell gc;
841: struct utf8_data *ud = NULL;
842: size_t size = 0;
843:
844: if (!ft->m.valid)
845: return;
846: wp = cmd_mouse_pane(&ft->m, NULL, NULL);
847: if (wp == NULL)
848: return;
849: if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
850: return;
851: gd = wp->base.grid;
852:
853: y = gd->hsize + y;
854: for (x = 0; x < grid_line_length(gd, y); x++) {
855: grid_get_cell(gd, x, y, &gc);
856: if (gc.flags & GRID_FLAG_PADDING)
857: break;
858:
859: ud = xreallocarray(ud, size + 2, sizeof *ud);
860: memcpy(&ud[size++], &gc.data, sizeof *ud);
861: }
862: if (size != 0) {
863: ud[size].size = 0;
864: fe->value = utf8_tocstr(ud);
865: free(ud);
866: }
867: }
868:
1.112 nicm 869: /* Merge a format tree. */
870: static void
871: format_merge(struct format_tree *ft, struct format_tree *from)
872: {
873: struct format_entry *fe;
874:
875: RB_FOREACH(fe, format_entry_tree, &from->tree) {
876: if (fe->value != NULL)
877: format_add(ft, fe->key, "%s", fe->value);
878: }
879: }
880:
1.196 nicm 881: /* Add item bits to tree. */
882: static void
883: format_create_add_item(struct format_tree *ft, struct cmdq_item *item)
884: {
1.197 nicm 885: struct mouse_event *m;
886: struct window_pane *wp;
887: u_int x, y;
888:
1.196 nicm 889: if (item->cmd != NULL)
890: format_add(ft, "command", "%s", item->cmd->entry->name);
1.197 nicm 891:
892: if (item->shared == NULL)
893: return;
894: if (item->shared->formats != NULL)
1.196 nicm 895: format_merge(ft, item->shared->formats);
1.197 nicm 896:
897: m = &item->shared->mouse;
898: if (m->valid && ((wp = cmd_mouse_pane(m, NULL, NULL)) != NULL)) {
899: format_add(ft, "mouse_pane", "%%%u", wp->id);
900: if (cmd_mouse_at(wp, m, &x, &y, 0) == 0) {
901: format_add(ft, "mouse_x", "%u", x);
902: format_add(ft, "mouse_y", "%u", y);
903: format_add_cb(ft, "mouse_word", format_cb_mouse_word);
904: format_add_cb(ft, "mouse_line", format_cb_mouse_line);
905: }
906: }
907: memcpy(&ft->m, m, sizeof ft->m);
1.196 nicm 908: }
909:
1.1 nicm 910: /* Create a new tree. */
911: struct format_tree *
1.131 nicm 912: format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
1.68 nicm 913: {
1.184 nicm 914: struct format_tree *ft;
915: const struct window_mode **wm;
916: char tmp[64];
1.77 nicm 917:
918: if (!event_initialized(&format_job_event)) {
919: evtimer_set(&format_job_event, format_job_timer, NULL);
920: format_job_timer(-1, 0, NULL);
921: }
1.1 nicm 922:
1.54 nicm 923: ft = xcalloc(1, sizeof *ft);
924: RB_INIT(&ft->tree);
1.120 nicm 925:
1.131 nicm 926: if (c != NULL) {
927: ft->client = c;
928: ft->client->references++;
929: }
1.172 nicm 930: ft->item = item;
1.131 nicm 931:
1.120 nicm 932: ft->tag = tag;
1.84 nicm 933: ft->flags = flags;
1.177 nicm 934: ft->time = time(NULL);
1.1 nicm 935:
1.79 nicm 936: format_add_cb(ft, "host", format_cb_host);
937: format_add_cb(ft, "host_short", format_cb_host_short);
938: format_add_cb(ft, "pid", format_cb_pid);
1.100 nicm 939: format_add(ft, "socket_path", "%s", socket_path);
940: format_add_tv(ft, "start_time", &start_time);
1.102 nicm 941:
1.184 nicm 942: for (wm = all_window_modes; *wm != NULL; wm++) {
943: if ((*wm)->default_format != NULL) {
944: xsnprintf(tmp, sizeof tmp, "%s_format", (*wm)->name);
945: tmp[strcspn(tmp, "-")] = '_';
946: format_add(ft, tmp, "%s", (*wm)->default_format);
947: }
948: }
1.192 nicm 949:
950: format_add(ft, "client_menu", "%s", DEFAULT_CLIENT_MENU);
951: format_add(ft, "session_menu", "%s", DEFAULT_SESSION_MENU);
952: format_add(ft, "window_menu", "%s", DEFAULT_WINDOW_MENU);
953: format_add(ft, "pane_menu", "%s", DEFAULT_PANE_MENU);
1.184 nicm 954:
1.196 nicm 955: if (item != NULL)
956: format_create_add_item(ft, item);
1.1 nicm 957:
958: return (ft);
959: }
960:
961: /* Free a tree. */
962: void
963: format_free(struct format_tree *ft)
964: {
1.54 nicm 965: struct format_entry *fe, *fe1;
1.1 nicm 966:
1.68 nicm 967: RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
968: RB_REMOVE(format_entry_tree, &ft->tree, fe);
1.9 nicm 969: free(fe->value);
970: free(fe->key);
971: free(fe);
1.1 nicm 972: }
973:
1.131 nicm 974: if (ft->client != NULL)
975: server_client_unref(ft->client);
1.25 nicm 976: free(ft);
1.1 nicm 977: }
1.184 nicm 978:
979: /* Walk each format. */
980: void
981: format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
982: void *), void *arg)
983: {
984: struct format_entry *fe;
985: static char s[64];
986:
987: RB_FOREACH(fe, format_entry_tree, &ft->tree) {
988: if (fe->t != 0) {
989: xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
990: cb(fe->key, fe->value, s);
991: } else {
992: if (fe->value == NULL && fe->cb != NULL) {
993: fe->cb(ft, fe);
994: if (fe->value == NULL)
995: fe->value = xstrdup("");
996: }
997: cb(fe->key, fe->value, arg);
998: }
999: }
1000: }
1001:
1.1 nicm 1002:
1003: /* Add a key-value pair. */
1004: void
1005: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
1006: {
1007: struct format_entry *fe;
1.28 nicm 1008: struct format_entry *fe_now;
1.1 nicm 1009: va_list ap;
1010:
1011: fe = xmalloc(sizeof *fe);
1012: fe->key = xstrdup(key);
1013:
1.79 nicm 1014: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1015: if (fe_now != NULL) {
1016: free(fe->key);
1017: free(fe);
1018: free(fe_now->value);
1019: fe = fe_now;
1020: }
1021:
1022: fe->cb = NULL;
1.87 nicm 1023: fe->t = 0;
1.79 nicm 1024:
1.1 nicm 1025: va_start(ap, fmt);
1026: xvasprintf(&fe->value, fmt, ap);
1027: va_end(ap);
1.79 nicm 1028: }
1029:
1.87 nicm 1030: /* Add a key and time. */
1.108 nicm 1031: static void
1.87 nicm 1032: format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
1033: {
1034: struct format_entry *fe;
1035: struct format_entry *fe_now;
1036:
1037: fe = xmalloc(sizeof *fe);
1038: fe->key = xstrdup(key);
1039:
1040: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1041: if (fe_now != NULL) {
1042: free(fe->key);
1043: free(fe);
1044: free(fe_now->value);
1045: fe = fe_now;
1046: }
1047:
1048: fe->cb = NULL;
1049: fe->t = tv->tv_sec;
1050:
1051: fe->value = NULL;
1052: }
1053:
1.79 nicm 1054: /* Add a key and function. */
1.108 nicm 1055: static void
1.79 nicm 1056: format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
1057: {
1058: struct format_entry *fe;
1059: struct format_entry *fe_now;
1060:
1061: fe = xmalloc(sizeof *fe);
1062: fe->key = xstrdup(key);
1.1 nicm 1063:
1.68 nicm 1064: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1.28 nicm 1065: if (fe_now != NULL) {
1066: free(fe->key);
1067: free(fe);
1.79 nicm 1068: free(fe_now->value);
1069: fe = fe_now;
1.28 nicm 1070: }
1.79 nicm 1071:
1072: fe->cb = cb;
1.87 nicm 1073: fe->t = 0;
1.79 nicm 1074:
1075: fe->value = NULL;
1.1 nicm 1076: }
1077:
1.161 nicm 1078: /* Quote special characters in string. */
1079: static char *
1080: format_quote(const char *s)
1081: {
1082: const char *cp;
1083: char *out, *at;
1084:
1085: at = out = xmalloc(strlen(s) * 2 + 1);
1086: for (cp = s; *cp != '\0'; cp++) {
1087: if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
1088: *at++ = '\\';
1089: *at++ = *cp;
1090: }
1091: *at = '\0';
1092: return (out);
1093: }
1094:
1.1 nicm 1095: /* Find a format entry. */
1.108 nicm 1096: static char *
1.87 nicm 1097: format_find(struct format_tree *ft, const char *key, int modifiers)
1.1 nicm 1098: {
1099: struct format_entry *fe, fe_find;
1.76 nicm 1100: struct environ_entry *envent;
1.87 nicm 1101: static char s[64];
1.117 nicm 1102: struct options_entry *o;
1.116 nicm 1103: int idx;
1.189 nicm 1104: char *found, *saved;
1.87 nicm 1105:
1106: if (~modifiers & FORMAT_TIMESTRING) {
1.116 nicm 1107: o = options_parse_get(global_options, key, &idx, 0);
1.87 nicm 1108: if (o == NULL && ft->w != NULL)
1.116 nicm 1109: o = options_parse_get(ft->w->options, key, &idx, 0);
1.87 nicm 1110: if (o == NULL)
1.116 nicm 1111: o = options_parse_get(global_w_options, key, &idx, 0);
1.87 nicm 1112: if (o == NULL && ft->s != NULL)
1.116 nicm 1113: o = options_parse_get(ft->s->options, key, &idx, 0);
1.87 nicm 1114: if (o == NULL)
1.116 nicm 1115: o = options_parse_get(global_s_options, key, &idx, 0);
1.87 nicm 1116: if (o != NULL) {
1.118 nicm 1117: found = options_tostring(o, idx, 1);
1.116 nicm 1118: goto found;
1.54 nicm 1119: }
1120: }
1.116 nicm 1121: found = NULL;
1.1 nicm 1122:
1123: fe_find.key = (char *) key;
1.68 nicm 1124: fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.79 nicm 1125: if (fe != NULL) {
1.87 nicm 1126: if (modifiers & FORMAT_TIMESTRING) {
1127: if (fe->t == 0)
1128: return (NULL);
1129: ctime_r(&fe->t, s);
1130: s[strcspn(s, "\n")] = '\0';
1.189 nicm 1131: found = xstrdup(s);
1.87 nicm 1132: goto found;
1133: }
1134: if (fe->t != 0) {
1.189 nicm 1135: xasprintf(&found, "%lld", (long long)fe->t);
1.87 nicm 1136: goto found;
1137: }
1.149 nicm 1138: if (fe->value == NULL && fe->cb != NULL) {
1.79 nicm 1139: fe->cb(ft, fe);
1.149 nicm 1140: if (fe->value == NULL)
1141: fe->value = xstrdup("");
1142: }
1.189 nicm 1143: found = xstrdup(fe->value);
1.87 nicm 1144: goto found;
1.79 nicm 1145: }
1.76 nicm 1146:
1.87 nicm 1147: if (~modifiers & FORMAT_TIMESTRING) {
1148: envent = NULL;
1149: if (ft->s != NULL)
1.91 nicm 1150: envent = environ_find(ft->s->environ, key);
1.87 nicm 1151: if (envent == NULL)
1.91 nicm 1152: envent = environ_find(global_environ, key);
1.87 nicm 1153: if (envent != NULL) {
1.189 nicm 1154: found = xstrdup(envent->value);
1.87 nicm 1155: goto found;
1156: }
1157: }
1.76 nicm 1158:
1159: return (NULL);
1.87 nicm 1160:
1161: found:
1.88 nicm 1162: if (found == NULL)
1163: return (NULL);
1.87 nicm 1164: if (modifiers & FORMAT_BASENAME) {
1.189 nicm 1165: saved = found;
1166: found = xstrdup(basename(saved));
1.87 nicm 1167: free(saved);
1168: }
1169: if (modifiers & FORMAT_DIRNAME) {
1.189 nicm 1170: saved = found;
1171: found = xstrdup(dirname(saved));
1.87 nicm 1172: free(saved);
1173: }
1.161 nicm 1174: if (modifiers & FORMAT_QUOTE) {
1.189 nicm 1175: saved = found;
1176: found = xstrdup(format_quote(saved));
1.161 nicm 1177: free(saved);
1178: }
1.189 nicm 1179: return (found);
1.1 nicm 1180: }
1181:
1.155 nicm 1182: /* Skip until end. */
1.185 nicm 1183: const char *
1.169 nicm 1184: format_skip(const char *s, const char *end)
1.114 nicm 1185: {
1186: int brackets = 0;
1187:
1188: for (; *s != '\0'; s++) {
1.155 nicm 1189: if (*s == '#' && s[1] == '{')
1.114 nicm 1190: brackets++;
1.155 nicm 1191: if (*s == '#' && strchr(",#{}", s[1]) != NULL) {
1192: s++;
1193: continue;
1194: }
1.114 nicm 1195: if (*s == '}')
1196: brackets--;
1.169 nicm 1197: if (strchr(end, *s) != NULL && brackets == 0)
1.114 nicm 1198: break;
1199: }
1200: if (*s == '\0')
1201: return (NULL);
1202: return (s);
1203: }
1204:
1205: /* Return left and right alternatives separated by commas. */
1206: static int
1.169 nicm 1207: format_choose(struct format_tree *ft, const char *s, char **left, char **right,
1208: int expand)
1.114 nicm 1209: {
1.169 nicm 1210: const char *cp;
1211: char *left0, *right0;
1.114 nicm 1212:
1.169 nicm 1213: cp = format_skip(s, ",");
1.114 nicm 1214: if (cp == NULL)
1215: return (-1);
1.169 nicm 1216: left0 = xstrndup(s, cp - s);
1217: right0 = xstrdup(cp + 1);
1.114 nicm 1218:
1.169 nicm 1219: if (expand) {
1220: *left = format_expand(ft, left0);
1.188 nicm 1221: free(left0);
1.169 nicm 1222: *right = format_expand(ft, right0);
1.188 nicm 1223: free(right0);
1.169 nicm 1224: } else {
1225: *left = left0;
1226: *right = right0;
1227: }
1.114 nicm 1228: return (0);
1229: }
1230:
1231: /* Is this true? */
1.141 nicm 1232: int
1.114 nicm 1233: format_true(const char *s)
1234: {
1235: if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
1236: return (1);
1237: return (0);
1238: }
1239:
1.169 nicm 1240: /* Check if modifier end. */
1241: static int
1242: format_is_end(char c)
1243: {
1244: return (c == ';' || c == ':');
1245: }
1246:
1247: /* Add to modifier list. */
1248: static void
1249: format_add_modifier(struct format_modifier **list, u_int *count,
1250: const char *c, size_t n, char **argv, int argc)
1251: {
1252: struct format_modifier *fm;
1253:
1254: *list = xreallocarray(*list, (*count) + 1, sizeof **list);
1255: fm = &(*list)[(*count)++];
1256:
1257: memcpy(fm->modifier, c, n);
1258: fm->modifier[n] = '\0';
1259: fm->size = n;
1260:
1261: fm->argv = argv;
1262: fm->argc = argc;
1263: }
1264:
1265: /* Free modifier list. */
1266: static void
1267: format_free_modifiers(struct format_modifier *list, u_int count)
1268: {
1269: u_int i;
1270:
1271: for (i = 0; i < count; i++)
1272: cmd_free_argv(list[i].argc, list[i].argv);
1273: free(list);
1274: }
1275:
1276: /* Build modifier list. */
1277: static struct format_modifier *
1278: format_build_modifiers(struct format_tree *ft, const char **s, u_int *count)
1279: {
1280: const char *cp = *s, *end;
1281: struct format_modifier *list = NULL;
1282: char c, last[] = "X;:", **argv, *value;
1283: int argc;
1284:
1285: /*
1286: * Modifiers are a ; separated list of the forms:
1.195 nicm 1287: * l,m,C,b,d,t,q,E,T,S,W,P,<,>
1.169 nicm 1288: * =a
1289: * =/a
1290: * =/a/
1291: * s/a/b/
1292: * s/a/b
1.195 nicm 1293: * ||,&&,!=,==,<=,>=
1.169 nicm 1294: */
1295:
1296: *count = 0;
1297:
1298: while (*cp != '\0' && *cp != ':') {
1299: /* Skip and separator character. */
1300: if (*cp == ';')
1301: cp++;
1302:
1303: /* Check single character modifiers with no arguments. */
1.195 nicm 1304: if (strchr("lmCbdtqETSWP<>", cp[0]) != NULL &&
1.172 nicm 1305: format_is_end(cp[1])) {
1.169 nicm 1306: format_add_modifier(&list, count, cp, 1, NULL, 0);
1307: cp++;
1308: continue;
1309: }
1310:
1311: /* Then try double character with no arguments. */
1312: if ((memcmp("||", cp, 2) == 0 ||
1313: memcmp("&&", cp, 2) == 0 ||
1314: memcmp("!=", cp, 2) == 0 ||
1.195 nicm 1315: memcmp("==", cp, 2) == 0 ||
1316: memcmp("<=", cp, 2) == 0 ||
1317: memcmp(">=", cp, 2) == 0) &&
1.169 nicm 1318: format_is_end(cp[2])) {
1319: format_add_modifier(&list, count, cp, 2, NULL, 0);
1320: cp += 2;
1321: continue;
1322: }
1323:
1324: /* Now try single character with arguments. */
1325: if (strchr("s=", cp[0]) == NULL)
1326: break;
1327: c = cp[0];
1328:
1329: /* No arguments provided. */
1330: if (format_is_end(cp[1])) {
1331: format_add_modifier(&list, count, cp, 1, NULL, 0);
1332: cp++;
1333: continue;
1334: }
1335: argv = NULL;
1336: argc = 0;
1337:
1338: /* Single argument with no wrapper character. */
1339: if (!ispunct(cp[1]) || cp[1] == '-') {
1340: end = format_skip(cp + 1, ":;");
1341: if (end == NULL)
1342: break;
1343:
1344: argv = xcalloc(1, sizeof *argv);
1345: value = xstrndup(cp + 1, end - (cp + 1));
1346: argv[0] = format_expand(ft, value);
1347: free(value);
1348: argc = 1;
1349:
1350: format_add_modifier(&list, count, &c, 1, argv, argc);
1351: cp = end;
1352: continue;
1353: }
1354:
1355: /* Multiple arguments with a wrapper character. */
1356: last[0] = cp[1];
1357: cp++;
1358: do {
1359: if (cp[0] == last[0] && format_is_end(cp[1])) {
1360: cp++;
1361: break;
1362: }
1363: end = format_skip(cp + 1, last);
1364: if (end == NULL)
1365: break;
1366: cp++;
1367:
1368: argv = xreallocarray (argv, argc + 1, sizeof *argv);
1369: value = xstrndup(cp, end - cp);
1370: argv[argc++] = format_expand(ft, value);
1371: free(value);
1372:
1373: cp = end;
1374: } while (!format_is_end(cp[0]));
1375: format_add_modifier(&list, count, &c, 1, argv, argc);
1376: }
1377: if (*cp != ':') {
1378: format_free_modifiers(list, *count);
1379: *count = 0;
1380: return (NULL);
1381: }
1382: *s = cp + 1;
1383: return list;
1384: }
1385:
1386: /* Perform substitution in string. */
1387: static char *
1388: format_substitute(const char *source, const char *from, const char *to)
1389: {
1390: char *copy, *new;
1391: const char *cp;
1392: size_t fromlen, tolen, newlen, used;
1393:
1394: fromlen = strlen(from);
1395: tolen = strlen(to);
1396:
1397: newlen = strlen(source) + 1;
1398: copy = new = xmalloc(newlen);
1399:
1400: for (cp = source; *cp != '\0'; /* nothing */) {
1401: if (strncmp(cp, from, fromlen) != 0) {
1402: *new++ = *cp++;
1403: continue;
1404: }
1405: used = new - copy;
1406:
1407: newlen += tolen;
1408: copy = xrealloc(copy, newlen);
1409:
1410: new = copy + used;
1411: memcpy(new, to, tolen);
1412:
1413: new += tolen;
1414: cp += fromlen;
1415: }
1416:
1417: *new = '\0';
1418: return (copy);
1419: }
1420:
1.172 nicm 1421: /* Loop over sessions. */
1422: static char *
1423: format_loop_sessions(struct format_tree *ft, const char *fmt)
1424: {
1.180 nicm 1425: struct client *c = ft->client;
1.172 nicm 1426: struct cmdq_item *item = ft->item;
1.180 nicm 1427: struct format_tree *nft;
1.172 nicm 1428: char *expanded, *value;
1429: size_t valuelen;
1430: struct session *s;
1431:
1432: value = xcalloc(1, 1);
1433: valuelen = 1;
1434:
1435: RB_FOREACH(s, sessions, &sessions) {
1.179 nicm 1436: format_log(ft, "session loop: $%u", s->id);
1.180 nicm 1437: nft = format_create(c, item, FORMAT_NONE, ft->flags);
1.182 nicm 1438: nft->loop = ft->loop;
1.180 nicm 1439: format_defaults(nft, ft->c, s, NULL, NULL);
1440: expanded = format_expand(nft, fmt);
1441: format_free(nft);
1.172 nicm 1442:
1443: valuelen += strlen(expanded);
1444: value = xrealloc(value, valuelen);
1445:
1446: strlcat(value, expanded, valuelen);
1447: free(expanded);
1448: }
1449:
1450: return (value);
1451: }
1452:
1453: /* Loop over windows. */
1454: static char *
1455: format_loop_windows(struct format_tree *ft, const char *fmt)
1456: {
1.180 nicm 1457: struct client *c = ft->client;
1.172 nicm 1458: struct cmdq_item *item = ft->item;
1.180 nicm 1459: struct format_tree *nft;
1.172 nicm 1460: char *all, *active, *use, *expanded, *value;
1461: size_t valuelen;
1462: struct winlink *wl;
1.180 nicm 1463: struct window *w;
1.172 nicm 1464:
1.179 nicm 1465: if (ft->s == NULL) {
1466: format_log(ft, "window loop but no session");
1.172 nicm 1467: return (NULL);
1.179 nicm 1468: }
1.172 nicm 1469:
1470: if (format_choose(ft, fmt, &all, &active, 0) != 0) {
1471: all = xstrdup(fmt);
1472: active = NULL;
1473: }
1474:
1475: value = xcalloc(1, 1);
1476: valuelen = 1;
1477:
1478: RB_FOREACH(wl, winlinks, &ft->s->windows) {
1.180 nicm 1479: w = wl->window;
1480: format_log(ft, "window loop: %u @%u", wl->idx, w->id);
1.172 nicm 1481: if (active != NULL && wl == ft->s->curw)
1482: use = active;
1483: else
1484: use = all;
1.180 nicm 1485: nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
1.182 nicm 1486: nft->loop = ft->loop;
1.180 nicm 1487: format_defaults(nft, ft->c, ft->s, wl, NULL);
1488: expanded = format_expand(nft, use);
1489: format_free(nft);
1.172 nicm 1490:
1491: valuelen += strlen(expanded);
1492: value = xrealloc(value, valuelen);
1493:
1494: strlcat(value, expanded, valuelen);
1495: free(expanded);
1496: }
1497:
1498: free(active);
1499: free(all);
1500:
1501: return (value);
1502: }
1503:
1504: /* Loop over panes. */
1505: static char *
1506: format_loop_panes(struct format_tree *ft, const char *fmt)
1507: {
1.180 nicm 1508: struct client *c = ft->client;
1.172 nicm 1509: struct cmdq_item *item = ft->item;
1.180 nicm 1510: struct format_tree *nft;
1.172 nicm 1511: char *all, *active, *use, *expanded, *value;
1512: size_t valuelen;
1513: struct window_pane *wp;
1514:
1.179 nicm 1515: if (ft->w == NULL) {
1516: format_log(ft, "pane loop but no window");
1.172 nicm 1517: return (NULL);
1.179 nicm 1518: }
1.172 nicm 1519:
1520: if (format_choose(ft, fmt, &all, &active, 0) != 0) {
1521: all = xstrdup(fmt);
1522: active = NULL;
1523: }
1524:
1525: value = xcalloc(1, 1);
1526: valuelen = 1;
1527:
1528: TAILQ_FOREACH(wp, &ft->w->panes, entry) {
1.179 nicm 1529: format_log(ft, "pane loop: %%%u", wp->id);
1.172 nicm 1530: if (active != NULL && wp == ft->w->active)
1531: use = active;
1532: else
1533: use = all;
1.180 nicm 1534: nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
1.182 nicm 1535: nft->loop = ft->loop;
1.180 nicm 1536: format_defaults(nft, ft->c, ft->s, ft->wl, wp);
1537: expanded = format_expand(nft, use);
1538: format_free(nft);
1.172 nicm 1539:
1540: valuelen += strlen(expanded);
1541: value = xrealloc(value, valuelen);
1542:
1543: strlcat(value, expanded, valuelen);
1544: free(expanded);
1545: }
1546:
1547: free(active);
1548: free(all);
1549:
1550: return (value);
1551: }
1552:
1.140 nicm 1553: /* Replace a key. */
1.108 nicm 1554: static int
1.30 nicm 1555: format_replace(struct format_tree *ft, const char *key, size_t keylen,
1556: char **buf, size_t *len, size_t *off)
1.1 nicm 1557: {
1.140 nicm 1558: struct window_pane *wp = ft->wp;
1.196 nicm 1559: const char *errptr, *copy, *cp, *marker;
1.169 nicm 1560: char *copy0, *condition, *found, *new;
1561: char *value, *left, *right;
1562: size_t valuelen;
1563: int modifiers = 0, limit = 0;
1564: struct format_modifier *list, *fm, *cmp = NULL, *search = NULL;
1565: struct format_modifier *sub = NULL;
1566: u_int i, count;
1.179 nicm 1567: int j;
1.1 nicm 1568:
1569: /* Make a copy of the key. */
1.169 nicm 1570: copy = copy0 = xstrndup(key, keylen);
1571:
1572: /* Process modifier list. */
1573: list = format_build_modifiers(ft, ©, &count);
1574: for (i = 0; i < count; i++) {
1575: fm = &list[i];
1.179 nicm 1576: if (format_logging(ft)) {
1577: format_log(ft, "modifier %u is %s", i, fm->modifier);
1578: for (j = 0; j < fm->argc; j++) {
1579: format_log(ft, "modifier %u argument %d: %s", i,
1580: j, fm->argv[j]);
1581: }
1582: }
1.169 nicm 1583: if (fm->size == 1) {
1584: switch (fm->modifier[0]) {
1585: case 'm':
1.195 nicm 1586: case '<':
1587: case '>':
1.169 nicm 1588: cmp = fm;
1589: break;
1590: case 'C':
1591: search = fm;
1592: break;
1593: case 's':
1594: if (fm->argc != 2)
1595: break;
1596: sub = fm;
1597: break;
1598: case '=':
1.196 nicm 1599: if (fm->argc != 1 && fm->argc != 2)
1.169 nicm 1600: break;
1601: limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
1602: &errptr);
1603: if (errptr != NULL)
1604: limit = 0;
1.196 nicm 1605: if (fm->argc == 2 && fm->argv[1] != NULL)
1606: marker = fm->argv[1];
1607: else
1608: marker = NULL;
1.169 nicm 1609: break;
1610: case 'l':
1611: modifiers |= FORMAT_LITERAL;
1612: break;
1613: case 'b':
1614: modifiers |= FORMAT_BASENAME;
1615: break;
1616: case 'd':
1617: modifiers |= FORMAT_DIRNAME;
1618: break;
1619: case 't':
1620: modifiers |= FORMAT_TIMESTRING;
1621: break;
1622: case 'q':
1623: modifiers |= FORMAT_QUOTE;
1624: break;
1.170 nicm 1625: case 'E':
1626: modifiers |= FORMAT_EXPAND;
1627: break;
1.175 nicm 1628: case 'T':
1629: modifiers |= FORMAT_EXPANDTIME;
1630: break;
1.172 nicm 1631: case 'S':
1632: modifiers |= FORMAT_SESSIONS;
1633: break;
1634: case 'W':
1635: modifiers |= FORMAT_WINDOWS;
1636: break;
1637: case 'P':
1638: modifiers |= FORMAT_PANES;
1639: break;
1.169 nicm 1640: }
1641: } else if (fm->size == 2) {
1642: if (strcmp(fm->modifier, "||") == 0 ||
1643: strcmp(fm->modifier, "&&") == 0 ||
1644: strcmp(fm->modifier, "==") == 0 ||
1.195 nicm 1645: strcmp(fm->modifier, "!=") == 0 ||
1646: strcmp(fm->modifier, ">=") == 0 ||
1647: strcmp(fm->modifier, "<=") == 0)
1.169 nicm 1648: cmp = fm;
1.98 nicm 1649: }
1.30 nicm 1650: }
1651:
1.155 nicm 1652: /* Is this a literal string? */
1.169 nicm 1653: if (modifiers & FORMAT_LITERAL) {
1.155 nicm 1654: value = xstrdup(copy);
1655: goto done;
1656: }
1657:
1.172 nicm 1658: /* Is this a loop, comparison or condition? */
1659: if (modifiers & FORMAT_SESSIONS) {
1660: value = format_loop_sessions(ft, copy);
1661: if (value == NULL)
1662: goto fail;
1663: } else if (modifiers & FORMAT_WINDOWS) {
1664: value = format_loop_windows(ft, copy);
1665: if (value == NULL)
1666: goto fail;
1667: } else if (modifiers & FORMAT_PANES) {
1668: value = format_loop_panes(ft, copy);
1669: if (value == NULL)
1670: goto fail;
1671: } else if (search != NULL) {
1.140 nicm 1672: /* Search in pane. */
1.179 nicm 1673: if (wp == NULL) {
1674: format_log(ft, "search '%s' but no pane", copy);
1.140 nicm 1675: value = xstrdup("0");
1.179 nicm 1676: } else {
1677: format_log(ft, "search '%s' pane %%%u", copy, wp->id);
1.140 nicm 1678: xasprintf(&value, "%u", window_pane_search(wp, copy));
1.179 nicm 1679: }
1.169 nicm 1680: } else if (cmp != NULL) {
1681: /* Comparison of left and right. */
1.179 nicm 1682: if (format_choose(ft, copy, &left, &right, 1) != 0) {
1683: format_log(ft, "compare %s syntax error: %s",
1684: cmp->modifier, copy);
1.114 nicm 1685: goto fail;
1.179 nicm 1686: }
1687: format_log(ft, "compare %s left is: %s", cmp->modifier, left);
1688: format_log(ft, "compare %s right is: %s", cmp->modifier, right);
1.169 nicm 1689:
1690: if (strcmp(cmp->modifier, "||") == 0) {
1691: if (format_true(left) || format_true(right))
1692: value = xstrdup("1");
1693: else
1694: value = xstrdup("0");
1695: } else if (strcmp(cmp->modifier, "&&") == 0) {
1696: if (format_true(left) && format_true(right))
1697: value = xstrdup("1");
1698: else
1699: value = xstrdup("0");
1700: } else if (strcmp(cmp->modifier, "==") == 0) {
1701: if (strcmp(left, right) == 0)
1702: value = xstrdup("1");
1703: else
1704: value = xstrdup("0");
1705: } else if (strcmp(cmp->modifier, "!=") == 0) {
1706: if (strcmp(left, right) != 0)
1707: value = xstrdup("1");
1708: else
1709: value = xstrdup("0");
1.195 nicm 1710: } else if (strcmp(cmp->modifier, "<") == 0) {
1711: if (strcmp(left, right) < 0)
1712: value = xstrdup("1");
1713: else
1714: value = xstrdup("0");
1715: } else if (strcmp(cmp->modifier, ">") == 0) {
1716: if (strcmp(left, right) > 0)
1717: value = xstrdup("1");
1718: else
1719: value = xstrdup("0");
1720: } else if (strcmp(cmp->modifier, "<=") == 0) {
1721: if (strcmp(left, right) <= 0)
1722: value = xstrdup("1");
1723: else
1724: value = xstrdup("0");
1725: } else if (strcmp(cmp->modifier, ">=") == 0) {
1726: if (strcmp(left, right) >= 0)
1727: value = xstrdup("1");
1728: else
1729: value = xstrdup("0");
1730: } else if (strcmp(cmp->modifier, "m") == 0) {
1.169 nicm 1731: if (fnmatch(left, right, 0) == 0)
1732: value = xstrdup("1");
1733: else
1734: value = xstrdup("0");
1735: }
1736:
1.114 nicm 1737: free(right);
1738: free(left);
1739: } else if (*copy == '?') {
1740: /* Conditional: check first and choose second or third. */
1.169 nicm 1741: cp = format_skip(copy + 1, ",");
1.179 nicm 1742: if (cp == NULL) {
1743: format_log(ft, "condition syntax error: %s", copy + 1);
1.1 nicm 1744: goto fail;
1.179 nicm 1745: }
1.169 nicm 1746: condition = xstrndup(copy + 1, cp - (copy + 1));
1.179 nicm 1747: format_log(ft, "condition is: %s", condition);
1.1 nicm 1748:
1.169 nicm 1749: found = format_find(ft, condition, modifiers);
1.156 nicm 1750: if (found == NULL) {
1751: /*
1.169 nicm 1752: * If the condition not found, try to expand it. If
1.156 nicm 1753: * the expansion doesn't have any effect, then assume
1754: * false.
1755: */
1.169 nicm 1756: found = format_expand(ft, condition);
1757: if (strcmp(found, condition) == 0) {
1.156 nicm 1758: free(found);
1759: found = xstrdup("");
1.179 nicm 1760: format_log(ft, "condition '%s' found: %s",
1761: condition, found);
1762: } else {
1763: format_log(ft,
1764: "condition '%s' not found; assuming false",
1765: condition);
1.156 nicm 1766: }
1.179 nicm 1767: } else
1768: format_log(ft, "condition '%s' found", condition);
1.169 nicm 1769:
1770: if (format_choose(ft, cp + 1, &left, &right, 0) != 0) {
1.179 nicm 1771: format_log(ft, "condition '%s' syntax error: %s",
1772: condition, cp + 1);
1.162 nicm 1773: free(found);
1.89 nicm 1774: goto fail;
1.162 nicm 1775: }
1.179 nicm 1776: if (format_true(found)) {
1777: format_log(ft, "condition '%s' is true", condition);
1.114 nicm 1778: value = format_expand(ft, left);
1.179 nicm 1779: } else {
1780: format_log(ft, "condition '%s' is false", condition);
1.114 nicm 1781: value = format_expand(ft, right);
1.179 nicm 1782: }
1.169 nicm 1783: free(right);
1784: free(left);
1785:
1.179 nicm 1786: free(condition);
1.98 nicm 1787: free(found);
1.1 nicm 1788: } else {
1.114 nicm 1789: /* Neither: look up directly. */
1.98 nicm 1790: value = format_find(ft, copy, modifiers);
1.179 nicm 1791: if (value == NULL) {
1792: format_log(ft, "format '%s' not found", copy);
1.98 nicm 1793: value = xstrdup("");
1.179 nicm 1794: } else
1795: format_log(ft, "format '%s' found: %s", copy, value);
1.170 nicm 1796: }
1797:
1.171 nicm 1798: done:
1.170 nicm 1799: /* Expand again if required. */
1800: if (modifiers & FORMAT_EXPAND) {
1801: new = format_expand(ft, value);
1.175 nicm 1802: free(value);
1803: value = new;
1804: }
1805: else if (modifiers & FORMAT_EXPANDTIME) {
1.177 nicm 1806: new = format_expand_time(ft, value);
1.170 nicm 1807: free(value);
1808: value = new;
1.98 nicm 1809: }
1810:
1811: /* Perform substitution if any. */
1.169 nicm 1812: if (sub != NULL) {
1813: new = format_substitute(value, sub->argv[0], sub->argv[1]);
1.179 nicm 1814: format_log(ft, "substituted '%s' to '%s: %s", sub->argv[0],
1815: sub->argv[1], new);
1.98 nicm 1816: free(value);
1.169 nicm 1817: value = new;
1.1 nicm 1818: }
1819:
1.30 nicm 1820: /* Truncate the value if needed. */
1.105 nicm 1821: if (limit > 0) {
1.185 nicm 1822: new = format_trim_left(value, limit);
1.196 nicm 1823: if (marker != NULL && strcmp(new, value) != 0) {
1824: free(value);
1825: xasprintf(&value, "%s%s", new, marker);
1826: } else {
1827: free(value);
1828: value = new;
1829: }
1830: format_log(ft, "applied length limit %d: %s", limit, value);
1.105 nicm 1831: } else if (limit < 0) {
1.185 nicm 1832: new = format_trim_right(value, -limit);
1.196 nicm 1833: if (marker != NULL && strcmp(new, value) != 0) {
1834: free(value);
1835: xasprintf(&value, "%s%s", marker, new);
1836: } else {
1837: free(value);
1838: value = new;
1839: }
1840: format_log(ft, "applied length limit %d: %s", limit, value);
1.44 nicm 1841: }
1.30 nicm 1842:
1.1 nicm 1843: /* Expand the buffer and copy in the value. */
1.98 nicm 1844: valuelen = strlen(value);
1.1 nicm 1845: while (*len - *off < valuelen + 1) {
1.50 nicm 1846: *buf = xreallocarray(*buf, 2, *len);
1.1 nicm 1847: *len *= 2;
1848: }
1849: memcpy(*buf + *off, value, valuelen);
1850: *off += valuelen;
1851:
1.179 nicm 1852: format_log(ft, "replaced '%s' with '%s'", copy0, value);
1.98 nicm 1853: free(value);
1.179 nicm 1854:
1.169 nicm 1855: format_free_modifiers(list, count);
1.30 nicm 1856: free(copy0);
1.1 nicm 1857: return (0);
1858:
1859: fail:
1.179 nicm 1860: format_log(ft, "failed %s", copy0);
1.169 nicm 1861: format_free_modifiers(list, count);
1.30 nicm 1862: free(copy0);
1.1 nicm 1863: return (-1);
1864: }
1865:
1866: /* Expand keys in a template. */
1.179 nicm 1867: static char *
1868: format_expand1(struct format_tree *ft, const char *fmt, int time)
1.1 nicm 1869: {
1.152 nicm 1870: char *buf, *out, *name;
1.179 nicm 1871: const char *ptr, *s;
1.86 nicm 1872: size_t off, len, n, outlen;
1.31 nicm 1873: int ch, brackets;
1.179 nicm 1874: struct tm *tm;
1875: char expanded[8192];
1.58 nicm 1876:
1.179 nicm 1877: if (fmt == NULL || *fmt == '\0')
1.58 nicm 1878: return (xstrdup(""));
1.1 nicm 1879:
1.178 nicm 1880: if (ft->loop == FORMAT_LOOP_LIMIT)
1881: return (xstrdup(""));
1882: ft->loop++;
1883:
1.179 nicm 1884: format_log(ft, "expanding format: %s", fmt);
1885:
1886: if (time) {
1887: tm = localtime(&ft->time);
1888: if (strftime(expanded, sizeof expanded, fmt, tm) == 0) {
1889: format_log(ft, "format is too long");
1890: return (xstrdup(""));
1891: }
1892: if (format_logging(ft) && strcmp(expanded, fmt) != 0)
1893: format_log(ft, "after time expanded: %s", expanded);
1894: fmt = expanded;
1895: }
1896:
1.1 nicm 1897: len = 64;
1898: buf = xmalloc(len);
1899: off = 0;
1900:
1901: while (*fmt != '\0') {
1902: if (*fmt != '#') {
1903: while (len - off < 2) {
1.50 nicm 1904: buf = xreallocarray(buf, 2, len);
1.1 nicm 1905: len *= 2;
1906: }
1907: buf[off++] = *fmt++;
1908: continue;
1909: }
1910: fmt++;
1911:
1.179 nicm 1912: ch = (u_char)*fmt++;
1.1 nicm 1913: switch (ch) {
1.68 nicm 1914: case '(':
1915: brackets = 1;
1916: for (ptr = fmt; *ptr != '\0'; ptr++) {
1917: if (*ptr == '(')
1918: brackets++;
1919: if (*ptr == ')' && --brackets == 0)
1920: break;
1921: }
1922: if (*ptr != ')' || brackets != 0)
1923: break;
1924: n = ptr - fmt;
1925:
1.179 nicm 1926: name = xstrndup(fmt, n);
1927: format_log(ft, "found #(): %s", name);
1928:
1929: if (ft->flags & FORMAT_NOJOBS) {
1.114 nicm 1930: out = xstrdup("");
1.179 nicm 1931: format_log(ft, "#() is disabled");
1932: } else {
1.152 nicm 1933: out = format_job_get(ft, name);
1.179 nicm 1934: format_log(ft, "#() result: %s", out);
1.152 nicm 1935: }
1.179 nicm 1936: free(name);
1937:
1.86 nicm 1938: outlen = strlen(out);
1939: while (len - off < outlen + 1) {
1.68 nicm 1940: buf = xreallocarray(buf, 2, len);
1941: len *= 2;
1942: }
1.86 nicm 1943: memcpy(buf + off, out, outlen);
1944: off += outlen;
1945:
1946: free(out);
1.68 nicm 1947:
1948: fmt += n + 1;
1949: continue;
1.1 nicm 1950: case '{':
1.169 nicm 1951: ptr = format_skip((char *)fmt - 2, "}");
1.155 nicm 1952: if (ptr == NULL)
1.1 nicm 1953: break;
1954: n = ptr - fmt;
1955:
1.179 nicm 1956: format_log(ft, "found #{}: %.*s", (int)n, fmt);
1.1 nicm 1957: if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
1958: break;
1959: fmt += n + 1;
1.40 nicm 1960: continue;
1.155 nicm 1961: case '}':
1.40 nicm 1962: case '#':
1.155 nicm 1963: case ',':
1.179 nicm 1964: format_log(ft, "found #%c", ch);
1.40 nicm 1965: while (len - off < 2) {
1.50 nicm 1966: buf = xreallocarray(buf, 2, len);
1.40 nicm 1967: len *= 2;
1968: }
1.155 nicm 1969: buf[off++] = ch;
1.1 nicm 1970: continue;
1971: default:
1.25 nicm 1972: s = NULL;
1973: if (ch >= 'A' && ch <= 'Z')
1974: s = format_upper[ch - 'A'];
1975: else if (ch >= 'a' && ch <= 'z')
1976: s = format_lower[ch - 'a'];
1977: if (s == NULL) {
1978: while (len - off < 3) {
1.50 nicm 1979: buf = xreallocarray(buf, 2, len);
1.25 nicm 1980: len *= 2;
1.1 nicm 1981: }
1.25 nicm 1982: buf[off++] = '#';
1983: buf[off++] = ch;
1984: continue;
1.1 nicm 1985: }
1.25 nicm 1986: n = strlen(s);
1.179 nicm 1987: format_log(ft, "found #%c: %s", ch, s);
1.25 nicm 1988: if (format_replace(ft, s, n, &buf, &len, &off) != 0)
1989: break;
1.1 nicm 1990: continue;
1991: }
1992:
1993: break;
1994: }
1995: buf[off] = '\0';
1996:
1.179 nicm 1997: format_log(ft, "result is: %s", buf);
1998: ft->loop--;
1.178 nicm 1999:
1.1 nicm 2000: return (buf);
1.179 nicm 2001: }
2002:
2003: /* Expand keys in a template, passing through strftime first. */
2004: char *
2005: format_expand_time(struct format_tree *ft, const char *fmt)
2006: {
2007: return (format_expand1(ft, fmt, 1));
2008: }
2009:
2010: /* Expand keys in a template. */
2011: char *
2012: format_expand(struct format_tree *ft, const char *fmt)
2013: {
2014: return (format_expand1(ft, fmt, 0));
1.123 nicm 2015: }
2016:
2017: /* Expand a single string. */
2018: char *
2019: format_single(struct cmdq_item *item, const char *fmt, struct client *c,
2020: struct session *s, struct winlink *wl, struct window_pane *wp)
2021: {
2022: struct format_tree *ft;
2023: char *expanded;
2024:
1.131 nicm 2025: if (item != NULL)
2026: ft = format_create(item->client, item, FORMAT_NONE, 0);
2027: else
2028: ft = format_create(NULL, item, FORMAT_NONE, 0);
1.123 nicm 2029: format_defaults(ft, c, s, wl, wp);
2030:
2031: expanded = format_expand(ft, fmt);
2032: format_free(ft);
2033: return (expanded);
1.1 nicm 2034: }
2035:
1.57 nicm 2036: /* Set defaults for any of arguments that are not NULL. */
2037: void
2038: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
2039: struct winlink *wl, struct window_pane *wp)
2040: {
1.187 nicm 2041: if (c != NULL)
2042: log_debug("%s: c=%s", __func__, c->name);
2043: else
2044: log_debug("%s: s=none", __func__);
2045: if (s != NULL)
2046: log_debug("%s: s=$%u", __func__, s->id);
2047: else
2048: log_debug("%s: s=none", __func__);
2049: if (wl != NULL)
2050: log_debug("%s: wl=%u w=@%u", __func__, wl->idx, wl->window->id);
2051: else
2052: log_debug("%s: wl=none", __func__);
2053: if (wp != NULL)
2054: log_debug("%s: wp=%%%u", __func__, wp->id);
2055: else
2056: log_debug("%s: wp=none", __func__);
2057:
1.154 nicm 2058: if (c != NULL && s != NULL && c->session != s)
2059: log_debug("%s: session does not match", __func__);
2060:
1.146 nicm 2061: format_add(ft, "session_format", "%d", s != NULL);
2062: format_add(ft, "window_format", "%d", wl != NULL);
2063: format_add(ft, "pane_format", "%d", wp != NULL);
2064:
1.57 nicm 2065: if (s == NULL && c != NULL)
2066: s = c->session;
2067: if (wl == NULL && s != NULL)
2068: wl = s->curw;
2069: if (wp == NULL && wl != NULL)
2070: wp = wl->window->active;
2071:
2072: if (c != NULL)
2073: format_defaults_client(ft, c);
2074: if (s != NULL)
2075: format_defaults_session(ft, s);
1.129 nicm 2076: if (wl != NULL)
2077: format_defaults_winlink(ft, wl);
1.57 nicm 2078: if (wp != NULL)
2079: format_defaults_pane(ft, wp);
2080: }
2081:
1.1 nicm 2082: /* Set default format keys for a session. */
1.108 nicm 2083: static void
1.57 nicm 2084: format_defaults_session(struct format_tree *ft, struct session *s)
1.1 nicm 2085: {
2086: struct session_group *sg;
2087:
1.54 nicm 2088: ft->s = s;
2089:
1.1 nicm 2090: format_add(ft, "session_name", "%s", s->name);
2091: format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
1.23 nicm 2092: format_add(ft, "session_id", "$%u", s->id);
1.1 nicm 2093:
1.122 nicm 2094: sg = session_group_contains(s);
1.1 nicm 2095: format_add(ft, "session_grouped", "%d", sg != NULL);
1.148 nicm 2096: if (sg != NULL) {
1.122 nicm 2097: format_add(ft, "session_group", "%s", sg->name);
1.148 nicm 2098: format_add(ft, "session_group_size", "%u",
2099: session_group_count (sg));
1.150 nicm 2100: format_add_cb(ft, "session_group_list",
2101: format_cb_session_group_list);
1.148 nicm 2102: }
1.1 nicm 2103:
1.87 nicm 2104: format_add_tv(ft, "session_created", &s->creation_time);
2105: format_add_tv(ft, "session_last_attached", &s->last_attached_time);
2106: format_add_tv(ft, "session_activity", &s->activity_time);
1.1 nicm 2107:
1.41 nicm 2108: format_add(ft, "session_attached", "%u", s->attached);
1.59 nicm 2109: format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66 nicm 2110:
1.80 nicm 2111: format_add_cb(ft, "session_alerts", format_cb_session_alerts);
1.133 nicm 2112: format_add_cb(ft, "session_stack", format_cb_session_stack);
1.3 nicm 2113: }
2114:
2115: /* Set default format keys for a client. */
1.108 nicm 2116: static void
1.57 nicm 2117: format_defaults_client(struct format_tree *ft, struct client *c)
1.3 nicm 2118: {
1.60 nicm 2119: struct session *s;
1.103 nicm 2120: const char *name;
1.115 nicm 2121: struct tty *tty = &c->tty;
2122: const char *types[] = TTY_TYPES;
1.3 nicm 2123:
1.54 nicm 2124: if (ft->s == NULL)
2125: ft->s = c->session;
1.164 nicm 2126: ft->c = c;
1.54 nicm 2127:
1.124 nicm 2128: format_add(ft, "client_name", "%s", c->name);
1.72 nicm 2129: format_add(ft, "client_pid", "%ld", (long) c->pid);
1.115 nicm 2130: format_add(ft, "client_height", "%u", tty->sy);
2131: format_add(ft, "client_width", "%u", tty->sx);
1.124 nicm 2132: format_add(ft, "client_tty", "%s", c->ttyname);
1.75 nicm 2133: format_add(ft, "client_control_mode", "%d",
2134: !!(c->flags & CLIENT_CONTROL));
1.3 nicm 2135:
1.115 nicm 2136: if (tty->term_name != NULL)
2137: format_add(ft, "client_termname", "%s", tty->term_name);
2138: if (tty->term_name != NULL)
2139: format_add(ft, "client_termtype", "%s", types[tty->term_type]);
2140:
1.87 nicm 2141: format_add_tv(ft, "client_created", &c->creation_time);
2142: format_add_tv(ft, "client_activity", &c->activity_time);
1.126 nicm 2143:
2144: format_add(ft, "client_written", "%zu", c->written);
2145: format_add(ft, "client_discarded", "%zu", c->discarded);
1.14 nicm 2146:
1.103 nicm 2147: name = server_client_get_key_table(c);
2148: if (strcmp(c->keytable->name, name) == 0)
1.61 nicm 2149: format_add(ft, "client_prefix", "%d", 0);
2150: else
2151: format_add(ft, "client_prefix", "%d", 1);
2152: format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3 nicm 2153:
1.115 nicm 2154: if (tty->flags & TTY_UTF8)
1.3 nicm 2155: format_add(ft, "client_utf8", "%d", 1);
2156: else
2157: format_add(ft, "client_utf8", "%d", 0);
2158:
2159: if (c->flags & CLIENT_READONLY)
2160: format_add(ft, "client_readonly", "%d", 1);
2161: else
2162: format_add(ft, "client_readonly", "%d", 0);
1.15 nicm 2163:
2164: s = c->session;
2165: if (s != NULL)
2166: format_add(ft, "client_session", "%s", s->name);
2167: s = c->last_session;
2168: if (s != NULL && session_alive(s))
2169: format_add(ft, "client_last_session", "%s", s->name);
1.1 nicm 2170: }
2171:
1.32 nicm 2172: /* Set default format keys for a window. */
2173: void
1.57 nicm 2174: format_defaults_window(struct format_tree *ft, struct window *w)
1.32 nicm 2175: {
1.54 nicm 2176: ft->w = w;
2177:
1.87 nicm 2178: format_add_tv(ft, "window_activity", &w->activity_time);
1.32 nicm 2179: format_add(ft, "window_id", "@%u", w->id);
2180: format_add(ft, "window_name", "%s", w->name);
2181: format_add(ft, "window_width", "%u", w->sx);
2182: format_add(ft, "window_height", "%u", w->sy);
1.80 nicm 2183: format_add_cb(ft, "window_layout", format_cb_window_layout);
1.96 nicm 2184: format_add_cb(ft, "window_visible_layout",
2185: format_cb_window_visible_layout);
1.32 nicm 2186: format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59 nicm 2187: format_add(ft, "window_zoomed_flag", "%d",
1.53 nicm 2188: !!(w->flags & WINDOW_ZOOMED));
1.32 nicm 2189: }
2190:
1.1 nicm 2191: /* Set default format keys for a winlink. */
1.108 nicm 2192: static void
1.129 nicm 2193: format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
1.1 nicm 2194: {
1.164 nicm 2195: struct client *c = ft->c;
1.129 nicm 2196: struct session *s = wl->session;
1.1 nicm 2197: struct window *w = wl->window;
1.164 nicm 2198: int flag;
2199: u_int ox, oy, sx, sy;
1.1 nicm 2200:
1.54 nicm 2201: if (ft->w == NULL)
2202: ft->w = wl->window;
1.133 nicm 2203: ft->wl = wl;
1.54 nicm 2204:
1.57 nicm 2205: format_defaults_window(ft, w);
1.32 nicm 2206:
1.164 nicm 2207: if (c != NULL) {
2208: flag = tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
2209: format_add(ft, "window_bigger", "%d", flag);
2210: if (flag) {
2211: format_add(ft, "window_offset_x", "%u", ox);
2212: format_add(ft, "window_offset_y", "%u", oy);
2213: }
2214: }
2215:
1.1 nicm 2216: format_add(ft, "window_index", "%d", wl->idx);
1.133 nicm 2217: format_add_cb(ft, "window_stack_index", format_cb_window_stack_index);
1.129 nicm 2218: format_add(ft, "window_flags", "%s", window_printable_flags(wl));
1.1 nicm 2219: format_add(ft, "window_active", "%d", wl == s->curw);
1.176 nicm 2220:
2221: format_add(ft, "window_start_flag", "%d",
2222: !!(wl == RB_MIN(winlinks, &s->windows)));
2223: format_add(ft, "window_end_flag", "%d",
2224: !!(wl == RB_MAX(winlinks, &s->windows)));
1.29 nicm 2225:
1.59 nicm 2226: format_add(ft, "window_bell_flag", "%d",
1.29 nicm 2227: !!(wl->flags & WINLINK_BELL));
1.59 nicm 2228: format_add(ft, "window_activity_flag", "%d",
1.29 nicm 2229: !!(wl->flags & WINLINK_ACTIVITY));
1.59 nicm 2230: format_add(ft, "window_silence_flag", "%d",
1.29 nicm 2231: !!(wl->flags & WINLINK_SILENCE));
1.59 nicm 2232: format_add(ft, "window_last_flag", "%d",
1.49 nicm 2233: !!(wl == TAILQ_FIRST(&s->lastw)));
1.64 nicm 2234: format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.1 nicm 2235: }
2236:
2237: /* Set default format keys for a window pane. */
2238: void
1.57 nicm 2239: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1 nicm 2240: {
1.168 nicm 2241: struct window *w = wp->window;
2242: struct grid *gd = wp->base.grid;
2243: int status = wp->status;
2244: u_int idx;
2245: struct window_mode_entry *wme;
1.54 nicm 2246:
2247: if (ft->w == NULL)
1.159 nicm 2248: ft->w = w;
1.79 nicm 2249: ft->wp = wp;
1.1 nicm 2250:
1.16 nicm 2251: format_add(ft, "history_size", "%u", gd->hsize);
2252: format_add(ft, "history_limit", "%u", gd->hlimit);
1.80 nicm 2253: format_add_cb(ft, "history_bytes", format_cb_history_bytes);
1.1 nicm 2254:
1.4 nicm 2255: if (window_pane_index(wp, &idx) != 0)
2256: fatalx("index not found");
1.16 nicm 2257: format_add(ft, "pane_index", "%u", idx);
1.4 nicm 2258:
1.1 nicm 2259: format_add(ft, "pane_width", "%u", wp->sx);
2260: format_add(ft, "pane_height", "%u", wp->sy);
2261: format_add(ft, "pane_title", "%s", wp->base.title);
2262: format_add(ft, "pane_id", "%%%u", wp->id);
1.159 nicm 2263: format_add(ft, "pane_active", "%d", wp == w->active);
1.55 nicm 2264: format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
1.143 nicm 2265: format_add(ft, "pane_pipe", "%d", wp->pipe_fd != -1);
1.55 nicm 2266:
1.147 nicm 2267: if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(status))
1.55 nicm 2268: format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.190 nicm 2269: if (~wp->flags & PANE_EMPTY)
2270: format_add(ft, "pane_dead", "%d", wp->fd == -1);
2271: else
2272: format_add(ft, "pane_dead", "0");
1.191 nicm 2273:
2274: if (server_check_marked() && marked_pane.wp == wp)
2275: format_add(ft, "pane_marked", "1");
2276: else
2277: format_add(ft, "pane_marked", "0");
2278: format_add(ft, "pane_marked_set", "%d", server_check_marked());
1.47 nicm 2279:
1.164 nicm 2280: format_add(ft, "pane_left", "%u", wp->xoff);
2281: format_add(ft, "pane_top", "%u", wp->yoff);
2282: format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
2283: format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
2284: format_add(ft, "pane_at_left", "%d", wp->xoff == 0);
2285: format_add(ft, "pane_at_top", "%d", wp->yoff == 0);
2286: format_add(ft, "pane_at_right", "%d", wp->xoff + wp->sx == w->sx);
2287: format_add(ft, "pane_at_bottom", "%d", wp->yoff + wp->sy == w->sy);
1.16 nicm 2288:
1.168 nicm 2289: wme = TAILQ_FIRST(&wp->modes);
2290: if (wme != NULL) {
2291: format_add(ft, "pane_mode", "%s", wme->mode->name);
2292: if (wme->mode->formats != NULL)
2293: wme->mode->formats(wme, ft);
2294: }
2295: format_add_cb(ft, "pane_in_mode", format_cb_pane_in_mode);
1.134 nicm 2296:
1.26 nicm 2297: format_add(ft, "pane_synchronized", "%d",
1.159 nicm 2298: !!options_get_number(w->options, "synchronize-panes"));
1.135 nicm 2299: if (wp->searchstr != NULL)
2300: format_add(ft, "pane_search_string", "%s", wp->searchstr);
1.16 nicm 2301:
1.71 nicm 2302: format_add(ft, "pane_tty", "%s", wp->tty);
1.16 nicm 2303: format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.79 nicm 2304: format_add_cb(ft, "pane_start_command", format_cb_start_command);
2305: format_add_cb(ft, "pane_current_command", format_cb_current_command);
1.16 nicm 2306:
1.59 nicm 2307: format_add(ft, "cursor_x", "%u", wp->base.cx);
2308: format_add(ft, "cursor_y", "%u", wp->base.cy);
1.186 nicm 2309: format_add_cb(ft, "cursor_character", format_cb_cursor_character);
2310:
1.59 nicm 2311: format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
2312: format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.16 nicm 2313:
2314: format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59 nicm 2315: format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
2316: format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16 nicm 2317:
2318: format_add(ft, "cursor_flag", "%d",
2319: !!(wp->base.mode & MODE_CURSOR));
2320: format_add(ft, "insert_flag", "%d",
2321: !!(wp->base.mode & MODE_INSERT));
2322: format_add(ft, "keypad_cursor_flag", "%d",
2323: !!(wp->base.mode & MODE_KCURSOR));
2324: format_add(ft, "keypad_flag", "%d",
2325: !!(wp->base.mode & MODE_KKEYPAD));
2326: format_add(ft, "wrap_flag", "%d",
2327: !!(wp->base.mode & MODE_WRAP));
2328:
1.62 nicm 2329: format_add(ft, "mouse_any_flag", "%d",
1.119 nicm 2330: !!(wp->base.mode & ALL_MOUSE_MODES));
1.16 nicm 2331: format_add(ft, "mouse_standard_flag", "%d",
2332: !!(wp->base.mode & MODE_MOUSE_STANDARD));
2333: format_add(ft, "mouse_button_flag", "%d",
2334: !!(wp->base.mode & MODE_MOUSE_BUTTON));
1.119 nicm 2335: format_add(ft, "mouse_all_flag", "%d",
2336: !!(wp->base.mode & MODE_MOUSE_ALL));
1.19 nicm 2337:
1.80 nicm 2338: format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
1.8 nicm 2339: }
2340:
1.19 nicm 2341: /* Set default format keys for paste buffer. */
1.8 nicm 2342: void
1.94 nicm 2343: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
1.8 nicm 2344: {
1.146 nicm 2345: struct timeval tv;
2346: size_t size;
2347: char *s;
2348:
2349: timerclear(&tv);
2350: tv.tv_sec = paste_buffer_created(pb);
2351: paste_buffer_data(pb, &size);
1.8 nicm 2352:
1.146 nicm 2353: format_add(ft, "buffer_size", "%zu", size);
1.81 nicm 2354: format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
1.146 nicm 2355: format_add_tv(ft, "buffer_created", &tv);
1.8 nicm 2356:
1.94 nicm 2357: s = paste_make_sample(pb);
1.42 nicm 2358: format_add(ft, "buffer_sample", "%s", s);
2359: free(s);
1.1 nicm 2360: }