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