Annotation of src/usr.bin/tmux/format.c, Revision 1.175
1.175 ! nicm 1: /* $OpenBSD: format.c,v 1.174 2019/03/14 21:27:26 nicm Exp $ */
1.1 nicm 2:
3: /*
1.104 nicm 4: * Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
1.55 nicm 20: #include <sys/wait.h>
1.1 nicm 21:
1.157 nicm 22: #include <ctype.h>
1.30 nicm 23: #include <errno.h>
1.139 nicm 24: #include <fnmatch.h>
1.87 nicm 25: #include <libgen.h>
1.1 nicm 26: #include <stdarg.h>
1.9 nicm 27: #include <stdlib.h>
1.1 nicm 28: #include <string.h>
29: #include <time.h>
30: #include <unistd.h>
31:
32: #include "tmux.h"
33:
34: /*
35: * Build a list of key-value pairs and use them to expand #{key} entries in a
36: * string.
37: */
38:
1.79 nicm 39: struct format_entry;
40: typedef void (*format_cb)(struct format_tree *, struct format_entry *);
41:
1.108 nicm 42: static char *format_job_get(struct format_tree *, const char *);
43: static void format_job_timer(int, short, void *);
44:
45: static char *format_find(struct format_tree *, const char *, int);
46: static void format_add_cb(struct format_tree *, const char *, format_cb);
47: static void format_add_tv(struct format_tree *, const char *,
48: struct timeval *);
49: static int format_replace(struct format_tree *, const char *, size_t,
50: char **, size_t *, size_t *);
51:
52: static void format_defaults_session(struct format_tree *,
53: struct session *);
54: static void format_defaults_client(struct format_tree *, struct client *);
1.164 nicm 55: static void format_defaults_winlink(struct format_tree *, struct winlink *);
1.1 nicm 56:
1.68 nicm 57: /* Entry in format job tree. */
58: struct format_job {
1.131 nicm 59: struct client *client;
1.120 nicm 60: u_int tag;
1.68 nicm 61: const char *cmd;
1.113 nicm 62: const char *expanded;
1.68 nicm 63:
64: time_t last;
65: char *out;
1.127 nicm 66: int updated;
1.68 nicm 67:
68: struct job *job;
69: int status;
70:
71: RB_ENTRY(format_job) entry;
72: };
73:
74: /* Format job tree. */
1.108 nicm 75: static struct event format_job_event;
76: static int format_job_cmp(struct format_job *, struct format_job *);
1.109 nicm 77: static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
1.108 nicm 78: RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp);
1.68 nicm 79:
80: /* Format job tree comparison function. */
1.108 nicm 81: static int
1.68 nicm 82: format_job_cmp(struct format_job *fj1, struct format_job *fj2)
83: {
1.120 nicm 84: if (fj1->tag < fj2->tag)
85: return (-1);
86: if (fj1->tag > fj2->tag)
87: return (1);
1.68 nicm 88: return (strcmp(fj1->cmd, fj2->cmd));
89: }
90:
1.87 nicm 91: /* Format modifiers. */
92: #define FORMAT_TIMESTRING 0x1
93: #define FORMAT_BASENAME 0x2
94: #define FORMAT_DIRNAME 0x4
1.169 nicm 95: #define FORMAT_QUOTE 0x8
96: #define FORMAT_LITERAL 0x10
1.170 nicm 97: #define FORMAT_EXPAND 0x20
1.175 ! nicm 98: #define FORMAT_EXPANDTIME 0x40
! 99: #define FORMAT_SESSIONS 0x80
! 100: #define FORMAT_WINDOWS 0x100
! 101: #define FORMAT_PANES 0x200
1.87 nicm 102:
1.54 nicm 103: /* Entry in format tree. */
104: struct format_entry {
1.79 nicm 105: char *key;
106: char *value;
1.87 nicm 107: time_t t;
1.79 nicm 108: format_cb cb;
109: RB_ENTRY(format_entry) entry;
1.54 nicm 110: };
111:
1.68 nicm 112: /* Format entry tree. */
1.54 nicm 113: struct format_tree {
1.164 nicm 114: struct client *c;
115: struct session *s;
116: struct winlink *wl;
1.79 nicm 117: struct window *w;
118: struct window_pane *wp;
1.54 nicm 119:
1.172 nicm 120: struct cmdq_item *item;
1.131 nicm 121: struct client *client;
1.120 nicm 122: u_int tag;
1.84 nicm 123: int flags;
1.68 nicm 124:
125: RB_HEAD(format_entry_tree, format_entry) tree;
1.54 nicm 126: };
1.108 nicm 127: static int format_entry_cmp(struct format_entry *, struct format_entry *);
128: RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
1.54 nicm 129:
1.169 nicm 130: /* Format modifiers. */
131: struct format_modifier {
132: char modifier[3];
133: u_int size;
134:
135: char **argv;
136: int argc;
137: };
138:
1.68 nicm 139: /* Format entry tree comparison function. */
1.108 nicm 140: static int
1.68 nicm 141: format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
1.1 nicm 142: {
143: return (strcmp(fe1->key, fe2->key));
144: }
145:
1.25 nicm 146: /* Single-character uppercase aliases. */
1.108 nicm 147: static const char *format_upper[] = {
1.1 nicm 148: NULL, /* A */
149: NULL, /* B */
150: NULL, /* C */
151: "pane_id", /* D */
152: NULL, /* E */
153: "window_flags", /* F */
154: NULL, /* G */
155: "host", /* H */
156: "window_index", /* I */
157: NULL, /* J */
158: NULL, /* K */
159: NULL, /* L */
160: NULL, /* M */
161: NULL, /* N */
162: NULL, /* O */
163: "pane_index", /* P */
164: NULL, /* Q */
165: NULL, /* R */
166: "session_name", /* S */
167: "pane_title", /* T */
168: NULL, /* U */
169: NULL, /* V */
170: "window_name", /* W */
171: NULL, /* X */
172: NULL, /* Y */
173: NULL /* Z */
174: };
175:
1.25 nicm 176: /* Single-character lowercase aliases. */
1.108 nicm 177: static const char *format_lower[] = {
1.25 nicm 178: NULL, /* a */
179: NULL, /* b */
180: NULL, /* c */
181: NULL, /* d */
182: NULL, /* e */
183: NULL, /* f */
184: NULL, /* g */
185: "host_short", /* h */
186: NULL, /* i */
187: NULL, /* j */
188: NULL, /* k */
189: NULL, /* l */
190: NULL, /* m */
191: NULL, /* n */
192: NULL, /* o */
193: NULL, /* p */
194: NULL, /* q */
195: NULL, /* r */
196: NULL, /* s */
197: NULL, /* t */
198: NULL, /* u */
199: NULL, /* v */
200: NULL, /* w */
201: NULL, /* x */
202: NULL, /* y */
203: NULL /* z */
204: };
205:
1.127 nicm 206: /* Format job update callback. */
1.108 nicm 207: static void
1.127 nicm 208: format_job_update(struct job *job)
209: {
1.160 nicm 210: struct format_job *fj = job_get_data(job);
211: struct evbuffer *evb = job_get_event(job)->input;
1.151 nicm 212: char *line = NULL, *next;
1.127 nicm 213: time_t t;
214:
1.151 nicm 215: while ((next = evbuffer_readline(evb)) != NULL) {
216: free(line);
217: line = next;
218: }
219: if (line == NULL)
1.127 nicm 220: return;
221: fj->updated = 1;
222:
223: free(fj->out);
224: fj->out = line;
225:
1.136 nicm 226: log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out);
1.127 nicm 227:
1.142 nicm 228: t = time(NULL);
1.127 nicm 229: if (fj->status && fj->last != t) {
1.136 nicm 230: if (fj->client != NULL)
231: server_status_client(fj->client);
1.127 nicm 232: fj->last = t;
233: }
234: }
235:
236: /* Format job complete callback. */
237: static void
238: format_job_complete(struct job *job)
1.68 nicm 239: {
1.160 nicm 240: struct format_job *fj = job_get_data(job);
241: struct evbuffer *evb = job_get_event(job)->input;
1.68 nicm 242: char *line, *buf;
243: size_t len;
244:
245: fj->job = NULL;
246:
247: buf = NULL;
1.160 nicm 248: if ((line = evbuffer_readline(evb)) == NULL) {
249: len = EVBUFFER_LENGTH(evb);
1.68 nicm 250: buf = xmalloc(len + 1);
251: if (len != 0)
1.160 nicm 252: memcpy(buf, EVBUFFER_DATA(evb), len);
1.68 nicm 253: buf[len] = '\0';
254: } else
255: buf = line;
1.127 nicm 256:
1.136 nicm 257: log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf);
258:
1.127 nicm 259: if (*buf != '\0' || !fj->updated) {
260: free(fj->out);
261: fj->out = buf;
262: } else
263: free(buf);
1.68 nicm 264:
265: if (fj->status) {
1.131 nicm 266: if (fj->client != NULL)
267: server_status_client(fj->client);
1.68 nicm 268: fj->status = 0;
269: }
270: }
271:
272: /* Find a job. */
1.108 nicm 273: static char *
1.68 nicm 274: format_job_get(struct format_tree *ft, const char *cmd)
275: {
1.131 nicm 276: struct format_job_tree *jobs;
1.113 nicm 277: struct format_job fj0, *fj;
278: time_t t;
279: char *expanded;
280: int force;
1.68 nicm 281:
1.131 nicm 282: if (ft->client == NULL)
283: jobs = &format_jobs;
284: else if (ft->client->jobs != NULL)
285: jobs = ft->client->jobs;
286: else {
287: jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
288: RB_INIT(jobs);
289: }
290:
1.120 nicm 291: fj0.tag = ft->tag;
1.68 nicm 292: fj0.cmd = cmd;
1.131 nicm 293: if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
1.68 nicm 294: fj = xcalloc(1, sizeof *fj);
1.131 nicm 295: fj->client = ft->client;
1.120 nicm 296: fj->tag = ft->tag;
1.68 nicm 297: fj->cmd = xstrdup(cmd);
1.113 nicm 298: fj->expanded = NULL;
1.68 nicm 299:
300: xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
301:
1.131 nicm 302: RB_INSERT(format_job_tree, jobs, fj);
1.68 nicm 303: }
304:
1.113 nicm 305: expanded = format_expand(ft, cmd);
306: if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
307: free((void *)fj->expanded);
308: fj->expanded = xstrdup(expanded);
309: force = 1;
310: } else
311: force = (ft->flags & FORMAT_FORCE);
312:
1.84 nicm 313: t = time(NULL);
1.113 nicm 314: if (fj->job == NULL && (force || fj->last != t)) {
1.163 nicm 315: fj->job = job_run(expanded, NULL,
316: server_client_get_cwd(ft->client, NULL), format_job_update,
1.153 nicm 317: format_job_complete, NULL, fj, JOB_NOWAIT);
1.68 nicm 318: if (fj->job == NULL) {
319: free(fj->out);
320: xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
321: }
1.84 nicm 322: fj->last = t;
1.137 nicm 323: fj->updated = 0;
1.68 nicm 324: }
1.84 nicm 325:
326: if (ft->flags & FORMAT_STATUS)
327: fj->status = 1;
1.68 nicm 328:
1.113 nicm 329: free(expanded);
1.86 nicm 330: return (format_expand(ft, fj->out));
1.68 nicm 331: }
332:
333: /* Remove old jobs. */
1.108 nicm 334: static void
1.131 nicm 335: format_job_tidy(struct format_job_tree *jobs, int force)
1.68 nicm 336: {
337: struct format_job *fj, *fj1;
338: time_t now;
339:
340: now = time(NULL);
1.131 nicm 341: RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
342: if (!force && (fj->last > now || now - fj->last < 3600))
1.68 nicm 343: continue;
1.131 nicm 344: RB_REMOVE(format_job_tree, jobs, fj);
1.68 nicm 345:
1.77 nicm 346: log_debug("%s: %s", __func__, fj->cmd);
347:
1.68 nicm 348: if (fj->job != NULL)
349: job_free(fj->job);
350:
1.113 nicm 351: free((void *)fj->expanded);
1.78 nicm 352: free((void *)fj->cmd);
1.68 nicm 353: free(fj->out);
354:
355: free(fj);
356: }
1.131 nicm 357: }
358:
359: /* Remove old jobs for client. */
360: void
361: format_lost_client(struct client *c)
362: {
363: if (c->jobs != NULL)
364: format_job_tidy(c->jobs, 1);
365: free(c->jobs);
366: }
367:
368: /* Remove old jobs periodically. */
369: static void
370: format_job_timer(__unused int fd, __unused short events, __unused void *arg)
371: {
372: struct client *c;
373: struct timeval tv = { .tv_sec = 60 };
374:
375: format_job_tidy(&format_jobs, 0);
376: TAILQ_FOREACH(c, &clients, entry) {
377: if (c->jobs != NULL)
378: format_job_tidy(c->jobs, 0);
379: }
1.77 nicm 380:
381: evtimer_del(&format_job_event);
382: evtimer_add(&format_job_event, &tv);
1.68 nicm 383: }
384:
1.79 nicm 385: /* Callback for host. */
1.108 nicm 386: static void
1.99 nicm 387: format_cb_host(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 388: {
389: char host[HOST_NAME_MAX + 1];
390:
391: if (gethostname(host, sizeof host) != 0)
392: fe->value = xstrdup("");
393: else
394: fe->value = xstrdup(host);
395: }
396:
397: /* Callback for host_short. */
1.108 nicm 398: static void
1.99 nicm 399: format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 400: {
401: char host[HOST_NAME_MAX + 1], *cp;
402:
403: if (gethostname(host, sizeof host) != 0)
404: fe->value = xstrdup("");
405: else {
406: if ((cp = strchr(host, '.')) != NULL)
407: *cp = '\0';
408: fe->value = xstrdup(host);
409: }
410: }
411:
412: /* Callback for pid. */
1.108 nicm 413: static void
1.99 nicm 414: format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 415: {
416: xasprintf(&fe->value, "%ld", (long)getpid());
417: }
418:
1.80 nicm 419: /* Callback for session_alerts. */
1.108 nicm 420: static void
1.80 nicm 421: format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe)
422: {
423: struct session *s = ft->s;
424: struct winlink *wl;
1.133 nicm 425: char alerts[1024], tmp[16];
1.80 nicm 426:
427: if (s == NULL)
428: return;
429:
430: *alerts = '\0';
431: RB_FOREACH(wl, winlinks, &s->windows) {
432: if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
433: continue;
434: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
435:
436: if (*alerts != '\0')
437: strlcat(alerts, ",", sizeof alerts);
438: strlcat(alerts, tmp, sizeof alerts);
439: if (wl->flags & WINLINK_ACTIVITY)
440: strlcat(alerts, "#", sizeof alerts);
441: if (wl->flags & WINLINK_BELL)
442: strlcat(alerts, "!", sizeof alerts);
443: if (wl->flags & WINLINK_SILENCE)
444: strlcat(alerts, "~", sizeof alerts);
445: }
446: fe->value = xstrdup(alerts);
447: }
448:
1.133 nicm 449: /* Callback for session_stack. */
450: static void
451: format_cb_session_stack(struct format_tree *ft, struct format_entry *fe)
452: {
453: struct session *s = ft->s;
454: struct winlink *wl;
455: char result[1024], tmp[16];
456:
457: if (s == NULL)
458: return;
459:
460: xsnprintf(result, sizeof result, "%u", s->curw->idx);
461: TAILQ_FOREACH(wl, &s->lastw, sentry) {
462: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
463:
464: if (*result != '\0')
465: strlcat(result, ",", sizeof result);
466: strlcat(result, tmp, sizeof result);
467: }
468: fe->value = xstrdup(result);
469: }
470:
471: /* Callback for window_stack_index. */
472: static void
473: format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe)
474: {
475: struct session *s = ft->wl->session;
476: struct winlink *wl;
477: u_int idx;
478:
479: idx = 0;
480: TAILQ_FOREACH(wl, &s->lastw, sentry) {
481: idx++;
482: if (wl == ft->wl)
483: break;
484: }
485: if (wl != NULL)
486: xasprintf(&fe->value, "%u", idx);
487: else
488: fe->value = xstrdup("0");
489: }
490:
1.80 nicm 491: /* Callback for window_layout. */
1.108 nicm 492: static void
1.80 nicm 493: format_cb_window_layout(struct format_tree *ft, struct format_entry *fe)
494: {
495: struct window *w = ft->w;
496:
497: if (w == NULL)
498: return;
499:
500: if (w->saved_layout_root != NULL)
501: fe->value = layout_dump(w->saved_layout_root);
502: else
503: fe->value = layout_dump(w->layout_root);
504: }
505:
1.96 nicm 506: /* Callback for window_visible_layout. */
1.108 nicm 507: static void
1.96 nicm 508: format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe)
509: {
510: struct window *w = ft->w;
511:
512: if (w == NULL)
513: return;
514:
515: fe->value = layout_dump(w->layout_root);
516: }
517:
1.79 nicm 518: /* Callback for pane_start_command. */
1.108 nicm 519: static void
1.79 nicm 520: format_cb_start_command(struct format_tree *ft, struct format_entry *fe)
521: {
522: struct window_pane *wp = ft->wp;
523:
524: if (wp == NULL)
525: return;
526:
527: fe->value = cmd_stringify_argv(wp->argc, wp->argv);
528: }
529:
530: /* Callback for pane_current_command. */
1.108 nicm 531: static void
1.79 nicm 532: format_cb_current_command(struct format_tree *ft, struct format_entry *fe)
533: {
534: struct window_pane *wp = ft->wp;
535: char *cmd;
536:
537: if (wp == NULL)
538: return;
539:
540: cmd = get_proc_name(wp->fd, wp->tty);
541: if (cmd == NULL || *cmd == '\0') {
542: free(cmd);
543: cmd = cmd_stringify_argv(wp->argc, wp->argv);
544: if (cmd == NULL || *cmd == '\0') {
545: free(cmd);
546: cmd = xstrdup(wp->shell);
547: }
548: }
549: fe->value = parse_window_name(cmd);
550: free(cmd);
551: }
552:
1.80 nicm 553: /* Callback for history_bytes. */
1.108 nicm 554: static void
1.80 nicm 555: format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe)
556: {
557: struct window_pane *wp = ft->wp;
558: struct grid *gd;
559: struct grid_line *gl;
560: unsigned long long size;
561: u_int i;
562:
563: if (wp == NULL)
564: return;
565: gd = wp->base.grid;
566:
567: size = 0;
568: for (i = 0; i < gd->hsize; i++) {
1.158 nicm 569: gl = grid_get_line(gd, i);
1.80 nicm 570: size += gl->cellsize * sizeof *gl->celldata;
1.95 nicm 571: size += gl->extdsize * sizeof *gl->extddata;
1.80 nicm 572: }
1.158 nicm 573: size += gd->hsize * sizeof *gl;
1.80 nicm 574:
575: xasprintf(&fe->value, "%llu", size);
576: }
577:
578: /* Callback for pane_tabs. */
1.108 nicm 579: static void
1.80 nicm 580: format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe)
581: {
582: struct window_pane *wp = ft->wp;
583: struct evbuffer *buffer;
584: u_int i;
585: int size;
586:
587: if (wp == NULL)
588: return;
589:
590: buffer = evbuffer_new();
1.165 nicm 591: if (buffer == NULL)
592: fatalx("out of memory");
1.80 nicm 593: for (i = 0; i < wp->base.grid->sx; i++) {
594: if (!bit_test(wp->base.tabs, i))
595: continue;
596:
597: if (EVBUFFER_LENGTH(buffer) > 0)
598: evbuffer_add(buffer, ",", 1);
599: evbuffer_add_printf(buffer, "%u", i);
600: }
1.148 nicm 601: if ((size = EVBUFFER_LENGTH(buffer)) != 0)
602: xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
603: evbuffer_free(buffer);
604: }
605:
1.150 nicm 606: /* Callback for session_group_list. */
1.148 nicm 607: static void
1.150 nicm 608: format_cb_session_group_list(struct format_tree *ft, struct format_entry *fe)
1.148 nicm 609: {
610: struct session *s = ft->s;
611: struct session_group *sg;
612: struct session *loop;
613: struct evbuffer *buffer;
614: int size;
615:
616: if (s == NULL)
617: return;
618: sg = session_group_contains(s);
619: if (sg == NULL)
620: return;
621:
622: buffer = evbuffer_new();
1.165 nicm 623: if (buffer == NULL)
624: fatalx("out of memory");
1.148 nicm 625: TAILQ_FOREACH(loop, &sg->sessions, gentry) {
626: if (EVBUFFER_LENGTH(buffer) > 0)
627: evbuffer_add(buffer, ",", 1);
628: evbuffer_add_printf(buffer, "%s", loop->name);
629: }
630: if ((size = EVBUFFER_LENGTH(buffer)) != 0)
631: xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
1.80 nicm 632: evbuffer_free(buffer);
633: }
634:
1.168 nicm 635: /* Callback for pane_in_mode. */
636: static void
637: format_cb_pane_in_mode(struct format_tree *ft, struct format_entry *fe)
638: {
639: struct window_pane *wp = ft->wp;
640: u_int n = 0;
641: struct window_mode_entry *wme;
642:
643: if (wp == NULL)
644: return;
645:
646: TAILQ_FOREACH(wme, &wp->modes, entry)
647: n++;
648: xasprintf(&fe->value, "%u", n);
649: }
650:
1.112 nicm 651: /* Merge a format tree. */
652: static void
653: format_merge(struct format_tree *ft, struct format_tree *from)
654: {
655: struct format_entry *fe;
656:
657: RB_FOREACH(fe, format_entry_tree, &from->tree) {
658: if (fe->value != NULL)
659: format_add(ft, fe->key, "%s", fe->value);
660: }
661: }
662:
1.1 nicm 663: /* Create a new tree. */
664: struct format_tree *
1.131 nicm 665: format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
1.68 nicm 666: {
1.1 nicm 667: struct format_tree *ft;
1.77 nicm 668:
669: if (!event_initialized(&format_job_event)) {
670: evtimer_set(&format_job_event, format_job_timer, NULL);
671: format_job_timer(-1, 0, NULL);
672: }
1.1 nicm 673:
1.54 nicm 674: ft = xcalloc(1, sizeof *ft);
675: RB_INIT(&ft->tree);
1.120 nicm 676:
1.131 nicm 677: if (c != NULL) {
678: ft->client = c;
679: ft->client->references++;
680: }
1.172 nicm 681: ft->item = item;
1.131 nicm 682:
1.120 nicm 683: ft->tag = tag;
1.84 nicm 684: ft->flags = flags;
1.1 nicm 685:
1.79 nicm 686: format_add_cb(ft, "host", format_cb_host);
687: format_add_cb(ft, "host_short", format_cb_host_short);
688: format_add_cb(ft, "pid", format_cb_pid);
1.100 nicm 689: format_add(ft, "socket_path", "%s", socket_path);
690: format_add_tv(ft, "start_time", &start_time);
1.102 nicm 691:
1.130 nicm 692: if (item != NULL) {
693: if (item->cmd != NULL)
694: format_add(ft, "command", "%s", item->cmd->entry->name);
695: if (item->shared != NULL && item->shared->formats != NULL)
696: format_merge(ft, item->shared->formats);
697: }
1.1 nicm 698:
699: return (ft);
700: }
701:
702: /* Free a tree. */
703: void
704: format_free(struct format_tree *ft)
705: {
1.54 nicm 706: struct format_entry *fe, *fe1;
1.1 nicm 707:
1.68 nicm 708: RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
709: RB_REMOVE(format_entry_tree, &ft->tree, fe);
1.9 nicm 710: free(fe->value);
711: free(fe->key);
712: free(fe);
1.1 nicm 713: }
714:
1.131 nicm 715: if (ft->client != NULL)
716: server_client_unref(ft->client);
1.25 nicm 717: free(ft);
1.1 nicm 718: }
719:
720: /* Add a key-value pair. */
721: void
722: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
723: {
724: struct format_entry *fe;
1.28 nicm 725: struct format_entry *fe_now;
1.1 nicm 726: va_list ap;
727:
728: fe = xmalloc(sizeof *fe);
729: fe->key = xstrdup(key);
730:
1.79 nicm 731: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
732: if (fe_now != NULL) {
733: free(fe->key);
734: free(fe);
735: free(fe_now->value);
736: fe = fe_now;
737: }
738:
739: fe->cb = NULL;
1.87 nicm 740: fe->t = 0;
1.79 nicm 741:
1.1 nicm 742: va_start(ap, fmt);
743: xvasprintf(&fe->value, fmt, ap);
744: va_end(ap);
1.79 nicm 745: }
746:
1.87 nicm 747: /* Add a key and time. */
1.108 nicm 748: static void
1.87 nicm 749: format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
750: {
751: struct format_entry *fe;
752: struct format_entry *fe_now;
753:
754: fe = xmalloc(sizeof *fe);
755: fe->key = xstrdup(key);
756:
757: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
758: if (fe_now != NULL) {
759: free(fe->key);
760: free(fe);
761: free(fe_now->value);
762: fe = fe_now;
763: }
764:
765: fe->cb = NULL;
766: fe->t = tv->tv_sec;
767:
768: fe->value = NULL;
769: }
770:
1.79 nicm 771: /* Add a key and function. */
1.108 nicm 772: static void
1.79 nicm 773: format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
774: {
775: struct format_entry *fe;
776: struct format_entry *fe_now;
777:
778: fe = xmalloc(sizeof *fe);
779: fe->key = xstrdup(key);
1.1 nicm 780:
1.68 nicm 781: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1.28 nicm 782: if (fe_now != NULL) {
783: free(fe->key);
784: free(fe);
1.79 nicm 785: free(fe_now->value);
786: fe = fe_now;
1.28 nicm 787: }
1.79 nicm 788:
789: fe->cb = cb;
1.87 nicm 790: fe->t = 0;
1.79 nicm 791:
792: fe->value = NULL;
1.1 nicm 793: }
794:
1.161 nicm 795: /* Quote special characters in string. */
796: static char *
797: format_quote(const char *s)
798: {
799: const char *cp;
800: char *out, *at;
801:
802: at = out = xmalloc(strlen(s) * 2 + 1);
803: for (cp = s; *cp != '\0'; cp++) {
804: if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
805: *at++ = '\\';
806: *at++ = *cp;
807: }
808: *at = '\0';
809: return (out);
810: }
811:
1.1 nicm 812: /* Find a format entry. */
1.108 nicm 813: static char *
1.87 nicm 814: format_find(struct format_tree *ft, const char *key, int modifiers)
1.1 nicm 815: {
816: struct format_entry *fe, fe_find;
1.76 nicm 817: struct environ_entry *envent;
1.87 nicm 818: static char s[64];
1.117 nicm 819: struct options_entry *o;
1.87 nicm 820: const char *found;
1.116 nicm 821: int idx;
1.87 nicm 822: char *copy, *saved;
823:
824: if (~modifiers & FORMAT_TIMESTRING) {
1.116 nicm 825: o = options_parse_get(global_options, key, &idx, 0);
1.87 nicm 826: if (o == NULL && ft->w != NULL)
1.116 nicm 827: o = options_parse_get(ft->w->options, key, &idx, 0);
1.87 nicm 828: if (o == NULL)
1.116 nicm 829: o = options_parse_get(global_w_options, key, &idx, 0);
1.87 nicm 830: if (o == NULL && ft->s != NULL)
1.116 nicm 831: o = options_parse_get(ft->s->options, key, &idx, 0);
1.87 nicm 832: if (o == NULL)
1.116 nicm 833: o = options_parse_get(global_s_options, key, &idx, 0);
1.87 nicm 834: if (o != NULL) {
1.118 nicm 835: found = options_tostring(o, idx, 1);
1.116 nicm 836: goto found;
1.54 nicm 837: }
838: }
1.116 nicm 839: found = NULL;
1.1 nicm 840:
841: fe_find.key = (char *) key;
1.68 nicm 842: fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.79 nicm 843: if (fe != NULL) {
1.87 nicm 844: if (modifiers & FORMAT_TIMESTRING) {
845: if (fe->t == 0)
846: return (NULL);
847: ctime_r(&fe->t, s);
848: s[strcspn(s, "\n")] = '\0';
849: found = s;
850: goto found;
851: }
852: if (fe->t != 0) {
853: xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
854: found = s;
855: goto found;
856: }
1.149 nicm 857: if (fe->value == NULL && fe->cb != NULL) {
1.79 nicm 858: fe->cb(ft, fe);
1.149 nicm 859: if (fe->value == NULL)
860: fe->value = xstrdup("");
861: }
1.87 nicm 862: found = fe->value;
863: goto found;
1.79 nicm 864: }
1.76 nicm 865:
1.87 nicm 866: if (~modifiers & FORMAT_TIMESTRING) {
867: envent = NULL;
868: if (ft->s != NULL)
1.91 nicm 869: envent = environ_find(ft->s->environ, key);
1.87 nicm 870: if (envent == NULL)
1.91 nicm 871: envent = environ_find(global_environ, key);
1.87 nicm 872: if (envent != NULL) {
873: found = envent->value;
874: goto found;
875: }
876: }
1.76 nicm 877:
878: return (NULL);
1.87 nicm 879:
880: found:
1.88 nicm 881: if (found == NULL)
882: return (NULL);
1.87 nicm 883: copy = xstrdup(found);
884: if (modifiers & FORMAT_BASENAME) {
885: saved = copy;
886: copy = xstrdup(basename(saved));
887: free(saved);
888: }
889: if (modifiers & FORMAT_DIRNAME) {
890: saved = copy;
891: copy = xstrdup(dirname(saved));
892: free(saved);
893: }
1.161 nicm 894: if (modifiers & FORMAT_QUOTE) {
895: saved = copy;
896: copy = xstrdup(format_quote(saved));
897: free(saved);
898: }
1.87 nicm 899: return (copy);
1.1 nicm 900: }
901:
1.155 nicm 902: /* Skip until end. */
903: static const char *
1.169 nicm 904: format_skip(const char *s, const char *end)
1.114 nicm 905: {
906: int brackets = 0;
907:
908: for (; *s != '\0'; s++) {
1.155 nicm 909: if (*s == '#' && s[1] == '{')
1.114 nicm 910: brackets++;
1.155 nicm 911: if (*s == '#' && strchr(",#{}", s[1]) != NULL) {
912: s++;
913: continue;
914: }
1.114 nicm 915: if (*s == '}')
916: brackets--;
1.169 nicm 917: if (strchr(end, *s) != NULL && brackets == 0)
1.114 nicm 918: break;
919: }
920: if (*s == '\0')
921: return (NULL);
922: return (s);
923: }
924:
925: /* Return left and right alternatives separated by commas. */
926: static int
1.169 nicm 927: format_choose(struct format_tree *ft, const char *s, char **left, char **right,
928: int expand)
1.114 nicm 929: {
1.169 nicm 930: const char *cp;
931: char *left0, *right0;
1.114 nicm 932:
1.169 nicm 933: cp = format_skip(s, ",");
1.114 nicm 934: if (cp == NULL)
935: return (-1);
1.169 nicm 936: left0 = xstrndup(s, cp - s);
937: right0 = xstrdup(cp + 1);
1.114 nicm 938:
1.169 nicm 939: if (expand) {
940: *left = format_expand(ft, left0);
941: *right = format_expand(ft, right0);
942: } else {
943: *left = left0;
944: *right = right0;
945: }
1.114 nicm 946: return (0);
947: }
948:
949: /* Is this true? */
1.141 nicm 950: int
1.114 nicm 951: format_true(const char *s)
952: {
953: if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
954: return (1);
955: return (0);
956: }
957:
1.169 nicm 958: /* Check if modifier end. */
959: static int
960: format_is_end(char c)
961: {
962: return (c == ';' || c == ':');
963: }
964:
965: /* Add to modifier list. */
966: static void
967: format_add_modifier(struct format_modifier **list, u_int *count,
968: const char *c, size_t n, char **argv, int argc)
969: {
970: struct format_modifier *fm;
971:
972: *list = xreallocarray(*list, (*count) + 1, sizeof **list);
973: fm = &(*list)[(*count)++];
974:
975: memcpy(fm->modifier, c, n);
976: fm->modifier[n] = '\0';
977: fm->size = n;
978:
979: fm->argv = argv;
980: fm->argc = argc;
981: }
982:
983: /* Free modifier list. */
984: static void
985: format_free_modifiers(struct format_modifier *list, u_int count)
986: {
987: u_int i;
988:
989: for (i = 0; i < count; i++)
990: cmd_free_argv(list[i].argc, list[i].argv);
991: free(list);
992: }
993:
994: /* Build modifier list. */
995: static struct format_modifier *
996: format_build_modifiers(struct format_tree *ft, const char **s, u_int *count)
997: {
998: const char *cp = *s, *end;
999: struct format_modifier *list = NULL;
1000: char c, last[] = "X;:", **argv, *value;
1001: int argc;
1002:
1003: /*
1004: * Modifiers are a ; separated list of the forms:
1.175 ! nicm 1005: * l,m,C,b,d,t,q,E,T,S,W,P
1.169 nicm 1006: * =a
1007: * =/a
1008: * =/a/
1009: * s/a/b/
1010: * s/a/b
1011: * ||,&&,!=,==
1012: */
1013:
1014: *count = 0;
1015:
1016: while (*cp != '\0' && *cp != ':') {
1017: /* Skip and separator character. */
1018: if (*cp == ';')
1019: cp++;
1020:
1021: /* Check single character modifiers with no arguments. */
1.175 ! nicm 1022: if (strchr("lmCbdtqETSWP", cp[0]) != NULL &&
1.172 nicm 1023: format_is_end(cp[1])) {
1.169 nicm 1024: format_add_modifier(&list, count, cp, 1, NULL, 0);
1025: cp++;
1026: continue;
1027: }
1028:
1029: /* Then try double character with no arguments. */
1030: if ((memcmp("||", cp, 2) == 0 ||
1031: memcmp("&&", cp, 2) == 0 ||
1032: memcmp("!=", cp, 2) == 0 ||
1033: memcmp("==", cp, 2) == 0) &&
1034: format_is_end(cp[2])) {
1035: format_add_modifier(&list, count, cp, 2, NULL, 0);
1036: cp += 2;
1037: continue;
1038: }
1039:
1040: /* Now try single character with arguments. */
1041: if (strchr("s=", cp[0]) == NULL)
1042: break;
1043: c = cp[0];
1044:
1045: /* No arguments provided. */
1046: if (format_is_end(cp[1])) {
1047: format_add_modifier(&list, count, cp, 1, NULL, 0);
1048: cp++;
1049: continue;
1050: }
1051: argv = NULL;
1052: argc = 0;
1053:
1054: /* Single argument with no wrapper character. */
1055: if (!ispunct(cp[1]) || cp[1] == '-') {
1056: end = format_skip(cp + 1, ":;");
1057: if (end == NULL)
1058: break;
1059:
1060: argv = xcalloc(1, sizeof *argv);
1061: value = xstrndup(cp + 1, end - (cp + 1));
1062: argv[0] = format_expand(ft, value);
1063: free(value);
1064: argc = 1;
1065:
1066: format_add_modifier(&list, count, &c, 1, argv, argc);
1067: cp = end;
1068: continue;
1069: }
1070:
1071: /* Multiple arguments with a wrapper character. */
1072: last[0] = cp[1];
1073: cp++;
1074: do {
1075: if (cp[0] == last[0] && format_is_end(cp[1])) {
1076: cp++;
1077: break;
1078: }
1079: end = format_skip(cp + 1, last);
1080: if (end == NULL)
1081: break;
1082: cp++;
1083:
1084: argv = xreallocarray (argv, argc + 1, sizeof *argv);
1085: value = xstrndup(cp, end - cp);
1086: argv[argc++] = format_expand(ft, value);
1087: free(value);
1088:
1089: cp = end;
1090: } while (!format_is_end(cp[0]));
1091: format_add_modifier(&list, count, &c, 1, argv, argc);
1092: }
1093: if (*cp != ':') {
1094: format_free_modifiers(list, *count);
1095: *count = 0;
1096: return (NULL);
1097: }
1098: *s = cp + 1;
1099: return list;
1100: }
1101:
1102: /* Perform substitution in string. */
1103: static char *
1104: format_substitute(const char *source, const char *from, const char *to)
1105: {
1106: char *copy, *new;
1107: const char *cp;
1108: size_t fromlen, tolen, newlen, used;
1109:
1110: fromlen = strlen(from);
1111: tolen = strlen(to);
1112:
1113: newlen = strlen(source) + 1;
1114: copy = new = xmalloc(newlen);
1115:
1116: for (cp = source; *cp != '\0'; /* nothing */) {
1117: if (strncmp(cp, from, fromlen) != 0) {
1118: *new++ = *cp++;
1119: continue;
1120: }
1121: used = new - copy;
1122:
1123: newlen += tolen;
1124: copy = xrealloc(copy, newlen);
1125:
1126: new = copy + used;
1127: memcpy(new, to, tolen);
1128:
1129: new += tolen;
1130: cp += fromlen;
1131: }
1132:
1133: *new = '\0';
1134: return (copy);
1135: }
1136:
1.172 nicm 1137: /* Loop over sessions. */
1138: static char *
1139: format_loop_sessions(struct format_tree *ft, const char *fmt)
1140: {
1141: struct cmdq_item *item = ft->item;
1142: char *expanded, *value;
1143: size_t valuelen;
1144: struct session *s;
1145:
1146: value = xcalloc(1, 1);
1147: valuelen = 1;
1148:
1149: RB_FOREACH(s, sessions, &sessions) {
1150: expanded = format_single(item, fmt, ft->c, ft->s, NULL, NULL);
1151:
1152: valuelen += strlen(expanded);
1153: value = xrealloc(value, valuelen);
1154:
1155: strlcat(value, expanded, valuelen);
1156: free(expanded);
1157: }
1158:
1159: return (value);
1160: }
1161:
1162: /* Loop over windows. */
1163: static char *
1164: format_loop_windows(struct format_tree *ft, const char *fmt)
1165: {
1166: struct cmdq_item *item = ft->item;
1167: char *all, *active, *use, *expanded, *value;
1168: size_t valuelen;
1169: struct winlink *wl;
1170:
1171: if (ft->s == NULL)
1172: return (NULL);
1173:
1174: if (format_choose(ft, fmt, &all, &active, 0) != 0) {
1175: all = xstrdup(fmt);
1176: active = NULL;
1177: }
1178:
1179: value = xcalloc(1, 1);
1180: valuelen = 1;
1181:
1182: RB_FOREACH(wl, winlinks, &ft->s->windows) {
1183: if (active != NULL && wl == ft->s->curw)
1184: use = active;
1185: else
1186: use = all;
1187: expanded = format_single(item, use, ft->c, ft->s, wl, NULL);
1188:
1189: valuelen += strlen(expanded);
1190: value = xrealloc(value, valuelen);
1191:
1192: strlcat(value, expanded, valuelen);
1193: free(expanded);
1194: }
1195:
1196: free(active);
1197: free(all);
1198:
1199: return (value);
1200: }
1201:
1202: /* Loop over panes. */
1203: static char *
1204: format_loop_panes(struct format_tree *ft, const char *fmt)
1205: {
1206: struct cmdq_item *item = ft->item;
1207: char *all, *active, *use, *expanded, *value;
1208: size_t valuelen;
1209: struct window_pane *wp;
1210:
1211: if (ft->w == NULL)
1212: return (NULL);
1213:
1214: if (format_choose(ft, fmt, &all, &active, 0) != 0) {
1215: all = xstrdup(fmt);
1216: active = NULL;
1217: }
1218:
1219: value = xcalloc(1, 1);
1220: valuelen = 1;
1221:
1222: TAILQ_FOREACH(wp, &ft->w->panes, entry) {
1223: if (active != NULL && wp == ft->w->active)
1224: use = active;
1225: else
1226: use = all;
1227: expanded = format_single(item, use, ft->c, ft->s, ft->wl, wp);
1228:
1229: valuelen += strlen(expanded);
1230: value = xrealloc(value, valuelen);
1231:
1232: strlcat(value, expanded, valuelen);
1233: free(expanded);
1234: }
1235:
1236: free(active);
1237: free(all);
1238:
1239: return (value);
1240: }
1241:
1.140 nicm 1242: /* Replace a key. */
1.108 nicm 1243: static int
1.30 nicm 1244: format_replace(struct format_tree *ft, const char *key, size_t keylen,
1245: char **buf, size_t *len, size_t *off)
1.1 nicm 1246: {
1.140 nicm 1247: struct window_pane *wp = ft->wp;
1.169 nicm 1248: const char *errptr, *copy, *cp;
1249: char *copy0, *condition, *found, *new;
1250: char *value, *left, *right;
1251: char tmp[64];
1252: size_t valuelen;
1253: int modifiers = 0, limit = 0;
1254: struct format_modifier *list, *fm, *cmp = NULL, *search = NULL;
1255: struct format_modifier *sub = NULL;
1256: u_int i, count;
1.1 nicm 1257:
1258: /* Make a copy of the key. */
1.169 nicm 1259: copy = copy0 = xstrndup(key, keylen);
1.173 nicm 1260: log_debug("%s: format = '%s'", __func__, copy);
1.169 nicm 1261:
1262: /* Process modifier list. */
1263: list = format_build_modifiers(ft, ©, &count);
1264: for (i = 0; i < count; i++) {
1265: xsnprintf(tmp, sizeof tmp, "%s: modifier %u", __func__, i);
1266: log_debug("%s = %s", tmp, list[i].modifier);
1267: cmd_log_argv(list[i].argc, list[i].argv, tmp);
1268:
1269: fm = &list[i];
1270: if (fm->size == 1) {
1271: switch (fm->modifier[0]) {
1272: case 'm':
1273: cmp = fm;
1274: break;
1275: case 'C':
1276: search = fm;
1277: break;
1278: case 's':
1279: if (fm->argc != 2)
1280: break;
1281: sub = fm;
1282: break;
1283: case '=':
1284: if (fm->argc != 1)
1285: break;
1286: limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
1287: &errptr);
1288: if (errptr != NULL)
1289: limit = 0;
1290: break;
1291: case 'l':
1292: modifiers |= FORMAT_LITERAL;
1293: break;
1294: case 'b':
1295: modifiers |= FORMAT_BASENAME;
1296: break;
1297: case 'd':
1298: modifiers |= FORMAT_DIRNAME;
1299: break;
1300: case 't':
1301: modifiers |= FORMAT_TIMESTRING;
1302: break;
1303: case 'q':
1304: modifiers |= FORMAT_QUOTE;
1305: break;
1.170 nicm 1306: case 'E':
1307: modifiers |= FORMAT_EXPAND;
1308: break;
1.175 ! nicm 1309: case 'T':
! 1310: modifiers |= FORMAT_EXPANDTIME;
! 1311: break;
1.172 nicm 1312: case 'S':
1313: modifiers |= FORMAT_SESSIONS;
1314: break;
1315: case 'W':
1316: modifiers |= FORMAT_WINDOWS;
1317: break;
1318: case 'P':
1319: modifiers |= FORMAT_PANES;
1320: break;
1.169 nicm 1321: }
1322: } else if (fm->size == 2) {
1323: if (strcmp(fm->modifier, "||") == 0 ||
1324: strcmp(fm->modifier, "&&") == 0 ||
1325: strcmp(fm->modifier, "==") == 0 ||
1326: strcmp(fm->modifier, "!=") == 0)
1327: cmp = fm;
1.98 nicm 1328: }
1.30 nicm 1329: }
1330:
1.155 nicm 1331: /* Is this a literal string? */
1.169 nicm 1332: if (modifiers & FORMAT_LITERAL) {
1.155 nicm 1333: value = xstrdup(copy);
1334: goto done;
1335: }
1336:
1.172 nicm 1337: /* Is this a loop, comparison or condition? */
1338: if (modifiers & FORMAT_SESSIONS) {
1339: value = format_loop_sessions(ft, copy);
1340: if (value == NULL)
1341: goto fail;
1342: } else if (modifiers & FORMAT_WINDOWS) {
1343: value = format_loop_windows(ft, copy);
1344: if (value == NULL)
1345: goto fail;
1346: } else if (modifiers & FORMAT_PANES) {
1347: value = format_loop_panes(ft, copy);
1348: if (value == NULL)
1349: goto fail;
1350: } else if (search != NULL) {
1.140 nicm 1351: /* Search in pane. */
1352: if (wp == NULL)
1353: value = xstrdup("0");
1354: else
1355: xasprintf(&value, "%u", window_pane_search(wp, copy));
1.169 nicm 1356: } else if (cmp != NULL) {
1357: /* Comparison of left and right. */
1358: if (format_choose(ft, copy, &left, &right, 1) != 0)
1.114 nicm 1359: goto fail;
1.169 nicm 1360:
1361: if (strcmp(cmp->modifier, "||") == 0) {
1362: if (format_true(left) || format_true(right))
1363: value = xstrdup("1");
1364: else
1365: value = xstrdup("0");
1366: } else if (strcmp(cmp->modifier, "&&") == 0) {
1367: if (format_true(left) && format_true(right))
1368: value = xstrdup("1");
1369: else
1370: value = xstrdup("0");
1371: } else if (strcmp(cmp->modifier, "==") == 0) {
1372: if (strcmp(left, right) == 0)
1373: value = xstrdup("1");
1374: else
1375: value = xstrdup("0");
1376: } else if (strcmp(cmp->modifier, "!=") == 0) {
1377: if (strcmp(left, right) != 0)
1378: value = xstrdup("1");
1379: else
1380: value = xstrdup("0");
1381: }
1382: else if (strcmp(cmp->modifier, "m") == 0) {
1383: if (fnmatch(left, right, 0) == 0)
1384: value = xstrdup("1");
1385: else
1386: value = xstrdup("0");
1387: }
1388:
1.114 nicm 1389: free(right);
1390: free(left);
1391: } else if (*copy == '?') {
1392: /* Conditional: check first and choose second or third. */
1.169 nicm 1393: cp = format_skip(copy + 1, ",");
1394: if (cp == NULL)
1.1 nicm 1395: goto fail;
1.169 nicm 1396: condition = xstrndup(copy + 1, cp - (copy + 1));
1.1 nicm 1397:
1.169 nicm 1398: found = format_find(ft, condition, modifiers);
1.156 nicm 1399: if (found == NULL) {
1400: /*
1.169 nicm 1401: * If the condition not found, try to expand it. If
1.156 nicm 1402: * the expansion doesn't have any effect, then assume
1403: * false.
1404: */
1.169 nicm 1405: found = format_expand(ft, condition);
1406: if (strcmp(found, condition) == 0) {
1.156 nicm 1407: free(found);
1408: found = xstrdup("");
1409: }
1410: }
1.169 nicm 1411: free(condition);
1412:
1413: if (format_choose(ft, cp + 1, &left, &right, 0) != 0) {
1.162 nicm 1414: free(found);
1.89 nicm 1415: goto fail;
1.162 nicm 1416: }
1.114 nicm 1417: if (format_true(found))
1418: value = format_expand(ft, left);
1419: else
1420: value = format_expand(ft, right);
1.169 nicm 1421: free(right);
1422: free(left);
1423:
1.98 nicm 1424: free(found);
1.1 nicm 1425: } else {
1.114 nicm 1426: /* Neither: look up directly. */
1.98 nicm 1427: value = format_find(ft, copy, modifiers);
1.1 nicm 1428: if (value == NULL)
1.98 nicm 1429: value = xstrdup("");
1.170 nicm 1430: }
1431:
1.171 nicm 1432: done:
1.170 nicm 1433: /* Expand again if required. */
1434: if (modifiers & FORMAT_EXPAND) {
1435: new = format_expand(ft, value);
1.175 ! nicm 1436: free(value);
! 1437: value = new;
! 1438: }
! 1439: else if (modifiers & FORMAT_EXPANDTIME) {
! 1440: new = format_expand_time(ft, value, 0);
1.170 nicm 1441: free(value);
1442: value = new;
1.98 nicm 1443: }
1444:
1445: /* Perform substitution if any. */
1.169 nicm 1446: if (sub != NULL) {
1447: new = format_substitute(value, sub->argv[0], sub->argv[1]);
1.98 nicm 1448: free(value);
1.169 nicm 1449: value = new;
1.1 nicm 1450: }
1451:
1.30 nicm 1452: /* Truncate the value if needed. */
1.105 nicm 1453: if (limit > 0) {
1.98 nicm 1454: new = utf8_trimcstr(value, limit);
1.105 nicm 1455: free(value);
1456: value = new;
1457: } else if (limit < 0) {
1458: new = utf8_rtrimcstr(value, -limit);
1.98 nicm 1459: free(value);
1460: value = new;
1.44 nicm 1461: }
1.30 nicm 1462:
1.1 nicm 1463: /* Expand the buffer and copy in the value. */
1.173 nicm 1464: log_debug("%s: '%s' -> '%s'", __func__, copy0, value);
1.98 nicm 1465: valuelen = strlen(value);
1.1 nicm 1466: while (*len - *off < valuelen + 1) {
1.50 nicm 1467: *buf = xreallocarray(*buf, 2, *len);
1.1 nicm 1468: *len *= 2;
1469: }
1470: memcpy(*buf + *off, value, valuelen);
1471: *off += valuelen;
1472:
1.98 nicm 1473: free(value);
1.169 nicm 1474: format_free_modifiers(list, count);
1.30 nicm 1475: free(copy0);
1.1 nicm 1476: return (0);
1477:
1478: fail:
1.169 nicm 1479: format_free_modifiers(list, count);
1.30 nicm 1480: free(copy0);
1.1 nicm 1481: return (-1);
1482: }
1483:
1.58 nicm 1484: /* Expand keys in a template, passing through strftime first. */
1485: char *
1486: format_expand_time(struct format_tree *ft, const char *fmt, time_t t)
1487: {
1488: struct tm *tm;
1.107 nicm 1489: char s[2048];
1.58 nicm 1490:
1.67 nicm 1491: if (fmt == NULL || *fmt == '\0')
1.58 nicm 1492: return (xstrdup(""));
1493:
1.174 nicm 1494: if (t == 0)
1495: t = time(NULL);
1.58 nicm 1496: tm = localtime(&t);
1497:
1.107 nicm 1498: if (strftime(s, sizeof s, fmt, tm) == 0)
1499: return (xstrdup(""));
1.58 nicm 1500:
1.107 nicm 1501: return (format_expand(ft, s));
1.58 nicm 1502: }
1503:
1.1 nicm 1504: /* Expand keys in a template. */
1505: char *
1506: format_expand(struct format_tree *ft, const char *fmt)
1507: {
1.152 nicm 1508: char *buf, *out, *name;
1.79 nicm 1509: const char *ptr, *s, *saved = fmt;
1.86 nicm 1510: size_t off, len, n, outlen;
1.31 nicm 1511: int ch, brackets;
1.58 nicm 1512:
1513: if (fmt == NULL)
1514: return (xstrdup(""));
1.1 nicm 1515:
1516: len = 64;
1517: buf = xmalloc(len);
1518: off = 0;
1519:
1520: while (*fmt != '\0') {
1521: if (*fmt != '#') {
1522: while (len - off < 2) {
1.50 nicm 1523: buf = xreallocarray(buf, 2, len);
1.1 nicm 1524: len *= 2;
1525: }
1526: buf[off++] = *fmt++;
1527: continue;
1528: }
1529: fmt++;
1530:
1531: ch = (u_char) *fmt++;
1532: switch (ch) {
1.68 nicm 1533: case '(':
1534: brackets = 1;
1535: for (ptr = fmt; *ptr != '\0'; ptr++) {
1536: if (*ptr == '(')
1537: brackets++;
1538: if (*ptr == ')' && --brackets == 0)
1539: break;
1540: }
1541: if (*ptr != ')' || brackets != 0)
1542: break;
1543: n = ptr - fmt;
1544:
1.114 nicm 1545: if (ft->flags & FORMAT_NOJOBS)
1546: out = xstrdup("");
1.152 nicm 1547: else {
1548: name = xstrndup(fmt, n);
1549: out = format_job_get(ft, name);
1550: free(name);
1551: }
1.86 nicm 1552: outlen = strlen(out);
1.68 nicm 1553:
1.86 nicm 1554: while (len - off < outlen + 1) {
1.68 nicm 1555: buf = xreallocarray(buf, 2, len);
1556: len *= 2;
1557: }
1.86 nicm 1558: memcpy(buf + off, out, outlen);
1559: off += outlen;
1560:
1561: free(out);
1.68 nicm 1562:
1563: fmt += n + 1;
1564: continue;
1.1 nicm 1565: case '{':
1.169 nicm 1566: ptr = format_skip((char *)fmt - 2, "}");
1.155 nicm 1567: if (ptr == NULL)
1.1 nicm 1568: break;
1569: n = ptr - fmt;
1570:
1571: if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
1572: break;
1573: fmt += n + 1;
1.40 nicm 1574: continue;
1.155 nicm 1575: case '}':
1.40 nicm 1576: case '#':
1.155 nicm 1577: case ',':
1.40 nicm 1578: while (len - off < 2) {
1.50 nicm 1579: buf = xreallocarray(buf, 2, len);
1.40 nicm 1580: len *= 2;
1581: }
1.155 nicm 1582: buf[off++] = ch;
1.1 nicm 1583: continue;
1584: default:
1.25 nicm 1585: s = NULL;
1586: if (ch >= 'A' && ch <= 'Z')
1587: s = format_upper[ch - 'A'];
1588: else if (ch >= 'a' && ch <= 'z')
1589: s = format_lower[ch - 'a'];
1590: if (s == NULL) {
1591: while (len - off < 3) {
1.50 nicm 1592: buf = xreallocarray(buf, 2, len);
1.25 nicm 1593: len *= 2;
1.1 nicm 1594: }
1.25 nicm 1595: buf[off++] = '#';
1596: buf[off++] = ch;
1597: continue;
1.1 nicm 1598: }
1.25 nicm 1599: n = strlen(s);
1600: if (format_replace(ft, s, n, &buf, &len, &off) != 0)
1601: break;
1.1 nicm 1602: continue;
1603: }
1604:
1605: break;
1606: }
1607: buf[off] = '\0';
1608:
1.169 nicm 1609: log_debug("%s: '%s' -> '%s'", __func__, saved, buf);
1.1 nicm 1610: return (buf);
1.123 nicm 1611: }
1612:
1613: /* Expand a single string. */
1614: char *
1615: format_single(struct cmdq_item *item, const char *fmt, struct client *c,
1616: struct session *s, struct winlink *wl, struct window_pane *wp)
1617: {
1618: struct format_tree *ft;
1619: char *expanded;
1620:
1.131 nicm 1621: if (item != NULL)
1622: ft = format_create(item->client, item, FORMAT_NONE, 0);
1623: else
1624: ft = format_create(NULL, item, FORMAT_NONE, 0);
1.123 nicm 1625: format_defaults(ft, c, s, wl, wp);
1626:
1627: expanded = format_expand(ft, fmt);
1628: format_free(ft);
1629: return (expanded);
1.1 nicm 1630: }
1631:
1.57 nicm 1632: /* Set defaults for any of arguments that are not NULL. */
1633: void
1634: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
1635: struct winlink *wl, struct window_pane *wp)
1636: {
1.154 nicm 1637: if (c != NULL && s != NULL && c->session != s)
1638: log_debug("%s: session does not match", __func__);
1639:
1.146 nicm 1640: format_add(ft, "session_format", "%d", s != NULL);
1641: format_add(ft, "window_format", "%d", wl != NULL);
1642: format_add(ft, "pane_format", "%d", wp != NULL);
1643:
1.57 nicm 1644: if (s == NULL && c != NULL)
1645: s = c->session;
1646: if (wl == NULL && s != NULL)
1647: wl = s->curw;
1648: if (wp == NULL && wl != NULL)
1649: wp = wl->window->active;
1650:
1651: if (c != NULL)
1652: format_defaults_client(ft, c);
1653: if (s != NULL)
1654: format_defaults_session(ft, s);
1.129 nicm 1655: if (wl != NULL)
1656: format_defaults_winlink(ft, wl);
1.57 nicm 1657: if (wp != NULL)
1658: format_defaults_pane(ft, wp);
1659: }
1660:
1.1 nicm 1661: /* Set default format keys for a session. */
1.108 nicm 1662: static void
1.57 nicm 1663: format_defaults_session(struct format_tree *ft, struct session *s)
1.1 nicm 1664: {
1665: struct session_group *sg;
1666:
1.54 nicm 1667: ft->s = s;
1668:
1.1 nicm 1669: format_add(ft, "session_name", "%s", s->name);
1670: format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
1.23 nicm 1671: format_add(ft, "session_id", "$%u", s->id);
1.1 nicm 1672:
1.122 nicm 1673: sg = session_group_contains(s);
1.1 nicm 1674: format_add(ft, "session_grouped", "%d", sg != NULL);
1.148 nicm 1675: if (sg != NULL) {
1.122 nicm 1676: format_add(ft, "session_group", "%s", sg->name);
1.148 nicm 1677: format_add(ft, "session_group_size", "%u",
1678: session_group_count (sg));
1.150 nicm 1679: format_add_cb(ft, "session_group_list",
1680: format_cb_session_group_list);
1.148 nicm 1681: }
1.1 nicm 1682:
1.87 nicm 1683: format_add_tv(ft, "session_created", &s->creation_time);
1684: format_add_tv(ft, "session_last_attached", &s->last_attached_time);
1685: format_add_tv(ft, "session_activity", &s->activity_time);
1.1 nicm 1686:
1.41 nicm 1687: format_add(ft, "session_attached", "%u", s->attached);
1.59 nicm 1688: format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66 nicm 1689:
1.80 nicm 1690: format_add_cb(ft, "session_alerts", format_cb_session_alerts);
1.133 nicm 1691: format_add_cb(ft, "session_stack", format_cb_session_stack);
1.3 nicm 1692: }
1693:
1694: /* Set default format keys for a client. */
1.108 nicm 1695: static void
1.57 nicm 1696: format_defaults_client(struct format_tree *ft, struct client *c)
1.3 nicm 1697: {
1.60 nicm 1698: struct session *s;
1.103 nicm 1699: const char *name;
1.115 nicm 1700: struct tty *tty = &c->tty;
1701: const char *types[] = TTY_TYPES;
1.3 nicm 1702:
1.54 nicm 1703: if (ft->s == NULL)
1704: ft->s = c->session;
1.164 nicm 1705: ft->c = c;
1.54 nicm 1706:
1.124 nicm 1707: format_add(ft, "client_name", "%s", c->name);
1.72 nicm 1708: format_add(ft, "client_pid", "%ld", (long) c->pid);
1.115 nicm 1709: format_add(ft, "client_height", "%u", tty->sy);
1710: format_add(ft, "client_width", "%u", tty->sx);
1.124 nicm 1711: format_add(ft, "client_tty", "%s", c->ttyname);
1.75 nicm 1712: format_add(ft, "client_control_mode", "%d",
1713: !!(c->flags & CLIENT_CONTROL));
1.3 nicm 1714:
1.115 nicm 1715: if (tty->term_name != NULL)
1716: format_add(ft, "client_termname", "%s", tty->term_name);
1717: if (tty->term_name != NULL)
1718: format_add(ft, "client_termtype", "%s", types[tty->term_type]);
1719:
1.87 nicm 1720: format_add_tv(ft, "client_created", &c->creation_time);
1721: format_add_tv(ft, "client_activity", &c->activity_time);
1.126 nicm 1722:
1723: format_add(ft, "client_written", "%zu", c->written);
1724: format_add(ft, "client_discarded", "%zu", c->discarded);
1.14 nicm 1725:
1.103 nicm 1726: name = server_client_get_key_table(c);
1727: if (strcmp(c->keytable->name, name) == 0)
1.61 nicm 1728: format_add(ft, "client_prefix", "%d", 0);
1729: else
1730: format_add(ft, "client_prefix", "%d", 1);
1731: format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3 nicm 1732:
1.115 nicm 1733: if (tty->flags & TTY_UTF8)
1.3 nicm 1734: format_add(ft, "client_utf8", "%d", 1);
1735: else
1736: format_add(ft, "client_utf8", "%d", 0);
1737:
1738: if (c->flags & CLIENT_READONLY)
1739: format_add(ft, "client_readonly", "%d", 1);
1740: else
1741: format_add(ft, "client_readonly", "%d", 0);
1.15 nicm 1742:
1743: s = c->session;
1744: if (s != NULL)
1745: format_add(ft, "client_session", "%s", s->name);
1746: s = c->last_session;
1747: if (s != NULL && session_alive(s))
1748: format_add(ft, "client_last_session", "%s", s->name);
1.1 nicm 1749: }
1750:
1.32 nicm 1751: /* Set default format keys for a window. */
1752: void
1.57 nicm 1753: format_defaults_window(struct format_tree *ft, struct window *w)
1.32 nicm 1754: {
1.54 nicm 1755: ft->w = w;
1756:
1.87 nicm 1757: format_add_tv(ft, "window_activity", &w->activity_time);
1.32 nicm 1758: format_add(ft, "window_id", "@%u", w->id);
1759: format_add(ft, "window_name", "%s", w->name);
1760: format_add(ft, "window_width", "%u", w->sx);
1761: format_add(ft, "window_height", "%u", w->sy);
1.80 nicm 1762: format_add_cb(ft, "window_layout", format_cb_window_layout);
1.96 nicm 1763: format_add_cb(ft, "window_visible_layout",
1764: format_cb_window_visible_layout);
1.32 nicm 1765: format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59 nicm 1766: format_add(ft, "window_zoomed_flag", "%d",
1.53 nicm 1767: !!(w->flags & WINDOW_ZOOMED));
1.32 nicm 1768: }
1769:
1.1 nicm 1770: /* Set default format keys for a winlink. */
1.108 nicm 1771: static void
1.129 nicm 1772: format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
1.1 nicm 1773: {
1.164 nicm 1774: struct client *c = ft->c;
1.129 nicm 1775: struct session *s = wl->session;
1.1 nicm 1776: struct window *w = wl->window;
1.164 nicm 1777: int flag;
1778: u_int ox, oy, sx, sy;
1.1 nicm 1779:
1.54 nicm 1780: if (ft->w == NULL)
1781: ft->w = wl->window;
1.133 nicm 1782: ft->wl = wl;
1.54 nicm 1783:
1.57 nicm 1784: format_defaults_window(ft, w);
1.32 nicm 1785:
1.164 nicm 1786: if (c != NULL) {
1787: flag = tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
1788: format_add(ft, "window_bigger", "%d", flag);
1789: if (flag) {
1790: format_add(ft, "window_offset_x", "%u", ox);
1791: format_add(ft, "window_offset_y", "%u", oy);
1792: }
1793: }
1794:
1.1 nicm 1795: format_add(ft, "window_index", "%d", wl->idx);
1.133 nicm 1796: format_add_cb(ft, "window_stack_index", format_cb_window_stack_index);
1.129 nicm 1797: format_add(ft, "window_flags", "%s", window_printable_flags(wl));
1.1 nicm 1798: format_add(ft, "window_active", "%d", wl == s->curw);
1.29 nicm 1799:
1.59 nicm 1800: format_add(ft, "window_bell_flag", "%d",
1.29 nicm 1801: !!(wl->flags & WINLINK_BELL));
1.59 nicm 1802: format_add(ft, "window_activity_flag", "%d",
1.29 nicm 1803: !!(wl->flags & WINLINK_ACTIVITY));
1.59 nicm 1804: format_add(ft, "window_silence_flag", "%d",
1.29 nicm 1805: !!(wl->flags & WINLINK_SILENCE));
1.59 nicm 1806: format_add(ft, "window_last_flag", "%d",
1.49 nicm 1807: !!(wl == TAILQ_FIRST(&s->lastw)));
1.64 nicm 1808: format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.1 nicm 1809: }
1810:
1811: /* Set default format keys for a window pane. */
1812: void
1.57 nicm 1813: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1 nicm 1814: {
1.168 nicm 1815: struct window *w = wp->window;
1816: struct grid *gd = wp->base.grid;
1817: int status = wp->status;
1818: u_int idx;
1819: struct window_mode_entry *wme;
1.54 nicm 1820:
1821: if (ft->w == NULL)
1.159 nicm 1822: ft->w = w;
1.79 nicm 1823: ft->wp = wp;
1.1 nicm 1824:
1.16 nicm 1825: format_add(ft, "history_size", "%u", gd->hsize);
1826: format_add(ft, "history_limit", "%u", gd->hlimit);
1.80 nicm 1827: format_add_cb(ft, "history_bytes", format_cb_history_bytes);
1.1 nicm 1828:
1.4 nicm 1829: if (window_pane_index(wp, &idx) != 0)
1830: fatalx("index not found");
1.16 nicm 1831: format_add(ft, "pane_index", "%u", idx);
1.4 nicm 1832:
1.1 nicm 1833: format_add(ft, "pane_width", "%u", wp->sx);
1834: format_add(ft, "pane_height", "%u", wp->sy);
1835: format_add(ft, "pane_title", "%s", wp->base.title);
1836: format_add(ft, "pane_id", "%%%u", wp->id);
1.159 nicm 1837: format_add(ft, "pane_active", "%d", wp == w->active);
1.55 nicm 1838: format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
1.143 nicm 1839: format_add(ft, "pane_pipe", "%d", wp->pipe_fd != -1);
1.55 nicm 1840:
1.147 nicm 1841: if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(status))
1.55 nicm 1842: format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1 nicm 1843: format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47 nicm 1844:
1.164 nicm 1845: format_add(ft, "pane_left", "%u", wp->xoff);
1846: format_add(ft, "pane_top", "%u", wp->yoff);
1847: format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
1848: format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
1849: format_add(ft, "pane_at_left", "%d", wp->xoff == 0);
1850: format_add(ft, "pane_at_top", "%d", wp->yoff == 0);
1851: format_add(ft, "pane_at_right", "%d", wp->xoff + wp->sx == w->sx);
1852: format_add(ft, "pane_at_bottom", "%d", wp->yoff + wp->sy == w->sy);
1.16 nicm 1853:
1.168 nicm 1854: wme = TAILQ_FIRST(&wp->modes);
1855: if (wme != NULL) {
1856: format_add(ft, "pane_mode", "%s", wme->mode->name);
1857: if (wme->mode->formats != NULL)
1858: wme->mode->formats(wme, ft);
1859: }
1860: format_add_cb(ft, "pane_in_mode", format_cb_pane_in_mode);
1.134 nicm 1861:
1.26 nicm 1862: format_add(ft, "pane_synchronized", "%d",
1.159 nicm 1863: !!options_get_number(w->options, "synchronize-panes"));
1.135 nicm 1864: if (wp->searchstr != NULL)
1865: format_add(ft, "pane_search_string", "%s", wp->searchstr);
1.16 nicm 1866:
1.71 nicm 1867: format_add(ft, "pane_tty", "%s", wp->tty);
1.16 nicm 1868: format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.79 nicm 1869: format_add_cb(ft, "pane_start_command", format_cb_start_command);
1870: format_add_cb(ft, "pane_current_command", format_cb_current_command);
1.16 nicm 1871:
1.59 nicm 1872: format_add(ft, "cursor_x", "%u", wp->base.cx);
1873: format_add(ft, "cursor_y", "%u", wp->base.cy);
1874: format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
1875: format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.16 nicm 1876:
1877: format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59 nicm 1878: format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
1879: format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16 nicm 1880:
1881: format_add(ft, "cursor_flag", "%d",
1882: !!(wp->base.mode & MODE_CURSOR));
1883: format_add(ft, "insert_flag", "%d",
1884: !!(wp->base.mode & MODE_INSERT));
1885: format_add(ft, "keypad_cursor_flag", "%d",
1886: !!(wp->base.mode & MODE_KCURSOR));
1887: format_add(ft, "keypad_flag", "%d",
1888: !!(wp->base.mode & MODE_KKEYPAD));
1889: format_add(ft, "wrap_flag", "%d",
1890: !!(wp->base.mode & MODE_WRAP));
1891:
1.62 nicm 1892: format_add(ft, "mouse_any_flag", "%d",
1.119 nicm 1893: !!(wp->base.mode & ALL_MOUSE_MODES));
1.16 nicm 1894: format_add(ft, "mouse_standard_flag", "%d",
1895: !!(wp->base.mode & MODE_MOUSE_STANDARD));
1896: format_add(ft, "mouse_button_flag", "%d",
1897: !!(wp->base.mode & MODE_MOUSE_BUTTON));
1.119 nicm 1898: format_add(ft, "mouse_all_flag", "%d",
1899: !!(wp->base.mode & MODE_MOUSE_ALL));
1.19 nicm 1900:
1.80 nicm 1901: format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
1.8 nicm 1902: }
1903:
1.19 nicm 1904: /* Set default format keys for paste buffer. */
1.8 nicm 1905: void
1.94 nicm 1906: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
1.8 nicm 1907: {
1.146 nicm 1908: struct timeval tv;
1909: size_t size;
1910: char *s;
1911:
1912: timerclear(&tv);
1913: tv.tv_sec = paste_buffer_created(pb);
1914: paste_buffer_data(pb, &size);
1.8 nicm 1915:
1.146 nicm 1916: format_add(ft, "buffer_size", "%zu", size);
1.81 nicm 1917: format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
1.146 nicm 1918: format_add_tv(ft, "buffer_created", &tv);
1.8 nicm 1919:
1.94 nicm 1920: s = paste_make_sample(pb);
1.42 nicm 1921: format_add(ft, "buffer_sample", "%s", s);
1922: free(s);
1.1 nicm 1923: }