Fix compiler warnings, missing #include. From Thomas Adam.
[tmux-openbsd.git] / format.c
blobb357876e399805e1a3dec42993a56d8ae83ccf7f
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 aliases. */
50 const char *format_aliases[26] = {
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 /* Create a new tree. */
80 struct format_tree *
81 format_create(void)
83 struct format_tree *ft;
84 char host[MAXHOSTNAMELEN];
86 ft = xmalloc(sizeof *ft);
87 RB_INIT(ft);
89 if (gethostname(host, sizeof host) == 0)
90 format_add(ft, "host", "%s", host);
92 return (ft);
95 /* Free a tree. */
96 void
97 format_free(struct format_tree *ft)
99 struct format_entry *fe, *fe_next;
101 fe_next = RB_MIN(format_tree, ft);
102 while (fe_next != NULL) {
103 fe = fe_next;
104 fe_next = RB_NEXT(format_tree, ft, fe);
106 RB_REMOVE(format_tree, ft, fe);
107 free(fe->value);
108 free(fe->key);
109 free(fe);
112 free (ft);
115 /* Add a key-value pair. */
116 void
117 format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
119 struct format_entry *fe;
120 va_list ap;
122 fe = xmalloc(sizeof *fe);
123 fe->key = xstrdup(key);
125 va_start(ap, fmt);
126 xvasprintf(&fe->value, fmt, ap);
127 va_end(ap);
129 RB_INSERT(format_tree, ft, fe);
132 /* Find a format entry. */
133 const char *
134 format_find(struct format_tree *ft, const char *key)
136 struct format_entry *fe, fe_find;
138 fe_find.key = (char *) key;
139 fe = RB_FIND(format_tree, ft, &fe_find);
140 if (fe == NULL)
141 return (NULL);
142 return (fe->value);
146 * Replace a key/value pair in buffer. #{blah} is expanded directly,
147 * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
150 format_replace(struct format_tree *ft,
151 const char *key, size_t keylen, char **buf, size_t *len, size_t *off)
153 char *copy, *ptr;
154 const char *value;
155 size_t valuelen;
157 /* Make a copy of the key. */
158 copy = xmalloc(keylen + 1);
159 memcpy(copy, key, keylen);
160 copy[keylen] = '\0';
163 * Is this a conditional? If so, check it exists and extract either the
164 * first or second element. If not, look up the key directly.
166 if (*copy == '?') {
167 ptr = strchr(copy, ',');
168 if (ptr == NULL)
169 goto fail;
170 *ptr = '\0';
172 value = format_find(ft, copy + 1);
173 if (value != NULL && (value[0] != '0' || value[1] != '\0')) {
174 value = ptr + 1;
175 ptr = strchr(value, ',');
176 if (ptr == NULL)
177 goto fail;
178 *ptr = '\0';
179 } else {
180 ptr = strchr(ptr + 1, ',');
181 if (ptr == NULL)
182 goto fail;
183 value = ptr + 1;
185 } else {
186 value = format_find(ft, copy);
187 if (value == NULL)
188 value = "";
190 valuelen = strlen(value);
192 /* Expand the buffer and copy in the value. */
193 while (*len - *off < valuelen + 1) {
194 *buf = xrealloc(*buf, 2, *len);
195 *len *= 2;
197 memcpy(*buf + *off, value, valuelen);
198 *off += valuelen;
200 free(copy);
201 return (0);
203 fail:
204 free(copy);
205 return (-1);
208 /* Expand keys in a template. */
209 char *
210 format_expand(struct format_tree *ft, const char *fmt)
212 char *buf, *ptr;
213 const char *s;
214 size_t off, len, n;
215 int ch;
217 len = 64;
218 buf = xmalloc(len);
219 off = 0;
221 while (*fmt != '\0') {
222 if (*fmt != '#') {
223 while (len - off < 2) {
224 buf = xrealloc(buf, 2, len);
225 len *= 2;
227 buf[off++] = *fmt++;
228 continue;
230 fmt++;
232 ch = (u_char) *fmt++;
233 switch (ch) {
234 case '{':
235 ptr = strchr(fmt, '}');
236 if (ptr == NULL)
237 break;
238 n = ptr - fmt;
240 if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
241 break;
242 fmt += n + 1;
243 continue;
244 default:
245 if (ch >= 'A' && ch <= 'Z') {
246 s = format_aliases[ch - 'A'];
247 if (s != NULL) {
248 n = strlen(s);
249 if (format_replace (
250 ft, s, n, &buf, &len, &off) != 0)
251 break;
252 continue;
255 while (len - off < 3) {
256 buf = xrealloc(buf, 2, len);
257 len *= 2;
259 buf[off++] = '#';
260 buf[off++] = ch;
261 continue;
264 break;
266 buf[off] = '\0';
268 return (buf);
271 /* Set default format keys for a session. */
272 void
273 format_session(struct format_tree *ft, struct session *s)
275 struct session_group *sg;
276 char *tim;
277 time_t t;
279 format_add(ft, "session_name", "%s", s->name);
280 format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
281 format_add(ft, "session_width", "%u", s->sx);
282 format_add(ft, "session_height", "%u", s->sy);
283 format_add(ft, "session_id", "$%u", s->id);
285 sg = session_group_find(s);
286 format_add(ft, "session_grouped", "%d", sg != NULL);
287 if (sg != NULL)
288 format_add(ft, "session_group", "%u", session_group_index(sg));
290 t = s->creation_time.tv_sec;
291 format_add(ft, "session_created", "%ld", (long) t);
292 tim = ctime(&t);
293 *strchr(tim, '\n') = '\0';
294 format_add(ft, "session_created_string", "%s", tim);
296 if (s->flags & SESSION_UNATTACHED)
297 format_add(ft, "session_attached", "%d", 0);
298 else
299 format_add(ft, "session_attached", "%d", 1);
302 /* Set default format keys for a client. */
303 void
304 format_client(struct format_tree *ft, struct client *c)
306 char *tim;
307 time_t t;
308 struct session *s;
310 format_add(ft, "client_cwd", "%s", c->cwd);
311 format_add(ft, "client_height", "%u", c->tty.sy);
312 format_add(ft, "client_width", "%u", c->tty.sx);
313 format_add(ft, "client_tty", "%s", c->tty.path);
314 format_add(ft, "client_termname", "%s", c->tty.termname);
316 t = c->creation_time.tv_sec;
317 format_add(ft, "client_created", "%ld", (long) t);
318 tim = ctime(&t);
319 *strchr(tim, '\n') = '\0';
320 format_add(ft, "client_created_string", "%s", tim);
322 t = c->activity_time.tv_sec;
323 format_add(ft, "client_activity", "%ld", (long) t);
324 tim = ctime(&t);
325 *strchr(tim, '\n') = '\0';
326 format_add(ft, "client_activity_string", "%s", tim);
328 format_add(ft, "client_prefix", "%d", !!(c->flags & CLIENT_PREFIX));
330 if (c->tty.flags & TTY_UTF8)
331 format_add(ft, "client_utf8", "%d", 1);
332 else
333 format_add(ft, "client_utf8", "%d", 0);
335 if (c->flags & CLIENT_READONLY)
336 format_add(ft, "client_readonly", "%d", 1);
337 else
338 format_add(ft, "client_readonly", "%d", 0);
340 s = c->session;
341 if (s != NULL)
342 format_add(ft, "client_session", "%s", s->name);
343 s = c->last_session;
344 if (s != NULL && session_alive(s))
345 format_add(ft, "client_last_session", "%s", s->name);
348 /* Set default format keys for a winlink. */
349 void
350 format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)
352 struct window *w = wl->window;
353 char *layout, *flags;
355 layout = layout_dump(w);
356 flags = window_printable_flags(s, wl);
358 format_add(ft, "window_id", "@%u", w->id);
359 format_add(ft, "window_index", "%d", wl->idx);
360 format_add(ft, "window_name", "%s", w->name);
361 format_add(ft, "window_width", "%u", w->sx);
362 format_add(ft, "window_height", "%u", w->sy);
363 format_add(ft, "window_flags", "%s", flags);
364 format_add(ft, "window_layout", "%s", layout);
365 format_add(ft, "window_active", "%d", wl == s->curw);
366 format_add(ft, "window_panes", "%u", window_count_panes(w));
368 free(flags);
369 free(layout);
372 /* Add window pane tabs. */
373 void
374 format_window_pane_tabs(struct format_tree *ft, struct window_pane *wp)
376 struct evbuffer *buffer;
377 u_int i;
379 buffer = evbuffer_new();
380 for (i = 0; i < wp->base.grid->sx; i++) {
381 if (!bit_test(wp->base.tabs, i))
382 continue;
384 if (EVBUFFER_LENGTH(buffer) > 0)
385 evbuffer_add(buffer, ",", 1);
386 evbuffer_add_printf(buffer, "%d", i);
389 format_add(ft, "pane_tabs", "%.*s", (int) EVBUFFER_LENGTH(buffer),
390 EVBUFFER_DATA(buffer));
391 evbuffer_free(buffer);
394 /* Set default format keys for a window pane. */
395 void
396 format_window_pane(struct format_tree *ft, struct window_pane *wp)
398 struct grid *gd = wp->base.grid;
399 struct grid_line *gl;
400 unsigned long long size;
401 u_int i, idx;
402 const char *cwd;
403 char *cmd;
405 size = 0;
406 for (i = 0; i < gd->hsize; i++) {
407 gl = &gd->linedata[i];
408 size += gl->cellsize * sizeof *gl->celldata;
410 size += gd->hsize * sizeof *gd->linedata;
411 format_add(ft, "history_size", "%u", gd->hsize);
412 format_add(ft, "history_limit", "%u", gd->hlimit);
413 format_add(ft, "history_bytes", "%llu", size);
415 if (window_pane_index(wp, &idx) != 0)
416 fatalx("index not found");
417 format_add(ft, "pane_index", "%u", idx);
419 format_add(ft, "pane_width", "%u", wp->sx);
420 format_add(ft, "pane_height", "%u", wp->sy);
421 format_add(ft, "pane_title", "%s", wp->base.title);
422 format_add(ft, "pane_id", "%%%u", wp->id);
423 format_add(ft, "pane_active", "%d", wp == wp->window->active);
424 format_add(ft, "pane_dead", "%d", wp->fd == -1);
426 format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
428 if (wp->tty != NULL)
429 format_add(ft, "pane_tty", "%s", wp->tty);
430 format_add(ft, "pane_pid", "%ld", (long) wp->pid);
431 if (wp->cmd != NULL)
432 format_add(ft, "pane_start_command", "%s", wp->cmd);
433 if (wp->cwd != NULL)
434 format_add(ft, "pane_start_path", "%s", wp->cwd);
435 if ((cwd = get_proc_cwd(wp->fd)) != NULL)
436 format_add(ft, "pane_current_path", "%s", cwd);
437 if ((cmd = get_proc_name(wp->fd, wp->tty)) != NULL) {
438 format_add(ft, "pane_current_command", "%s", cmd);
439 free(cmd);
442 format_add(ft, "cursor_x", "%d", wp->base.cx);
443 format_add(ft, "cursor_y", "%d", wp->base.cy);
444 format_add(ft, "scroll_region_upper", "%d", wp->base.rupper);
445 format_add(ft, "scroll_region_lower", "%d", wp->base.rlower);
446 format_add(ft, "saved_cursor_x", "%d", wp->ictx.old_cx);
447 format_add(ft, "saved_cursor_y", "%d", wp->ictx.old_cy);
449 format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
450 format_add(ft, "alternate_saved_x", "%d", wp->saved_cx);
451 format_add(ft, "alternate_saved_y", "%d", wp->saved_cy);
453 format_add(ft, "cursor_flag", "%d",
454 !!(wp->base.mode & MODE_CURSOR));
455 format_add(ft, "insert_flag", "%d",
456 !!(wp->base.mode & MODE_INSERT));
457 format_add(ft, "keypad_cursor_flag", "%d",
458 !!(wp->base.mode & MODE_KCURSOR));
459 format_add(ft, "keypad_flag", "%d",
460 !!(wp->base.mode & MODE_KKEYPAD));
461 format_add(ft, "wrap_flag", "%d",
462 !!(wp->base.mode & MODE_WRAP));
464 format_add(ft, "mouse_standard_flag", "%d",
465 !!(wp->base.mode & MODE_MOUSE_STANDARD));
466 format_add(ft, "mouse_button_flag", "%d",
467 !!(wp->base.mode & MODE_MOUSE_BUTTON));
468 format_add(ft, "mouse_any_flag", "%d",
469 !!(wp->base.mode & MODE_MOUSE_ANY));
470 format_add(ft, "mouse_utf8_flag", "%d",
471 !!(wp->base.mode & MODE_MOUSE_UTF8));
473 format_window_pane_tabs(ft, wp);
476 /* Set default format keys for paste buffer. */
477 void
478 format_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
480 char *pb_print = paste_print(pb, 50);
482 format_add(ft, "buffer_size", "%zu", pb->size);
483 format_add(ft, "buffer_sample", "%s", pb_print);
485 free(pb_print);