Annotation of src/usr.bin/tmux/format.c, Revision 1.136
1.136 ! nicm 1: /* $OpenBSD: format.c,v 1.135 2017/05/12 10:45:38 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.30 nicm 22: #include <ctype.h>
23: #include <errno.h>
1.87 nicm 24: #include <libgen.h>
1.1 nicm 25: #include <netdb.h>
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.129 nicm 55: static void format_defaults_winlink(struct format_tree *,
1.108 nicm 56: struct winlink *);
1.1 nicm 57:
1.68 nicm 58: /* Entry in format job tree. */
59: struct format_job {
1.131 nicm 60: struct client *client;
1.120 nicm 61: u_int tag;
1.68 nicm 62: const char *cmd;
1.113 nicm 63: const char *expanded;
1.68 nicm 64:
65: time_t last;
66: char *out;
1.127 nicm 67: int updated;
1.68 nicm 68:
69: struct job *job;
70: int status;
71:
72: RB_ENTRY(format_job) entry;
73: };
74:
75: /* Format job tree. */
1.108 nicm 76: static struct event format_job_event;
77: static int format_job_cmp(struct format_job *, struct format_job *);
1.109 nicm 78: static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
1.108 nicm 79: RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp);
1.68 nicm 80:
81: /* Format job tree comparison function. */
1.108 nicm 82: static int
1.68 nicm 83: format_job_cmp(struct format_job *fj1, struct format_job *fj2)
84: {
1.120 nicm 85: if (fj1->tag < fj2->tag)
86: return (-1);
87: if (fj1->tag > fj2->tag)
88: return (1);
1.68 nicm 89: return (strcmp(fj1->cmd, fj2->cmd));
90: }
91:
1.87 nicm 92: /* Format modifiers. */
93: #define FORMAT_TIMESTRING 0x1
94: #define FORMAT_BASENAME 0x2
95: #define FORMAT_DIRNAME 0x4
1.98 nicm 96: #define FORMAT_SUBSTITUTE 0x8
1.87 nicm 97:
1.54 nicm 98: /* Entry in format tree. */
99: struct format_entry {
1.79 nicm 100: char *key;
101: char *value;
1.87 nicm 102: time_t t;
1.79 nicm 103: format_cb cb;
104: RB_ENTRY(format_entry) entry;
1.54 nicm 105: };
106:
1.68 nicm 107: /* Format entry tree. */
1.54 nicm 108: struct format_tree {
1.79 nicm 109: struct window *w;
1.133 nicm 110: struct winlink *wl;
1.79 nicm 111: struct session *s;
112: struct window_pane *wp;
1.54 nicm 113:
1.131 nicm 114: struct client *client;
1.120 nicm 115: u_int tag;
1.84 nicm 116: int flags;
1.68 nicm 117:
118: RB_HEAD(format_entry_tree, format_entry) tree;
1.54 nicm 119: };
1.108 nicm 120: static int format_entry_cmp(struct format_entry *, struct format_entry *);
121: RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
1.54 nicm 122:
1.68 nicm 123: /* Format entry tree comparison function. */
1.108 nicm 124: static int
1.68 nicm 125: format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
1.1 nicm 126: {
127: return (strcmp(fe1->key, fe2->key));
128: }
129:
1.25 nicm 130: /* Single-character uppercase aliases. */
1.108 nicm 131: static const char *format_upper[] = {
1.1 nicm 132: NULL, /* A */
133: NULL, /* B */
134: NULL, /* C */
135: "pane_id", /* D */
136: NULL, /* E */
137: "window_flags", /* F */
138: NULL, /* G */
139: "host", /* H */
140: "window_index", /* I */
141: NULL, /* J */
142: NULL, /* K */
143: NULL, /* L */
144: NULL, /* M */
145: NULL, /* N */
146: NULL, /* O */
147: "pane_index", /* P */
148: NULL, /* Q */
149: NULL, /* R */
150: "session_name", /* S */
151: "pane_title", /* T */
152: NULL, /* U */
153: NULL, /* V */
154: "window_name", /* W */
155: NULL, /* X */
156: NULL, /* Y */
157: NULL /* Z */
158: };
159:
1.25 nicm 160: /* Single-character lowercase aliases. */
1.108 nicm 161: static const char *format_lower[] = {
1.25 nicm 162: NULL, /* a */
163: NULL, /* b */
164: NULL, /* c */
165: NULL, /* d */
166: NULL, /* e */
167: NULL, /* f */
168: NULL, /* g */
169: "host_short", /* h */
170: NULL, /* i */
171: NULL, /* j */
172: NULL, /* k */
173: NULL, /* l */
174: NULL, /* m */
175: NULL, /* n */
176: NULL, /* o */
177: NULL, /* p */
178: NULL, /* q */
179: NULL, /* r */
180: NULL, /* s */
181: NULL, /* t */
182: NULL, /* u */
183: NULL, /* v */
184: NULL, /* w */
185: NULL, /* x */
186: NULL, /* y */
187: NULL /* z */
188: };
189:
1.127 nicm 190: /* Format job update callback. */
1.108 nicm 191: static void
1.127 nicm 192: format_job_update(struct job *job)
193: {
194: struct format_job *fj = job->data;
195: char *line;
196: time_t t;
197: struct client *c;
198:
199: if ((line = evbuffer_readline(job->event->input)) == NULL)
200: return;
201: fj->updated = 1;
202:
203: free(fj->out);
204: fj->out = line;
205:
1.136 ! nicm 206: log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out);
1.127 nicm 207:
208: t = time (NULL);
209: if (fj->status && fj->last != t) {
1.136 ! nicm 210: if (fj->client != NULL)
! 211: server_status_client(fj->client);
1.127 nicm 212: fj->last = t;
213: }
214: }
215:
216: /* Format job complete callback. */
217: static void
218: format_job_complete(struct job *job)
1.68 nicm 219: {
220: struct format_job *fj = job->data;
221: char *line, *buf;
222: size_t len;
223:
224: fj->job = NULL;
225:
226: buf = NULL;
227: if ((line = evbuffer_readline(job->event->input)) == NULL) {
228: len = EVBUFFER_LENGTH(job->event->input);
229: buf = xmalloc(len + 1);
230: if (len != 0)
231: memcpy(buf, EVBUFFER_DATA(job->event->input), len);
232: buf[len] = '\0';
233: } else
234: buf = line;
1.127 nicm 235:
1.136 ! nicm 236: log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf);
! 237:
1.127 nicm 238: if (*buf != '\0' || !fj->updated) {
239: free(fj->out);
240: fj->out = buf;
241: } else
242: free(buf);
1.68 nicm 243:
244: if (fj->status) {
1.131 nicm 245: if (fj->client != NULL)
246: server_status_client(fj->client);
1.68 nicm 247: fj->status = 0;
248: }
249: }
250:
251: /* Find a job. */
1.108 nicm 252: static char *
1.68 nicm 253: format_job_get(struct format_tree *ft, const char *cmd)
254: {
1.131 nicm 255: struct format_job_tree *jobs;
1.113 nicm 256: struct format_job fj0, *fj;
257: time_t t;
258: char *expanded;
259: int force;
1.68 nicm 260:
1.131 nicm 261: if (ft->client == NULL)
262: jobs = &format_jobs;
263: else if (ft->client->jobs != NULL)
264: jobs = ft->client->jobs;
265: else {
266: jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
267: RB_INIT(jobs);
268: }
269:
1.120 nicm 270: fj0.tag = ft->tag;
1.68 nicm 271: fj0.cmd = cmd;
1.131 nicm 272: if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
1.68 nicm 273: fj = xcalloc(1, sizeof *fj);
1.131 nicm 274: fj->client = ft->client;
1.120 nicm 275: fj->tag = ft->tag;
1.68 nicm 276: fj->cmd = xstrdup(cmd);
1.113 nicm 277: fj->expanded = NULL;
1.68 nicm 278:
279: xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
280:
1.131 nicm 281: RB_INSERT(format_job_tree, jobs, fj);
1.68 nicm 282: }
283:
1.113 nicm 284: expanded = format_expand(ft, cmd);
285: if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
286: free((void *)fj->expanded);
287: fj->expanded = xstrdup(expanded);
288: force = 1;
289: } else
290: force = (ft->flags & FORMAT_FORCE);
291:
1.84 nicm 292: t = time(NULL);
1.113 nicm 293: if (fj->job == NULL && (force || fj->last != t)) {
1.127 nicm 294: fj->job = job_run(expanded, NULL, NULL, format_job_update,
295: format_job_complete, NULL, fj);
1.68 nicm 296: if (fj->job == NULL) {
297: free(fj->out);
298: xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
299: }
1.84 nicm 300: fj->last = t;
1.68 nicm 301: }
1.84 nicm 302:
303: if (ft->flags & FORMAT_STATUS)
304: fj->status = 1;
1.68 nicm 305:
1.113 nicm 306: free(expanded);
1.86 nicm 307: return (format_expand(ft, fj->out));
1.68 nicm 308: }
309:
310: /* Remove old jobs. */
1.108 nicm 311: static void
1.131 nicm 312: format_job_tidy(struct format_job_tree *jobs, int force)
1.68 nicm 313: {
314: struct format_job *fj, *fj1;
315: time_t now;
316:
317: now = time(NULL);
1.131 nicm 318: RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
319: if (!force && (fj->last > now || now - fj->last < 3600))
1.68 nicm 320: continue;
1.131 nicm 321: RB_REMOVE(format_job_tree, jobs, fj);
1.68 nicm 322:
1.77 nicm 323: log_debug("%s: %s", __func__, fj->cmd);
324:
1.68 nicm 325: if (fj->job != NULL)
326: job_free(fj->job);
327:
1.113 nicm 328: free((void *)fj->expanded);
1.78 nicm 329: free((void *)fj->cmd);
1.68 nicm 330: free(fj->out);
331:
332: free(fj);
333: }
1.131 nicm 334: }
335:
336: /* Remove old jobs for client. */
337: void
338: format_lost_client(struct client *c)
339: {
340: if (c->jobs != NULL)
341: format_job_tidy(c->jobs, 1);
342: free(c->jobs);
343: }
344:
345: /* Remove old jobs periodically. */
346: static void
347: format_job_timer(__unused int fd, __unused short events, __unused void *arg)
348: {
349: struct client *c;
350: struct timeval tv = { .tv_sec = 60 };
351:
352: format_job_tidy(&format_jobs, 0);
353: TAILQ_FOREACH(c, &clients, entry) {
354: if (c->jobs != NULL)
355: format_job_tidy(c->jobs, 0);
356: }
1.77 nicm 357:
358: evtimer_del(&format_job_event);
359: evtimer_add(&format_job_event, &tv);
1.68 nicm 360: }
361:
1.79 nicm 362: /* Callback for host. */
1.108 nicm 363: static void
1.99 nicm 364: format_cb_host(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 365: {
366: char host[HOST_NAME_MAX + 1];
367:
368: if (gethostname(host, sizeof host) != 0)
369: fe->value = xstrdup("");
370: else
371: fe->value = xstrdup(host);
372: }
373:
374: /* Callback for host_short. */
1.108 nicm 375: static void
1.99 nicm 376: format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 377: {
378: char host[HOST_NAME_MAX + 1], *cp;
379:
380: if (gethostname(host, sizeof host) != 0)
381: fe->value = xstrdup("");
382: else {
383: if ((cp = strchr(host, '.')) != NULL)
384: *cp = '\0';
385: fe->value = xstrdup(host);
386: }
387: }
388:
389: /* Callback for pid. */
1.108 nicm 390: static void
1.99 nicm 391: format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 392: {
393: xasprintf(&fe->value, "%ld", (long)getpid());
394: }
395:
1.80 nicm 396: /* Callback for session_alerts. */
1.108 nicm 397: static void
1.80 nicm 398: format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe)
399: {
400: struct session *s = ft->s;
401: struct winlink *wl;
1.133 nicm 402: char alerts[1024], tmp[16];
1.80 nicm 403:
404: if (s == NULL)
405: return;
406:
407: *alerts = '\0';
408: RB_FOREACH(wl, winlinks, &s->windows) {
409: if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
410: continue;
411: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
412:
413: if (*alerts != '\0')
414: strlcat(alerts, ",", sizeof alerts);
415: strlcat(alerts, tmp, sizeof alerts);
416: if (wl->flags & WINLINK_ACTIVITY)
417: strlcat(alerts, "#", sizeof alerts);
418: if (wl->flags & WINLINK_BELL)
419: strlcat(alerts, "!", sizeof alerts);
420: if (wl->flags & WINLINK_SILENCE)
421: strlcat(alerts, "~", sizeof alerts);
422: }
423: fe->value = xstrdup(alerts);
424: }
425:
1.133 nicm 426: /* Callback for session_stack. */
427: static void
428: format_cb_session_stack(struct format_tree *ft, struct format_entry *fe)
429: {
430: struct session *s = ft->s;
431: struct winlink *wl;
432: char result[1024], tmp[16];
433:
434: if (s == NULL)
435: return;
436:
437: xsnprintf(result, sizeof result, "%u", s->curw->idx);
438: TAILQ_FOREACH(wl, &s->lastw, sentry) {
439: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
440:
441: if (*result != '\0')
442: strlcat(result, ",", sizeof result);
443: strlcat(result, tmp, sizeof result);
444: }
445: fe->value = xstrdup(result);
446: }
447:
448: /* Callback for window_stack_index. */
449: static void
450: format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe)
451: {
452: struct session *s = ft->wl->session;
453: struct winlink *wl;
454: u_int idx;
455:
456: idx = 0;
457: TAILQ_FOREACH(wl, &s->lastw, sentry) {
458: idx++;
459: if (wl == ft->wl)
460: break;
461: }
462: if (wl != NULL)
463: xasprintf(&fe->value, "%u", idx);
464: else
465: fe->value = xstrdup("0");
466: }
467:
1.80 nicm 468: /* Callback for window_layout. */
1.108 nicm 469: static void
1.80 nicm 470: format_cb_window_layout(struct format_tree *ft, struct format_entry *fe)
471: {
472: struct window *w = ft->w;
473:
474: if (w == NULL)
475: return;
476:
477: if (w->saved_layout_root != NULL)
478: fe->value = layout_dump(w->saved_layout_root);
479: else
480: fe->value = layout_dump(w->layout_root);
481: }
482:
1.96 nicm 483: /* Callback for window_visible_layout. */
1.108 nicm 484: static void
1.96 nicm 485: format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe)
486: {
487: struct window *w = ft->w;
488:
489: if (w == NULL)
490: return;
491:
492: fe->value = layout_dump(w->layout_root);
493: }
494:
1.79 nicm 495: /* Callback for pane_start_command. */
1.108 nicm 496: static void
1.79 nicm 497: format_cb_start_command(struct format_tree *ft, struct format_entry *fe)
498: {
499: struct window_pane *wp = ft->wp;
500:
501: if (wp == NULL)
502: return;
503:
504: fe->value = cmd_stringify_argv(wp->argc, wp->argv);
505: }
506:
507: /* Callback for pane_current_command. */
1.108 nicm 508: static void
1.79 nicm 509: format_cb_current_command(struct format_tree *ft, struct format_entry *fe)
510: {
511: struct window_pane *wp = ft->wp;
512: char *cmd;
513:
514: if (wp == NULL)
515: return;
516:
517: cmd = get_proc_name(wp->fd, wp->tty);
518: if (cmd == NULL || *cmd == '\0') {
519: free(cmd);
520: cmd = cmd_stringify_argv(wp->argc, wp->argv);
521: if (cmd == NULL || *cmd == '\0') {
522: free(cmd);
523: cmd = xstrdup(wp->shell);
524: }
525: }
526: fe->value = parse_window_name(cmd);
527: free(cmd);
528: }
529:
1.80 nicm 530: /* Callback for history_bytes. */
1.108 nicm 531: static void
1.80 nicm 532: format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe)
533: {
534: struct window_pane *wp = ft->wp;
535: struct grid *gd;
536: struct grid_line *gl;
537: unsigned long long size;
538: u_int i;
539:
540: if (wp == NULL)
541: return;
542: gd = wp->base.grid;
543:
544: size = 0;
545: for (i = 0; i < gd->hsize; i++) {
546: gl = &gd->linedata[i];
547: size += gl->cellsize * sizeof *gl->celldata;
1.95 nicm 548: size += gl->extdsize * sizeof *gl->extddata;
1.80 nicm 549: }
550: size += gd->hsize * sizeof *gd->linedata;
551:
552: xasprintf(&fe->value, "%llu", size);
553: }
554:
555: /* Callback for pane_tabs. */
1.108 nicm 556: static void
1.80 nicm 557: format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe)
558: {
559: struct window_pane *wp = ft->wp;
560: struct evbuffer *buffer;
561: u_int i;
562: int size;
563:
564: if (wp == NULL)
565: return;
566:
567: buffer = evbuffer_new();
568: for (i = 0; i < wp->base.grid->sx; i++) {
569: if (!bit_test(wp->base.tabs, i))
570: continue;
571:
572: if (EVBUFFER_LENGTH(buffer) > 0)
573: evbuffer_add(buffer, ",", 1);
574: evbuffer_add_printf(buffer, "%u", i);
575: }
576: size = EVBUFFER_LENGTH(buffer);
577: xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
578: evbuffer_free(buffer);
579: }
580:
1.112 nicm 581: /* Merge a format tree. */
582: static void
583: format_merge(struct format_tree *ft, struct format_tree *from)
584: {
585: struct format_entry *fe;
586:
587: RB_FOREACH(fe, format_entry_tree, &from->tree) {
588: if (fe->value != NULL)
589: format_add(ft, fe->key, "%s", fe->value);
590: }
591: }
592:
1.1 nicm 593: /* Create a new tree. */
594: struct format_tree *
1.131 nicm 595: format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
1.68 nicm 596: {
1.1 nicm 597: struct format_tree *ft;
1.77 nicm 598:
599: if (!event_initialized(&format_job_event)) {
600: evtimer_set(&format_job_event, format_job_timer, NULL);
601: format_job_timer(-1, 0, NULL);
602: }
1.1 nicm 603:
1.54 nicm 604: ft = xcalloc(1, sizeof *ft);
605: RB_INIT(&ft->tree);
1.120 nicm 606:
1.131 nicm 607: if (c != NULL) {
608: ft->client = c;
609: ft->client->references++;
610: }
611:
1.120 nicm 612: ft->tag = tag;
1.84 nicm 613: ft->flags = flags;
1.1 nicm 614:
1.79 nicm 615: format_add_cb(ft, "host", format_cb_host);
616: format_add_cb(ft, "host_short", format_cb_host_short);
617: format_add_cb(ft, "pid", format_cb_pid);
1.100 nicm 618: format_add(ft, "socket_path", "%s", socket_path);
619: format_add_tv(ft, "start_time", &start_time);
1.102 nicm 620:
1.130 nicm 621: if (item != NULL) {
622: if (item->cmd != NULL)
623: format_add(ft, "command", "%s", item->cmd->entry->name);
624: if (item->shared != NULL && item->shared->formats != NULL)
625: format_merge(ft, item->shared->formats);
626: }
1.1 nicm 627:
628: return (ft);
629: }
630:
631: /* Free a tree. */
632: void
633: format_free(struct format_tree *ft)
634: {
1.54 nicm 635: struct format_entry *fe, *fe1;
1.1 nicm 636:
1.68 nicm 637: RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
638: RB_REMOVE(format_entry_tree, &ft->tree, fe);
1.9 nicm 639: free(fe->value);
640: free(fe->key);
641: free(fe);
1.1 nicm 642: }
643:
1.131 nicm 644: if (ft->client != NULL)
645: server_client_unref(ft->client);
1.25 nicm 646: free(ft);
1.1 nicm 647: }
648:
649: /* Add a key-value pair. */
650: void
651: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
652: {
653: struct format_entry *fe;
1.28 nicm 654: struct format_entry *fe_now;
1.1 nicm 655: va_list ap;
656:
657: fe = xmalloc(sizeof *fe);
658: fe->key = xstrdup(key);
659:
1.79 nicm 660: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
661: if (fe_now != NULL) {
662: free(fe->key);
663: free(fe);
664: free(fe_now->value);
665: fe = fe_now;
666: }
667:
668: fe->cb = NULL;
1.87 nicm 669: fe->t = 0;
1.79 nicm 670:
1.1 nicm 671: va_start(ap, fmt);
672: xvasprintf(&fe->value, fmt, ap);
673: va_end(ap);
1.79 nicm 674: }
675:
1.87 nicm 676: /* Add a key and time. */
1.108 nicm 677: static void
1.87 nicm 678: format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
679: {
680: struct format_entry *fe;
681: struct format_entry *fe_now;
682:
683: fe = xmalloc(sizeof *fe);
684: fe->key = xstrdup(key);
685:
686: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
687: if (fe_now != NULL) {
688: free(fe->key);
689: free(fe);
690: free(fe_now->value);
691: fe = fe_now;
692: }
693:
694: fe->cb = NULL;
695: fe->t = tv->tv_sec;
696:
697: fe->value = NULL;
698: }
699:
1.79 nicm 700: /* Add a key and function. */
1.108 nicm 701: static void
1.79 nicm 702: format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
703: {
704: struct format_entry *fe;
705: struct format_entry *fe_now;
706:
707: fe = xmalloc(sizeof *fe);
708: fe->key = xstrdup(key);
1.1 nicm 709:
1.68 nicm 710: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1.28 nicm 711: if (fe_now != NULL) {
712: free(fe->key);
713: free(fe);
1.79 nicm 714: free(fe_now->value);
715: fe = fe_now;
1.28 nicm 716: }
1.79 nicm 717:
718: fe->cb = cb;
1.87 nicm 719: fe->t = 0;
1.79 nicm 720:
721: fe->value = NULL;
1.1 nicm 722: }
723:
724: /* Find a format entry. */
1.108 nicm 725: static char *
1.87 nicm 726: format_find(struct format_tree *ft, const char *key, int modifiers)
1.1 nicm 727: {
728: struct format_entry *fe, fe_find;
1.76 nicm 729: struct environ_entry *envent;
1.87 nicm 730: static char s[64];
1.117 nicm 731: struct options_entry *o;
1.87 nicm 732: const char *found;
1.116 nicm 733: int idx;
1.87 nicm 734: char *copy, *saved;
735:
736: if (~modifiers & FORMAT_TIMESTRING) {
1.116 nicm 737: o = options_parse_get(global_options, key, &idx, 0);
1.87 nicm 738: if (o == NULL && ft->w != NULL)
1.116 nicm 739: o = options_parse_get(ft->w->options, key, &idx, 0);
1.87 nicm 740: if (o == NULL)
1.116 nicm 741: o = options_parse_get(global_w_options, key, &idx, 0);
1.87 nicm 742: if (o == NULL && ft->s != NULL)
1.116 nicm 743: o = options_parse_get(ft->s->options, key, &idx, 0);
1.87 nicm 744: if (o == NULL)
1.116 nicm 745: o = options_parse_get(global_s_options, key, &idx, 0);
1.87 nicm 746: if (o != NULL) {
1.118 nicm 747: found = options_tostring(o, idx, 1);
1.116 nicm 748: goto found;
1.54 nicm 749: }
750: }
1.116 nicm 751: found = NULL;
1.1 nicm 752:
753: fe_find.key = (char *) key;
1.68 nicm 754: fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.79 nicm 755: if (fe != NULL) {
1.87 nicm 756: if (modifiers & FORMAT_TIMESTRING) {
757: if (fe->t == 0)
758: return (NULL);
759: ctime_r(&fe->t, s);
760: s[strcspn(s, "\n")] = '\0';
761: found = s;
762: goto found;
763: }
764: if (fe->t != 0) {
765: xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
766: found = s;
767: goto found;
768: }
1.79 nicm 769: if (fe->value == NULL && fe->cb != NULL)
770: fe->cb(ft, fe);
1.87 nicm 771: found = fe->value;
772: goto found;
1.79 nicm 773: }
1.76 nicm 774:
1.87 nicm 775: if (~modifiers & FORMAT_TIMESTRING) {
776: envent = NULL;
777: if (ft->s != NULL)
1.91 nicm 778: envent = environ_find(ft->s->environ, key);
1.87 nicm 779: if (envent == NULL)
1.91 nicm 780: envent = environ_find(global_environ, key);
1.87 nicm 781: if (envent != NULL) {
782: found = envent->value;
783: goto found;
784: }
785: }
1.76 nicm 786:
787: return (NULL);
1.87 nicm 788:
789: found:
1.88 nicm 790: if (found == NULL)
791: return (NULL);
1.87 nicm 792: copy = xstrdup(found);
793: if (modifiers & FORMAT_BASENAME) {
794: saved = copy;
795: copy = xstrdup(basename(saved));
796: free(saved);
797: }
798: if (modifiers & FORMAT_DIRNAME) {
799: saved = copy;
800: copy = xstrdup(dirname(saved));
801: free(saved);
802: }
803: return (copy);
1.1 nicm 804: }
805:
1.114 nicm 806: /* Skip until comma. */
807: static char *
808: format_skip(char *s)
809: {
810: int brackets = 0;
811:
812: for (; *s != '\0'; s++) {
813: if (*s == '{')
814: brackets++;
815: if (*s == '}')
816: brackets--;
817: if (*s == ',' && brackets == 0)
818: break;
819: }
820: if (*s == '\0')
821: return (NULL);
822: return (s);
823: }
824:
825: /* Return left and right alternatives separated by commas. */
826: static int
827: format_choose(char *s, char **left, char **right)
828: {
829: char *cp;
830:
831: cp = format_skip(s);
832: if (cp == NULL)
833: return (-1);
834: *cp = '\0';
835:
836: *left = s;
837: *right = cp + 1;
838: return (0);
839: }
840:
841: /* Is this true? */
842: static int
843: format_true(const char *s)
844: {
845: if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
846: return (1);
847: return (0);
848: }
849:
1.1 nicm 850: /*
851: * Replace a key/value pair in buffer. #{blah} is expanded directly,
852: * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
853: */
1.108 nicm 854: static int
1.30 nicm 855: format_replace(struct format_tree *ft, const char *key, size_t keylen,
856: char **buf, size_t *len, size_t *off)
1.1 nicm 857: {
1.98 nicm 858: char *copy, *copy0, *endptr, *ptr, *found, *new, *value;
1.114 nicm 859: char *from = NULL, *to = NULL, *left, *right;
1.98 nicm 860: size_t valuelen, newlen, fromlen, tolen, used;
1.105 nicm 861: long limit = 0;
1.114 nicm 862: int modifiers = 0, compare = 0;
1.1 nicm 863:
864: /* Make a copy of the key. */
1.30 nicm 865: copy0 = copy = xmalloc(keylen + 1);
1.1 nicm 866: memcpy(copy, key, keylen);
867: copy[keylen] = '\0';
868:
1.30 nicm 869: /* Is there a length limit or whatnot? */
1.87 nicm 870: switch (copy[0]) {
1.114 nicm 871: case '!':
872: if (copy[1] == '=' && copy[2] == ':') {
873: compare = -1;
874: copy += 3;
875: break;
876: }
877: break;
1.87 nicm 878: case '=':
1.114 nicm 879: if (copy[1] == '=' && copy[2] == ':') {
880: compare = 1;
881: copy += 3;
882: break;
883: }
1.87 nicm 884: errno = 0;
1.105 nicm 885: limit = strtol(copy + 1, &endptr, 10);
886: if (errno == ERANGE && (limit == LONG_MIN || limit == LONG_MAX))
1.87 nicm 887: break;
888: if (*endptr != ':')
889: break;
890: copy = endptr + 1;
891: break;
892: case 'b':
893: if (copy[1] != ':')
894: break;
895: modifiers |= FORMAT_BASENAME;
896: copy += 2;
897: break;
898: case 'd':
899: if (copy[1] != ':')
900: break;
901: modifiers |= FORMAT_DIRNAME;
902: copy += 2;
903: break;
904: case 't':
905: if (copy[1] != ':')
906: break;
907: modifiers |= FORMAT_TIMESTRING;
908: copy += 2;
909: break;
1.98 nicm 910: case 's':
911: if (copy[1] != '/')
912: break;
913: from = copy + 2;
914: for (copy = from; *copy != '\0' && *copy != '/'; copy++)
915: /* nothing */;
916: if (copy[0] != '/' || copy == from) {
917: copy = copy0;
918: break;
919: }
920: copy[0] = '\0';
921: to = copy + 1;
922: for (copy = to; *copy != '\0' && *copy != '/'; copy++)
923: /* nothing */;
924: if (copy[0] != '/' || copy[1] != ':') {
925: copy = copy0;
926: break;
927: }
928: copy[0] = '\0';
929:
930: modifiers |= FORMAT_SUBSTITUTE;
931: copy += 2;
932: break;
1.30 nicm 933: }
934:
1.114 nicm 935: /* Is this a comparison or a conditional? */
936: if (compare != 0) {
937: /* Comparison: compare comma-separated left and right. */
938: if (format_choose(copy, &left, &right) != 0)
939: goto fail;
940: left = format_expand(ft, left);
941: right = format_expand(ft, right);
942: if (compare == 1 && strcmp(left, right) == 0)
943: value = xstrdup("1");
944: else if (compare == -1 && strcmp(left, right) != 0)
945: value = xstrdup("1");
946: else
947: value = xstrdup("0");
948: free(right);
949: free(left);
950: } else if (*copy == '?') {
951: /* Conditional: check first and choose second or third. */
952: ptr = format_skip(copy);
1.1 nicm 953: if (ptr == NULL)
954: goto fail;
955: *ptr = '\0';
956:
1.98 nicm 957: found = format_find(ft, copy + 1, modifiers);
1.121 nicm 958: if (found == NULL)
959: found = format_expand(ft, copy + 1);
1.114 nicm 960: if (format_choose(ptr + 1, &left, &right) != 0)
1.89 nicm 961: goto fail;
962:
1.114 nicm 963: if (format_true(found))
964: value = format_expand(ft, left);
965: else
966: value = format_expand(ft, right);
1.98 nicm 967: free(found);
1.1 nicm 968: } else {
1.114 nicm 969: /* Neither: look up directly. */
1.98 nicm 970: value = format_find(ft, copy, modifiers);
1.1 nicm 971: if (value == NULL)
1.98 nicm 972: value = xstrdup("");
973: }
974:
975: /* Perform substitution if any. */
976: if (modifiers & FORMAT_SUBSTITUTE) {
977: fromlen = strlen(from);
978: tolen = strlen(to);
979:
980: newlen = strlen(value) + 1;
981: copy = new = xmalloc(newlen);
982: for (ptr = value; *ptr != '\0'; /* nothing */) {
983: if (strncmp(ptr, from, fromlen) != 0) {
984: *new++ = *ptr++;
985: continue;
986: }
987: used = new - copy;
988:
989: newlen += tolen;
990: copy = xrealloc(copy, newlen);
991:
992: new = copy + used;
993: memcpy(new, to, tolen);
994:
995: new += tolen;
996: ptr += fromlen;
997: }
998: *new = '\0';
999: free(value);
1000: value = copy;
1.1 nicm 1001: }
1002:
1.30 nicm 1003: /* Truncate the value if needed. */
1.105 nicm 1004: if (limit > 0) {
1.98 nicm 1005: new = utf8_trimcstr(value, limit);
1.105 nicm 1006: free(value);
1007: value = new;
1008: } else if (limit < 0) {
1009: new = utf8_rtrimcstr(value, -limit);
1.98 nicm 1010: free(value);
1011: value = new;
1.44 nicm 1012: }
1.30 nicm 1013:
1.1 nicm 1014: /* Expand the buffer and copy in the value. */
1.98 nicm 1015: valuelen = strlen(value);
1.1 nicm 1016: while (*len - *off < valuelen + 1) {
1.50 nicm 1017: *buf = xreallocarray(*buf, 2, *len);
1.1 nicm 1018: *len *= 2;
1019: }
1020: memcpy(*buf + *off, value, valuelen);
1021: *off += valuelen;
1022:
1.98 nicm 1023: free(value);
1.30 nicm 1024: free(copy0);
1.1 nicm 1025: return (0);
1026:
1027: fail:
1.30 nicm 1028: free(copy0);
1.1 nicm 1029: return (-1);
1030: }
1031:
1.58 nicm 1032: /* Expand keys in a template, passing through strftime first. */
1033: char *
1034: format_expand_time(struct format_tree *ft, const char *fmt, time_t t)
1035: {
1036: struct tm *tm;
1.107 nicm 1037: char s[2048];
1.58 nicm 1038:
1.67 nicm 1039: if (fmt == NULL || *fmt == '\0')
1.58 nicm 1040: return (xstrdup(""));
1041:
1042: tm = localtime(&t);
1043:
1.107 nicm 1044: if (strftime(s, sizeof s, fmt, tm) == 0)
1045: return (xstrdup(""));
1.58 nicm 1046:
1.107 nicm 1047: return (format_expand(ft, s));
1.58 nicm 1048: }
1049:
1.1 nicm 1050: /* Expand keys in a template. */
1051: char *
1052: format_expand(struct format_tree *ft, const char *fmt)
1053: {
1.113 nicm 1054: char *buf, *out;
1.79 nicm 1055: const char *ptr, *s, *saved = fmt;
1.86 nicm 1056: size_t off, len, n, outlen;
1.31 nicm 1057: int ch, brackets;
1.58 nicm 1058:
1059: if (fmt == NULL)
1060: return (xstrdup(""));
1.1 nicm 1061:
1062: len = 64;
1063: buf = xmalloc(len);
1064: off = 0;
1065:
1066: while (*fmt != '\0') {
1067: if (*fmt != '#') {
1068: while (len - off < 2) {
1.50 nicm 1069: buf = xreallocarray(buf, 2, len);
1.1 nicm 1070: len *= 2;
1071: }
1072: buf[off++] = *fmt++;
1073: continue;
1074: }
1075: fmt++;
1076:
1077: ch = (u_char) *fmt++;
1078: switch (ch) {
1.68 nicm 1079: case '(':
1080: brackets = 1;
1081: for (ptr = fmt; *ptr != '\0'; ptr++) {
1082: if (*ptr == '(')
1083: brackets++;
1084: if (*ptr == ')' && --brackets == 0)
1085: break;
1086: }
1087: if (*ptr != ')' || brackets != 0)
1088: break;
1089: n = ptr - fmt;
1090:
1.114 nicm 1091: if (ft->flags & FORMAT_NOJOBS)
1092: out = xstrdup("");
1093: else
1094: out = format_job_get(ft, xstrndup(fmt, n));
1.86 nicm 1095: outlen = strlen(out);
1.68 nicm 1096:
1.86 nicm 1097: while (len - off < outlen + 1) {
1.68 nicm 1098: buf = xreallocarray(buf, 2, len);
1099: len *= 2;
1100: }
1.86 nicm 1101: memcpy(buf + off, out, outlen);
1102: off += outlen;
1103:
1104: free(out);
1.68 nicm 1105:
1106: fmt += n + 1;
1107: continue;
1.1 nicm 1108: case '{':
1.31 nicm 1109: brackets = 1;
1110: for (ptr = fmt; *ptr != '\0'; ptr++) {
1111: if (*ptr == '{')
1112: brackets++;
1113: if (*ptr == '}' && --brackets == 0)
1114: break;
1115: }
1116: if (*ptr != '}' || brackets != 0)
1.1 nicm 1117: break;
1118: n = ptr - fmt;
1119:
1120: if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
1121: break;
1122: fmt += n + 1;
1.40 nicm 1123: continue;
1124: case '#':
1125: while (len - off < 2) {
1.50 nicm 1126: buf = xreallocarray(buf, 2, len);
1.40 nicm 1127: len *= 2;
1128: }
1129: buf[off++] = '#';
1.1 nicm 1130: continue;
1131: default:
1.25 nicm 1132: s = NULL;
1133: if (ch >= 'A' && ch <= 'Z')
1134: s = format_upper[ch - 'A'];
1135: else if (ch >= 'a' && ch <= 'z')
1136: s = format_lower[ch - 'a'];
1137: if (s == NULL) {
1138: while (len - off < 3) {
1.50 nicm 1139: buf = xreallocarray(buf, 2, len);
1.25 nicm 1140: len *= 2;
1.1 nicm 1141: }
1.25 nicm 1142: buf[off++] = '#';
1143: buf[off++] = ch;
1144: continue;
1.1 nicm 1145: }
1.25 nicm 1146: n = strlen(s);
1147: if (format_replace(ft, s, n, &buf, &len, &off) != 0)
1148: break;
1.1 nicm 1149: continue;
1150: }
1151:
1152: break;
1153: }
1154: buf[off] = '\0';
1155:
1.79 nicm 1156: log_debug("format '%s' -> '%s'", saved, buf);
1.1 nicm 1157: return (buf);
1.123 nicm 1158: }
1159:
1160: /* Expand a single string. */
1161: char *
1162: format_single(struct cmdq_item *item, const char *fmt, struct client *c,
1163: struct session *s, struct winlink *wl, struct window_pane *wp)
1164: {
1165: struct format_tree *ft;
1166: char *expanded;
1167:
1.131 nicm 1168: if (item != NULL)
1169: ft = format_create(item->client, item, FORMAT_NONE, 0);
1170: else
1171: ft = format_create(NULL, item, FORMAT_NONE, 0);
1.123 nicm 1172: format_defaults(ft, c, s, wl, wp);
1173:
1174: expanded = format_expand(ft, fmt);
1175: format_free(ft);
1176: return (expanded);
1.1 nicm 1177: }
1178:
1.57 nicm 1179: /* Set defaults for any of arguments that are not NULL. */
1180: void
1181: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
1182: struct winlink *wl, struct window_pane *wp)
1183: {
1184: if (s == NULL && c != NULL)
1185: s = c->session;
1186: if (wl == NULL && s != NULL)
1187: wl = s->curw;
1188: if (wp == NULL && wl != NULL)
1189: wp = wl->window->active;
1190:
1191: if (c != NULL)
1192: format_defaults_client(ft, c);
1193: if (s != NULL)
1194: format_defaults_session(ft, s);
1.129 nicm 1195: if (wl != NULL)
1196: format_defaults_winlink(ft, wl);
1.57 nicm 1197: if (wp != NULL)
1198: format_defaults_pane(ft, wp);
1199: }
1200:
1.1 nicm 1201: /* Set default format keys for a session. */
1.108 nicm 1202: static void
1.57 nicm 1203: format_defaults_session(struct format_tree *ft, struct session *s)
1.1 nicm 1204: {
1205: struct session_group *sg;
1206:
1.54 nicm 1207: ft->s = s;
1208:
1.1 nicm 1209: format_add(ft, "session_name", "%s", s->name);
1210: format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
1211: format_add(ft, "session_width", "%u", s->sx);
1212: format_add(ft, "session_height", "%u", s->sy);
1.23 nicm 1213: format_add(ft, "session_id", "$%u", s->id);
1.1 nicm 1214:
1.122 nicm 1215: sg = session_group_contains(s);
1.1 nicm 1216: format_add(ft, "session_grouped", "%d", sg != NULL);
1217: if (sg != NULL)
1.122 nicm 1218: format_add(ft, "session_group", "%s", sg->name);
1.1 nicm 1219:
1.87 nicm 1220: format_add_tv(ft, "session_created", &s->creation_time);
1221: format_add_tv(ft, "session_last_attached", &s->last_attached_time);
1222: format_add_tv(ft, "session_activity", &s->activity_time);
1.1 nicm 1223:
1.41 nicm 1224: format_add(ft, "session_attached", "%u", s->attached);
1.59 nicm 1225: format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66 nicm 1226:
1.80 nicm 1227: format_add_cb(ft, "session_alerts", format_cb_session_alerts);
1.133 nicm 1228: format_add_cb(ft, "session_stack", format_cb_session_stack);
1.3 nicm 1229: }
1230:
1231: /* Set default format keys for a client. */
1.108 nicm 1232: static void
1.57 nicm 1233: format_defaults_client(struct format_tree *ft, struct client *c)
1.3 nicm 1234: {
1.60 nicm 1235: struct session *s;
1.103 nicm 1236: const char *name;
1.115 nicm 1237: struct tty *tty = &c->tty;
1238: const char *types[] = TTY_TYPES;
1.3 nicm 1239:
1.54 nicm 1240: if (ft->s == NULL)
1241: ft->s = c->session;
1242:
1.124 nicm 1243: format_add(ft, "client_name", "%s", c->name);
1.72 nicm 1244: format_add(ft, "client_pid", "%ld", (long) c->pid);
1.115 nicm 1245: format_add(ft, "client_height", "%u", tty->sy);
1246: format_add(ft, "client_width", "%u", tty->sx);
1.124 nicm 1247: format_add(ft, "client_tty", "%s", c->ttyname);
1.75 nicm 1248: format_add(ft, "client_control_mode", "%d",
1249: !!(c->flags & CLIENT_CONTROL));
1.3 nicm 1250:
1.115 nicm 1251: if (tty->term_name != NULL)
1252: format_add(ft, "client_termname", "%s", tty->term_name);
1253: if (tty->term_name != NULL)
1254: format_add(ft, "client_termtype", "%s", types[tty->term_type]);
1255:
1.87 nicm 1256: format_add_tv(ft, "client_created", &c->creation_time);
1257: format_add_tv(ft, "client_activity", &c->activity_time);
1.126 nicm 1258:
1259: format_add(ft, "client_written", "%zu", c->written);
1260: format_add(ft, "client_discarded", "%zu", c->discarded);
1.14 nicm 1261:
1.103 nicm 1262: name = server_client_get_key_table(c);
1263: if (strcmp(c->keytable->name, name) == 0)
1.61 nicm 1264: format_add(ft, "client_prefix", "%d", 0);
1265: else
1266: format_add(ft, "client_prefix", "%d", 1);
1267: format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3 nicm 1268:
1.115 nicm 1269: if (tty->flags & TTY_UTF8)
1.3 nicm 1270: format_add(ft, "client_utf8", "%d", 1);
1271: else
1272: format_add(ft, "client_utf8", "%d", 0);
1273:
1274: if (c->flags & CLIENT_READONLY)
1275: format_add(ft, "client_readonly", "%d", 1);
1276: else
1277: format_add(ft, "client_readonly", "%d", 0);
1.15 nicm 1278:
1279: s = c->session;
1280: if (s != NULL)
1281: format_add(ft, "client_session", "%s", s->name);
1282: s = c->last_session;
1283: if (s != NULL && session_alive(s))
1284: format_add(ft, "client_last_session", "%s", s->name);
1.1 nicm 1285: }
1286:
1.32 nicm 1287: /* Set default format keys for a window. */
1288: void
1.57 nicm 1289: format_defaults_window(struct format_tree *ft, struct window *w)
1.32 nicm 1290: {
1.54 nicm 1291: ft->w = w;
1292:
1.87 nicm 1293: format_add_tv(ft, "window_activity", &w->activity_time);
1.32 nicm 1294: format_add(ft, "window_id", "@%u", w->id);
1295: format_add(ft, "window_name", "%s", w->name);
1296: format_add(ft, "window_width", "%u", w->sx);
1297: format_add(ft, "window_height", "%u", w->sy);
1.80 nicm 1298: format_add_cb(ft, "window_layout", format_cb_window_layout);
1.96 nicm 1299: format_add_cb(ft, "window_visible_layout",
1300: format_cb_window_visible_layout);
1.32 nicm 1301: format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59 nicm 1302: format_add(ft, "window_zoomed_flag", "%d",
1.53 nicm 1303: !!(w->flags & WINDOW_ZOOMED));
1.32 nicm 1304: }
1305:
1.1 nicm 1306: /* Set default format keys for a winlink. */
1.108 nicm 1307: static void
1.129 nicm 1308: format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
1.1 nicm 1309: {
1.129 nicm 1310: struct session *s = wl->session;
1.1 nicm 1311: struct window *w = wl->window;
1312:
1.54 nicm 1313: if (ft->w == NULL)
1314: ft->w = wl->window;
1.133 nicm 1315: ft->wl = wl;
1.54 nicm 1316:
1.57 nicm 1317: format_defaults_window(ft, w);
1.32 nicm 1318:
1.1 nicm 1319: format_add(ft, "window_index", "%d", wl->idx);
1.133 nicm 1320: format_add_cb(ft, "window_stack_index", format_cb_window_stack_index);
1.129 nicm 1321: format_add(ft, "window_flags", "%s", window_printable_flags(wl));
1.1 nicm 1322: format_add(ft, "window_active", "%d", wl == s->curw);
1.29 nicm 1323:
1.59 nicm 1324: format_add(ft, "window_bell_flag", "%d",
1.29 nicm 1325: !!(wl->flags & WINLINK_BELL));
1.59 nicm 1326: format_add(ft, "window_activity_flag", "%d",
1.29 nicm 1327: !!(wl->flags & WINLINK_ACTIVITY));
1.59 nicm 1328: format_add(ft, "window_silence_flag", "%d",
1.29 nicm 1329: !!(wl->flags & WINLINK_SILENCE));
1.59 nicm 1330: format_add(ft, "window_last_flag", "%d",
1.49 nicm 1331: !!(wl == TAILQ_FIRST(&s->lastw)));
1.64 nicm 1332: format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.1 nicm 1333: }
1334:
1335: /* Set default format keys for a window pane. */
1336: void
1.57 nicm 1337: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1 nicm 1338: {
1.80 nicm 1339: struct grid *gd = wp->base.grid;
1340: u_int idx;
1.85 nicm 1341: int status, scroll_position;
1.54 nicm 1342:
1343: if (ft->w == NULL)
1344: ft->w = wp->window;
1.79 nicm 1345: ft->wp = wp;
1.1 nicm 1346:
1.16 nicm 1347: format_add(ft, "history_size", "%u", gd->hsize);
1348: format_add(ft, "history_limit", "%u", gd->hlimit);
1.80 nicm 1349: format_add_cb(ft, "history_bytes", format_cb_history_bytes);
1.1 nicm 1350:
1.4 nicm 1351: if (window_pane_index(wp, &idx) != 0)
1352: fatalx("index not found");
1.16 nicm 1353: format_add(ft, "pane_index", "%u", idx);
1.4 nicm 1354:
1.1 nicm 1355: format_add(ft, "pane_width", "%u", wp->sx);
1356: format_add(ft, "pane_height", "%u", wp->sy);
1357: format_add(ft, "pane_title", "%s", wp->base.title);
1358: format_add(ft, "pane_id", "%%%u", wp->id);
1359: format_add(ft, "pane_active", "%d", wp == wp->window->active);
1.55 nicm 1360: format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
1361:
1362: status = wp->status;
1363: if (wp->fd == -1 && WIFEXITED(status))
1364: format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1 nicm 1365: format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47 nicm 1366:
1367: if (window_pane_visible(wp)) {
1368: format_add(ft, "pane_left", "%u", wp->xoff);
1369: format_add(ft, "pane_top", "%u", wp->yoff);
1370: format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
1371: format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
1372: }
1.16 nicm 1373:
1374: format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
1.134 nicm 1375: if (wp->mode != NULL)
1376: format_add(ft, "pane_mode", "%s", wp->mode->name);
1377:
1.26 nicm 1378: format_add(ft, "pane_synchronized", "%d",
1.90 nicm 1379: !!options_get_number(wp->window->options, "synchronize-panes"));
1.135 nicm 1380: if (wp->searchstr != NULL)
1381: format_add(ft, "pane_search_string", "%s", wp->searchstr);
1.16 nicm 1382:
1.71 nicm 1383: format_add(ft, "pane_tty", "%s", wp->tty);
1.16 nicm 1384: format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.79 nicm 1385: format_add_cb(ft, "pane_start_command", format_cb_start_command);
1386: format_add_cb(ft, "pane_current_command", format_cb_current_command);
1.16 nicm 1387:
1.59 nicm 1388: format_add(ft, "cursor_x", "%u", wp->base.cx);
1389: format_add(ft, "cursor_y", "%u", wp->base.cy);
1390: format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
1391: format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.85 nicm 1392:
1393: scroll_position = window_copy_scroll_position(wp);
1394: if (scroll_position != -1)
1395: format_add(ft, "scroll_position", "%d", scroll_position);
1.16 nicm 1396:
1397: format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59 nicm 1398: format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
1399: format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16 nicm 1400:
1401: format_add(ft, "cursor_flag", "%d",
1402: !!(wp->base.mode & MODE_CURSOR));
1403: format_add(ft, "insert_flag", "%d",
1404: !!(wp->base.mode & MODE_INSERT));
1405: format_add(ft, "keypad_cursor_flag", "%d",
1406: !!(wp->base.mode & MODE_KCURSOR));
1407: format_add(ft, "keypad_flag", "%d",
1408: !!(wp->base.mode & MODE_KKEYPAD));
1409: format_add(ft, "wrap_flag", "%d",
1410: !!(wp->base.mode & MODE_WRAP));
1411:
1.62 nicm 1412: format_add(ft, "mouse_any_flag", "%d",
1.119 nicm 1413: !!(wp->base.mode & ALL_MOUSE_MODES));
1.16 nicm 1414: format_add(ft, "mouse_standard_flag", "%d",
1415: !!(wp->base.mode & MODE_MOUSE_STANDARD));
1416: format_add(ft, "mouse_button_flag", "%d",
1417: !!(wp->base.mode & MODE_MOUSE_BUTTON));
1.119 nicm 1418: format_add(ft, "mouse_all_flag", "%d",
1419: !!(wp->base.mode & MODE_MOUSE_ALL));
1.19 nicm 1420:
1.80 nicm 1421: format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
1.8 nicm 1422: }
1423:
1.19 nicm 1424: /* Set default format keys for paste buffer. */
1.8 nicm 1425: void
1.94 nicm 1426: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
1.8 nicm 1427: {
1.81 nicm 1428: size_t bufsize;
1.42 nicm 1429: char *s;
1.8 nicm 1430:
1.81 nicm 1431: paste_buffer_data(pb, &bufsize);
1432: format_add(ft, "buffer_size", "%zu", bufsize);
1433: format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
1.8 nicm 1434:
1.94 nicm 1435: s = paste_make_sample(pb);
1.42 nicm 1436: format_add(ft, "buffer_sample", "%s", s);
1437: free(s);
1.1 nicm 1438: }