Annotation of src/usr.bin/tmux/format.c, Revision 1.260
1.260 ! nicm 1: /* $OpenBSD: format.c,v 1.259 2020/06/23 05:23:26 nicm Exp $ */
1.1 nicm 2:
3: /*
1.104 nicm 4: * Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
1.55 nicm 20: #include <sys/wait.h>
1.1 nicm 21:
1.157 nicm 22: #include <ctype.h>
1.30 nicm 23: #include <errno.h>
1.139 nicm 24: #include <fnmatch.h>
1.87 nicm 25: #include <libgen.h>
1.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: int m;
1377:
1378: time(&now);
1379: if (now < t)
1380: now = t;
1381: age = now - t;
1382:
1383: localtime_r(&now, &now_tm);
1384: localtime_r(&t, &tm);
1385:
1386: /* Last 24 hours. */
1387: if (age < 24 * 3600) {
1388: strftime(s, sizeof s, "%H:%M", &tm);
1389: return (xstrdup(s));
1390: }
1391:
1392: /* This month or last 28 days. */
1393: if ((tm.tm_year == now_tm.tm_year && tm.tm_mon == now_tm.tm_mon) ||
1394: age < 28 * 24 * 3600) {
1395: strftime(s, sizeof s, "%a%d", &tm);
1396: return (xstrdup(s));
1397: }
1398:
1399: /* Last 12 months. */
1400: if (now_tm.tm_mon == 0)
1401: m = 11;
1402: else
1403: m = now_tm.tm_mon - 1;
1404: if ((tm.tm_year == now_tm.tm_year && tm.tm_mon < now_tm.tm_mon) ||
1405: (tm.tm_year == now_tm.tm_year - 1 && tm.tm_mon > now_tm.tm_mon)) {
1406: strftime(s, sizeof s, "%d%b", &tm);
1407: return (xstrdup(s));
1408: }
1409:
1410: /* Older than that. */
1411: strftime(s, sizeof s, "%h%y", &tm);
1412: return (xstrdup(s));
1413: }
1414:
1.1 nicm 1415: /* Find a format entry. */
1.108 nicm 1416: static char *
1.254 nicm 1417: format_find(struct format_tree *ft, const char *key, int modifiers,
1418: const char *time_format)
1.1 nicm 1419: {
1420: struct format_entry *fe, fe_find;
1.76 nicm 1421: struct environ_entry *envent;
1.117 nicm 1422: struct options_entry *o;
1.116 nicm 1423: int idx;
1.254 nicm 1424: char *found = NULL, *saved, s[512];
1.248 nicm 1425: const char *errstr;
1426: time_t t = 0;
1.254 nicm 1427: struct tm tm;
1.248 nicm 1428:
1429: o = options_parse_get(global_options, key, &idx, 0);
1430: if (o == NULL && ft->wp != NULL)
1431: o = options_parse_get(ft->wp->options, key, &idx, 0);
1432: if (o == NULL && ft->w != NULL)
1433: o = options_parse_get(ft->w->options, key, &idx, 0);
1434: if (o == NULL)
1435: o = options_parse_get(global_w_options, key, &idx, 0);
1436: if (o == NULL && ft->s != NULL)
1437: o = options_parse_get(ft->s->options, key, &idx, 0);
1438: if (o == NULL)
1439: o = options_parse_get(global_s_options, key, &idx, 0);
1440: if (o != NULL) {
1.255 nicm 1441: found = options_to_string(o, idx, 1);
1.248 nicm 1442: goto found;
1.54 nicm 1443: }
1.1 nicm 1444:
1.248 nicm 1445: fe_find.key = (char *)key;
1.68 nicm 1446: fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.79 nicm 1447: if (fe != NULL) {
1.87 nicm 1448: if (fe->t != 0) {
1.248 nicm 1449: t = fe->t;
1.87 nicm 1450: goto found;
1451: }
1.257 nicm 1452: if (fe->value == NULL && fe->cb != NULL) {
1453: fe->value = fe->cb(ft);
1454: if (fe->value == NULL)
1455: fe->value = xstrdup("");
1456: }
1.189 nicm 1457: found = xstrdup(fe->value);
1.87 nicm 1458: goto found;
1.79 nicm 1459: }
1.76 nicm 1460:
1.87 nicm 1461: if (~modifiers & FORMAT_TIMESTRING) {
1462: envent = NULL;
1463: if (ft->s != NULL)
1.91 nicm 1464: envent = environ_find(ft->s->environ, key);
1.87 nicm 1465: if (envent == NULL)
1.91 nicm 1466: envent = environ_find(global_environ, key);
1.203 nicm 1467: if (envent != NULL && envent->value != NULL) {
1.189 nicm 1468: found = xstrdup(envent->value);
1.87 nicm 1469: goto found;
1470: }
1471: }
1.76 nicm 1472:
1473: return (NULL);
1.87 nicm 1474:
1475: found:
1.248 nicm 1476: if (modifiers & FORMAT_TIMESTRING) {
1477: if (t == 0 && found != NULL) {
1478: t = strtonum(found, 0, INT64_MAX, &errstr);
1479: if (errstr != NULL)
1480: t = 0;
1481: free(found);
1482: }
1483: if (t == 0)
1484: return (NULL);
1485: if (modifiers & FORMAT_PRETTY)
1486: found = format_pretty_time(t);
1487: else {
1.254 nicm 1488: if (time_format != NULL) {
1489: localtime_r(&t, &tm);
1490: strftime(s, sizeof s, time_format, &tm);
1491: } else {
1492: ctime_r(&t, s);
1493: s[strcspn(s, "\n")] = '\0';
1494: }
1.248 nicm 1495: found = xstrdup(s);
1496: }
1497: return (found);
1498: }
1499:
1500: if (t != 0)
1501: xasprintf(&found, "%lld", (long long)t);
1502: else if (found == NULL)
1.88 nicm 1503: return (NULL);
1.87 nicm 1504: if (modifiers & FORMAT_BASENAME) {
1.189 nicm 1505: saved = found;
1506: found = xstrdup(basename(saved));
1.87 nicm 1507: free(saved);
1508: }
1509: if (modifiers & FORMAT_DIRNAME) {
1.189 nicm 1510: saved = found;
1511: found = xstrdup(dirname(saved));
1.87 nicm 1512: free(saved);
1513: }
1.161 nicm 1514: if (modifiers & FORMAT_QUOTE) {
1.189 nicm 1515: saved = found;
1516: found = xstrdup(format_quote(saved));
1.161 nicm 1517: free(saved);
1518: }
1.189 nicm 1519: return (found);
1.1 nicm 1520: }
1521:
1.254 nicm 1522: /* Remove escaped characters from string. */
1523: static char *
1524: format_strip(const char *s)
1525: {
1526: char *out, *cp;
1527: int brackets = 0;
1528:
1529: cp = out = xmalloc(strlen(s) + 1);
1530: for (; *s != '\0'; s++) {
1531: if (*s == '#' && s[1] == '{')
1532: brackets++;
1533: if (*s == '#' && strchr(",#{}:", s[1]) != NULL) {
1534: if (brackets != 0)
1535: *cp++ = *s;
1536: continue;
1537: }
1538: if (*s == '}')
1539: brackets--;
1540: *cp++ = *s;
1541: }
1542: *cp = '\0';
1543: return (out);
1544: }
1545:
1.155 nicm 1546: /* Skip until end. */
1.185 nicm 1547: const char *
1.169 nicm 1548: format_skip(const char *s, const char *end)
1.114 nicm 1549: {
1550: int brackets = 0;
1551:
1552: for (; *s != '\0'; s++) {
1.155 nicm 1553: if (*s == '#' && s[1] == '{')
1.114 nicm 1554: brackets++;
1.254 nicm 1555: if (*s == '#' && strchr(",#{}:", s[1]) != NULL) {
1.155 nicm 1556: s++;
1557: continue;
1558: }
1.114 nicm 1559: if (*s == '}')
1560: brackets--;
1.169 nicm 1561: if (strchr(end, *s) != NULL && brackets == 0)
1.114 nicm 1562: break;
1563: }
1564: if (*s == '\0')
1565: return (NULL);
1566: return (s);
1567: }
1568:
1569: /* Return left and right alternatives separated by commas. */
1570: static int
1.169 nicm 1571: format_choose(struct format_tree *ft, const char *s, char **left, char **right,
1572: int expand)
1.114 nicm 1573: {
1.169 nicm 1574: const char *cp;
1575: char *left0, *right0;
1.114 nicm 1576:
1.169 nicm 1577: cp = format_skip(s, ",");
1.114 nicm 1578: if (cp == NULL)
1579: return (-1);
1.169 nicm 1580: left0 = xstrndup(s, cp - s);
1581: right0 = xstrdup(cp + 1);
1.114 nicm 1582:
1.169 nicm 1583: if (expand) {
1584: *left = format_expand(ft, left0);
1.188 nicm 1585: free(left0);
1.169 nicm 1586: *right = format_expand(ft, right0);
1.188 nicm 1587: free(right0);
1.169 nicm 1588: } else {
1589: *left = left0;
1590: *right = right0;
1591: }
1.114 nicm 1592: return (0);
1593: }
1594:
1595: /* Is this true? */
1.141 nicm 1596: int
1.114 nicm 1597: format_true(const char *s)
1598: {
1599: if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
1600: return (1);
1601: return (0);
1602: }
1603:
1.169 nicm 1604: /* Check if modifier end. */
1605: static int
1606: format_is_end(char c)
1607: {
1608: return (c == ';' || c == ':');
1609: }
1610:
1611: /* Add to modifier list. */
1612: static void
1613: format_add_modifier(struct format_modifier **list, u_int *count,
1614: const char *c, size_t n, char **argv, int argc)
1615: {
1616: struct format_modifier *fm;
1617:
1618: *list = xreallocarray(*list, (*count) + 1, sizeof **list);
1619: fm = &(*list)[(*count)++];
1620:
1621: memcpy(fm->modifier, c, n);
1622: fm->modifier[n] = '\0';
1623: fm->size = n;
1624:
1625: fm->argv = argv;
1626: fm->argc = argc;
1627: }
1628:
1629: /* Free modifier list. */
1630: static void
1631: format_free_modifiers(struct format_modifier *list, u_int count)
1632: {
1633: u_int i;
1634:
1635: for (i = 0; i < count; i++)
1636: cmd_free_argv(list[i].argc, list[i].argv);
1637: free(list);
1638: }
1639:
1640: /* Build modifier list. */
1641: static struct format_modifier *
1642: format_build_modifiers(struct format_tree *ft, const char **s, u_int *count)
1643: {
1644: const char *cp = *s, *end;
1645: struct format_modifier *list = NULL;
1646: char c, last[] = "X;:", **argv, *value;
1647: int argc;
1648:
1649: /*
1650: * Modifiers are a ; separated list of the forms:
1.260 ! nicm 1651: * l,m,C,b,d,n,t,q,E,T,S,W,P,<,>
1.169 nicm 1652: * =a
1653: * =/a
1654: * =/a/
1655: * s/a/b/
1656: * s/a/b
1.195 nicm 1657: * ||,&&,!=,==,<=,>=
1.169 nicm 1658: */
1659:
1660: *count = 0;
1661:
1662: while (*cp != '\0' && *cp != ':') {
1.254 nicm 1663: /* Skip any separator character. */
1.169 nicm 1664: if (*cp == ';')
1665: cp++;
1666:
1667: /* Check single character modifiers with no arguments. */
1.260 ! nicm 1668: if (strchr("lbdnqETSWP<>", cp[0]) != NULL &&
1.172 nicm 1669: format_is_end(cp[1])) {
1.169 nicm 1670: format_add_modifier(&list, count, cp, 1, NULL, 0);
1671: cp++;
1672: continue;
1673: }
1674:
1675: /* Then try double character with no arguments. */
1676: if ((memcmp("||", cp, 2) == 0 ||
1677: memcmp("&&", cp, 2) == 0 ||
1678: memcmp("!=", cp, 2) == 0 ||
1.195 nicm 1679: memcmp("==", cp, 2) == 0 ||
1680: memcmp("<=", cp, 2) == 0 ||
1681: memcmp(">=", cp, 2) == 0) &&
1.169 nicm 1682: format_is_end(cp[2])) {
1683: format_add_modifier(&list, count, cp, 2, NULL, 0);
1684: cp += 2;
1685: continue;
1686: }
1687:
1688: /* Now try single character with arguments. */
1.248 nicm 1689: if (strchr("mCst=pe", cp[0]) == NULL)
1.169 nicm 1690: break;
1691: c = cp[0];
1692:
1693: /* No arguments provided. */
1694: if (format_is_end(cp[1])) {
1695: format_add_modifier(&list, count, cp, 1, NULL, 0);
1696: cp++;
1697: continue;
1698: }
1699: argv = NULL;
1700: argc = 0;
1701:
1702: /* Single argument with no wrapper character. */
1703: if (!ispunct(cp[1]) || cp[1] == '-') {
1704: end = format_skip(cp + 1, ":;");
1705: if (end == NULL)
1706: break;
1707:
1708: argv = xcalloc(1, sizeof *argv);
1709: value = xstrndup(cp + 1, end - (cp + 1));
1710: argv[0] = format_expand(ft, value);
1711: free(value);
1712: argc = 1;
1713:
1714: format_add_modifier(&list, count, &c, 1, argv, argc);
1715: cp = end;
1716: continue;
1717: }
1718:
1719: /* Multiple arguments with a wrapper character. */
1720: last[0] = cp[1];
1721: cp++;
1722: do {
1723: if (cp[0] == last[0] && format_is_end(cp[1])) {
1724: cp++;
1725: break;
1726: }
1727: end = format_skip(cp + 1, last);
1728: if (end == NULL)
1729: break;
1730: cp++;
1731:
1732: argv = xreallocarray (argv, argc + 1, sizeof *argv);
1733: value = xstrndup(cp, end - cp);
1734: argv[argc++] = format_expand(ft, value);
1735: free(value);
1736:
1737: cp = end;
1738: } while (!format_is_end(cp[0]));
1739: format_add_modifier(&list, count, &c, 1, argv, argc);
1740: }
1741: if (*cp != ':') {
1742: format_free_modifiers(list, *count);
1743: *count = 0;
1744: return (NULL);
1745: }
1746: *s = cp + 1;
1.235 nicm 1747: return (list);
1.169 nicm 1748: }
1749:
1.202 nicm 1750: /* Match against an fnmatch(3) pattern or regular expression. */
1751: static char *
1752: format_match(struct format_modifier *fm, const char *pattern, const char *text)
1753: {
1754: const char *s = "";
1755: regex_t r;
1756: int flags = 0;
1757:
1758: if (fm->argc >= 1)
1759: s = fm->argv[0];
1760: if (strchr(s, 'r') == NULL) {
1761: if (strchr(s, 'i') != NULL)
1762: flags |= FNM_CASEFOLD;
1763: if (fnmatch(pattern, text, flags) != 0)
1764: return (xstrdup("0"));
1765: } else {
1766: flags = REG_EXTENDED|REG_NOSUB;
1767: if (strchr(s, 'i') != NULL)
1768: flags |= REG_ICASE;
1769: if (regcomp(&r, pattern, flags) != 0)
1770: return (xstrdup("0"));
1771: if (regexec(&r, text, 0, NULL, 0) != 0) {
1772: regfree(&r);
1773: return (xstrdup("0"));
1774: }
1775: regfree(&r);
1776: }
1777: return (xstrdup("1"));
1778: }
1779:
1.169 nicm 1780: /* Perform substitution in string. */
1781: static char *
1.202 nicm 1782: format_sub(struct format_modifier *fm, const char *text, const char *pattern,
1783: const char *with)
1.169 nicm 1784: {
1.202 nicm 1785: char *value;
1786: int flags = REG_EXTENDED;
1.169 nicm 1787:
1.202 nicm 1788: if (fm->argc >= 3 && strchr(fm->argv[2], 'i') != NULL)
1789: flags |= REG_ICASE;
1790: value = regsub(pattern, with, text, flags);
1791: if (value == NULL)
1792: return (xstrdup(text));
1793: return (value);
1794: }
1.169 nicm 1795:
1.202 nicm 1796: /* Search inside pane. */
1797: static char *
1798: format_search(struct format_modifier *fm, struct window_pane *wp, const char *s)
1799: {
1800: int ignore = 0, regex = 0;
1801: char *value;
1.169 nicm 1802:
1.202 nicm 1803: if (fm->argc >= 1) {
1804: if (strchr(fm->argv[0], 'i') != NULL)
1805: ignore = 1;
1806: if (strchr(fm->argv[0], 'r') != NULL)
1807: regex = 1;
1.169 nicm 1808: }
1.202 nicm 1809: xasprintf(&value, "%u", window_pane_search(wp, s, regex, ignore));
1810: return (value);
1.169 nicm 1811: }
1812:
1.172 nicm 1813: /* Loop over sessions. */
1814: static char *
1815: format_loop_sessions(struct format_tree *ft, const char *fmt)
1816: {
1.180 nicm 1817: struct client *c = ft->client;
1.172 nicm 1818: struct cmdq_item *item = ft->item;
1.180 nicm 1819: struct format_tree *nft;
1.172 nicm 1820: char *expanded, *value;
1821: size_t valuelen;
1822: struct session *s;
1823:
1824: value = xcalloc(1, 1);
1825: valuelen = 1;
1826:
1827: RB_FOREACH(s, sessions, &sessions) {
1.179 nicm 1828: format_log(ft, "session loop: $%u", s->id);
1.180 nicm 1829: nft = format_create(c, item, FORMAT_NONE, ft->flags);
1.182 nicm 1830: nft->loop = ft->loop;
1.180 nicm 1831: format_defaults(nft, ft->c, s, NULL, NULL);
1832: expanded = format_expand(nft, fmt);
1833: format_free(nft);
1.172 nicm 1834:
1835: valuelen += strlen(expanded);
1836: value = xrealloc(value, valuelen);
1837:
1838: strlcat(value, expanded, valuelen);
1839: free(expanded);
1840: }
1841:
1842: return (value);
1843: }
1844:
1845: /* Loop over windows. */
1846: static char *
1847: format_loop_windows(struct format_tree *ft, const char *fmt)
1848: {
1.180 nicm 1849: struct client *c = ft->client;
1.172 nicm 1850: struct cmdq_item *item = ft->item;
1.180 nicm 1851: struct format_tree *nft;
1.172 nicm 1852: char *all, *active, *use, *expanded, *value;
1853: size_t valuelen;
1854: struct winlink *wl;
1.180 nicm 1855: struct window *w;
1.172 nicm 1856:
1.179 nicm 1857: if (ft->s == NULL) {
1858: format_log(ft, "window loop but no session");
1.172 nicm 1859: return (NULL);
1.179 nicm 1860: }
1.172 nicm 1861:
1862: if (format_choose(ft, fmt, &all, &active, 0) != 0) {
1863: all = xstrdup(fmt);
1864: active = NULL;
1865: }
1866:
1867: value = xcalloc(1, 1);
1868: valuelen = 1;
1869:
1870: RB_FOREACH(wl, winlinks, &ft->s->windows) {
1.180 nicm 1871: w = wl->window;
1872: format_log(ft, "window loop: %u @%u", wl->idx, w->id);
1.172 nicm 1873: if (active != NULL && wl == ft->s->curw)
1874: use = active;
1875: else
1876: use = all;
1.180 nicm 1877: nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
1.182 nicm 1878: nft->loop = ft->loop;
1.180 nicm 1879: format_defaults(nft, ft->c, ft->s, wl, NULL);
1880: expanded = format_expand(nft, use);
1881: format_free(nft);
1.172 nicm 1882:
1883: valuelen += strlen(expanded);
1884: value = xrealloc(value, valuelen);
1885:
1886: strlcat(value, expanded, valuelen);
1887: free(expanded);
1888: }
1889:
1890: free(active);
1891: free(all);
1892:
1893: return (value);
1894: }
1895:
1896: /* Loop over panes. */
1897: static char *
1898: format_loop_panes(struct format_tree *ft, const char *fmt)
1899: {
1.180 nicm 1900: struct client *c = ft->client;
1.172 nicm 1901: struct cmdq_item *item = ft->item;
1.180 nicm 1902: struct format_tree *nft;
1.172 nicm 1903: char *all, *active, *use, *expanded, *value;
1904: size_t valuelen;
1905: struct window_pane *wp;
1906:
1.179 nicm 1907: if (ft->w == NULL) {
1908: format_log(ft, "pane loop but no window");
1.172 nicm 1909: return (NULL);
1.179 nicm 1910: }
1.172 nicm 1911:
1912: if (format_choose(ft, fmt, &all, &active, 0) != 0) {
1913: all = xstrdup(fmt);
1914: active = NULL;
1915: }
1916:
1917: value = xcalloc(1, 1);
1918: valuelen = 1;
1919:
1920: TAILQ_FOREACH(wp, &ft->w->panes, entry) {
1.179 nicm 1921: format_log(ft, "pane loop: %%%u", wp->id);
1.172 nicm 1922: if (active != NULL && wp == ft->w->active)
1923: use = active;
1924: else
1925: use = all;
1.180 nicm 1926: nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
1.182 nicm 1927: nft->loop = ft->loop;
1.180 nicm 1928: format_defaults(nft, ft->c, ft->s, ft->wl, wp);
1929: expanded = format_expand(nft, use);
1930: format_free(nft);
1.172 nicm 1931:
1932: valuelen += strlen(expanded);
1933: value = xrealloc(value, valuelen);
1934:
1935: strlcat(value, expanded, valuelen);
1936: free(expanded);
1937: }
1938:
1939: free(active);
1940: free(all);
1941:
1942: return (value);
1943: }
1944:
1.226 nicm 1945: static char *
1946: format_replace_expression(struct format_modifier *mexp, struct format_tree *ft,
1947: const char *copy)
1948: {
1949: int argc = mexp->argc;
1950: const char *errstr;
1951: char *endch, *value, *left = NULL, *right = NULL;
1952: int use_fp = 0;
1953: u_int prec = 0;
1954: double mleft, mright, result;
1955: enum { ADD, SUBTRACT, MULTIPLY, DIVIDE, MODULUS } operator;
1956:
1957: if (strcmp(mexp->argv[0], "+") == 0)
1958: operator = ADD;
1959: else if (strcmp(mexp->argv[0], "-") == 0)
1960: operator = SUBTRACT;
1961: else if (strcmp(mexp->argv[0], "*") == 0)
1962: operator = MULTIPLY;
1963: else if (strcmp(mexp->argv[0], "/") == 0)
1964: operator = DIVIDE;
1965: else if (strcmp(mexp->argv[0], "%") == 0 ||
1966: strcmp(mexp->argv[0], "m") == 0)
1967: operator = MODULUS;
1968: else {
1969: format_log(ft, "expression has no valid operator: '%s'",
1970: mexp->argv[0]);
1971: goto fail;
1972: }
1973:
1974: /* The second argument may be flags. */
1975: if (argc >= 2 && strchr(mexp->argv[1], 'f') != NULL) {
1976: use_fp = 1;
1977: prec = 2;
1978: }
1979:
1980: /* The third argument may be precision. */
1981: if (argc >= 3) {
1982: prec = strtonum(mexp->argv[2], INT_MIN, INT_MAX, &errstr);
1983: if (errstr != NULL) {
1984: format_log (ft, "expression precision %s: %s", errstr,
1985: mexp->argv[2]);
1986: goto fail;
1987: }
1988: }
1989:
1990: if (format_choose(ft, copy, &left, &right, 1) != 0) {
1991: format_log(ft, "expression syntax error");
1992: goto fail;
1993: }
1994:
1995: mleft = strtod(left, &endch);
1996: if (*endch != '\0') {
1997: format_log(ft, "expression left side is invalid: %s", left);
1998: goto fail;
1999: }
2000:
2001: mright = strtod(right, &endch);
2002: if (*endch != '\0') {
2003: format_log(ft, "expression right side is invalid: %s", right);
2004: goto fail;
2005: }
2006:
2007: if (!use_fp) {
2008: mleft = (long long)mleft;
2009: mright = (long long)mright;
2010: }
2011: format_log(ft, "expression left side is: %.*f", prec, mleft);
2012: format_log(ft, "expression right side is: %.*f", prec, mright);
2013:
2014: switch (operator) {
2015: case ADD:
2016: result = mleft + mright;
2017: break;
2018: case SUBTRACT:
2019: result = mleft - mright;
2020: break;
2021: case MULTIPLY:
2022: result = mleft * mright;
2023: break;
2024: case DIVIDE:
2025: result = mleft / mright;
2026: break;
2027: case MODULUS:
2028: result = fmod(mleft, mright);
2029: break;
2030: }
2031: if (use_fp)
2032: xasprintf(&value, "%.*f", prec, result);
2033: else
2034: xasprintf(&value, "%.*f", prec, (double)(long long)result);
2035: format_log(ft, "expression result is %s", value);
2036:
2037: free(right);
2038: free(left);
1.235 nicm 2039: return (value);
1.226 nicm 2040:
2041: fail:
2042: free(right);
2043: free(left);
2044: return (NULL);
2045: }
2046:
1.140 nicm 2047: /* Replace a key. */
1.108 nicm 2048: static int
1.30 nicm 2049: format_replace(struct format_tree *ft, const char *key, size_t keylen,
2050: char **buf, size_t *len, size_t *off)
1.1 nicm 2051: {
1.216 nicm 2052: struct window_pane *wp = ft->wp;
2053: const char *errptr, *copy, *cp, *marker = NULL;
1.254 nicm 2054: const char *time_format = NULL;
1.216 nicm 2055: char *copy0, *condition, *found, *new;
2056: char *value, *left, *right;
2057: size_t valuelen;
1.217 nicm 2058: int modifiers = 0, limit = 0, width = 0, j;
1.216 nicm 2059: struct format_modifier *list, *fm, *cmp = NULL, *search = NULL;
1.226 nicm 2060: struct format_modifier **sub = NULL, *mexp = NULL;
1.216 nicm 2061: u_int i, count, nsub = 0;
1.1 nicm 2062:
2063: /* Make a copy of the key. */
1.169 nicm 2064: copy = copy0 = xstrndup(key, keylen);
2065:
2066: /* Process modifier list. */
2067: list = format_build_modifiers(ft, ©, &count);
2068: for (i = 0; i < count; i++) {
2069: fm = &list[i];
1.179 nicm 2070: if (format_logging(ft)) {
2071: format_log(ft, "modifier %u is %s", i, fm->modifier);
2072: for (j = 0; j < fm->argc; j++) {
2073: format_log(ft, "modifier %u argument %d: %s", i,
2074: j, fm->argv[j]);
2075: }
2076: }
1.169 nicm 2077: if (fm->size == 1) {
2078: switch (fm->modifier[0]) {
2079: case 'm':
1.195 nicm 2080: case '<':
2081: case '>':
1.169 nicm 2082: cmp = fm;
2083: break;
2084: case 'C':
2085: search = fm;
2086: break;
2087: case 's':
1.202 nicm 2088: if (fm->argc < 2)
1.169 nicm 2089: break;
1.216 nicm 2090: sub = xreallocarray (sub, nsub + 1,
2091: sizeof *sub);
2092: sub[nsub++] = fm;
1.169 nicm 2093: break;
2094: case '=':
1.202 nicm 2095: if (fm->argc < 1)
1.169 nicm 2096: break;
2097: limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
2098: &errptr);
2099: if (errptr != NULL)
2100: limit = 0;
1.202 nicm 2101: if (fm->argc >= 2 && fm->argv[1] != NULL)
1.196 nicm 2102: marker = fm->argv[1];
1.169 nicm 2103: break;
1.217 nicm 2104: case 'p':
2105: if (fm->argc < 1)
2106: break;
2107: width = strtonum(fm->argv[0], INT_MIN, INT_MAX,
2108: &errptr);
2109: if (errptr != NULL)
2110: width = 0;
2111: break;
1.226 nicm 2112: case 'e':
2113: if (fm->argc < 1 || fm->argc > 3)
2114: break;
1.248 nicm 2115: mexp = fm;
1.226 nicm 2116: break;
1.169 nicm 2117: case 'l':
2118: modifiers |= FORMAT_LITERAL;
2119: break;
2120: case 'b':
2121: modifiers |= FORMAT_BASENAME;
2122: break;
2123: case 'd':
2124: modifiers |= FORMAT_DIRNAME;
2125: break;
1.260 ! nicm 2126: case 'n':
! 2127: modifiers |= FORMAT_LENGTH;
! 2128: break;
1.169 nicm 2129: case 't':
2130: modifiers |= FORMAT_TIMESTRING;
1.248 nicm 2131: if (fm->argc < 1)
2132: break;
2133: if (strchr(fm->argv[0], 'p') != NULL)
2134: modifiers |= FORMAT_PRETTY;
1.254 nicm 2135: else if (fm->argc >= 2 &&
2136: strchr(fm->argv[0], 'f') != NULL)
2137: time_format = format_strip(fm->argv[1]);
1.169 nicm 2138: break;
2139: case 'q':
2140: modifiers |= FORMAT_QUOTE;
2141: break;
1.170 nicm 2142: case 'E':
2143: modifiers |= FORMAT_EXPAND;
2144: break;
1.175 nicm 2145: case 'T':
2146: modifiers |= FORMAT_EXPANDTIME;
2147: break;
1.172 nicm 2148: case 'S':
2149: modifiers |= FORMAT_SESSIONS;
2150: break;
2151: case 'W':
2152: modifiers |= FORMAT_WINDOWS;
2153: break;
2154: case 'P':
2155: modifiers |= FORMAT_PANES;
2156: break;
1.169 nicm 2157: }
2158: } else if (fm->size == 2) {
2159: if (strcmp(fm->modifier, "||") == 0 ||
2160: strcmp(fm->modifier, "&&") == 0 ||
2161: strcmp(fm->modifier, "==") == 0 ||
1.195 nicm 2162: strcmp(fm->modifier, "!=") == 0 ||
2163: strcmp(fm->modifier, ">=") == 0 ||
2164: strcmp(fm->modifier, "<=") == 0)
1.169 nicm 2165: cmp = fm;
1.98 nicm 2166: }
1.30 nicm 2167: }
2168:
1.155 nicm 2169: /* Is this a literal string? */
1.169 nicm 2170: if (modifiers & FORMAT_LITERAL) {
1.155 nicm 2171: value = xstrdup(copy);
2172: goto done;
2173: }
2174:
1.172 nicm 2175: /* Is this a loop, comparison or condition? */
2176: if (modifiers & FORMAT_SESSIONS) {
2177: value = format_loop_sessions(ft, copy);
2178: if (value == NULL)
2179: goto fail;
2180: } else if (modifiers & FORMAT_WINDOWS) {
2181: value = format_loop_windows(ft, copy);
2182: if (value == NULL)
2183: goto fail;
2184: } else if (modifiers & FORMAT_PANES) {
2185: value = format_loop_panes(ft, copy);
2186: if (value == NULL)
2187: goto fail;
2188: } else if (search != NULL) {
1.140 nicm 2189: /* Search in pane. */
1.207 nicm 2190: new = format_expand(ft, copy);
1.179 nicm 2191: if (wp == NULL) {
1.207 nicm 2192: format_log(ft, "search '%s' but no pane", new);
1.140 nicm 2193: value = xstrdup("0");
1.179 nicm 2194: } else {
1.207 nicm 2195: format_log(ft, "search '%s' pane %%%u", new, wp->id);
2196: value = format_search(fm, wp, new);
1.179 nicm 2197: }
1.207 nicm 2198: free(new);
1.169 nicm 2199: } else if (cmp != NULL) {
2200: /* Comparison of left and right. */
1.179 nicm 2201: if (format_choose(ft, copy, &left, &right, 1) != 0) {
2202: format_log(ft, "compare %s syntax error: %s",
2203: cmp->modifier, copy);
1.114 nicm 2204: goto fail;
1.179 nicm 2205: }
2206: format_log(ft, "compare %s left is: %s", cmp->modifier, left);
2207: format_log(ft, "compare %s right is: %s", cmp->modifier, right);
1.169 nicm 2208:
2209: 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 (format_true(left) && format_true(right))
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");
2224: } else if (strcmp(cmp->modifier, "!=") == 0) {
2225: if (strcmp(left, right) != 0)
2226: value = xstrdup("1");
2227: else
2228: value = xstrdup("0");
1.195 nicm 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");
2244: } else if (strcmp(cmp->modifier, ">=") == 0) {
2245: if (strcmp(left, right) >= 0)
2246: value = xstrdup("1");
2247: else
2248: value = xstrdup("0");
1.202 nicm 2249: } else if (strcmp(cmp->modifier, "m") == 0)
1.204 nicm 2250: value = format_match(cmp, left, right);
1.169 nicm 2251:
1.114 nicm 2252: free(right);
2253: free(left);
2254: } else if (*copy == '?') {
2255: /* Conditional: check first and choose second or third. */
1.169 nicm 2256: cp = format_skip(copy + 1, ",");
1.179 nicm 2257: if (cp == NULL) {
2258: format_log(ft, "condition syntax error: %s", copy + 1);
1.1 nicm 2259: goto fail;
1.179 nicm 2260: }
1.169 nicm 2261: condition = xstrndup(copy + 1, cp - (copy + 1));
1.179 nicm 2262: format_log(ft, "condition is: %s", condition);
1.1 nicm 2263:
1.254 nicm 2264: found = format_find(ft, condition, modifiers, time_format);
1.156 nicm 2265: if (found == NULL) {
2266: /*
1.169 nicm 2267: * If the condition not found, try to expand it. If
1.156 nicm 2268: * the expansion doesn't have any effect, then assume
2269: * false.
2270: */
1.169 nicm 2271: found = format_expand(ft, condition);
2272: if (strcmp(found, condition) == 0) {
1.156 nicm 2273: free(found);
2274: found = xstrdup("");
1.179 nicm 2275: format_log(ft, "condition '%s' found: %s",
2276: condition, found);
2277: } else {
2278: format_log(ft,
2279: "condition '%s' not found; assuming false",
2280: condition);
1.156 nicm 2281: }
1.179 nicm 2282: } else
2283: format_log(ft, "condition '%s' found", condition);
1.169 nicm 2284:
2285: if (format_choose(ft, cp + 1, &left, &right, 0) != 0) {
1.179 nicm 2286: format_log(ft, "condition '%s' syntax error: %s",
2287: condition, cp + 1);
1.162 nicm 2288: free(found);
1.89 nicm 2289: goto fail;
1.162 nicm 2290: }
1.179 nicm 2291: if (format_true(found)) {
2292: format_log(ft, "condition '%s' is true", condition);
1.114 nicm 2293: value = format_expand(ft, left);
1.179 nicm 2294: } else {
2295: format_log(ft, "condition '%s' is false", condition);
1.114 nicm 2296: value = format_expand(ft, right);
1.179 nicm 2297: }
1.169 nicm 2298: free(right);
2299: free(left);
2300:
1.179 nicm 2301: free(condition);
1.98 nicm 2302: free(found);
1.226 nicm 2303: } else if (mexp != NULL) {
2304: value = format_replace_expression(mexp, ft, copy);
2305: if (value == NULL)
2306: value = xstrdup("");
1.1 nicm 2307: } else {
1.260 ! nicm 2308: if (strstr(copy, "#{") != 0) {
! 2309: format_log(ft, "expanding inner format '%s'", copy);
! 2310: value = format_expand(ft, copy);
! 2311: } else {
! 2312: value = format_find(ft, copy, modifiers, time_format);
! 2313: if (value == NULL) {
! 2314: format_log(ft, "format '%s' not found", copy);
! 2315: value = xstrdup("");
! 2316: } else
! 2317: format_log(ft, "format '%s' found: %s", copy, value);
! 2318: }
1.170 nicm 2319: }
2320:
1.171 nicm 2321: done:
1.170 nicm 2322: /* Expand again if required. */
2323: if (modifiers & FORMAT_EXPAND) {
2324: new = format_expand(ft, value);
1.175 nicm 2325: free(value);
2326: value = new;
1.260 ! nicm 2327: } else if (modifiers & FORMAT_EXPANDTIME) {
1.177 nicm 2328: new = format_expand_time(ft, value);
1.170 nicm 2329: free(value);
2330: value = new;
1.98 nicm 2331: }
2332:
2333: /* Perform substitution if any. */
1.216 nicm 2334: for (i = 0; i < nsub; i++) {
2335: left = format_expand(ft, sub[i]->argv[0]);
2336: right = format_expand(ft, sub[i]->argv[1]);
2337: new = format_sub(sub[i], value, left, right);
1.207 nicm 2338: format_log(ft, "substitute '%s' to '%s': %s", left, right, new);
1.98 nicm 2339: free(value);
1.169 nicm 2340: value = new;
1.207 nicm 2341: free(right);
2342: free(left);
1.1 nicm 2343: }
2344:
1.30 nicm 2345: /* Truncate the value if needed. */
1.105 nicm 2346: if (limit > 0) {
1.185 nicm 2347: new = format_trim_left(value, limit);
1.196 nicm 2348: if (marker != NULL && strcmp(new, value) != 0) {
2349: free(value);
2350: xasprintf(&value, "%s%s", new, marker);
2351: } else {
2352: free(value);
2353: value = new;
2354: }
2355: format_log(ft, "applied length limit %d: %s", limit, value);
1.105 nicm 2356: } else if (limit < 0) {
1.185 nicm 2357: new = format_trim_right(value, -limit);
1.196 nicm 2358: if (marker != NULL && strcmp(new, value) != 0) {
2359: free(value);
2360: xasprintf(&value, "%s%s", marker, new);
2361: } else {
2362: free(value);
2363: value = new;
2364: }
2365: format_log(ft, "applied length limit %d: %s", limit, value);
1.217 nicm 2366: }
2367:
2368: /* Pad the value if needed. */
2369: if (width > 0) {
2370: new = utf8_padcstr(value, width);
2371: free(value);
2372: value = new;
2373: format_log(ft, "applied padding width %d: %s", width, value);
2374: } else if (width < 0) {
2375: new = utf8_rpadcstr(value, -width);
2376: free(value);
2377: value = new;
2378: format_log(ft, "applied padding width %d: %s", width, value);
1.260 ! nicm 2379: }
! 2380:
! 2381: /* Replace with the length if needed. */
! 2382: if (modifiers & FORMAT_LENGTH) {
! 2383: xasprintf(&new, "%zu", strlen(value));
! 2384: free(value);
! 2385: value = new;
! 2386: format_log(ft, "replacing with length: %s", new);
1.44 nicm 2387: }
1.30 nicm 2388:
1.1 nicm 2389: /* Expand the buffer and copy in the value. */
1.98 nicm 2390: valuelen = strlen(value);
1.1 nicm 2391: while (*len - *off < valuelen + 1) {
1.50 nicm 2392: *buf = xreallocarray(*buf, 2, *len);
1.1 nicm 2393: *len *= 2;
2394: }
2395: memcpy(*buf + *off, value, valuelen);
2396: *off += valuelen;
2397:
1.179 nicm 2398: format_log(ft, "replaced '%s' with '%s'", copy0, value);
1.98 nicm 2399: free(value);
1.179 nicm 2400:
1.216 nicm 2401: free(sub);
1.169 nicm 2402: format_free_modifiers(list, count);
1.30 nicm 2403: free(copy0);
1.1 nicm 2404: return (0);
2405:
2406: fail:
1.179 nicm 2407: format_log(ft, "failed %s", copy0);
1.216 nicm 2408:
2409: free(sub);
1.169 nicm 2410: format_free_modifiers(list, count);
1.30 nicm 2411: free(copy0);
1.1 nicm 2412: return (-1);
2413: }
2414:
2415: /* Expand keys in a template. */
1.179 nicm 2416: static char *
2417: format_expand1(struct format_tree *ft, const char *fmt, int time)
1.1 nicm 2418: {
1.152 nicm 2419: char *buf, *out, *name;
1.179 nicm 2420: const char *ptr, *s;
1.86 nicm 2421: size_t off, len, n, outlen;
1.31 nicm 2422: int ch, brackets;
1.179 nicm 2423: struct tm *tm;
2424: char expanded[8192];
1.58 nicm 2425:
1.179 nicm 2426: if (fmt == NULL || *fmt == '\0')
1.58 nicm 2427: return (xstrdup(""));
1.1 nicm 2428:
1.178 nicm 2429: if (ft->loop == FORMAT_LOOP_LIMIT)
2430: return (xstrdup(""));
2431: ft->loop++;
2432:
1.179 nicm 2433: format_log(ft, "expanding format: %s", fmt);
2434:
2435: if (time) {
2436: tm = localtime(&ft->time);
2437: if (strftime(expanded, sizeof expanded, fmt, tm) == 0) {
2438: format_log(ft, "format is too long");
2439: return (xstrdup(""));
2440: }
2441: if (format_logging(ft) && strcmp(expanded, fmt) != 0)
2442: format_log(ft, "after time expanded: %s", expanded);
2443: fmt = expanded;
2444: }
2445:
1.1 nicm 2446: len = 64;
2447: buf = xmalloc(len);
2448: off = 0;
2449:
2450: while (*fmt != '\0') {
2451: if (*fmt != '#') {
2452: while (len - off < 2) {
1.50 nicm 2453: buf = xreallocarray(buf, 2, len);
1.1 nicm 2454: len *= 2;
2455: }
2456: buf[off++] = *fmt++;
2457: continue;
2458: }
2459: fmt++;
2460:
1.179 nicm 2461: ch = (u_char)*fmt++;
1.1 nicm 2462: switch (ch) {
1.68 nicm 2463: case '(':
2464: brackets = 1;
2465: for (ptr = fmt; *ptr != '\0'; ptr++) {
2466: if (*ptr == '(')
2467: brackets++;
2468: if (*ptr == ')' && --brackets == 0)
2469: break;
2470: }
2471: if (*ptr != ')' || brackets != 0)
2472: break;
2473: n = ptr - fmt;
2474:
1.179 nicm 2475: name = xstrndup(fmt, n);
2476: format_log(ft, "found #(): %s", name);
2477:
2478: if (ft->flags & FORMAT_NOJOBS) {
1.114 nicm 2479: out = xstrdup("");
1.179 nicm 2480: format_log(ft, "#() is disabled");
2481: } else {
1.152 nicm 2482: out = format_job_get(ft, name);
1.179 nicm 2483: format_log(ft, "#() result: %s", out);
1.152 nicm 2484: }
1.179 nicm 2485: free(name);
2486:
1.86 nicm 2487: outlen = strlen(out);
2488: while (len - off < outlen + 1) {
1.68 nicm 2489: buf = xreallocarray(buf, 2, len);
2490: len *= 2;
2491: }
1.86 nicm 2492: memcpy(buf + off, out, outlen);
2493: off += outlen;
2494:
2495: free(out);
1.68 nicm 2496:
2497: fmt += n + 1;
2498: continue;
1.1 nicm 2499: case '{':
1.169 nicm 2500: ptr = format_skip((char *)fmt - 2, "}");
1.155 nicm 2501: if (ptr == NULL)
1.1 nicm 2502: break;
2503: n = ptr - fmt;
2504:
1.179 nicm 2505: format_log(ft, "found #{}: %.*s", (int)n, fmt);
1.1 nicm 2506: if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
2507: break;
2508: fmt += n + 1;
1.40 nicm 2509: continue;
1.155 nicm 2510: case '}':
1.40 nicm 2511: case '#':
1.155 nicm 2512: case ',':
1.179 nicm 2513: format_log(ft, "found #%c", ch);
1.40 nicm 2514: while (len - off < 2) {
1.50 nicm 2515: buf = xreallocarray(buf, 2, len);
1.40 nicm 2516: len *= 2;
2517: }
1.155 nicm 2518: buf[off++] = ch;
1.1 nicm 2519: continue;
2520: default:
1.25 nicm 2521: s = NULL;
2522: if (ch >= 'A' && ch <= 'Z')
2523: s = format_upper[ch - 'A'];
2524: else if (ch >= 'a' && ch <= 'z')
2525: s = format_lower[ch - 'a'];
2526: if (s == NULL) {
2527: while (len - off < 3) {
1.50 nicm 2528: buf = xreallocarray(buf, 2, len);
1.25 nicm 2529: len *= 2;
1.1 nicm 2530: }
1.25 nicm 2531: buf[off++] = '#';
2532: buf[off++] = ch;
2533: continue;
1.1 nicm 2534: }
1.25 nicm 2535: n = strlen(s);
1.179 nicm 2536: format_log(ft, "found #%c: %s", ch, s);
1.25 nicm 2537: if (format_replace(ft, s, n, &buf, &len, &off) != 0)
2538: break;
1.1 nicm 2539: continue;
2540: }
2541:
2542: break;
2543: }
2544: buf[off] = '\0';
2545:
1.179 nicm 2546: format_log(ft, "result is: %s", buf);
2547: ft->loop--;
1.178 nicm 2548:
1.1 nicm 2549: return (buf);
1.179 nicm 2550: }
2551:
2552: /* Expand keys in a template, passing through strftime first. */
2553: char *
2554: format_expand_time(struct format_tree *ft, const char *fmt)
2555: {
2556: return (format_expand1(ft, fmt, 1));
2557: }
2558:
2559: /* Expand keys in a template. */
2560: char *
2561: format_expand(struct format_tree *ft, const char *fmt)
2562: {
2563: return (format_expand1(ft, fmt, 0));
1.123 nicm 2564: }
2565:
2566: /* Expand a single string. */
2567: char *
2568: format_single(struct cmdq_item *item, const char *fmt, struct client *c,
2569: struct session *s, struct winlink *wl, struct window_pane *wp)
2570: {
2571: struct format_tree *ft;
2572: char *expanded;
2573:
1.250 nicm 2574: ft = format_create_defaults(item, c, s, wl, wp);
2575: expanded = format_expand(ft, fmt);
2576: format_free(ft);
2577: return (expanded);
2578: }
2579:
2580: /* Expand a single string using state. */
2581: char *
2582: format_single_from_state(struct cmdq_item *item, const char *fmt,
2583: struct client *c, struct cmd_find_state *fs)
2584: {
2585: return (format_single(item, fmt, c, fs->s, fs->wl, fs->wp));
2586: }
2587:
2588: /* Expand a single string using target. */
2589: char *
2590: format_single_from_target(struct cmdq_item *item, const char *fmt)
2591: {
2592: struct client *tc = cmdq_get_target_client(item);
2593:
2594: return (format_single_from_state(item, fmt, tc, cmdq_get_target(item)));
2595: }
2596:
2597: /* Create and add defaults. */
2598: struct format_tree *
2599: format_create_defaults(struct cmdq_item *item, struct client *c,
2600: struct session *s, struct winlink *wl, struct window_pane *wp)
2601: {
2602: struct format_tree *ft;
2603:
1.131 nicm 2604: if (item != NULL)
1.237 nicm 2605: ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
1.131 nicm 2606: else
2607: ft = format_create(NULL, item, FORMAT_NONE, 0);
1.123 nicm 2608: format_defaults(ft, c, s, wl, wp);
1.250 nicm 2609: return (ft);
2610: }
1.123 nicm 2611:
1.250 nicm 2612: /* Create and add defaults using state. */
2613: struct format_tree *
2614: format_create_from_state(struct cmdq_item *item, struct client *c,
2615: struct cmd_find_state *fs)
2616: {
2617: return (format_create_defaults(item, c, fs->s, fs->wl, fs->wp));
1.237 nicm 2618: }
2619:
1.250 nicm 2620: /* Create and add defaults using target. */
2621: struct format_tree *
2622: format_create_from_target(struct cmdq_item *item)
1.237 nicm 2623: {
1.250 nicm 2624: struct client *tc = cmdq_get_target_client(item);
1.237 nicm 2625:
1.250 nicm 2626: return (format_create_from_state(item, tc, cmdq_get_target(item)));
1.1 nicm 2627: }
2628:
1.57 nicm 2629: /* Set defaults for any of arguments that are not NULL. */
2630: void
2631: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
2632: struct winlink *wl, struct window_pane *wp)
2633: {
1.230 nicm 2634: struct paste_buffer *pb;
2635:
1.205 nicm 2636: if (c != NULL && c->name != NULL)
1.187 nicm 2637: log_debug("%s: c=%s", __func__, c->name);
2638: else
1.205 nicm 2639: log_debug("%s: c=none", __func__);
1.187 nicm 2640: if (s != NULL)
2641: log_debug("%s: s=$%u", __func__, s->id);
2642: else
2643: log_debug("%s: s=none", __func__);
2644: if (wl != NULL)
1.251 nicm 2645: log_debug("%s: wl=%u", __func__, wl->idx);
1.187 nicm 2646: else
2647: log_debug("%s: wl=none", __func__);
2648: if (wp != NULL)
2649: log_debug("%s: wp=%%%u", __func__, wp->id);
2650: else
2651: log_debug("%s: wp=none", __func__);
2652:
1.154 nicm 2653: if (c != NULL && s != NULL && c->session != s)
2654: log_debug("%s: session does not match", __func__);
2655:
1.146 nicm 2656: format_add(ft, "session_format", "%d", s != NULL);
2657: format_add(ft, "window_format", "%d", wl != NULL);
2658: format_add(ft, "pane_format", "%d", wp != NULL);
2659:
1.57 nicm 2660: if (s == NULL && c != NULL)
2661: s = c->session;
2662: if (wl == NULL && s != NULL)
2663: wl = s->curw;
2664: if (wp == NULL && wl != NULL)
2665: wp = wl->window->active;
2666:
2667: if (c != NULL)
2668: format_defaults_client(ft, c);
2669: if (s != NULL)
2670: format_defaults_session(ft, s);
1.129 nicm 2671: if (wl != NULL)
2672: format_defaults_winlink(ft, wl);
1.57 nicm 2673: if (wp != NULL)
2674: format_defaults_pane(ft, wp);
1.230 nicm 2675:
2676: pb = paste_get_top (NULL);
2677: if (pb != NULL)
2678: format_defaults_paste_buffer(ft, pb);
1.57 nicm 2679: }
2680:
1.1 nicm 2681: /* Set default format keys for a session. */
1.108 nicm 2682: static void
1.57 nicm 2683: format_defaults_session(struct format_tree *ft, struct session *s)
1.1 nicm 2684: {
2685: struct session_group *sg;
2686:
1.54 nicm 2687: ft->s = s;
2688:
1.1 nicm 2689: format_add(ft, "session_name", "%s", s->name);
1.232 nicm 2690: format_add(ft, "session_path", "%s", s->cwd);
1.1 nicm 2691: format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
1.23 nicm 2692: format_add(ft, "session_id", "$%u", s->id);
1.1 nicm 2693:
1.122 nicm 2694: sg = session_group_contains(s);
1.1 nicm 2695: format_add(ft, "session_grouped", "%d", sg != NULL);
1.148 nicm 2696: if (sg != NULL) {
1.122 nicm 2697: format_add(ft, "session_group", "%s", sg->name);
1.148 nicm 2698: format_add(ft, "session_group_size", "%u",
2699: session_group_count (sg));
1.221 nicm 2700: format_add(ft, "session_group_attached", "%u",
2701: session_group_attached_count (sg));
2702: format_add(ft, "session_group_many_attached", "%u",
2703: session_group_attached_count (sg) > 1);
1.150 nicm 2704: format_add_cb(ft, "session_group_list",
2705: format_cb_session_group_list);
1.221 nicm 2706: format_add_cb(ft, "session_group_attached_list",
2707: format_cb_session_group_attached_list);
1.148 nicm 2708: }
1.1 nicm 2709:
1.87 nicm 2710: format_add_tv(ft, "session_created", &s->creation_time);
2711: format_add_tv(ft, "session_last_attached", &s->last_attached_time);
2712: format_add_tv(ft, "session_activity", &s->activity_time);
1.1 nicm 2713:
1.41 nicm 2714: format_add(ft, "session_attached", "%u", s->attached);
1.59 nicm 2715: format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.221 nicm 2716: format_add_cb(ft, "session_attached_list",
2717: format_cb_session_attached_list);
1.66 nicm 2718:
1.80 nicm 2719: format_add_cb(ft, "session_alerts", format_cb_session_alerts);
1.133 nicm 2720: format_add_cb(ft, "session_stack", format_cb_session_stack);
1.247 nicm 2721:
2722: if (server_check_marked() && marked_pane.s == s)
2723: format_add(ft, "session_marked", "1");
2724: else
2725: format_add(ft, "session_marked", "0");
1.3 nicm 2726: }
2727:
2728: /* Set default format keys for a client. */
1.108 nicm 2729: static void
1.57 nicm 2730: format_defaults_client(struct format_tree *ft, struct client *c)
1.3 nicm 2731: {
1.60 nicm 2732: struct session *s;
1.103 nicm 2733: const char *name;
1.115 nicm 2734: struct tty *tty = &c->tty;
1.3 nicm 2735:
1.54 nicm 2736: if (ft->s == NULL)
2737: ft->s = c->session;
1.164 nicm 2738: ft->c = c;
1.54 nicm 2739:
1.124 nicm 2740: format_add(ft, "client_name", "%s", c->name);
1.72 nicm 2741: format_add(ft, "client_pid", "%ld", (long) c->pid);
1.115 nicm 2742: format_add(ft, "client_height", "%u", tty->sy);
2743: format_add(ft, "client_width", "%u", tty->sx);
1.218 nicm 2744: format_add(ft, "client_cell_width", "%u", tty->xpixel);
2745: format_add(ft, "client_cell_height", "%u", tty->ypixel);
1.124 nicm 2746: format_add(ft, "client_tty", "%s", c->ttyname);
1.75 nicm 2747: format_add(ft, "client_control_mode", "%d",
2748: !!(c->flags & CLIENT_CONTROL));
1.3 nicm 2749:
1.246 nicm 2750: format_add(ft, "client_termname", "%s", c->term_name);
2751: format_add(ft, "client_termfeatures", "%s",
2752: tty_get_features(c->term_features));
1.249 nicm 2753: if (c->term_type != NULL)
2754: format_add(ft, "client_termtype", "%s", c->term_type);
1.115 nicm 2755:
1.87 nicm 2756: format_add_tv(ft, "client_created", &c->creation_time);
2757: format_add_tv(ft, "client_activity", &c->activity_time);
1.126 nicm 2758:
2759: format_add(ft, "client_written", "%zu", c->written);
2760: format_add(ft, "client_discarded", "%zu", c->discarded);
1.14 nicm 2761:
1.103 nicm 2762: name = server_client_get_key_table(c);
2763: if (strcmp(c->keytable->name, name) == 0)
1.61 nicm 2764: format_add(ft, "client_prefix", "%d", 0);
2765: else
2766: format_add(ft, "client_prefix", "%d", 1);
2767: format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3 nicm 2768:
1.246 nicm 2769: if (c->flags & CLIENT_UTF8)
1.3 nicm 2770: format_add(ft, "client_utf8", "%d", 1);
2771: else
2772: format_add(ft, "client_utf8", "%d", 0);
2773: if (c->flags & CLIENT_READONLY)
2774: format_add(ft, "client_readonly", "%d", 1);
2775: else
2776: format_add(ft, "client_readonly", "%d", 0);
1.252 nicm 2777: format_add(ft, "client_flags", "%s", server_client_get_flags(c));
1.15 nicm 2778:
2779: s = c->session;
2780: if (s != NULL)
2781: format_add(ft, "client_session", "%s", s->name);
2782: s = c->last_session;
2783: if (s != NULL && session_alive(s))
2784: format_add(ft, "client_last_session", "%s", s->name);
1.1 nicm 2785: }
2786:
1.32 nicm 2787: /* Set default format keys for a window. */
2788: void
1.57 nicm 2789: format_defaults_window(struct format_tree *ft, struct window *w)
1.32 nicm 2790: {
1.54 nicm 2791: ft->w = w;
2792:
1.87 nicm 2793: format_add_tv(ft, "window_activity", &w->activity_time);
1.32 nicm 2794: format_add(ft, "window_id", "@%u", w->id);
2795: format_add(ft, "window_name", "%s", w->name);
2796: format_add(ft, "window_width", "%u", w->sx);
2797: format_add(ft, "window_height", "%u", w->sy);
1.219 nicm 2798: format_add(ft, "window_cell_width", "%u", w->xpixel);
2799: format_add(ft, "window_cell_height", "%u", w->ypixel);
1.80 nicm 2800: format_add_cb(ft, "window_layout", format_cb_window_layout);
1.96 nicm 2801: format_add_cb(ft, "window_visible_layout",
2802: format_cb_window_visible_layout);
1.32 nicm 2803: format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59 nicm 2804: format_add(ft, "window_zoomed_flag", "%d",
1.53 nicm 2805: !!(w->flags & WINDOW_ZOOMED));
1.32 nicm 2806: }
2807:
1.1 nicm 2808: /* Set default format keys for a winlink. */
1.108 nicm 2809: static void
1.129 nicm 2810: format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
1.1 nicm 2811: {
1.164 nicm 2812: struct client *c = ft->c;
1.129 nicm 2813: struct session *s = wl->session;
1.1 nicm 2814: struct window *w = wl->window;
1.164 nicm 2815: int flag;
2816: u_int ox, oy, sx, sy;
1.1 nicm 2817:
1.54 nicm 2818: if (ft->w == NULL)
1.251 nicm 2819: format_defaults_window(ft, w);
1.133 nicm 2820: ft->wl = wl;
1.54 nicm 2821:
1.164 nicm 2822: if (c != NULL) {
2823: flag = tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
2824: format_add(ft, "window_bigger", "%d", flag);
2825: if (flag) {
2826: format_add(ft, "window_offset_x", "%u", ox);
2827: format_add(ft, "window_offset_y", "%u", oy);
2828: }
2829: }
2830:
1.1 nicm 2831: format_add(ft, "window_index", "%d", wl->idx);
1.133 nicm 2832: format_add_cb(ft, "window_stack_index", format_cb_window_stack_index);
1.129 nicm 2833: format_add(ft, "window_flags", "%s", window_printable_flags(wl));
1.1 nicm 2834: format_add(ft, "window_active", "%d", wl == s->curw);
1.221 nicm 2835: format_add_cb(ft, "window_active_sessions",
2836: format_cb_window_active_sessions);
2837: format_add_cb(ft, "window_active_sessions_list",
2838: format_cb_window_active_sessions_list);
2839: format_add_cb(ft, "window_active_clients",
2840: format_cb_window_active_clients);
2841: format_add_cb(ft, "window_active_clients_list",
2842: format_cb_window_active_clients_list);
1.176 nicm 2843:
2844: format_add(ft, "window_start_flag", "%d",
2845: !!(wl == RB_MIN(winlinks, &s->windows)));
2846: format_add(ft, "window_end_flag", "%d",
2847: !!(wl == RB_MAX(winlinks, &s->windows)));
1.210 nicm 2848:
2849: if (server_check_marked() && marked_pane.wl == wl)
2850: format_add(ft, "window_marked_flag", "1");
2851: else
2852: format_add(ft, "window_marked_flag", "0");
1.29 nicm 2853:
1.59 nicm 2854: format_add(ft, "window_bell_flag", "%d",
1.29 nicm 2855: !!(wl->flags & WINLINK_BELL));
1.59 nicm 2856: format_add(ft, "window_activity_flag", "%d",
1.29 nicm 2857: !!(wl->flags & WINLINK_ACTIVITY));
1.59 nicm 2858: format_add(ft, "window_silence_flag", "%d",
1.29 nicm 2859: !!(wl->flags & WINLINK_SILENCE));
1.59 nicm 2860: format_add(ft, "window_last_flag", "%d",
1.49 nicm 2861: !!(wl == TAILQ_FIRST(&s->lastw)));
1.64 nicm 2862: format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.221 nicm 2863:
2864: format_add_cb(ft, "window_linked_sessions_list",
2865: format_cb_window_linked_sessions_list);
2866: format_add(ft, "window_linked_sessions", "%u",
2867: wl->window->references);
1.1 nicm 2868: }
2869:
2870: /* Set default format keys for a window pane. */
2871: void
1.57 nicm 2872: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1 nicm 2873: {
1.168 nicm 2874: struct window *w = wp->window;
2875: struct grid *gd = wp->base.grid;
2876: int status = wp->status;
2877: u_int idx;
2878: struct window_mode_entry *wme;
1.54 nicm 2879:
2880: if (ft->w == NULL)
1.251 nicm 2881: format_defaults_window(ft, w);
1.79 nicm 2882: ft->wp = wp;
1.1 nicm 2883:
1.16 nicm 2884: format_add(ft, "history_size", "%u", gd->hsize);
2885: format_add(ft, "history_limit", "%u", gd->hlimit);
1.80 nicm 2886: format_add_cb(ft, "history_bytes", format_cb_history_bytes);
1.256 nicm 2887: format_add_cb(ft, "history_all_bytes", format_cb_history_all_bytes);
1.243 nicm 2888:
2889: format_add(ft, "pane_written", "%zu", wp->written);
2890: format_add(ft, "pane_skipped", "%zu", wp->skipped);
1.1 nicm 2891:
1.4 nicm 2892: if (window_pane_index(wp, &idx) != 0)
2893: fatalx("index not found");
1.16 nicm 2894: format_add(ft, "pane_index", "%u", idx);
1.4 nicm 2895:
1.1 nicm 2896: format_add(ft, "pane_width", "%u", wp->sx);
2897: format_add(ft, "pane_height", "%u", wp->sy);
2898: format_add(ft, "pane_title", "%s", wp->base.title);
1.215 nicm 2899: if (wp->base.path != NULL)
2900: format_add(ft, "pane_path", "%s", wp->base.path);
1.1 nicm 2901: format_add(ft, "pane_id", "%%%u", wp->id);
1.159 nicm 2902: format_add(ft, "pane_active", "%d", wp == w->active);
1.55 nicm 2903: format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
1.143 nicm 2904: format_add(ft, "pane_pipe", "%d", wp->pipe_fd != -1);
1.55 nicm 2905:
1.147 nicm 2906: if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(status))
1.55 nicm 2907: format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.190 nicm 2908: if (~wp->flags & PANE_EMPTY)
2909: format_add(ft, "pane_dead", "%d", wp->fd == -1);
2910: else
2911: format_add(ft, "pane_dead", "0");
1.191 nicm 2912:
2913: if (server_check_marked() && marked_pane.wp == wp)
2914: format_add(ft, "pane_marked", "1");
2915: else
2916: format_add(ft, "pane_marked", "0");
2917: format_add(ft, "pane_marked_set", "%d", server_check_marked());
1.47 nicm 2918:
1.164 nicm 2919: format_add(ft, "pane_left", "%u", wp->xoff);
2920: format_add(ft, "pane_top", "%u", wp->yoff);
2921: format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
2922: format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
2923: format_add(ft, "pane_at_left", "%d", wp->xoff == 0);
1.225 nicm 2924: format_add_cb(ft, "pane_at_top", format_cb_pane_at_top);
1.164 nicm 2925: format_add(ft, "pane_at_right", "%d", wp->xoff + wp->sx == w->sx);
1.225 nicm 2926: format_add_cb(ft, "pane_at_bottom", format_cb_pane_at_bottom);
1.16 nicm 2927:
1.168 nicm 2928: wme = TAILQ_FIRST(&wp->modes);
2929: if (wme != NULL) {
2930: format_add(ft, "pane_mode", "%s", wme->mode->name);
2931: if (wme->mode->formats != NULL)
2932: wme->mode->formats(wme, ft);
2933: }
2934: format_add_cb(ft, "pane_in_mode", format_cb_pane_in_mode);
1.134 nicm 2935:
1.26 nicm 2936: format_add(ft, "pane_synchronized", "%d",
1.159 nicm 2937: !!options_get_number(w->options, "synchronize-panes"));
1.135 nicm 2938: if (wp->searchstr != NULL)
2939: format_add(ft, "pane_search_string", "%s", wp->searchstr);
1.16 nicm 2940:
1.71 nicm 2941: format_add(ft, "pane_tty", "%s", wp->tty);
1.16 nicm 2942: format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.79 nicm 2943: format_add_cb(ft, "pane_start_command", format_cb_start_command);
2944: format_add_cb(ft, "pane_current_command", format_cb_current_command);
1.233 nicm 2945: format_add_cb(ft, "pane_current_path", format_cb_current_path);
1.16 nicm 2946:
1.59 nicm 2947: format_add(ft, "cursor_x", "%u", wp->base.cx);
2948: format_add(ft, "cursor_y", "%u", wp->base.cy);
1.186 nicm 2949: format_add_cb(ft, "cursor_character", format_cb_cursor_character);
2950:
1.59 nicm 2951: format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
2952: format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.16 nicm 2953:
1.231 nicm 2954: format_add(ft, "alternate_on", "%d", wp->base.saved_grid != NULL);
2955: if (wp->base.saved_cx != UINT_MAX)
2956: format_add(ft, "alternate_saved_x", "%u", wp->base.saved_cx);
2957: if (wp->base.saved_cy != UINT_MAX)
2958: format_add(ft, "alternate_saved_y", "%u", wp->base.saved_cy);
1.16 nicm 2959:
2960: format_add(ft, "cursor_flag", "%d",
2961: !!(wp->base.mode & MODE_CURSOR));
2962: format_add(ft, "insert_flag", "%d",
2963: !!(wp->base.mode & MODE_INSERT));
2964: format_add(ft, "keypad_cursor_flag", "%d",
2965: !!(wp->base.mode & MODE_KCURSOR));
2966: format_add(ft, "keypad_flag", "%d",
2967: !!(wp->base.mode & MODE_KKEYPAD));
2968: format_add(ft, "wrap_flag", "%d",
2969: !!(wp->base.mode & MODE_WRAP));
1.208 nicm 2970: format_add(ft, "origin_flag", "%d",
2971: !!(wp->base.mode & MODE_ORIGIN));
1.16 nicm 2972:
1.62 nicm 2973: format_add(ft, "mouse_any_flag", "%d",
1.119 nicm 2974: !!(wp->base.mode & ALL_MOUSE_MODES));
1.16 nicm 2975: format_add(ft, "mouse_standard_flag", "%d",
2976: !!(wp->base.mode & MODE_MOUSE_STANDARD));
2977: format_add(ft, "mouse_button_flag", "%d",
2978: !!(wp->base.mode & MODE_MOUSE_BUTTON));
1.119 nicm 2979: format_add(ft, "mouse_all_flag", "%d",
2980: !!(wp->base.mode & MODE_MOUSE_ALL));
1.208 nicm 2981: format_add(ft, "mouse_utf8_flag", "%d",
2982: !!(wp->base.mode & MODE_MOUSE_UTF8));
2983: format_add(ft, "mouse_sgr_flag", "%d",
2984: !!(wp->base.mode & MODE_MOUSE_SGR));
1.19 nicm 2985:
1.80 nicm 2986: format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
1.8 nicm 2987: }
2988:
1.19 nicm 2989: /* Set default format keys for paste buffer. */
1.8 nicm 2990: void
1.94 nicm 2991: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
1.8 nicm 2992: {
1.146 nicm 2993: struct timeval tv;
2994: size_t size;
2995: char *s;
2996:
2997: timerclear(&tv);
2998: tv.tv_sec = paste_buffer_created(pb);
2999: paste_buffer_data(pb, &size);
1.8 nicm 3000:
1.146 nicm 3001: format_add(ft, "buffer_size", "%zu", size);
1.81 nicm 3002: format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
1.146 nicm 3003: format_add_tv(ft, "buffer_created", &tv);
1.8 nicm 3004:
1.94 nicm 3005: s = paste_make_sample(pb);
1.42 nicm 3006: format_add(ft, "buffer_sample", "%s", s);
3007: free(s);
1.1 nicm 3008: }