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