Add length limit operator for formats.
[tmux-openbsd.git] / format.c
blob72e42ba0e53e0344c09c09468839c5e5aad0e533
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2011 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <netdb.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <unistd.h>
30 #include "tmux.h"
33 * Build a list of key-value pairs and use them to expand #{key} entries in a
34 * string.
37 int format_replace(struct format_tree *, const char *, size_t, char **,
38 size_t *, size_t *);
39 void format_window_pane_tabs(struct format_tree *, struct window_pane *);
41 /* Format key-value replacement entry. */
42 RB_GENERATE(format_tree, format_entry, entry, format_cmp);
44 /* Format tree comparison function. */
45 int
46 format_cmp(struct format_entry *fe1, struct format_entry *fe2)
48 return (strcmp(fe1->key, fe2->key));
51 /* Single-character uppercase aliases. */
52 const char *format_upper[] = {
53 NULL, /* A */
54 NULL, /* B */
55 NULL, /* C */
56 "pane_id", /* D */
57 NULL, /* E */
58 "window_flags", /* F */
59 NULL, /* G */
60 "host", /* H */
61 "window_index", /* I */
62 NULL, /* J */
63 NULL, /* K */
64 NULL, /* L */
65 NULL, /* M */
66 NULL, /* N */
67 NULL, /* O */
68 "pane_index", /* P */
69 NULL, /* Q */
70 NULL, /* R */
71 "session_name", /* S */
72 "pane_title", /* T */
73 NULL, /* U */
74 NULL, /* V */
75 "window_name", /* W */
76 NULL, /* X */
77 NULL, /* Y */
78 NULL /* Z */
81 /* Single-character lowercase aliases. */
82 const char *format_lower[] = {
83 NULL, /* a */
84 NULL, /* b */
85 NULL, /* c */
86 NULL, /* d */
87 NULL, /* e */
88 NULL, /* f */
89 NULL, /* g */
90 "host_short", /* h */
91 NULL, /* i */
92 NULL, /* j */
93 NULL, /* k */
94 NULL, /* l */
95 NULL, /* m */
96 NULL, /* n */
97 NULL, /* o */
98 NULL, /* p */
99 NULL, /* q */
100 NULL, /* r */
101 NULL, /* s */
102 NULL, /* t */
103 NULL, /* u */
104 NULL, /* v */
105 NULL, /* w */
106 NULL, /* x */
107 NULL, /* y */
108 NULL /* z */
111 /* Create a new tree. */
112 struct format_tree *
113 format_create(void)
115 struct format_tree *ft;
116 char host[MAXHOSTNAMELEN], *ptr;
118 ft = xmalloc(sizeof *ft);
119 RB_INIT(ft);
121 if (gethostname(host, sizeof host) == 0) {
122 format_add(ft, "host", "%s", host);
123 if ((ptr = strrchr(host, '.')) != NULL)
124 *ptr = '\0';
125 format_add(ft, "host_short", "%s", host);
128 return (ft);
131 /* Free a tree. */
132 void
133 format_free(struct format_tree *ft)
135 struct format_entry *fe, *fe_next;
137 fe_next = RB_MIN(format_tree, ft);
138 while (fe_next != NULL) {
139 fe = fe_next;
140 fe_next = RB_NEXT(format_tree, ft, fe);
142 RB_REMOVE(format_tree, ft, fe);
143 free(fe->value);
144 free(fe->key);
145 free(fe);
148 free(ft);
151 /* Add a key-value pair. */
152 void
153 format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
155 struct format_entry *fe;
156 struct format_entry *fe_now;
157 va_list ap;
159 fe = xmalloc(sizeof *fe);
160 fe->key = xstrdup(key);
162 va_start(ap, fmt);
163 xvasprintf(&fe->value, fmt, ap);
164 va_end(ap);
166 fe_now = RB_INSERT(format_tree, ft, fe);
167 if (fe_now != NULL) {
168 free(fe_now->value);
169 fe_now->value = fe->value;
170 free(fe->key);
171 free(fe);
175 /* Find a format entry. */
176 const char *
177 format_find(struct format_tree *ft, const char *key)
179 struct format_entry *fe, fe_find;
181 fe_find.key = (char *) key;
182 fe = RB_FIND(format_tree, ft, &fe_find);
183 if (fe == NULL)
184 return (NULL);
185 return (fe->value);
189 * Replace a key/value pair in buffer. #{blah} is expanded directly,
190 * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
193 format_replace(struct format_tree *ft, const char *key, size_t keylen,
194 char **buf, size_t *len, size_t *off)
196 char *copy, *copy0, *endptr, *ptr;
197 const char *value;
198 size_t valuelen;
199 u_long limit = ULONG_MAX;
201 /* Make a copy of the key. */
202 copy0 = copy = xmalloc(keylen + 1);
203 memcpy(copy, key, keylen);
204 copy[keylen] = '\0';
206 /* Is there a length limit or whatnot? */
207 if (!islower((u_char) *copy) && *copy != '?') {
208 while (*copy != ':' && *copy != '\0') {
209 switch (*copy) {
210 case '=':
211 errno = 0;
212 limit = strtoul(copy + 1, &endptr, 10);
213 if (errno == ERANGE && limit == ULONG_MAX)
214 goto fail;
215 copy = endptr;
216 break;
217 default:
218 copy++;
219 break;
222 if (*copy != ':')
223 goto fail;
224 copy++;
228 * Is this a conditional? If so, check it exists and extract either the
229 * first or second element. If not, look up the key directly.
231 if (*copy == '?') {
232 ptr = strchr(copy, ',');
233 if (ptr == NULL)
234 goto fail;
235 *ptr = '\0';
237 value = format_find(ft, copy + 1);
238 if (value != NULL && (value[0] != '0' || value[1] != '\0')) {
239 value = ptr + 1;
240 ptr = strchr(value, ',');
241 if (ptr == NULL)
242 goto fail;
243 *ptr = '\0';
244 } else {
245 ptr = strchr(ptr + 1, ',');
246 if (ptr == NULL)
247 goto fail;
248 value = ptr + 1;
250 } else {
251 value = format_find(ft, copy);
252 if (value == NULL)
253 value = "";
255 valuelen = strlen(value);
257 /* Truncate the value if needed. */
258 if (valuelen > limit)
259 valuelen = limit;
261 /* Expand the buffer and copy in the value. */
262 while (*len - *off < valuelen + 1) {
263 *buf = xrealloc(*buf, 2, *len);
264 *len *= 2;
266 memcpy(*buf + *off, value, valuelen);
267 *off += valuelen;
269 free(copy0);
270 return (0);
272 fail:
273 free(copy0);
274 return (-1);
277 /* Expand keys in a template. */
278 char *
279 format_expand(struct format_tree *ft, const char *fmt)
281 char *buf, *ptr;
282 const char *s;
283 size_t off, len, n;
284 int ch;
286 len = 64;
287 buf = xmalloc(len);
288 off = 0;
290 while (*fmt != '\0') {
291 if (*fmt != '#') {
292 while (len - off < 2) {
293 buf = xrealloc(buf, 2, len);
294 len *= 2;
296 buf[off++] = *fmt++;
297 continue;
299 fmt++;
301 ch = (u_char) *fmt++;
303 switch (ch) {
304 case '{':
305 ptr = strchr(fmt, '}');
306 if (ptr == NULL)
307 break;
308 n = ptr - fmt;
310 if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
311 break;
312 fmt += n + 1;
313 continue;
314 default:
315 s = NULL;
316 if (ch >= 'A' && ch <= 'Z')
317 s = format_upper[ch - 'A'];
318 else if (ch >= 'a' && ch <= 'z')
319 s = format_lower[ch - 'a'];
320 if (s == NULL) {
321 while (len - off < 3) {
322 buf = xrealloc(buf, 2, len);
323 len *= 2;
325 buf[off++] = '#';
326 buf[off++] = ch;
327 continue;
329 n = strlen(s);
330 if (format_replace(ft, s, n, &buf, &len, &off) != 0)
331 break;
332 continue;
335 break;
337 buf[off] = '\0';
339 return (buf);
342 /* Set default format keys for a session. */
343 void
344 format_session(struct format_tree *ft, struct session *s)
346 struct session_group *sg;
347 char *tim;
348 time_t t;
350 format_add(ft, "session_name", "%s", s->name);
351 format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
352 format_add(ft, "session_width", "%u", s->sx);
353 format_add(ft, "session_height", "%u", s->sy);
354 format_add(ft, "session_id", "$%u", s->id);
356 sg = session_group_find(s);
357 format_add(ft, "session_grouped", "%d", sg != NULL);
358 if (sg != NULL)
359 format_add(ft, "session_group", "%u", session_group_index(sg));
361 t = s->creation_time.tv_sec;
362 format_add(ft, "session_created", "%lld", (long long) t);
363 tim = ctime(&t);
364 *strchr(tim, '\n') = '\0';
365 format_add(ft, "session_created_string", "%s", tim);
367 if (s->flags & SESSION_UNATTACHED)
368 format_add(ft, "session_attached", "%d", 0);
369 else
370 format_add(ft, "session_attached", "%d", 1);
373 /* Set default format keys for a client. */
374 void
375 format_client(struct format_tree *ft, struct client *c)
377 char *tim;
378 time_t t;
379 struct session *s;
381 format_add(ft, "client_cwd", "%s", c->cwd);
382 format_add(ft, "client_height", "%u", c->tty.sy);
383 format_add(ft, "client_width", "%u", c->tty.sx);
384 if (c->tty.path != NULL)
385 format_add(ft, "client_tty", "%s", c->tty.path);
386 if (c->tty.termname != NULL)
387 format_add(ft, "client_termname", "%s", c->tty.termname);
389 t = c->creation_time.tv_sec;
390 format_add(ft, "client_created", "%lld", (long long) t);
391 tim = ctime(&t);
392 *strchr(tim, '\n') = '\0';
393 format_add(ft, "client_created_string", "%s", tim);
395 t = c->activity_time.tv_sec;
396 format_add(ft, "client_activity", "%lld", (long long) t);
397 tim = ctime(&t);
398 *strchr(tim, '\n') = '\0';
399 format_add(ft, "client_activity_string", "%s", tim);
401 format_add(ft, "client_prefix", "%d", !!(c->flags & CLIENT_PREFIX));
403 if (c->tty.flags & TTY_UTF8)
404 format_add(ft, "client_utf8", "%d", 1);
405 else
406 format_add(ft, "client_utf8", "%d", 0);
408 if (c->flags & CLIENT_READONLY)
409 format_add(ft, "client_readonly", "%d", 1);
410 else
411 format_add(ft, "client_readonly", "%d", 0);
413 s = c->session;
414 if (s != NULL)
415 format_add(ft, "client_session", "%s", s->name);
416 s = c->last_session;
417 if (s != NULL && session_alive(s))
418 format_add(ft, "client_last_session", "%s", s->name);
421 /* Set default format keys for a winlink. */
422 void
423 format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)
425 struct window *w = wl->window;
426 char *layout, *flags;
428 layout = layout_dump(w);
429 flags = window_printable_flags(s, wl);
431 format_add(ft, "window_id", "@%u", w->id);
432 format_add(ft, "window_index", "%d", wl->idx);
433 format_add(ft, "window_name", "%s", w->name);
434 format_add(ft, "window_width", "%u", w->sx);
435 format_add(ft, "window_height", "%u", w->sy);
436 format_add(ft, "window_flags", "%s", flags);
437 format_add(ft, "window_layout", "%s", layout);
438 format_add(ft, "window_active", "%d", wl == s->curw);
439 format_add(ft, "window_panes", "%u", window_count_panes(w));
441 format_add(ft, "window_bell_flag", "%u",
442 !!(wl->flags & WINLINK_BELL));
443 format_add(ft, "window_content_flag", "%u",
444 !!(wl->flags & WINLINK_CONTENT));
445 format_add(ft, "window_activity_flag", "%u",
446 !!(wl->flags & WINLINK_ACTIVITY));
447 format_add(ft, "window_silence_flag", "%u",
448 !!(wl->flags & WINLINK_SILENCE));
450 free(flags);
451 free(layout);
454 /* Add window pane tabs. */
455 void
456 format_window_pane_tabs(struct format_tree *ft, struct window_pane *wp)
458 struct evbuffer *buffer;
459 u_int i;
461 buffer = evbuffer_new();
462 for (i = 0; i < wp->base.grid->sx; i++) {
463 if (!bit_test(wp->base.tabs, i))
464 continue;
466 if (EVBUFFER_LENGTH(buffer) > 0)
467 evbuffer_add(buffer, ",", 1);
468 evbuffer_add_printf(buffer, "%d", i);
471 format_add(ft, "pane_tabs", "%.*s", (int) EVBUFFER_LENGTH(buffer),
472 EVBUFFER_DATA(buffer));
473 evbuffer_free(buffer);
476 /* Set default format keys for a window pane. */
477 void
478 format_window_pane(struct format_tree *ft, struct window_pane *wp)
480 struct grid *gd = wp->base.grid;
481 struct grid_line *gl;
482 unsigned long long size;
483 u_int i, idx;
484 const char *cwd;
485 char *cmd;
487 size = 0;
488 for (i = 0; i < gd->hsize; i++) {
489 gl = &gd->linedata[i];
490 size += gl->cellsize * sizeof *gl->celldata;
492 size += gd->hsize * sizeof *gd->linedata;
493 format_add(ft, "history_size", "%u", gd->hsize);
494 format_add(ft, "history_limit", "%u", gd->hlimit);
495 format_add(ft, "history_bytes", "%llu", size);
497 if (window_pane_index(wp, &idx) != 0)
498 fatalx("index not found");
499 format_add(ft, "pane_index", "%u", idx);
501 format_add(ft, "pane_width", "%u", wp->sx);
502 format_add(ft, "pane_height", "%u", wp->sy);
503 format_add(ft, "pane_title", "%s", wp->base.title);
504 format_add(ft, "pane_id", "%%%u", wp->id);
505 format_add(ft, "pane_active", "%d", wp == wp->window->active);
506 format_add(ft, "pane_dead", "%d", wp->fd == -1);
508 format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
509 format_add(ft, "pane_synchronized", "%d",
510 !!options_get_number(&wp->window->options, "synchronize-panes"));
512 if (wp->tty != NULL)
513 format_add(ft, "pane_tty", "%s", wp->tty);
514 format_add(ft, "pane_pid", "%ld", (long) wp->pid);
515 if (wp->cmd != NULL)
516 format_add(ft, "pane_start_command", "%s", wp->cmd);
517 if (wp->cwd != NULL)
518 format_add(ft, "pane_start_path", "%s", wp->cwd);
519 if ((cwd = get_proc_cwd(wp->fd)) != NULL)
520 format_add(ft, "pane_current_path", "%s", cwd);
521 if ((cmd = get_proc_name(wp->fd, wp->tty)) != NULL) {
522 format_add(ft, "pane_current_command", "%s", cmd);
523 free(cmd);
526 format_add(ft, "cursor_x", "%d", wp->base.cx);
527 format_add(ft, "cursor_y", "%d", wp->base.cy);
528 format_add(ft, "scroll_region_upper", "%d", wp->base.rupper);
529 format_add(ft, "scroll_region_lower", "%d", wp->base.rlower);
530 format_add(ft, "saved_cursor_x", "%d", wp->ictx.old_cx);
531 format_add(ft, "saved_cursor_y", "%d", wp->ictx.old_cy);
533 format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
534 format_add(ft, "alternate_saved_x", "%d", wp->saved_cx);
535 format_add(ft, "alternate_saved_y", "%d", wp->saved_cy);
537 format_add(ft, "cursor_flag", "%d",
538 !!(wp->base.mode & MODE_CURSOR));
539 format_add(ft, "insert_flag", "%d",
540 !!(wp->base.mode & MODE_INSERT));
541 format_add(ft, "keypad_cursor_flag", "%d",
542 !!(wp->base.mode & MODE_KCURSOR));
543 format_add(ft, "keypad_flag", "%d",
544 !!(wp->base.mode & MODE_KKEYPAD));
545 format_add(ft, "wrap_flag", "%d",
546 !!(wp->base.mode & MODE_WRAP));
548 format_add(ft, "mouse_standard_flag", "%d",
549 !!(wp->base.mode & MODE_MOUSE_STANDARD));
550 format_add(ft, "mouse_button_flag", "%d",
551 !!(wp->base.mode & MODE_MOUSE_BUTTON));
552 format_add(ft, "mouse_any_flag", "%d",
553 !!(wp->base.mode & MODE_MOUSE_ANY));
554 format_add(ft, "mouse_utf8_flag", "%d",
555 !!(wp->base.mode & MODE_MOUSE_UTF8));
557 format_window_pane_tabs(ft, wp);
560 /* Set default format keys for paste buffer. */
561 void
562 format_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
564 char *pb_print = paste_print(pb, 50);
566 format_add(ft, "buffer_size", "%zu", pb->size);
567 format_add(ft, "buffer_sample", "%s", pb_print);
569 free(pb_print);