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>
33 * Build a list of key-value pairs and use them to expand #{key} entries in a
37 int format_replace(struct format_tree
*, const char *, size_t, char **,
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. */
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
[] = {
58 "window_flags", /* F */
61 "window_index", /* I */
71 "session_name", /* S */
75 "window_name", /* W */
81 /* Single-character lowercase aliases. */
82 const char *format_lower
[] = {
111 /* Create a new tree. */
115 struct format_tree
*ft
;
116 char host
[MAXHOSTNAMELEN
], *ptr
;
118 ft
= xmalloc(sizeof *ft
);
121 if (gethostname(host
, sizeof host
) == 0) {
122 format_add(ft
, "host", "%s", host
);
123 if ((ptr
= strrchr(host
, '.')) != NULL
)
125 format_add(ft
, "host_short", "%s", host
);
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
) {
140 fe_next
= RB_NEXT(format_tree
, ft
, fe
);
142 RB_REMOVE(format_tree
, ft
, fe
);
151 /* Add a key-value pair. */
153 format_add(struct format_tree
*ft
, const char *key
, const char *fmt
, ...)
155 struct format_entry
*fe
;
156 struct format_entry
*fe_now
;
159 fe
= xmalloc(sizeof *fe
);
160 fe
->key
= xstrdup(key
);
163 xvasprintf(&fe
->value
, fmt
, ap
);
166 fe_now
= RB_INSERT(format_tree
, ft
, fe
);
167 if (fe_now
!= NULL
) {
169 fe_now
->value
= fe
->value
;
175 /* Find a format entry. */
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
);
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
;
199 u_long limit
= ULONG_MAX
;
201 /* Make a copy of the key. */
202 copy0
= copy
= xmalloc(keylen
+ 1);
203 memcpy(copy
, key
, keylen
);
206 /* Is there a length limit or whatnot? */
207 if (!islower((u_char
) *copy
) && *copy
!= '?') {
208 while (*copy
!= ':' && *copy
!= '\0') {
212 limit
= strtoul(copy
+ 1, &endptr
, 10);
213 if (errno
== ERANGE
&& limit
== ULONG_MAX
)
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.
232 ptr
= strchr(copy
, ',');
237 value
= format_find(ft
, copy
+ 1);
238 if (value
!= NULL
&& (value
[0] != '0' || value
[1] != '\0')) {
240 ptr
= strchr(value
, ',');
245 ptr
= strchr(ptr
+ 1, ',');
250 saved
= format_expand(ft
, value
);
253 value
= format_find(ft
, copy
);
258 valuelen
= strlen(value
);
260 /* Truncate the value if needed. */
261 if (valuelen
> limit
)
264 /* Expand the buffer and copy in the value. */
265 while (*len
- *off
< valuelen
+ 1) {
266 *buf
= xrealloc(*buf
, 2, *len
);
269 memcpy(*buf
+ *off
, value
, valuelen
);
281 /* Expand keys in a template. */
283 format_expand(struct format_tree
*ft
, const char *fmt
)
294 while (*fmt
!= '\0') {
296 while (len
- off
< 2) {
297 buf
= xrealloc(buf
, 2, len
);
305 ch
= (u_char
) *fmt
++;
309 for (ptr
= fmt
; *ptr
!= '\0'; ptr
++) {
312 if (*ptr
== '}' && --brackets
== 0)
315 if (*ptr
!= '}' || brackets
!= 0)
319 if (format_replace(ft
, fmt
, n
, &buf
, &len
, &off
) != 0)
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'];
330 while (len
- off
< 3) {
331 buf
= xrealloc(buf
, 2, len
);
339 if (format_replace(ft
, s
, n
, &buf
, &len
, &off
) != 0)
351 /* Set default format keys for a session. */
353 format_session(struct format_tree
*ft
, struct session
*s
)
355 struct session_group
*sg
;
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
);
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
);
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);
379 format_add(ft
, "session_attached", "%d", 1);
382 /* Set default format keys for a client. */
384 format_client(struct format_tree
*ft
, struct client
*c
)
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
);
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
);
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);
415 format_add(ft
, "client_utf8", "%d", 0);
417 if (c
->flags
& CLIENT_READONLY
)
418 format_add(ft
, "client_readonly", "%d", 1);
420 format_add(ft
, "client_readonly", "%d", 0);
424 format_add(ft
, "client_session", "%s", s
->name
);
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. */
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
));
463 /* Add window pane tabs. */
465 format_window_pane_tabs(struct format_tree
*ft
, struct window_pane
*wp
)
467 struct evbuffer
*buffer
;
470 buffer
= evbuffer_new();
471 for (i
= 0; i
< wp
->base
.grid
->sx
; i
++) {
472 if (!bit_test(wp
->base
.tabs
, i
))
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. */
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
;
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"));
522 format_add(ft
, "pane_tty", "%s", wp
->tty
);
523 format_add(ft
, "pane_pid", "%ld", (long) wp
->pid
);
525 format_add(ft
, "pane_start_command", "%s", wp
->cmd
);
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
);
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. */
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
);