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