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