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