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