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