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