Add automatic-rename-format option allowing automatic rename to use
[tmux-openbsd.git] / format.c
blob1b46085582e39fe3fcdba24f53344730574ef474
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, *saved;
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 saved = format_expand(ft, value);
251 value = saved;
252 } else {
253 value = format_find(ft, copy);
254 if (value == NULL)
255 value = "";
256 saved = NULL;
258 valuelen = strlen(value);
260 /* Truncate the value if needed. */
261 if (valuelen > limit)
262 valuelen = limit;
264 /* Expand the buffer and copy in the value. */
265 while (*len - *off < valuelen + 1) {
266 *buf = xrealloc(*buf, 2, *len);
267 *len *= 2;
269 memcpy(*buf + *off, value, valuelen);
270 *off += valuelen;
272 free(saved);
273 free(copy0);
274 return (0);
276 fail:
277 free(copy0);
278 return (-1);
281 /* Expand keys in a template. */
282 char *
283 format_expand(struct format_tree *ft, const char *fmt)
285 char *buf;
286 const char *ptr, *s;
287 size_t off, len, n;
288 int ch, brackets;
290 len = 64;
291 buf = xmalloc(len);
292 off = 0;
294 while (*fmt != '\0') {
295 if (*fmt != '#') {
296 while (len - off < 2) {
297 buf = xrealloc(buf, 2, len);
298 len *= 2;
300 buf[off++] = *fmt++;
301 continue;
303 fmt++;
305 ch = (u_char) *fmt++;
306 switch (ch) {
307 case '{':
308 brackets = 1;
309 for (ptr = fmt; *ptr != '\0'; ptr++) {
310 if (*ptr == '{')
311 brackets++;
312 if (*ptr == '}' && --brackets == 0)
313 break;
315 if (*ptr != '}' || brackets != 0)
316 break;
317 n = ptr - fmt;
319 if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
320 break;
321 fmt += n + 1;
322 continue;
323 default:
324 s = NULL;
325 if (ch >= 'A' && ch <= 'Z')
326 s = format_upper[ch - 'A'];
327 else if (ch >= 'a' && ch <= 'z')
328 s = format_lower[ch - 'a'];
329 if (s == NULL) {
330 while (len - off < 3) {
331 buf = xrealloc(buf, 2, len);
332 len *= 2;
334 buf[off++] = '#';
335 buf[off++] = ch;
336 continue;
338 n = strlen(s);
339 if (format_replace(ft, s, n, &buf, &len, &off) != 0)
340 break;
341 continue;
344 break;
346 buf[off] = '\0';
348 return (buf);
351 /* Set default format keys for a session. */
352 void
353 format_session(struct format_tree *ft, struct session *s)
355 struct session_group *sg;
356 char *tim;
357 time_t t;
359 format_add(ft, "session_name", "%s", s->name);
360 format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
361 format_add(ft, "session_width", "%u", s->sx);
362 format_add(ft, "session_height", "%u", s->sy);
363 format_add(ft, "session_id", "$%u", s->id);
365 sg = session_group_find(s);
366 format_add(ft, "session_grouped", "%d", sg != NULL);
367 if (sg != NULL)
368 format_add(ft, "session_group", "%u", session_group_index(sg));
370 t = s->creation_time.tv_sec;
371 format_add(ft, "session_created", "%lld", (long long) t);
372 tim = ctime(&t);
373 *strchr(tim, '\n') = '\0';
374 format_add(ft, "session_created_string", "%s", tim);
376 if (s->flags & SESSION_UNATTACHED)
377 format_add(ft, "session_attached", "%d", 0);
378 else
379 format_add(ft, "session_attached", "%d", 1);
382 /* Set default format keys for a client. */
383 void
384 format_client(struct format_tree *ft, struct client *c)
386 char *tim;
387 time_t t;
388 struct session *s;
390 format_add(ft, "client_cwd", "%s", c->cwd);
391 format_add(ft, "client_height", "%u", c->tty.sy);
392 format_add(ft, "client_width", "%u", c->tty.sx);
393 if (c->tty.path != NULL)
394 format_add(ft, "client_tty", "%s", c->tty.path);
395 if (c->tty.termname != NULL)
396 format_add(ft, "client_termname", "%s", c->tty.termname);
398 t = c->creation_time.tv_sec;
399 format_add(ft, "client_created", "%lld", (long long) t);
400 tim = ctime(&t);
401 *strchr(tim, '\n') = '\0';
402 format_add(ft, "client_created_string", "%s", tim);
404 t = c->activity_time.tv_sec;
405 format_add(ft, "client_activity", "%lld", (long long) t);
406 tim = ctime(&t);
407 *strchr(tim, '\n') = '\0';
408 format_add(ft, "client_activity_string", "%s", tim);
410 format_add(ft, "client_prefix", "%d", !!(c->flags & CLIENT_PREFIX));
412 if (c->tty.flags & TTY_UTF8)
413 format_add(ft, "client_utf8", "%d", 1);
414 else
415 format_add(ft, "client_utf8", "%d", 0);
417 if (c->flags & CLIENT_READONLY)
418 format_add(ft, "client_readonly", "%d", 1);
419 else
420 format_add(ft, "client_readonly", "%d", 0);
422 s = c->session;
423 if (s != NULL)
424 format_add(ft, "client_session", "%s", s->name);
425 s = c->last_session;
426 if (s != NULL && session_alive(s))
427 format_add(ft, "client_last_session", "%s", s->name);
430 /* Set default format keys for a winlink. */
431 void
432 format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)
434 struct window *w = wl->window;
435 char *layout, *flags;
437 layout = layout_dump(w);
438 flags = window_printable_flags(s, wl);
440 format_add(ft, "window_id", "@%u", w->id);
441 format_add(ft, "window_index", "%d", wl->idx);
442 format_add(ft, "window_name", "%s", w->name);
443 format_add(ft, "window_width", "%u", w->sx);
444 format_add(ft, "window_height", "%u", w->sy);
445 format_add(ft, "window_flags", "%s", flags);
446 format_add(ft, "window_layout", "%s", layout);
447 format_add(ft, "window_active", "%d", wl == s->curw);
448 format_add(ft, "window_panes", "%u", window_count_panes(w));
450 format_add(ft, "window_bell_flag", "%u",
451 !!(wl->flags & WINLINK_BELL));
452 format_add(ft, "window_content_flag", "%u",
453 !!(wl->flags & WINLINK_CONTENT));
454 format_add(ft, "window_activity_flag", "%u",
455 !!(wl->flags & WINLINK_ACTIVITY));
456 format_add(ft, "window_silence_flag", "%u",
457 !!(wl->flags & WINLINK_SILENCE));
459 free(flags);
460 free(layout);
463 /* Add window pane tabs. */
464 void
465 format_window_pane_tabs(struct format_tree *ft, struct window_pane *wp)
467 struct evbuffer *buffer;
468 u_int i;
470 buffer = evbuffer_new();
471 for (i = 0; i < wp->base.grid->sx; i++) {
472 if (!bit_test(wp->base.tabs, i))
473 continue;
475 if (EVBUFFER_LENGTH(buffer) > 0)
476 evbuffer_add(buffer, ",", 1);
477 evbuffer_add_printf(buffer, "%d", i);
480 format_add(ft, "pane_tabs", "%.*s", (int) EVBUFFER_LENGTH(buffer),
481 EVBUFFER_DATA(buffer));
482 evbuffer_free(buffer);
485 /* Set default format keys for a window pane. */
486 void
487 format_window_pane(struct format_tree *ft, struct window_pane *wp)
489 struct grid *gd = wp->base.grid;
490 struct grid_line *gl;
491 unsigned long long size;
492 u_int i, idx;
493 const char *cwd;
494 char *cmd;
496 size = 0;
497 for (i = 0; i < gd->hsize; i++) {
498 gl = &gd->linedata[i];
499 size += gl->cellsize * sizeof *gl->celldata;
501 size += gd->hsize * sizeof *gd->linedata;
502 format_add(ft, "history_size", "%u", gd->hsize);
503 format_add(ft, "history_limit", "%u", gd->hlimit);
504 format_add(ft, "history_bytes", "%llu", size);
506 if (window_pane_index(wp, &idx) != 0)
507 fatalx("index not found");
508 format_add(ft, "pane_index", "%u", idx);
510 format_add(ft, "pane_width", "%u", wp->sx);
511 format_add(ft, "pane_height", "%u", wp->sy);
512 format_add(ft, "pane_title", "%s", wp->base.title);
513 format_add(ft, "pane_id", "%%%u", wp->id);
514 format_add(ft, "pane_active", "%d", wp == wp->window->active);
515 format_add(ft, "pane_dead", "%d", wp->fd == -1);
517 format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
518 format_add(ft, "pane_synchronized", "%d",
519 !!options_get_number(&wp->window->options, "synchronize-panes"));
521 if (wp->tty != NULL)
522 format_add(ft, "pane_tty", "%s", wp->tty);
523 format_add(ft, "pane_pid", "%ld", (long) wp->pid);
524 if (wp->cmd != NULL)
525 format_add(ft, "pane_start_command", "%s", wp->cmd);
526 if (wp->cwd != NULL)
527 format_add(ft, "pane_start_path", "%s", wp->cwd);
528 if ((cwd = get_proc_cwd(wp->fd)) != NULL)
529 format_add(ft, "pane_current_path", "%s", cwd);
530 if ((cmd = get_proc_name(wp->fd, wp->tty)) != NULL) {
531 format_add(ft, "pane_current_command", "%s", cmd);
532 free(cmd);
535 format_add(ft, "cursor_x", "%d", wp->base.cx);
536 format_add(ft, "cursor_y", "%d", wp->base.cy);
537 format_add(ft, "scroll_region_upper", "%d", wp->base.rupper);
538 format_add(ft, "scroll_region_lower", "%d", wp->base.rlower);
539 format_add(ft, "saved_cursor_x", "%d", wp->ictx.old_cx);
540 format_add(ft, "saved_cursor_y", "%d", wp->ictx.old_cy);
542 format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
543 format_add(ft, "alternate_saved_x", "%d", wp->saved_cx);
544 format_add(ft, "alternate_saved_y", "%d", wp->saved_cy);
546 format_add(ft, "cursor_flag", "%d",
547 !!(wp->base.mode & MODE_CURSOR));
548 format_add(ft, "insert_flag", "%d",
549 !!(wp->base.mode & MODE_INSERT));
550 format_add(ft, "keypad_cursor_flag", "%d",
551 !!(wp->base.mode & MODE_KCURSOR));
552 format_add(ft, "keypad_flag", "%d",
553 !!(wp->base.mode & MODE_KKEYPAD));
554 format_add(ft, "wrap_flag", "%d",
555 !!(wp->base.mode & MODE_WRAP));
557 format_add(ft, "mouse_standard_flag", "%d",
558 !!(wp->base.mode & MODE_MOUSE_STANDARD));
559 format_add(ft, "mouse_button_flag", "%d",
560 !!(wp->base.mode & MODE_MOUSE_BUTTON));
561 format_add(ft, "mouse_any_flag", "%d",
562 !!(wp->base.mode & MODE_MOUSE_ANY));
563 format_add(ft, "mouse_utf8_flag", "%d",
564 !!(wp->base.mode & MODE_MOUSE_UTF8));
566 format_window_pane_tabs(ft, wp);
569 /* Set default format keys for paste buffer. */
570 void
571 format_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
573 char *pb_print = paste_print(pb, 50);
575 format_add(ft, "buffer_size", "%zu", pb->size);
576 format_add(ft, "buffer_sample", "%s", pb_print);
578 free(pb_print);