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>
31 * Build a list of key-value pairs and use them to expand #{key} entries in a
35 int format_replace(struct format_tree
*, const char *, size_t, char **,
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. */
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] = {
56 "window_flags", /* F */
59 "window_index", /* I */
69 "session_name", /* S */
73 "window_name", /* W */
79 /* Create a new tree. */
83 struct format_tree
*ft
;
84 char host
[MAXHOSTNAMELEN
];
86 ft
= xmalloc(sizeof *ft
);
89 if (gethostname(host
, sizeof host
) == 0)
90 format_add(ft
, "host", "%s", host
);
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
) {
104 fe_next
= RB_NEXT(format_tree
, ft
, fe
);
106 RB_REMOVE(format_tree
, ft
, fe
);
115 /* Add a key-value pair. */
117 format_add(struct format_tree
*ft
, const char *key
, const char *fmt
, ...)
119 struct format_entry
*fe
;
122 fe
= xmalloc(sizeof *fe
);
123 fe
->key
= xstrdup(key
);
126 xvasprintf(&fe
->value
, fmt
, ap
);
129 RB_INSERT(format_tree
, ft
, fe
);
132 /* Find a format entry. */
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
);
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
)
157 /* Make a copy of the key. */
158 copy
= xmalloc(keylen
+ 1);
159 memcpy(copy
, key
, keylen
);
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.
167 ptr
= strchr(copy
, ',');
172 value
= format_find(ft
, copy
+ 1);
173 if (value
!= NULL
&& (value
[0] != '0' || value
[1] != '\0')) {
175 ptr
= strchr(value
, ',');
180 ptr
= strchr(ptr
+ 1, ',');
186 value
= format_find(ft
, copy
);
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
);
197 memcpy(*buf
+ *off
, value
, valuelen
);
208 /* Expand keys in a template. */
210 format_expand(struct format_tree
*ft
, const char *fmt
)
221 while (*fmt
!= '\0') {
223 while (len
- off
< 2) {
224 buf
= xrealloc(buf
, 2, len
);
232 ch
= (u_char
) *fmt
++;
235 ptr
= strchr(fmt
, '}');
240 if (format_replace(ft
, fmt
, n
, &buf
, &len
, &off
) != 0)
245 if (ch
>= 'A' && ch
<= 'Z') {
246 s
= format_aliases
[ch
- 'A'];
250 ft
, s
, n
, &buf
, &len
, &off
) != 0)
255 while (len
- off
< 3) {
256 buf
= xrealloc(buf
, 2, len
);
271 /* Set default format keys for a session. */
273 format_session(struct format_tree
*ft
, struct session
*s
)
275 struct session_group
*sg
;
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
);
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
);
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);
299 format_add(ft
, "session_attached", "%d", 1);
302 /* Set default format keys for a client. */
304 format_client(struct format_tree
*ft
, struct client
*c
)
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
);
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
);
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);
333 format_add(ft
, "client_utf8", "%d", 0);
335 if (c
->flags
& CLIENT_READONLY
)
336 format_add(ft
, "client_readonly", "%d", 1);
338 format_add(ft
, "client_readonly", "%d", 0);
342 format_add(ft
, "client_session", "%s", s
->name
);
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. */
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
));
372 /* Add window pane tabs. */
374 format_window_pane_tabs(struct format_tree
*ft
, struct window_pane
*wp
)
376 struct evbuffer
*buffer
;
379 buffer
= evbuffer_new();
380 for (i
= 0; i
< wp
->base
.grid
->sx
; i
++) {
381 if (!bit_test(wp
->base
.tabs
, i
))
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. */
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
;
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
);
429 format_add(ft
, "pane_tty", "%s", wp
->tty
);
430 format_add(ft
, "pane_pid", "%ld", (long) wp
->pid
);
432 format_add(ft
, "pane_start_command", "%s", wp
->cmd
);
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
);
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. */
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
);