Annotation of src/usr.bin/tmux/format.c, Revision 1.220
1.220 ! nicm 1: /* $OpenBSD: format.c,v 1.219 2019/11/28 09:45:15 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.202 nicm 26: #include <regex.h>
1.1 nicm 27: #include <stdarg.h>
1.9 nicm 28: #include <stdlib.h>
1.1 nicm 29: #include <string.h>
30: #include <time.h>
31: #include <unistd.h>
32:
33: #include "tmux.h"
34:
35: /*
36: * Build a list of key-value pairs and use them to expand #{key} entries in a
37: * string.
38: */
39:
1.79 nicm 40: struct format_entry;
41: typedef void (*format_cb)(struct format_tree *, struct format_entry *);
42:
1.108 nicm 43: static char *format_job_get(struct format_tree *, const char *);
44: static void format_job_timer(int, short, void *);
45:
46: static char *format_find(struct format_tree *, const char *, int);
47: static void format_add_cb(struct format_tree *, const char *, format_cb);
48: static void format_add_tv(struct format_tree *, const char *,
49: struct timeval *);
50: static int format_replace(struct format_tree *, const char *, size_t,
51: char **, size_t *, size_t *);
52:
53: static void format_defaults_session(struct format_tree *,
54: struct session *);
55: static void format_defaults_client(struct format_tree *, struct client *);
1.164 nicm 56: static void format_defaults_winlink(struct format_tree *, struct winlink *);
1.1 nicm 57:
1.68 nicm 58: /* Entry in format job tree. */
59: struct format_job {
1.131 nicm 60: struct client *client;
1.120 nicm 61: u_int tag;
1.68 nicm 62: const char *cmd;
1.113 nicm 63: const char *expanded;
1.68 nicm 64:
65: time_t last;
66: char *out;
1.127 nicm 67: int updated;
1.68 nicm 68:
69: struct job *job;
70: int status;
71:
72: RB_ENTRY(format_job) entry;
73: };
74:
75: /* Format job tree. */
1.108 nicm 76: static struct event format_job_event;
77: static int format_job_cmp(struct format_job *, struct format_job *);
1.109 nicm 78: static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
1.108 nicm 79: RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp);
1.68 nicm 80:
81: /* Format job tree comparison function. */
1.108 nicm 82: static int
1.68 nicm 83: format_job_cmp(struct format_job *fj1, struct format_job *fj2)
84: {
1.120 nicm 85: if (fj1->tag < fj2->tag)
86: return (-1);
87: if (fj1->tag > fj2->tag)
88: return (1);
1.68 nicm 89: return (strcmp(fj1->cmd, fj2->cmd));
90: }
91:
1.87 nicm 92: /* Format modifiers. */
93: #define FORMAT_TIMESTRING 0x1
94: #define FORMAT_BASENAME 0x2
95: #define FORMAT_DIRNAME 0x4
1.169 nicm 96: #define FORMAT_QUOTE 0x8
97: #define FORMAT_LITERAL 0x10
1.170 nicm 98: #define FORMAT_EXPAND 0x20
1.175 nicm 99: #define FORMAT_EXPANDTIME 0x40
100: #define FORMAT_SESSIONS 0x80
101: #define FORMAT_WINDOWS 0x100
102: #define FORMAT_PANES 0x200
1.87 nicm 103:
1.178 nicm 104: /* Limit on recursion. */
105: #define FORMAT_LOOP_LIMIT 10
106:
1.54 nicm 107: /* Entry in format tree. */
108: struct format_entry {
1.79 nicm 109: char *key;
110: char *value;
1.87 nicm 111: time_t t;
1.79 nicm 112: format_cb cb;
113: RB_ENTRY(format_entry) entry;
1.54 nicm 114: };
115:
1.68 nicm 116: /* Format entry tree. */
1.54 nicm 117: struct format_tree {
1.164 nicm 118: struct client *c;
119: struct session *s;
120: struct winlink *wl;
1.79 nicm 121: struct window *w;
122: struct window_pane *wp;
1.54 nicm 123:
1.172 nicm 124: struct cmdq_item *item;
1.131 nicm 125: struct client *client;
1.120 nicm 126: u_int tag;
1.84 nicm 127: int flags;
1.177 nicm 128: time_t time;
1.178 nicm 129: u_int loop;
1.68 nicm 130:
1.197 nicm 131: struct mouse_event m;
132:
1.68 nicm 133: RB_HEAD(format_entry_tree, format_entry) tree;
1.54 nicm 134: };
1.108 nicm 135: static int format_entry_cmp(struct format_entry *, struct format_entry *);
136: RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
1.54 nicm 137:
1.180 nicm 138: /* Format modifier. */
1.169 nicm 139: struct format_modifier {
140: char modifier[3];
141: u_int size;
142:
143: char **argv;
144: int argc;
145: };
146:
1.68 nicm 147: /* Format entry tree comparison function. */
1.108 nicm 148: static int
1.68 nicm 149: format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
1.1 nicm 150: {
151: return (strcmp(fe1->key, fe2->key));
152: }
153:
1.25 nicm 154: /* Single-character uppercase aliases. */
1.108 nicm 155: static const char *format_upper[] = {
1.1 nicm 156: NULL, /* A */
157: NULL, /* B */
158: NULL, /* C */
159: "pane_id", /* D */
160: NULL, /* E */
161: "window_flags", /* F */
162: NULL, /* G */
163: "host", /* H */
164: "window_index", /* I */
165: NULL, /* J */
166: NULL, /* K */
167: NULL, /* L */
168: NULL, /* M */
169: NULL, /* N */
170: NULL, /* O */
171: "pane_index", /* P */
172: NULL, /* Q */
173: NULL, /* R */
174: "session_name", /* S */
175: "pane_title", /* T */
176: NULL, /* U */
177: NULL, /* V */
178: "window_name", /* W */
179: NULL, /* X */
180: NULL, /* Y */
181: NULL /* Z */
182: };
183:
1.25 nicm 184: /* Single-character lowercase aliases. */
1.108 nicm 185: static const char *format_lower[] = {
1.25 nicm 186: NULL, /* a */
187: NULL, /* b */
188: NULL, /* c */
189: NULL, /* d */
190: NULL, /* e */
191: NULL, /* f */
192: NULL, /* g */
193: "host_short", /* h */
194: NULL, /* i */
195: NULL, /* j */
196: NULL, /* k */
197: NULL, /* l */
198: NULL, /* m */
199: NULL, /* n */
200: NULL, /* o */
201: NULL, /* p */
202: NULL, /* q */
203: NULL, /* r */
204: NULL, /* s */
205: NULL, /* t */
206: NULL, /* u */
207: NULL, /* v */
208: NULL, /* w */
209: NULL, /* x */
210: NULL, /* y */
211: NULL /* z */
212: };
213:
1.179 nicm 214: /* Is logging enabled? */
215: static inline int
216: format_logging(struct format_tree *ft)
217: {
218: return (log_get_level() != 0 || (ft->flags & FORMAT_VERBOSE));
219: }
220:
221: /* Log a message if verbose. */
222: static void printflike(3, 4)
223: format_log1(struct format_tree *ft, const char *from, const char *fmt, ...)
224: {
225: va_list ap;
226: char *s;
227: static const char spaces[] = " ";
228:
229: if (!format_logging(ft))
230: return;
231:
232: va_start(ap, fmt);
233: vasprintf(&s, fmt, ap);
234: va_end(ap);
235:
236: log_debug("%s: %s", from, s);
1.181 nicm 237: if (ft->item != NULL && (ft->flags & FORMAT_VERBOSE))
1.179 nicm 238: cmdq_print(ft->item, "#%.*s%s", ft->loop, spaces, s);
239:
240: free(s);
241: }
242: #define format_log(ft, fmt, ...) format_log1(ft, __func__, fmt, ##__VA_ARGS__)
243:
1.127 nicm 244: /* Format job update callback. */
1.108 nicm 245: static void
1.127 nicm 246: format_job_update(struct job *job)
247: {
1.160 nicm 248: struct format_job *fj = job_get_data(job);
249: struct evbuffer *evb = job_get_event(job)->input;
1.151 nicm 250: char *line = NULL, *next;
1.127 nicm 251: time_t t;
252:
1.151 nicm 253: while ((next = evbuffer_readline(evb)) != NULL) {
254: free(line);
255: line = next;
256: }
257: if (line == NULL)
1.127 nicm 258: return;
259: fj->updated = 1;
260:
261: free(fj->out);
262: fj->out = line;
263:
1.136 nicm 264: log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out);
1.127 nicm 265:
1.142 nicm 266: t = time(NULL);
1.127 nicm 267: if (fj->status && fj->last != t) {
1.136 nicm 268: if (fj->client != NULL)
269: server_status_client(fj->client);
1.127 nicm 270: fj->last = t;
271: }
272: }
273:
274: /* Format job complete callback. */
275: static void
276: format_job_complete(struct job *job)
1.68 nicm 277: {
1.160 nicm 278: struct format_job *fj = job_get_data(job);
279: struct evbuffer *evb = job_get_event(job)->input;
1.68 nicm 280: char *line, *buf;
281: size_t len;
282:
283: fj->job = NULL;
284:
285: buf = NULL;
1.160 nicm 286: if ((line = evbuffer_readline(evb)) == NULL) {
287: len = EVBUFFER_LENGTH(evb);
1.68 nicm 288: buf = xmalloc(len + 1);
289: if (len != 0)
1.160 nicm 290: memcpy(buf, EVBUFFER_DATA(evb), len);
1.68 nicm 291: buf[len] = '\0';
292: } else
293: buf = line;
1.127 nicm 294:
1.136 nicm 295: log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf);
296:
1.127 nicm 297: if (*buf != '\0' || !fj->updated) {
298: free(fj->out);
299: fj->out = buf;
300: } else
301: free(buf);
1.68 nicm 302:
303: if (fj->status) {
1.131 nicm 304: if (fj->client != NULL)
305: server_status_client(fj->client);
1.68 nicm 306: fj->status = 0;
307: }
308: }
309:
310: /* Find a job. */
1.108 nicm 311: static char *
1.68 nicm 312: format_job_get(struct format_tree *ft, const char *cmd)
313: {
1.131 nicm 314: struct format_job_tree *jobs;
1.113 nicm 315: struct format_job fj0, *fj;
316: time_t t;
317: char *expanded;
318: int force;
1.68 nicm 319:
1.131 nicm 320: if (ft->client == NULL)
321: jobs = &format_jobs;
322: else if (ft->client->jobs != NULL)
323: jobs = ft->client->jobs;
324: else {
325: jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
326: RB_INIT(jobs);
327: }
328:
1.120 nicm 329: fj0.tag = ft->tag;
1.68 nicm 330: fj0.cmd = cmd;
1.131 nicm 331: if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
1.68 nicm 332: fj = xcalloc(1, sizeof *fj);
1.131 nicm 333: fj->client = ft->client;
1.120 nicm 334: fj->tag = ft->tag;
1.68 nicm 335: fj->cmd = xstrdup(cmd);
1.113 nicm 336: fj->expanded = NULL;
1.68 nicm 337:
338: xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
339:
1.131 nicm 340: RB_INSERT(format_job_tree, jobs, fj);
1.68 nicm 341: }
342:
1.113 nicm 343: expanded = format_expand(ft, cmd);
344: if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
345: free((void *)fj->expanded);
346: fj->expanded = xstrdup(expanded);
347: force = 1;
348: } else
349: force = (ft->flags & FORMAT_FORCE);
350:
1.84 nicm 351: t = time(NULL);
1.183 nicm 352: if (force && fj->job != NULL)
353: job_free(fj->job);
354: if (force || (fj->job == NULL && fj->last != t)) {
1.163 nicm 355: fj->job = job_run(expanded, NULL,
356: server_client_get_cwd(ft->client, NULL), format_job_update,
1.153 nicm 357: format_job_complete, NULL, fj, JOB_NOWAIT);
1.68 nicm 358: if (fj->job == NULL) {
359: free(fj->out);
360: xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
361: }
1.84 nicm 362: fj->last = t;
1.137 nicm 363: fj->updated = 0;
1.68 nicm 364: }
1.84 nicm 365:
366: if (ft->flags & FORMAT_STATUS)
367: fj->status = 1;
1.68 nicm 368:
1.113 nicm 369: free(expanded);
1.86 nicm 370: return (format_expand(ft, fj->out));
1.68 nicm 371: }
372:
373: /* Remove old jobs. */
1.108 nicm 374: static void
1.131 nicm 375: format_job_tidy(struct format_job_tree *jobs, int force)
1.68 nicm 376: {
377: struct format_job *fj, *fj1;
378: time_t now;
379:
380: now = time(NULL);
1.131 nicm 381: RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
382: if (!force && (fj->last > now || now - fj->last < 3600))
1.68 nicm 383: continue;
1.131 nicm 384: RB_REMOVE(format_job_tree, jobs, fj);
1.68 nicm 385:
1.77 nicm 386: log_debug("%s: %s", __func__, fj->cmd);
387:
1.68 nicm 388: if (fj->job != NULL)
389: job_free(fj->job);
390:
1.113 nicm 391: free((void *)fj->expanded);
1.78 nicm 392: free((void *)fj->cmd);
1.68 nicm 393: free(fj->out);
394:
395: free(fj);
396: }
1.131 nicm 397: }
398:
399: /* Remove old jobs for client. */
400: void
401: format_lost_client(struct client *c)
402: {
403: if (c->jobs != NULL)
404: format_job_tidy(c->jobs, 1);
405: free(c->jobs);
406: }
407:
408: /* Remove old jobs periodically. */
409: static void
410: format_job_timer(__unused int fd, __unused short events, __unused void *arg)
411: {
412: struct client *c;
413: struct timeval tv = { .tv_sec = 60 };
414:
415: format_job_tidy(&format_jobs, 0);
416: TAILQ_FOREACH(c, &clients, entry) {
417: if (c->jobs != NULL)
418: format_job_tidy(c->jobs, 0);
419: }
1.77 nicm 420:
421: evtimer_del(&format_job_event);
422: evtimer_add(&format_job_event, &tv);
1.68 nicm 423: }
424:
1.79 nicm 425: /* Callback for host. */
1.108 nicm 426: static void
1.99 nicm 427: format_cb_host(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 428: {
429: char host[HOST_NAME_MAX + 1];
430:
431: if (gethostname(host, sizeof host) != 0)
432: fe->value = xstrdup("");
433: else
434: fe->value = xstrdup(host);
435: }
436:
437: /* Callback for host_short. */
1.108 nicm 438: static void
1.99 nicm 439: format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 440: {
441: char host[HOST_NAME_MAX + 1], *cp;
442:
443: if (gethostname(host, sizeof host) != 0)
444: fe->value = xstrdup("");
445: else {
446: if ((cp = strchr(host, '.')) != NULL)
447: *cp = '\0';
448: fe->value = xstrdup(host);
449: }
450: }
451:
452: /* Callback for pid. */
1.108 nicm 453: static void
1.99 nicm 454: format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 455: {
456: xasprintf(&fe->value, "%ld", (long)getpid());
457: }
458:
1.80 nicm 459: /* Callback for session_alerts. */
1.108 nicm 460: static void
1.80 nicm 461: format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe)
462: {
463: struct session *s = ft->s;
464: struct winlink *wl;
1.133 nicm 465: char alerts[1024], tmp[16];
1.80 nicm 466:
467: if (s == NULL)
468: return;
469:
470: *alerts = '\0';
471: RB_FOREACH(wl, winlinks, &s->windows) {
472: if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
473: continue;
474: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
475:
476: if (*alerts != '\0')
477: strlcat(alerts, ",", sizeof alerts);
478: strlcat(alerts, tmp, sizeof alerts);
479: if (wl->flags & WINLINK_ACTIVITY)
480: strlcat(alerts, "#", sizeof alerts);
481: if (wl->flags & WINLINK_BELL)
482: strlcat(alerts, "!", sizeof alerts);
483: if (wl->flags & WINLINK_SILENCE)
484: strlcat(alerts, "~", sizeof alerts);
485: }
486: fe->value = xstrdup(alerts);
487: }
488:
1.133 nicm 489: /* Callback for session_stack. */
490: static void
491: format_cb_session_stack(struct format_tree *ft, struct format_entry *fe)
492: {
493: struct session *s = ft->s;
494: struct winlink *wl;
495: char result[1024], tmp[16];
496:
497: if (s == NULL)
498: return;
499:
500: xsnprintf(result, sizeof result, "%u", s->curw->idx);
501: TAILQ_FOREACH(wl, &s->lastw, sentry) {
502: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
503:
504: if (*result != '\0')
505: strlcat(result, ",", sizeof result);
506: strlcat(result, tmp, sizeof result);
507: }
508: fe->value = xstrdup(result);
509: }
510:
511: /* Callback for window_stack_index. */
512: static void
513: format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe)
514: {
515: struct session *s = ft->wl->session;
516: struct winlink *wl;
517: u_int idx;
518:
519: idx = 0;
520: TAILQ_FOREACH(wl, &s->lastw, sentry) {
521: idx++;
522: if (wl == ft->wl)
523: break;
524: }
525: if (wl != NULL)
526: xasprintf(&fe->value, "%u", idx);
527: else
528: fe->value = xstrdup("0");
529: }
530:
1.80 nicm 531: /* Callback for window_layout. */
1.108 nicm 532: static void
1.80 nicm 533: format_cb_window_layout(struct format_tree *ft, struct format_entry *fe)
534: {
535: struct window *w = ft->w;
536:
537: if (w == NULL)
538: return;
539:
540: if (w->saved_layout_root != NULL)
541: fe->value = layout_dump(w->saved_layout_root);
542: else
543: fe->value = layout_dump(w->layout_root);
544: }
545:
1.96 nicm 546: /* Callback for window_visible_layout. */
1.108 nicm 547: static void
1.96 nicm 548: format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe)
549: {
550: struct window *w = ft->w;
551:
552: if (w == NULL)
553: return;
554:
555: fe->value = layout_dump(w->layout_root);
556: }
557:
1.79 nicm 558: /* Callback for pane_start_command. */
1.108 nicm 559: static void
1.79 nicm 560: format_cb_start_command(struct format_tree *ft, struct format_entry *fe)
561: {
562: struct window_pane *wp = ft->wp;
563:
564: if (wp == NULL)
565: return;
566:
567: fe->value = cmd_stringify_argv(wp->argc, wp->argv);
568: }
569:
570: /* Callback for pane_current_command. */
1.108 nicm 571: static void
1.79 nicm 572: format_cb_current_command(struct format_tree *ft, struct format_entry *fe)
573: {
574: struct window_pane *wp = ft->wp;
575: char *cmd;
576:
1.212 nicm 577: if (wp == NULL || wp->shell == NULL)
1.79 nicm 578: return;
579:
580: cmd = get_proc_name(wp->fd, wp->tty);
581: if (cmd == NULL || *cmd == '\0') {
582: free(cmd);
583: cmd = cmd_stringify_argv(wp->argc, wp->argv);
584: if (cmd == NULL || *cmd == '\0') {
585: free(cmd);
586: cmd = xstrdup(wp->shell);
587: }
588: }
589: fe->value = parse_window_name(cmd);
590: free(cmd);
591: }
592:
1.80 nicm 593: /* Callback for history_bytes. */
1.108 nicm 594: static void
1.80 nicm 595: format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe)
596: {
597: struct window_pane *wp = ft->wp;
598: struct grid *gd;
599: struct grid_line *gl;
600: unsigned long long size;
601: u_int i;
602:
603: if (wp == NULL)
604: return;
605: gd = wp->base.grid;
606:
607: size = 0;
608: for (i = 0; i < gd->hsize; i++) {
1.158 nicm 609: gl = grid_get_line(gd, i);
1.80 nicm 610: size += gl->cellsize * sizeof *gl->celldata;
1.95 nicm 611: size += gl->extdsize * sizeof *gl->extddata;
1.80 nicm 612: }
1.158 nicm 613: size += gd->hsize * sizeof *gl;
1.80 nicm 614:
615: xasprintf(&fe->value, "%llu", size);
616: }
617:
618: /* Callback for pane_tabs. */
1.108 nicm 619: static void
1.80 nicm 620: format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe)
621: {
622: struct window_pane *wp = ft->wp;
623: struct evbuffer *buffer;
624: u_int i;
625: int size;
626:
627: if (wp == NULL)
628: return;
629:
630: buffer = evbuffer_new();
1.165 nicm 631: if (buffer == NULL)
632: fatalx("out of memory");
1.80 nicm 633: for (i = 0; i < wp->base.grid->sx; i++) {
634: if (!bit_test(wp->base.tabs, i))
635: continue;
636:
637: if (EVBUFFER_LENGTH(buffer) > 0)
638: evbuffer_add(buffer, ",", 1);
639: evbuffer_add_printf(buffer, "%u", i);
640: }
1.148 nicm 641: if ((size = EVBUFFER_LENGTH(buffer)) != 0)
642: xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
643: evbuffer_free(buffer);
644: }
645:
1.150 nicm 646: /* Callback for session_group_list. */
1.148 nicm 647: static void
1.150 nicm 648: format_cb_session_group_list(struct format_tree *ft, struct format_entry *fe)
1.148 nicm 649: {
650: struct session *s = ft->s;
651: struct session_group *sg;
652: struct session *loop;
653: struct evbuffer *buffer;
654: int size;
655:
656: if (s == NULL)
657: return;
658: sg = session_group_contains(s);
659: if (sg == NULL)
660: return;
661:
662: buffer = evbuffer_new();
1.165 nicm 663: if (buffer == NULL)
664: fatalx("out of memory");
1.148 nicm 665: TAILQ_FOREACH(loop, &sg->sessions, gentry) {
666: if (EVBUFFER_LENGTH(buffer) > 0)
667: evbuffer_add(buffer, ",", 1);
668: evbuffer_add_printf(buffer, "%s", loop->name);
669: }
670: if ((size = EVBUFFER_LENGTH(buffer)) != 0)
671: xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
1.80 nicm 672: evbuffer_free(buffer);
673: }
674:
1.168 nicm 675: /* Callback for pane_in_mode. */
676: static void
677: format_cb_pane_in_mode(struct format_tree *ft, struct format_entry *fe)
678: {
679: struct window_pane *wp = ft->wp;
680: u_int n = 0;
681: struct window_mode_entry *wme;
682:
683: if (wp == NULL)
684: return;
685:
686: TAILQ_FOREACH(wme, &wp->modes, entry)
687: n++;
688: xasprintf(&fe->value, "%u", n);
689: }
690:
1.186 nicm 691: /* Callback for cursor_character. */
692: static void
693: format_cb_cursor_character(struct format_tree *ft, struct format_entry *fe)
694: {
695: struct window_pane *wp = ft->wp;
696: struct grid_cell gc;
697:
698: if (wp == NULL)
699: return;
700:
701: grid_view_get_cell(wp->base.grid, wp->base.cx, wp->base.cy, &gc);
702: if (~gc.flags & GRID_FLAG_PADDING)
703: xasprintf(&fe->value, "%.*s", (int)gc.data.size, gc.data.data);
704: }
705:
1.213 nicm 706: /* Return word at given coordinates. Caller frees. */
707: char *
708: format_grid_word(struct grid *gd, u_int x, u_int y)
1.197 nicm 709: {
710: struct grid_line *gl;
711: struct grid_cell gc;
712: const char *ws;
713: struct utf8_data *ud = NULL;
1.213 nicm 714: u_int end;
1.197 nicm 715: size_t size = 0;
716: int found = 0;
1.213 nicm 717: char *s = NULL;
1.197 nicm 718:
719: ws = options_get_string(global_s_options, "word-separators");
720:
721: y = gd->hsize + y;
722: for (;;) {
723: grid_get_cell(gd, x, y, &gc);
724: if (gc.flags & GRID_FLAG_PADDING)
725: break;
726: if (utf8_cstrhas(ws, &gc.data)) {
727: found = 1;
728: break;
729: }
730:
731: if (x == 0) {
732: if (y == 0)
733: break;
734: gl = &gd->linedata[y - 1];
735: if (~gl->flags & GRID_LINE_WRAPPED)
736: break;
737: y--;
738: x = grid_line_length(gd, y);
739: if (x == 0)
740: break;
741: }
742: x--;
743: }
744: for (;;) {
745: if (found) {
746: end = grid_line_length(gd, y);
747: if (end == 0 || x == end - 1) {
748: if (y == gd->hsize + gd->sy - 1)
749: break;
750: gl = &gd->linedata[y];
751: if (~gl->flags & GRID_LINE_WRAPPED)
752: break;
753: y++;
754: x = 0;
755: } else
756: x++;
757: }
758: found = 1;
759:
760: grid_get_cell(gd, x, y, &gc);
761: if (gc.flags & GRID_FLAG_PADDING)
762: break;
763: if (utf8_cstrhas(ws, &gc.data))
764: break;
765:
766: ud = xreallocarray(ud, size + 2, sizeof *ud);
767: memcpy(&ud[size++], &gc.data, sizeof *ud);
768: }
769: if (size != 0) {
770: ud[size].size = 0;
1.213 nicm 771: s = utf8_tocstr(ud);
1.197 nicm 772: free(ud);
773: }
1.213 nicm 774: return (s);
1.197 nicm 775: }
776:
1.213 nicm 777: /* Callback for mouse_word. */
1.197 nicm 778: static void
1.213 nicm 779: format_cb_mouse_word(struct format_tree *ft, struct format_entry *fe)
1.197 nicm 780: {
781: struct window_pane *wp;
782: u_int x, y;
1.213 nicm 783: char *s;
1.197 nicm 784:
785: if (!ft->m.valid)
786: return;
787: wp = cmd_mouse_pane(&ft->m, NULL, NULL);
788: if (wp == NULL)
1.211 nicm 789: return;
790: if (!TAILQ_EMPTY (&wp->modes))
1.197 nicm 791: return;
792: if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
793: return;
1.213 nicm 794:
795: s = format_grid_word(wp->base.grid, x, y);
796: if (s != NULL)
797: fe->value = s;
798: }
799:
800: /* Return line at given coordinates. Caller frees. */
801: char *
802: format_grid_line(struct grid *gd, u_int y)
803: {
804: struct grid_cell gc;
805: struct utf8_data *ud = NULL;
806: u_int x;
807: size_t size = 0;
808: char *s = NULL;
1.197 nicm 809:
810: y = gd->hsize + y;
811: for (x = 0; x < grid_line_length(gd, y); x++) {
812: grid_get_cell(gd, x, y, &gc);
813: if (gc.flags & GRID_FLAG_PADDING)
814: break;
815:
816: ud = xreallocarray(ud, size + 2, sizeof *ud);
817: memcpy(&ud[size++], &gc.data, sizeof *ud);
818: }
819: if (size != 0) {
820: ud[size].size = 0;
1.213 nicm 821: s = utf8_tocstr(ud);
1.197 nicm 822: free(ud);
823: }
1.213 nicm 824: return (s);
825: }
826:
827: /* Callback for mouse_line. */
828: static void
829: format_cb_mouse_line(struct format_tree *ft, struct format_entry *fe)
830: {
831: struct window_pane *wp;
832: u_int x, y;
833: char *s;
834:
835: if (!ft->m.valid)
836: return;
837: wp = cmd_mouse_pane(&ft->m, NULL, NULL);
838: if (wp == NULL)
839: return;
840: if (!TAILQ_EMPTY (&wp->modes))
841: return;
842: if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
843: return;
844:
845: s = format_grid_line(wp->base.grid, y);
846: if (s != NULL)
847: fe->value = s;
1.197 nicm 848: }
849:
1.112 nicm 850: /* Merge a format tree. */
851: static void
852: format_merge(struct format_tree *ft, struct format_tree *from)
853: {
854: struct format_entry *fe;
855:
856: RB_FOREACH(fe, format_entry_tree, &from->tree) {
857: if (fe->value != NULL)
858: format_add(ft, fe->key, "%s", fe->value);
859: }
860: }
861:
1.196 nicm 862: /* Add item bits to tree. */
863: static void
864: format_create_add_item(struct format_tree *ft, struct cmdq_item *item)
865: {
1.197 nicm 866: struct mouse_event *m;
867: struct window_pane *wp;
868: u_int x, y;
869:
1.196 nicm 870: if (item->cmd != NULL)
871: format_add(ft, "command", "%s", item->cmd->entry->name);
1.197 nicm 872:
873: if (item->shared == NULL)
874: return;
875: if (item->shared->formats != NULL)
1.196 nicm 876: format_merge(ft, item->shared->formats);
1.197 nicm 877:
878: m = &item->shared->mouse;
879: if (m->valid && ((wp = cmd_mouse_pane(m, NULL, NULL)) != NULL)) {
880: format_add(ft, "mouse_pane", "%%%u", wp->id);
881: if (cmd_mouse_at(wp, m, &x, &y, 0) == 0) {
882: format_add(ft, "mouse_x", "%u", x);
883: format_add(ft, "mouse_y", "%u", y);
884: format_add_cb(ft, "mouse_word", format_cb_mouse_word);
885: format_add_cb(ft, "mouse_line", format_cb_mouse_line);
886: }
887: }
888: memcpy(&ft->m, m, sizeof ft->m);
1.196 nicm 889: }
890:
1.1 nicm 891: /* Create a new tree. */
892: struct format_tree *
1.131 nicm 893: format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
1.68 nicm 894: {
1.184 nicm 895: struct format_tree *ft;
896: const struct window_mode **wm;
897: char tmp[64];
1.77 nicm 898:
899: if (!event_initialized(&format_job_event)) {
900: evtimer_set(&format_job_event, format_job_timer, NULL);
901: format_job_timer(-1, 0, NULL);
902: }
1.1 nicm 903:
1.54 nicm 904: ft = xcalloc(1, sizeof *ft);
905: RB_INIT(&ft->tree);
1.120 nicm 906:
1.131 nicm 907: if (c != NULL) {
908: ft->client = c;
909: ft->client->references++;
910: }
1.172 nicm 911: ft->item = item;
1.131 nicm 912:
1.120 nicm 913: ft->tag = tag;
1.84 nicm 914: ft->flags = flags;
1.177 nicm 915: ft->time = time(NULL);
1.1 nicm 916:
1.79 nicm 917: format_add_cb(ft, "host", format_cb_host);
918: format_add_cb(ft, "host_short", format_cb_host_short);
919: format_add_cb(ft, "pid", format_cb_pid);
1.100 nicm 920: format_add(ft, "socket_path", "%s", socket_path);
921: format_add_tv(ft, "start_time", &start_time);
1.102 nicm 922:
1.184 nicm 923: for (wm = all_window_modes; *wm != NULL; wm++) {
924: if ((*wm)->default_format != NULL) {
925: xsnprintf(tmp, sizeof tmp, "%s_format", (*wm)->name);
926: tmp[strcspn(tmp, "-")] = '_';
927: format_add(ft, tmp, "%s", (*wm)->default_format);
928: }
929: }
930:
1.196 nicm 931: if (item != NULL)
932: format_create_add_item(ft, item);
1.1 nicm 933:
934: return (ft);
935: }
936:
937: /* Free a tree. */
938: void
939: format_free(struct format_tree *ft)
940: {
1.54 nicm 941: struct format_entry *fe, *fe1;
1.1 nicm 942:
1.68 nicm 943: RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
944: RB_REMOVE(format_entry_tree, &ft->tree, fe);
1.9 nicm 945: free(fe->value);
946: free(fe->key);
947: free(fe);
1.1 nicm 948: }
949:
1.131 nicm 950: if (ft->client != NULL)
951: server_client_unref(ft->client);
1.25 nicm 952: free(ft);
1.1 nicm 953: }
1.184 nicm 954:
955: /* Walk each format. */
956: void
957: format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
958: void *), void *arg)
959: {
960: struct format_entry *fe;
961: static char s[64];
962:
963: RB_FOREACH(fe, format_entry_tree, &ft->tree) {
964: if (fe->t != 0) {
965: xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
966: cb(fe->key, fe->value, s);
967: } else {
968: if (fe->value == NULL && fe->cb != NULL) {
969: fe->cb(ft, fe);
970: if (fe->value == NULL)
971: fe->value = xstrdup("");
972: }
973: cb(fe->key, fe->value, arg);
974: }
975: }
976: }
1.1 nicm 977:
978: /* Add a key-value pair. */
979: void
980: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
981: {
982: struct format_entry *fe;
1.28 nicm 983: struct format_entry *fe_now;
1.1 nicm 984: va_list ap;
985:
986: fe = xmalloc(sizeof *fe);
987: fe->key = xstrdup(key);
988:
1.79 nicm 989: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
990: if (fe_now != NULL) {
991: free(fe->key);
992: free(fe);
993: free(fe_now->value);
994: fe = fe_now;
995: }
996:
997: fe->cb = NULL;
1.87 nicm 998: fe->t = 0;
1.79 nicm 999:
1.1 nicm 1000: va_start(ap, fmt);
1001: xvasprintf(&fe->value, fmt, ap);
1002: va_end(ap);
1.79 nicm 1003: }
1004:
1.87 nicm 1005: /* Add a key and time. */
1.108 nicm 1006: static void
1.87 nicm 1007: format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
1008: {
1009: struct format_entry *fe;
1010: struct format_entry *fe_now;
1011:
1012: fe = xmalloc(sizeof *fe);
1013: fe->key = xstrdup(key);
1014:
1015: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1016: if (fe_now != NULL) {
1017: free(fe->key);
1018: free(fe);
1019: free(fe_now->value);
1020: fe = fe_now;
1021: }
1022:
1023: fe->cb = NULL;
1024: fe->t = tv->tv_sec;
1025:
1026: fe->value = NULL;
1027: }
1028:
1.79 nicm 1029: /* Add a key and function. */
1.108 nicm 1030: static void
1.79 nicm 1031: format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
1032: {
1033: struct format_entry *fe;
1034: struct format_entry *fe_now;
1035:
1036: fe = xmalloc(sizeof *fe);
1037: fe->key = xstrdup(key);
1.1 nicm 1038:
1.68 nicm 1039: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1.28 nicm 1040: if (fe_now != NULL) {
1041: free(fe->key);
1042: free(fe);
1.79 nicm 1043: free(fe_now->value);
1044: fe = fe_now;
1.28 nicm 1045: }
1.79 nicm 1046:
1047: fe->cb = cb;
1.87 nicm 1048: fe->t = 0;
1.79 nicm 1049:
1050: fe->value = NULL;
1.1 nicm 1051: }
1052:
1.161 nicm 1053: /* Quote special characters in string. */
1054: static char *
1055: format_quote(const char *s)
1056: {
1057: const char *cp;
1058: char *out, *at;
1059:
1060: at = out = xmalloc(strlen(s) * 2 + 1);
1061: for (cp = s; *cp != '\0'; cp++) {
1062: if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
1063: *at++ = '\\';
1064: *at++ = *cp;
1065: }
1066: *at = '\0';
1067: return (out);
1068: }
1069:
1.1 nicm 1070: /* Find a format entry. */
1.108 nicm 1071: static char *
1.87 nicm 1072: format_find(struct format_tree *ft, const char *key, int modifiers)
1.1 nicm 1073: {
1074: struct format_entry *fe, fe_find;
1.76 nicm 1075: struct environ_entry *envent;
1.87 nicm 1076: static char s[64];
1.117 nicm 1077: struct options_entry *o;
1.116 nicm 1078: int idx;
1.189 nicm 1079: char *found, *saved;
1.87 nicm 1080:
1081: if (~modifiers & FORMAT_TIMESTRING) {
1.116 nicm 1082: o = options_parse_get(global_options, key, &idx, 0);
1.206 nicm 1083: if (o == NULL && ft->wp != NULL)
1084: o = options_parse_get(ft->wp->options, key, &idx, 0);
1.87 nicm 1085: if (o == NULL && ft->w != NULL)
1.116 nicm 1086: o = options_parse_get(ft->w->options, key, &idx, 0);
1.87 nicm 1087: if (o == NULL)
1.116 nicm 1088: o = options_parse_get(global_w_options, key, &idx, 0);
1.87 nicm 1089: if (o == NULL && ft->s != NULL)
1.116 nicm 1090: o = options_parse_get(ft->s->options, key, &idx, 0);
1.87 nicm 1091: if (o == NULL)
1.116 nicm 1092: o = options_parse_get(global_s_options, key, &idx, 0);
1.87 nicm 1093: if (o != NULL) {
1.118 nicm 1094: found = options_tostring(o, idx, 1);
1.116 nicm 1095: goto found;
1.54 nicm 1096: }
1097: }
1.116 nicm 1098: found = NULL;
1.1 nicm 1099:
1100: fe_find.key = (char *) key;
1.68 nicm 1101: fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.79 nicm 1102: if (fe != NULL) {
1.87 nicm 1103: if (modifiers & FORMAT_TIMESTRING) {
1104: if (fe->t == 0)
1105: return (NULL);
1106: ctime_r(&fe->t, s);
1107: s[strcspn(s, "\n")] = '\0';
1.189 nicm 1108: found = xstrdup(s);
1.87 nicm 1109: goto found;
1110: }
1111: if (fe->t != 0) {
1.189 nicm 1112: xasprintf(&found, "%lld", (long long)fe->t);
1.87 nicm 1113: goto found;
1114: }
1.220 ! nicm 1115: if (fe->value == NULL && fe->cb != NULL)
1.79 nicm 1116: fe->cb(ft, fe);
1.220 ! nicm 1117: if (fe->value == NULL)
! 1118: fe->value = xstrdup("");
1.189 nicm 1119: found = xstrdup(fe->value);
1.87 nicm 1120: goto found;
1.79 nicm 1121: }
1.76 nicm 1122:
1.87 nicm 1123: if (~modifiers & FORMAT_TIMESTRING) {
1124: envent = NULL;
1125: if (ft->s != NULL)
1.91 nicm 1126: envent = environ_find(ft->s->environ, key);
1.87 nicm 1127: if (envent == NULL)
1.91 nicm 1128: envent = environ_find(global_environ, key);
1.203 nicm 1129: if (envent != NULL && envent->value != NULL) {
1.189 nicm 1130: found = xstrdup(envent->value);
1.87 nicm 1131: goto found;
1132: }
1133: }
1.76 nicm 1134:
1135: return (NULL);
1.87 nicm 1136:
1137: found:
1.88 nicm 1138: if (found == NULL)
1139: return (NULL);
1.87 nicm 1140: if (modifiers & FORMAT_BASENAME) {
1.189 nicm 1141: saved = found;
1142: found = xstrdup(basename(saved));
1.87 nicm 1143: free(saved);
1144: }
1145: if (modifiers & FORMAT_DIRNAME) {
1.189 nicm 1146: saved = found;
1147: found = xstrdup(dirname(saved));
1.87 nicm 1148: free(saved);
1149: }
1.161 nicm 1150: if (modifiers & FORMAT_QUOTE) {
1.189 nicm 1151: saved = found;
1152: found = xstrdup(format_quote(saved));
1.161 nicm 1153: free(saved);
1154: }
1.189 nicm 1155: return (found);
1.1 nicm 1156: }
1157:
1.155 nicm 1158: /* Skip until end. */
1.185 nicm 1159: const char *
1.169 nicm 1160: format_skip(const char *s, const char *end)
1.114 nicm 1161: {
1162: int brackets = 0;
1163:
1164: for (; *s != '\0'; s++) {
1.155 nicm 1165: if (*s == '#' && s[1] == '{')
1.114 nicm 1166: brackets++;
1.155 nicm 1167: if (*s == '#' && strchr(",#{}", s[1]) != NULL) {
1168: s++;
1169: continue;
1170: }
1.114 nicm 1171: if (*s == '}')
1172: brackets--;
1.169 nicm 1173: if (strchr(end, *s) != NULL && brackets == 0)
1.114 nicm 1174: break;
1175: }
1176: if (*s == '\0')
1177: return (NULL);
1178: return (s);
1179: }
1180:
1181: /* Return left and right alternatives separated by commas. */
1182: static int
1.169 nicm 1183: format_choose(struct format_tree *ft, const char *s, char **left, char **right,
1184: int expand)
1.114 nicm 1185: {
1.169 nicm 1186: const char *cp;
1187: char *left0, *right0;
1.114 nicm 1188:
1.169 nicm 1189: cp = format_skip(s, ",");
1.114 nicm 1190: if (cp == NULL)
1191: return (-1);
1.169 nicm 1192: left0 = xstrndup(s, cp - s);
1193: right0 = xstrdup(cp + 1);
1.114 nicm 1194:
1.169 nicm 1195: if (expand) {
1196: *left = format_expand(ft, left0);
1.188 nicm 1197: free(left0);
1.169 nicm 1198: *right = format_expand(ft, right0);
1.188 nicm 1199: free(right0);
1.169 nicm 1200: } else {
1201: *left = left0;
1202: *right = right0;
1203: }
1.114 nicm 1204: return (0);
1205: }
1206:
1207: /* Is this true? */
1.141 nicm 1208: int
1.114 nicm 1209: format_true(const char *s)
1210: {
1211: if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
1212: return (1);
1213: return (0);
1214: }
1215:
1.169 nicm 1216: /* Check if modifier end. */
1217: static int
1218: format_is_end(char c)
1219: {
1220: return (c == ';' || c == ':');
1221: }
1222:
1223: /* Add to modifier list. */
1224: static void
1225: format_add_modifier(struct format_modifier **list, u_int *count,
1226: const char *c, size_t n, char **argv, int argc)
1227: {
1228: struct format_modifier *fm;
1229:
1230: *list = xreallocarray(*list, (*count) + 1, sizeof **list);
1231: fm = &(*list)[(*count)++];
1232:
1233: memcpy(fm->modifier, c, n);
1234: fm->modifier[n] = '\0';
1235: fm->size = n;
1236:
1237: fm->argv = argv;
1238: fm->argc = argc;
1239: }
1240:
1241: /* Free modifier list. */
1242: static void
1243: format_free_modifiers(struct format_modifier *list, u_int count)
1244: {
1245: u_int i;
1246:
1247: for (i = 0; i < count; i++)
1248: cmd_free_argv(list[i].argc, list[i].argv);
1249: free(list);
1250: }
1251:
1252: /* Build modifier list. */
1253: static struct format_modifier *
1254: format_build_modifiers(struct format_tree *ft, const char **s, u_int *count)
1255: {
1256: const char *cp = *s, *end;
1257: struct format_modifier *list = NULL;
1258: char c, last[] = "X;:", **argv, *value;
1259: int argc;
1260:
1261: /*
1262: * Modifiers are a ; separated list of the forms:
1.195 nicm 1263: * l,m,C,b,d,t,q,E,T,S,W,P,<,>
1.169 nicm 1264: * =a
1265: * =/a
1266: * =/a/
1267: * s/a/b/
1268: * s/a/b
1.195 nicm 1269: * ||,&&,!=,==,<=,>=
1.169 nicm 1270: */
1271:
1272: *count = 0;
1273:
1274: while (*cp != '\0' && *cp != ':') {
1275: /* Skip and separator character. */
1276: if (*cp == ';')
1277: cp++;
1278:
1279: /* Check single character modifiers with no arguments. */
1.202 nicm 1280: if (strchr("lbdtqETSWP<>", cp[0]) != NULL &&
1.172 nicm 1281: format_is_end(cp[1])) {
1.169 nicm 1282: format_add_modifier(&list, count, cp, 1, NULL, 0);
1283: cp++;
1284: continue;
1285: }
1286:
1287: /* Then try double character with no arguments. */
1288: if ((memcmp("||", cp, 2) == 0 ||
1289: memcmp("&&", cp, 2) == 0 ||
1290: memcmp("!=", cp, 2) == 0 ||
1.195 nicm 1291: memcmp("==", cp, 2) == 0 ||
1292: memcmp("<=", cp, 2) == 0 ||
1293: memcmp(">=", cp, 2) == 0) &&
1.169 nicm 1294: format_is_end(cp[2])) {
1295: format_add_modifier(&list, count, cp, 2, NULL, 0);
1296: cp += 2;
1297: continue;
1298: }
1299:
1300: /* Now try single character with arguments. */
1.217 nicm 1301: if (strchr("mCs=p", cp[0]) == NULL)
1.169 nicm 1302: break;
1303: c = cp[0];
1304:
1305: /* No arguments provided. */
1306: if (format_is_end(cp[1])) {
1307: format_add_modifier(&list, count, cp, 1, NULL, 0);
1308: cp++;
1309: continue;
1310: }
1311: argv = NULL;
1312: argc = 0;
1313:
1314: /* Single argument with no wrapper character. */
1315: if (!ispunct(cp[1]) || cp[1] == '-') {
1316: end = format_skip(cp + 1, ":;");
1317: if (end == NULL)
1318: break;
1319:
1320: argv = xcalloc(1, sizeof *argv);
1321: value = xstrndup(cp + 1, end - (cp + 1));
1322: argv[0] = format_expand(ft, value);
1323: free(value);
1324: argc = 1;
1325:
1326: format_add_modifier(&list, count, &c, 1, argv, argc);
1327: cp = end;
1328: continue;
1329: }
1330:
1331: /* Multiple arguments with a wrapper character. */
1332: last[0] = cp[1];
1333: cp++;
1334: do {
1335: if (cp[0] == last[0] && format_is_end(cp[1])) {
1336: cp++;
1337: break;
1338: }
1339: end = format_skip(cp + 1, last);
1340: if (end == NULL)
1341: break;
1342: cp++;
1343:
1344: argv = xreallocarray (argv, argc + 1, sizeof *argv);
1345: value = xstrndup(cp, end - cp);
1346: argv[argc++] = format_expand(ft, value);
1347: free(value);
1348:
1349: cp = end;
1350: } while (!format_is_end(cp[0]));
1351: format_add_modifier(&list, count, &c, 1, argv, argc);
1352: }
1353: if (*cp != ':') {
1354: format_free_modifiers(list, *count);
1355: *count = 0;
1356: return (NULL);
1357: }
1358: *s = cp + 1;
1359: return list;
1360: }
1361:
1.202 nicm 1362: /* Match against an fnmatch(3) pattern or regular expression. */
1363: static char *
1364: format_match(struct format_modifier *fm, const char *pattern, const char *text)
1365: {
1366: const char *s = "";
1367: regex_t r;
1368: int flags = 0;
1369:
1370: if (fm->argc >= 1)
1371: s = fm->argv[0];
1372: if (strchr(s, 'r') == NULL) {
1373: if (strchr(s, 'i') != NULL)
1374: flags |= FNM_CASEFOLD;
1375: if (fnmatch(pattern, text, flags) != 0)
1376: return (xstrdup("0"));
1377: } else {
1378: flags = REG_EXTENDED|REG_NOSUB;
1379: if (strchr(s, 'i') != NULL)
1380: flags |= REG_ICASE;
1381: if (regcomp(&r, pattern, flags) != 0)
1382: return (xstrdup("0"));
1383: if (regexec(&r, text, 0, NULL, 0) != 0) {
1384: regfree(&r);
1385: return (xstrdup("0"));
1386: }
1387: regfree(&r);
1388: }
1389: return (xstrdup("1"));
1390: }
1391:
1.169 nicm 1392: /* Perform substitution in string. */
1393: static char *
1.202 nicm 1394: format_sub(struct format_modifier *fm, const char *text, const char *pattern,
1395: const char *with)
1.169 nicm 1396: {
1.202 nicm 1397: char *value;
1398: int flags = REG_EXTENDED;
1.169 nicm 1399:
1.202 nicm 1400: if (fm->argc >= 3 && strchr(fm->argv[2], 'i') != NULL)
1401: flags |= REG_ICASE;
1402: value = regsub(pattern, with, text, flags);
1403: if (value == NULL)
1404: return (xstrdup(text));
1405: return (value);
1406: }
1.169 nicm 1407:
1.202 nicm 1408: /* Search inside pane. */
1409: static char *
1410: format_search(struct format_modifier *fm, struct window_pane *wp, const char *s)
1411: {
1412: int ignore = 0, regex = 0;
1413: char *value;
1.169 nicm 1414:
1.202 nicm 1415: if (fm->argc >= 1) {
1416: if (strchr(fm->argv[0], 'i') != NULL)
1417: ignore = 1;
1418: if (strchr(fm->argv[0], 'r') != NULL)
1419: regex = 1;
1.169 nicm 1420: }
1.202 nicm 1421: xasprintf(&value, "%u", window_pane_search(wp, s, regex, ignore));
1422: return (value);
1.169 nicm 1423: }
1424:
1.172 nicm 1425: /* Loop over sessions. */
1426: static char *
1427: format_loop_sessions(struct format_tree *ft, const char *fmt)
1428: {
1.180 nicm 1429: struct client *c = ft->client;
1.172 nicm 1430: struct cmdq_item *item = ft->item;
1.180 nicm 1431: struct format_tree *nft;
1.172 nicm 1432: char *expanded, *value;
1433: size_t valuelen;
1434: struct session *s;
1435:
1436: value = xcalloc(1, 1);
1437: valuelen = 1;
1438:
1439: RB_FOREACH(s, sessions, &sessions) {
1.179 nicm 1440: format_log(ft, "session loop: $%u", s->id);
1.180 nicm 1441: nft = format_create(c, item, FORMAT_NONE, ft->flags);
1.182 nicm 1442: nft->loop = ft->loop;
1.180 nicm 1443: format_defaults(nft, ft->c, s, NULL, NULL);
1444: expanded = format_expand(nft, fmt);
1445: format_free(nft);
1.172 nicm 1446:
1447: valuelen += strlen(expanded);
1448: value = xrealloc(value, valuelen);
1449:
1450: strlcat(value, expanded, valuelen);
1451: free(expanded);
1452: }
1453:
1454: return (value);
1455: }
1456:
1457: /* Loop over windows. */
1458: static char *
1459: format_loop_windows(struct format_tree *ft, const char *fmt)
1460: {
1.180 nicm 1461: struct client *c = ft->client;
1.172 nicm 1462: struct cmdq_item *item = ft->item;
1.180 nicm 1463: struct format_tree *nft;
1.172 nicm 1464: char *all, *active, *use, *expanded, *value;
1465: size_t valuelen;
1466: struct winlink *wl;
1.180 nicm 1467: struct window *w;
1.172 nicm 1468:
1.179 nicm 1469: if (ft->s == NULL) {
1470: format_log(ft, "window loop but no session");
1.172 nicm 1471: return (NULL);
1.179 nicm 1472: }
1.172 nicm 1473:
1474: if (format_choose(ft, fmt, &all, &active, 0) != 0) {
1475: all = xstrdup(fmt);
1476: active = NULL;
1477: }
1478:
1479: value = xcalloc(1, 1);
1480: valuelen = 1;
1481:
1482: RB_FOREACH(wl, winlinks, &ft->s->windows) {
1.180 nicm 1483: w = wl->window;
1484: format_log(ft, "window loop: %u @%u", wl->idx, w->id);
1.172 nicm 1485: if (active != NULL && wl == ft->s->curw)
1486: use = active;
1487: else
1488: use = all;
1.180 nicm 1489: nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
1.182 nicm 1490: nft->loop = ft->loop;
1.180 nicm 1491: format_defaults(nft, ft->c, ft->s, wl, NULL);
1492: expanded = format_expand(nft, use);
1493: format_free(nft);
1.172 nicm 1494:
1495: valuelen += strlen(expanded);
1496: value = xrealloc(value, valuelen);
1497:
1498: strlcat(value, expanded, valuelen);
1499: free(expanded);
1500: }
1501:
1502: free(active);
1503: free(all);
1504:
1505: return (value);
1506: }
1507:
1508: /* Loop over panes. */
1509: static char *
1510: format_loop_panes(struct format_tree *ft, const char *fmt)
1511: {
1.180 nicm 1512: struct client *c = ft->client;
1.172 nicm 1513: struct cmdq_item *item = ft->item;
1.180 nicm 1514: struct format_tree *nft;
1.172 nicm 1515: char *all, *active, *use, *expanded, *value;
1516: size_t valuelen;
1517: struct window_pane *wp;
1518:
1.179 nicm 1519: if (ft->w == NULL) {
1520: format_log(ft, "pane loop but no window");
1.172 nicm 1521: return (NULL);
1.179 nicm 1522: }
1.172 nicm 1523:
1524: if (format_choose(ft, fmt, &all, &active, 0) != 0) {
1525: all = xstrdup(fmt);
1526: active = NULL;
1527: }
1528:
1529: value = xcalloc(1, 1);
1530: valuelen = 1;
1531:
1532: TAILQ_FOREACH(wp, &ft->w->panes, entry) {
1.179 nicm 1533: format_log(ft, "pane loop: %%%u", wp->id);
1.172 nicm 1534: if (active != NULL && wp == ft->w->active)
1535: use = active;
1536: else
1537: use = all;
1.180 nicm 1538: nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
1.182 nicm 1539: nft->loop = ft->loop;
1.180 nicm 1540: format_defaults(nft, ft->c, ft->s, ft->wl, wp);
1541: expanded = format_expand(nft, use);
1542: format_free(nft);
1.172 nicm 1543:
1544: valuelen += strlen(expanded);
1545: value = xrealloc(value, valuelen);
1546:
1547: strlcat(value, expanded, valuelen);
1548: free(expanded);
1549: }
1550:
1551: free(active);
1552: free(all);
1553:
1554: return (value);
1555: }
1556:
1.140 nicm 1557: /* Replace a key. */
1.108 nicm 1558: static int
1.30 nicm 1559: format_replace(struct format_tree *ft, const char *key, size_t keylen,
1560: char **buf, size_t *len, size_t *off)
1.1 nicm 1561: {
1.216 nicm 1562: struct window_pane *wp = ft->wp;
1563: const char *errptr, *copy, *cp, *marker = NULL;
1564: char *copy0, *condition, *found, *new;
1565: char *value, *left, *right;
1566: size_t valuelen;
1.217 nicm 1567: int modifiers = 0, limit = 0, width = 0, j;
1.216 nicm 1568: struct format_modifier *list, *fm, *cmp = NULL, *search = NULL;
1569: struct format_modifier **sub = NULL;
1570: u_int i, count, nsub = 0;
1.1 nicm 1571:
1572: /* Make a copy of the key. */
1.169 nicm 1573: copy = copy0 = xstrndup(key, keylen);
1574:
1575: /* Process modifier list. */
1576: list = format_build_modifiers(ft, ©, &count);
1577: for (i = 0; i < count; i++) {
1578: fm = &list[i];
1.179 nicm 1579: if (format_logging(ft)) {
1580: format_log(ft, "modifier %u is %s", i, fm->modifier);
1581: for (j = 0; j < fm->argc; j++) {
1582: format_log(ft, "modifier %u argument %d: %s", i,
1583: j, fm->argv[j]);
1584: }
1585: }
1.169 nicm 1586: if (fm->size == 1) {
1587: switch (fm->modifier[0]) {
1588: case 'm':
1.195 nicm 1589: case '<':
1590: case '>':
1.169 nicm 1591: cmp = fm;
1592: break;
1593: case 'C':
1594: search = fm;
1595: break;
1596: case 's':
1.202 nicm 1597: if (fm->argc < 2)
1.169 nicm 1598: break;
1.216 nicm 1599: sub = xreallocarray (sub, nsub + 1,
1600: sizeof *sub);
1601: sub[nsub++] = fm;
1.169 nicm 1602: break;
1603: case '=':
1.202 nicm 1604: if (fm->argc < 1)
1.169 nicm 1605: break;
1606: limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
1607: &errptr);
1608: if (errptr != NULL)
1609: limit = 0;
1.202 nicm 1610: if (fm->argc >= 2 && fm->argv[1] != NULL)
1.196 nicm 1611: marker = fm->argv[1];
1.169 nicm 1612: break;
1.217 nicm 1613: case 'p':
1614: if (fm->argc < 1)
1615: break;
1616: width = strtonum(fm->argv[0], INT_MIN, INT_MAX,
1617: &errptr);
1618: if (errptr != NULL)
1619: width = 0;
1620: break;
1.169 nicm 1621: case 'l':
1622: modifiers |= FORMAT_LITERAL;
1623: break;
1624: case 'b':
1625: modifiers |= FORMAT_BASENAME;
1626: break;
1627: case 'd':
1628: modifiers |= FORMAT_DIRNAME;
1629: break;
1630: case 't':
1631: modifiers |= FORMAT_TIMESTRING;
1632: break;
1633: case 'q':
1634: modifiers |= FORMAT_QUOTE;
1635: break;
1.170 nicm 1636: case 'E':
1637: modifiers |= FORMAT_EXPAND;
1638: break;
1.175 nicm 1639: case 'T':
1640: modifiers |= FORMAT_EXPANDTIME;
1641: break;
1.172 nicm 1642: case 'S':
1643: modifiers |= FORMAT_SESSIONS;
1644: break;
1645: case 'W':
1646: modifiers |= FORMAT_WINDOWS;
1647: break;
1648: case 'P':
1649: modifiers |= FORMAT_PANES;
1650: break;
1.169 nicm 1651: }
1652: } else if (fm->size == 2) {
1653: if (strcmp(fm->modifier, "||") == 0 ||
1654: strcmp(fm->modifier, "&&") == 0 ||
1655: strcmp(fm->modifier, "==") == 0 ||
1.195 nicm 1656: strcmp(fm->modifier, "!=") == 0 ||
1657: strcmp(fm->modifier, ">=") == 0 ||
1658: strcmp(fm->modifier, "<=") == 0)
1.169 nicm 1659: cmp = fm;
1.98 nicm 1660: }
1.30 nicm 1661: }
1662:
1.155 nicm 1663: /* Is this a literal string? */
1.169 nicm 1664: if (modifiers & FORMAT_LITERAL) {
1.155 nicm 1665: value = xstrdup(copy);
1666: goto done;
1667: }
1668:
1.172 nicm 1669: /* Is this a loop, comparison or condition? */
1670: if (modifiers & FORMAT_SESSIONS) {
1671: value = format_loop_sessions(ft, copy);
1672: if (value == NULL)
1673: goto fail;
1674: } else if (modifiers & FORMAT_WINDOWS) {
1675: value = format_loop_windows(ft, copy);
1676: if (value == NULL)
1677: goto fail;
1678: } else if (modifiers & FORMAT_PANES) {
1679: value = format_loop_panes(ft, copy);
1680: if (value == NULL)
1681: goto fail;
1682: } else if (search != NULL) {
1.140 nicm 1683: /* Search in pane. */
1.207 nicm 1684: new = format_expand(ft, copy);
1.179 nicm 1685: if (wp == NULL) {
1.207 nicm 1686: format_log(ft, "search '%s' but no pane", new);
1.140 nicm 1687: value = xstrdup("0");
1.179 nicm 1688: } else {
1.207 nicm 1689: format_log(ft, "search '%s' pane %%%u", new, wp->id);
1690: value = format_search(fm, wp, new);
1.179 nicm 1691: }
1.207 nicm 1692: free(new);
1.169 nicm 1693: } else if (cmp != NULL) {
1694: /* Comparison of left and right. */
1.179 nicm 1695: if (format_choose(ft, copy, &left, &right, 1) != 0) {
1696: format_log(ft, "compare %s syntax error: %s",
1697: cmp->modifier, copy);
1.114 nicm 1698: goto fail;
1.179 nicm 1699: }
1700: format_log(ft, "compare %s left is: %s", cmp->modifier, left);
1701: format_log(ft, "compare %s right is: %s", cmp->modifier, right);
1.169 nicm 1702:
1703: if (strcmp(cmp->modifier, "||") == 0) {
1704: if (format_true(left) || format_true(right))
1705: value = xstrdup("1");
1706: else
1707: value = xstrdup("0");
1708: } else if (strcmp(cmp->modifier, "&&") == 0) {
1709: if (format_true(left) && format_true(right))
1710: value = xstrdup("1");
1711: else
1712: value = xstrdup("0");
1713: } else if (strcmp(cmp->modifier, "==") == 0) {
1714: if (strcmp(left, right) == 0)
1715: value = xstrdup("1");
1716: else
1717: value = xstrdup("0");
1718: } else if (strcmp(cmp->modifier, "!=") == 0) {
1719: if (strcmp(left, right) != 0)
1720: value = xstrdup("1");
1721: else
1722: value = xstrdup("0");
1.195 nicm 1723: } else if (strcmp(cmp->modifier, "<") == 0) {
1724: if (strcmp(left, right) < 0)
1725: value = xstrdup("1");
1726: else
1727: value = xstrdup("0");
1728: } else if (strcmp(cmp->modifier, ">") == 0) {
1729: if (strcmp(left, right) > 0)
1730: value = xstrdup("1");
1731: else
1732: value = xstrdup("0");
1733: } else if (strcmp(cmp->modifier, "<=") == 0) {
1734: if (strcmp(left, right) <= 0)
1735: value = xstrdup("1");
1736: else
1737: value = xstrdup("0");
1738: } else if (strcmp(cmp->modifier, ">=") == 0) {
1739: if (strcmp(left, right) >= 0)
1740: value = xstrdup("1");
1741: else
1742: value = xstrdup("0");
1.202 nicm 1743: } else if (strcmp(cmp->modifier, "m") == 0)
1.204 nicm 1744: value = format_match(cmp, left, right);
1.169 nicm 1745:
1.114 nicm 1746: free(right);
1747: free(left);
1748: } else if (*copy == '?') {
1749: /* Conditional: check first and choose second or third. */
1.169 nicm 1750: cp = format_skip(copy + 1, ",");
1.179 nicm 1751: if (cp == NULL) {
1752: format_log(ft, "condition syntax error: %s", copy + 1);
1.1 nicm 1753: goto fail;
1.179 nicm 1754: }
1.169 nicm 1755: condition = xstrndup(copy + 1, cp - (copy + 1));
1.179 nicm 1756: format_log(ft, "condition is: %s", condition);
1.1 nicm 1757:
1.169 nicm 1758: found = format_find(ft, condition, modifiers);
1.156 nicm 1759: if (found == NULL) {
1760: /*
1.169 nicm 1761: * If the condition not found, try to expand it. If
1.156 nicm 1762: * the expansion doesn't have any effect, then assume
1763: * false.
1764: */
1.169 nicm 1765: found = format_expand(ft, condition);
1766: if (strcmp(found, condition) == 0) {
1.156 nicm 1767: free(found);
1768: found = xstrdup("");
1.179 nicm 1769: format_log(ft, "condition '%s' found: %s",
1770: condition, found);
1771: } else {
1772: format_log(ft,
1773: "condition '%s' not found; assuming false",
1774: condition);
1.156 nicm 1775: }
1.179 nicm 1776: } else
1777: format_log(ft, "condition '%s' found", condition);
1.169 nicm 1778:
1779: if (format_choose(ft, cp + 1, &left, &right, 0) != 0) {
1.179 nicm 1780: format_log(ft, "condition '%s' syntax error: %s",
1781: condition, cp + 1);
1.162 nicm 1782: free(found);
1.89 nicm 1783: goto fail;
1.162 nicm 1784: }
1.179 nicm 1785: if (format_true(found)) {
1786: format_log(ft, "condition '%s' is true", condition);
1.114 nicm 1787: value = format_expand(ft, left);
1.179 nicm 1788: } else {
1789: format_log(ft, "condition '%s' is false", condition);
1.114 nicm 1790: value = format_expand(ft, right);
1.179 nicm 1791: }
1.169 nicm 1792: free(right);
1793: free(left);
1794:
1.179 nicm 1795: free(condition);
1.98 nicm 1796: free(found);
1.1 nicm 1797: } else {
1.114 nicm 1798: /* Neither: look up directly. */
1.98 nicm 1799: value = format_find(ft, copy, modifiers);
1.179 nicm 1800: if (value == NULL) {
1801: format_log(ft, "format '%s' not found", copy);
1.98 nicm 1802: value = xstrdup("");
1.179 nicm 1803: } else
1804: format_log(ft, "format '%s' found: %s", copy, value);
1.170 nicm 1805: }
1806:
1.171 nicm 1807: done:
1.170 nicm 1808: /* Expand again if required. */
1809: if (modifiers & FORMAT_EXPAND) {
1810: new = format_expand(ft, value);
1.175 nicm 1811: free(value);
1812: value = new;
1813: }
1814: else if (modifiers & FORMAT_EXPANDTIME) {
1.177 nicm 1815: new = format_expand_time(ft, value);
1.170 nicm 1816: free(value);
1817: value = new;
1.98 nicm 1818: }
1819:
1820: /* Perform substitution if any. */
1.216 nicm 1821: for (i = 0; i < nsub; i++) {
1822: left = format_expand(ft, sub[i]->argv[0]);
1823: right = format_expand(ft, sub[i]->argv[1]);
1824: new = format_sub(sub[i], value, left, right);
1.207 nicm 1825: format_log(ft, "substitute '%s' to '%s': %s", left, right, new);
1.98 nicm 1826: free(value);
1.169 nicm 1827: value = new;
1.207 nicm 1828: free(right);
1829: free(left);
1.1 nicm 1830: }
1831:
1.30 nicm 1832: /* Truncate the value if needed. */
1.105 nicm 1833: if (limit > 0) {
1.185 nicm 1834: new = format_trim_left(value, limit);
1.196 nicm 1835: if (marker != NULL && strcmp(new, value) != 0) {
1836: free(value);
1837: xasprintf(&value, "%s%s", new, marker);
1838: } else {
1839: free(value);
1840: value = new;
1841: }
1842: format_log(ft, "applied length limit %d: %s", limit, value);
1.105 nicm 1843: } else if (limit < 0) {
1.185 nicm 1844: new = format_trim_right(value, -limit);
1.196 nicm 1845: if (marker != NULL && strcmp(new, value) != 0) {
1846: free(value);
1847: xasprintf(&value, "%s%s", marker, new);
1848: } else {
1849: free(value);
1850: value = new;
1851: }
1852: format_log(ft, "applied length limit %d: %s", limit, value);
1.217 nicm 1853: }
1854:
1855: /* Pad the value if needed. */
1856: if (width > 0) {
1857: new = utf8_padcstr(value, width);
1858: free(value);
1859: value = new;
1860: format_log(ft, "applied padding width %d: %s", width, value);
1861: } else if (width < 0) {
1862: new = utf8_rpadcstr(value, -width);
1863: free(value);
1864: value = new;
1865: format_log(ft, "applied padding width %d: %s", width, value);
1.44 nicm 1866: }
1.30 nicm 1867:
1.1 nicm 1868: /* Expand the buffer and copy in the value. */
1.98 nicm 1869: valuelen = strlen(value);
1.1 nicm 1870: while (*len - *off < valuelen + 1) {
1.50 nicm 1871: *buf = xreallocarray(*buf, 2, *len);
1.1 nicm 1872: *len *= 2;
1873: }
1874: memcpy(*buf + *off, value, valuelen);
1875: *off += valuelen;
1876:
1.179 nicm 1877: format_log(ft, "replaced '%s' with '%s'", copy0, value);
1.98 nicm 1878: free(value);
1.179 nicm 1879:
1.216 nicm 1880: free(sub);
1.169 nicm 1881: format_free_modifiers(list, count);
1.30 nicm 1882: free(copy0);
1.1 nicm 1883: return (0);
1884:
1885: fail:
1.179 nicm 1886: format_log(ft, "failed %s", copy0);
1.216 nicm 1887:
1888: free(sub);
1.169 nicm 1889: format_free_modifiers(list, count);
1.30 nicm 1890: free(copy0);
1.1 nicm 1891: return (-1);
1892: }
1893:
1894: /* Expand keys in a template. */
1.179 nicm 1895: static char *
1896: format_expand1(struct format_tree *ft, const char *fmt, int time)
1.1 nicm 1897: {
1.152 nicm 1898: char *buf, *out, *name;
1.179 nicm 1899: const char *ptr, *s;
1.86 nicm 1900: size_t off, len, n, outlen;
1.31 nicm 1901: int ch, brackets;
1.179 nicm 1902: struct tm *tm;
1903: char expanded[8192];
1.58 nicm 1904:
1.179 nicm 1905: if (fmt == NULL || *fmt == '\0')
1.58 nicm 1906: return (xstrdup(""));
1.1 nicm 1907:
1.178 nicm 1908: if (ft->loop == FORMAT_LOOP_LIMIT)
1909: return (xstrdup(""));
1910: ft->loop++;
1911:
1.179 nicm 1912: format_log(ft, "expanding format: %s", fmt);
1913:
1914: if (time) {
1915: tm = localtime(&ft->time);
1916: if (strftime(expanded, sizeof expanded, fmt, tm) == 0) {
1917: format_log(ft, "format is too long");
1918: return (xstrdup(""));
1919: }
1920: if (format_logging(ft) && strcmp(expanded, fmt) != 0)
1921: format_log(ft, "after time expanded: %s", expanded);
1922: fmt = expanded;
1923: }
1924:
1.1 nicm 1925: len = 64;
1926: buf = xmalloc(len);
1927: off = 0;
1928:
1929: while (*fmt != '\0') {
1930: if (*fmt != '#') {
1931: while (len - off < 2) {
1.50 nicm 1932: buf = xreallocarray(buf, 2, len);
1.1 nicm 1933: len *= 2;
1934: }
1935: buf[off++] = *fmt++;
1936: continue;
1937: }
1938: fmt++;
1939:
1.179 nicm 1940: ch = (u_char)*fmt++;
1.1 nicm 1941: switch (ch) {
1.68 nicm 1942: case '(':
1943: brackets = 1;
1944: for (ptr = fmt; *ptr != '\0'; ptr++) {
1945: if (*ptr == '(')
1946: brackets++;
1947: if (*ptr == ')' && --brackets == 0)
1948: break;
1949: }
1950: if (*ptr != ')' || brackets != 0)
1951: break;
1952: n = ptr - fmt;
1953:
1.179 nicm 1954: name = xstrndup(fmt, n);
1955: format_log(ft, "found #(): %s", name);
1956:
1957: if (ft->flags & FORMAT_NOJOBS) {
1.114 nicm 1958: out = xstrdup("");
1.179 nicm 1959: format_log(ft, "#() is disabled");
1960: } else {
1.152 nicm 1961: out = format_job_get(ft, name);
1.179 nicm 1962: format_log(ft, "#() result: %s", out);
1.152 nicm 1963: }
1.179 nicm 1964: free(name);
1965:
1.86 nicm 1966: outlen = strlen(out);
1967: while (len - off < outlen + 1) {
1.68 nicm 1968: buf = xreallocarray(buf, 2, len);
1969: len *= 2;
1970: }
1.86 nicm 1971: memcpy(buf + off, out, outlen);
1972: off += outlen;
1973:
1974: free(out);
1.68 nicm 1975:
1976: fmt += n + 1;
1977: continue;
1.1 nicm 1978: case '{':
1.169 nicm 1979: ptr = format_skip((char *)fmt - 2, "}");
1.155 nicm 1980: if (ptr == NULL)
1.1 nicm 1981: break;
1982: n = ptr - fmt;
1983:
1.179 nicm 1984: format_log(ft, "found #{}: %.*s", (int)n, fmt);
1.1 nicm 1985: if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
1986: break;
1987: fmt += n + 1;
1.40 nicm 1988: continue;
1.155 nicm 1989: case '}':
1.40 nicm 1990: case '#':
1.155 nicm 1991: case ',':
1.179 nicm 1992: format_log(ft, "found #%c", ch);
1.40 nicm 1993: while (len - off < 2) {
1.50 nicm 1994: buf = xreallocarray(buf, 2, len);
1.40 nicm 1995: len *= 2;
1996: }
1.155 nicm 1997: buf[off++] = ch;
1.1 nicm 1998: continue;
1999: default:
1.25 nicm 2000: s = NULL;
2001: if (ch >= 'A' && ch <= 'Z')
2002: s = format_upper[ch - 'A'];
2003: else if (ch >= 'a' && ch <= 'z')
2004: s = format_lower[ch - 'a'];
2005: if (s == NULL) {
2006: while (len - off < 3) {
1.50 nicm 2007: buf = xreallocarray(buf, 2, len);
1.25 nicm 2008: len *= 2;
1.1 nicm 2009: }
1.25 nicm 2010: buf[off++] = '#';
2011: buf[off++] = ch;
2012: continue;
1.1 nicm 2013: }
1.25 nicm 2014: n = strlen(s);
1.179 nicm 2015: format_log(ft, "found #%c: %s", ch, s);
1.25 nicm 2016: if (format_replace(ft, s, n, &buf, &len, &off) != 0)
2017: break;
1.1 nicm 2018: continue;
2019: }
2020:
2021: break;
2022: }
2023: buf[off] = '\0';
2024:
1.179 nicm 2025: format_log(ft, "result is: %s", buf);
2026: ft->loop--;
1.178 nicm 2027:
1.1 nicm 2028: return (buf);
1.179 nicm 2029: }
2030:
2031: /* Expand keys in a template, passing through strftime first. */
2032: char *
2033: format_expand_time(struct format_tree *ft, const char *fmt)
2034: {
2035: return (format_expand1(ft, fmt, 1));
2036: }
2037:
2038: /* Expand keys in a template. */
2039: char *
2040: format_expand(struct format_tree *ft, const char *fmt)
2041: {
2042: return (format_expand1(ft, fmt, 0));
1.123 nicm 2043: }
2044:
2045: /* Expand a single string. */
2046: char *
2047: format_single(struct cmdq_item *item, const char *fmt, struct client *c,
2048: struct session *s, struct winlink *wl, struct window_pane *wp)
2049: {
2050: struct format_tree *ft;
2051: char *expanded;
2052:
1.131 nicm 2053: if (item != NULL)
2054: ft = format_create(item->client, item, FORMAT_NONE, 0);
2055: else
2056: ft = format_create(NULL, item, FORMAT_NONE, 0);
1.123 nicm 2057: format_defaults(ft, c, s, wl, wp);
2058:
2059: expanded = format_expand(ft, fmt);
2060: format_free(ft);
2061: return (expanded);
1.1 nicm 2062: }
2063:
1.57 nicm 2064: /* Set defaults for any of arguments that are not NULL. */
2065: void
2066: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
2067: struct winlink *wl, struct window_pane *wp)
2068: {
1.205 nicm 2069: if (c != NULL && c->name != NULL)
1.187 nicm 2070: log_debug("%s: c=%s", __func__, c->name);
2071: else
1.205 nicm 2072: log_debug("%s: c=none", __func__);
1.187 nicm 2073: if (s != NULL)
2074: log_debug("%s: s=$%u", __func__, s->id);
2075: else
2076: log_debug("%s: s=none", __func__);
2077: if (wl != NULL)
2078: log_debug("%s: wl=%u w=@%u", __func__, wl->idx, wl->window->id);
2079: else
2080: log_debug("%s: wl=none", __func__);
2081: if (wp != NULL)
2082: log_debug("%s: wp=%%%u", __func__, wp->id);
2083: else
2084: log_debug("%s: wp=none", __func__);
2085:
1.154 nicm 2086: if (c != NULL && s != NULL && c->session != s)
2087: log_debug("%s: session does not match", __func__);
2088:
1.146 nicm 2089: format_add(ft, "session_format", "%d", s != NULL);
2090: format_add(ft, "window_format", "%d", wl != NULL);
2091: format_add(ft, "pane_format", "%d", wp != NULL);
2092:
1.57 nicm 2093: if (s == NULL && c != NULL)
2094: s = c->session;
2095: if (wl == NULL && s != NULL)
2096: wl = s->curw;
2097: if (wp == NULL && wl != NULL)
2098: wp = wl->window->active;
2099:
2100: if (c != NULL)
2101: format_defaults_client(ft, c);
2102: if (s != NULL)
2103: format_defaults_session(ft, s);
1.129 nicm 2104: if (wl != NULL)
2105: format_defaults_winlink(ft, wl);
1.57 nicm 2106: if (wp != NULL)
2107: format_defaults_pane(ft, wp);
2108: }
2109:
1.1 nicm 2110: /* Set default format keys for a session. */
1.108 nicm 2111: static void
1.57 nicm 2112: format_defaults_session(struct format_tree *ft, struct session *s)
1.1 nicm 2113: {
2114: struct session_group *sg;
2115:
1.54 nicm 2116: ft->s = s;
2117:
1.1 nicm 2118: format_add(ft, "session_name", "%s", s->name);
2119: format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
1.23 nicm 2120: format_add(ft, "session_id", "$%u", s->id);
1.1 nicm 2121:
1.122 nicm 2122: sg = session_group_contains(s);
1.1 nicm 2123: format_add(ft, "session_grouped", "%d", sg != NULL);
1.148 nicm 2124: if (sg != NULL) {
1.122 nicm 2125: format_add(ft, "session_group", "%s", sg->name);
1.148 nicm 2126: format_add(ft, "session_group_size", "%u",
2127: session_group_count (sg));
1.150 nicm 2128: format_add_cb(ft, "session_group_list",
2129: format_cb_session_group_list);
1.148 nicm 2130: }
1.1 nicm 2131:
1.87 nicm 2132: format_add_tv(ft, "session_created", &s->creation_time);
2133: format_add_tv(ft, "session_last_attached", &s->last_attached_time);
2134: format_add_tv(ft, "session_activity", &s->activity_time);
1.1 nicm 2135:
1.41 nicm 2136: format_add(ft, "session_attached", "%u", s->attached);
1.59 nicm 2137: format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66 nicm 2138:
1.80 nicm 2139: format_add_cb(ft, "session_alerts", format_cb_session_alerts);
1.133 nicm 2140: format_add_cb(ft, "session_stack", format_cb_session_stack);
1.3 nicm 2141: }
2142:
2143: /* Set default format keys for a client. */
1.108 nicm 2144: static void
1.57 nicm 2145: format_defaults_client(struct format_tree *ft, struct client *c)
1.3 nicm 2146: {
1.60 nicm 2147: struct session *s;
1.103 nicm 2148: const char *name;
1.115 nicm 2149: struct tty *tty = &c->tty;
2150: const char *types[] = TTY_TYPES;
1.3 nicm 2151:
1.54 nicm 2152: if (ft->s == NULL)
2153: ft->s = c->session;
1.164 nicm 2154: ft->c = c;
1.54 nicm 2155:
1.124 nicm 2156: format_add(ft, "client_name", "%s", c->name);
1.72 nicm 2157: format_add(ft, "client_pid", "%ld", (long) c->pid);
1.115 nicm 2158: format_add(ft, "client_height", "%u", tty->sy);
2159: format_add(ft, "client_width", "%u", tty->sx);
1.218 nicm 2160: format_add(ft, "client_cell_width", "%u", tty->xpixel);
2161: format_add(ft, "client_cell_height", "%u", tty->ypixel);
1.124 nicm 2162: format_add(ft, "client_tty", "%s", c->ttyname);
1.75 nicm 2163: format_add(ft, "client_control_mode", "%d",
2164: !!(c->flags & CLIENT_CONTROL));
1.3 nicm 2165:
1.115 nicm 2166: if (tty->term_name != NULL)
2167: format_add(ft, "client_termname", "%s", tty->term_name);
2168: if (tty->term_name != NULL)
2169: format_add(ft, "client_termtype", "%s", types[tty->term_type]);
2170:
1.87 nicm 2171: format_add_tv(ft, "client_created", &c->creation_time);
2172: format_add_tv(ft, "client_activity", &c->activity_time);
1.126 nicm 2173:
2174: format_add(ft, "client_written", "%zu", c->written);
2175: format_add(ft, "client_discarded", "%zu", c->discarded);
1.14 nicm 2176:
1.103 nicm 2177: name = server_client_get_key_table(c);
2178: if (strcmp(c->keytable->name, name) == 0)
1.61 nicm 2179: format_add(ft, "client_prefix", "%d", 0);
2180: else
2181: format_add(ft, "client_prefix", "%d", 1);
2182: format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3 nicm 2183:
1.115 nicm 2184: if (tty->flags & TTY_UTF8)
1.3 nicm 2185: format_add(ft, "client_utf8", "%d", 1);
2186: else
2187: format_add(ft, "client_utf8", "%d", 0);
2188:
2189: if (c->flags & CLIENT_READONLY)
2190: format_add(ft, "client_readonly", "%d", 1);
2191: else
2192: format_add(ft, "client_readonly", "%d", 0);
1.15 nicm 2193:
2194: s = c->session;
2195: if (s != NULL)
2196: format_add(ft, "client_session", "%s", s->name);
2197: s = c->last_session;
2198: if (s != NULL && session_alive(s))
2199: format_add(ft, "client_last_session", "%s", s->name);
1.1 nicm 2200: }
2201:
1.32 nicm 2202: /* Set default format keys for a window. */
2203: void
1.57 nicm 2204: format_defaults_window(struct format_tree *ft, struct window *w)
1.32 nicm 2205: {
1.54 nicm 2206: ft->w = w;
2207:
1.87 nicm 2208: format_add_tv(ft, "window_activity", &w->activity_time);
1.32 nicm 2209: format_add(ft, "window_id", "@%u", w->id);
2210: format_add(ft, "window_name", "%s", w->name);
2211: format_add(ft, "window_width", "%u", w->sx);
2212: format_add(ft, "window_height", "%u", w->sy);
1.219 nicm 2213: format_add(ft, "window_cell_width", "%u", w->xpixel);
2214: format_add(ft, "window_cell_height", "%u", w->ypixel);
1.80 nicm 2215: format_add_cb(ft, "window_layout", format_cb_window_layout);
1.96 nicm 2216: format_add_cb(ft, "window_visible_layout",
2217: format_cb_window_visible_layout);
1.32 nicm 2218: format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59 nicm 2219: format_add(ft, "window_zoomed_flag", "%d",
1.53 nicm 2220: !!(w->flags & WINDOW_ZOOMED));
1.32 nicm 2221: }
2222:
1.1 nicm 2223: /* Set default format keys for a winlink. */
1.108 nicm 2224: static void
1.129 nicm 2225: format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
1.1 nicm 2226: {
1.164 nicm 2227: struct client *c = ft->c;
1.129 nicm 2228: struct session *s = wl->session;
1.1 nicm 2229: struct window *w = wl->window;
1.164 nicm 2230: int flag;
2231: u_int ox, oy, sx, sy;
1.1 nicm 2232:
1.54 nicm 2233: if (ft->w == NULL)
2234: ft->w = wl->window;
1.133 nicm 2235: ft->wl = wl;
1.54 nicm 2236:
1.57 nicm 2237: format_defaults_window(ft, w);
1.32 nicm 2238:
1.164 nicm 2239: if (c != NULL) {
2240: flag = tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
2241: format_add(ft, "window_bigger", "%d", flag);
2242: if (flag) {
2243: format_add(ft, "window_offset_x", "%u", ox);
2244: format_add(ft, "window_offset_y", "%u", oy);
2245: }
2246: }
2247:
1.1 nicm 2248: format_add(ft, "window_index", "%d", wl->idx);
1.133 nicm 2249: format_add_cb(ft, "window_stack_index", format_cb_window_stack_index);
1.129 nicm 2250: format_add(ft, "window_flags", "%s", window_printable_flags(wl));
1.1 nicm 2251: format_add(ft, "window_active", "%d", wl == s->curw);
1.176 nicm 2252:
2253: format_add(ft, "window_start_flag", "%d",
2254: !!(wl == RB_MIN(winlinks, &s->windows)));
2255: format_add(ft, "window_end_flag", "%d",
2256: !!(wl == RB_MAX(winlinks, &s->windows)));
1.210 nicm 2257:
2258: if (server_check_marked() && marked_pane.wl == wl)
2259: format_add(ft, "window_marked_flag", "1");
2260: else
2261: format_add(ft, "window_marked_flag", "0");
1.29 nicm 2262:
1.59 nicm 2263: format_add(ft, "window_bell_flag", "%d",
1.29 nicm 2264: !!(wl->flags & WINLINK_BELL));
1.59 nicm 2265: format_add(ft, "window_activity_flag", "%d",
1.29 nicm 2266: !!(wl->flags & WINLINK_ACTIVITY));
1.59 nicm 2267: format_add(ft, "window_silence_flag", "%d",
1.29 nicm 2268: !!(wl->flags & WINLINK_SILENCE));
1.59 nicm 2269: format_add(ft, "window_last_flag", "%d",
1.49 nicm 2270: !!(wl == TAILQ_FIRST(&s->lastw)));
1.64 nicm 2271: format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.1 nicm 2272: }
2273:
2274: /* Set default format keys for a window pane. */
2275: void
1.57 nicm 2276: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1 nicm 2277: {
1.168 nicm 2278: struct window *w = wp->window;
2279: struct grid *gd = wp->base.grid;
2280: int status = wp->status;
2281: u_int idx;
2282: struct window_mode_entry *wme;
1.54 nicm 2283:
2284: if (ft->w == NULL)
1.159 nicm 2285: ft->w = w;
1.79 nicm 2286: ft->wp = wp;
1.1 nicm 2287:
1.16 nicm 2288: format_add(ft, "history_size", "%u", gd->hsize);
2289: format_add(ft, "history_limit", "%u", gd->hlimit);
1.80 nicm 2290: format_add_cb(ft, "history_bytes", format_cb_history_bytes);
1.1 nicm 2291:
1.4 nicm 2292: if (window_pane_index(wp, &idx) != 0)
2293: fatalx("index not found");
1.16 nicm 2294: format_add(ft, "pane_index", "%u", idx);
1.4 nicm 2295:
1.1 nicm 2296: format_add(ft, "pane_width", "%u", wp->sx);
2297: format_add(ft, "pane_height", "%u", wp->sy);
2298: format_add(ft, "pane_title", "%s", wp->base.title);
1.215 nicm 2299: if (wp->base.path != NULL)
2300: format_add(ft, "pane_path", "%s", wp->base.path);
1.1 nicm 2301: format_add(ft, "pane_id", "%%%u", wp->id);
1.159 nicm 2302: format_add(ft, "pane_active", "%d", wp == w->active);
1.55 nicm 2303: format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
1.143 nicm 2304: format_add(ft, "pane_pipe", "%d", wp->pipe_fd != -1);
1.55 nicm 2305:
1.147 nicm 2306: if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(status))
1.55 nicm 2307: format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.190 nicm 2308: if (~wp->flags & PANE_EMPTY)
2309: format_add(ft, "pane_dead", "%d", wp->fd == -1);
2310: else
2311: format_add(ft, "pane_dead", "0");
1.191 nicm 2312:
2313: if (server_check_marked() && marked_pane.wp == wp)
2314: format_add(ft, "pane_marked", "1");
2315: else
2316: format_add(ft, "pane_marked", "0");
2317: format_add(ft, "pane_marked_set", "%d", server_check_marked());
1.47 nicm 2318:
1.164 nicm 2319: format_add(ft, "pane_left", "%u", wp->xoff);
2320: format_add(ft, "pane_top", "%u", wp->yoff);
2321: format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
2322: format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
2323: format_add(ft, "pane_at_left", "%d", wp->xoff == 0);
2324: format_add(ft, "pane_at_top", "%d", wp->yoff == 0);
2325: format_add(ft, "pane_at_right", "%d", wp->xoff + wp->sx == w->sx);
2326: format_add(ft, "pane_at_bottom", "%d", wp->yoff + wp->sy == w->sy);
1.16 nicm 2327:
1.168 nicm 2328: wme = TAILQ_FIRST(&wp->modes);
2329: if (wme != NULL) {
2330: format_add(ft, "pane_mode", "%s", wme->mode->name);
2331: if (wme->mode->formats != NULL)
2332: wme->mode->formats(wme, ft);
2333: }
2334: format_add_cb(ft, "pane_in_mode", format_cb_pane_in_mode);
1.134 nicm 2335:
1.26 nicm 2336: format_add(ft, "pane_synchronized", "%d",
1.159 nicm 2337: !!options_get_number(w->options, "synchronize-panes"));
1.135 nicm 2338: if (wp->searchstr != NULL)
2339: format_add(ft, "pane_search_string", "%s", wp->searchstr);
1.16 nicm 2340:
1.71 nicm 2341: format_add(ft, "pane_tty", "%s", wp->tty);
1.16 nicm 2342: format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.79 nicm 2343: format_add_cb(ft, "pane_start_command", format_cb_start_command);
2344: format_add_cb(ft, "pane_current_command", format_cb_current_command);
1.16 nicm 2345:
1.59 nicm 2346: format_add(ft, "cursor_x", "%u", wp->base.cx);
2347: format_add(ft, "cursor_y", "%u", wp->base.cy);
1.186 nicm 2348: format_add_cb(ft, "cursor_character", format_cb_cursor_character);
2349:
1.59 nicm 2350: format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
2351: format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.16 nicm 2352:
2353: format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59 nicm 2354: format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
2355: format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16 nicm 2356:
2357: format_add(ft, "cursor_flag", "%d",
2358: !!(wp->base.mode & MODE_CURSOR));
2359: format_add(ft, "insert_flag", "%d",
2360: !!(wp->base.mode & MODE_INSERT));
2361: format_add(ft, "keypad_cursor_flag", "%d",
2362: !!(wp->base.mode & MODE_KCURSOR));
2363: format_add(ft, "keypad_flag", "%d",
2364: !!(wp->base.mode & MODE_KKEYPAD));
2365: format_add(ft, "wrap_flag", "%d",
2366: !!(wp->base.mode & MODE_WRAP));
1.208 nicm 2367: format_add(ft, "origin_flag", "%d",
2368: !!(wp->base.mode & MODE_ORIGIN));
1.16 nicm 2369:
1.62 nicm 2370: format_add(ft, "mouse_any_flag", "%d",
1.119 nicm 2371: !!(wp->base.mode & ALL_MOUSE_MODES));
1.16 nicm 2372: format_add(ft, "mouse_standard_flag", "%d",
2373: !!(wp->base.mode & MODE_MOUSE_STANDARD));
2374: format_add(ft, "mouse_button_flag", "%d",
2375: !!(wp->base.mode & MODE_MOUSE_BUTTON));
1.119 nicm 2376: format_add(ft, "mouse_all_flag", "%d",
2377: !!(wp->base.mode & MODE_MOUSE_ALL));
1.208 nicm 2378: format_add(ft, "mouse_utf8_flag", "%d",
2379: !!(wp->base.mode & MODE_MOUSE_UTF8));
2380: format_add(ft, "mouse_sgr_flag", "%d",
2381: !!(wp->base.mode & MODE_MOUSE_SGR));
1.19 nicm 2382:
1.80 nicm 2383: format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
1.8 nicm 2384: }
2385:
1.19 nicm 2386: /* Set default format keys for paste buffer. */
1.8 nicm 2387: void
1.94 nicm 2388: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
1.8 nicm 2389: {
1.146 nicm 2390: struct timeval tv;
2391: size_t size;
2392: char *s;
2393:
2394: timerclear(&tv);
2395: tv.tv_sec = paste_buffer_created(pb);
2396: paste_buffer_data(pb, &size);
1.8 nicm 2397:
1.146 nicm 2398: format_add(ft, "buffer_size", "%zu", size);
1.81 nicm 2399: format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
1.146 nicm 2400: format_add_tv(ft, "buffer_created", &tv);
1.8 nicm 2401:
1.94 nicm 2402: s = paste_make_sample(pb);
1.42 nicm 2403: format_add(ft, "buffer_sample", "%s", s);
2404: free(s);
1.1 nicm 2405: }