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