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