Annotation of src/usr.bin/tmux/format.c, Revision 1.85
1.85 ! nicm 1: /* $OpenBSD: format.c,v 1.84 2015/09/14 10:25:52 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 *);
42: const 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. */
215: const char *
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:
245: return (fj->out);
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.69 nicm 712: char *buf, *tmp, *cmd;
1.79 nicm 713: const char *ptr, *s, *saved = fmt;
1.68 nicm 714: size_t off, len, n, slen;
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.69 nicm 754: s = format_job_get(ft, cmd);
1.68 nicm 755: slen = strlen(s);
1.69 nicm 756:
757: free(cmd);
758: free(tmp);
1.68 nicm 759:
760: while (len - off < slen + 1) {
761: buf = xreallocarray(buf, 2, len);
762: len *= 2;
763: }
764: memcpy(buf + off, s, slen);
765: off += slen;
766:
767: fmt += n + 1;
768: continue;
1.1 nicm 769: case '{':
1.31 nicm 770: brackets = 1;
771: for (ptr = fmt; *ptr != '\0'; ptr++) {
772: if (*ptr == '{')
773: brackets++;
774: if (*ptr == '}' && --brackets == 0)
775: break;
776: }
777: if (*ptr != '}' || brackets != 0)
1.1 nicm 778: break;
779: n = ptr - fmt;
780:
781: if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
782: break;
783: fmt += n + 1;
1.40 nicm 784: continue;
785: case '#':
786: while (len - off < 2) {
1.50 nicm 787: buf = xreallocarray(buf, 2, len);
1.40 nicm 788: len *= 2;
789: }
790: buf[off++] = '#';
1.1 nicm 791: continue;
792: default:
1.25 nicm 793: s = NULL;
794: if (ch >= 'A' && ch <= 'Z')
795: s = format_upper[ch - 'A'];
796: else if (ch >= 'a' && ch <= 'z')
797: s = format_lower[ch - 'a'];
798: if (s == NULL) {
799: while (len - off < 3) {
1.50 nicm 800: buf = xreallocarray(buf, 2, len);
1.25 nicm 801: len *= 2;
1.1 nicm 802: }
1.25 nicm 803: buf[off++] = '#';
804: buf[off++] = ch;
805: continue;
1.1 nicm 806: }
1.25 nicm 807: n = strlen(s);
808: if (format_replace(ft, s, n, &buf, &len, &off) != 0)
809: break;
1.1 nicm 810: continue;
811: }
812:
813: break;
814: }
815: buf[off] = '\0';
816:
1.79 nicm 817: log_debug("format '%s' -> '%s'", saved, buf);
1.1 nicm 818: return (buf);
819: }
820:
1.60 nicm 821: /* Get time as a string. */
822: char *
823: format_time_string(time_t t)
824: {
825: char *tim;
826:
827: tim = ctime(&t);
828: *strchr(tim, '\n') = '\0';
829:
830: return (tim);
831: }
832:
1.57 nicm 833: /* Set defaults for any of arguments that are not NULL. */
834: void
835: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
836: struct winlink *wl, struct window_pane *wp)
837: {
838: if (s == NULL && c != NULL)
839: s = c->session;
840: if (wl == NULL && s != NULL)
841: wl = s->curw;
842: if (wp == NULL && wl != NULL)
843: wp = wl->window->active;
844:
845: if (c != NULL)
846: format_defaults_client(ft, c);
847: if (s != NULL)
848: format_defaults_session(ft, s);
849: if (s != NULL && wl != NULL)
850: format_defaults_winlink(ft, s, wl);
851: if (wp != NULL)
852: format_defaults_pane(ft, wp);
853: }
854:
1.1 nicm 855: /* Set default format keys for a session. */
856: void
1.57 nicm 857: format_defaults_session(struct format_tree *ft, struct session *s)
1.1 nicm 858: {
859: struct session_group *sg;
860: time_t t;
861:
1.54 nicm 862: ft->s = s;
863:
1.1 nicm 864: format_add(ft, "session_name", "%s", s->name);
865: format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
866: format_add(ft, "session_width", "%u", s->sx);
867: format_add(ft, "session_height", "%u", s->sy);
1.23 nicm 868: format_add(ft, "session_id", "$%u", s->id);
1.1 nicm 869:
870: sg = session_group_find(s);
871: format_add(ft, "session_grouped", "%d", sg != NULL);
872: if (sg != NULL)
873: format_add(ft, "session_group", "%u", session_group_index(sg));
874:
875: t = s->creation_time.tv_sec;
1.24 deraadt 876: format_add(ft, "session_created", "%lld", (long long) t);
1.60 nicm 877: format_add(ft, "session_created_string", "%s", format_time_string(t));
1.83 nicm 878:
879: t = s->last_attached_time.tv_sec;
880: if (t != 0) { /* zero if never attached */
881: format_add(ft, "session_last_attached", "%lld", (long long) t);
882: format_add(ft, "session_last_attached_string", "%s",
883: format_time_string(t));
884: }
1.60 nicm 885:
886: t = s->activity_time.tv_sec;
887: format_add(ft, "session_activity", "%lld", (long long) t);
888: format_add(ft, "session_activity_string", "%s", format_time_string(t));
1.1 nicm 889:
1.41 nicm 890: format_add(ft, "session_attached", "%u", s->attached);
1.59 nicm 891: format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66 nicm 892:
1.80 nicm 893: format_add_cb(ft, "session_alerts", format_cb_session_alerts);
1.3 nicm 894: }
895:
896: /* Set default format keys for a client. */
897: void
1.57 nicm 898: format_defaults_client(struct format_tree *ft, struct client *c)
1.3 nicm 899: {
1.60 nicm 900: struct session *s;
1.15 nicm 901: time_t t;
1.3 nicm 902:
1.54 nicm 903: if (ft->s == NULL)
904: ft->s = c->session;
905:
1.72 nicm 906: format_add(ft, "client_pid", "%ld", (long) c->pid);
1.6 nicm 907: format_add(ft, "client_height", "%u", c->tty.sy);
908: format_add(ft, "client_width", "%u", c->tty.sx);
1.27 nicm 909: if (c->tty.path != NULL)
910: format_add(ft, "client_tty", "%s", c->tty.path);
911: if (c->tty.termname != NULL)
912: format_add(ft, "client_termname", "%s", c->tty.termname);
1.75 nicm 913: format_add(ft, "client_control_mode", "%d",
914: !!(c->flags & CLIENT_CONTROL));
1.3 nicm 915:
916: t = c->creation_time.tv_sec;
1.24 deraadt 917: format_add(ft, "client_created", "%lld", (long long) t);
1.60 nicm 918: format_add(ft, "client_created_string", "%s", format_time_string(t));
1.3 nicm 919:
920: t = c->activity_time.tv_sec;
1.24 deraadt 921: format_add(ft, "client_activity", "%lld", (long long) t);
1.60 nicm 922: format_add(ft, "client_activity_string", "%s", format_time_string(t));
1.14 nicm 923:
1.61 nicm 924: if (strcmp(c->keytable->name, "root") == 0)
925: format_add(ft, "client_prefix", "%d", 0);
926: else
927: format_add(ft, "client_prefix", "%d", 1);
928: format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3 nicm 929:
930: if (c->tty.flags & TTY_UTF8)
931: format_add(ft, "client_utf8", "%d", 1);
932: else
933: format_add(ft, "client_utf8", "%d", 0);
934:
935: if (c->flags & CLIENT_READONLY)
936: format_add(ft, "client_readonly", "%d", 1);
937: else
938: format_add(ft, "client_readonly", "%d", 0);
1.15 nicm 939:
940: s = c->session;
941: if (s != NULL)
942: format_add(ft, "client_session", "%s", s->name);
943: s = c->last_session;
944: if (s != NULL && session_alive(s))
945: format_add(ft, "client_last_session", "%s", s->name);
1.1 nicm 946: }
947:
1.32 nicm 948: /* Set default format keys for a window. */
949: void
1.57 nicm 950: format_defaults_window(struct format_tree *ft, struct window *w)
1.32 nicm 951: {
1.73 nicm 952: time_t t;
1.32 nicm 953:
1.54 nicm 954: ft->w = w;
955:
1.73 nicm 956: t = w->activity_time.tv_sec;
957: format_add(ft, "window_activity", "%lld", (long long) t);
958: format_add(ft, "window_activity_string", "%s", format_time_string(t));
1.32 nicm 959:
960: format_add(ft, "window_id", "@%u", w->id);
961: format_add(ft, "window_name", "%s", w->name);
962: format_add(ft, "window_width", "%u", w->sx);
963: format_add(ft, "window_height", "%u", w->sy);
1.80 nicm 964: format_add_cb(ft, "window_layout", format_cb_window_layout);
1.32 nicm 965: format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59 nicm 966: format_add(ft, "window_zoomed_flag", "%d",
1.53 nicm 967: !!(w->flags & WINDOW_ZOOMED));
1.32 nicm 968: }
969:
1.1 nicm 970: /* Set default format keys for a winlink. */
971: void
1.57 nicm 972: format_defaults_winlink(struct format_tree *ft, struct session *s,
973: struct winlink *wl)
1.1 nicm 974: {
975: struct window *w = wl->window;
1.32 nicm 976: char *flags;
1.1 nicm 977:
1.54 nicm 978: if (ft->w == NULL)
979: ft->w = wl->window;
980:
1.1 nicm 981: flags = window_printable_flags(s, wl);
982:
1.57 nicm 983: format_defaults_window(ft, w);
1.32 nicm 984:
1.1 nicm 985: format_add(ft, "window_index", "%d", wl->idx);
986: format_add(ft, "window_flags", "%s", flags);
987: format_add(ft, "window_active", "%d", wl == s->curw);
1.29 nicm 988:
1.59 nicm 989: format_add(ft, "window_bell_flag", "%d",
1.29 nicm 990: !!(wl->flags & WINLINK_BELL));
1.59 nicm 991: format_add(ft, "window_activity_flag", "%d",
1.29 nicm 992: !!(wl->flags & WINLINK_ACTIVITY));
1.59 nicm 993: format_add(ft, "window_silence_flag", "%d",
1.29 nicm 994: !!(wl->flags & WINLINK_SILENCE));
1.59 nicm 995: format_add(ft, "window_last_flag", "%d",
1.49 nicm 996: !!(wl == TAILQ_FIRST(&s->lastw)));
1.64 nicm 997: format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.32 nicm 998:
1.9 nicm 999: free(flags);
1.1 nicm 1000: }
1001:
1002: /* Set default format keys for a window pane. */
1003: void
1.57 nicm 1004: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1 nicm 1005: {
1.80 nicm 1006: struct grid *gd = wp->base.grid;
1007: u_int idx;
1.85 ! nicm 1008: int status, scroll_position;
1.54 nicm 1009:
1010: if (ft->w == NULL)
1011: ft->w = wp->window;
1.79 nicm 1012: ft->wp = wp;
1.1 nicm 1013:
1.16 nicm 1014: format_add(ft, "history_size", "%u", gd->hsize);
1015: format_add(ft, "history_limit", "%u", gd->hlimit);
1.80 nicm 1016: format_add_cb(ft, "history_bytes", format_cb_history_bytes);
1.1 nicm 1017:
1.4 nicm 1018: if (window_pane_index(wp, &idx) != 0)
1019: fatalx("index not found");
1.16 nicm 1020: format_add(ft, "pane_index", "%u", idx);
1.4 nicm 1021:
1.1 nicm 1022: format_add(ft, "pane_width", "%u", wp->sx);
1023: format_add(ft, "pane_height", "%u", wp->sy);
1024: format_add(ft, "pane_title", "%s", wp->base.title);
1025: format_add(ft, "pane_id", "%%%u", wp->id);
1026: format_add(ft, "pane_active", "%d", wp == wp->window->active);
1.55 nicm 1027: format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
1028:
1029: status = wp->status;
1030: if (wp->fd == -1 && WIFEXITED(status))
1031: format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1 nicm 1032: format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47 nicm 1033:
1034: if (window_pane_visible(wp)) {
1035: format_add(ft, "pane_left", "%u", wp->xoff);
1036: format_add(ft, "pane_top", "%u", wp->yoff);
1037: format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
1038: format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
1039: }
1.16 nicm 1040:
1041: format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
1.26 nicm 1042: format_add(ft, "pane_synchronized", "%d",
1043: !!options_get_number(&wp->window->options, "synchronize-panes"));
1.16 nicm 1044:
1.71 nicm 1045: format_add(ft, "pane_tty", "%s", wp->tty);
1.16 nicm 1046: format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.79 nicm 1047: format_add_cb(ft, "pane_start_command", format_cb_start_command);
1048: format_add_cb(ft, "pane_current_command", format_cb_current_command);
1.16 nicm 1049:
1.59 nicm 1050: format_add(ft, "cursor_x", "%u", wp->base.cx);
1051: format_add(ft, "cursor_y", "%u", wp->base.cy);
1052: format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
1053: format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.85 ! nicm 1054:
! 1055: scroll_position = window_copy_scroll_position(wp);
! 1056: if (scroll_position != -1)
! 1057: format_add(ft, "scroll_position", "%d", scroll_position);
1.16 nicm 1058:
1059: format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59 nicm 1060: format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
1061: format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16 nicm 1062:
1063: format_add(ft, "cursor_flag", "%d",
1064: !!(wp->base.mode & MODE_CURSOR));
1065: format_add(ft, "insert_flag", "%d",
1066: !!(wp->base.mode & MODE_INSERT));
1067: format_add(ft, "keypad_cursor_flag", "%d",
1068: !!(wp->base.mode & MODE_KCURSOR));
1069: format_add(ft, "keypad_flag", "%d",
1070: !!(wp->base.mode & MODE_KKEYPAD));
1071: format_add(ft, "wrap_flag", "%d",
1072: !!(wp->base.mode & MODE_WRAP));
1073:
1.62 nicm 1074: format_add(ft, "mouse_any_flag", "%d",
1075: !!(wp->base.mode & (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON)));
1.16 nicm 1076: format_add(ft, "mouse_standard_flag", "%d",
1077: !!(wp->base.mode & MODE_MOUSE_STANDARD));
1078: format_add(ft, "mouse_button_flag", "%d",
1079: !!(wp->base.mode & MODE_MOUSE_BUTTON));
1080: format_add(ft, "mouse_utf8_flag", "%d",
1081: !!(wp->base.mode & MODE_MOUSE_UTF8));
1.19 nicm 1082:
1.80 nicm 1083: format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
1.8 nicm 1084: }
1085:
1.19 nicm 1086: /* Set default format keys for paste buffer. */
1.8 nicm 1087: void
1.57 nicm 1088: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb,
1.42 nicm 1089: int utf8flag)
1.8 nicm 1090: {
1.81 nicm 1091: size_t bufsize;
1.42 nicm 1092: char *s;
1.8 nicm 1093:
1.81 nicm 1094: paste_buffer_data(pb, &bufsize);
1095: format_add(ft, "buffer_size", "%zu", bufsize);
1096: format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
1.8 nicm 1097:
1.42 nicm 1098: s = paste_make_sample(pb, utf8flag);
1099: format_add(ft, "buffer_sample", "%s", s);
1100: free(s);
1.1 nicm 1101: }