Alter how tmux handles the working directory to internally use file
[tmux-openbsd.git] / format.c
blob19c4dc15465a24665185e9761e5d8514c4d568ff
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 char *format_get_command(struct window_pane *);
40 void format_window_pane_tabs(struct format_tree *, struct window_pane *);
42 /* Format key-value replacement entry. */
43 RB_GENERATE(format_tree, format_entry, entry, format_cmp);
45 /* Format tree comparison function. */
46 int
47 format_cmp(struct format_entry *fe1, struct format_entry *fe2)
49 return (strcmp(fe1->key, fe2->key));
52 /* Single-character uppercase aliases. */
53 const char *format_upper[] = {
54 NULL, /* A */
55 NULL, /* B */
56 NULL, /* C */
57 "pane_id", /* D */
58 NULL, /* E */
59 "window_flags", /* F */
60 NULL, /* G */
61 "host", /* H */
62 "window_index", /* I */
63 NULL, /* J */
64 NULL, /* K */
65 NULL, /* L */
66 NULL, /* M */
67 NULL, /* N */
68 NULL, /* O */
69 "pane_index", /* P */
70 NULL, /* Q */
71 NULL, /* R */
72 "session_name", /* S */
73 "pane_title", /* T */
74 NULL, /* U */
75 NULL, /* V */
76 "window_name", /* W */
77 NULL, /* X */
78 NULL, /* Y */
79 NULL /* Z */
82 /* Single-character lowercase aliases. */
83 const char *format_lower[] = {
84 NULL, /* a */
85 NULL, /* b */
86 NULL, /* c */
87 NULL, /* d */
88 NULL, /* e */
89 NULL, /* f */
90 NULL, /* g */
91 "host_short", /* h */
92 NULL, /* i */
93 NULL, /* j */
94 NULL, /* k */
95 NULL, /* l */
96 NULL, /* m */
97 NULL, /* n */
98 NULL, /* o */
99 NULL, /* p */
100 NULL, /* q */
101 NULL, /* r */
102 NULL, /* s */
103 NULL, /* t */
104 NULL, /* u */
105 NULL, /* v */
106 NULL, /* w */
107 NULL, /* x */
108 NULL, /* y */
109 NULL /* z */
112 /* Create a new tree. */
113 struct format_tree *
114 format_create(void)
116 struct format_tree *ft;
117 char host[MAXHOSTNAMELEN], *ptr;
119 ft = xmalloc(sizeof *ft);
120 RB_INIT(ft);
122 if (gethostname(host, sizeof host) == 0) {
123 format_add(ft, "host", "%s", host);
124 if ((ptr = strchr(host, '.')) != NULL)
125 *ptr = '\0';
126 format_add(ft, "host_short", "%s", host);
129 return (ft);
132 /* Free a tree. */
133 void
134 format_free(struct format_tree *ft)
136 struct format_entry *fe, *fe_next;
138 fe_next = RB_MIN(format_tree, ft);
139 while (fe_next != NULL) {
140 fe = fe_next;
141 fe_next = RB_NEXT(format_tree, ft, fe);
143 RB_REMOVE(format_tree, ft, fe);
144 free(fe->value);
145 free(fe->key);
146 free(fe);
149 free(ft);
152 /* Add a key-value pair. */
153 void
154 format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
156 struct format_entry *fe;
157 struct format_entry *fe_now;
158 va_list ap;
160 fe = xmalloc(sizeof *fe);
161 fe->key = xstrdup(key);
163 va_start(ap, fmt);
164 xvasprintf(&fe->value, fmt, ap);
165 va_end(ap);
167 fe_now = RB_INSERT(format_tree, ft, fe);
168 if (fe_now != NULL) {
169 free(fe_now->value);
170 fe_now->value = fe->value;
171 free(fe->key);
172 free(fe);
176 /* Find a format entry. */
177 const char *
178 format_find(struct format_tree *ft, const char *key)
180 struct format_entry *fe, fe_find;
182 fe_find.key = (char *) key;
183 fe = RB_FIND(format_tree, ft, &fe_find);
184 if (fe == NULL)
185 return (NULL);
186 return (fe->value);
190 * Replace a key/value pair in buffer. #{blah} is expanded directly,
191 * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
194 format_replace(struct format_tree *ft, const char *key, size_t keylen,
195 char **buf, size_t *len, size_t *off)
197 char *copy, *copy0, *endptr, *ptr, *saved;
198 const char *value;
199 size_t valuelen;
200 u_long limit = ULONG_MAX;
202 /* Make a copy of the key. */
203 copy0 = copy = xmalloc(keylen + 1);
204 memcpy(copy, key, keylen);
205 copy[keylen] = '\0';
207 /* Is there a length limit or whatnot? */
208 if (!islower((u_char) *copy) && *copy != '?') {
209 while (*copy != ':' && *copy != '\0') {
210 switch (*copy) {
211 case '=':
212 errno = 0;
213 limit = strtoul(copy + 1, &endptr, 10);
214 if (errno == ERANGE && limit == ULONG_MAX)
215 goto fail;
216 copy = endptr;
217 break;
218 default:
219 copy++;
220 break;
223 if (*copy != ':')
224 goto fail;
225 copy++;
229 * Is this a conditional? If so, check it exists and extract either the
230 * first or second element. If not, look up the key directly.
232 if (*copy == '?') {
233 ptr = strchr(copy, ',');
234 if (ptr == NULL)
235 goto fail;
236 *ptr = '\0';
238 value = format_find(ft, copy + 1);
239 if (value != NULL && (value[0] != '0' || value[1] != '\0')) {
240 value = ptr + 1;
241 ptr = strchr(value, ',');
242 if (ptr == NULL)
243 goto fail;
244 *ptr = '\0';
245 } else {
246 ptr = strchr(ptr + 1, ',');
247 if (ptr == NULL)
248 goto fail;
249 value = ptr + 1;
251 saved = format_expand(ft, value);
252 value = saved;
253 } else {
254 value = format_find(ft, copy);
255 if (value == NULL)
256 value = "";
257 saved = NULL;
259 valuelen = strlen(value);
261 /* Truncate the value if needed. */
262 if (valuelen > limit)
263 valuelen = limit;
265 /* Expand the buffer and copy in the value. */
266 while (*len - *off < valuelen + 1) {
267 *buf = xrealloc(*buf, 2, *len);
268 *len *= 2;
270 memcpy(*buf + *off, value, valuelen);
271 *off += valuelen;
273 free(saved);
274 free(copy0);
275 return (0);
277 fail:
278 free(copy0);
279 return (-1);
282 /* Expand keys in a template. */
283 char *
284 format_expand(struct format_tree *ft, const char *fmt)
286 char *buf;
287 const char *ptr, *s;
288 size_t off, len, n;
289 int ch, brackets;
291 len = 64;
292 buf = xmalloc(len);
293 off = 0;
295 while (*fmt != '\0') {
296 if (*fmt != '#') {
297 while (len - off < 2) {
298 buf = xrealloc(buf, 2, len);
299 len *= 2;
301 buf[off++] = *fmt++;
302 continue;
304 fmt++;
306 ch = (u_char) *fmt++;
307 switch (ch) {
308 case '{':
309 brackets = 1;
310 for (ptr = fmt; *ptr != '\0'; ptr++) {
311 if (*ptr == '{')
312 brackets++;
313 if (*ptr == '}' && --brackets == 0)
314 break;
316 if (*ptr != '}' || brackets != 0)
317 break;
318 n = ptr - fmt;
320 if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
321 break;
322 fmt += n + 1;
323 continue;
324 default:
325 s = NULL;
326 if (ch >= 'A' && ch <= 'Z')
327 s = format_upper[ch - 'A'];
328 else if (ch >= 'a' && ch <= 'z')
329 s = format_lower[ch - 'a'];
330 if (s == NULL) {
331 while (len - off < 3) {
332 buf = xrealloc(buf, 2, len);
333 len *= 2;
335 buf[off++] = '#';
336 buf[off++] = ch;
337 continue;
339 n = strlen(s);
340 if (format_replace(ft, s, n, &buf, &len, &off) != 0)
341 break;
342 continue;
345 break;
347 buf[off] = '\0';
349 return (buf);
352 /* Get command name for format. */
353 char *
354 format_get_command(struct window_pane *wp)
356 char *cmd;
358 cmd = get_proc_name(wp->fd, wp->tty);
359 if (cmd == NULL || *cmd == '\0') {
360 cmd = wp->cmd;
361 if (cmd == NULL || *cmd == '\0')
362 cmd = wp->shell;
364 return (parse_window_name(cmd));
367 /* Set default format keys for a session. */
368 void
369 format_session(struct format_tree *ft, struct session *s)
371 struct session_group *sg;
372 char *tim;
373 time_t t;
375 format_add(ft, "session_name", "%s", s->name);
376 format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
377 format_add(ft, "session_width", "%u", s->sx);
378 format_add(ft, "session_height", "%u", s->sy);
379 format_add(ft, "session_id", "$%u", s->id);
381 sg = session_group_find(s);
382 format_add(ft, "session_grouped", "%d", sg != NULL);
383 if (sg != NULL)
384 format_add(ft, "session_group", "%u", session_group_index(sg));
386 t = s->creation_time.tv_sec;
387 format_add(ft, "session_created", "%lld", (long long) t);
388 tim = ctime(&t);
389 *strchr(tim, '\n') = '\0';
390 format_add(ft, "session_created_string", "%s", tim);
392 if (s->flags & SESSION_UNATTACHED)
393 format_add(ft, "session_attached", "%d", 0);
394 else
395 format_add(ft, "session_attached", "%d", 1);
398 /* Set default format keys for a client. */
399 void
400 format_client(struct format_tree *ft, struct client *c)
402 char *tim;
403 time_t t;
404 struct session *s;
406 format_add(ft, "client_height", "%u", c->tty.sy);
407 format_add(ft, "client_width", "%u", c->tty.sx);
408 if (c->tty.path != NULL)
409 format_add(ft, "client_tty", "%s", c->tty.path);
410 if (c->tty.termname != NULL)
411 format_add(ft, "client_termname", "%s", c->tty.termname);
413 t = c->creation_time.tv_sec;
414 format_add(ft, "client_created", "%lld", (long long) t);
415 tim = ctime(&t);
416 *strchr(tim, '\n') = '\0';
417 format_add(ft, "client_created_string", "%s", tim);
419 t = c->activity_time.tv_sec;
420 format_add(ft, "client_activity", "%lld", (long long) t);
421 tim = ctime(&t);
422 *strchr(tim, '\n') = '\0';
423 format_add(ft, "client_activity_string", "%s", tim);
425 format_add(ft, "client_prefix", "%d", !!(c->flags & CLIENT_PREFIX));
427 if (c->tty.flags & TTY_UTF8)
428 format_add(ft, "client_utf8", "%d", 1);
429 else
430 format_add(ft, "client_utf8", "%d", 0);
432 if (c->flags & CLIENT_READONLY)
433 format_add(ft, "client_readonly", "%d", 1);
434 else
435 format_add(ft, "client_readonly", "%d", 0);
437 s = c->session;
438 if (s != NULL)
439 format_add(ft, "client_session", "%s", s->name);
440 s = c->last_session;
441 if (s != NULL && session_alive(s))
442 format_add(ft, "client_last_session", "%s", s->name);
445 /* Set default format keys for a window. */
446 void
447 format_window(struct format_tree *ft, struct window *w)
449 char *layout;
451 layout = layout_dump(w);
453 format_add(ft, "window_id", "@%u", w->id);
454 format_add(ft, "window_name", "%s", w->name);
455 format_add(ft, "window_width", "%u", w->sx);
456 format_add(ft, "window_height", "%u", w->sy);
457 format_add(ft, "window_layout", "%s", layout);
458 format_add(ft, "window_panes", "%u", window_count_panes(w));
460 free(layout);
463 /* Set default format keys for a winlink. */
464 void
465 format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)
467 struct window *w = wl->window;
468 char *flags;
470 flags = window_printable_flags(s, wl);
472 format_window(ft, w);
474 format_add(ft, "window_index", "%d", wl->idx);
475 format_add(ft, "window_flags", "%s", flags);
476 format_add(ft, "window_active", "%d", wl == s->curw);
478 format_add(ft, "window_bell_flag", "%u",
479 !!(wl->flags & WINLINK_BELL));
480 format_add(ft, "window_content_flag", "%u",
481 !!(wl->flags & WINLINK_CONTENT));
482 format_add(ft, "window_activity_flag", "%u",
483 !!(wl->flags & WINLINK_ACTIVITY));
484 format_add(ft, "window_silence_flag", "%u",
485 !!(wl->flags & WINLINK_SILENCE));
488 free(flags);
491 /* Add window pane tabs. */
492 void
493 format_window_pane_tabs(struct format_tree *ft, struct window_pane *wp)
495 struct evbuffer *buffer;
496 u_int i;
498 buffer = evbuffer_new();
499 for (i = 0; i < wp->base.grid->sx; i++) {
500 if (!bit_test(wp->base.tabs, i))
501 continue;
503 if (EVBUFFER_LENGTH(buffer) > 0)
504 evbuffer_add(buffer, ",", 1);
505 evbuffer_add_printf(buffer, "%d", i);
508 format_add(ft, "pane_tabs", "%.*s", (int) EVBUFFER_LENGTH(buffer),
509 EVBUFFER_DATA(buffer));
510 evbuffer_free(buffer);
513 /* Set default format keys for a window pane. */
514 void
515 format_window_pane(struct format_tree *ft, struct window_pane *wp)
517 struct grid *gd = wp->base.grid;
518 struct grid_line *gl;
519 unsigned long long size;
520 u_int i, idx;
521 const char *cwd;
522 char *cmd;
524 size = 0;
525 for (i = 0; i < gd->hsize; i++) {
526 gl = &gd->linedata[i];
527 size += gl->cellsize * sizeof *gl->celldata;
529 size += gd->hsize * sizeof *gd->linedata;
530 format_add(ft, "history_size", "%u", gd->hsize);
531 format_add(ft, "history_limit", "%u", gd->hlimit);
532 format_add(ft, "history_bytes", "%llu", size);
534 if (window_pane_index(wp, &idx) != 0)
535 fatalx("index not found");
536 format_add(ft, "pane_index", "%u", idx);
538 format_add(ft, "pane_width", "%u", wp->sx);
539 format_add(ft, "pane_height", "%u", wp->sy);
540 format_add(ft, "pane_title", "%s", wp->base.title);
541 format_add(ft, "pane_id", "%%%u", wp->id);
542 format_add(ft, "pane_active", "%d", wp == wp->window->active);
543 format_add(ft, "pane_dead", "%d", wp->fd == -1);
545 format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
546 format_add(ft, "pane_synchronized", "%d",
547 !!options_get_number(&wp->window->options, "synchronize-panes"));
549 if (wp->tty != NULL)
550 format_add(ft, "pane_tty", "%s", wp->tty);
551 format_add(ft, "pane_pid", "%ld", (long) wp->pid);
552 if (wp->cmd != NULL)
553 format_add(ft, "pane_start_command", "%s", wp->cmd);
554 if ((cwd = get_proc_cwd(wp->fd)) != NULL)
555 format_add(ft, "pane_current_path", "%s", cwd);
556 if ((cmd = get_proc_name(wp->fd, wp->tty)) != NULL) {
557 format_add(ft, "pane_current_command", "%s", cmd);
558 free(cmd);
561 format_add(ft, "cursor_x", "%d", wp->base.cx);
562 format_add(ft, "cursor_y", "%d", wp->base.cy);
563 format_add(ft, "scroll_region_upper", "%d", wp->base.rupper);
564 format_add(ft, "scroll_region_lower", "%d", wp->base.rlower);
565 format_add(ft, "saved_cursor_x", "%d", wp->ictx.old_cx);
566 format_add(ft, "saved_cursor_y", "%d", wp->ictx.old_cy);
568 format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
569 format_add(ft, "alternate_saved_x", "%d", wp->saved_cx);
570 format_add(ft, "alternate_saved_y", "%d", wp->saved_cy);
572 format_add(ft, "cursor_flag", "%d",
573 !!(wp->base.mode & MODE_CURSOR));
574 format_add(ft, "insert_flag", "%d",
575 !!(wp->base.mode & MODE_INSERT));
576 format_add(ft, "keypad_cursor_flag", "%d",
577 !!(wp->base.mode & MODE_KCURSOR));
578 format_add(ft, "keypad_flag", "%d",
579 !!(wp->base.mode & MODE_KKEYPAD));
580 format_add(ft, "wrap_flag", "%d",
581 !!(wp->base.mode & MODE_WRAP));
583 format_add(ft, "mouse_standard_flag", "%d",
584 !!(wp->base.mode & MODE_MOUSE_STANDARD));
585 format_add(ft, "mouse_button_flag", "%d",
586 !!(wp->base.mode & MODE_MOUSE_BUTTON));
587 format_add(ft, "mouse_any_flag", "%d",
588 !!(wp->base.mode & MODE_MOUSE_ANY));
589 format_add(ft, "mouse_utf8_flag", "%d",
590 !!(wp->base.mode & MODE_MOUSE_UTF8));
592 format_window_pane_tabs(ft, wp);
595 /* Set default format keys for paste buffer. */
596 void
597 format_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
599 char *pb_print = paste_print(pb, 50);
601 format_add(ft, "buffer_size", "%zu", pb->size);
602 format_add(ft, "buffer_sample", "%s", pb_print);
604 free(pb_print);