When the session option renumber-window is used, ensure we iterate over
[tmux-openbsd.git] / format.c
blob8ed1c1be90d6f36a3debdab39150b6175b82bbfe
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 <netdb.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
28 #include "tmux.h"
31 * Build a list of key-value pairs and use them to expand #{key} entries in a
32 * string.
35 int format_replace(struct format_tree *, const char *, size_t, char **,
36 size_t *, size_t *);
37 void format_window_pane_tabs(struct format_tree *, struct window_pane *);
39 /* Format key-value replacement entry. */
40 RB_GENERATE(format_tree, format_entry, entry, format_cmp);
42 /* Format tree comparison function. */
43 int
44 format_cmp(struct format_entry *fe1, struct format_entry *fe2)
46 return (strcmp(fe1->key, fe2->key));
49 /* Single-character uppercase aliases. */
50 const char *format_upper[] = {
51 NULL, /* A */
52 NULL, /* B */
53 NULL, /* C */
54 "pane_id", /* D */
55 NULL, /* E */
56 "window_flags", /* F */
57 NULL, /* G */
58 "host", /* H */
59 "window_index", /* I */
60 NULL, /* J */
61 NULL, /* K */
62 NULL, /* L */
63 NULL, /* M */
64 NULL, /* N */
65 NULL, /* O */
66 "pane_index", /* P */
67 NULL, /* Q */
68 NULL, /* R */
69 "session_name", /* S */
70 "pane_title", /* T */
71 NULL, /* U */
72 NULL, /* V */
73 "window_name", /* W */
74 NULL, /* X */
75 NULL, /* Y */
76 NULL /* Z */
79 /* Single-character lowercase aliases. */
80 const char *format_lower[] = {
81 NULL, /* a */
82 NULL, /* b */
83 NULL, /* c */
84 NULL, /* d */
85 NULL, /* e */
86 NULL, /* f */
87 NULL, /* g */
88 "host_short", /* h */
89 NULL, /* i */
90 NULL, /* j */
91 NULL, /* k */
92 NULL, /* l */
93 NULL, /* m */
94 NULL, /* n */
95 NULL, /* o */
96 NULL, /* p */
97 NULL, /* q */
98 NULL, /* r */
99 NULL, /* s */
100 NULL, /* t */
101 NULL, /* u */
102 NULL, /* v */
103 NULL, /* w */
104 NULL, /* x */
105 NULL, /* y */
106 NULL /* z */
109 /* Create a new tree. */
110 struct format_tree *
111 format_create(void)
113 struct format_tree *ft;
114 char host[MAXHOSTNAMELEN], *ptr;
116 ft = xmalloc(sizeof *ft);
117 RB_INIT(ft);
119 if (gethostname(host, sizeof host) == 0) {
120 format_add(ft, "host", "%s", host);
121 if ((ptr = strrchr(host, '.')) != NULL)
122 *ptr = '\0';
123 format_add(ft, "host_short", "%s", host);
126 return (ft);
129 /* Free a tree. */
130 void
131 format_free(struct format_tree *ft)
133 struct format_entry *fe, *fe_next;
135 fe_next = RB_MIN(format_tree, ft);
136 while (fe_next != NULL) {
137 fe = fe_next;
138 fe_next = RB_NEXT(format_tree, ft, fe);
140 RB_REMOVE(format_tree, ft, fe);
141 free(fe->value);
142 free(fe->key);
143 free(fe);
146 free(ft);
149 /* Add a key-value pair. */
150 void
151 format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
153 struct format_entry *fe;
154 va_list ap;
156 fe = xmalloc(sizeof *fe);
157 fe->key = xstrdup(key);
159 va_start(ap, fmt);
160 xvasprintf(&fe->value, fmt, ap);
161 va_end(ap);
163 RB_INSERT(format_tree, ft, fe);
166 /* Find a format entry. */
167 const char *
168 format_find(struct format_tree *ft, const char *key)
170 struct format_entry *fe, fe_find;
172 fe_find.key = (char *) key;
173 fe = RB_FIND(format_tree, ft, &fe_find);
174 if (fe == NULL)
175 return (NULL);
176 return (fe->value);
180 * Replace a key/value pair in buffer. #{blah} is expanded directly,
181 * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
184 format_replace(struct format_tree *ft,
185 const char *key, size_t keylen, char **buf, size_t *len, size_t *off)
187 char *copy, *ptr;
188 const char *value;
189 size_t valuelen;
191 /* Make a copy of the key. */
192 copy = xmalloc(keylen + 1);
193 memcpy(copy, key, keylen);
194 copy[keylen] = '\0';
197 * Is this a conditional? If so, check it exists and extract either the
198 * first or second element. If not, look up the key directly.
200 if (*copy == '?') {
201 ptr = strchr(copy, ',');
202 if (ptr == NULL)
203 goto fail;
204 *ptr = '\0';
206 value = format_find(ft, copy + 1);
207 if (value != NULL && (value[0] != '0' || value[1] != '\0')) {
208 value = ptr + 1;
209 ptr = strchr(value, ',');
210 if (ptr == NULL)
211 goto fail;
212 *ptr = '\0';
213 } else {
214 ptr = strchr(ptr + 1, ',');
215 if (ptr == NULL)
216 goto fail;
217 value = ptr + 1;
219 } else {
220 value = format_find(ft, copy);
221 if (value == NULL)
222 value = "";
224 valuelen = strlen(value);
226 /* Expand the buffer and copy in the value. */
227 while (*len - *off < valuelen + 1) {
228 *buf = xrealloc(*buf, 2, *len);
229 *len *= 2;
231 memcpy(*buf + *off, value, valuelen);
232 *off += valuelen;
234 free(copy);
235 return (0);
237 fail:
238 free(copy);
239 return (-1);
242 /* Expand keys in a template. */
243 char *
244 format_expand(struct format_tree *ft, const char *fmt)
246 char *buf, *ptr;
247 const char *s;
248 size_t off, len, n;
249 int ch;
251 len = 64;
252 buf = xmalloc(len);
253 off = 0;
255 while (*fmt != '\0') {
256 if (*fmt != '#') {
257 while (len - off < 2) {
258 buf = xrealloc(buf, 2, len);
259 len *= 2;
261 buf[off++] = *fmt++;
262 continue;
264 fmt++;
266 ch = (u_char) *fmt++;
268 switch (ch) {
269 case '{':
270 ptr = strchr(fmt, '}');
271 if (ptr == NULL)
272 break;
273 n = ptr - fmt;
275 if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
276 break;
277 fmt += n + 1;
278 continue;
279 default:
280 s = NULL;
281 if (ch >= 'A' && ch <= 'Z')
282 s = format_upper[ch - 'A'];
283 else if (ch >= 'a' && ch <= 'z')
284 s = format_lower[ch - 'a'];
285 if (s == NULL) {
286 while (len - off < 3) {
287 buf = xrealloc(buf, 2, len);
288 len *= 2;
290 buf[off++] = '#';
291 buf[off++] = ch;
292 continue;
294 n = strlen(s);
295 if (format_replace(ft, s, n, &buf, &len, &off) != 0)
296 break;
297 continue;
300 break;
302 buf[off] = '\0';
304 return (buf);
307 /* Set default format keys for a session. */
308 void
309 format_session(struct format_tree *ft, struct session *s)
311 struct session_group *sg;
312 char *tim;
313 time_t t;
315 format_add(ft, "session_name", "%s", s->name);
316 format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
317 format_add(ft, "session_width", "%u", s->sx);
318 format_add(ft, "session_height", "%u", s->sy);
319 format_add(ft, "session_id", "$%u", s->id);
321 sg = session_group_find(s);
322 format_add(ft, "session_grouped", "%d", sg != NULL);
323 if (sg != NULL)
324 format_add(ft, "session_group", "%u", session_group_index(sg));
326 t = s->creation_time.tv_sec;
327 format_add(ft, "session_created", "%lld", (long long) t);
328 tim = ctime(&t);
329 *strchr(tim, '\n') = '\0';
330 format_add(ft, "session_created_string", "%s", tim);
332 if (s->flags & SESSION_UNATTACHED)
333 format_add(ft, "session_attached", "%d", 0);
334 else
335 format_add(ft, "session_attached", "%d", 1);
338 /* Set default format keys for a client. */
339 void
340 format_client(struct format_tree *ft, struct client *c)
342 char *tim;
343 time_t t;
344 struct session *s;
346 format_add(ft, "client_cwd", "%s", c->cwd);
347 format_add(ft, "client_height", "%u", c->tty.sy);
348 format_add(ft, "client_width", "%u", c->tty.sx);
349 format_add(ft, "client_tty", "%s", c->tty.path);
350 format_add(ft, "client_termname", "%s", c->tty.termname);
352 t = c->creation_time.tv_sec;
353 format_add(ft, "client_created", "%lld", (long long) t);
354 tim = ctime(&t);
355 *strchr(tim, '\n') = '\0';
356 format_add(ft, "client_created_string", "%s", tim);
358 t = c->activity_time.tv_sec;
359 format_add(ft, "client_activity", "%lld", (long long) t);
360 tim = ctime(&t);
361 *strchr(tim, '\n') = '\0';
362 format_add(ft, "client_activity_string", "%s", tim);
364 format_add(ft, "client_prefix", "%d", !!(c->flags & CLIENT_PREFIX));
366 if (c->tty.flags & TTY_UTF8)
367 format_add(ft, "client_utf8", "%d", 1);
368 else
369 format_add(ft, "client_utf8", "%d", 0);
371 if (c->flags & CLIENT_READONLY)
372 format_add(ft, "client_readonly", "%d", 1);
373 else
374 format_add(ft, "client_readonly", "%d", 0);
376 s = c->session;
377 if (s != NULL)
378 format_add(ft, "client_session", "%s", s->name);
379 s = c->last_session;
380 if (s != NULL && session_alive(s))
381 format_add(ft, "client_last_session", "%s", s->name);
384 /* Set default format keys for a winlink. */
385 void
386 format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)
388 struct window *w = wl->window;
389 char *layout, *flags;
391 layout = layout_dump(w);
392 flags = window_printable_flags(s, wl);
394 format_add(ft, "window_id", "@%u", w->id);
395 format_add(ft, "window_index", "%d", wl->idx);
396 format_add(ft, "window_name", "%s", w->name);
397 format_add(ft, "window_width", "%u", w->sx);
398 format_add(ft, "window_height", "%u", w->sy);
399 format_add(ft, "window_flags", "%s", flags);
400 format_add(ft, "window_layout", "%s", layout);
401 format_add(ft, "window_active", "%d", wl == s->curw);
402 format_add(ft, "window_panes", "%u", window_count_panes(w));
404 free(flags);
405 free(layout);
408 /* Add window pane tabs. */
409 void
410 format_window_pane_tabs(struct format_tree *ft, struct window_pane *wp)
412 struct evbuffer *buffer;
413 u_int i;
415 buffer = evbuffer_new();
416 for (i = 0; i < wp->base.grid->sx; i++) {
417 if (!bit_test(wp->base.tabs, i))
418 continue;
420 if (EVBUFFER_LENGTH(buffer) > 0)
421 evbuffer_add(buffer, ",", 1);
422 evbuffer_add_printf(buffer, "%d", i);
425 format_add(ft, "pane_tabs", "%.*s", (int) EVBUFFER_LENGTH(buffer),
426 EVBUFFER_DATA(buffer));
427 evbuffer_free(buffer);
430 /* Set default format keys for a window pane. */
431 void
432 format_window_pane(struct format_tree *ft, struct window_pane *wp)
434 struct grid *gd = wp->base.grid;
435 struct grid_line *gl;
436 unsigned long long size;
437 u_int i, idx;
438 const char *cwd;
439 char *cmd;
441 size = 0;
442 for (i = 0; i < gd->hsize; i++) {
443 gl = &gd->linedata[i];
444 size += gl->cellsize * sizeof *gl->celldata;
446 size += gd->hsize * sizeof *gd->linedata;
447 format_add(ft, "history_size", "%u", gd->hsize);
448 format_add(ft, "history_limit", "%u", gd->hlimit);
449 format_add(ft, "history_bytes", "%llu", size);
451 if (window_pane_index(wp, &idx) != 0)
452 fatalx("index not found");
453 format_add(ft, "pane_index", "%u", idx);
455 format_add(ft, "pane_width", "%u", wp->sx);
456 format_add(ft, "pane_height", "%u", wp->sy);
457 format_add(ft, "pane_title", "%s", wp->base.title);
458 format_add(ft, "pane_id", "%%%u", wp->id);
459 format_add(ft, "pane_active", "%d", wp == wp->window->active);
460 format_add(ft, "pane_dead", "%d", wp->fd == -1);
462 format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
464 if (wp->tty != NULL)
465 format_add(ft, "pane_tty", "%s", wp->tty);
466 format_add(ft, "pane_pid", "%ld", (long) wp->pid);
467 if (wp->cmd != NULL)
468 format_add(ft, "pane_start_command", "%s", wp->cmd);
469 if (wp->cwd != NULL)
470 format_add(ft, "pane_start_path", "%s", wp->cwd);
471 if ((cwd = get_proc_cwd(wp->fd)) != NULL)
472 format_add(ft, "pane_current_path", "%s", cwd);
473 if ((cmd = get_proc_name(wp->fd, wp->tty)) != NULL) {
474 format_add(ft, "pane_current_command", "%s", cmd);
475 free(cmd);
478 format_add(ft, "cursor_x", "%d", wp->base.cx);
479 format_add(ft, "cursor_y", "%d", wp->base.cy);
480 format_add(ft, "scroll_region_upper", "%d", wp->base.rupper);
481 format_add(ft, "scroll_region_lower", "%d", wp->base.rlower);
482 format_add(ft, "saved_cursor_x", "%d", wp->ictx.old_cx);
483 format_add(ft, "saved_cursor_y", "%d", wp->ictx.old_cy);
485 format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
486 format_add(ft, "alternate_saved_x", "%d", wp->saved_cx);
487 format_add(ft, "alternate_saved_y", "%d", wp->saved_cy);
489 format_add(ft, "cursor_flag", "%d",
490 !!(wp->base.mode & MODE_CURSOR));
491 format_add(ft, "insert_flag", "%d",
492 !!(wp->base.mode & MODE_INSERT));
493 format_add(ft, "keypad_cursor_flag", "%d",
494 !!(wp->base.mode & MODE_KCURSOR));
495 format_add(ft, "keypad_flag", "%d",
496 !!(wp->base.mode & MODE_KKEYPAD));
497 format_add(ft, "wrap_flag", "%d",
498 !!(wp->base.mode & MODE_WRAP));
500 format_add(ft, "mouse_standard_flag", "%d",
501 !!(wp->base.mode & MODE_MOUSE_STANDARD));
502 format_add(ft, "mouse_button_flag", "%d",
503 !!(wp->base.mode & MODE_MOUSE_BUTTON));
504 format_add(ft, "mouse_any_flag", "%d",
505 !!(wp->base.mode & MODE_MOUSE_ANY));
506 format_add(ft, "mouse_utf8_flag", "%d",
507 !!(wp->base.mode & MODE_MOUSE_UTF8));
509 format_window_pane_tabs(ft, wp);
512 /* Set default format keys for paste buffer. */
513 void
514 format_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
516 char *pb_print = paste_print(pb, 50);
518 format_add(ft, "buffer_size", "%zu", pb->size);
519 format_add(ft, "buffer_sample", "%s", pb_print);
521 free(pb_print);