Line | Count | Source |
1 | | /* $OpenBSD: format.c,v 1.431 2026/10/16 12:21:12 nicm Exp $ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com> |
5 | | * |
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. |
9 | | * |
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. |
17 | | */ |
18 | | |
19 | | #include <sys/types.h> |
20 | | #include <sys/wait.h> |
21 | | |
22 | | #include <ctype.h> |
23 | | #include <errno.h> |
24 | | #include <fnmatch.h> |
25 | | #include <libgen.h> |
26 | | #include <math.h> |
27 | | #include <pwd.h> |
28 | | #include <regex.h> |
29 | | #include <stdarg.h> |
30 | | #include <stdlib.h> |
31 | | #include <string.h> |
32 | | #include <time.h> |
33 | | #include <unistd.h> |
34 | | |
35 | | #include "tmux.h" |
36 | | |
37 | | /* |
38 | | * Build a list of key-value pairs and use them to expand #{key} entries in a |
39 | | * string. |
40 | | */ |
41 | | |
42 | | struct format_expand_state; |
43 | | |
44 | | static char *format_job_get(struct format_expand_state *, const char *); |
45 | | static char *format_quote_shell_single(const char *); |
46 | | static char *format_expand1(struct format_expand_state *, const char *); |
47 | | static int format_replace(struct format_expand_state *, const char *, |
48 | | size_t, char **, size_t *, size_t *); |
49 | | static void format_defaults_session(struct format_tree *, |
50 | | struct session *); |
51 | | static void format_defaults_client(struct format_tree *, struct client *); |
52 | | static void format_defaults_winlink(struct format_tree *, |
53 | | struct winlink *); |
54 | | |
55 | | /* Entry in format job tree. */ |
56 | | struct format_job { |
57 | | struct client *client; |
58 | | u_int tag; |
59 | | const char *cmd; |
60 | | const char *expanded; |
61 | | |
62 | | time_t last; |
63 | | char *out; |
64 | | int updated; |
65 | | |
66 | | struct job *job; |
67 | | int status; |
68 | | |
69 | | RB_ENTRY(format_job) entry; |
70 | | }; |
71 | | |
72 | | /* Format job tree. */ |
73 | | static int format_job_cmp(struct format_job *, struct format_job *); |
74 | | static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER(); |
75 | 0 | RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp); Unexecuted instantiation: format.c:format_job_tree_RB_MINMAX Unexecuted instantiation: format.c:format_job_tree_RB_REMOVE Unexecuted instantiation: format.c:format_job_tree_RB_REMOVE_COLOR Unexecuted instantiation: format.c:format_job_tree_RB_FIND Unexecuted instantiation: format.c:format_job_tree_RB_INSERT |
76 | 0 |
|
77 | 0 | /* Format job tree comparison function. */ |
78 | 0 | static int |
79 | 0 | format_job_cmp(struct format_job *fj1, struct format_job *fj2) |
80 | 0 | { |
81 | 0 | if (fj1->tag < fj2->tag) |
82 | 0 | return (-1); |
83 | 0 | if (fj1->tag > fj2->tag) |
84 | 0 | return (1); |
85 | 0 | return (strcmp(fj1->cmd, fj2->cmd)); |
86 | 0 | } |
87 | | |
88 | | /* Maximum pad and trim width. */ |
89 | 552k | #define FORMAT_MAX_WIDTH 10000 |
90 | | |
91 | | /* Maximum repeat count. */ |
92 | 550k | #define FORMAT_MAX_REPEAT 10000 |
93 | | |
94 | | /* Maximum repeat result size in bytes. */ |
95 | 453k | #define FORMAT_MAX_REPEAT_SIZE 65536 |
96 | | |
97 | | /* Maximum precision. */ |
98 | 3.16k | #define FORMAT_MAX_PRECISION 100 |
99 | | |
100 | | /* Format modifiers. */ |
101 | 3.66M | #define FORMAT_TIMESTRING 0x1 |
102 | 714k | #define FORMAT_BASENAME 0x2 |
103 | 714k | #define FORMAT_DIRNAME 0x4 |
104 | 714k | #define FORMAT_QUOTE_SHELL 0x8 |
105 | 6.19M | #define FORMAT_LITERAL 0x10 |
106 | 6.14M | #define FORMAT_EXPAND 0x20 |
107 | 6.08M | #define FORMAT_EXPANDTIME 0x40 |
108 | 6.05M | #define FORMAT_SESSIONS 0x80 |
109 | 5.98M | #define FORMAT_WINDOWS 0x100 |
110 | 6.03M | #define FORMAT_PANES 0x200 |
111 | 7.26k | #define FORMAT_PRETTY 0x400 |
112 | 6.12M | #define FORMAT_LENGTH 0x800 |
113 | 6.11M | #define FORMAT_WIDTH 0x1000 |
114 | 716k | #define FORMAT_QUOTE_STYLE 0x2000 |
115 | 5.53M | #define FORMAT_WINDOW_NAME 0x4000 |
116 | 5.53M | #define FORMAT_SESSION_NAME 0x8000 |
117 | 6.19M | #define FORMAT_CHARACTER 0x10000 |
118 | 6.34M | #define FORMAT_COLOUR 0x20000 |
119 | 5.93M | #define FORMAT_CLIENTS 0x40000 |
120 | 5.01M | #define FORMAT_NOT 0x80000 |
121 | 4.90M | #define FORMAT_NOT_NOT 0x100000 |
122 | 6.09M | #define FORMAT_REPEAT 0x200000 |
123 | 741k | #define FORMAT_QUOTE_ARGUMENTS 0x400000 |
124 | 20.0k | #define FORMAT_RELATIVE 0x800000 |
125 | 6.21M | #define FORMAT_CLIENT_TERMCAP 0x1000000 |
126 | 6.21M | #define FORMAT_CLIENT_TERMFEAT 0x2000000 |
127 | 6.21M | #define FORMAT_CLIENT_ENVIRON 0x4000000 |
128 | 167k | #define FORMAT_COLOUR_ESC_FG 0x8000000 |
129 | 201k | #define FORMAT_COLOUR_ESC_BG 0x10000000 |
130 | 715k | #define FORMAT_QUOTE_SHELL_SQ 0x20000000 |
131 | 6.01M | #define FORMAT_OPTIONS 0x40000000 |
132 | 6.13M | #define FORMAT_ENVIRON 0x80000000ULL |
133 | 5.00k | #define FORMAT_DIFFERENCE 0x100000000ULL |
134 | 6.23M | #define FORMAT_CYCLE 0x200000000ULL |
135 | | |
136 | | /* Limit on recursion. */ |
137 | 10.1M | #define FORMAT_LOOP_LIMIT 100 |
138 | | |
139 | | /* Limit on time taken (milliseconds). */ |
140 | 23.2M | #define FORMAT_TIME_LIMIT 100 |
141 | | |
142 | | /* How often to check the time in long loops. */ |
143 | 971M | #define FORMAT_TIME_LOOP_CHECK 10000 |
144 | | |
145 | | /* Fixed animation period (ms): redraw interval and shortest frame step. */ |
146 | 0 | #define FORMAT_CYCLE_PERIOD 100 |
147 | | |
148 | | /* Format expand flags. */ |
149 | 9.92M | #define FORMAT_EXPAND_TIME 0x1 |
150 | 0 | #define FORMAT_EXPAND_NOJOBS 0x2 |
151 | 0 | #define FORMAT_EXPAND_NOCYCLE 0x4 |
152 | | |
153 | | /* Entry in format tree. */ |
154 | | struct format_entry { |
155 | | char *key; |
156 | | char *value; |
157 | | time_t time; |
158 | | format_cb cb; |
159 | | RB_ENTRY(format_entry) entry; |
160 | | }; |
161 | | |
162 | | /* Format type. */ |
163 | | enum format_type { |
164 | | FORMAT_TYPE_UNKNOWN, |
165 | | FORMAT_TYPE_SESSION, |
166 | | FORMAT_TYPE_WINDOW, |
167 | | FORMAT_TYPE_PANE |
168 | | }; |
169 | | |
170 | | static struct sort_criteria sort_crit; |
171 | | |
172 | | struct format_tree { |
173 | | enum format_type type; |
174 | | |
175 | | struct client *c; |
176 | | struct session *s; |
177 | | struct winlink *wl; |
178 | | struct window *w; |
179 | | struct window_pane *wp; |
180 | | struct paste_buffer *pb; |
181 | | |
182 | | struct cmdq_item *item; |
183 | | struct client *client; |
184 | | int flags; |
185 | | u_int tag; |
186 | | |
187 | | struct mouse_event m; |
188 | | |
189 | | RB_HEAD(format_entry_tree, format_entry) tree; |
190 | | }; |
191 | | static int format_entry_cmp(struct format_entry *, struct format_entry *); |
192 | 675M | RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp); format.c:format_entry_tree_RB_MINMAX Line | Count | Source | 192 | | RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp); |
format.c:format_entry_tree_RB_REMOVE Line | Count | Source | 192 | | RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp); |
format.c:format_entry_tree_RB_REMOVE_COLOR Line | Count | Source | 192 | | RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp); |
format.c:format_entry_tree_RB_INSERT Line | Count | Source | 192 | | RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp); |
format.c:format_entry_tree_RB_FIND Line | Count | Source | 192 | | RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp); |
|
193 | 675M | |
194 | 675M | /* Format expand state. */ |
195 | 675M | struct format_expand_state { |
196 | 675M | struct format_tree *ft; |
197 | 675M | u_int loop; |
198 | 675M | uint64_t start_time; |
199 | 675M | int flags; |
200 | 675M | |
201 | 675M | time_t time; |
202 | 675M | struct tm tm; |
203 | 675M | }; |
204 | 675M | |
205 | 675M | /* Format modifier. */ |
206 | 675M | struct format_modifier { |
207 | 675M | char modifier[3]; |
208 | 675M | u_int size; |
209 | 675M | |
210 | 675M | char **argv; |
211 | 675M | int argc; |
212 | 675M | }; |
213 | 675M | |
214 | 675M | /* Format entry tree comparison function. */ |
215 | 675M | static int |
216 | 675M | format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2) |
217 | 675M | { |
218 | 179M | return (strcmp(fe1->key, fe2->key)); |
219 | 179M | } |
220 | | |
221 | | /* Single-character uppercase aliases. */ |
222 | | static const char *format_upper[] = { |
223 | | NULL, /* A */ |
224 | | NULL, /* B */ |
225 | | NULL, /* C */ |
226 | | "pane_id", /* D */ |
227 | | NULL, /* E */ |
228 | | "window_flags", /* F */ |
229 | | NULL, /* G */ |
230 | | "host", /* H */ |
231 | | "window_index", /* I */ |
232 | | NULL, /* J */ |
233 | | NULL, /* K */ |
234 | | NULL, /* L */ |
235 | | NULL, /* M */ |
236 | | NULL, /* N */ |
237 | | NULL, /* O */ |
238 | | "pane_index", /* P */ |
239 | | NULL, /* Q */ |
240 | | NULL, /* R */ |
241 | | "session_name", /* S */ |
242 | | "pane_title", /* T */ |
243 | | NULL, /* U */ |
244 | | NULL, /* V */ |
245 | | "window_name", /* W */ |
246 | | NULL, /* X */ |
247 | | NULL, /* Y */ |
248 | | NULL /* Z */ |
249 | | }; |
250 | | |
251 | | /* Single-character lowercase aliases. */ |
252 | | static const char *format_lower[] = { |
253 | | NULL, /* a */ |
254 | | NULL, /* b */ |
255 | | NULL, /* c */ |
256 | | NULL, /* d */ |
257 | | NULL, /* e */ |
258 | | NULL, /* f */ |
259 | | NULL, /* g */ |
260 | | "host_short", /* h */ |
261 | | NULL, /* i */ |
262 | | NULL, /* j */ |
263 | | NULL, /* k */ |
264 | | NULL, /* l */ |
265 | | NULL, /* m */ |
266 | | NULL, /* n */ |
267 | | NULL, /* o */ |
268 | | NULL, /* p */ |
269 | | NULL, /* q */ |
270 | | NULL, /* r */ |
271 | | NULL, /* s */ |
272 | | NULL, /* t */ |
273 | | NULL, /* u */ |
274 | | NULL, /* v */ |
275 | | NULL, /* w */ |
276 | | NULL, /* x */ |
277 | | NULL, /* y */ |
278 | | NULL /* z */ |
279 | | }; |
280 | | |
281 | | /* Is logging enabled? */ |
282 | | static inline int |
283 | | format_logging(struct format_tree *ft) |
284 | 52.7M | { |
285 | 52.7M | return (log_get_level() != 0 || (ft->flags & FORMAT_VERBOSE)); |
286 | 52.7M | } |
287 | | |
288 | | /* Log a message if verbose. */ |
289 | | static void printflike(3, 4) |
290 | | format_log1(struct format_expand_state *es, const char *from, const char *fmt, |
291 | | ...) |
292 | 50.6M | { |
293 | 50.6M | struct format_tree *ft = es->ft; |
294 | 50.6M | va_list ap; |
295 | 50.6M | char *s; |
296 | 50.6M | static const char spaces[] = " "; |
297 | | |
298 | 50.6M | if (!format_logging(ft)) |
299 | 50.6M | return; |
300 | | |
301 | 50.6M | va_start(ap, fmt); |
302 | 0 | xvasprintf(&s, fmt, ap); |
303 | 0 | va_end(ap); |
304 | |
|
305 | 0 | log_debug("%s: %s", from, s); |
306 | 0 | if (ft->item != NULL && (ft->flags & FORMAT_VERBOSE)) |
307 | 0 | cmdq_print(ft->item, "#%.*s%s", es->loop, spaces, s); |
308 | |
|
309 | 0 | free(s); |
310 | 0 | } |
311 | 50.6M | #define format_log(es, fmt, ...) format_log1(es, __func__, fmt, ##__VA_ARGS__) |
312 | | |
313 | | /* Copy expand state. */ |
314 | | static void |
315 | | format_copy_state(struct format_expand_state *to, |
316 | | struct format_expand_state *from, int flags) |
317 | 6.57M | { |
318 | 6.57M | to->ft = from->ft; |
319 | 6.57M | to->loop = from->loop; |
320 | 6.57M | to->time = from->time; |
321 | 6.57M | memcpy(&to->tm, &from->tm, sizeof to->tm); |
322 | 6.57M | to->flags = from->flags|flags; |
323 | 6.57M | to->start_time = from->start_time; |
324 | 6.57M | } |
325 | | |
326 | | /* Format job update callback. */ |
327 | | static void |
328 | | format_job_update(struct job *job) |
329 | 0 | { |
330 | 0 | struct format_job *fj = job_get_data(job); |
331 | 0 | struct evbuffer *evb = job_get_event(job)->input; |
332 | 0 | char *line = NULL, *next; |
333 | 0 | time_t t; |
334 | |
|
335 | 0 | while ((next = evbuffer_readline(evb)) != NULL) { |
336 | 0 | free(line); |
337 | 0 | line = next; |
338 | 0 | } |
339 | 0 | if (line == NULL) |
340 | 0 | return; |
341 | 0 | fj->updated = 1; |
342 | |
|
343 | 0 | free(fj->out); |
344 | 0 | fj->out = line; |
345 | |
|
346 | 0 | log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out); |
347 | |
|
348 | 0 | t = time(NULL); |
349 | 0 | if (fj->status && fj->last != t) { |
350 | 0 | if (fj->client != NULL) |
351 | 0 | server_status_client(fj->client); |
352 | 0 | fj->last = t; |
353 | 0 | } |
354 | 0 | } |
355 | | |
356 | | /* Format job complete callback. */ |
357 | | static void |
358 | | format_job_complete(struct job *job) |
359 | 0 | { |
360 | 0 | struct format_job *fj = job_get_data(job); |
361 | 0 | struct evbuffer *evb = job_get_event(job)->input; |
362 | 0 | char *line, *buf; |
363 | 0 | size_t len; |
364 | |
|
365 | 0 | fj->job = NULL; |
366 | |
|
367 | 0 | buf = NULL; |
368 | 0 | if ((line = evbuffer_readline(evb)) == NULL) { |
369 | 0 | len = EVBUFFER_LENGTH(evb); |
370 | 0 | buf = xmalloc(len + 1); |
371 | 0 | if (len != 0) |
372 | 0 | memcpy(buf, EVBUFFER_DATA(evb), len); |
373 | 0 | buf[len] = '\0'; |
374 | 0 | } else |
375 | 0 | buf = line; |
376 | |
|
377 | 0 | log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf); |
378 | |
|
379 | 0 | if (*buf != '\0' || !fj->updated) { |
380 | 0 | free(fj->out); |
381 | 0 | fj->out = buf; |
382 | 0 | } else |
383 | 0 | free(buf); |
384 | |
|
385 | 0 | if (fj->status) { |
386 | 0 | if (fj->client != NULL) |
387 | 0 | server_status_client(fj->client); |
388 | 0 | fj->status = 0; |
389 | 0 | } |
390 | 0 | } |
391 | | |
392 | | /* Find a job. */ |
393 | | static char * |
394 | | format_job_get(struct format_expand_state *es, const char *cmd) |
395 | 0 | { |
396 | 0 | struct format_tree *ft = es->ft; |
397 | 0 | struct format_job_tree *jobs; |
398 | 0 | struct format_job fj0, *fj; |
399 | 0 | time_t t; |
400 | 0 | char *expanded; |
401 | 0 | int force; |
402 | 0 | struct format_expand_state next; |
403 | |
|
404 | 0 | if (ft->client == NULL) |
405 | 0 | jobs = &format_jobs; |
406 | 0 | else if (ft->client->jobs != NULL) |
407 | 0 | jobs = ft->client->jobs; |
408 | 0 | else { |
409 | 0 | jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs); |
410 | 0 | RB_INIT(jobs); |
411 | 0 | } |
412 | |
|
413 | 0 | fj0.tag = ft->tag; |
414 | 0 | fj0.cmd = cmd; |
415 | 0 | if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) { |
416 | 0 | fj = xcalloc(1, sizeof *fj); |
417 | 0 | fj->client = ft->client; |
418 | 0 | fj->tag = ft->tag; |
419 | 0 | fj->cmd = xstrdup(cmd); |
420 | |
|
421 | 0 | RB_INSERT(format_job_tree, jobs, fj); |
422 | 0 | } |
423 | |
|
424 | 0 | format_copy_state(&next, es, FORMAT_EXPAND_NOJOBS| |
425 | 0 | FORMAT_EXPAND_NOCYCLE); |
426 | 0 | next.flags &= ~FORMAT_EXPAND_TIME; |
427 | |
|
428 | 0 | expanded = format_expand1(&next, cmd); |
429 | 0 | if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) { |
430 | 0 | free((void *)fj->expanded); |
431 | 0 | fj->expanded = xstrdup(expanded); |
432 | 0 | force = 1; |
433 | 0 | } else |
434 | 0 | force = (ft->flags & FORMAT_FORCE); |
435 | |
|
436 | 0 | t = time(NULL); |
437 | 0 | if (force && fj->job != NULL) |
438 | 0 | job_free(fj->job); |
439 | 0 | if (force || (fj->job == NULL && fj->last != t)) { |
440 | 0 | fj->job = job_run(expanded, 0, NULL, NULL, NULL, |
441 | 0 | server_client_get_cwd(ft->client, NULL), format_job_update, |
442 | 0 | format_job_complete, NULL, fj, JOB_NOWAIT, -1, -1); |
443 | 0 | if (fj->job == NULL) { |
444 | 0 | free(fj->out); |
445 | 0 | xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd); |
446 | 0 | } |
447 | 0 | fj->last = t; |
448 | 0 | fj->updated = 0; |
449 | 0 | } else if (fj->job != NULL && (t - fj->last) > 1 && fj->out == NULL) |
450 | 0 | xasprintf(&fj->out, "<'%s' not ready>", fj->cmd); |
451 | 0 | free(expanded); |
452 | |
|
453 | 0 | if (ft->flags & FORMAT_STATUS) |
454 | 0 | fj->status = 1; |
455 | 0 | if (fj->out == NULL) |
456 | 0 | return (xstrdup("")); |
457 | 0 | return (format_expand1(&next, fj->out)); |
458 | 0 | } |
459 | | |
460 | | /* Remove old jobs. */ |
461 | | static void |
462 | | format_job_tidy(struct format_job_tree *jobs, int force) |
463 | 0 | { |
464 | 0 | struct format_job *fj, *fj1; |
465 | 0 | time_t now; |
466 | |
|
467 | 0 | now = time(NULL); |
468 | 0 | RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) { |
469 | 0 | if (!force && (fj->last > now || now - fj->last < 3600)) |
470 | 0 | continue; |
471 | 0 | RB_REMOVE(format_job_tree, jobs, fj); |
472 | |
|
473 | 0 | log_debug("%s: %s", __func__, fj->cmd); |
474 | |
|
475 | 0 | if (fj->job != NULL) |
476 | 0 | job_free(fj->job); |
477 | |
|
478 | 0 | free((void *)fj->expanded); |
479 | 0 | free((void *)fj->cmd); |
480 | 0 | free(fj->out); |
481 | |
|
482 | 0 | free(fj); |
483 | 0 | } |
484 | 0 | } |
485 | | |
486 | | /* Work around needless -Wformat-nonliteral gcc warning. */ |
487 | | #ifdef __GNUC__ |
488 | | #pragma GCC diagnostic push |
489 | | #pragma GCC diagnostic ignored "-Wformat-nonliteral" |
490 | | #endif |
491 | | static size_t |
492 | | format_strftime(char *s, size_t max, const char *fmt, const struct tm *tm) |
493 | 2.69k | { |
494 | 2.69k | return (strftime(s, max, fmt, tm)); |
495 | 2.69k | } |
496 | | #ifdef __GNUC__ |
497 | | #pragma GCC diagnostic pop |
498 | | #endif |
499 | | |
500 | | /* Tidy old jobs for all clients. */ |
501 | | void |
502 | | format_tidy_jobs(void) |
503 | 0 | { |
504 | 0 | struct client *c; |
505 | |
|
506 | 0 | format_job_tidy(&format_jobs, 0); |
507 | 0 | TAILQ_FOREACH(c, &clients, entry) { |
508 | 0 | if (c->jobs != NULL) |
509 | 0 | format_job_tidy(c->jobs, 0); |
510 | 0 | } |
511 | 0 | } |
512 | | |
513 | | /* Remove old jobs for client. */ |
514 | | void |
515 | | format_lost_client(struct client *c) |
516 | 0 | { |
517 | 0 | if (c->jobs != NULL) |
518 | 0 | format_job_tidy(c->jobs, 1); |
519 | 0 | free(c->jobs); |
520 | 0 | } |
521 | | |
522 | | /* Wrapper for asprintf. */ |
523 | | static char * printflike(1, 2) |
524 | | format_printf(const char *fmt, ...) |
525 | 2.44k | { |
526 | 2.44k | va_list ap; |
527 | 2.44k | char *s; |
528 | | |
529 | 2.44k | va_start(ap, fmt); |
530 | 2.44k | xvasprintf(&s, fmt, ap); |
531 | 2.44k | va_end(ap); |
532 | 2.44k | return (s); |
533 | 2.44k | } |
534 | | |
535 | | /* Callback for host. */ |
536 | | static void * |
537 | | format_cb_host(__unused struct format_tree *ft) |
538 | 97.9k | { |
539 | 97.9k | char host[HOST_NAME_MAX + 1]; |
540 | | |
541 | 97.9k | if (gethostname(host, sizeof host) != 0) |
542 | 0 | return (xstrdup("")); |
543 | 97.9k | return (xstrdup(host)); |
544 | 97.9k | } |
545 | | |
546 | | /* Callback for host_short. */ |
547 | | static void * |
548 | | format_cb_host_short(__unused struct format_tree *ft) |
549 | 174k | { |
550 | 174k | char host[HOST_NAME_MAX + 1], *cp; |
551 | | |
552 | 174k | if (gethostname(host, sizeof host) != 0) |
553 | 0 | return (xstrdup("")); |
554 | 174k | if ((cp = strchr(host, '.')) != NULL) |
555 | 0 | *cp = '\0'; |
556 | 174k | return (xstrdup(host)); |
557 | 174k | } |
558 | | |
559 | | /* Callback for pid. */ |
560 | | static void * |
561 | | format_cb_pid(__unused struct format_tree *ft) |
562 | 5.78k | { |
563 | 5.78k | char *value; |
564 | | |
565 | 5.78k | xasprintf(&value, "%ld", (long)getpid()); |
566 | 5.78k | return (value); |
567 | 5.78k | } |
568 | | |
569 | | /* Callback for session_attached_list. */ |
570 | | static void * |
571 | | format_cb_session_attached_list(struct format_tree *ft) |
572 | 388 | { |
573 | 388 | struct session *s = ft->s; |
574 | 388 | struct client *loop; |
575 | 388 | struct evbuffer *buffer; |
576 | 388 | int size; |
577 | 388 | char *value = NULL; |
578 | | |
579 | 388 | if (s == NULL) |
580 | 388 | return (NULL); |
581 | | |
582 | 0 | buffer = evbuffer_new(); |
583 | 0 | if (buffer == NULL) |
584 | 0 | fatalx("out of memory"); |
585 | | |
586 | 0 | TAILQ_FOREACH(loop, &clients, entry) { |
587 | 0 | if (loop->session == s) { |
588 | 0 | if (EVBUFFER_LENGTH(buffer) > 0) |
589 | 0 | evbuffer_add(buffer, ",", 1); |
590 | 0 | evbuffer_add_printf(buffer, "%s", loop->name); |
591 | 0 | } |
592 | 0 | } |
593 | |
|
594 | 0 | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
595 | 0 | value = xmemdup(EVBUFFER_DATA(buffer), size); |
596 | 0 | evbuffer_free(buffer); |
597 | 0 | return (value); |
598 | 0 | } |
599 | | |
600 | | /* Callback for session_alert. */ |
601 | | static void * |
602 | | format_cb_session_alert(struct format_tree *ft) |
603 | 388 | { |
604 | 388 | struct session *s = ft->s; |
605 | 388 | struct winlink *wl; |
606 | 388 | char alerts[1024]; |
607 | 388 | int alerted = 0; |
608 | | |
609 | 388 | if (s == NULL) |
610 | 388 | return (NULL); |
611 | | |
612 | 0 | *alerts = '\0'; |
613 | 0 | RB_FOREACH(wl, winlinks, &s->windows) { |
614 | 0 | if ((wl->flags & WINLINK_ALERTFLAGS) == 0) |
615 | 0 | continue; |
616 | 0 | if (~alerted & wl->flags & WINLINK_ACTIVITY) { |
617 | 0 | strlcat(alerts, "#", sizeof alerts); |
618 | 0 | alerted |= WINLINK_ACTIVITY; |
619 | 0 | } |
620 | 0 | if (~alerted & wl->flags & WINLINK_BELL) { |
621 | 0 | strlcat(alerts, "!", sizeof alerts); |
622 | 0 | alerted |= WINLINK_BELL; |
623 | 0 | } |
624 | 0 | if (~alerted & wl->flags & WINLINK_SILENCE) { |
625 | 0 | strlcat(alerts, "~", sizeof alerts); |
626 | 0 | alerted |= WINLINK_SILENCE; |
627 | 0 | } |
628 | 0 | } |
629 | 0 | return (xstrdup(alerts)); |
630 | 388 | } |
631 | | |
632 | | /* Callback for session_alerts. */ |
633 | | static void * |
634 | | format_cb_session_alerts(struct format_tree *ft) |
635 | 388 | { |
636 | 388 | struct session *s = ft->s; |
637 | 388 | struct winlink *wl; |
638 | 388 | char alerts[1024], tmp[16]; |
639 | | |
640 | 388 | if (s == NULL) |
641 | 388 | return (NULL); |
642 | | |
643 | 0 | *alerts = '\0'; |
644 | 0 | RB_FOREACH(wl, winlinks, &s->windows) { |
645 | 0 | if ((wl->flags & WINLINK_ALERTFLAGS) == 0) |
646 | 0 | continue; |
647 | 0 | xsnprintf(tmp, sizeof tmp, "%u", wl->idx); |
648 | |
|
649 | 0 | if (*alerts != '\0') |
650 | 0 | strlcat(alerts, ",", sizeof alerts); |
651 | 0 | strlcat(alerts, tmp, sizeof alerts); |
652 | 0 | if (wl->flags & WINLINK_ACTIVITY) |
653 | 0 | strlcat(alerts, "#", sizeof alerts); |
654 | 0 | if (wl->flags & WINLINK_BELL) |
655 | 0 | strlcat(alerts, "!", sizeof alerts); |
656 | 0 | if (wl->flags & WINLINK_SILENCE) |
657 | 0 | strlcat(alerts, "~", sizeof alerts); |
658 | 0 | } |
659 | 0 | return (xstrdup(alerts)); |
660 | 388 | } |
661 | | |
662 | | /* Callback for session_stack. */ |
663 | | static void * |
664 | | format_cb_session_stack(struct format_tree *ft) |
665 | 482 | { |
666 | 482 | struct session *s = ft->s; |
667 | 482 | struct winlink *wl; |
668 | 482 | char result[1024], tmp[16]; |
669 | | |
670 | 482 | if (s == NULL) |
671 | 482 | return (NULL); |
672 | | |
673 | 0 | xsnprintf(result, sizeof result, "%u", s->curw->idx); |
674 | 0 | TAILQ_FOREACH(wl, &s->lastw, sentry) { |
675 | 0 | xsnprintf(tmp, sizeof tmp, "%u", wl->idx); |
676 | |
|
677 | 0 | if (*result != '\0') |
678 | 0 | strlcat(result, ",", sizeof result); |
679 | 0 | strlcat(result, tmp, sizeof result); |
680 | 0 | } |
681 | 0 | return (xstrdup(result)); |
682 | 482 | } |
683 | | |
684 | | /* Callback for window_stack_index. */ |
685 | | static void * |
686 | | format_cb_window_stack_index(struct format_tree *ft) |
687 | 388 | { |
688 | 388 | struct session *s; |
689 | 388 | struct winlink *wl; |
690 | 388 | u_int idx; |
691 | 388 | char *value = NULL; |
692 | | |
693 | 388 | if (ft->wl == NULL) |
694 | 388 | return (NULL); |
695 | 0 | s = ft->wl->session; |
696 | |
|
697 | 0 | idx = 0; |
698 | 0 | TAILQ_FOREACH(wl, &s->lastw, sentry) { |
699 | 0 | idx++; |
700 | 0 | if (wl == ft->wl) |
701 | 0 | break; |
702 | 0 | } |
703 | 0 | if (wl == NULL) |
704 | 0 | return (xstrdup("0")); |
705 | 0 | xasprintf(&value, "%u", idx); |
706 | 0 | return (value); |
707 | 0 | } |
708 | | |
709 | | /* Callback for window_linked_sessions_list. */ |
710 | | static void * |
711 | | format_cb_window_linked_sessions_list(struct format_tree *ft) |
712 | 478 | { |
713 | 478 | struct window *w; |
714 | 478 | struct winlink *wl; |
715 | 478 | struct evbuffer *buffer; |
716 | 478 | int size; |
717 | 478 | char *value = NULL; |
718 | | |
719 | 478 | if (ft->wl == NULL) |
720 | 478 | return (NULL); |
721 | 0 | w = ft->wl->window; |
722 | |
|
723 | 0 | buffer = evbuffer_new(); |
724 | 0 | if (buffer == NULL) |
725 | 0 | fatalx("out of memory"); |
726 | | |
727 | 0 | TAILQ_FOREACH(wl, &w->winlinks, wentry) { |
728 | 0 | if (EVBUFFER_LENGTH(buffer) > 0) |
729 | 0 | evbuffer_add(buffer, ",", 1); |
730 | 0 | evbuffer_add_printf(buffer, "%s", wl->session->name); |
731 | 0 | } |
732 | |
|
733 | 0 | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
734 | 0 | value = xmemdup(EVBUFFER_DATA(buffer), size); |
735 | 0 | evbuffer_free(buffer); |
736 | 0 | return (value); |
737 | 0 | } |
738 | | |
739 | | /* Callback for window_active_sessions. */ |
740 | | static void * |
741 | | format_cb_window_active_sessions(struct format_tree *ft) |
742 | 388 | { |
743 | 388 | struct window *w; |
744 | 388 | struct winlink *wl; |
745 | 388 | u_int n = 0; |
746 | 388 | char *value; |
747 | | |
748 | 388 | if (ft->wl == NULL) |
749 | 388 | return (NULL); |
750 | 0 | w = ft->wl->window; |
751 | |
|
752 | 0 | TAILQ_FOREACH(wl, &w->winlinks, wentry) { |
753 | 0 | if (wl->session->curw == wl) |
754 | 0 | n++; |
755 | 0 | } |
756 | |
|
757 | 0 | xasprintf(&value, "%u", n); |
758 | 0 | return (value); |
759 | 388 | } |
760 | | |
761 | | /* Callback for window_active_sessions_list. */ |
762 | | static void * |
763 | | format_cb_window_active_sessions_list(struct format_tree *ft) |
764 | 478 | { |
765 | 478 | struct window *w; |
766 | 478 | struct winlink *wl; |
767 | 478 | struct evbuffer *buffer; |
768 | 478 | int size; |
769 | 478 | char *value = NULL; |
770 | | |
771 | 478 | if (ft->wl == NULL) |
772 | 478 | return (NULL); |
773 | 0 | w = ft->wl->window; |
774 | |
|
775 | 0 | buffer = evbuffer_new(); |
776 | 0 | if (buffer == NULL) |
777 | 0 | fatalx("out of memory"); |
778 | | |
779 | 0 | TAILQ_FOREACH(wl, &w->winlinks, wentry) { |
780 | 0 | if (wl->session->curw == wl) { |
781 | 0 | if (EVBUFFER_LENGTH(buffer) > 0) |
782 | 0 | evbuffer_add(buffer, ",", 1); |
783 | 0 | evbuffer_add_printf(buffer, "%s", wl->session->name); |
784 | 0 | } |
785 | 0 | } |
786 | |
|
787 | 0 | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
788 | 0 | value = xmemdup(EVBUFFER_DATA(buffer), size); |
789 | 0 | evbuffer_free(buffer); |
790 | 0 | return (value); |
791 | 0 | } |
792 | | |
793 | | /* Callback for window_active_clients. */ |
794 | | static void * |
795 | | format_cb_window_active_clients(struct format_tree *ft) |
796 | 388 | { |
797 | 388 | struct window *w; |
798 | 388 | struct client *loop; |
799 | 388 | struct session *client_session; |
800 | 388 | u_int n = 0; |
801 | 388 | char *value; |
802 | | |
803 | 388 | if (ft->wl == NULL) |
804 | 388 | return (NULL); |
805 | 0 | w = ft->wl->window; |
806 | |
|
807 | 0 | TAILQ_FOREACH(loop, &clients, entry) { |
808 | 0 | client_session = loop->session; |
809 | 0 | if (client_session == NULL) |
810 | 0 | continue; |
811 | | |
812 | 0 | if (w == client_session->curw->window) |
813 | 0 | n++; |
814 | 0 | } |
815 | |
|
816 | 0 | xasprintf(&value, "%u", n); |
817 | 0 | return (value); |
818 | 388 | } |
819 | | |
820 | | /* Callback for window_active_clients_list. */ |
821 | | static void * |
822 | | format_cb_window_active_clients_list(struct format_tree *ft) |
823 | 392 | { |
824 | 392 | struct window *w; |
825 | 392 | struct client *loop; |
826 | 392 | struct session *client_session; |
827 | 392 | struct evbuffer *buffer; |
828 | 392 | int size; |
829 | 392 | char *value = NULL; |
830 | | |
831 | 392 | if (ft->wl == NULL) |
832 | 392 | return (NULL); |
833 | 0 | w = ft->wl->window; |
834 | |
|
835 | 0 | buffer = evbuffer_new(); |
836 | 0 | if (buffer == NULL) |
837 | 0 | fatalx("out of memory"); |
838 | | |
839 | 0 | TAILQ_FOREACH(loop, &clients, entry) { |
840 | 0 | client_session = loop->session; |
841 | 0 | if (client_session == NULL) |
842 | 0 | continue; |
843 | | |
844 | 0 | if (w == client_session->curw->window) { |
845 | 0 | if (EVBUFFER_LENGTH(buffer) > 0) |
846 | 0 | evbuffer_add(buffer, ",", 1); |
847 | 0 | evbuffer_add_printf(buffer, "%s", loop->name); |
848 | 0 | } |
849 | 0 | } |
850 | |
|
851 | 0 | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
852 | 0 | value = xmemdup(EVBUFFER_DATA(buffer), size); |
853 | 0 | evbuffer_free(buffer); |
854 | 0 | return (value); |
855 | 0 | } |
856 | | |
857 | | /* Callback for window_layout. */ |
858 | | static void * |
859 | | format_cb_window_layout(struct format_tree *ft) |
860 | 388 | { |
861 | 388 | struct client *c = ft->client; |
862 | 388 | struct window *w = ft->w; |
863 | 388 | struct layout_cell *lcroot; |
864 | 388 | int flags = 0; |
865 | | |
866 | 388 | if (w == NULL) |
867 | 388 | return (NULL); |
868 | | |
869 | 0 | if (w->saved_layout_root != NULL) |
870 | 0 | lcroot = w->saved_layout_root; |
871 | 0 | else |
872 | 0 | lcroot = w->layout_root; |
873 | |
|
874 | 0 | if (c != NULL && |
875 | 0 | (c->flags & CLIENT_CONTROL) && |
876 | 0 | (~c->flags & CLIENT_CONTROL_NEWLAYOUTS)) |
877 | 0 | flags |= LAYOUT_CUSTOM_OLD_FORMAT; |
878 | 0 | return (layout_dump(w, lcroot, flags)); |
879 | 388 | } |
880 | | |
881 | | /* Callback for window_visible_layout. */ |
882 | | static void * |
883 | | format_cb_window_visible_layout(struct format_tree *ft) |
884 | 396 | { |
885 | 396 | struct client *c = ft->client; |
886 | 396 | struct window *w = ft->w; |
887 | 396 | int flags = 0; |
888 | | |
889 | 396 | if (w == NULL) |
890 | 396 | return (NULL); |
891 | | |
892 | 0 | if (c != NULL && |
893 | 0 | (c->flags & CLIENT_CONTROL) && |
894 | 0 | (~c->flags & CLIENT_CONTROL_NEWLAYOUTS)) |
895 | 0 | flags |= LAYOUT_CUSTOM_OLD_FORMAT; |
896 | 0 | return (layout_dump(w, w->layout_root, flags)); |
897 | 396 | } |
898 | | |
899 | | /* Callback for pane_start_command. */ |
900 | | static void * |
901 | | format_cb_start_command(struct format_tree *ft) |
902 | 392 | { |
903 | 392 | struct window_pane *wp = ft->wp; |
904 | | |
905 | 392 | if (wp == NULL) |
906 | 392 | return (NULL); |
907 | | |
908 | 0 | return (cmd_stringify_argv(wp->argc, wp->argv)); |
909 | 392 | } |
910 | | |
911 | | /* Callback for pane_start_command_list. */ |
912 | | static void * |
913 | | format_cb_start_command_list(struct format_tree *ft) |
914 | 419 | { |
915 | 419 | struct window_pane *wp = ft->wp; |
916 | 419 | char *buf = NULL, *s; |
917 | 419 | size_t len = 0; |
918 | 419 | int i; |
919 | | |
920 | 419 | if (wp == NULL) |
921 | 419 | return (NULL); |
922 | 0 | if (wp->argc == 0) |
923 | 0 | return (xstrdup("")); |
924 | | |
925 | 0 | for (i = 0; i < wp->argc; i++) { |
926 | 0 | s = format_quote_shell_single(wp->argv[i]); |
927 | |
|
928 | 0 | len += strlen(s) + 1; |
929 | 0 | buf = xrealloc(buf, len); |
930 | |
|
931 | 0 | if (i == 0) |
932 | 0 | *buf = '\0'; |
933 | 0 | else |
934 | 0 | strlcat(buf, " ", len); |
935 | 0 | strlcat(buf, s, len); |
936 | |
|
937 | 0 | free(s); |
938 | 0 | } |
939 | 0 | return (buf); |
940 | 0 | } |
941 | | |
942 | | /* Callback for pane_start_path. */ |
943 | | static void * |
944 | | format_cb_start_path(struct format_tree *ft) |
945 | 388 | { |
946 | 388 | struct window_pane *wp = ft->wp; |
947 | | |
948 | 388 | if (wp == NULL) |
949 | 388 | return (NULL); |
950 | | |
951 | 0 | if (wp->cwd == NULL) |
952 | 0 | return (xstrdup("")); |
953 | 0 | return (xstrdup(wp->cwd)); |
954 | 0 | } |
955 | | |
956 | | /* Callback for pane_current_command. */ |
957 | | static void * |
958 | | format_cb_current_command(struct format_tree *ft) |
959 | 579 | { |
960 | 579 | struct window_pane *wp = ft->wp; |
961 | 579 | char *cmd, *value; |
962 | | |
963 | 579 | if (wp == NULL || wp->shell == NULL) |
964 | 579 | return (NULL); |
965 | | |
966 | 0 | cmd = osdep_get_name(wp->fd, wp->tty); |
967 | 0 | if (cmd == NULL || *cmd == '\0') { |
968 | 0 | free(cmd); |
969 | 0 | cmd = cmd_stringify_argv(wp->argc, wp->argv); |
970 | 0 | if (cmd == NULL || *cmd == '\0') { |
971 | 0 | free(cmd); |
972 | 0 | cmd = xstrdup(wp->shell); |
973 | 0 | } |
974 | 0 | } |
975 | 0 | value = parse_window_name(cmd); |
976 | 0 | free(cmd); |
977 | 0 | return (value); |
978 | 579 | } |
979 | | |
980 | | /* Callback for pane_current_path. */ |
981 | | static void * |
982 | | format_cb_current_path(struct format_tree *ft) |
983 | 415 | { |
984 | 415 | struct window_pane *wp = ft->wp; |
985 | 415 | char *cwd; |
986 | | |
987 | 415 | if (wp == NULL) |
988 | 415 | return (NULL); |
989 | | |
990 | 0 | cwd = osdep_get_cwd(wp->fd); |
991 | 0 | if (cwd == NULL) |
992 | 0 | return (NULL); |
993 | 0 | return (xstrdup(cwd)); |
994 | 0 | } |
995 | | |
996 | | /* Callback for history_bytes. */ |
997 | | static void * |
998 | | format_cb_history_bytes(struct format_tree *ft) |
999 | 21.9k | { |
1000 | 21.9k | struct window_pane *wp = ft->wp; |
1001 | 21.9k | struct grid *gd; |
1002 | 21.9k | struct grid_line *gl; |
1003 | 21.9k | size_t size = 0; |
1004 | 21.9k | u_int i; |
1005 | 21.9k | char *value; |
1006 | | |
1007 | 21.9k | if (wp == NULL) |
1008 | 21.9k | return (NULL); |
1009 | 0 | gd = wp->base.grid; |
1010 | |
|
1011 | 0 | for (i = 0; i < gd->hsize + gd->sy; i++) { |
1012 | 0 | gl = grid_get_line(gd, i); |
1013 | 0 | size += gl->cellsize * sizeof *gl->celldata; |
1014 | 0 | size += gl->extdsize * sizeof *gl->extddata; |
1015 | 0 | } |
1016 | 0 | size += (gd->hsize + gd->sy) * sizeof *gl; |
1017 | |
|
1018 | 0 | xasprintf(&value, "%zu", size); |
1019 | 0 | return (value); |
1020 | 21.9k | } |
1021 | | |
1022 | | /* Callback for history_all_bytes. */ |
1023 | | static void * |
1024 | | format_cb_history_all_bytes(struct format_tree *ft) |
1025 | 388 | { |
1026 | 388 | struct window_pane *wp = ft->wp; |
1027 | 388 | struct grid *gd; |
1028 | 388 | struct grid_line *gl; |
1029 | 388 | u_int i, lines, cells = 0, extended_cells = 0; |
1030 | 388 | char *value; |
1031 | | |
1032 | 388 | if (wp == NULL) |
1033 | 388 | return (NULL); |
1034 | 0 | gd = wp->base.grid; |
1035 | |
|
1036 | 0 | lines = gd->hsize + gd->sy; |
1037 | 0 | for (i = 0; i < lines; i++) { |
1038 | 0 | gl = grid_get_line(gd, i); |
1039 | 0 | cells += gl->cellsize; |
1040 | 0 | extended_cells += gl->extdsize; |
1041 | 0 | } |
1042 | |
|
1043 | 0 | xasprintf(&value, "%u,%zu,%u,%zu,%u,%zu", lines, |
1044 | 0 | lines * sizeof *gl, cells, cells * sizeof *gl->celldata, |
1045 | 0 | extended_cells, extended_cells * sizeof *gl->extddata); |
1046 | 0 | return (value); |
1047 | 388 | } |
1048 | | |
1049 | | /* Callback for pane_tabs. */ |
1050 | | static void * |
1051 | | format_cb_pane_tabs(struct format_tree *ft) |
1052 | 388 | { |
1053 | 388 | struct window_pane *wp = ft->wp; |
1054 | 388 | struct evbuffer *buffer; |
1055 | 388 | u_int i; |
1056 | 388 | int size; |
1057 | 388 | char *value = NULL; |
1058 | | |
1059 | 388 | if (wp == NULL) |
1060 | 388 | return (NULL); |
1061 | | |
1062 | 0 | buffer = evbuffer_new(); |
1063 | 0 | if (buffer == NULL) |
1064 | 0 | fatalx("out of memory"); |
1065 | 0 | for (i = 0; i < wp->base.grid->sx; i++) { |
1066 | 0 | if (!bit_test(wp->base.tabs, i)) |
1067 | 0 | continue; |
1068 | | |
1069 | 0 | if (EVBUFFER_LENGTH(buffer) > 0) |
1070 | 0 | evbuffer_add(buffer, ",", 1); |
1071 | 0 | evbuffer_add_printf(buffer, "%u", i); |
1072 | 0 | } |
1073 | 0 | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
1074 | 0 | value = xmemdup(EVBUFFER_DATA(buffer), size); |
1075 | 0 | evbuffer_free(buffer); |
1076 | 0 | return (value); |
1077 | 0 | } |
1078 | | |
1079 | | /* Callback for pane_fg. */ |
1080 | | static void * |
1081 | | format_cb_pane_fg(struct format_tree *ft) |
1082 | 440 | { |
1083 | 440 | struct window_pane *wp = ft->wp; |
1084 | 440 | struct grid_cell gc; |
1085 | | |
1086 | 440 | if (wp == NULL) |
1087 | 440 | return (NULL); |
1088 | | |
1089 | 0 | tty_default_colours(&gc, wp, NULL); |
1090 | 0 | return (xstrdup(colour_tostring(gc.fg))); |
1091 | 440 | } |
1092 | | |
1093 | | /* Callback for pane_flags. */ |
1094 | | static void * |
1095 | | format_cb_pane_flags(struct format_tree *ft) |
1096 | 808 | { |
1097 | 808 | if (ft->wp != NULL) |
1098 | 0 | return (xstrdup(window_pane_printable_flags(ft->wp))); |
1099 | 808 | return (NULL); |
1100 | 808 | } |
1101 | | |
1102 | | /* Callback for pane_floating_flag. */ |
1103 | | static void * |
1104 | | format_cb_pane_floating_flag(struct format_tree *ft) |
1105 | 670 | { |
1106 | 670 | struct window_pane *wp = ft->wp; |
1107 | | |
1108 | 670 | if (wp != NULL) { |
1109 | 0 | if (window_pane_is_floating(wp)) |
1110 | 0 | return (xstrdup("1")); |
1111 | 0 | return (xstrdup("0")); |
1112 | 0 | } |
1113 | 670 | return (NULL); |
1114 | 670 | } |
1115 | | |
1116 | | /* Callback for pane_modal_flag. */ |
1117 | | static void * |
1118 | | format_cb_pane_modal_flag(struct format_tree *ft) |
1119 | 555 | { |
1120 | 555 | struct window_pane *wp = ft->wp; |
1121 | | |
1122 | 555 | if (wp != NULL) { |
1123 | 0 | if (wp == wp->window->modal) |
1124 | 0 | return (xstrdup("1")); |
1125 | 0 | return (xstrdup("0")); |
1126 | 0 | } |
1127 | 555 | return (NULL); |
1128 | 555 | } |
1129 | | |
1130 | | /* Callback for pane_bg. */ |
1131 | | static void * |
1132 | | format_cb_pane_bg(struct format_tree *ft) |
1133 | 389 | { |
1134 | 389 | struct window_pane *wp = ft->wp; |
1135 | 389 | struct grid_cell gc; |
1136 | | |
1137 | 389 | if (wp == NULL) |
1138 | 389 | return (NULL); |
1139 | | |
1140 | 0 | tty_default_colours(&gc, wp, NULL); |
1141 | 0 | return (xstrdup(colour_tostring(gc.bg))); |
1142 | 389 | } |
1143 | | |
1144 | | /* Callback for session_group_list. */ |
1145 | | static void * |
1146 | | format_cb_session_group_list(struct format_tree *ft) |
1147 | 392 | { |
1148 | 392 | struct session *s = ft->s; |
1149 | 392 | struct session_group *sg; |
1150 | 392 | struct session *loop; |
1151 | 392 | struct evbuffer *buffer; |
1152 | 392 | int size; |
1153 | 392 | char *value = NULL; |
1154 | | |
1155 | 392 | if (s == NULL) |
1156 | 392 | return (NULL); |
1157 | 0 | sg = session_group_contains(s); |
1158 | 0 | if (sg == NULL) |
1159 | 0 | return (NULL); |
1160 | | |
1161 | 0 | buffer = evbuffer_new(); |
1162 | 0 | if (buffer == NULL) |
1163 | 0 | fatalx("out of memory"); |
1164 | | |
1165 | 0 | TAILQ_FOREACH(loop, &sg->sessions, gentry) { |
1166 | 0 | if (EVBUFFER_LENGTH(buffer) > 0) |
1167 | 0 | evbuffer_add(buffer, ",", 1); |
1168 | 0 | evbuffer_add_printf(buffer, "%s", loop->name); |
1169 | 0 | } |
1170 | |
|
1171 | 0 | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
1172 | 0 | value = xmemdup(EVBUFFER_DATA(buffer), size); |
1173 | 0 | evbuffer_free(buffer); |
1174 | 0 | return (value); |
1175 | 0 | } |
1176 | | |
1177 | | /* Callback for session_group_attached_list. */ |
1178 | | static void * |
1179 | | format_cb_session_group_attached_list(struct format_tree *ft) |
1180 | 388 | { |
1181 | 388 | struct session *s = ft->s, *client_session, *session_loop; |
1182 | 388 | struct session_group *sg; |
1183 | 388 | struct client *loop; |
1184 | 388 | struct evbuffer *buffer; |
1185 | 388 | int size; |
1186 | 388 | char *value = NULL; |
1187 | | |
1188 | 388 | if (s == NULL) |
1189 | 388 | return (NULL); |
1190 | 0 | sg = session_group_contains(s); |
1191 | 0 | if (sg == NULL) |
1192 | 0 | return (NULL); |
1193 | | |
1194 | 0 | buffer = evbuffer_new(); |
1195 | 0 | if (buffer == NULL) |
1196 | 0 | fatalx("out of memory"); |
1197 | | |
1198 | 0 | TAILQ_FOREACH(loop, &clients, entry) { |
1199 | 0 | client_session = loop->session; |
1200 | 0 | if (client_session == NULL) |
1201 | 0 | continue; |
1202 | 0 | TAILQ_FOREACH(session_loop, &sg->sessions, gentry) { |
1203 | 0 | if (session_loop == client_session){ |
1204 | 0 | if (EVBUFFER_LENGTH(buffer) > 0) |
1205 | 0 | evbuffer_add(buffer, ",", 1); |
1206 | 0 | evbuffer_add_printf(buffer, "%s", loop->name); |
1207 | 0 | } |
1208 | 0 | } |
1209 | 0 | } |
1210 | |
|
1211 | 0 | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
1212 | 0 | value = xmemdup(EVBUFFER_DATA(buffer), size); |
1213 | 0 | evbuffer_free(buffer); |
1214 | 0 | return (value); |
1215 | 0 | } |
1216 | | |
1217 | | /* Callback for pane_in_mode. */ |
1218 | | static void * |
1219 | | format_cb_pane_in_mode(struct format_tree *ft) |
1220 | 388 | { |
1221 | 388 | struct window_pane *wp = ft->wp; |
1222 | 388 | u_int n = 0; |
1223 | 388 | struct window_mode_entry *wme; |
1224 | 388 | char *value; |
1225 | | |
1226 | 388 | if (wp == NULL) |
1227 | 388 | return (NULL); |
1228 | | |
1229 | 0 | TAILQ_FOREACH(wme, &wp->modes, entry) |
1230 | 0 | n++; |
1231 | 0 | xasprintf(&value, "%u", n); |
1232 | 0 | return (value); |
1233 | 388 | } |
1234 | | |
1235 | | /* Callback for pane_at_top. */ |
1236 | | static void * |
1237 | | format_cb_pane_at_top(struct format_tree *ft) |
1238 | 405 | { |
1239 | 405 | struct window_pane *wp = ft->wp; |
1240 | 405 | int status, flag; |
1241 | 405 | char *value; |
1242 | | |
1243 | 405 | if (wp == NULL) |
1244 | 405 | return (NULL); |
1245 | | |
1246 | 0 | status = window_pane_get_pane_status(wp); |
1247 | 0 | if (status == PANE_STATUS_TOP) |
1248 | 0 | flag = (wp->yoff == 1); |
1249 | 0 | else |
1250 | 0 | flag = (wp->yoff == 0); |
1251 | 0 | xasprintf(&value, "%d", flag); |
1252 | 0 | return (value); |
1253 | 405 | } |
1254 | | |
1255 | | /* Callback for pane_at_bottom. */ |
1256 | | static void * |
1257 | | format_cb_pane_at_bottom(struct format_tree *ft) |
1258 | 388 | { |
1259 | 388 | struct window_pane *wp = ft->wp; |
1260 | 388 | struct window *w; |
1261 | 388 | int status, flag; |
1262 | 388 | char *value; |
1263 | | |
1264 | 388 | if (wp == NULL) |
1265 | 388 | return (NULL); |
1266 | 0 | w = wp->window; |
1267 | |
|
1268 | 0 | status = window_pane_get_pane_status(wp); |
1269 | 0 | if (status == PANE_STATUS_BOTTOM) |
1270 | 0 | flag = (wp->yoff + (int)wp->sy == (int)w->sy - 1); |
1271 | 0 | else |
1272 | 0 | flag = (wp->yoff + (int)wp->sy == (int)w->sy); |
1273 | 0 | xasprintf(&value, "%d", flag); |
1274 | 0 | return (value); |
1275 | 388 | } |
1276 | | |
1277 | | /* Callback for cursor_character. */ |
1278 | | static void * |
1279 | | format_cb_cursor_character(struct format_tree *ft) |
1280 | 396 | { |
1281 | 396 | struct window_pane *wp = ft->wp; |
1282 | 396 | struct grid_cell gc; |
1283 | 396 | char *value = NULL; |
1284 | | |
1285 | 396 | if (wp == NULL) |
1286 | 396 | return (NULL); |
1287 | | |
1288 | 0 | grid_view_get_cell(wp->base.grid, wp->base.cx, wp->base.cy, &gc); |
1289 | 0 | if (~gc.flags & GRID_FLAG_PADDING) |
1290 | 0 | xasprintf(&value, "%.*s", (int)gc.data.size, gc.data.data); |
1291 | 0 | return (value); |
1292 | 396 | } |
1293 | | |
1294 | | /* Callback for cursor_colour. */ |
1295 | | static void * |
1296 | | format_cb_cursor_colour(struct format_tree *ft) |
1297 | 38.7k | { |
1298 | 38.7k | struct window_pane *wp = ft->wp; |
1299 | | |
1300 | 38.7k | if (wp == NULL || wp->screen == NULL) |
1301 | 38.7k | return (NULL); |
1302 | | |
1303 | 0 | if (wp->screen->ccolour != -1) |
1304 | 0 | return (xstrdup(colour_tostring(wp->screen->ccolour))); |
1305 | 0 | return (xstrdup(colour_tostring(wp->screen->default_ccolour))); |
1306 | 0 | } |
1307 | | |
1308 | | /* Callback for mouse_word. */ |
1309 | | static void * |
1310 | | format_cb_mouse_word(struct format_tree *ft) |
1311 | 388 | { |
1312 | 388 | struct window_pane *wp; |
1313 | 388 | struct grid *gd; |
1314 | 388 | u_int x, y; |
1315 | | |
1316 | 388 | if (!ft->m.valid) |
1317 | 388 | return (NULL); |
1318 | 0 | wp = cmd_mouse_pane(&ft->m, NULL, NULL); |
1319 | 0 | if (wp == NULL) |
1320 | 0 | return (NULL); |
1321 | 0 | if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0) |
1322 | 0 | return (NULL); |
1323 | | |
1324 | 0 | if (!TAILQ_EMPTY(&wp->modes)) { |
1325 | 0 | if (window_pane_mode(wp) != WINDOW_PANE_NO_MODE) |
1326 | 0 | return (window_copy_get_word(wp, x, y)); |
1327 | 0 | return (NULL); |
1328 | 0 | } |
1329 | 0 | gd = wp->base.grid; |
1330 | 0 | return (format_grid_word(gd, x, gd->hsize + y)); |
1331 | 0 | } |
1332 | | |
1333 | | /* Callback for mouse_hyperlink. */ |
1334 | | static void * |
1335 | | format_cb_mouse_hyperlink(struct format_tree *ft) |
1336 | 388 | { |
1337 | 388 | struct window_pane *wp; |
1338 | 388 | struct grid *gd; |
1339 | 388 | u_int x, y; |
1340 | | |
1341 | 388 | if (!ft->m.valid) |
1342 | 388 | return (NULL); |
1343 | 0 | wp = cmd_mouse_pane(&ft->m, NULL, NULL); |
1344 | 0 | if (wp == NULL) |
1345 | 0 | return (NULL); |
1346 | 0 | if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0) |
1347 | 0 | return (NULL); |
1348 | | |
1349 | 0 | if (!TAILQ_EMPTY(&wp->modes)) { |
1350 | 0 | if (window_pane_mode(wp) != WINDOW_PANE_NO_MODE) |
1351 | 0 | return (window_copy_get_hyperlink(wp, x, y)); |
1352 | 0 | return (NULL); |
1353 | 0 | } |
1354 | 0 | gd = wp->base.grid; |
1355 | 0 | return (format_grid_hyperlink(gd, x, gd->hsize + y, wp->screen)); |
1356 | 0 | } |
1357 | | |
1358 | | /* Callback for mouse_line. */ |
1359 | | static void * |
1360 | | format_cb_mouse_line(struct format_tree *ft) |
1361 | 18.6k | { |
1362 | 18.6k | struct window_pane *wp; |
1363 | 18.6k | struct grid *gd; |
1364 | 18.6k | u_int x, y; |
1365 | | |
1366 | 18.6k | if (!ft->m.valid) |
1367 | 18.6k | return (NULL); |
1368 | 0 | wp = cmd_mouse_pane(&ft->m, NULL, NULL); |
1369 | 0 | if (wp == NULL) |
1370 | 0 | return (NULL); |
1371 | 0 | if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0) |
1372 | 0 | return (NULL); |
1373 | | |
1374 | 0 | if (!TAILQ_EMPTY(&wp->modes)) { |
1375 | 0 | if (window_pane_mode(wp) != WINDOW_PANE_NO_MODE) |
1376 | 0 | return (window_copy_get_line(wp, y)); |
1377 | 0 | return (NULL); |
1378 | 0 | } |
1379 | 0 | gd = wp->base.grid; |
1380 | 0 | return (format_grid_line(gd, gd->hsize + y)); |
1381 | 0 | } |
1382 | | |
1383 | | /* Get mouse status position: 0 horizontal, 1 side, -1 unavailable. */ |
1384 | | static int |
1385 | | format_mouse_status_at(struct format_tree *ft, u_int *x, u_int *y) |
1386 | 975 | { |
1387 | 975 | struct mouse_event *m = &ft->m; |
1388 | | |
1389 | 975 | if (!m->valid) |
1390 | 975 | return (-1); |
1391 | 0 | if (ft->c == NULL || (~ft->c->tty.flags & TTY_STARTED)) |
1392 | 0 | return (-1); |
1393 | | |
1394 | 0 | *x = m->x; |
1395 | 0 | *y = m->y; |
1396 | 0 | if (m->statusat == 0 && m->y < m->statuslines) |
1397 | 0 | return (0); |
1398 | 0 | if (m->statusat > 0 && m->y >= (u_int)m->statusat) { |
1399 | 0 | *y -= m->statusat; |
1400 | 0 | return (0); |
1401 | 0 | } |
1402 | 0 | if (m->sideat == -1 || |
1403 | 0 | m->x < (u_int)m->sideat || |
1404 | 0 | m->x >= m->sideat + m->sidewidth) |
1405 | 0 | return (-1); |
1406 | 0 | *x -= m->sideat; |
1407 | 0 | if (m->statusat == 0) |
1408 | 0 | *y -= m->statuslines; |
1409 | 0 | return (1); |
1410 | 0 | } |
1411 | | |
1412 | | /* Callback for mouse_status_line. */ |
1413 | | static void * |
1414 | | format_cb_mouse_status_line(struct format_tree *ft) |
1415 | 388 | { |
1416 | 388 | u_int x, y; |
1417 | | |
1418 | 388 | if (format_mouse_status_at(ft, &x, &y) == -1) |
1419 | 388 | return (NULL); |
1420 | 0 | return (format_printf("%u", y)); |
1421 | 388 | } |
1422 | | |
1423 | | /* Callback for mouse_status_range. */ |
1424 | | static void * |
1425 | | format_cb_mouse_status_range(struct format_tree *ft) |
1426 | 587 | { |
1427 | 587 | struct style_range *sr; |
1428 | 587 | u_int x, y; |
1429 | 587 | int side; |
1430 | | |
1431 | 587 | side = format_mouse_status_at(ft, &x, &y); |
1432 | 587 | if (side == -1) |
1433 | 587 | return (NULL); |
1434 | 0 | if (side) |
1435 | 0 | sr = status_side_get_range(ft->c, x, y); |
1436 | 0 | else |
1437 | 0 | sr = status_get_range(ft->c, x, y); |
1438 | |
|
1439 | 0 | if (sr == NULL) |
1440 | 0 | return (NULL); |
1441 | 0 | switch (sr->type) { |
1442 | 0 | case STYLE_RANGE_NONE: |
1443 | 0 | return (NULL); |
1444 | 0 | case STYLE_RANGE_LEFT: |
1445 | 0 | return (xstrdup("left")); |
1446 | 0 | case STYLE_RANGE_RIGHT: |
1447 | 0 | return (xstrdup("right")); |
1448 | 0 | case STYLE_RANGE_PANE: |
1449 | 0 | return (xstrdup("pane")); |
1450 | 0 | case STYLE_RANGE_WINDOW: |
1451 | 0 | return (xstrdup("window")); |
1452 | 0 | case STYLE_RANGE_SESSION: |
1453 | 0 | return (xstrdup("session")); |
1454 | 0 | case STYLE_RANGE_USER: |
1455 | 0 | return (xstrdup(sr->string)); |
1456 | 0 | case STYLE_RANGE_CONTROL: |
1457 | 0 | return (xstrdup("control")); |
1458 | 0 | } |
1459 | 0 | return (NULL); |
1460 | 0 | } |
1461 | | |
1462 | | /* Callback for alternate_on. */ |
1463 | | static void * |
1464 | | format_cb_alternate_on(struct format_tree *ft) |
1465 | 392 | { |
1466 | 392 | if (ft->wp != NULL) { |
1467 | 0 | if (ft->wp->base.saved_grid != NULL) |
1468 | 0 | return (xstrdup("1")); |
1469 | 0 | return (xstrdup("0")); |
1470 | 0 | } |
1471 | 392 | return (NULL); |
1472 | 392 | } |
1473 | | |
1474 | | /* Callback for alternate_saved_x. */ |
1475 | | static void * |
1476 | | format_cb_alternate_saved_x(struct format_tree *ft) |
1477 | 388 | { |
1478 | 388 | if (ft->wp != NULL) |
1479 | 0 | return (format_printf("%u", ft->wp->base.saved_cx)); |
1480 | 388 | return (NULL); |
1481 | 388 | } |
1482 | | |
1483 | | /* Callback for alternate_saved_y. */ |
1484 | | static void * |
1485 | | format_cb_alternate_saved_y(struct format_tree *ft) |
1486 | 388 | { |
1487 | 388 | if (ft->wp != NULL) |
1488 | 0 | return (format_printf("%u", ft->wp->base.saved_cy)); |
1489 | 388 | return (NULL); |
1490 | 388 | } |
1491 | | |
1492 | | /* Callback for bracket_paste_flag. */ |
1493 | | static void * |
1494 | | format_cb_bracket_paste_flag(struct format_tree *ft) |
1495 | 392 | { |
1496 | 392 | if (ft->wp != NULL && ft->wp->screen != NULL) { |
1497 | 0 | if (ft->wp->screen->mode & MODE_BRACKETPASTE) |
1498 | 0 | return (xstrdup("1")); |
1499 | 0 | return (xstrdup("0")); |
1500 | 0 | } |
1501 | 392 | return (NULL); |
1502 | 392 | } |
1503 | | |
1504 | | /* Callback for buffer_name. */ |
1505 | | static void * |
1506 | | format_cb_buffer_name(struct format_tree *ft) |
1507 | 420 | { |
1508 | 420 | if (ft->pb != NULL) |
1509 | 0 | return (xstrdup(paste_buffer_name(ft->pb))); |
1510 | 420 | return (NULL); |
1511 | 420 | } |
1512 | | |
1513 | | /* Callback for buffer_sample. */ |
1514 | | static void * |
1515 | | format_cb_buffer_sample(struct format_tree *ft) |
1516 | 578 | { |
1517 | 578 | if (ft->pb != NULL) |
1518 | 0 | return (paste_make_sample(ft->pb)); |
1519 | 578 | return (NULL); |
1520 | 578 | } |
1521 | | |
1522 | | /* Callback for buffer_full. */ |
1523 | | static void * |
1524 | | format_cb_buffer_full(struct format_tree *ft) |
1525 | 388 | { |
1526 | 388 | size_t size; |
1527 | 388 | const char *s; |
1528 | | |
1529 | 388 | if (ft->pb != NULL) { |
1530 | 0 | s = paste_buffer_data(ft->pb, &size); |
1531 | 0 | if (s != NULL) |
1532 | 0 | return (xstrndup(s, size)); |
1533 | 0 | } |
1534 | 388 | return (NULL); |
1535 | 388 | } |
1536 | | |
1537 | | /* Callback for buffer_size. */ |
1538 | | static void * |
1539 | | format_cb_buffer_size(struct format_tree *ft) |
1540 | 408 | { |
1541 | 408 | size_t size; |
1542 | | |
1543 | 408 | if (ft->pb != NULL) { |
1544 | 0 | paste_buffer_data(ft->pb, &size); |
1545 | 0 | return (format_printf("%zu", size)); |
1546 | 0 | } |
1547 | 408 | return (NULL); |
1548 | 408 | } |
1549 | | |
1550 | | /* Callback for client_cell_height. */ |
1551 | | static void * |
1552 | | format_cb_client_cell_height(struct format_tree *ft) |
1553 | 480 | { |
1554 | 480 | if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) |
1555 | 0 | return (format_printf("%u", ft->c->tty.ypixel)); |
1556 | 480 | return (NULL); |
1557 | 480 | } |
1558 | | |
1559 | | /* Callback for client_cell_width. */ |
1560 | | static void * |
1561 | | format_cb_client_cell_width(struct format_tree *ft) |
1562 | 496 | { |
1563 | 496 | if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) |
1564 | 0 | return (format_printf("%u", ft->c->tty.xpixel)); |
1565 | 496 | return (NULL); |
1566 | 496 | } |
1567 | | |
1568 | | /* Callback for client_colours. */ |
1569 | | static void * |
1570 | | format_cb_client_colours(struct format_tree *ft) |
1571 | 512 | { |
1572 | 512 | struct tty_term *term; |
1573 | 512 | u_int colours; |
1574 | | |
1575 | 512 | if (ft->c == NULL || (~ft->c->tty.flags & TTY_STARTED)) |
1576 | 512 | return (NULL); |
1577 | 0 | term = ft->c->tty.term; |
1578 | |
|
1579 | 0 | if (term->flags & TERM_RGBCOLOURS) |
1580 | 0 | colours = 16777216; |
1581 | 0 | else if (term->flags & TERM_256COLOURS) |
1582 | 0 | colours = 256; |
1583 | 0 | else { |
1584 | 0 | colours = tty_term_number(term, TTYC_COLORS); |
1585 | 0 | if (colours < 8) |
1586 | 0 | colours = 2; |
1587 | 0 | else if (colours < 16) |
1588 | 0 | colours = 8; |
1589 | 0 | else |
1590 | 0 | colours = 16; |
1591 | 0 | } |
1592 | 0 | return (format_printf("%u", colours)); |
1593 | 512 | } |
1594 | | |
1595 | | /* Callback for client_control_mode. */ |
1596 | | static void * |
1597 | | format_cb_client_control_mode(struct format_tree *ft) |
1598 | 425 | { |
1599 | 425 | if (ft->c != NULL) { |
1600 | 0 | if (ft->c->flags & CLIENT_CONTROL) |
1601 | 0 | return (xstrdup("1")); |
1602 | 0 | return (xstrdup("0")); |
1603 | 0 | } |
1604 | 425 | return (NULL); |
1605 | 425 | } |
1606 | | |
1607 | | /* Callback for client_discarded. */ |
1608 | | static void * |
1609 | | format_cb_client_discarded(struct format_tree *ft) |
1610 | 388 | { |
1611 | 388 | if (ft->c != NULL) |
1612 | 0 | return (format_printf("%zu", ft->c->discarded)); |
1613 | 388 | return (NULL); |
1614 | 388 | } |
1615 | | |
1616 | | /* Callback for client_flags. */ |
1617 | | static void * |
1618 | | format_cb_client_flags(struct format_tree *ft) |
1619 | 392 | { |
1620 | 392 | if (ft->c != NULL) |
1621 | 0 | return (xstrdup(server_client_get_flags(ft->c))); |
1622 | 392 | return (NULL); |
1623 | 392 | } |
1624 | | |
1625 | | /* Callback for client_height. */ |
1626 | | static void * |
1627 | | format_cb_client_height(struct format_tree *ft) |
1628 | 388 | { |
1629 | 388 | if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) |
1630 | 0 | return (format_printf("%u", ft->c->tty.sy)); |
1631 | 388 | return (NULL); |
1632 | 388 | } |
1633 | | |
1634 | | /* Callback for client_key_table. */ |
1635 | | static void * |
1636 | | format_cb_client_key_table(struct format_tree *ft) |
1637 | 388 | { |
1638 | 388 | if (ft->c != NULL) |
1639 | 0 | return (xstrdup(ft->c->keytable->name)); |
1640 | 388 | return (NULL); |
1641 | 388 | } |
1642 | | |
1643 | | /* Callback for client_last_session. */ |
1644 | | static void * |
1645 | | format_cb_client_last_session(struct format_tree *ft) |
1646 | 391 | { |
1647 | 391 | if (ft->c != NULL && |
1648 | 0 | ft->c->last_session != NULL && |
1649 | 0 | session_alive(ft->c->last_session)) |
1650 | 0 | return (xstrdup(ft->c->last_session->name)); |
1651 | 391 | return (NULL); |
1652 | 391 | } |
1653 | | |
1654 | | /* Callback for client_name. */ |
1655 | | static void * |
1656 | | format_cb_client_name(struct format_tree *ft) |
1657 | 415 | { |
1658 | 415 | if (ft->c != NULL && ft->c->name != NULL) |
1659 | 0 | return (xstrdup(ft->c->name)); |
1660 | 415 | return (NULL); |
1661 | 415 | } |
1662 | | |
1663 | | /* Callback for client_pid. */ |
1664 | | static void * |
1665 | | format_cb_client_pid(struct format_tree *ft) |
1666 | 388 | { |
1667 | 388 | if (ft->c != NULL) |
1668 | 0 | return (format_printf("%ld", (long)ft->c->pid)); |
1669 | 388 | return (NULL); |
1670 | 388 | } |
1671 | | |
1672 | | /* Callback for client_prefix. */ |
1673 | | static void * |
1674 | | format_cb_client_prefix(struct format_tree *ft) |
1675 | 431 | { |
1676 | 431 | const char *name; |
1677 | | |
1678 | 431 | if (ft->c != NULL) { |
1679 | 0 | name = server_client_get_key_table(ft->c); |
1680 | 0 | if (strcmp(ft->c->keytable->name, name) == 0) |
1681 | 0 | return (xstrdup("0")); |
1682 | 0 | return (xstrdup("1")); |
1683 | 0 | } |
1684 | 431 | return (NULL); |
1685 | 431 | } |
1686 | | |
1687 | | /* Callback for client_readonly. */ |
1688 | | static void * |
1689 | | format_cb_client_readonly(struct format_tree *ft) |
1690 | 388 | { |
1691 | 388 | if (ft->c != NULL) { |
1692 | 0 | if (ft->c->flags & CLIENT_READONLY) |
1693 | 0 | return (xstrdup("1")); |
1694 | 0 | return (xstrdup("0")); |
1695 | 0 | } |
1696 | 388 | return (NULL); |
1697 | 388 | } |
1698 | | |
1699 | | /* Callback for client_session. */ |
1700 | | static void * |
1701 | | format_cb_client_session(struct format_tree *ft) |
1702 | 388 | { |
1703 | 388 | if (ft->c != NULL && ft->c->session != NULL) |
1704 | 0 | return (xstrdup(ft->c->session->name)); |
1705 | 388 | return (NULL); |
1706 | 388 | } |
1707 | | |
1708 | | /* Callback for client_termfeatures. */ |
1709 | | static void * |
1710 | | format_cb_client_termfeatures(struct format_tree *ft) |
1711 | 388 | { |
1712 | 388 | if (ft->c != NULL) |
1713 | 0 | return (xstrdup(tty_get_features(ft->c->term_features))); |
1714 | 388 | return (NULL); |
1715 | 388 | } |
1716 | | |
1717 | | /* Callback for client_termname. */ |
1718 | | static void * |
1719 | | format_cb_client_termname(struct format_tree *ft) |
1720 | 388 | { |
1721 | 388 | if (ft->c != NULL && ft->c->term_name != NULL) |
1722 | 0 | return (xstrdup(ft->c->term_name)); |
1723 | 388 | return (NULL); |
1724 | 388 | } |
1725 | | |
1726 | | /* Callback for client_termtype. */ |
1727 | | static void * |
1728 | | format_cb_client_termtype(struct format_tree *ft) |
1729 | 388 | { |
1730 | 388 | if (ft->c != NULL) { |
1731 | 0 | if (ft->c->term_type == NULL) |
1732 | 0 | return (xstrdup("")); |
1733 | 0 | return (xstrdup(ft->c->term_type)); |
1734 | 0 | } |
1735 | 388 | return (NULL); |
1736 | 388 | } |
1737 | | |
1738 | | /* Callback for client_tty. */ |
1739 | | static void * |
1740 | | format_cb_client_tty(struct format_tree *ft) |
1741 | 392 | { |
1742 | 392 | if (ft->c != NULL && ft->c->ttyname != NULL) |
1743 | 0 | return (xstrdup(ft->c->ttyname)); |
1744 | 392 | return (NULL); |
1745 | 392 | } |
1746 | | |
1747 | | /* Callback for client_uid. */ |
1748 | | static void * |
1749 | | format_cb_client_uid(struct format_tree *ft) |
1750 | 469 | { |
1751 | 469 | uid_t uid; |
1752 | | |
1753 | 469 | if (ft->c != NULL) { |
1754 | 0 | uid = proc_get_peer_uid(ft->c->peer); |
1755 | 0 | if (uid != (uid_t)-1) |
1756 | 0 | return (format_printf("%ld", (long)uid)); |
1757 | 0 | } |
1758 | 469 | return (NULL); |
1759 | 469 | } |
1760 | | |
1761 | | /* Callback for client_user. */ |
1762 | | static void * |
1763 | | format_cb_client_user(struct format_tree *ft) |
1764 | 392 | { |
1765 | 392 | uid_t uid; |
1766 | 392 | struct passwd *pw; |
1767 | | |
1768 | 392 | if (ft->c != NULL) { |
1769 | 0 | if (ft->c->user != NULL) |
1770 | 0 | return (xstrdup(ft->c->user)); |
1771 | 0 | uid = proc_get_peer_uid(ft->c->peer); |
1772 | 0 | if (uid != (uid_t)-1 && (pw = getpwuid(uid)) != NULL) { |
1773 | 0 | ft->c->user = xstrdup(pw->pw_name); |
1774 | 0 | return (xstrdup(ft->c->user)); |
1775 | 0 | } |
1776 | 0 | } |
1777 | 392 | return (NULL); |
1778 | 392 | } |
1779 | | |
1780 | | /* Callback for client_utf8. */ |
1781 | | static void * |
1782 | | format_cb_client_utf8(struct format_tree *ft) |
1783 | 388 | { |
1784 | 388 | if (ft->c != NULL) { |
1785 | 0 | if (ft->c->flags & CLIENT_UTF8) |
1786 | 0 | return (xstrdup("1")); |
1787 | 0 | return (xstrdup("0")); |
1788 | 0 | } |
1789 | 388 | return (NULL); |
1790 | 388 | } |
1791 | | |
1792 | | /* Callback for client_width. */ |
1793 | | static void * |
1794 | | format_cb_client_width(struct format_tree *ft) |
1795 | 452 | { |
1796 | 452 | if (ft->c != NULL) |
1797 | 0 | return (format_printf("%u", ft->c->tty.sx)); |
1798 | 452 | return (NULL); |
1799 | 452 | } |
1800 | | |
1801 | | /* Callback for client_written. */ |
1802 | | static void * |
1803 | | format_cb_client_written(struct format_tree *ft) |
1804 | 390 | { |
1805 | 390 | if (ft->c != NULL) |
1806 | 0 | return (format_printf("%zu", ft->c->written)); |
1807 | 390 | return (NULL); |
1808 | 390 | } |
1809 | | |
1810 | | /* Callback for client_theme. */ |
1811 | | static void * |
1812 | | format_cb_client_theme(struct format_tree *ft) |
1813 | 388 | { |
1814 | 388 | if (ft->c != NULL) { |
1815 | 0 | switch (ft->c->theme) { |
1816 | 0 | case THEME_DARK: |
1817 | 0 | return (xstrdup("dark")); |
1818 | 0 | case THEME_LIGHT: |
1819 | 0 | return (xstrdup("light")); |
1820 | 0 | case THEME_UNKNOWN: |
1821 | 0 | return (NULL); |
1822 | 0 | } |
1823 | 0 | } |
1824 | 388 | return (NULL); |
1825 | 388 | } |
1826 | | |
1827 | | /* Callback for config_files. */ |
1828 | | static void * |
1829 | | format_cb_config_files(__unused struct format_tree *ft) |
1830 | 388 | { |
1831 | 388 | char *s = NULL; |
1832 | 388 | size_t slen = 0; |
1833 | 388 | u_int i; |
1834 | 388 | size_t n; |
1835 | | |
1836 | 388 | for (i = 0; i < cfg_nfiles; i++) { |
1837 | 0 | n = strlen(cfg_files[i]) + 1; |
1838 | 0 | s = xrealloc(s, slen + n + 1); |
1839 | 0 | slen += xsnprintf(s + slen, n + 1, "%s,", cfg_files[i]); |
1840 | 0 | } |
1841 | 388 | if (s == NULL) |
1842 | 388 | return (xstrdup("")); |
1843 | 0 | s[slen - 1] = '\0'; |
1844 | 0 | return (s); |
1845 | 388 | } |
1846 | | |
1847 | | /* Callback for cursor_flag. */ |
1848 | | static void * |
1849 | | format_cb_cursor_flag(struct format_tree *ft) |
1850 | 394 | { |
1851 | 394 | if (ft->wp != NULL) { |
1852 | 0 | if (ft->wp->base.mode & MODE_CURSOR) |
1853 | 0 | return (xstrdup("1")); |
1854 | 0 | return (xstrdup("0")); |
1855 | 0 | } |
1856 | 394 | return (NULL); |
1857 | 394 | } |
1858 | | |
1859 | | /* Callback for cursor_shape. */ |
1860 | | static void * |
1861 | | format_cb_cursor_shape(struct format_tree *ft) |
1862 | 388 | { |
1863 | 388 | if (ft->wp != NULL && ft->wp->screen != NULL) { |
1864 | 0 | switch (ft->wp->screen->cstyle) { |
1865 | 0 | case SCREEN_CURSOR_BLOCK: |
1866 | 0 | return (xstrdup("block")); |
1867 | 0 | case SCREEN_CURSOR_UNDERLINE: |
1868 | 0 | return (xstrdup("underline")); |
1869 | 0 | case SCREEN_CURSOR_BAR: |
1870 | 0 | return (xstrdup("bar")); |
1871 | 0 | default: |
1872 | 0 | return (xstrdup("default")); |
1873 | 0 | } |
1874 | 0 | } |
1875 | 388 | return (NULL); |
1876 | 388 | } |
1877 | | |
1878 | | /* Callback for cursor_very_visible. */ |
1879 | | static void * |
1880 | | format_cb_cursor_very_visible(struct format_tree *ft) |
1881 | 392 | { |
1882 | 392 | if (ft->wp != NULL && ft->wp->screen != NULL) { |
1883 | 0 | if (ft->wp->screen->mode & MODE_CURSOR_VERY_VISIBLE) |
1884 | 0 | return (xstrdup("1")); |
1885 | 0 | return (xstrdup("0")); |
1886 | 0 | } |
1887 | 392 | return (NULL); |
1888 | 392 | } |
1889 | | |
1890 | | /* Callback for cursor_x. */ |
1891 | | static void * |
1892 | | format_cb_cursor_x(struct format_tree *ft) |
1893 | 517 | { |
1894 | 517 | if (ft->wp != NULL) |
1895 | 0 | return (format_printf("%u", ft->wp->base.cx)); |
1896 | 517 | return (NULL); |
1897 | 517 | } |
1898 | | |
1899 | | /* Callback for cursor_y. */ |
1900 | | static void * |
1901 | | format_cb_cursor_y(struct format_tree *ft) |
1902 | 392 | { |
1903 | 392 | if (ft->wp != NULL) |
1904 | 0 | return (format_printf("%u", ft->wp->base.cy)); |
1905 | 392 | return (NULL); |
1906 | 392 | } |
1907 | | |
1908 | | /* Callback for cursor_blinking. */ |
1909 | | static void * |
1910 | | format_cb_cursor_blinking(struct format_tree *ft) |
1911 | 388 | { |
1912 | 388 | if (ft->wp != NULL && ft->wp->screen != NULL) { |
1913 | 0 | if (ft->wp->screen->mode & MODE_CURSOR_BLINKING) |
1914 | 0 | return (xstrdup("1")); |
1915 | 0 | return (xstrdup("0")); |
1916 | 0 | } |
1917 | 388 | return (NULL); |
1918 | 388 | } |
1919 | | |
1920 | | /* Callback for history_added. */ |
1921 | | static void * |
1922 | | format_cb_history_added(struct format_tree *ft) |
1923 | 560 | { |
1924 | 560 | if (ft->wp != NULL) |
1925 | 0 | return (format_printf("%u", ft->wp->base.grid->scroll_added)); |
1926 | 560 | return (NULL); |
1927 | 560 | } |
1928 | | |
1929 | | /* Callback for history_collected. */ |
1930 | | static void * |
1931 | | format_cb_history_collected(struct format_tree *ft) |
1932 | 388 | { |
1933 | 388 | struct window_pane *wp = ft->wp; |
1934 | | |
1935 | 388 | if (wp != NULL) |
1936 | 0 | return (format_printf("%u", wp->base.grid->scroll_collected)); |
1937 | 388 | return (NULL); |
1938 | 388 | } |
1939 | | |
1940 | | /* Callback for history_generation. */ |
1941 | | static void * |
1942 | | format_cb_history_generation(struct format_tree *ft) |
1943 | 5.56k | { |
1944 | 5.56k | struct window_pane *wp = ft->wp; |
1945 | | |
1946 | 5.56k | if (wp != NULL) |
1947 | 0 | return (format_printf("%u", wp->base.grid->scroll_generation)); |
1948 | 5.56k | return (NULL); |
1949 | 5.56k | } |
1950 | | |
1951 | | /* Callback for history_limit. */ |
1952 | | static void * |
1953 | | format_cb_history_limit(struct format_tree *ft) |
1954 | 388 | { |
1955 | 388 | if (ft->wp != NULL) |
1956 | 0 | return (format_printf("%u", ft->wp->base.grid->hlimit)); |
1957 | 388 | return (NULL); |
1958 | 388 | } |
1959 | | |
1960 | | /* Callback for history_size. */ |
1961 | | static void * |
1962 | | format_cb_history_size(struct format_tree *ft) |
1963 | 415 | { |
1964 | 415 | if (ft->wp != NULL) |
1965 | 0 | return (format_printf("%u", ft->wp->base.grid->hsize)); |
1966 | 415 | return (NULL); |
1967 | 415 | } |
1968 | | |
1969 | | /* Callback for insert_flag. */ |
1970 | | static void * |
1971 | | format_cb_insert_flag(struct format_tree *ft) |
1972 | 392 | { |
1973 | 392 | if (ft->wp != NULL) { |
1974 | 0 | if (ft->wp->base.mode & MODE_INSERT) |
1975 | 0 | return (xstrdup("1")); |
1976 | 0 | return (xstrdup("0")); |
1977 | 0 | } |
1978 | 392 | return (NULL); |
1979 | 392 | } |
1980 | | |
1981 | | /* Callback for keypad_cursor_flag. */ |
1982 | | static void * |
1983 | | format_cb_keypad_cursor_flag(struct format_tree *ft) |
1984 | 404 | { |
1985 | 404 | if (ft->wp != NULL) { |
1986 | 0 | if (ft->wp->base.mode & MODE_KCURSOR) |
1987 | 0 | return (xstrdup("1")); |
1988 | 0 | return (xstrdup("0")); |
1989 | 0 | } |
1990 | 404 | return (NULL); |
1991 | 404 | } |
1992 | | |
1993 | | /* Callback for keypad_flag. */ |
1994 | | static void * |
1995 | | format_cb_keypad_flag(struct format_tree *ft) |
1996 | 388 | { |
1997 | 388 | if (ft->wp != NULL) { |
1998 | 0 | if (ft->wp->base.mode & MODE_KKEYPAD) |
1999 | 0 | return (xstrdup("1")); |
2000 | 0 | return (xstrdup("0")); |
2001 | 0 | } |
2002 | 388 | return (NULL); |
2003 | 388 | } |
2004 | | |
2005 | | /* Callback for mouse_all_flag. */ |
2006 | | static void * |
2007 | | format_cb_mouse_all_flag(struct format_tree *ft) |
2008 | 388 | { |
2009 | 388 | if (ft->wp != NULL) { |
2010 | 0 | if (ft->wp->base.mode & MODE_MOUSE_ALL) |
2011 | 0 | return (xstrdup("1")); |
2012 | 0 | return (xstrdup("0")); |
2013 | 0 | } |
2014 | 388 | return (NULL); |
2015 | 388 | } |
2016 | | |
2017 | | /* Callback for mouse_any_flag. */ |
2018 | | static void * |
2019 | | format_cb_mouse_any_flag(struct format_tree *ft) |
2020 | 388 | { |
2021 | 388 | if (ft->wp != NULL) { |
2022 | 0 | if (ft->wp->base.mode & ALL_MOUSE_MODES) |
2023 | 0 | return (xstrdup("1")); |
2024 | 0 | return (xstrdup("0")); |
2025 | 0 | } |
2026 | 388 | return (NULL); |
2027 | 388 | } |
2028 | | |
2029 | | /* Callback for mouse_button_flag. */ |
2030 | | static void * |
2031 | | format_cb_mouse_button_flag(struct format_tree *ft) |
2032 | 392 | { |
2033 | 392 | if (ft->wp != NULL) { |
2034 | 0 | if (ft->wp->base.mode & MODE_MOUSE_BUTTON) |
2035 | 0 | return (xstrdup("1")); |
2036 | 0 | return (xstrdup("0")); |
2037 | 0 | } |
2038 | 392 | return (NULL); |
2039 | 392 | } |
2040 | | |
2041 | | /* Callback for mouse_pane. */ |
2042 | | static void * |
2043 | | format_cb_mouse_pane(struct format_tree *ft) |
2044 | 520 | { |
2045 | 520 | struct window_pane *wp; |
2046 | | |
2047 | 520 | if (ft->m.valid) { |
2048 | 0 | wp = cmd_mouse_pane(&ft->m, NULL, NULL); |
2049 | 0 | if (wp != NULL) |
2050 | 0 | return (format_printf("%%%u", wp->id)); |
2051 | 0 | return (NULL); |
2052 | 0 | } |
2053 | 520 | return (NULL); |
2054 | 520 | } |
2055 | | |
2056 | | /* Callback for mouse_sgr_flag. */ |
2057 | | static void * |
2058 | | format_cb_mouse_sgr_flag(struct format_tree *ft) |
2059 | 388 | { |
2060 | 388 | if (ft->wp != NULL) { |
2061 | 0 | if (ft->wp->base.mode & MODE_MOUSE_SGR) |
2062 | 0 | return (xstrdup("1")); |
2063 | 0 | return (xstrdup("0")); |
2064 | 0 | } |
2065 | 388 | return (NULL); |
2066 | 388 | } |
2067 | | |
2068 | | /* Callback for mouse_standard_flag. */ |
2069 | | static void * |
2070 | | format_cb_mouse_standard_flag(struct format_tree *ft) |
2071 | 392 | { |
2072 | 392 | if (ft->wp != NULL) { |
2073 | 0 | if (ft->wp->base.mode & MODE_MOUSE_STANDARD) |
2074 | 0 | return (xstrdup("1")); |
2075 | 0 | return (xstrdup("0")); |
2076 | 0 | } |
2077 | 392 | return (NULL); |
2078 | 392 | } |
2079 | | |
2080 | | /* Callback for mouse_utf8_flag. */ |
2081 | | static void * |
2082 | | format_cb_mouse_utf8_flag(struct format_tree *ft) |
2083 | 388 | { |
2084 | 388 | if (ft->wp != NULL) { |
2085 | 0 | if (ft->wp->base.mode & MODE_MOUSE_UTF8) |
2086 | 0 | return (xstrdup("1")); |
2087 | 0 | return (xstrdup("0")); |
2088 | 0 | } |
2089 | 388 | return (NULL); |
2090 | 388 | } |
2091 | | |
2092 | | /* Callback for mouse_absolute_x. */ |
2093 | | static void * |
2094 | | format_cb_mouse_absolute_x(struct format_tree *ft) |
2095 | 0 | { |
2096 | 0 | if (!ft->m.valid) |
2097 | 0 | return (NULL); |
2098 | 0 | return (format_printf("%u", ft->m.x)); |
2099 | 0 | } |
2100 | | |
2101 | | /* Callback for mouse_absolute_y. */ |
2102 | | static void * |
2103 | | format_cb_mouse_absolute_y(struct format_tree *ft) |
2104 | 0 | { |
2105 | 0 | if (!ft->m.valid) |
2106 | 0 | return (NULL); |
2107 | 0 | return (format_printf("%u", ft->m.y)); |
2108 | 0 | } |
2109 | | |
2110 | | /* Callback for mouse_x. */ |
2111 | | static void * |
2112 | | format_cb_mouse_x(struct format_tree *ft) |
2113 | 392 | { |
2114 | 392 | struct window_pane *wp; |
2115 | 392 | u_int x, y; |
2116 | | |
2117 | 392 | if (!ft->m.valid) |
2118 | 392 | return (NULL); |
2119 | 0 | if (format_mouse_status_at(ft, &x, &y) != -1) |
2120 | 0 | return (format_printf("%u", x)); |
2121 | 0 | wp = cmd_mouse_pane(&ft->m, NULL, NULL); |
2122 | 0 | if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0) |
2123 | 0 | return (format_printf("%u", x)); |
2124 | 0 | return (NULL); |
2125 | 0 | } |
2126 | | |
2127 | | /* Callback for mouse_y. */ |
2128 | | static void * |
2129 | | format_cb_mouse_y(struct format_tree *ft) |
2130 | 393 | { |
2131 | 393 | struct window_pane *wp; |
2132 | 393 | u_int x, y; |
2133 | | |
2134 | 393 | if (!ft->m.valid) |
2135 | 393 | return (NULL); |
2136 | 0 | if (format_mouse_status_at(ft, &x, &y) != -1) |
2137 | 0 | return (format_printf("%u", y)); |
2138 | 0 | wp = cmd_mouse_pane(&ft->m, NULL, NULL); |
2139 | 0 | if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0) |
2140 | 0 | return (format_printf("%u", y)); |
2141 | 0 | return (NULL); |
2142 | 0 | } |
2143 | | |
2144 | | /* Callback for next_session_id. */ |
2145 | | static void * |
2146 | | format_cb_next_session_id(__unused struct format_tree *ft) |
2147 | 580 | { |
2148 | 580 | return (format_printf("$%u", next_session_id)); |
2149 | 580 | } |
2150 | | |
2151 | | /* Callback for origin_flag. */ |
2152 | | static void * |
2153 | | format_cb_origin_flag(struct format_tree *ft) |
2154 | 388 | { |
2155 | 388 | if (ft->wp != NULL) { |
2156 | 0 | if (ft->wp->base.mode & MODE_ORIGIN) |
2157 | 0 | return (xstrdup("1")); |
2158 | 0 | return (xstrdup("0")); |
2159 | 0 | } |
2160 | 388 | return (NULL); |
2161 | 388 | } |
2162 | | |
2163 | | /* Callback for synchronized_output_flag. */ |
2164 | | static void * |
2165 | | format_cb_synchronized_output_flag(struct format_tree *ft) |
2166 | 511 | { |
2167 | 511 | if (ft->wp != NULL) { |
2168 | 0 | if (ft->wp->base.mode & MODE_SYNC) |
2169 | 0 | return (xstrdup("1")); |
2170 | 0 | return (xstrdup("0")); |
2171 | 0 | } |
2172 | 511 | return (NULL); |
2173 | 511 | } |
2174 | | |
2175 | | /* Callback for pane_private_modes. */ |
2176 | | static void * |
2177 | | format_cb_pane_private_modes(struct format_tree *ft) |
2178 | 419 | { |
2179 | 419 | static const struct { |
2180 | 419 | int mode; |
2181 | 419 | int number; |
2182 | 419 | } table[] = { |
2183 | 419 | { MODE_KCURSOR, 1 }, /* DECCKM */ |
2184 | 419 | { MODE_ORIGIN, 6 }, /* DECOM */ |
2185 | 419 | { MODE_WRAP, 7 }, /* DECAWM */ |
2186 | 419 | { MODE_CURSOR_BLINKING, 12 }, /* cursor blinking */ |
2187 | 419 | { MODE_CURSOR, 25 }, /* DECTCEM */ |
2188 | 419 | { MODE_MOUSE_STANDARD, 1000 }, /* mouse normal tracking */ |
2189 | 419 | { MODE_MOUSE_BUTTON, 1002 }, /* mouse button tracking */ |
2190 | 419 | { MODE_MOUSE_ALL, 1003 }, /* mouse any tracking */ |
2191 | 419 | { MODE_FOCUSON, 1004 }, /* focus reporting */ |
2192 | 419 | { MODE_MOUSE_UTF8, 1005 }, /* mouse: UTF-8 */ |
2193 | 419 | { MODE_MOUSE_SGR, 1006 }, /* mouse: SGR */ |
2194 | 419 | { MODE_BRACKETPASTE, 2004 }, /* bracketed paste */ |
2195 | 419 | { MODE_SYNC, 2026 }, /* synchronized output */ |
2196 | 419 | { MODE_THEME_UPDATES, 2031 }, /* theme update notifications */ |
2197 | 419 | }; |
2198 | 419 | int mode; |
2199 | 419 | char *value = NULL, *tmp; |
2200 | 419 | u_int i; |
2201 | | |
2202 | 419 | if (ft->wp == NULL) |
2203 | 419 | return (NULL); |
2204 | 0 | mode = ft->wp->base.mode; |
2205 | |
|
2206 | 0 | for (i = 0; i < nitems(table); i++) { |
2207 | 0 | if (~mode & table[i].mode) |
2208 | 0 | continue; |
2209 | | /* |
2210 | | * Only report cursor blinking when set by the application, not |
2211 | | * when it comes from the cursor-style option. |
2212 | | */ |
2213 | 0 | if (table[i].mode == MODE_CURSOR_BLINKING && |
2214 | 0 | (~mode & MODE_CURSOR_BLINKING_SET)) |
2215 | 0 | continue; |
2216 | 0 | if (value == NULL) |
2217 | 0 | xasprintf(&value, "%d", table[i].number); |
2218 | 0 | else { |
2219 | 0 | xasprintf(&tmp, "%s,%d", value, table[i].number); |
2220 | 0 | free(value); |
2221 | 0 | value = tmp; |
2222 | 0 | } |
2223 | 0 | } |
2224 | 0 | if (value == NULL) |
2225 | 0 | return (xstrdup("")); |
2226 | 0 | return (value); |
2227 | 0 | } |
2228 | | |
2229 | | /* Callback for pane_active. */ |
2230 | | static void * |
2231 | | format_cb_pane_active(struct format_tree *ft) |
2232 | 7.68k | { |
2233 | 7.68k | if (ft->wp != NULL) { |
2234 | 0 | if (ft->wp == ft->wp->window->active) |
2235 | 0 | return (xstrdup("1")); |
2236 | 0 | return (xstrdup("0")); |
2237 | 0 | } |
2238 | 7.68k | return (NULL); |
2239 | 7.68k | } |
2240 | | |
2241 | | /* Callback for pane_at_left. */ |
2242 | | static void * |
2243 | | format_cb_pane_at_left(struct format_tree *ft) |
2244 | 411 | { |
2245 | 411 | if (ft->wp != NULL) { |
2246 | 0 | if (ft->wp->xoff == 0) |
2247 | 0 | return (xstrdup("1")); |
2248 | 0 | return (xstrdup("0")); |
2249 | 0 | } |
2250 | 411 | return (NULL); |
2251 | 411 | } |
2252 | | |
2253 | | /* Callback for pane_at_right. */ |
2254 | | static void * |
2255 | | format_cb_pane_at_right(struct format_tree *ft) |
2256 | 388 | { |
2257 | 388 | if (ft->wp != NULL) { |
2258 | 0 | if (ft->wp->xoff + (int)ft->wp->sx == (int)ft->wp->window->sx) |
2259 | 0 | return (xstrdup("1")); |
2260 | 0 | return (xstrdup("0")); |
2261 | 0 | } |
2262 | 388 | return (NULL); |
2263 | 388 | } |
2264 | | |
2265 | | /* Callback for pane_bottom. */ |
2266 | | static void * |
2267 | | format_cb_pane_bottom(struct format_tree *ft) |
2268 | 388 | { |
2269 | 388 | struct window_pane *wp = ft->wp; |
2270 | | |
2271 | 388 | if (wp != NULL) |
2272 | 0 | return (format_printf("%d", wp->yoff + (int)wp->sy - 1)); |
2273 | 388 | return (NULL); |
2274 | 388 | } |
2275 | | |
2276 | | /* Callback for pane_dead. */ |
2277 | | static void * |
2278 | | format_cb_pane_dead(struct format_tree *ft) |
2279 | 390 | { |
2280 | 390 | struct window_pane *wp = ft->wp; |
2281 | | |
2282 | 390 | if (wp != NULL) { |
2283 | 0 | if (wp->fd == -1 && (wp->flags & PANE_STATUSREADY)) |
2284 | 0 | return (xstrdup("1")); |
2285 | 0 | return (xstrdup("0")); |
2286 | 0 | } |
2287 | 390 | return (NULL); |
2288 | 390 | } |
2289 | | |
2290 | | /* Callback for pane_dead_signal. */ |
2291 | | static void * |
2292 | | format_cb_pane_dead_signal(struct format_tree *ft) |
2293 | 388 | { |
2294 | 388 | struct window_pane *wp = ft->wp; |
2295 | 388 | const char *name; |
2296 | | |
2297 | 388 | if (wp != NULL) { |
2298 | 0 | if ((wp->flags & PANE_STATUSREADY) && WIFSIGNALED(wp->status)) { |
2299 | 0 | name = sig2name(WTERMSIG(wp->status)); |
2300 | 0 | return (format_printf("%s", name)); |
2301 | 0 | } |
2302 | 0 | return (NULL); |
2303 | 0 | } |
2304 | 388 | return (NULL); |
2305 | 388 | } |
2306 | | |
2307 | | /* Callback for pane_dead_status. */ |
2308 | | static void * |
2309 | | format_cb_pane_dead_status(struct format_tree *ft) |
2310 | 653 | { |
2311 | 653 | struct window_pane *wp = ft->wp; |
2312 | | |
2313 | 653 | if (wp != NULL) { |
2314 | 0 | if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(wp->status)) |
2315 | 0 | return (format_printf("%d", WEXITSTATUS(wp->status))); |
2316 | 0 | return (NULL); |
2317 | 0 | } |
2318 | 653 | return (NULL); |
2319 | 653 | } |
2320 | | |
2321 | | /* Callback for pane_dead_time. */ |
2322 | | static void * |
2323 | | format_cb_pane_dead_time(struct format_tree *ft) |
2324 | 388 | { |
2325 | 388 | struct window_pane *wp = ft->wp; |
2326 | | |
2327 | 388 | if (wp != NULL) { |
2328 | 0 | if (wp->flags & PANE_STATUSDRAWN) |
2329 | 0 | return (&wp->dead_time); |
2330 | 0 | return (NULL); |
2331 | 0 | } |
2332 | 388 | return (NULL); |
2333 | 388 | } |
2334 | | |
2335 | | /* Callback for pane_last_output_time. */ |
2336 | | static void * |
2337 | | format_cb_pane_last_output_time(struct format_tree *ft) |
2338 | 524 | { |
2339 | 524 | struct window_pane *wp = ft->wp; |
2340 | 524 | static struct timeval tv; |
2341 | | |
2342 | 524 | if (wp != NULL && wp->last_output_time != 0) { |
2343 | 0 | tv.tv_sec = wp->last_output_time; |
2344 | 0 | tv.tv_usec = 0; |
2345 | 0 | return (&tv); |
2346 | 0 | } |
2347 | 524 | return (NULL); |
2348 | 524 | } |
2349 | | |
2350 | | /* Callback for pane_output_generation. */ |
2351 | | static void * |
2352 | | format_cb_pane_output_generation(struct format_tree *ft) |
2353 | 496 | { |
2354 | 496 | unsigned long long value; |
2355 | | |
2356 | 496 | if (ft->wp != NULL) { |
2357 | 0 | value = ft->wp->output_generation; |
2358 | 0 | return (format_printf("%llu", value)); |
2359 | 0 | } |
2360 | 496 | return (NULL); |
2361 | 496 | } |
2362 | | |
2363 | | /* Callback for pane_last_prompt_time. */ |
2364 | | static void * |
2365 | | format_cb_pane_last_prompt_time(struct format_tree *ft) |
2366 | 492 | { |
2367 | 492 | struct window_pane *wp = ft->wp; |
2368 | 492 | static struct timeval tv; |
2369 | | |
2370 | 492 | if (wp != NULL && wp->last_prompt_time != 0) { |
2371 | 0 | tv.tv_sec = wp->last_prompt_time; |
2372 | 0 | tv.tv_usec = 0; |
2373 | 0 | return (&tv); |
2374 | 0 | } |
2375 | 492 | return (NULL); |
2376 | 492 | } |
2377 | | |
2378 | | /* Callback for pane_command_line. */ |
2379 | | static void * |
2380 | | format_cb_pane_command_line(struct format_tree *ft) |
2381 | 0 | { |
2382 | 0 | struct window_pane *wp = ft->wp; |
2383 | |
|
2384 | 0 | if (wp != NULL && wp->cmd_line != NULL) |
2385 | 0 | return (xstrdup(wp->cmd_line)); |
2386 | 0 | return (NULL); |
2387 | 0 | } |
2388 | | |
2389 | | /* Callback for pane_command_start_time. */ |
2390 | | static void * |
2391 | | format_cb_pane_command_start_time(struct format_tree *ft) |
2392 | 408 | { |
2393 | 408 | struct window_pane *wp = ft->wp; |
2394 | 408 | static struct timeval tv; |
2395 | | |
2396 | 408 | if (wp != NULL && wp->cmd_start_time != 0) { |
2397 | 0 | tv.tv_sec = wp->cmd_start_time; |
2398 | 0 | tv.tv_usec = 0; |
2399 | 0 | return (&tv); |
2400 | 0 | } |
2401 | 408 | return (NULL); |
2402 | 408 | } |
2403 | | |
2404 | | /* Callback for pane_command_end_time. */ |
2405 | | static void * |
2406 | | format_cb_pane_command_end_time(struct format_tree *ft) |
2407 | 392 | { |
2408 | 392 | struct window_pane *wp = ft->wp; |
2409 | 392 | static struct timeval tv; |
2410 | | |
2411 | 392 | if (wp != NULL && wp->cmd_end_time != 0) { |
2412 | 0 | tv.tv_sec = wp->cmd_end_time; |
2413 | 0 | tv.tv_usec = 0; |
2414 | 0 | return (&tv); |
2415 | 0 | } |
2416 | 392 | return (NULL); |
2417 | 392 | } |
2418 | | |
2419 | | /* Callback for pane_command_running. */ |
2420 | | static void * |
2421 | | format_cb_pane_command_running(struct format_tree *ft) |
2422 | 555 | { |
2423 | 555 | struct window_pane *wp = ft->wp; |
2424 | | |
2425 | 555 | if (wp != NULL) |
2426 | 0 | return (format_printf("%d", !!(wp->flags & PANE_CMDRUNNING))); |
2427 | 555 | return (NULL); |
2428 | 555 | } |
2429 | | |
2430 | | /* Callback for pane_command_duration. */ |
2431 | | static void * |
2432 | | format_cb_pane_command_duration(struct format_tree *ft) |
2433 | 582 | { |
2434 | 582 | struct window_pane *wp = ft->wp; |
2435 | 582 | time_t end; |
2436 | | |
2437 | 582 | if (wp == NULL || wp->cmd_start_time == 0) |
2438 | 582 | return (NULL); |
2439 | 0 | if (wp->flags & PANE_CMDRUNNING) |
2440 | 0 | end = time(NULL); |
2441 | 0 | else |
2442 | 0 | end = wp->cmd_end_time; |
2443 | 0 | if (end < wp->cmd_start_time) |
2444 | 0 | end = wp->cmd_start_time; |
2445 | 0 | return (format_printf("%lld", (long long)(end - wp->cmd_start_time))); |
2446 | 582 | } |
2447 | | |
2448 | | /* Callback for pane_command_status. */ |
2449 | | static void * |
2450 | | format_cb_pane_command_status(struct format_tree *ft) |
2451 | 395 | { |
2452 | 395 | struct window_pane *wp = ft->wp; |
2453 | | |
2454 | 395 | if (wp != NULL && wp->cmd_status != -1) |
2455 | 0 | return (format_printf("%d", wp->cmd_status)); |
2456 | 395 | return (NULL); |
2457 | 395 | } |
2458 | | |
2459 | | /* Callback for pane_format. */ |
2460 | | static void * |
2461 | | format_cb_pane_format(struct format_tree *ft) |
2462 | 457 | { |
2463 | 457 | if (ft->type == FORMAT_TYPE_PANE) |
2464 | 0 | return (xstrdup("1")); |
2465 | 457 | return (xstrdup("0")); |
2466 | 457 | } |
2467 | | |
2468 | | /* Callback for pane_height. */ |
2469 | | static void * |
2470 | | format_cb_pane_height(struct format_tree *ft) |
2471 | 469 | { |
2472 | 469 | if (ft->wp != NULL) |
2473 | 0 | return (format_printf("%u", ft->wp->sy)); |
2474 | 469 | return (NULL); |
2475 | 469 | } |
2476 | | |
2477 | | /* Callback for pane_id. */ |
2478 | | static void * |
2479 | | format_cb_pane_id(struct format_tree *ft) |
2480 | 1.43k | { |
2481 | 1.43k | if (ft->wp != NULL) |
2482 | 0 | return (format_printf("%%%u", ft->wp->id)); |
2483 | 1.43k | return (NULL); |
2484 | 1.43k | } |
2485 | | |
2486 | | /* Callback for pane_index. */ |
2487 | | static void * |
2488 | | format_cb_pane_index(struct format_tree *ft) |
2489 | 16.8k | { |
2490 | 16.8k | u_int idx; |
2491 | | |
2492 | 16.8k | if (ft->wp != NULL && window_pane_index(ft->wp, &idx) == 0) |
2493 | 0 | return (format_printf("%u", idx)); |
2494 | 16.8k | return (NULL); |
2495 | 16.8k | } |
2496 | | |
2497 | | /* Callback for pane_input_off. */ |
2498 | | static void * |
2499 | | format_cb_pane_input_off(struct format_tree *ft) |
2500 | 388 | { |
2501 | 388 | if (ft->wp != NULL) { |
2502 | 0 | if (ft->wp->flags & PANE_INPUTOFF) |
2503 | 0 | return (xstrdup("1")); |
2504 | 0 | return (xstrdup("0")); |
2505 | 0 | } |
2506 | 388 | return (NULL); |
2507 | 388 | } |
2508 | | |
2509 | | /* Callback for pane_unseen_changes. */ |
2510 | | static void * |
2511 | | format_cb_pane_unseen_changes(struct format_tree *ft) |
2512 | 388 | { |
2513 | 388 | if (ft->wp != NULL) { |
2514 | 0 | if (ft->wp->flags & PANE_UNSEENCHANGES) |
2515 | 0 | return (xstrdup("1")); |
2516 | 0 | return (xstrdup("0")); |
2517 | 0 | } |
2518 | 388 | return (NULL); |
2519 | 388 | } |
2520 | | |
2521 | | /* Callback for pane_key_mode. */ |
2522 | | static void * |
2523 | | format_cb_pane_key_mode(struct format_tree *ft) |
2524 | 388 | { |
2525 | 388 | if (ft->wp != NULL && ft->wp->screen != NULL) { |
2526 | 0 | switch (ft->wp->screen->mode & EXTENDED_KEY_MODES) { |
2527 | 0 | case MODE_KEYS_EXTENDED: |
2528 | 0 | return (xstrdup("Ext 1")); |
2529 | 0 | case MODE_KEYS_EXTENDED_2: |
2530 | 0 | return (xstrdup("Ext 2")); |
2531 | 0 | default: |
2532 | 0 | return (xstrdup("VT10x")); |
2533 | 0 | } |
2534 | 0 | } |
2535 | 388 | return (NULL); |
2536 | 388 | } |
2537 | | |
2538 | | /* Callback for pane_last. */ |
2539 | | static void * |
2540 | | format_cb_pane_last(struct format_tree *ft) |
2541 | 415 | { |
2542 | 415 | if (ft->wp != NULL) { |
2543 | 0 | if (ft->wp == TAILQ_FIRST(&ft->wp->window->last_panes)) |
2544 | 0 | return (xstrdup("1")); |
2545 | 0 | return (xstrdup("0")); |
2546 | 0 | } |
2547 | 415 | return (NULL); |
2548 | 415 | } |
2549 | | |
2550 | | /* Callback for pane_left. */ |
2551 | | static void * |
2552 | | format_cb_pane_left(struct format_tree *ft) |
2553 | 427 | { |
2554 | 427 | if (ft->wp != NULL) |
2555 | 0 | return (format_printf("%d", ft->wp->xoff)); |
2556 | 427 | return (NULL); |
2557 | 427 | } |
2558 | | |
2559 | | /* Callback for pane_marked. */ |
2560 | | static void * |
2561 | | format_cb_pane_marked(struct format_tree *ft) |
2562 | 588 | { |
2563 | 588 | if (ft->wp != NULL) { |
2564 | 0 | if (server_check_marked() && marked_pane.wp == ft->wp) |
2565 | 0 | return (xstrdup("1")); |
2566 | 0 | return (xstrdup("0")); |
2567 | 0 | } |
2568 | 588 | return (NULL); |
2569 | 588 | } |
2570 | | |
2571 | | /* Callback for pane_marked_set. */ |
2572 | | static void * |
2573 | | format_cb_pane_marked_set(struct format_tree *ft) |
2574 | 415 | { |
2575 | 415 | if (ft->wp != NULL) { |
2576 | 0 | if (server_check_marked()) |
2577 | 0 | return (xstrdup("1")); |
2578 | 0 | return (xstrdup("0")); |
2579 | 0 | } |
2580 | 415 | return (NULL); |
2581 | 415 | } |
2582 | | |
2583 | | /* Callback for pane_mode. */ |
2584 | | static void * |
2585 | | format_cb_pane_mode(struct format_tree *ft) |
2586 | 408 | { |
2587 | 408 | struct window_mode_entry *wme; |
2588 | | |
2589 | 408 | if (ft->wp != NULL) { |
2590 | 0 | wme = TAILQ_FIRST(&ft->wp->modes); |
2591 | 0 | if (wme != NULL) |
2592 | 0 | return (xstrdup(wme->mode->name)); |
2593 | 0 | return (NULL); |
2594 | 0 | } |
2595 | 408 | return (NULL); |
2596 | 408 | } |
2597 | | |
2598 | | /* Callback for pane_path. */ |
2599 | | static void * |
2600 | | format_cb_pane_path(struct format_tree *ft) |
2601 | 392 | { |
2602 | 392 | if (ft->wp != NULL) { |
2603 | 0 | if (ft->wp->base.path == NULL) |
2604 | 0 | return (xstrdup("")); |
2605 | 0 | return (xstrdup(ft->wp->base.path)); |
2606 | 0 | } |
2607 | 392 | return (NULL); |
2608 | 392 | } |
2609 | | |
2610 | | /* Callback for pane_pid. */ |
2611 | | static void * |
2612 | | format_cb_pane_pid(struct format_tree *ft) |
2613 | 419 | { |
2614 | 419 | if (ft->wp != NULL && ft->wp->fd != -1) |
2615 | 0 | return (format_printf("%ld", (long)ft->wp->pid)); |
2616 | 419 | return (NULL); |
2617 | 419 | } |
2618 | | |
2619 | | /* Callback for pane_pipe. */ |
2620 | | static void * |
2621 | | format_cb_pane_pipe(struct format_tree *ft) |
2622 | 388 | { |
2623 | 388 | if (ft->wp != NULL) { |
2624 | 0 | if (ft->wp->pipe_fd != -1) |
2625 | 0 | return (xstrdup("1")); |
2626 | 0 | return (xstrdup("0")); |
2627 | 0 | } |
2628 | 388 | return (NULL); |
2629 | 388 | } |
2630 | | |
2631 | | /* Callback for pane_pipe_pid. */ |
2632 | | static void * |
2633 | | format_cb_pane_pipe_pid(struct format_tree *ft) |
2634 | 388 | { |
2635 | 388 | char *value = NULL; |
2636 | | |
2637 | 388 | if (ft->wp != NULL && ft->wp->pipe_fd != -1) |
2638 | 0 | xasprintf(&value, "%ld", (long)ft->wp->pipe_pid); |
2639 | 388 | return (value); |
2640 | 388 | } |
2641 | | |
2642 | | /* Callback for pane_pb_progress. */ |
2643 | | static void * |
2644 | | format_cb_pane_pb_progress(struct format_tree *ft) |
2645 | 14.8k | { |
2646 | 14.8k | char *value = NULL; |
2647 | | |
2648 | 14.8k | if (ft->wp != NULL) |
2649 | 0 | xasprintf(&value, "%d", ft->wp->base.progress_bar.progress); |
2650 | 14.8k | return (value); |
2651 | 14.8k | } |
2652 | | |
2653 | | /* Callback for pane_pb_state. */ |
2654 | | static void * |
2655 | | format_cb_pane_pb_state(struct format_tree *ft) |
2656 | 388 | { |
2657 | 388 | if (ft->wp != NULL) { |
2658 | 0 | switch (ft->wp->base.progress_bar.state) { |
2659 | 0 | case PROGRESS_BAR_HIDDEN: |
2660 | 0 | return xstrdup("hidden"); |
2661 | 0 | case PROGRESS_BAR_NORMAL: |
2662 | 0 | return xstrdup("normal"); |
2663 | 0 | case PROGRESS_BAR_ERROR: |
2664 | 0 | return xstrdup("error"); |
2665 | 0 | case PROGRESS_BAR_INDETERMINATE: |
2666 | 0 | return xstrdup("indeterminate"); |
2667 | 0 | case PROGRESS_BAR_PAUSED: |
2668 | 0 | return xstrdup("paused"); |
2669 | 0 | } |
2670 | 0 | } |
2671 | 388 | return (NULL); |
2672 | 388 | } |
2673 | | |
2674 | | /* Callback for pane_right. */ |
2675 | | static void * |
2676 | | format_cb_pane_right(struct format_tree *ft) |
2677 | 558 | { |
2678 | 558 | struct window_pane *wp = ft->wp; |
2679 | | |
2680 | 558 | if (wp != NULL) |
2681 | 0 | return (format_printf("%d", wp->xoff + (int)wp->sx - 1)); |
2682 | 558 | return (NULL); |
2683 | 558 | } |
2684 | | |
2685 | | /* Callback for pane_search_string. */ |
2686 | | static void * |
2687 | | format_cb_pane_search_string(struct format_tree *ft) |
2688 | 390 | { |
2689 | 390 | if (ft->wp != NULL) { |
2690 | 0 | if (ft->wp->searchstr == NULL) |
2691 | 0 | return (xstrdup("")); |
2692 | 0 | return (xstrdup(ft->wp->searchstr)); |
2693 | 0 | } |
2694 | 390 | return (NULL); |
2695 | 390 | } |
2696 | | |
2697 | | /* Callback for pane_synchronized. */ |
2698 | | static void * |
2699 | | format_cb_pane_synchronized(struct format_tree *ft) |
2700 | 388 | { |
2701 | 388 | if (ft->wp != NULL) { |
2702 | 0 | if (options_get_number(ft->wp->options, "synchronize-panes")) |
2703 | 0 | return (xstrdup("1")); |
2704 | 0 | return (xstrdup("0")); |
2705 | 0 | } |
2706 | 388 | return (NULL); |
2707 | 388 | } |
2708 | | |
2709 | | /* Callback for pane_title. */ |
2710 | | static void * |
2711 | | format_cb_pane_title(struct format_tree *ft) |
2712 | 2.32k | { |
2713 | 2.32k | if (ft->wp != NULL) |
2714 | 0 | return (xstrdup(ft->wp->base.title)); |
2715 | 2.32k | return (NULL); |
2716 | 2.32k | } |
2717 | | |
2718 | | /* Callback for pane_top. */ |
2719 | | static void * |
2720 | | format_cb_pane_top(struct format_tree *ft) |
2721 | 388 | { |
2722 | 388 | if (ft->wp != NULL) |
2723 | 0 | return (format_printf("%d", ft->wp->yoff)); |
2724 | 388 | return (NULL); |
2725 | 388 | } |
2726 | | |
2727 | | /* Callback for pane_tty. */ |
2728 | | static void * |
2729 | | format_cb_pane_tty(struct format_tree *ft) |
2730 | 414 | { |
2731 | 414 | if (ft->wp != NULL) |
2732 | 0 | return (xstrdup(ft->wp->tty)); |
2733 | 414 | return (NULL); |
2734 | 414 | } |
2735 | | |
2736 | | /* Callback for pane_unzoomed_height. */ |
2737 | | static void * |
2738 | | format_cb_pane_unzoomed_height(struct format_tree *ft) |
2739 | 475 | { |
2740 | 475 | struct window_pane *wp = ft->wp; |
2741 | 475 | struct window *w; |
2742 | 475 | struct layout_cell *lc, *root; |
2743 | 475 | int status, floating; |
2744 | 475 | u_int sy; |
2745 | | |
2746 | 475 | if (wp == NULL) |
2747 | 475 | return (NULL); |
2748 | 0 | w = wp->window; |
2749 | |
|
2750 | 0 | lc = wp->saved_layout_cell; |
2751 | 0 | if (lc == NULL) |
2752 | 0 | lc = wp->layout_cell; |
2753 | 0 | if (lc == NULL) |
2754 | 0 | return (NULL); |
2755 | 0 | sy = lc->g.sy; |
2756 | 0 | floating = (lc->flags & LAYOUT_CELL_FLOATING); |
2757 | |
|
2758 | 0 | root = w->saved_layout_root; |
2759 | 0 | if (root == NULL) |
2760 | 0 | root = w->layout_root; |
2761 | 0 | if (lc == wp->saved_layout_cell && !floating) |
2762 | 0 | status = window_get_pane_status(w); |
2763 | 0 | else |
2764 | 0 | status = window_pane_get_pane_status(wp); |
2765 | 0 | if (!floating && |
2766 | 0 | root != NULL && |
2767 | 0 | layout_add_horizontal_border(root, lc, status) && |
2768 | 0 | sy > 1) |
2769 | 0 | sy--; |
2770 | |
|
2771 | 0 | return (format_printf("%u", sy)); |
2772 | 0 | } |
2773 | | |
2774 | | /* Callback for pane_unzoomed_width. */ |
2775 | | static void * |
2776 | | format_cb_pane_unzoomed_width(struct format_tree *ft) |
2777 | 605 | { |
2778 | 605 | struct window_pane *wp = ft->wp; |
2779 | 605 | struct layout_cell *lc; |
2780 | 605 | int saved, sb_w, sb_pad; |
2781 | 605 | u_int sx; |
2782 | | |
2783 | 605 | if (wp == NULL) |
2784 | 605 | return (NULL); |
2785 | | |
2786 | 0 | lc = wp->saved_layout_cell; |
2787 | 0 | saved = (lc != NULL); |
2788 | 0 | if (lc == NULL) |
2789 | 0 | lc = wp->layout_cell; |
2790 | 0 | if (lc == NULL) |
2791 | 0 | return (NULL); |
2792 | 0 | sx = lc->g.sx; |
2793 | |
|
2794 | 0 | if ((saved && !SCREEN_IS_ALTERNATE(&wp->base) && |
2795 | 0 | wp->window->sb == PANE_SCROLLBARS_ALWAYS) || |
2796 | 0 | (!saved && window_pane_scrollbar_reserve(wp))) { |
2797 | 0 | sb_w = wp->scrollbar_style.width; |
2798 | 0 | sb_pad = wp->scrollbar_style.pad; |
2799 | 0 | if (sb_w < 1) |
2800 | 0 | sb_w = 1; |
2801 | 0 | if (sb_pad < 0) |
2802 | 0 | sb_pad = 0; |
2803 | 0 | if ((int)sx - sb_w - sb_pad < PANE_MINIMUM) |
2804 | 0 | sx = PANE_MINIMUM; |
2805 | 0 | else |
2806 | 0 | sx -= sb_w + sb_pad; |
2807 | 0 | } |
2808 | |
|
2809 | 0 | return (format_printf("%u", sx)); |
2810 | 0 | } |
2811 | | |
2812 | | /* Callback for pane_width. */ |
2813 | | static void * |
2814 | | format_cb_pane_width(struct format_tree *ft) |
2815 | 415 | { |
2816 | 415 | if (ft->wp != NULL) |
2817 | 0 | return (format_printf("%u", ft->wp->sx)); |
2818 | 415 | return (NULL); |
2819 | 415 | } |
2820 | | |
2821 | | /* Callback for pane_x. */ |
2822 | | static void * |
2823 | | format_cb_pane_x(struct format_tree *ft) |
2824 | 415 | { |
2825 | 415 | if (ft->wp != NULL) |
2826 | 0 | return (format_printf("%d", ft->wp->xoff)); |
2827 | 415 | return (NULL); |
2828 | 415 | } |
2829 | | |
2830 | | /* Callback for pane_y. */ |
2831 | | static void * |
2832 | | format_cb_pane_y(struct format_tree *ft) |
2833 | 392 | { |
2834 | 392 | if (ft->wp != NULL) |
2835 | 0 | return (format_printf("%d", ft->wp->yoff)); |
2836 | 392 | return (NULL); |
2837 | 392 | } |
2838 | | |
2839 | | /* Callback for pane_z. */ |
2840 | | static void * |
2841 | | format_cb_pane_z(struct format_tree *ft) |
2842 | 496 | { |
2843 | 496 | u_int idx; |
2844 | | |
2845 | 496 | if (ft->wp != NULL && window_pane_zindex(ft->wp, &idx) == 0) |
2846 | 0 | return (format_printf("%u", idx)); |
2847 | 496 | return (NULL); |
2848 | 496 | } |
2849 | | |
2850 | | /* Callback for pane_zoomed_flag. */ |
2851 | | static void * |
2852 | | format_cb_pane_zoomed_flag(struct format_tree *ft) |
2853 | 506 | { |
2854 | 506 | struct window_pane *wp = ft->wp; |
2855 | | |
2856 | 506 | if (wp != NULL) { |
2857 | 0 | if (wp->flags & PANE_ZOOMED) |
2858 | 0 | return (xstrdup("1")); |
2859 | 0 | return (xstrdup("0")); |
2860 | 0 | } |
2861 | 506 | return (NULL); |
2862 | 506 | } |
2863 | | |
2864 | | /* Callback for scroll_region_lower. */ |
2865 | | static void * |
2866 | | format_cb_scroll_region_lower(struct format_tree *ft) |
2867 | 388 | { |
2868 | 388 | if (ft->wp != NULL) |
2869 | 0 | return (format_printf("%u", ft->wp->base.rlower)); |
2870 | 388 | return (NULL); |
2871 | 388 | } |
2872 | | |
2873 | | /* Callback for scroll_region_upper. */ |
2874 | | static void * |
2875 | | format_cb_scroll_region_upper(struct format_tree *ft) |
2876 | 388 | { |
2877 | 388 | if (ft->wp != NULL) |
2878 | 0 | return (format_printf("%u", ft->wp->base.rupper)); |
2879 | 388 | return (NULL); |
2880 | 388 | } |
2881 | | |
2882 | | /* Callback for server_sessions. */ |
2883 | | static void * |
2884 | | format_cb_server_sessions(__unused struct format_tree *ft) |
2885 | 388 | { |
2886 | 388 | struct session *s; |
2887 | 388 | u_int n = 0; |
2888 | | |
2889 | 388 | RB_FOREACH(s, sessions, &sessions) |
2890 | 0 | n++; |
2891 | 388 | return (format_printf("%u", n)); |
2892 | 388 | } |
2893 | | |
2894 | | /* Callback for session_active. */ |
2895 | | static void * |
2896 | | format_cb_session_active(struct format_tree *ft) |
2897 | 388 | { |
2898 | 388 | if (ft->s == NULL || ft->c == NULL) |
2899 | 388 | return (NULL); |
2900 | | |
2901 | 0 | if (ft->c->session == ft->s) |
2902 | 0 | return (xstrdup("1")); |
2903 | 0 | return (xstrdup("0")); |
2904 | 0 | } |
2905 | | |
2906 | | /* Callback for session_activity_flag. */ |
2907 | | static void * |
2908 | | format_cb_session_activity_flag(struct format_tree *ft) |
2909 | 388 | { |
2910 | 388 | struct winlink *wl; |
2911 | | |
2912 | 388 | if (ft->s != NULL) { |
2913 | 0 | RB_FOREACH(wl, winlinks, &ft->s->windows) { |
2914 | 0 | if (wl->flags & WINLINK_ACTIVITY) |
2915 | 0 | return (xstrdup("1")); |
2916 | 0 | } |
2917 | 0 | return (xstrdup("0")); |
2918 | 0 | } |
2919 | 388 | return (NULL); |
2920 | 388 | } |
2921 | | |
2922 | | /* Callback for session_bell_flag. */ |
2923 | | static void * |
2924 | | format_cb_session_bell_flag(struct format_tree *ft) |
2925 | 388 | { |
2926 | 388 | struct winlink *wl; |
2927 | | |
2928 | 388 | if (ft->s != NULL) { |
2929 | 0 | RB_FOREACH(wl, winlinks, &ft->s->windows) { |
2930 | 0 | if (wl->flags & WINLINK_BELL) |
2931 | 0 | return (xstrdup("1")); |
2932 | 0 | } |
2933 | 0 | return (xstrdup("0")); |
2934 | 0 | } |
2935 | 388 | return (NULL); |
2936 | 388 | } |
2937 | | |
2938 | | /* Callback for session_silence_flag. */ |
2939 | | static void * |
2940 | | format_cb_session_silence_flag(struct format_tree *ft) |
2941 | 5.62k | { |
2942 | 5.62k | struct winlink *wl; |
2943 | | |
2944 | 5.62k | if (ft->s != NULL) { |
2945 | 0 | RB_FOREACH(wl, winlinks, &ft->s->windows) { |
2946 | 0 | if (wl->flags & WINLINK_SILENCE) |
2947 | 0 | return (xstrdup("1")); |
2948 | 0 | } |
2949 | 0 | return (xstrdup("0")); |
2950 | 0 | } |
2951 | 5.62k | return (NULL); |
2952 | 5.62k | } |
2953 | | |
2954 | | /* Callback for session_attached. */ |
2955 | | static void * |
2956 | | format_cb_session_attached(struct format_tree *ft) |
2957 | 1.04k | { |
2958 | 1.04k | if (ft->s != NULL) |
2959 | 0 | return (format_printf("%u", ft->s->attached)); |
2960 | 1.04k | return (NULL); |
2961 | 1.04k | } |
2962 | | |
2963 | | /* Callback for session_format. */ |
2964 | | static void * |
2965 | | format_cb_session_format(struct format_tree *ft) |
2966 | 421 | { |
2967 | 421 | if (ft->type == FORMAT_TYPE_SESSION) |
2968 | 0 | return (xstrdup("1")); |
2969 | 421 | return (xstrdup("0")); |
2970 | 421 | } |
2971 | | |
2972 | | /* Callback for session_group. */ |
2973 | | static void * |
2974 | | format_cb_session_group(struct format_tree *ft) |
2975 | 388 | { |
2976 | 388 | struct session_group *sg; |
2977 | | |
2978 | 388 | if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL) |
2979 | 0 | return (xstrdup(sg->name)); |
2980 | 388 | return (NULL); |
2981 | 388 | } |
2982 | | |
2983 | | /* Callback for session_group_attached. */ |
2984 | | static void * |
2985 | | format_cb_session_group_attached(struct format_tree *ft) |
2986 | 480 | { |
2987 | 480 | struct session_group *sg; |
2988 | | |
2989 | 480 | if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL) |
2990 | 0 | return (format_printf("%u", session_group_attached_count (sg))); |
2991 | 480 | return (NULL); |
2992 | 480 | } |
2993 | | |
2994 | | /* Callback for session_group_many_attached. */ |
2995 | | static void * |
2996 | | format_cb_session_group_many_attached(struct format_tree *ft) |
2997 | 425 | { |
2998 | 425 | struct session_group *sg; |
2999 | | |
3000 | 425 | if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL) { |
3001 | 0 | if (session_group_attached_count (sg) > 1) |
3002 | 0 | return (xstrdup("1")); |
3003 | 0 | return (xstrdup("0")); |
3004 | 0 | } |
3005 | 425 | return (NULL); |
3006 | 425 | } |
3007 | | |
3008 | | /* Callback for session_group_size. */ |
3009 | | static void * |
3010 | | format_cb_session_group_size(struct format_tree *ft) |
3011 | 388 | { |
3012 | 388 | struct session_group *sg; |
3013 | | |
3014 | 388 | if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL) |
3015 | 0 | return (format_printf("%u", session_group_count (sg))); |
3016 | 388 | return (NULL); |
3017 | 388 | } |
3018 | | |
3019 | | /* Callback for session_grouped. */ |
3020 | | static void * |
3021 | | format_cb_session_grouped(struct format_tree *ft) |
3022 | 870 | { |
3023 | 870 | if (ft->s != NULL) { |
3024 | 0 | if (session_group_contains(ft->s) != NULL) |
3025 | 0 | return (xstrdup("1")); |
3026 | 0 | return (xstrdup("0")); |
3027 | 0 | } |
3028 | 870 | return (NULL); |
3029 | 870 | } |
3030 | | |
3031 | | /* Callback for session_id. */ |
3032 | | static void * |
3033 | | format_cb_session_id(struct format_tree *ft) |
3034 | 394 | { |
3035 | 394 | if (ft->s != NULL) |
3036 | 0 | return (format_printf("$%u", ft->s->id)); |
3037 | 394 | return (NULL); |
3038 | 394 | } |
3039 | | |
3040 | | /* Callback for session_many_attached. */ |
3041 | | static void * |
3042 | | format_cb_session_many_attached(struct format_tree *ft) |
3043 | 404 | { |
3044 | 404 | if (ft->s != NULL) { |
3045 | 0 | if (ft->s->attached > 1) |
3046 | 0 | return (xstrdup("1")); |
3047 | 0 | return (xstrdup("0")); |
3048 | 0 | } |
3049 | 404 | return (NULL); |
3050 | 404 | } |
3051 | | |
3052 | | /* Callback for session_marked. */ |
3053 | | static void * |
3054 | | format_cb_session_marked(struct format_tree *ft) |
3055 | 388 | { |
3056 | 388 | if (ft->s != NULL) { |
3057 | 0 | if (server_check_marked() && marked_pane.s == ft->s) |
3058 | 0 | return (xstrdup("1")); |
3059 | 0 | return (xstrdup("0")); |
3060 | 0 | } |
3061 | 388 | return (NULL); |
3062 | 388 | } |
3063 | | |
3064 | | /* Callback for session_name. */ |
3065 | | static void * |
3066 | | format_cb_session_name(struct format_tree *ft) |
3067 | 1.21M | { |
3068 | 1.21M | if (ft->s != NULL) |
3069 | 0 | return (xstrdup(ft->s->name)); |
3070 | 1.21M | return (NULL); |
3071 | 1.21M | } |
3072 | | |
3073 | | /* Callback for session_path. */ |
3074 | | static void * |
3075 | | format_cb_session_path(struct format_tree *ft) |
3076 | 388 | { |
3077 | 388 | if (ft->s != NULL) |
3078 | 0 | return (xstrdup(ft->s->cwd)); |
3079 | 388 | return (NULL); |
3080 | 388 | } |
3081 | | |
3082 | | /* Callback for session_windows. */ |
3083 | | static void * |
3084 | | format_cb_session_windows(struct format_tree *ft) |
3085 | 870 | { |
3086 | 870 | if (ft->s != NULL) |
3087 | 0 | return (format_printf("%u", winlink_count(&ft->s->windows))); |
3088 | 870 | return (NULL); |
3089 | 870 | } |
3090 | | |
3091 | | /* Callback for socket_path. */ |
3092 | | static void * |
3093 | | format_cb_socket_path(__unused struct format_tree *ft) |
3094 | 392 | { |
3095 | 392 | return (xstrdup(socket_path)); |
3096 | 392 | } |
3097 | | |
3098 | | /* Callback for version. */ |
3099 | | static void * |
3100 | | format_cb_version(__unused struct format_tree *ft) |
3101 | 500 | { |
3102 | 500 | return (xstrdup(getversion())); |
3103 | 500 | } |
3104 | | |
3105 | | /* Callback for image_support. */ |
3106 | | static void * |
3107 | | format_cb_image_support(__unused struct format_tree *ft) |
3108 | 481 | { |
3109 | | #ifdef ENABLE_IMAGES |
3110 | | return (xstrdup("1")); |
3111 | | #else |
3112 | 481 | return (xstrdup("0")); |
3113 | 481 | #endif |
3114 | 481 | } |
3115 | | |
3116 | | /* Callback for active_window_index. */ |
3117 | | static void * |
3118 | | format_cb_active_window_index(struct format_tree *ft) |
3119 | 388 | { |
3120 | 388 | if (ft->s != NULL) |
3121 | 0 | return (format_printf("%u", ft->s->curw->idx)); |
3122 | 388 | return (NULL); |
3123 | 388 | } |
3124 | | |
3125 | | /* Callback for last_window_index. */ |
3126 | | static void * |
3127 | | format_cb_last_window_index(struct format_tree *ft) |
3128 | 388 | { |
3129 | 388 | struct winlink *wl; |
3130 | | |
3131 | 388 | if (ft->s != NULL) { |
3132 | 0 | wl = RB_MAX(winlinks, &ft->s->windows); |
3133 | 0 | return (format_printf("%u", wl->idx)); |
3134 | 0 | } |
3135 | 388 | return (NULL); |
3136 | 388 | } |
3137 | | |
3138 | | /* Callback for window_active. */ |
3139 | | static void * |
3140 | | format_cb_window_active(struct format_tree *ft) |
3141 | 388 | { |
3142 | 388 | if (ft->wl != NULL) { |
3143 | 0 | if (ft->wl == ft->wl->session->curw) |
3144 | 0 | return (xstrdup("1")); |
3145 | 0 | return (xstrdup("0")); |
3146 | 0 | } |
3147 | 388 | return (NULL); |
3148 | 388 | } |
3149 | | |
3150 | | /* Callback for window_activity_flag. */ |
3151 | | static void * |
3152 | | format_cb_window_activity_flag(struct format_tree *ft) |
3153 | 829 | { |
3154 | 829 | if (ft->wl != NULL) { |
3155 | 0 | if (ft->wl->flags & WINLINK_ACTIVITY) |
3156 | 0 | return (xstrdup("1")); |
3157 | 0 | return (xstrdup("0")); |
3158 | 0 | } |
3159 | 829 | return (NULL); |
3160 | 829 | } |
3161 | | |
3162 | | /* Callback for window_bell_flag. */ |
3163 | | static void * |
3164 | | format_cb_window_bell_flag(struct format_tree *ft) |
3165 | 934 | { |
3166 | 934 | if (ft->wl != NULL) { |
3167 | 0 | if (ft->wl->flags & WINLINK_BELL) |
3168 | 0 | return (xstrdup("1")); |
3169 | 0 | return (xstrdup("0")); |
3170 | 0 | } |
3171 | 934 | return (NULL); |
3172 | 934 | } |
3173 | | |
3174 | | /* Callback for window_bigger. */ |
3175 | | static void * |
3176 | | format_cb_window_bigger(struct format_tree *ft) |
3177 | 529 | { |
3178 | 529 | u_int ox, oy, sx, sy; |
3179 | | |
3180 | 529 | if (ft->c != NULL) { |
3181 | 0 | if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy)) |
3182 | 0 | return (xstrdup("1")); |
3183 | 0 | return (xstrdup("0")); |
3184 | 0 | } |
3185 | 529 | return (NULL); |
3186 | 529 | } |
3187 | | |
3188 | | /* Callback for window_cell_height. */ |
3189 | | static void * |
3190 | | format_cb_window_cell_height(struct format_tree *ft) |
3191 | 388 | { |
3192 | 388 | if (ft->w != NULL) |
3193 | 0 | return (format_printf("%u", ft->w->ypixel)); |
3194 | 388 | return (NULL); |
3195 | 388 | } |
3196 | | |
3197 | | /* Callback for window_cell_width. */ |
3198 | | static void * |
3199 | | format_cb_window_cell_width(struct format_tree *ft) |
3200 | 388 | { |
3201 | 388 | if (ft->w != NULL) |
3202 | 0 | return (format_printf("%u", ft->w->xpixel)); |
3203 | 388 | return (NULL); |
3204 | 388 | } |
3205 | | |
3206 | | /* Callback for window_end_flag. */ |
3207 | | static void * |
3208 | | format_cb_window_end_flag(struct format_tree *ft) |
3209 | 392 | { |
3210 | 392 | if (ft->wl != NULL) { |
3211 | 0 | if (ft->wl == RB_MAX(winlinks, &ft->wl->session->windows)) |
3212 | 0 | return (xstrdup("1")); |
3213 | 0 | return (xstrdup("0")); |
3214 | 0 | } |
3215 | 392 | return (NULL); |
3216 | 392 | } |
3217 | | |
3218 | | /* Callback for window_flags. */ |
3219 | | static void * |
3220 | | format_cb_window_flags(struct format_tree *ft) |
3221 | 922 | { |
3222 | 922 | if (ft->wl != NULL) |
3223 | 0 | return (xstrdup(window_printable_flags(ft->wl, 1))); |
3224 | 922 | return (NULL); |
3225 | 922 | } |
3226 | | |
3227 | | /* Callback for window_format. */ |
3228 | | static void * |
3229 | | format_cb_window_format(struct format_tree *ft) |
3230 | 454 | { |
3231 | 454 | if (ft->type == FORMAT_TYPE_WINDOW) |
3232 | 0 | return (xstrdup("1")); |
3233 | 454 | return (xstrdup("0")); |
3234 | 454 | } |
3235 | | |
3236 | | /* Callback for window_height. */ |
3237 | | static void * |
3238 | | format_cb_window_height(struct format_tree *ft) |
3239 | 388 | { |
3240 | 388 | if (ft->w != NULL) |
3241 | 0 | return (format_printf("%u", ft->w->sy)); |
3242 | 388 | return (NULL); |
3243 | 388 | } |
3244 | | |
3245 | | /* Callback for window_manual_height. */ |
3246 | | static void * |
3247 | | format_cb_window_manual_height(struct format_tree *ft) |
3248 | 469 | { |
3249 | 469 | struct window *w = ft->w; |
3250 | | |
3251 | 469 | if (w == NULL) |
3252 | 469 | return (NULL); |
3253 | 0 | if (options_get_number(w->options, "window-size") != WINDOW_SIZE_MANUAL) |
3254 | 0 | return (xstrdup("")); |
3255 | 0 | return (format_printf("%u", w->manual_sy)); |
3256 | 0 | } |
3257 | | |
3258 | | /* Callback for window_id. */ |
3259 | | static void * |
3260 | | format_cb_window_id(struct format_tree *ft) |
3261 | 416 | { |
3262 | 416 | if (ft->w != NULL) |
3263 | 0 | return (format_printf("@%u", ft->w->id)); |
3264 | 416 | return (NULL); |
3265 | 416 | } |
3266 | | |
3267 | | /* Callback for window_index. */ |
3268 | | static void * |
3269 | | format_cb_window_index(struct format_tree *ft) |
3270 | 14.7k | { |
3271 | 14.7k | if (ft->wl != NULL) |
3272 | 0 | return (format_printf("%d", ft->wl->idx)); |
3273 | 14.7k | return (NULL); |
3274 | 14.7k | } |
3275 | | |
3276 | | /* Callback for window_last_flag. */ |
3277 | | static void * |
3278 | | format_cb_window_last_flag(struct format_tree *ft) |
3279 | 9.59k | { |
3280 | 9.59k | if (ft->wl != NULL) { |
3281 | 0 | if (ft->wl == TAILQ_FIRST(&ft->wl->session->lastw)) |
3282 | 0 | return (xstrdup("1")); |
3283 | 0 | return (xstrdup("0")); |
3284 | 0 | } |
3285 | 9.59k | return (NULL); |
3286 | 9.59k | } |
3287 | | |
3288 | | /* Callback for window_linked. */ |
3289 | | static void * |
3290 | | format_cb_window_linked(struct format_tree *ft) |
3291 | 401 | { |
3292 | 401 | struct winlink *wl; |
3293 | 401 | struct session *s; |
3294 | 401 | int found = 0; |
3295 | | |
3296 | 401 | if (ft->wl != NULL) { |
3297 | 0 | RB_FOREACH(s, sessions, &sessions) { |
3298 | 0 | RB_FOREACH(wl, winlinks, &s->windows) { |
3299 | 0 | if (wl->window == ft->wl->window) { |
3300 | 0 | if (found) |
3301 | 0 | return (xstrdup("1")); |
3302 | 0 | found = 1; |
3303 | 0 | } |
3304 | 0 | } |
3305 | 0 | } |
3306 | 0 | return (xstrdup("0")); |
3307 | 0 | } |
3308 | 401 | return (NULL); |
3309 | 401 | } |
3310 | | |
3311 | | /* Callback for window_linked_sessions. */ |
3312 | | static void * |
3313 | | format_cb_window_linked_sessions(struct format_tree *ft) |
3314 | 388 | { |
3315 | 388 | struct window *w; |
3316 | 388 | struct session_group *sg; |
3317 | 388 | struct session *s; |
3318 | 388 | u_int n = 0; |
3319 | | |
3320 | 388 | if (ft->wl == NULL) |
3321 | 388 | return (NULL); |
3322 | 0 | w = ft->wl->window; |
3323 | |
|
3324 | 0 | RB_FOREACH(sg, session_groups, &session_groups) { |
3325 | 0 | s = TAILQ_FIRST(&sg->sessions); |
3326 | 0 | if (winlink_find_by_window(&s->windows, w) != NULL) |
3327 | 0 | n++; |
3328 | 0 | } |
3329 | 0 | RB_FOREACH(s, sessions, &sessions) { |
3330 | 0 | if (session_group_contains(s) != NULL) |
3331 | 0 | continue; |
3332 | 0 | if (winlink_find_by_window(&s->windows, w) != NULL) |
3333 | 0 | n++; |
3334 | 0 | } |
3335 | 0 | return (format_printf("%u", n)); |
3336 | 388 | } |
3337 | | |
3338 | | /* Callback for window_marked_flag. */ |
3339 | | static void * |
3340 | | format_cb_window_marked_flag(struct format_tree *ft) |
3341 | 417 | { |
3342 | 417 | if (ft->wl != NULL) { |
3343 | 0 | if (server_check_marked() && marked_pane.wl == ft->wl) |
3344 | 0 | return (xstrdup("1")); |
3345 | 0 | return (xstrdup("0")); |
3346 | 0 | } |
3347 | 417 | return (NULL); |
3348 | 417 | } |
3349 | | |
3350 | | /* Callback for window_modal_pane. */ |
3351 | | static void * |
3352 | | format_cb_window_modal_pane(struct format_tree *ft) |
3353 | 804 | { |
3354 | 804 | if (ft->w != NULL && ft->w->modal != NULL) |
3355 | 0 | return (format_printf("%%%u", ft->w->modal->id)); |
3356 | 804 | return (NULL); |
3357 | 804 | } |
3358 | | |
3359 | | /* Callback for window_name. */ |
3360 | | static void * |
3361 | | format_cb_window_name(struct format_tree *ft) |
3362 | 14.1k | { |
3363 | 14.1k | if (ft->w != NULL) |
3364 | 0 | return (format_printf("%s", ft->w->name)); |
3365 | 14.1k | return (NULL); |
3366 | 14.1k | } |
3367 | | |
3368 | | /* Callback for window_offset_x. */ |
3369 | | static void * |
3370 | | format_cb_window_offset_x(struct format_tree *ft) |
3371 | 415 | { |
3372 | 415 | u_int ox, oy, sx, sy; |
3373 | | |
3374 | 415 | if (ft->c != NULL) { |
3375 | 0 | if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy)) |
3376 | 0 | return (format_printf("%u", ox)); |
3377 | 0 | return (NULL); |
3378 | 0 | } |
3379 | 415 | return (NULL); |
3380 | 415 | } |
3381 | | |
3382 | | /* Callback for window_offset_y. */ |
3383 | | static void * |
3384 | | format_cb_window_offset_y(struct format_tree *ft) |
3385 | 416 | { |
3386 | 416 | u_int ox, oy, sx, sy; |
3387 | | |
3388 | 416 | if (ft->c != NULL) { |
3389 | 0 | if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy)) |
3390 | 0 | return (format_printf("%u", oy)); |
3391 | 0 | return (NULL); |
3392 | 0 | } |
3393 | 416 | return (NULL); |
3394 | 416 | } |
3395 | | |
3396 | | /* Callback for window_panes. */ |
3397 | | static void * |
3398 | | format_cb_window_panes(struct format_tree *ft) |
3399 | 416 | { |
3400 | 416 | if (ft->w != NULL) |
3401 | 0 | return (format_printf("%u", window_count_panes(ft->w, 1))); |
3402 | 416 | return (NULL); |
3403 | 416 | } |
3404 | | |
3405 | | /* Callback for window_raw_flags. */ |
3406 | | static void * |
3407 | | format_cb_window_raw_flags(struct format_tree *ft) |
3408 | 408 | { |
3409 | 408 | if (ft->wl != NULL) |
3410 | 0 | return (xstrdup(window_printable_flags(ft->wl, 0))); |
3411 | 408 | return (NULL); |
3412 | 408 | } |
3413 | | |
3414 | | /* Callback for window_silence_flag. */ |
3415 | | static void * |
3416 | | format_cb_window_silence_flag(struct format_tree *ft) |
3417 | 934 | { |
3418 | 934 | if (ft->wl != NULL) { |
3419 | 0 | if (ft->wl->flags & WINLINK_SILENCE) |
3420 | 0 | return (xstrdup("1")); |
3421 | 0 | return (xstrdup("0")); |
3422 | 0 | } |
3423 | 934 | return (NULL); |
3424 | 934 | } |
3425 | | |
3426 | | /* Callback for window_start_flag. */ |
3427 | | static void * |
3428 | | format_cb_window_start_flag(struct format_tree *ft) |
3429 | 402 | { |
3430 | 402 | if (ft->wl != NULL) { |
3431 | 0 | if (ft->wl == RB_MIN(winlinks, &ft->wl->session->windows)) |
3432 | 0 | return (xstrdup("1")); |
3433 | 0 | return (xstrdup("0")); |
3434 | 0 | } |
3435 | 402 | return (NULL); |
3436 | 402 | } |
3437 | | |
3438 | | /* Callback for window_width. */ |
3439 | | static void * |
3440 | | format_cb_window_width(struct format_tree *ft) |
3441 | 8.54k | { |
3442 | 8.54k | if (ft->w != NULL) |
3443 | 0 | return (format_printf("%u", ft->w->sx)); |
3444 | 8.54k | return (NULL); |
3445 | 8.54k | } |
3446 | | |
3447 | | /* Callback for window_manual_width. */ |
3448 | | static void * |
3449 | | format_cb_window_manual_width(struct format_tree *ft) |
3450 | 533 | { |
3451 | 533 | struct window *w = ft->w; |
3452 | | |
3453 | 533 | if (w == NULL) |
3454 | 533 | return (NULL); |
3455 | 0 | if (options_get_number(w->options, "window-size") != WINDOW_SIZE_MANUAL) |
3456 | 0 | return (xstrdup("")); |
3457 | 0 | return (format_printf("%u", w->manual_sx)); |
3458 | 0 | } |
3459 | | |
3460 | | /* Callback for window_zoomed_flag. */ |
3461 | | static void * |
3462 | | format_cb_window_zoomed_flag(struct format_tree *ft) |
3463 | 388 | { |
3464 | 388 | if (ft->w != NULL) { |
3465 | 0 | if (ft->w->flags & WINDOW_ZOOMED) |
3466 | 0 | return (xstrdup("1")); |
3467 | 0 | return (xstrdup("0")); |
3468 | 0 | } |
3469 | 388 | return (NULL); |
3470 | 388 | } |
3471 | | |
3472 | | /* Callback for wrap_flag. */ |
3473 | | static void * |
3474 | | format_cb_wrap_flag(struct format_tree *ft) |
3475 | 388 | { |
3476 | 388 | if (ft->wp != NULL) { |
3477 | 0 | if (ft->wp->base.mode & MODE_WRAP) |
3478 | 0 | return (xstrdup("1")); |
3479 | 0 | return (xstrdup("0")); |
3480 | 0 | } |
3481 | 388 | return (NULL); |
3482 | 388 | } |
3483 | | |
3484 | | /* Callback for buffer_created. */ |
3485 | | static void * |
3486 | | format_cb_buffer_created(struct format_tree *ft) |
3487 | 389 | { |
3488 | 389 | static struct timeval tv; |
3489 | | |
3490 | 389 | if (ft->pb != NULL) { |
3491 | 0 | timerclear(&tv); |
3492 | 0 | tv.tv_sec = paste_buffer_created(ft->pb); |
3493 | 0 | return (&tv); |
3494 | 0 | } |
3495 | 389 | return (NULL); |
3496 | 389 | } |
3497 | | |
3498 | | /* Callback for client_activity. */ |
3499 | | static void * |
3500 | | format_cb_client_activity(struct format_tree *ft) |
3501 | 392 | { |
3502 | 392 | if (ft->c != NULL) |
3503 | 0 | return (&ft->c->activity_time); |
3504 | 392 | return (NULL); |
3505 | 392 | } |
3506 | | |
3507 | | /* Callback for client_created. */ |
3508 | | static void * |
3509 | | format_cb_client_created(struct format_tree *ft) |
3510 | 388 | { |
3511 | 388 | if (ft->c != NULL) |
3512 | 0 | return (&ft->c->creation_time); |
3513 | 388 | return (NULL); |
3514 | 388 | } |
3515 | | |
3516 | | /* Callback for session_activity. */ |
3517 | | static void * |
3518 | | format_cb_session_activity(struct format_tree *ft) |
3519 | 415 | { |
3520 | 415 | if (ft->s != NULL) |
3521 | 0 | return (&ft->s->activity_time); |
3522 | 415 | return (NULL); |
3523 | 415 | } |
3524 | | |
3525 | | /* Callback for session_created. */ |
3526 | | static void * |
3527 | | format_cb_session_created(struct format_tree *ft) |
3528 | 428 | { |
3529 | 428 | if (ft->s != NULL) |
3530 | 0 | return (&ft->s->creation_time); |
3531 | 428 | return (NULL); |
3532 | 428 | } |
3533 | | |
3534 | | /* Callback for session_last_attached. */ |
3535 | | static void * |
3536 | | format_cb_session_last_attached(struct format_tree *ft) |
3537 | 404 | { |
3538 | 404 | if (ft->s != NULL) |
3539 | 0 | return (&ft->s->last_attached_time); |
3540 | 404 | return (NULL); |
3541 | 404 | } |
3542 | | |
3543 | | /* Callback for start_time. */ |
3544 | | static void * |
3545 | | format_cb_start_time(__unused struct format_tree *ft) |
3546 | 485 | { |
3547 | 485 | return (&start_time); |
3548 | 485 | } |
3549 | | |
3550 | | /* Callback for window_activity. */ |
3551 | | static void * |
3552 | | format_cb_window_activity(struct format_tree *ft) |
3553 | 388 | { |
3554 | 388 | if (ft->w != NULL) |
3555 | 0 | return (&ft->w->activity_time); |
3556 | 388 | return (NULL); |
3557 | 388 | } |
3558 | | |
3559 | | /* Callback for buffer_mode_format, */ |
3560 | | static void * |
3561 | | format_cb_buffer_mode_format(__unused struct format_tree *ft) |
3562 | 434 | { |
3563 | 434 | return (xstrdup(window_buffer_mode.default_format)); |
3564 | 434 | } |
3565 | | |
3566 | | /* Callback for client_mode_format, */ |
3567 | | static void * |
3568 | | format_cb_client_mode_format(__unused struct format_tree *ft) |
3569 | 389 | { |
3570 | 389 | return (xstrdup(window_client_mode.default_format)); |
3571 | 389 | } |
3572 | | |
3573 | | /* Callback for tree_mode_format, */ |
3574 | | static void * |
3575 | | format_cb_tree_mode_format(__unused struct format_tree *ft) |
3576 | 8.26k | { |
3577 | 8.26k | return (xstrdup(window_tree_mode.default_format)); |
3578 | 8.26k | } |
3579 | | |
3580 | | /* Callback for uid. */ |
3581 | | static void * |
3582 | | format_cb_uid(__unused struct format_tree *ft) |
3583 | 1.47k | { |
3584 | 1.47k | return (format_printf("%ld", (long)getuid())); |
3585 | 1.47k | } |
3586 | | |
3587 | | /* Callback for user. */ |
3588 | | static void * |
3589 | | format_cb_user(__unused struct format_tree *ft) |
3590 | 442 | { |
3591 | 442 | static char *cached; |
3592 | 442 | struct passwd *pw; |
3593 | | |
3594 | 442 | if (cached == NULL && (pw = getpwuid(getuid())) != NULL) |
3595 | 2 | cached = xstrdup(pw->pw_name); |
3596 | 442 | if (cached != NULL) |
3597 | 442 | return (xstrdup(cached)); |
3598 | 0 | return (NULL); |
3599 | 442 | } |
3600 | | |
3601 | | /* Format table type. */ |
3602 | | enum format_table_type { |
3603 | | FORMAT_TABLE_STRING, |
3604 | | FORMAT_TABLE_TIME |
3605 | | }; |
3606 | | |
3607 | | /* Format table entry. */ |
3608 | | struct format_table_entry { |
3609 | | const char *key; |
3610 | | enum format_table_type type; |
3611 | | format_cb cb; |
3612 | | }; |
3613 | | |
3614 | | /* |
3615 | | * Format table. Default format variables (that are almost always in the tree |
3616 | | * and where the value is expanded by a callback in this file) are listed here. |
3617 | | * Only variables which are added by the caller go into the tree. |
3618 | | */ |
3619 | | static const struct format_table_entry format_table[] = { |
3620 | | { "active_window_index", FORMAT_TABLE_STRING, |
3621 | | format_cb_active_window_index |
3622 | | }, |
3623 | | { "alternate_on", FORMAT_TABLE_STRING, |
3624 | | format_cb_alternate_on |
3625 | | }, |
3626 | | { "alternate_saved_x", FORMAT_TABLE_STRING, |
3627 | | format_cb_alternate_saved_x |
3628 | | }, |
3629 | | { "alternate_saved_y", FORMAT_TABLE_STRING, |
3630 | | format_cb_alternate_saved_y |
3631 | | }, |
3632 | | { "bracket_paste_flag", FORMAT_TABLE_STRING, |
3633 | | format_cb_bracket_paste_flag |
3634 | | }, |
3635 | | { "buffer_created", FORMAT_TABLE_TIME, |
3636 | | format_cb_buffer_created |
3637 | | }, |
3638 | | { "buffer_full", FORMAT_TABLE_STRING, |
3639 | | format_cb_buffer_full |
3640 | | }, |
3641 | | { "buffer_mode_format", FORMAT_TABLE_STRING, |
3642 | | format_cb_buffer_mode_format |
3643 | | }, |
3644 | | { "buffer_name", FORMAT_TABLE_STRING, |
3645 | | format_cb_buffer_name |
3646 | | }, |
3647 | | { "buffer_sample", FORMAT_TABLE_STRING, |
3648 | | format_cb_buffer_sample |
3649 | | }, |
3650 | | { "buffer_size", FORMAT_TABLE_STRING, |
3651 | | format_cb_buffer_size |
3652 | | }, |
3653 | | { "client_activity", FORMAT_TABLE_TIME, |
3654 | | format_cb_client_activity |
3655 | | }, |
3656 | | { "client_cell_height", FORMAT_TABLE_STRING, |
3657 | | format_cb_client_cell_height |
3658 | | }, |
3659 | | { "client_cell_width", FORMAT_TABLE_STRING, |
3660 | | format_cb_client_cell_width |
3661 | | }, |
3662 | | { "client_colours", FORMAT_TABLE_STRING, |
3663 | | format_cb_client_colours |
3664 | | }, |
3665 | | { "client_control_mode", FORMAT_TABLE_STRING, |
3666 | | format_cb_client_control_mode |
3667 | | }, |
3668 | | { "client_created", FORMAT_TABLE_TIME, |
3669 | | format_cb_client_created |
3670 | | }, |
3671 | | { "client_discarded", FORMAT_TABLE_STRING, |
3672 | | format_cb_client_discarded |
3673 | | }, |
3674 | | { "client_flags", FORMAT_TABLE_STRING, |
3675 | | format_cb_client_flags |
3676 | | }, |
3677 | | { "client_height", FORMAT_TABLE_STRING, |
3678 | | format_cb_client_height |
3679 | | }, |
3680 | | { "client_key_table", FORMAT_TABLE_STRING, |
3681 | | format_cb_client_key_table |
3682 | | }, |
3683 | | { "client_last_session", FORMAT_TABLE_STRING, |
3684 | | format_cb_client_last_session |
3685 | | }, |
3686 | | { "client_mode_format", FORMAT_TABLE_STRING, |
3687 | | format_cb_client_mode_format |
3688 | | }, |
3689 | | { "client_name", FORMAT_TABLE_STRING, |
3690 | | format_cb_client_name |
3691 | | }, |
3692 | | { "client_pid", FORMAT_TABLE_STRING, |
3693 | | format_cb_client_pid |
3694 | | }, |
3695 | | { "client_prefix", FORMAT_TABLE_STRING, |
3696 | | format_cb_client_prefix |
3697 | | }, |
3698 | | { "client_readonly", FORMAT_TABLE_STRING, |
3699 | | format_cb_client_readonly |
3700 | | }, |
3701 | | { "client_session", FORMAT_TABLE_STRING, |
3702 | | format_cb_client_session |
3703 | | }, |
3704 | | { "client_termfeatures", FORMAT_TABLE_STRING, |
3705 | | format_cb_client_termfeatures |
3706 | | }, |
3707 | | { "client_termname", FORMAT_TABLE_STRING, |
3708 | | format_cb_client_termname |
3709 | | }, |
3710 | | { "client_termtype", FORMAT_TABLE_STRING, |
3711 | | format_cb_client_termtype |
3712 | | }, |
3713 | | { "client_theme", FORMAT_TABLE_STRING, |
3714 | | format_cb_client_theme |
3715 | | }, |
3716 | | { "client_tty", FORMAT_TABLE_STRING, |
3717 | | format_cb_client_tty |
3718 | | }, |
3719 | | { "client_uid", FORMAT_TABLE_STRING, |
3720 | | format_cb_client_uid |
3721 | | }, |
3722 | | { "client_user", FORMAT_TABLE_STRING, |
3723 | | format_cb_client_user |
3724 | | }, |
3725 | | { "client_utf8", FORMAT_TABLE_STRING, |
3726 | | format_cb_client_utf8 |
3727 | | }, |
3728 | | { "client_width", FORMAT_TABLE_STRING, |
3729 | | format_cb_client_width |
3730 | | }, |
3731 | | { "client_written", FORMAT_TABLE_STRING, |
3732 | | format_cb_client_written |
3733 | | }, |
3734 | | { "config_files", FORMAT_TABLE_STRING, |
3735 | | format_cb_config_files |
3736 | | }, |
3737 | | { "cursor_blinking", FORMAT_TABLE_STRING, |
3738 | | format_cb_cursor_blinking |
3739 | | }, |
3740 | | { "cursor_character", FORMAT_TABLE_STRING, |
3741 | | format_cb_cursor_character |
3742 | | }, |
3743 | | { "cursor_colour", FORMAT_TABLE_STRING, |
3744 | | format_cb_cursor_colour |
3745 | | }, |
3746 | | { "cursor_flag", FORMAT_TABLE_STRING, |
3747 | | format_cb_cursor_flag |
3748 | | }, |
3749 | | { "cursor_shape", FORMAT_TABLE_STRING, |
3750 | | format_cb_cursor_shape |
3751 | | }, |
3752 | | { "cursor_very_visible", FORMAT_TABLE_STRING, |
3753 | | format_cb_cursor_very_visible |
3754 | | }, |
3755 | | { "cursor_x", FORMAT_TABLE_STRING, |
3756 | | format_cb_cursor_x |
3757 | | }, |
3758 | | { "cursor_y", FORMAT_TABLE_STRING, |
3759 | | format_cb_cursor_y |
3760 | | }, |
3761 | | { "history_added", FORMAT_TABLE_STRING, |
3762 | | format_cb_history_added |
3763 | | }, |
3764 | | { "history_all_bytes", FORMAT_TABLE_STRING, |
3765 | | format_cb_history_all_bytes |
3766 | | }, |
3767 | | { "history_bytes", FORMAT_TABLE_STRING, |
3768 | | format_cb_history_bytes |
3769 | | }, |
3770 | | { "history_collected", FORMAT_TABLE_STRING, |
3771 | | format_cb_history_collected |
3772 | | }, |
3773 | | { "history_generation", FORMAT_TABLE_STRING, |
3774 | | format_cb_history_generation |
3775 | | }, |
3776 | | { "history_limit", FORMAT_TABLE_STRING, |
3777 | | format_cb_history_limit |
3778 | | }, |
3779 | | { "history_size", FORMAT_TABLE_STRING, |
3780 | | format_cb_history_size |
3781 | | }, |
3782 | | { "host", FORMAT_TABLE_STRING, |
3783 | | format_cb_host |
3784 | | }, |
3785 | | { "host_short", FORMAT_TABLE_STRING, |
3786 | | format_cb_host_short |
3787 | | }, |
3788 | | { "image_support", FORMAT_TABLE_STRING, |
3789 | | format_cb_image_support |
3790 | | }, |
3791 | | { "insert_flag", FORMAT_TABLE_STRING, |
3792 | | format_cb_insert_flag |
3793 | | }, |
3794 | | { "keypad_cursor_flag", FORMAT_TABLE_STRING, |
3795 | | format_cb_keypad_cursor_flag |
3796 | | }, |
3797 | | { "keypad_flag", FORMAT_TABLE_STRING, |
3798 | | format_cb_keypad_flag |
3799 | | }, |
3800 | | { "last_window_index", FORMAT_TABLE_STRING, |
3801 | | format_cb_last_window_index |
3802 | | }, |
3803 | | { "mouse_absolute_x", FORMAT_TABLE_STRING, |
3804 | | format_cb_mouse_absolute_x |
3805 | | }, |
3806 | | { "mouse_absolute_y", FORMAT_TABLE_STRING, |
3807 | | format_cb_mouse_absolute_y |
3808 | | }, |
3809 | | { "mouse_all_flag", FORMAT_TABLE_STRING, |
3810 | | format_cb_mouse_all_flag |
3811 | | }, |
3812 | | { "mouse_any_flag", FORMAT_TABLE_STRING, |
3813 | | format_cb_mouse_any_flag |
3814 | | }, |
3815 | | { "mouse_button_flag", FORMAT_TABLE_STRING, |
3816 | | format_cb_mouse_button_flag |
3817 | | }, |
3818 | | { "mouse_hyperlink", FORMAT_TABLE_STRING, |
3819 | | format_cb_mouse_hyperlink |
3820 | | }, |
3821 | | { "mouse_line", FORMAT_TABLE_STRING, |
3822 | | format_cb_mouse_line |
3823 | | }, |
3824 | | { "mouse_pane", FORMAT_TABLE_STRING, |
3825 | | format_cb_mouse_pane |
3826 | | }, |
3827 | | { "mouse_sgr_flag", FORMAT_TABLE_STRING, |
3828 | | format_cb_mouse_sgr_flag |
3829 | | }, |
3830 | | { "mouse_standard_flag", FORMAT_TABLE_STRING, |
3831 | | format_cb_mouse_standard_flag |
3832 | | }, |
3833 | | { "mouse_status_line", FORMAT_TABLE_STRING, |
3834 | | format_cb_mouse_status_line |
3835 | | }, |
3836 | | { "mouse_status_range", FORMAT_TABLE_STRING, |
3837 | | format_cb_mouse_status_range |
3838 | | }, |
3839 | | { "mouse_utf8_flag", FORMAT_TABLE_STRING, |
3840 | | format_cb_mouse_utf8_flag |
3841 | | }, |
3842 | | { "mouse_word", FORMAT_TABLE_STRING, |
3843 | | format_cb_mouse_word |
3844 | | }, |
3845 | | { "mouse_x", FORMAT_TABLE_STRING, |
3846 | | format_cb_mouse_x |
3847 | | }, |
3848 | | { "mouse_y", FORMAT_TABLE_STRING, |
3849 | | format_cb_mouse_y |
3850 | | }, |
3851 | | { "next_session_id", FORMAT_TABLE_STRING, |
3852 | | format_cb_next_session_id |
3853 | | }, |
3854 | | { "origin_flag", FORMAT_TABLE_STRING, |
3855 | | format_cb_origin_flag |
3856 | | }, |
3857 | | { "pane_active", FORMAT_TABLE_STRING, |
3858 | | format_cb_pane_active |
3859 | | }, |
3860 | | { "pane_at_bottom", FORMAT_TABLE_STRING, |
3861 | | format_cb_pane_at_bottom |
3862 | | }, |
3863 | | { "pane_at_left", FORMAT_TABLE_STRING, |
3864 | | format_cb_pane_at_left |
3865 | | }, |
3866 | | { "pane_at_right", FORMAT_TABLE_STRING, |
3867 | | format_cb_pane_at_right |
3868 | | }, |
3869 | | { "pane_at_top", FORMAT_TABLE_STRING, |
3870 | | format_cb_pane_at_top |
3871 | | }, |
3872 | | { "pane_bg", FORMAT_TABLE_STRING, |
3873 | | format_cb_pane_bg |
3874 | | }, |
3875 | | { "pane_bottom", FORMAT_TABLE_STRING, |
3876 | | format_cb_pane_bottom |
3877 | | }, |
3878 | | { "pane_command_duration", FORMAT_TABLE_STRING, |
3879 | | format_cb_pane_command_duration |
3880 | | }, |
3881 | | { "pane_command_end_time", FORMAT_TABLE_TIME, |
3882 | | format_cb_pane_command_end_time |
3883 | | }, |
3884 | | { "pane_command_line", FORMAT_TABLE_STRING, |
3885 | | format_cb_pane_command_line |
3886 | | }, |
3887 | | { "pane_command_running", FORMAT_TABLE_STRING, |
3888 | | format_cb_pane_command_running |
3889 | | }, |
3890 | | { "pane_command_start_time", FORMAT_TABLE_TIME, |
3891 | | format_cb_pane_command_start_time |
3892 | | }, |
3893 | | { "pane_command_status", FORMAT_TABLE_STRING, |
3894 | | format_cb_pane_command_status |
3895 | | }, |
3896 | | { "pane_current_command", FORMAT_TABLE_STRING, |
3897 | | format_cb_current_command |
3898 | | }, |
3899 | | { "pane_current_path", FORMAT_TABLE_STRING, |
3900 | | format_cb_current_path |
3901 | | }, |
3902 | | { "pane_dead", FORMAT_TABLE_STRING, |
3903 | | format_cb_pane_dead |
3904 | | }, |
3905 | | { "pane_dead_signal", FORMAT_TABLE_STRING, |
3906 | | format_cb_pane_dead_signal |
3907 | | }, |
3908 | | { "pane_dead_status", FORMAT_TABLE_STRING, |
3909 | | format_cb_pane_dead_status |
3910 | | }, |
3911 | | { "pane_dead_time", FORMAT_TABLE_TIME, |
3912 | | format_cb_pane_dead_time |
3913 | | }, |
3914 | | { "pane_fg", FORMAT_TABLE_STRING, |
3915 | | format_cb_pane_fg |
3916 | | }, |
3917 | | { "pane_flags", FORMAT_TABLE_STRING, |
3918 | | format_cb_pane_flags |
3919 | | }, |
3920 | | { "pane_floating_flag", FORMAT_TABLE_STRING, |
3921 | | format_cb_pane_floating_flag |
3922 | | }, |
3923 | | { "pane_format", FORMAT_TABLE_STRING, |
3924 | | format_cb_pane_format |
3925 | | }, |
3926 | | { "pane_height", FORMAT_TABLE_STRING, |
3927 | | format_cb_pane_height |
3928 | | }, |
3929 | | { "pane_id", FORMAT_TABLE_STRING, |
3930 | | format_cb_pane_id |
3931 | | }, |
3932 | | { "pane_in_mode", FORMAT_TABLE_STRING, |
3933 | | format_cb_pane_in_mode |
3934 | | }, |
3935 | | { "pane_index", FORMAT_TABLE_STRING, |
3936 | | format_cb_pane_index |
3937 | | }, |
3938 | | { "pane_input_off", FORMAT_TABLE_STRING, |
3939 | | format_cb_pane_input_off |
3940 | | }, |
3941 | | { "pane_key_mode", FORMAT_TABLE_STRING, |
3942 | | format_cb_pane_key_mode |
3943 | | }, |
3944 | | { "pane_last", FORMAT_TABLE_STRING, |
3945 | | format_cb_pane_last |
3946 | | }, |
3947 | | { "pane_last_output_time", FORMAT_TABLE_TIME, |
3948 | | format_cb_pane_last_output_time |
3949 | | }, |
3950 | | { "pane_last_prompt_time", FORMAT_TABLE_TIME, |
3951 | | format_cb_pane_last_prompt_time |
3952 | | }, |
3953 | | { "pane_left", FORMAT_TABLE_STRING, |
3954 | | format_cb_pane_left |
3955 | | }, |
3956 | | { "pane_marked", FORMAT_TABLE_STRING, |
3957 | | format_cb_pane_marked |
3958 | | }, |
3959 | | { "pane_marked_set", FORMAT_TABLE_STRING, |
3960 | | format_cb_pane_marked_set |
3961 | | }, |
3962 | | { "pane_modal_flag", FORMAT_TABLE_STRING, |
3963 | | format_cb_pane_modal_flag |
3964 | | }, |
3965 | | { "pane_mode", FORMAT_TABLE_STRING, |
3966 | | format_cb_pane_mode |
3967 | | }, |
3968 | | { "pane_output_generation", FORMAT_TABLE_STRING, |
3969 | | format_cb_pane_output_generation |
3970 | | }, |
3971 | | { "pane_path", FORMAT_TABLE_STRING, |
3972 | | format_cb_pane_path |
3973 | | }, |
3974 | | { "pane_pb_progress", FORMAT_TABLE_STRING, |
3975 | | format_cb_pane_pb_progress |
3976 | | }, |
3977 | | { "pane_pb_state", FORMAT_TABLE_STRING, |
3978 | | format_cb_pane_pb_state |
3979 | | }, |
3980 | | { "pane_pid", FORMAT_TABLE_STRING, |
3981 | | format_cb_pane_pid |
3982 | | }, |
3983 | | { "pane_pipe", FORMAT_TABLE_STRING, |
3984 | | format_cb_pane_pipe |
3985 | | }, |
3986 | | { "pane_pipe_pid", FORMAT_TABLE_STRING, |
3987 | | format_cb_pane_pipe_pid |
3988 | | }, |
3989 | | { "pane_private_modes", FORMAT_TABLE_STRING, |
3990 | | format_cb_pane_private_modes |
3991 | | }, |
3992 | | { "pane_right", FORMAT_TABLE_STRING, |
3993 | | format_cb_pane_right |
3994 | | }, |
3995 | | { "pane_search_string", FORMAT_TABLE_STRING, |
3996 | | format_cb_pane_search_string |
3997 | | }, |
3998 | | { "pane_start_command", FORMAT_TABLE_STRING, |
3999 | | format_cb_start_command |
4000 | | }, |
4001 | | { "pane_start_command_list", FORMAT_TABLE_STRING, |
4002 | | format_cb_start_command_list |
4003 | | }, |
4004 | | { "pane_start_path", FORMAT_TABLE_STRING, |
4005 | | format_cb_start_path |
4006 | | }, |
4007 | | { "pane_synchronized", FORMAT_TABLE_STRING, |
4008 | | format_cb_pane_synchronized |
4009 | | }, |
4010 | | { "pane_tabs", FORMAT_TABLE_STRING, |
4011 | | format_cb_pane_tabs |
4012 | | }, |
4013 | | { "pane_title", FORMAT_TABLE_STRING, |
4014 | | format_cb_pane_title |
4015 | | }, |
4016 | | { "pane_top", FORMAT_TABLE_STRING, |
4017 | | format_cb_pane_top |
4018 | | }, |
4019 | | { "pane_tty", FORMAT_TABLE_STRING, |
4020 | | format_cb_pane_tty |
4021 | | }, |
4022 | | { "pane_unseen_changes", FORMAT_TABLE_STRING, |
4023 | | format_cb_pane_unseen_changes |
4024 | | }, |
4025 | | { "pane_unzoomed_height", FORMAT_TABLE_STRING, |
4026 | | format_cb_pane_unzoomed_height |
4027 | | }, |
4028 | | { "pane_unzoomed_width", FORMAT_TABLE_STRING, |
4029 | | format_cb_pane_unzoomed_width |
4030 | | }, |
4031 | | { "pane_width", FORMAT_TABLE_STRING, |
4032 | | format_cb_pane_width |
4033 | | }, |
4034 | | { "pane_x", FORMAT_TABLE_STRING, |
4035 | | format_cb_pane_x |
4036 | | }, |
4037 | | { "pane_y", FORMAT_TABLE_STRING, |
4038 | | format_cb_pane_y |
4039 | | }, |
4040 | | { "pane_z", FORMAT_TABLE_STRING, |
4041 | | format_cb_pane_z |
4042 | | }, |
4043 | | { "pane_zoomed_flag", FORMAT_TABLE_STRING, |
4044 | | format_cb_pane_zoomed_flag |
4045 | | }, |
4046 | | { "pid", FORMAT_TABLE_STRING, |
4047 | | format_cb_pid |
4048 | | }, |
4049 | | { "scroll_region_lower", FORMAT_TABLE_STRING, |
4050 | | format_cb_scroll_region_lower |
4051 | | }, |
4052 | | { "scroll_region_upper", FORMAT_TABLE_STRING, |
4053 | | format_cb_scroll_region_upper |
4054 | | }, |
4055 | | { "server_sessions", FORMAT_TABLE_STRING, |
4056 | | format_cb_server_sessions |
4057 | | }, |
4058 | | { "session_active", FORMAT_TABLE_STRING, |
4059 | | format_cb_session_active |
4060 | | }, |
4061 | | { "session_activity", FORMAT_TABLE_TIME, |
4062 | | format_cb_session_activity |
4063 | | }, |
4064 | | { "session_activity_flag", FORMAT_TABLE_STRING, |
4065 | | format_cb_session_activity_flag |
4066 | | }, |
4067 | | { "session_alert", FORMAT_TABLE_STRING, |
4068 | | format_cb_session_alert |
4069 | | }, |
4070 | | { "session_alerts", FORMAT_TABLE_STRING, |
4071 | | format_cb_session_alerts |
4072 | | }, |
4073 | | { "session_attached", FORMAT_TABLE_STRING, |
4074 | | format_cb_session_attached |
4075 | | }, |
4076 | | { "session_attached_list", FORMAT_TABLE_STRING, |
4077 | | format_cb_session_attached_list |
4078 | | }, |
4079 | | { "session_bell_flag", FORMAT_TABLE_STRING, |
4080 | | format_cb_session_bell_flag |
4081 | | }, |
4082 | | { "session_created", FORMAT_TABLE_TIME, |
4083 | | format_cb_session_created |
4084 | | }, |
4085 | | { "session_format", FORMAT_TABLE_STRING, |
4086 | | format_cb_session_format |
4087 | | }, |
4088 | | { "session_group", FORMAT_TABLE_STRING, |
4089 | | format_cb_session_group |
4090 | | }, |
4091 | | { "session_group_attached", FORMAT_TABLE_STRING, |
4092 | | format_cb_session_group_attached |
4093 | | }, |
4094 | | { "session_group_attached_list", FORMAT_TABLE_STRING, |
4095 | | format_cb_session_group_attached_list |
4096 | | }, |
4097 | | { "session_group_list", FORMAT_TABLE_STRING, |
4098 | | format_cb_session_group_list |
4099 | | }, |
4100 | | { "session_group_many_attached", FORMAT_TABLE_STRING, |
4101 | | format_cb_session_group_many_attached |
4102 | | }, |
4103 | | { "session_group_size", FORMAT_TABLE_STRING, |
4104 | | format_cb_session_group_size |
4105 | | }, |
4106 | | { "session_grouped", FORMAT_TABLE_STRING, |
4107 | | format_cb_session_grouped |
4108 | | }, |
4109 | | { "session_id", FORMAT_TABLE_STRING, |
4110 | | format_cb_session_id |
4111 | | }, |
4112 | | { "session_last_attached", FORMAT_TABLE_TIME, |
4113 | | format_cb_session_last_attached |
4114 | | }, |
4115 | | { "session_many_attached", FORMAT_TABLE_STRING, |
4116 | | format_cb_session_many_attached |
4117 | | }, |
4118 | | { "session_marked", FORMAT_TABLE_STRING, |
4119 | | format_cb_session_marked, |
4120 | | }, |
4121 | | { "session_name", FORMAT_TABLE_STRING, |
4122 | | format_cb_session_name |
4123 | | }, |
4124 | | { "session_path", FORMAT_TABLE_STRING, |
4125 | | format_cb_session_path |
4126 | | }, |
4127 | | { "session_silence_flag", FORMAT_TABLE_STRING, |
4128 | | format_cb_session_silence_flag |
4129 | | }, |
4130 | | { "session_stack", FORMAT_TABLE_STRING, |
4131 | | format_cb_session_stack |
4132 | | }, |
4133 | | { "session_windows", FORMAT_TABLE_STRING, |
4134 | | format_cb_session_windows |
4135 | | }, |
4136 | | { "sixel_support", FORMAT_TABLE_STRING, |
4137 | | format_cb_image_support |
4138 | | }, |
4139 | | { "socket_path", FORMAT_TABLE_STRING, |
4140 | | format_cb_socket_path |
4141 | | }, |
4142 | | { "start_time", FORMAT_TABLE_TIME, |
4143 | | format_cb_start_time |
4144 | | }, |
4145 | | { "synchronized_output_flag", FORMAT_TABLE_STRING, |
4146 | | format_cb_synchronized_output_flag |
4147 | | }, |
4148 | | { "tree_mode_format", FORMAT_TABLE_STRING, |
4149 | | format_cb_tree_mode_format |
4150 | | }, |
4151 | | { "uid", FORMAT_TABLE_STRING, |
4152 | | format_cb_uid |
4153 | | }, |
4154 | | { "user", FORMAT_TABLE_STRING, |
4155 | | format_cb_user |
4156 | | }, |
4157 | | { "version", FORMAT_TABLE_STRING, |
4158 | | format_cb_version |
4159 | | }, |
4160 | | { "window_active", FORMAT_TABLE_STRING, |
4161 | | format_cb_window_active |
4162 | | }, |
4163 | | { "window_active_clients", FORMAT_TABLE_STRING, |
4164 | | format_cb_window_active_clients |
4165 | | }, |
4166 | | { "window_active_clients_list", FORMAT_TABLE_STRING, |
4167 | | format_cb_window_active_clients_list |
4168 | | }, |
4169 | | { "window_active_sessions", FORMAT_TABLE_STRING, |
4170 | | format_cb_window_active_sessions |
4171 | | }, |
4172 | | { "window_active_sessions_list", FORMAT_TABLE_STRING, |
4173 | | format_cb_window_active_sessions_list |
4174 | | }, |
4175 | | { "window_activity", FORMAT_TABLE_TIME, |
4176 | | format_cb_window_activity |
4177 | | }, |
4178 | | { "window_activity_flag", FORMAT_TABLE_STRING, |
4179 | | format_cb_window_activity_flag |
4180 | | }, |
4181 | | { "window_bell_flag", FORMAT_TABLE_STRING, |
4182 | | format_cb_window_bell_flag |
4183 | | }, |
4184 | | { "window_bigger", FORMAT_TABLE_STRING, |
4185 | | format_cb_window_bigger |
4186 | | }, |
4187 | | { "window_cell_height", FORMAT_TABLE_STRING, |
4188 | | format_cb_window_cell_height |
4189 | | }, |
4190 | | { "window_cell_width", FORMAT_TABLE_STRING, |
4191 | | format_cb_window_cell_width |
4192 | | }, |
4193 | | { "window_end_flag", FORMAT_TABLE_STRING, |
4194 | | format_cb_window_end_flag |
4195 | | }, |
4196 | | { "window_flags", FORMAT_TABLE_STRING, |
4197 | | format_cb_window_flags |
4198 | | }, |
4199 | | { "window_format", FORMAT_TABLE_STRING, |
4200 | | format_cb_window_format |
4201 | | }, |
4202 | | { "window_height", FORMAT_TABLE_STRING, |
4203 | | format_cb_window_height |
4204 | | }, |
4205 | | { "window_id", FORMAT_TABLE_STRING, |
4206 | | format_cb_window_id |
4207 | | }, |
4208 | | { "window_index", FORMAT_TABLE_STRING, |
4209 | | format_cb_window_index |
4210 | | }, |
4211 | | { "window_last_flag", FORMAT_TABLE_STRING, |
4212 | | format_cb_window_last_flag |
4213 | | }, |
4214 | | { "window_layout", FORMAT_TABLE_STRING, |
4215 | | format_cb_window_layout |
4216 | | }, |
4217 | | { "window_linked", FORMAT_TABLE_STRING, |
4218 | | format_cb_window_linked |
4219 | | }, |
4220 | | { "window_linked_sessions", FORMAT_TABLE_STRING, |
4221 | | format_cb_window_linked_sessions |
4222 | | }, |
4223 | | { "window_linked_sessions_list", FORMAT_TABLE_STRING, |
4224 | | format_cb_window_linked_sessions_list |
4225 | | }, |
4226 | | { "window_manual_height", FORMAT_TABLE_STRING, |
4227 | | format_cb_window_manual_height |
4228 | | }, |
4229 | | { "window_manual_width", FORMAT_TABLE_STRING, |
4230 | | format_cb_window_manual_width |
4231 | | }, |
4232 | | { "window_marked_flag", FORMAT_TABLE_STRING, |
4233 | | format_cb_window_marked_flag |
4234 | | }, |
4235 | | { "window_modal_pane", FORMAT_TABLE_STRING, |
4236 | | format_cb_window_modal_pane |
4237 | | }, |
4238 | | { "window_name", FORMAT_TABLE_STRING, |
4239 | | format_cb_window_name |
4240 | | }, |
4241 | | { "window_offset_x", FORMAT_TABLE_STRING, |
4242 | | format_cb_window_offset_x |
4243 | | }, |
4244 | | { "window_offset_y", FORMAT_TABLE_STRING, |
4245 | | format_cb_window_offset_y |
4246 | | }, |
4247 | | { "window_panes", FORMAT_TABLE_STRING, |
4248 | | format_cb_window_panes |
4249 | | }, |
4250 | | { "window_raw_flags", FORMAT_TABLE_STRING, |
4251 | | format_cb_window_raw_flags |
4252 | | }, |
4253 | | { "window_silence_flag", FORMAT_TABLE_STRING, |
4254 | | format_cb_window_silence_flag |
4255 | | }, |
4256 | | { "window_stack_index", FORMAT_TABLE_STRING, |
4257 | | format_cb_window_stack_index |
4258 | | }, |
4259 | | { "window_start_flag", FORMAT_TABLE_STRING, |
4260 | | format_cb_window_start_flag |
4261 | | }, |
4262 | | { "window_visible_layout", FORMAT_TABLE_STRING, |
4263 | | format_cb_window_visible_layout |
4264 | | }, |
4265 | | { "window_width", FORMAT_TABLE_STRING, |
4266 | | format_cb_window_width |
4267 | | }, |
4268 | | { "window_zoomed_flag", FORMAT_TABLE_STRING, |
4269 | | format_cb_window_zoomed_flag |
4270 | | }, |
4271 | | { "wrap_flag", FORMAT_TABLE_STRING, |
4272 | | format_cb_wrap_flag |
4273 | | } |
4274 | | }; |
4275 | | |
4276 | | /* Compare format table entries. */ |
4277 | | static int |
4278 | | format_table_compare(const void *key0, const void *entry0) |
4279 | 26.8M | { |
4280 | 26.8M | const char *key = key0; |
4281 | 26.8M | const struct format_table_entry *entry = entry0; |
4282 | | |
4283 | 26.8M | return (strcmp(key, entry->key)); |
4284 | 26.8M | } |
4285 | | |
4286 | | /* Get a format callback. */ |
4287 | | static const struct format_table_entry * |
4288 | | format_table_get(const char *key) |
4289 | 3.54M | { |
4290 | 3.54M | return (bsearch(key, format_table, nitems(format_table), |
4291 | 3.54M | sizeof *format_table, format_table_compare)); |
4292 | 3.54M | } |
4293 | | |
4294 | | /* Merge one format tree into another. */ |
4295 | | void |
4296 | | format_merge(struct format_tree *ft, struct format_tree *from) |
4297 | 0 | { |
4298 | 0 | struct format_entry *fe; |
4299 | |
|
4300 | 0 | RB_FOREACH(fe, format_entry_tree, &from->tree) { |
4301 | 0 | if (fe->value != NULL) |
4302 | 0 | format_add(ft, fe->key, "%s", fe->value); |
4303 | 0 | } |
4304 | 0 | } |
4305 | | |
4306 | | /* Get format pane. */ |
4307 | | struct window_pane * |
4308 | | format_get_pane(struct format_tree *ft) |
4309 | 0 | { |
4310 | 0 | return (ft->wp); |
4311 | 0 | } |
4312 | | |
4313 | | /* Add item bits to tree. */ |
4314 | | static void |
4315 | | format_create_add_item(struct format_tree *ft, struct cmdq_item *item) |
4316 | 0 | { |
4317 | 0 | struct key_event *event = cmdq_get_event(item); |
4318 | 0 | struct mouse_event *m = &event->m; |
4319 | 0 | struct client *c = cmdq_get_client(item); |
4320 | |
|
4321 | 0 | if (c != NULL && (c->flags & CLIENT_READONLY)) |
4322 | 0 | ft->flags |= FORMAT_NOJOBS; |
4323 | 0 | cmdq_merge_formats(item, ft); |
4324 | 0 | memcpy(&ft->m, m, sizeof ft->m); |
4325 | 0 | } |
4326 | | |
4327 | | /* Create a new tree. */ |
4328 | | struct format_tree * |
4329 | | format_create(struct client *c, struct cmdq_item *item, int tag, int flags) |
4330 | 6.68M | { |
4331 | 6.68M | struct format_tree *ft; |
4332 | | |
4333 | 6.68M | ft = xcalloc(1, sizeof *ft); |
4334 | 6.68M | RB_INIT(&ft->tree); |
4335 | | |
4336 | 6.68M | if (c != NULL) { |
4337 | 0 | ft->client = c; |
4338 | 0 | ft->client->references++; |
4339 | 0 | } |
4340 | 6.68M | ft->item = item; |
4341 | | |
4342 | 6.68M | ft->tag = tag; |
4343 | 6.68M | ft->flags = flags; |
4344 | | |
4345 | 6.68M | if (item != NULL) |
4346 | 0 | format_create_add_item(ft, item); |
4347 | | |
4348 | 6.68M | return (ft); |
4349 | 6.68M | } |
4350 | | |
4351 | | /* Free a tree. */ |
4352 | | void |
4353 | | format_free(struct format_tree *ft) |
4354 | 6.68M | { |
4355 | 6.68M | struct format_entry *fe, *fe1; |
4356 | | |
4357 | 68.0M | RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) { |
4358 | 68.0M | RB_REMOVE(format_entry_tree, &ft->tree, fe); |
4359 | 68.0M | free(fe->value); |
4360 | 68.0M | free(fe->key); |
4361 | 68.0M | free(fe); |
4362 | 68.0M | } |
4363 | | |
4364 | 6.68M | if (ft->client != NULL) |
4365 | 0 | server_client_unref(ft->client); |
4366 | 6.68M | free(ft); |
4367 | 6.68M | } |
4368 | | |
4369 | | /* Log each format. */ |
4370 | | static void |
4371 | | format_log_debug_cb(const char *key, const char *value, void *arg) |
4372 | 0 | { |
4373 | 0 | const char *prefix = arg; |
4374 | |
|
4375 | 0 | log_debug("%s: %s=%s", prefix, key, value); |
4376 | 0 | } |
4377 | | |
4378 | | /* Log a format tree. */ |
4379 | | void |
4380 | | format_log_debug(struct format_tree *ft, const char *prefix) |
4381 | 0 | { |
4382 | 0 | if (log_get_level() == 0) |
4383 | 0 | return; |
4384 | 0 | format_each(ft, format_log_debug_cb, (void *)prefix); |
4385 | 0 | } |
4386 | | |
4387 | | /* Walk each format. */ |
4388 | | void |
4389 | | format_each(struct format_tree *ft, void (*cb)(const char *, const char *, |
4390 | | void *), void *arg) |
4391 | 0 | { |
4392 | 0 | const struct format_table_entry *fte; |
4393 | 0 | struct format_entry *fe; |
4394 | 0 | u_int i; |
4395 | 0 | char s[64]; |
4396 | 0 | void *value; |
4397 | 0 | struct timeval *tv; |
4398 | |
|
4399 | 0 | for (i = 0; i < nitems(format_table); i++) { |
4400 | 0 | fte = &format_table[i]; |
4401 | |
|
4402 | 0 | value = fte->cb(ft); |
4403 | 0 | if (value == NULL) |
4404 | 0 | continue; |
4405 | 0 | if (fte->type == FORMAT_TABLE_TIME) { |
4406 | 0 | tv = value; |
4407 | 0 | xsnprintf(s, sizeof s, "%lld", (long long)tv->tv_sec); |
4408 | 0 | cb(fte->key, s, arg); |
4409 | 0 | } else { |
4410 | 0 | cb(fte->key, value, arg); |
4411 | 0 | free(value); |
4412 | 0 | } |
4413 | 0 | } |
4414 | 0 | RB_FOREACH(fe, format_entry_tree, &ft->tree) { |
4415 | 0 | if (fe->time != 0) { |
4416 | 0 | xsnprintf(s, sizeof s, "%lld", (long long)fe->time); |
4417 | 0 | cb(fe->key, s, arg); |
4418 | 0 | } else { |
4419 | 0 | if (fe->value == NULL && fe->cb != NULL) { |
4420 | 0 | fe->value = fe->cb(ft); |
4421 | 0 | if (fe->value == NULL) |
4422 | 0 | fe->value = xstrdup(""); |
4423 | 0 | } |
4424 | 0 | cb(fe->key, fe->value, arg); |
4425 | 0 | } |
4426 | 0 | } |
4427 | 0 | } |
4428 | | |
4429 | | /* Add a key-value pair. */ |
4430 | | void |
4431 | | format_add(struct format_tree *ft, const char *key, const char *fmt, ...) |
4432 | 68.0M | { |
4433 | 68.0M | struct format_entry *fe; |
4434 | 68.0M | struct format_entry *fe_now; |
4435 | 68.0M | va_list ap; |
4436 | | |
4437 | 68.0M | fe = xmalloc(sizeof *fe); |
4438 | 68.0M | fe->key = xstrdup(key); |
4439 | | |
4440 | 68.0M | fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe); |
4441 | 68.0M | if (fe_now != NULL) { |
4442 | 0 | free(fe->key); |
4443 | 0 | free(fe); |
4444 | 0 | free(fe_now->value); |
4445 | 0 | fe = fe_now; |
4446 | 0 | } |
4447 | | |
4448 | 68.0M | fe->cb = NULL; |
4449 | 68.0M | fe->time = 0; |
4450 | | |
4451 | 68.0M | va_start(ap, fmt); |
4452 | 68.0M | xvasprintf(&fe->value, fmt, ap); |
4453 | 68.0M | va_end(ap); |
4454 | 68.0M | } |
4455 | | |
4456 | | /* Add a key and time. */ |
4457 | | void |
4458 | | format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv) |
4459 | 0 | { |
4460 | 0 | struct format_entry *fe, *fe_now; |
4461 | |
|
4462 | 0 | fe = xmalloc(sizeof *fe); |
4463 | 0 | fe->key = xstrdup(key); |
4464 | |
|
4465 | 0 | fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe); |
4466 | 0 | if (fe_now != NULL) { |
4467 | 0 | free(fe->key); |
4468 | 0 | free(fe); |
4469 | 0 | free(fe_now->value); |
4470 | 0 | fe = fe_now; |
4471 | 0 | } |
4472 | |
|
4473 | 0 | fe->cb = NULL; |
4474 | 0 | fe->time = tv->tv_sec; |
4475 | |
|
4476 | 0 | fe->value = NULL; |
4477 | 0 | } |
4478 | | |
4479 | | /* Add a key and function. */ |
4480 | | void |
4481 | | format_add_cb(struct format_tree *ft, const char *key, format_cb cb) |
4482 | 0 | { |
4483 | 0 | struct format_entry *fe; |
4484 | 0 | struct format_entry *fe_now; |
4485 | |
|
4486 | 0 | fe = xmalloc(sizeof *fe); |
4487 | 0 | fe->key = xstrdup(key); |
4488 | |
|
4489 | 0 | fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe); |
4490 | 0 | if (fe_now != NULL) { |
4491 | 0 | free(fe->key); |
4492 | 0 | free(fe); |
4493 | 0 | free(fe_now->value); |
4494 | 0 | fe = fe_now; |
4495 | 0 | } |
4496 | |
|
4497 | 0 | fe->cb = cb; |
4498 | 0 | fe->time = 0; |
4499 | |
|
4500 | 0 | fe->value = NULL; |
4501 | 0 | } |
4502 | | |
4503 | | /* Quote shell special characters in string. */ |
4504 | | static char * |
4505 | | format_quote_shell(const char *s) |
4506 | 959 | { |
4507 | 959 | const char *cp; |
4508 | 959 | char *out, *at; |
4509 | | |
4510 | 959 | at = out = xmalloc(strlen(s) * 2 + 1); |
4511 | 7.42k | for (cp = s; *cp != '\0'; cp++) { |
4512 | 6.46k | if (strchr("|&;<>(){}$`\\\"'*?[# =%\n\t", *cp) != NULL) |
4513 | 1.47k | *at++ = '\\'; |
4514 | 6.46k | *at++ = *cp; |
4515 | 6.46k | } |
4516 | 959 | *at = '\0'; |
4517 | 959 | return (out); |
4518 | 959 | } |
4519 | | |
4520 | | /* Quote string with POSIX shell single quotes. */ |
4521 | | static char * |
4522 | | format_quote_shell_single(const char *s) |
4523 | 1.53k | { |
4524 | 1.53k | const char *cp; |
4525 | 1.53k | char *out, *at; |
4526 | | |
4527 | 1.53k | at = out = xmalloc(strlen(s) * 4 + 3); |
4528 | 1.53k | *at++ = '\''; |
4529 | 13.3k | for (cp = s; *cp != '\0'; cp++) { |
4530 | 11.8k | if (*cp == '\'') { |
4531 | 395 | *at++ = '\''; |
4532 | 395 | *at++ = '\\'; |
4533 | 395 | *at++ = '\''; |
4534 | 395 | *at++ = '\''; |
4535 | 395 | } else |
4536 | 11.4k | *at++ = *cp; |
4537 | 11.8k | } |
4538 | 1.53k | *at++ = '\''; |
4539 | 1.53k | *at = '\0'; |
4540 | 1.53k | return (out); |
4541 | 1.53k | } |
4542 | | |
4543 | | /* Quote #s in string. */ |
4544 | | static char * |
4545 | | format_quote_style(const char *s) |
4546 | 2.18k | { |
4547 | 2.18k | const char *cp; |
4548 | 2.18k | char *out, *at; |
4549 | | |
4550 | 2.18k | at = out = xmalloc(strlen(s) * 2 + 1); |
4551 | 2.63M | for (cp = s; *cp != '\0'; cp++) { |
4552 | 2.63M | if (*cp == '#') |
4553 | 142k | *at++ = '#'; |
4554 | 2.63M | *at++ = *cp; |
4555 | 2.63M | } |
4556 | 2.18k | *at = '\0'; |
4557 | 2.18k | return (out); |
4558 | 2.18k | } |
4559 | | |
4560 | | /* Make a prettier time. */ |
4561 | | char * |
4562 | | format_pretty_time(time_t t, int seconds) |
4563 | 2.54k | { |
4564 | 2.54k | struct tm now_tm, tm; |
4565 | 2.54k | time_t now, age; |
4566 | 2.54k | char s[9]; |
4567 | | |
4568 | 2.54k | time(&now); |
4569 | 2.54k | if (now < t) |
4570 | 0 | now = t; |
4571 | 2.54k | age = now - t; |
4572 | | |
4573 | 2.54k | if (localtime_r(&now, &now_tm) == NULL || localtime_r(&t, &tm) == NULL) |
4574 | 0 | return (xstrdup("")); |
4575 | | |
4576 | | /* Last 24 hours. */ |
4577 | 2.54k | if (age < 24 * 3600) { |
4578 | 0 | if (seconds) |
4579 | 0 | strftime(s, sizeof s, "%H:%M:%S", &tm); |
4580 | 0 | else |
4581 | 0 | strftime(s, sizeof s, "%H:%M", &tm); |
4582 | 0 | return (xstrdup(s)); |
4583 | 0 | } |
4584 | | |
4585 | | /* This month or last 28 days. */ |
4586 | 2.54k | if ((tm.tm_year == now_tm.tm_year && tm.tm_mon == now_tm.tm_mon) || |
4587 | 2.54k | age < 28 * 24 * 3600) { |
4588 | 0 | strftime(s, sizeof s, "%a%d", &tm); |
4589 | 0 | return (xstrdup(s)); |
4590 | 0 | } |
4591 | | |
4592 | | /* Last 12 months. */ |
4593 | 2.54k | if ((tm.tm_year == now_tm.tm_year && tm.tm_mon < now_tm.tm_mon) || |
4594 | 2.54k | (tm.tm_year == now_tm.tm_year - 1 && tm.tm_mon > now_tm.tm_mon)) { |
4595 | 0 | strftime(s, sizeof s, "%d%b", &tm); |
4596 | 0 | return (xstrdup(s)); |
4597 | 0 | } |
4598 | | |
4599 | | /* Older than that. */ |
4600 | 2.54k | strftime(s, sizeof s, "%h%y", &tm); |
4601 | 2.54k | return (xstrdup(s)); |
4602 | 2.54k | } |
4603 | | |
4604 | | /* Make a relative time. */ |
4605 | | static char * |
4606 | | format_relative_time(time_t t) |
4607 | 685 | { |
4608 | 685 | time_t now, age; |
4609 | 685 | u_int d, h, m, s; |
4610 | 685 | char out[32]; |
4611 | | |
4612 | 685 | time(&now); |
4613 | 685 | if (t > now) |
4614 | 0 | return (NULL); |
4615 | 685 | if (t == now) |
4616 | 0 | return (xstrdup("0s")); |
4617 | 685 | age = now - t; |
4618 | | |
4619 | 685 | d = age / 86400; |
4620 | 685 | h = (age % 86400) / 3600; |
4621 | 685 | m = (age % 3600) / 60; |
4622 | 685 | s = age % 60; |
4623 | | |
4624 | 685 | if (d != 0) { |
4625 | 685 | if (h != 0) |
4626 | 685 | xsnprintf(out, sizeof out, "%ud%uh", d, h); |
4627 | 0 | else |
4628 | 0 | xsnprintf(out, sizeof out, "%ud", d); |
4629 | 685 | } else if (h != 0) { |
4630 | 0 | if (m != 0) |
4631 | 0 | xsnprintf(out, sizeof out, "%uh%um", h, m); |
4632 | 0 | else |
4633 | 0 | xsnprintf(out, sizeof out, "%uh", h); |
4634 | 0 | } else if (m != 0) { |
4635 | 0 | if (s != 0) |
4636 | 0 | xsnprintf(out, sizeof out, "%um%us", m, s); |
4637 | 0 | else |
4638 | 0 | xsnprintf(out, sizeof out, "%um", m); |
4639 | 0 | } else |
4640 | 0 | xsnprintf(out, sizeof out, "%us", s); |
4641 | 685 | return (xstrdup(out)); |
4642 | 685 | } |
4643 | | |
4644 | | /* Make a time difference in seconds. */ |
4645 | | static char * |
4646 | | format_time_difference(time_t t) |
4647 | 437 | { |
4648 | 437 | time_t now = time(NULL); |
4649 | 437 | char *out; |
4650 | | |
4651 | 437 | xasprintf(&out, "%ld", (long)now - (long)t); |
4652 | 437 | return (out); |
4653 | 437 | } |
4654 | | |
4655 | | /* Find a format entry. */ |
4656 | | static char * |
4657 | | format_find(struct format_tree *ft, const char *key, uint64_t modifiers, |
4658 | | const char *time_format) |
4659 | 3.57M | { |
4660 | 3.57M | const struct format_table_entry *fte; |
4661 | 3.57M | void *value; |
4662 | 3.57M | struct format_entry *fe, fe_find; |
4663 | 3.57M | struct environ_entry *envent; |
4664 | 3.57M | struct options_entry *o; |
4665 | 3.57M | char *found = NULL, *saved, s[512]; |
4666 | 3.57M | char *array_key = NULL; |
4667 | 3.57M | const char *errstr; |
4668 | 3.57M | time_t t = 0; |
4669 | 3.57M | struct tm tm; |
4670 | | |
4671 | 3.57M | o = options_parse_get(global_options, key, &array_key, 0); |
4672 | 3.57M | if (o == NULL && ft->wp != NULL) |
4673 | 0 | o = options_parse_get(ft->wp->options, key, &array_key, 0); |
4674 | 3.57M | if (o == NULL && ft->w != NULL) |
4675 | 0 | o = options_parse_get(ft->w->options, key, &array_key, 0); |
4676 | 3.57M | if (o == NULL) |
4677 | 3.56M | o = options_parse_get(global_w_options, key, &array_key, 0); |
4678 | 3.57M | if (o == NULL && ft->s != NULL) |
4679 | 0 | o = options_parse_get(ft->s->options, key, &array_key, 0); |
4680 | 3.57M | if (o == NULL) |
4681 | 3.56M | o = options_parse_get(global_s_options, key, &array_key, 0); |
4682 | 3.57M | if (o != NULL) { |
4683 | 28.4k | found = options_to_string(o, array_key, 1); |
4684 | 28.4k | free(array_key); |
4685 | 28.4k | goto found; |
4686 | 28.4k | } |
4687 | | |
4688 | 3.54M | fte = format_table_get(key); |
4689 | 3.54M | if (fte != NULL) { |
4690 | 1.76M | value = fte->cb(ft); |
4691 | 1.76M | if (fte->type == FORMAT_TABLE_TIME && value != NULL) |
4692 | 485 | t = ((struct timeval *)value)->tv_sec; |
4693 | 1.76M | else |
4694 | 1.76M | found = value; |
4695 | 1.76M | goto found; |
4696 | 1.76M | } |
4697 | 1.77M | fe_find.key = (char *)key; |
4698 | 1.77M | fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find); |
4699 | 1.77M | if (fe != NULL) { |
4700 | 394k | if (fe->time != 0) { |
4701 | 0 | t = fe->time; |
4702 | 0 | goto found; |
4703 | 0 | } |
4704 | 394k | if (fe->value == NULL && fe->cb != NULL) { |
4705 | 0 | fe->value = fe->cb(ft); |
4706 | 0 | if (fe->value == NULL) |
4707 | 0 | fe->value = xstrdup(""); |
4708 | 0 | } |
4709 | 394k | found = xstrdup(fe->value); |
4710 | 394k | goto found; |
4711 | 394k | } |
4712 | | |
4713 | 1.37M | if (~modifiers & FORMAT_TIMESTRING) { |
4714 | 1.30M | envent = NULL; |
4715 | 1.30M | if (ft->s != NULL) |
4716 | 0 | envent = environ_find(ft->s->environ, key); |
4717 | 1.30M | if (envent == NULL) |
4718 | 1.30M | envent = environ_find(global_environ, key); |
4719 | 1.30M | if (envent != NULL && envent->value != NULL) { |
4720 | 2.30k | found = xstrdup(envent->value); |
4721 | 2.30k | goto found; |
4722 | 2.30k | } |
4723 | 1.30M | } |
4724 | | |
4725 | 1.37M | return (NULL); |
4726 | | |
4727 | 2.19M | found: |
4728 | 2.19M | if (modifiers & FORMAT_TIMESTRING) { |
4729 | 6.91k | if (t == 0 && found != NULL) { |
4730 | 5.22k | t = strtonum(found, 0, INT64_MAX, &errstr); |
4731 | 5.22k | if (errstr != NULL) |
4732 | 440 | t = 0; |
4733 | 5.22k | free(found); |
4734 | 5.22k | } |
4735 | 6.91k | if (t == 0) |
4736 | 2.25k | return (NULL); |
4737 | 4.65k | if (modifiers & FORMAT_RELATIVE) |
4738 | 685 | found = format_relative_time(t); |
4739 | 3.96k | else if (modifiers & FORMAT_DIFFERENCE) |
4740 | 437 | found = format_time_difference(t); |
4741 | 3.53k | else if (modifiers & FORMAT_PRETTY) |
4742 | 2.54k | found = format_pretty_time(t, 0); |
4743 | 984 | else { |
4744 | 984 | if (time_format != NULL) { |
4745 | 486 | if (localtime_r(&t, &tm) == NULL) |
4746 | 0 | return (NULL); |
4747 | 486 | format_strftime(s, sizeof s, time_format, &tm); |
4748 | 498 | } else { |
4749 | 498 | if (ctime_r(&t, s) == NULL) |
4750 | 0 | return (NULL); |
4751 | 498 | s[strcspn(s, "\n")] = '\0'; |
4752 | 498 | } |
4753 | 984 | found = xstrdup(s); |
4754 | 984 | } |
4755 | 4.65k | return (found); |
4756 | 4.65k | } |
4757 | | |
4758 | 2.18M | if (t != 0) |
4759 | 0 | xasprintf(&found, "%lld", (long long)t); |
4760 | 2.18M | else if (found == NULL) |
4761 | 1.47M | return (NULL); |
4762 | 713k | if (modifiers & FORMAT_BASENAME) { |
4763 | 500 | saved = found; |
4764 | 500 | found = xstrdup(basename(saved)); |
4765 | 500 | free(saved); |
4766 | 500 | } |
4767 | 713k | if (modifiers & FORMAT_DIRNAME) { |
4768 | 534 | saved = found; |
4769 | 534 | found = xstrdup(dirname(saved)); |
4770 | 534 | free(saved); |
4771 | 534 | } |
4772 | 713k | if (modifiers & FORMAT_QUOTE_SHELL) { |
4773 | 959 | saved = found; |
4774 | 959 | found = format_quote_shell(saved); |
4775 | 959 | free(saved); |
4776 | 959 | } |
4777 | 713k | if (modifiers & FORMAT_QUOTE_SHELL_SQ) { |
4778 | 1.53k | saved = found; |
4779 | 1.53k | found = format_quote_shell_single(saved); |
4780 | 1.53k | free(saved); |
4781 | 1.53k | } |
4782 | 713k | if (modifiers & FORMAT_QUOTE_STYLE) { |
4783 | 2.18k | saved = found; |
4784 | 2.18k | found = format_quote_style(saved); |
4785 | 2.18k | free(saved); |
4786 | 2.18k | } |
4787 | 713k | if (modifiers & FORMAT_QUOTE_ARGUMENTS) { |
4788 | 8.68k | saved = found; |
4789 | 8.68k | found = args_escape(saved); |
4790 | 8.68k | free(saved); |
4791 | 8.68k | } |
4792 | 713k | return (found); |
4793 | 2.18M | } |
4794 | | |
4795 | | /* Check if format has not taken too long. */ |
4796 | | static int |
4797 | | format_check_time(struct format_expand_state *es, u_int *check) |
4798 | 994M | { |
4799 | 994M | uint64_t t; |
4800 | | |
4801 | 994M | if (check != NULL && ++*check % FORMAT_TIME_LOOP_CHECK != 0) |
4802 | 971M | return (1); |
4803 | | |
4804 | 23.2M | t = get_timer(); |
4805 | 23.2M | if (t - es->start_time < FORMAT_TIME_LIMIT) |
4806 | 20.6M | return (1); |
4807 | 2.60M | t -= es->start_time; |
4808 | | |
4809 | 2.60M | format_log(es, "reached time limit (%llu)", (unsigned long long)t); |
4810 | 2.60M | return (0); |
4811 | 23.2M | } |
4812 | | |
4813 | | /* Unescape escaped characters. */ |
4814 | | static char * |
4815 | | format_unescape(struct format_expand_state *es, const char *s, size_t n) |
4816 | 1.82M | { |
4817 | 1.82M | const char *end = s + n; |
4818 | 1.82M | char *out, *cp; |
4819 | 1.82M | int brackets = 0; |
4820 | 1.82M | u_int check = 0; |
4821 | | |
4822 | 1.82M | cp = out = xmalloc(n + 1); |
4823 | 36.6M | for (; s != end; s++) { |
4824 | 34.8M | if (!format_check_time(es, &check)) { |
4825 | 0 | free(out); |
4826 | 0 | return (xstrdup("")); |
4827 | 0 | } |
4828 | 34.8M | if (*s == '#' && s + 1 != end && s[1] == '{') |
4829 | 753k | brackets++; |
4830 | 34.8M | if (brackets == 0 && |
4831 | 17.9M | *s == '#' && |
4832 | 434k | s + 1 != end && |
4833 | 422k | strchr(",#{}:", s[1]) != NULL) { |
4834 | 48.7k | *cp++ = *++s; |
4835 | 48.7k | continue; |
4836 | 48.7k | } |
4837 | 34.8M | if (*s == '}') |
4838 | 767k | brackets--; |
4839 | 34.8M | *cp++ = *s; |
4840 | 34.8M | } |
4841 | 1.82M | *cp = '\0'; |
4842 | 1.82M | return (out); |
4843 | 1.82M | } |
4844 | | |
4845 | | /* Remove escaped characters. */ |
4846 | | static char * |
4847 | | format_strip(struct format_expand_state *es, const char *s) |
4848 | 3.09k | { |
4849 | 3.09k | char *out, *cp; |
4850 | 3.09k | int brackets = 0; |
4851 | 3.09k | u_int check = 0; |
4852 | | |
4853 | 3.09k | cp = out = xmalloc(strlen(s) + 1); |
4854 | 72.8M | for (; *s != '\0'; s++) { |
4855 | 72.8M | if (!format_check_time(es, &check)) { |
4856 | 1 | free(out); |
4857 | 1 | return (xstrdup("")); |
4858 | 1 | } |
4859 | 72.8M | if (*s == '#' && s[1] == '{') |
4860 | 19.3k | brackets++; |
4861 | 72.8M | if (*s == '#' && strchr(",#{}:", s[1]) != NULL) { |
4862 | 1.67M | if (brackets != 0) |
4863 | 53.6k | *cp++ = *s; |
4864 | 1.67M | continue; |
4865 | 1.67M | } |
4866 | 71.1M | if (*s == '}') |
4867 | 1.14M | brackets--; |
4868 | 71.1M | *cp++ = *s; |
4869 | 71.1M | } |
4870 | 3.09k | *cp = '\0'; |
4871 | 3.09k | return (out); |
4872 | 3.09k | } |
4873 | | |
4874 | | /* Skip until end. */ |
4875 | | static const char * |
4876 | | format_skip1(struct format_expand_state *es, const char *s, const char *end) |
4877 | 14.5M | { |
4878 | 14.5M | int brackets = 0; |
4879 | 14.5M | u_int check = 0; |
4880 | | |
4881 | 14.5M | if (es != NULL && !format_check_time(es, NULL)) |
4882 | 2.22M | return (NULL); |
4883 | | |
4884 | 1.60G | for (; *s != '\0'; s++) { |
4885 | 1.60G | if (es != NULL && !format_check_time(es, &check)) |
4886 | 26 | return (NULL); |
4887 | 1.60G | if (*s == '#' && s[1] == '{') |
4888 | 54.4M | brackets++; |
4889 | 1.60G | if (*s == '#' && |
4890 | 258M | s[1] != '\0' && |
4891 | 258M | strchr(",#{}:", s[1]) != NULL) { |
4892 | 142M | s++; |
4893 | 142M | continue; |
4894 | 142M | } |
4895 | 1.45G | if (*s == '}') |
4896 | 183M | brackets--; |
4897 | 1.45G | if (strchr(end, *s) != NULL && brackets == 0) |
4898 | 10.3M | break; |
4899 | 1.45G | } |
4900 | 12.3M | if (*s == '\0') |
4901 | 2.01M | return (NULL); |
4902 | 10.3M | return (s); |
4903 | 12.3M | } |
4904 | | |
4905 | | /* Skip until end. */ |
4906 | | const char * |
4907 | | format_skip(const char *s, const char *end) |
4908 | 1.75M | { |
4909 | 1.75M | return (format_skip1(NULL, s, end)); |
4910 | 1.75M | } |
4911 | | |
4912 | | /* Return left and right alternatives separated by commas. */ |
4913 | | static int |
4914 | | format_choose(struct format_expand_state *es, const char *s, char **left, |
4915 | | char **right, int expand) |
4916 | 895k | { |
4917 | 895k | const char *cp; |
4918 | 895k | char *left0, *right0; |
4919 | | |
4920 | 895k | cp = format_skip1(es, s, ","); |
4921 | 895k | if (cp == NULL) |
4922 | 129k | return (-1); |
4923 | 766k | left0 = xstrndup(s, cp - s); |
4924 | 766k | right0 = xstrdup(cp + 1); |
4925 | | |
4926 | 766k | if (expand) { |
4927 | 765k | *left = format_expand1(es, left0); |
4928 | 765k | free(left0); |
4929 | 765k | *right = format_expand1(es, right0); |
4930 | 765k | free(right0); |
4931 | 765k | } else { |
4932 | 849 | *left = left0; |
4933 | 849 | *right = right0; |
4934 | 849 | } |
4935 | 766k | return (0); |
4936 | 895k | } |
4937 | | |
4938 | | /* Is this true? */ |
4939 | | int |
4940 | | format_true(const char *s) |
4941 | 210k | { |
4942 | 210k | if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0')) |
4943 | 111k | return (1); |
4944 | 98.8k | return (0); |
4945 | 210k | } |
4946 | | |
4947 | | /* Check if modifier end. */ |
4948 | | static int |
4949 | | format_is_end(char c) |
4950 | 7.55M | { |
4951 | 7.55M | return (c == ';' || c == ':'); |
4952 | 7.55M | } |
4953 | | |
4954 | | /* Add to modifier list. */ |
4955 | | static void |
4956 | | format_add_modifier(struct format_modifier **list, u_int *count, |
4957 | | const char *c, size_t n, char **argv, int argc) |
4958 | 2.70M | { |
4959 | 2.70M | struct format_modifier *fm; |
4960 | | |
4961 | 2.70M | *list = xreallocarray(*list, (*count) + 1, sizeof **list); |
4962 | 2.70M | fm = &(*list)[(*count)++]; |
4963 | | |
4964 | 2.70M | memcpy(fm->modifier, c, n); |
4965 | 2.70M | fm->modifier[n] = '\0'; |
4966 | 2.70M | fm->size = n; |
4967 | | |
4968 | 2.70M | fm->argv = argv; |
4969 | 2.70M | fm->argc = argc; |
4970 | 2.70M | } |
4971 | | |
4972 | | /* Free modifier list. */ |
4973 | | static void |
4974 | | format_free_modifiers(struct format_modifier *list, u_int count) |
4975 | 10.2M | { |
4976 | 10.2M | u_int i; |
4977 | | |
4978 | 12.9M | for (i = 0; i < count; i++) |
4979 | 2.70M | cmd_free_argv(list[i].argc, list[i].argv); |
4980 | 10.2M | free(list); |
4981 | 10.2M | } |
4982 | | |
4983 | | /* Build modifier list. */ |
4984 | | static struct format_modifier * |
4985 | | format_build_modifiers(struct format_expand_state *es, const char **s, |
4986 | | u_int *count) |
4987 | 6.21M | { |
4988 | 6.21M | const char *cp = *s, *end; |
4989 | 6.21M | struct format_modifier *list = NULL; |
4990 | 6.21M | char c, last[] = "X;:", **argv, *value; |
4991 | 6.21M | int argc; |
4992 | | |
4993 | | /* |
4994 | | * Modifiers are a ; separated list of the forms: |
4995 | | * l,m,C,a,b,c,d,I,n,t,w,q,E,T,S,W,P,O,V,R,A,<,> |
4996 | | * =a |
4997 | | * =/a |
4998 | | * =/a/ |
4999 | | * s/a/b/ |
5000 | | * s/a/b |
5001 | | * ||,&&,!=,==,<=,>= |
5002 | | */ |
5003 | | |
5004 | 6.21M | *count = 0; |
5005 | | |
5006 | 8.91M | while (*cp != '\0' && *cp != ':') { |
5007 | | /* Skip any separator character. */ |
5008 | 6.54M | if (*cp == ';') |
5009 | 363k | cp++; |
5010 | 6.54M | if (*cp == '\0') |
5011 | 201k | break; |
5012 | | |
5013 | | /* Check single character modifiers with no arguments. */ |
5014 | 6.34M | if (strchr("labdnwETSWPOVL!<>A", cp[0]) != NULL && |
5015 | 1.72M | format_is_end(cp[1])) { |
5016 | 190k | format_add_modifier(&list, count, cp, 1, NULL, 0); |
5017 | 190k | cp++; |
5018 | 190k | continue; |
5019 | 190k | } |
5020 | | |
5021 | | /* Then try double character with no arguments. */ |
5022 | 6.15M | if ((memcmp("||", cp, 2) == 0 || |
5023 | 6.12M | memcmp("&&", cp, 2) == 0 || |
5024 | 6.12M | memcmp("!!", cp, 2) == 0 || |
5025 | 6.12M | memcmp("!=", cp, 2) == 0 || |
5026 | 6.08M | memcmp("==", cp, 2) == 0 || |
5027 | 6.04M | memcmp("<=", cp, 2) == 0 || |
5028 | 6.03M | memcmp(">=", cp, 2) == 0) && |
5029 | 120k | format_is_end(cp[2])) { |
5030 | 65.0k | format_add_modifier(&list, count, cp, 2, NULL, 0); |
5031 | 65.0k | cp += 2; |
5032 | 65.0k | continue; |
5033 | 65.0k | } |
5034 | | |
5035 | | /* Now try single character with arguments. */ |
5036 | 6.09M | if (strchr("ImCLNPSOVst=pReqWcA", cp[0]) == NULL) |
5037 | 1.83M | break; |
5038 | 4.25M | c = cp[0]; |
5039 | | |
5040 | | /* No arguments provided. */ |
5041 | 4.25M | if (format_is_end(cp[1])) { |
5042 | 487k | format_add_modifier(&list, count, cp, 1, NULL, 0); |
5043 | 487k | cp++; |
5044 | 487k | continue; |
5045 | 487k | } |
5046 | 3.76M | argv = NULL; |
5047 | 3.76M | argc = 0; |
5048 | | |
5049 | | /* Single argument with no wrapper character. */ |
5050 | 3.76M | if (!ispunct((u_char)cp[1]) || cp[1] == '-') { |
5051 | 3.05M | end = format_skip1(es, cp + 1, ":;"); |
5052 | 3.05M | if (end == NULL) |
5053 | 1.80M | break; |
5054 | | |
5055 | 1.25M | argv = xcalloc(1, sizeof *argv); |
5056 | 1.25M | value = format_unescape(es, cp + 1, end - (cp + 1)); |
5057 | 1.25M | argv[0] = format_expand1(es, value); |
5058 | 1.25M | free(value); |
5059 | 1.25M | argc = 1; |
5060 | | |
5061 | 1.25M | format_add_modifier(&list, count, &c, 1, argv, argc); |
5062 | 1.25M | cp = end; |
5063 | 1.25M | continue; |
5064 | 3.05M | } |
5065 | | |
5066 | | /* Multiple arguments with a wrapper character. */ |
5067 | 711k | last[0] = cp[1]; |
5068 | 711k | cp++; |
5069 | 877k | do { |
5070 | 877k | if (cp[0] == last[0] && format_is_end(cp[1])) { |
5071 | 85.3k | cp++; |
5072 | 85.3k | break; |
5073 | 85.3k | } |
5074 | 792k | end = format_skip1(es, cp + 1, last); |
5075 | 792k | if (end == NULL) |
5076 | 219k | break; |
5077 | 573k | cp++; |
5078 | | |
5079 | 573k | argv = xreallocarray(argv, argc + 1, sizeof *argv); |
5080 | 573k | value = format_unescape(es, cp, end - cp); |
5081 | 573k | argv[argc++] = format_expand1(es, value); |
5082 | 573k | free(value); |
5083 | | |
5084 | 573k | cp = end; |
5085 | 573k | } while (!format_is_end(cp[0])); |
5086 | 711k | format_add_modifier(&list, count, &c, 1, argv, argc); |
5087 | 711k | } |
5088 | 6.21M | if (*cp != ':') { |
5089 | 4.04M | format_free_modifiers(list, *count); |
5090 | 4.04M | *count = 0; |
5091 | 4.04M | return (NULL); |
5092 | 4.04M | } |
5093 | 2.16M | *s = cp + 1; |
5094 | 2.16M | return (list); |
5095 | 6.21M | } |
5096 | | |
5097 | | /* Match using the fuzzy matcher. */ |
5098 | | static char * |
5099 | | format_match_fuzzy(const char *pattern, const char *text, int positions) |
5100 | 159k | { |
5101 | 159k | struct evbuffer *buffer; |
5102 | 159k | bitstr_t *bs; |
5103 | 159k | char *value; |
5104 | 159k | size_t size; |
5105 | 159k | u_int i, width = 1; |
5106 | | |
5107 | 159k | if (positions) { |
5108 | 123k | width = format_width(text); |
5109 | 123k | if (width == 0) |
5110 | 16.6k | width = 1; |
5111 | 123k | } |
5112 | 159k | bs = fuzzy_match(pattern, text, width, NULL); |
5113 | 159k | if (bs == NULL) |
5114 | 120k | return (xstrdup(positions ? "" : "0")); |
5115 | | |
5116 | 39.5k | if (!positions) { |
5117 | 10.4k | free(bs); |
5118 | 10.4k | return (xstrdup("1")); |
5119 | 10.4k | } |
5120 | | |
5121 | 29.0k | buffer = evbuffer_new(); |
5122 | 29.0k | if (buffer == NULL) |
5123 | 0 | fatalx("out of memory"); |
5124 | 85.7M | for (i = 0; i < width; i++) { |
5125 | 85.6M | if (!bit_test(bs, i)) |
5126 | 84.8M | continue; |
5127 | 845k | if (EVBUFFER_LENGTH(buffer) != 0) |
5128 | 839k | evbuffer_add(buffer, ",", 1); |
5129 | 845k | evbuffer_add_printf(buffer, "%u", i); |
5130 | 845k | } |
5131 | 29.0k | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
5132 | 6.58k | value = xmemdup(EVBUFFER_DATA(buffer), size); |
5133 | 22.5k | else |
5134 | 22.5k | value = xstrdup(""); |
5135 | 29.0k | evbuffer_free(buffer); |
5136 | 29.0k | free(bs); |
5137 | 29.0k | return (value); |
5138 | 29.0k | } |
5139 | | |
5140 | | /* Match against an fnmatch(3) pattern or regular expression. */ |
5141 | | static char * |
5142 | | format_match(struct format_modifier *fm, const char *pattern, const char *text) |
5143 | 162k | { |
5144 | 162k | const char *s = ""; |
5145 | 162k | regex_t r; |
5146 | 162k | int flags = 0; |
5147 | | |
5148 | 162k | if (fm->argc >= 1) |
5149 | 161k | s = fm->argv[0]; |
5150 | 162k | if (strchr(s, 'p') != NULL) |
5151 | 123k | return (format_match_fuzzy(pattern, text, 1)); |
5152 | 38.8k | if (strchr(s, 'z') != NULL) |
5153 | 36.4k | return (format_match_fuzzy(pattern, text, 0)); |
5154 | 2.48k | if (strchr(s, 'r') == NULL) { |
5155 | 1.92k | if (strchr(s, 'i') != NULL) |
5156 | 438 | flags |= FNM_CASEFOLD; |
5157 | 1.92k | if (fnmatch(pattern, text, flags) != 0) |
5158 | 1.38k | return (xstrdup("0")); |
5159 | 1.92k | } else { |
5160 | 552 | flags = REG_EXTENDED|REG_NOSUB; |
5161 | 552 | if (strchr(s, 'i') != NULL) |
5162 | 111 | flags |= REG_ICASE; |
5163 | 552 | if (regcomp(&r, pattern, flags) != 0) |
5164 | 0 | return (xstrdup("0")); |
5165 | 552 | if (regexec(&r, text, 0, NULL, 0) != 0) { |
5166 | 552 | regfree(&r); |
5167 | 552 | return (xstrdup("0")); |
5168 | 552 | } |
5169 | 0 | regfree(&r); |
5170 | 0 | } |
5171 | 548 | return (xstrdup("1")); |
5172 | 2.48k | } |
5173 | | |
5174 | | /* Perform substitution in string. */ |
5175 | | static char * |
5176 | | format_sub(struct format_modifier *fm, const char *text, const char *pattern, |
5177 | | const char *with) |
5178 | 16.9k | { |
5179 | 16.9k | char *value; |
5180 | 16.9k | int flags = REG_EXTENDED; |
5181 | | |
5182 | 16.9k | if (fm->argc >= 3 && strchr(fm->argv[2], 'i') != NULL) |
5183 | 441 | flags |= REG_ICASE; |
5184 | 16.9k | value = regsub(pattern, with, text, flags); |
5185 | 16.9k | if (value == NULL) |
5186 | 0 | return (xstrdup(text)); |
5187 | 16.9k | return (value); |
5188 | 16.9k | } |
5189 | | |
5190 | | /* Search inside pane. */ |
5191 | | static char * |
5192 | | format_search(struct format_modifier *fm, struct window_pane *wp, const char *s) |
5193 | 0 | { |
5194 | 0 | int ignore = 0, regex = 0; |
5195 | 0 | char *value; |
5196 | |
|
5197 | 0 | if (fm->argc >= 1) { |
5198 | 0 | if (strchr(fm->argv[0], 'i') != NULL) |
5199 | 0 | ignore = 1; |
5200 | 0 | if (strchr(fm->argv[0], 'r') != NULL) |
5201 | 0 | regex = 1; |
5202 | 0 | } |
5203 | 0 | xasprintf(&value, "%u", window_pane_search(wp, s, regex, ignore)); |
5204 | 0 | return (value); |
5205 | 0 | } |
5206 | | |
5207 | | /* Handle unary boolean operators, "!" and "!!". */ |
5208 | | static char * |
5209 | | format_bool_op_1(struct format_expand_state *es, const char *fmt, int not) |
5210 | 54.4k | { |
5211 | 54.4k | int result; |
5212 | 54.4k | char *expanded; |
5213 | | |
5214 | 54.4k | expanded = format_expand1(es, fmt); |
5215 | 54.4k | result = format_true(expanded); |
5216 | 54.4k | if (not) |
5217 | 54.0k | result = !result; |
5218 | 54.4k | free(expanded); |
5219 | | |
5220 | 54.4k | return (xstrdup(result ? "1" : "0")); |
5221 | 54.4k | } |
5222 | | |
5223 | | /* Handle n-ary boolean operators, "&&" and "||". */ |
5224 | | static char * |
5225 | | format_bool_op_n(struct format_expand_state *es, const char *fmt, int and) |
5226 | 30.7k | { |
5227 | 30.7k | int result; |
5228 | 30.7k | const char *cp1, *cp2; |
5229 | 30.7k | char *raw, *expanded; |
5230 | | |
5231 | 30.7k | result = and ? 1 : 0; |
5232 | 30.7k | cp1 = fmt; |
5233 | | |
5234 | 35.2k | while (and ? result : !result) { |
5235 | 32.2k | cp2 = format_skip1(es, cp1, ","); |
5236 | | |
5237 | 32.2k | if (cp2 == NULL) |
5238 | 27.6k | raw = xstrdup(cp1); |
5239 | 4.53k | else |
5240 | 4.53k | raw = xstrndup(cp1, cp2 - cp1); |
5241 | 32.2k | expanded = format_expand1(es, raw); |
5242 | 32.2k | free(raw); |
5243 | 32.2k | format_log(es, "operator %s has operand: %s", |
5244 | 32.2k | and ? "&&" : "||", expanded); |
5245 | | |
5246 | 32.2k | if (and) |
5247 | 4.83k | result = result && format_true(expanded); |
5248 | 27.3k | else |
5249 | 27.3k | result = result || format_true(expanded); |
5250 | 32.2k | free(expanded); |
5251 | | |
5252 | 32.2k | if (cp2 == NULL) |
5253 | 27.6k | break; |
5254 | 4.53k | else |
5255 | 4.53k | cp1 = cp2 + 1; |
5256 | 32.2k | } |
5257 | | |
5258 | 30.7k | return (xstrdup(result ? "1" : "0")); |
5259 | 30.7k | } |
5260 | | |
5261 | | /* Does session name exist? */ |
5262 | | static char * |
5263 | | format_session_name(struct format_expand_state *es, const char *fmt) |
5264 | 690 | { |
5265 | 690 | char *name; |
5266 | 690 | struct session *s; |
5267 | | |
5268 | 690 | name = format_expand1(es, fmt); |
5269 | 690 | RB_FOREACH(s, sessions, &sessions) { |
5270 | 0 | if (strcmp(s->name, name) == 0) { |
5271 | 0 | free(name); |
5272 | 0 | return (xstrdup("1")); |
5273 | 0 | } |
5274 | 0 | } |
5275 | 690 | free(name); |
5276 | 690 | return (xstrdup("0")); |
5277 | 690 | } |
5278 | | |
5279 | | /* Loop over sessions. */ |
5280 | | static char * |
5281 | | format_loop_sessions(struct format_expand_state *es, const char *fmt) |
5282 | 37.2k | { |
5283 | 37.2k | struct sort_criteria *sc = &sort_crit; |
5284 | 37.2k | struct format_tree *ft = es->ft; |
5285 | 37.2k | struct client *c = ft->client; |
5286 | 37.2k | struct cmdq_item *item = ft->item; |
5287 | 37.2k | struct format_tree *nft; |
5288 | 37.2k | struct format_expand_state next; |
5289 | 37.2k | char *all, *active, *use, *expanded, *value; |
5290 | 37.2k | struct evbuffer *buffer; |
5291 | 37.2k | size_t size; |
5292 | 37.2k | struct session *s, **l; |
5293 | 37.2k | int i, n; |
5294 | | |
5295 | 37.2k | if (format_choose(es, fmt, &all, &active, 0) != 0) { |
5296 | 36.3k | all = xstrdup(fmt); |
5297 | 36.3k | active = NULL; |
5298 | 36.3k | } |
5299 | | |
5300 | 37.2k | buffer = evbuffer_new(); |
5301 | 37.2k | if (buffer == NULL) |
5302 | 0 | fatalx("out of memory"); |
5303 | | |
5304 | 37.2k | l = sort_get_sessions(&n, sc); |
5305 | 37.2k | for (i = 0; i < n; i++) { |
5306 | 0 | s = l[i]; |
5307 | 0 | format_log(es, "session loop: $%u", s->id); |
5308 | 0 | if (active != NULL && |
5309 | 0 | ft->c != NULL && |
5310 | 0 | ft->c->session != NULL && |
5311 | 0 | s->id == ft->c->session->id) |
5312 | 0 | use = active; |
5313 | 0 | else |
5314 | 0 | use = all; |
5315 | 0 | nft = format_create(c, item, FORMAT_NONE, ft->flags); |
5316 | |
|
5317 | 0 | format_add(nft, "loop_index", "%d", i); |
5318 | 0 | format_add(nft, "loop_last_flag", "%d", i == n - 1); |
5319 | |
|
5320 | 0 | format_defaults(nft, ft->c, s, NULL, NULL); |
5321 | 0 | format_copy_state(&next, es, 0); |
5322 | 0 | next.ft = nft; |
5323 | |
|
5324 | 0 | expanded = format_expand1(&next, use); |
5325 | 0 | format_free(next.ft); |
5326 | |
|
5327 | 0 | evbuffer_add(buffer, expanded, strlen(expanded)); |
5328 | 0 | free(expanded); |
5329 | 0 | } |
5330 | | |
5331 | 37.2k | free(active); |
5332 | 37.2k | free(all); |
5333 | | |
5334 | 37.2k | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
5335 | 0 | value = xmemdup(EVBUFFER_DATA(buffer), size); |
5336 | 37.2k | else |
5337 | 37.2k | value = xstrdup(""); |
5338 | 37.2k | evbuffer_free(buffer); |
5339 | 37.2k | return (value); |
5340 | 37.2k | } |
5341 | | |
5342 | | /* Does window name exist? */ |
5343 | | static char * |
5344 | | format_window_name(struct format_expand_state *es, const char *fmt) |
5345 | 697 | { |
5346 | 697 | struct format_tree *ft = es->ft; |
5347 | 697 | char *name; |
5348 | 697 | struct winlink *wl; |
5349 | | |
5350 | 697 | if (ft->s == NULL) { |
5351 | 697 | format_log(es, "window name but no session"); |
5352 | 697 | return (NULL); |
5353 | 697 | } |
5354 | | |
5355 | 0 | name = format_expand1(es, fmt); |
5356 | 0 | RB_FOREACH(wl, winlinks, &ft->s->windows) { |
5357 | 0 | if (strcmp(wl->window->name, name) == 0) { |
5358 | 0 | free(name); |
5359 | 0 | return (xstrdup("1")); |
5360 | 0 | } |
5361 | 0 | } |
5362 | 0 | free(name); |
5363 | 0 | return (xstrdup("0")); |
5364 | 0 | } |
5365 | | |
5366 | | /* Add neighbour window variables to the format tree. */ |
5367 | | static void |
5368 | | format_add_window_neighbour(struct format_tree *nft, struct winlink *wl, |
5369 | | struct session *s, const char *prefix) |
5370 | 0 | { |
5371 | 0 | struct options_entry *o; |
5372 | 0 | const char *oname; |
5373 | 0 | char *key, *prefixed, *oval; |
5374 | |
|
5375 | 0 | xasprintf(&key, "%s_window_index", prefix); |
5376 | 0 | format_add(nft, key, "%u", wl->idx); |
5377 | 0 | free(key); |
5378 | |
|
5379 | 0 | xasprintf(&key, "%s_window_active", prefix); |
5380 | 0 | format_add(nft, key, "%d", wl == s->curw); |
5381 | 0 | free(key); |
5382 | |
|
5383 | 0 | o = options_first(wl->window->options); |
5384 | 0 | while (o != NULL) { |
5385 | 0 | oname = options_name(o); |
5386 | 0 | if (*oname == '@') { |
5387 | 0 | xasprintf(&prefixed, "%s_%s", prefix, oname); |
5388 | 0 | oval = options_to_string(o, NULL, 1); |
5389 | 0 | format_add(nft, prefixed, "%s", oval); |
5390 | 0 | free(oval); |
5391 | 0 | free(prefixed); |
5392 | 0 | } |
5393 | 0 | o = options_next(o); |
5394 | 0 | } |
5395 | 0 | } |
5396 | | |
5397 | | /* Loop over windows. */ |
5398 | | static char * |
5399 | | format_loop_windows(struct format_expand_state *es, const char *fmt) |
5400 | 3.02k | { |
5401 | 3.02k | struct sort_criteria *sc = &sort_crit; |
5402 | 3.02k | struct format_tree *ft = es->ft; |
5403 | 3.02k | struct client *c = ft->client; |
5404 | 3.02k | struct session *s = ft->s; |
5405 | 3.02k | struct cmdq_item *item = ft->item; |
5406 | 3.02k | struct format_tree *nft; |
5407 | 3.02k | struct format_expand_state next; |
5408 | 3.02k | char *all, *active, *use, *expanded, *value; |
5409 | 3.02k | struct evbuffer *buffer; |
5410 | 3.02k | size_t size; |
5411 | 3.02k | struct winlink *wl, **l; |
5412 | 3.02k | struct window *w; |
5413 | 3.02k | int i, n; |
5414 | | |
5415 | 3.02k | if (s == NULL) { |
5416 | 3.02k | format_log(es, "window loop but no session"); |
5417 | 3.02k | return (NULL); |
5418 | 3.02k | } |
5419 | | |
5420 | 0 | if (format_choose(es, fmt, &all, &active, 0) != 0) { |
5421 | 0 | all = xstrdup(fmt); |
5422 | 0 | active = NULL; |
5423 | 0 | } |
5424 | |
|
5425 | 0 | buffer = evbuffer_new(); |
5426 | 0 | if (buffer == NULL) |
5427 | 0 | fatalx("out of memory"); |
5428 | | |
5429 | 0 | l = sort_get_winlinks_session(s, &n, sc); |
5430 | 0 | for (i = 0; i < n; i++) { |
5431 | 0 | wl = l[i]; |
5432 | 0 | w = wl->window; |
5433 | 0 | format_log(es, "window loop: %u @%u", wl->idx, w->id); |
5434 | 0 | if (active != NULL && wl == s->curw) |
5435 | 0 | use = active; |
5436 | 0 | else |
5437 | 0 | use = all; |
5438 | 0 | nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags); |
5439 | |
|
5440 | 0 | format_add(nft, "loop_index", "%d", i); |
5441 | 0 | format_add(nft, "loop_last_flag", "%d", i == n - 1); |
5442 | | |
5443 | | /* Add neighbour window data to the format tree. */ |
5444 | 0 | if (i > 0 && l[i - 1] == s->curw) |
5445 | 0 | format_add(nft, "window_after_active", "1"); |
5446 | 0 | else |
5447 | 0 | format_add(nft, "window_after_active", "0"); |
5448 | 0 | if (i + 1 < n && l[i + 1] == s->curw) |
5449 | 0 | format_add(nft, "window_before_active", "1"); |
5450 | 0 | else |
5451 | 0 | format_add(nft, "window_before_active", "0"); |
5452 | 0 | if (i + 1 < n) |
5453 | 0 | format_add_window_neighbour(nft, l[i + 1], s, "next"); |
5454 | 0 | if (i > 0) |
5455 | 0 | format_add_window_neighbour(nft, l[i - 1], s, "prev"); |
5456 | |
|
5457 | 0 | format_defaults(nft, ft->c, s, wl, NULL); |
5458 | 0 | format_copy_state(&next, es, 0); |
5459 | 0 | next.ft = nft; |
5460 | |
|
5461 | 0 | expanded = format_expand1(&next, use); |
5462 | 0 | format_free(nft); |
5463 | |
|
5464 | 0 | evbuffer_add(buffer, expanded, strlen(expanded)); |
5465 | 0 | free(expanded); |
5466 | 0 | } |
5467 | |
|
5468 | 0 | free(active); |
5469 | 0 | free(all); |
5470 | |
|
5471 | 0 | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
5472 | 0 | value = xmemdup(EVBUFFER_DATA(buffer), size); |
5473 | 0 | else |
5474 | 0 | value = xstrdup(""); |
5475 | 0 | evbuffer_free(buffer); |
5476 | 0 | return (value); |
5477 | 0 | } |
5478 | | |
5479 | | /* Loop over panes. */ |
5480 | | static char * |
5481 | | format_loop_panes(struct format_expand_state *es, const char *fmt) |
5482 | 51.3k | { |
5483 | 51.3k | struct sort_criteria *sc = &sort_crit; |
5484 | 51.3k | struct format_tree *ft = es->ft; |
5485 | 51.3k | struct client *c = ft->client; |
5486 | 51.3k | struct cmdq_item *item = ft->item; |
5487 | 51.3k | struct format_tree *nft; |
5488 | 51.3k | struct format_expand_state next; |
5489 | 51.3k | char *all, *active, *use, *expanded, *value; |
5490 | 51.3k | struct evbuffer *buffer; |
5491 | 51.3k | size_t size; |
5492 | 51.3k | struct window_pane *wp, **l; |
5493 | 51.3k | int i, n; |
5494 | | |
5495 | 51.3k | if (ft->w == NULL) { |
5496 | 51.3k | format_log(es, "pane loop but no window"); |
5497 | 51.3k | return (NULL); |
5498 | 51.3k | } |
5499 | | |
5500 | 0 | if (format_choose(es, fmt, &all, &active, 0) != 0) { |
5501 | 0 | all = xstrdup(fmt); |
5502 | 0 | active = NULL; |
5503 | 0 | } |
5504 | |
|
5505 | 0 | buffer = evbuffer_new(); |
5506 | 0 | if (buffer == NULL) |
5507 | 0 | fatalx("out of memory"); |
5508 | | |
5509 | 0 | l = sort_get_panes_window(ft->w, &n, sc); |
5510 | 0 | for (i = 0; i < n; i++) { |
5511 | 0 | wp = l[i]; |
5512 | 0 | format_log(es, "pane loop: %%%u", wp->id); |
5513 | 0 | if (active != NULL && wp == ft->w->active) |
5514 | 0 | use = active; |
5515 | 0 | else |
5516 | 0 | use = all; |
5517 | 0 | nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags); |
5518 | |
|
5519 | 0 | format_add(nft, "loop_index", "%d", i); |
5520 | 0 | format_add(nft, "loop_last_flag", "%d", i == n - 1); |
5521 | |
|
5522 | 0 | format_defaults(nft, ft->c, ft->s, ft->wl, wp); |
5523 | 0 | format_copy_state(&next, es, 0); |
5524 | 0 | next.ft = nft; |
5525 | |
|
5526 | 0 | expanded = format_expand1(&next, use); |
5527 | 0 | format_free(nft); |
5528 | |
|
5529 | 0 | evbuffer_add(buffer, expanded, strlen(expanded)); |
5530 | 0 | free(expanded); |
5531 | 0 | } |
5532 | |
|
5533 | 0 | free(active); |
5534 | 0 | free(all); |
5535 | |
|
5536 | 0 | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
5537 | 0 | value = xmemdup(EVBUFFER_DATA(buffer), size); |
5538 | 0 | else |
5539 | 0 | value = xstrdup(""); |
5540 | 0 | evbuffer_free(buffer); |
5541 | 0 | return (value); |
5542 | 0 | } |
5543 | | |
5544 | | /* Add an option to an options loop. */ |
5545 | | static void |
5546 | | format_loop_add_option(struct format_expand_state *es, const char *fmt, |
5547 | | struct evbuffer *buffer, struct options_entry *o, u_int n, u_int i) |
5548 | 4.22M | { |
5549 | 4.22M | struct format_tree *ft = es->ft, *nft; |
5550 | 4.22M | struct format_expand_state next; |
5551 | 4.22M | const struct options_table_entry *oe = options_table_entry(o); |
5552 | 4.22M | const char *name = options_name(o); |
5553 | 4.22M | char *expanded, *s; |
5554 | 4.22M | int is_array = options_is_array(o); |
5555 | | |
5556 | 4.22M | format_log(es, "option loop: %s", name); |
5557 | 4.22M | nft = format_create(ft->client, ft->item, FORMAT_NONE, ft->flags); |
5558 | | |
5559 | 4.22M | format_add(nft, "option_name", "%s", name); |
5560 | 4.22M | s = options_to_string(o, NULL, 0); |
5561 | 4.22M | format_add(nft, "option_value", "%s", s); |
5562 | 4.22M | free(s); |
5563 | | |
5564 | 4.22M | format_add(nft, "option_is_array", "%d", is_array); |
5565 | 4.22M | format_add(nft, "option_array_key", "%s", ""); |
5566 | 4.22M | format_add(nft, "option_array_index", "%s", ""); |
5567 | 4.22M | format_add(nft, "option_array_first", "%d", is_array); |
5568 | 4.22M | format_add(nft, "option_array_last", "%d", is_array); |
5569 | 4.22M | format_add(nft, "option_array_count", "%u", n); |
5570 | | |
5571 | 4.22M | if (oe != NULL && (oe->flags & OPTIONS_TABLE_IS_HOOK)) |
5572 | 1.55M | format_add(nft, "option_is_hook", "1"); |
5573 | 2.66M | else |
5574 | 2.66M | format_add(nft, "option_is_hook", "0"); |
5575 | 4.22M | format_add(nft, "option_is_user", "%d", oe == NULL); |
5576 | | |
5577 | 4.22M | if (options_next(o) == NULL) |
5578 | 47.8k | format_add(nft, "loop_last_flag", "1"); |
5579 | 4.17M | else |
5580 | 4.17M | format_add(nft, "loop_last_flag", "0"); |
5581 | 4.22M | format_add(nft, "loop_index", "%u", i); |
5582 | | |
5583 | 4.22M | format_defaults(nft, ft->c, ft->s, ft->wl, ft->wp); |
5584 | 4.22M | format_copy_state(&next, es, 0); |
5585 | 4.22M | next.ft = nft; |
5586 | | |
5587 | 4.22M | expanded = format_expand1(&next, fmt); |
5588 | 4.22M | format_free(nft); |
5589 | 4.22M | evbuffer_add(buffer, expanded, strlen(expanded)); |
5590 | 4.22M | free(expanded); |
5591 | 4.22M | } |
5592 | | |
5593 | | /* Add an array option item to an options loop. */ |
5594 | | static void |
5595 | | format_loop_add_array_item(struct format_expand_state *es, const char *fmt, |
5596 | | struct evbuffer *buffer, struct options_entry *o, |
5597 | | struct options_array_item *a, int n, u_int i) |
5598 | 529k | { |
5599 | 529k | struct format_tree *ft = es->ft, *nft; |
5600 | 529k | struct format_expand_state next; |
5601 | 529k | const struct options_table_entry *oe = options_table_entry(o); |
5602 | 529k | const char *name = options_name(o); |
5603 | 529k | const char *array_key; |
5604 | 529k | char *expanded, *s; |
5605 | | |
5606 | 529k | array_key = options_array_item_key(a); |
5607 | 529k | format_log(es, "option loop: %s[%s]", name, array_key); |
5608 | 529k | nft = format_create(ft->client, ft->item, FORMAT_NONE, ft->flags); |
5609 | | |
5610 | 529k | format_add(nft, "option_name", "%s", name); |
5611 | 529k | s = options_to_string(o, array_key, 0); |
5612 | 529k | format_add(nft, "option_value", "%s", s); |
5613 | 529k | free(s); |
5614 | | |
5615 | 529k | format_add(nft, "option_is_array", "1"); |
5616 | 529k | format_add(nft, "option_array_key", "%s", array_key); |
5617 | 529k | format_add(nft, "option_array_index", "%s", array_key); |
5618 | 529k | if (a == options_array_first(o)) |
5619 | 94.6k | format_add(nft, "option_array_first", "1"); |
5620 | 434k | else |
5621 | 434k | format_add(nft, "option_array_first", "0"); |
5622 | 529k | if (options_array_next(a) == NULL) |
5623 | 94.6k | format_add(nft, "option_array_last", "1"); |
5624 | 434k | else |
5625 | 434k | format_add(nft, "option_array_last", "0"); |
5626 | 529k | format_add(nft, "option_array_count", "%u", n); |
5627 | | |
5628 | 529k | if (oe != NULL && (oe->flags & OPTIONS_TABLE_IS_HOOK)) |
5629 | 0 | format_add(nft, "option_is_hook", "1"); |
5630 | 529k | else |
5631 | 529k | format_add(nft, "option_is_hook", "0"); |
5632 | 529k | format_add(nft, "option_is_user", "%d", oe == NULL); |
5633 | | |
5634 | 529k | if (options_array_next(a) == NULL && options_next(o) == NULL) |
5635 | 0 | format_add(nft, "loop_last_flag", "1"); |
5636 | 529k | else |
5637 | 529k | format_add(nft, "loop_last_flag", "0"); |
5638 | 529k | format_add(nft, "loop_index", "%u", i); |
5639 | | |
5640 | 529k | format_defaults(nft, ft->c, ft->s, ft->wl, ft->wp); |
5641 | 529k | format_copy_state(&next, es, 0); |
5642 | 529k | next.ft = nft; |
5643 | | |
5644 | 529k | expanded = format_expand1(&next, fmt); |
5645 | 529k | format_free(nft); |
5646 | 529k | evbuffer_add(buffer, expanded, strlen(expanded)); |
5647 | 529k | free(expanded); |
5648 | 529k | } |
5649 | | |
5650 | | /* Loop over options. */ |
5651 | | static char * |
5652 | | format_loop_options(struct format_expand_state *es, const char *fmt, |
5653 | | const char *flags) |
5654 | 91.0k | { |
5655 | 91.0k | struct format_tree *ft = es->ft; |
5656 | 91.0k | struct options *oo = NULL; |
5657 | 91.0k | struct options_entry *o; |
5658 | 91.0k | struct options_array_item *a; |
5659 | 91.0k | char *value; |
5660 | 91.0k | struct evbuffer *buffer; |
5661 | 91.0k | size_t size; |
5662 | 91.0k | u_int i = 0, n; |
5663 | 91.0k | int global = 0; |
5664 | | |
5665 | 91.0k | if (flags == NULL || *flags == '\0') |
5666 | 22.2k | flags = "s"; |
5667 | 91.0k | if (strchr(flags, 'v') != NULL) |
5668 | 17.4k | oo = global_options; |
5669 | 73.5k | else { |
5670 | 73.5k | if (strchr(flags, 'g') != NULL) |
5671 | 45.4k | global = 1; |
5672 | 73.5k | if (strchr(flags, 'w') != NULL) { |
5673 | 9.74k | if (global) |
5674 | 9.24k | oo = global_w_options; |
5675 | 497 | else if (ft->w != NULL) |
5676 | 0 | oo = ft->w->options; |
5677 | 63.8k | } else if (strchr(flags, 's') != NULL) { |
5678 | 24.2k | if (global) |
5679 | 1.82k | oo = global_s_options; |
5680 | 22.3k | else if (ft->s != NULL) |
5681 | 0 | oo = ft->s->options; |
5682 | 39.6k | } else if (strchr(flags, 'p') != NULL) { |
5683 | 15.8k | if (global) |
5684 | 15.1k | /* invalid */; |
5685 | 679 | else if (ft->wp != NULL) |
5686 | 0 | oo = ft->wp->options; |
5687 | 23.8k | } else if (global) |
5688 | 19.2k | oo = global_s_options; |
5689 | 73.5k | } |
5690 | 91.0k | if (oo == NULL) |
5691 | 43.2k | return (xstrdup("")); |
5692 | | |
5693 | 47.8k | buffer = evbuffer_new(); |
5694 | 47.8k | if (buffer == NULL) |
5695 | 0 | fatalx("out of memory"); |
5696 | | |
5697 | 47.8k | o = options_first(oo); |
5698 | 4.36M | while (o != NULL) { |
5699 | | |
5700 | 4.31M | n = 0; |
5701 | 4.31M | if (options_is_array(o)) { |
5702 | 1.69M | a = options_array_first(o); |
5703 | 2.22M | while (a != NULL) { |
5704 | 529k | n++; |
5705 | 529k | a = options_array_next(a); |
5706 | 529k | } |
5707 | 1.69M | } |
5708 | | |
5709 | 4.31M | if (!options_is_array(o) || n == 0) { |
5710 | 4.22M | format_loop_add_option(es, fmt, buffer, o, n, i); |
5711 | 4.22M | i++; |
5712 | 4.22M | o = options_next(o); |
5713 | 4.22M | continue; |
5714 | 4.22M | } |
5715 | | |
5716 | 94.6k | a = options_array_first(o); |
5717 | 624k | while (a != NULL) { |
5718 | 529k | format_loop_add_array_item(es, fmt, buffer, o, a, n, i); |
5719 | 529k | i++; |
5720 | 529k | a = options_array_next(a); |
5721 | 529k | } |
5722 | 94.6k | o = options_next(o); |
5723 | 94.6k | } |
5724 | | |
5725 | 47.8k | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
5726 | 38.0k | value = xmemdup(EVBUFFER_DATA(buffer), size); |
5727 | 9.80k | else |
5728 | 9.80k | value = xstrdup(""); |
5729 | 47.8k | evbuffer_free(buffer); |
5730 | 47.8k | return (value); |
5731 | 47.8k | } |
5732 | | |
5733 | | /* Loop over an environment. */ |
5734 | | static char * |
5735 | | format_loop_environ(struct format_expand_state *es, const char *fmt, |
5736 | | const char *flags) |
5737 | 297k | { |
5738 | 297k | struct format_tree *ft = es->ft, *nft; |
5739 | 297k | struct client *c = ft->client; |
5740 | 297k | struct cmdq_item *item = ft->item; |
5741 | 297k | struct format_expand_state next; |
5742 | 297k | struct environ *env = NULL; |
5743 | 297k | struct environ_entry *envent; |
5744 | 297k | char *expanded, *value; |
5745 | 297k | struct evbuffer *buffer; |
5746 | 297k | size_t size; |
5747 | 297k | u_int i = 0; |
5748 | | |
5749 | 297k | if (flags == NULL || *flags == '\0' || strcmp(flags, "s") == 0) { |
5750 | 208k | if (ft->s != NULL) |
5751 | 0 | env = ft->s->environ; |
5752 | 208k | } else if (strcmp(flags, "g") == 0) |
5753 | 7.46k | env = global_environ; |
5754 | 81.6k | else if (strcmp(flags, "c") == 0) { |
5755 | 421 | if (ft->client != NULL) |
5756 | 0 | env = ft->client->environ; |
5757 | 421 | } |
5758 | 297k | if (env == NULL) |
5759 | 290k | return (xstrdup("")); |
5760 | | |
5761 | 7.46k | buffer = evbuffer_new(); |
5762 | 7.46k | if (buffer == NULL) |
5763 | 0 | fatalx("out of memory"); |
5764 | | |
5765 | 7.46k | envent = environ_first(env); |
5766 | 1.83M | while (envent != NULL) { |
5767 | 1.82M | format_log(es, "environment loop: %s", envent->name); |
5768 | 1.82M | nft = format_create(c, item, FORMAT_NONE, ft->flags); |
5769 | | |
5770 | 1.82M | format_add(nft, "environ_name", "%s", envent->name); |
5771 | 1.82M | if (envent->value == NULL) |
5772 | 0 | format_add(nft, "environ_value", "%s", ""); |
5773 | 1.82M | else |
5774 | 1.82M | format_add(nft, "environ_value", "%s", envent->value); |
5775 | | |
5776 | 1.82M | if (envent->flags & ENVIRON_HIDDEN) |
5777 | 30.0k | format_add(nft, "environ_hidden", "1"); |
5778 | 1.79M | else |
5779 | 1.79M | format_add(nft, "environ_hidden", "0"); |
5780 | 1.82M | format_add(nft, "environ_removed", "%d", envent->value == NULL); |
5781 | | |
5782 | 1.82M | if (environ_next(envent) == NULL) |
5783 | 7.15k | format_add(nft, "loop_last_flag", "1"); |
5784 | 1.81M | else |
5785 | 1.81M | format_add(nft, "loop_last_flag", "0"); |
5786 | 1.82M | format_add(nft, "loop_index", "%u", i); |
5787 | | |
5788 | 1.82M | format_defaults(nft, ft->c, ft->s, ft->wl, ft->wp); |
5789 | 1.82M | format_copy_state(&next, es, 0); |
5790 | 1.82M | next.ft = nft; |
5791 | | |
5792 | 1.82M | expanded = format_expand1(&next, fmt); |
5793 | 1.82M | format_free(nft); |
5794 | 1.82M | evbuffer_add(buffer, expanded, strlen(expanded)); |
5795 | 1.82M | free(expanded); |
5796 | | |
5797 | 1.82M | i++; |
5798 | 1.82M | envent = environ_next(envent); |
5799 | 1.82M | } |
5800 | | |
5801 | 7.46k | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
5802 | 5.08k | value = xmemdup(EVBUFFER_DATA(buffer), size); |
5803 | 2.37k | else |
5804 | 2.37k | value = xstrdup(""); |
5805 | 7.46k | evbuffer_free(buffer); |
5806 | 7.46k | return (value); |
5807 | 7.46k | } |
5808 | | |
5809 | | /* Loop over clients. */ |
5810 | | static char * |
5811 | | format_loop_clients(struct format_expand_state *es, const char *fmt) |
5812 | 3.22k | { |
5813 | 3.22k | struct sort_criteria *sc = &sort_crit; |
5814 | 3.22k | struct format_tree *ft = es->ft; |
5815 | 3.22k | struct client *c, **l; |
5816 | 3.22k | struct cmdq_item *item = ft->item; |
5817 | 3.22k | struct format_tree *nft; |
5818 | 3.22k | struct format_expand_state next; |
5819 | 3.22k | char *expanded, *value; |
5820 | 3.22k | struct evbuffer *buffer; |
5821 | 3.22k | size_t size; |
5822 | 3.22k | int i, n; |
5823 | | |
5824 | 3.22k | buffer = evbuffer_new(); |
5825 | 3.22k | if (buffer == NULL) |
5826 | 0 | fatalx("out of memory"); |
5827 | | |
5828 | 3.22k | l = sort_get_clients(&n, sc); |
5829 | 3.22k | for (i = 0; i < n; i++) { |
5830 | 0 | c = l[i]; |
5831 | 0 | format_log(es, "client loop: %s", c->name); |
5832 | 0 | nft = format_create(c, item, 0, ft->flags); |
5833 | |
|
5834 | 0 | format_add(nft, "loop_index", "%d", i); |
5835 | 0 | format_add(nft, "loop_last_flag", "%d", i == n - 1); |
5836 | |
|
5837 | 0 | format_defaults(nft, c, ft->s, ft->wl, ft->wp); |
5838 | 0 | format_copy_state(&next, es, 0); |
5839 | 0 | next.ft = nft; |
5840 | |
|
5841 | 0 | expanded = format_expand1(&next, fmt); |
5842 | 0 | format_free(nft); |
5843 | 0 | evbuffer_add(buffer, expanded, strlen(expanded)); |
5844 | 0 | free(expanded); |
5845 | 0 | } |
5846 | | |
5847 | 3.22k | if ((size = EVBUFFER_LENGTH(buffer)) != 0) |
5848 | 0 | value = xmemdup(EVBUFFER_DATA(buffer), size); |
5849 | 3.22k | else |
5850 | 3.22k | value = xstrdup(""); |
5851 | 3.22k | evbuffer_free(buffer); |
5852 | 3.22k | return (value); |
5853 | 3.22k | } |
5854 | | |
5855 | | /* Truncate a value to a long long, failing if it is NaN or out of range. */ |
5856 | | static int |
5857 | | format_truncate_integer(double *v) |
5858 | 65.7k | { |
5859 | | /* (double)LLONG_MAX is 2^63, which does not fit; -2^63 does. */ |
5860 | 65.7k | if (isnan(*v) || *v >= (double)LLONG_MAX || *v < (double)LLONG_MIN) |
5861 | 4.61k | return (-1); |
5862 | 61.1k | *v = (long long)*v; |
5863 | 61.1k | return (0); |
5864 | 65.7k | } |
5865 | | |
5866 | | static char * |
5867 | | format_replace_expression(struct format_modifier *mexp, |
5868 | | struct format_expand_state *es, const char *copy) |
5869 | 100k | { |
5870 | 100k | int argc = mexp->argc; |
5871 | 100k | const char *errstr; |
5872 | 100k | char *endch, *value, *left = NULL, *right = NULL; |
5873 | 100k | int use_fp = 0; |
5874 | 100k | u_int prec = 0; |
5875 | 100k | double mleft, mright, result; |
5876 | 100k | enum { ADD, |
5877 | 100k | SUBTRACT, |
5878 | 100k | MULTIPLY, |
5879 | 100k | DIVIDE, |
5880 | 100k | MODULUS, |
5881 | 100k | EQUAL, |
5882 | 100k | NOT_EQUAL, |
5883 | 100k | GREATER_THAN, |
5884 | 100k | GREATER_THAN_EQUAL, |
5885 | 100k | LESS_THAN, |
5886 | 100k | LESS_THAN_EQUAL } operator; |
5887 | | |
5888 | 100k | if (strcmp(mexp->argv[0], "+") == 0) |
5889 | 792 | operator = ADD; |
5890 | 99.9k | else if (strcmp(mexp->argv[0], "-") == 0) |
5891 | 2.47k | operator = SUBTRACT; |
5892 | 97.4k | else if (strcmp(mexp->argv[0], "*") == 0) |
5893 | 13.4k | operator = MULTIPLY; |
5894 | 83.9k | else if (strcmp(mexp->argv[0], "/") == 0) |
5895 | 2.83k | operator = DIVIDE; |
5896 | 81.1k | else if (strcmp(mexp->argv[0], "%") == 0 || |
5897 | 75.0k | strcmp(mexp->argv[0], "m") == 0) |
5898 | 8.06k | operator = MODULUS; |
5899 | 73.0k | else if (strcmp(mexp->argv[0], "==") == 0) |
5900 | 31.4k | operator = EQUAL; |
5901 | 41.6k | else if (strcmp(mexp->argv[0], "!=") == 0) |
5902 | 1.10k | operator = NOT_EQUAL; |
5903 | 40.5k | else if (strcmp(mexp->argv[0], ">") == 0) |
5904 | 12.0k | operator = GREATER_THAN; |
5905 | 28.4k | else if (strcmp(mexp->argv[0], "<") == 0) |
5906 | 1.29k | operator = LESS_THAN; |
5907 | 27.1k | else if (strcmp(mexp->argv[0], ">=") == 0) |
5908 | 638 | operator = GREATER_THAN_EQUAL; |
5909 | 26.5k | else if (strcmp(mexp->argv[0], "<=") == 0) |
5910 | 738 | operator = LESS_THAN_EQUAL; |
5911 | 25.7k | else { |
5912 | 25.7k | format_log(es, "expression has no valid operator: '%s'", |
5913 | 25.7k | mexp->argv[0]); |
5914 | 25.7k | goto fail; |
5915 | 25.7k | } |
5916 | | |
5917 | | /* The second argument may be flags. */ |
5918 | 74.9k | if (argc >= 2 && strchr(mexp->argv[1], 'f') != NULL) { |
5919 | 824 | use_fp = 1; |
5920 | 824 | prec = 2; |
5921 | 824 | } |
5922 | | |
5923 | | /* The third argument may be precision. */ |
5924 | 74.9k | if (argc >= 3) { |
5925 | 1.58k | prec = strtonum(mexp->argv[2], -FORMAT_MAX_PRECISION, |
5926 | 1.58k | FORMAT_MAX_PRECISION, &errstr); |
5927 | 1.58k | if (errstr != NULL) { |
5928 | 398 | format_log(es, "expression precision %s: %s", errstr, |
5929 | 398 | mexp->argv[2]); |
5930 | 398 | goto fail; |
5931 | 398 | } |
5932 | 1.58k | } |
5933 | | |
5934 | 74.5k | if (format_choose(es, copy, &left, &right, 1) != 0) { |
5935 | 47.8k | format_log(es, "expression syntax error"); |
5936 | 47.8k | goto fail; |
5937 | 47.8k | } |
5938 | | |
5939 | 26.7k | mleft = strtod(left, &endch); |
5940 | 26.7k | if (*endch != '\0') { |
5941 | 2.06k | format_log(es, "expression left side is invalid: %s", left); |
5942 | 2.06k | goto fail; |
5943 | 2.06k | } |
5944 | | |
5945 | 24.6k | mright = strtod(right, &endch); |
5946 | 24.6k | if (*endch != '\0') { |
5947 | 609 | format_log(es, "expression right side is invalid: %s", right); |
5948 | 609 | goto fail; |
5949 | 609 | } |
5950 | | |
5951 | 24.0k | if (!use_fp && |
5952 | 23.2k | (format_truncate_integer(&mleft) != 0 || |
5953 | 21.9k | format_truncate_integer(&mright) != 0)) { |
5954 | 2.70k | format_log(es, "expression side is out of range"); |
5955 | 2.70k | goto fail; |
5956 | 2.70k | } |
5957 | 21.3k | format_log(es, "expression left side is: %.*f", prec, mleft); |
5958 | 21.3k | format_log(es, "expression right side is: %.*f", prec, mright); |
5959 | | |
5960 | 21.3k | switch (operator) { |
5961 | 478 | case ADD: |
5962 | 478 | result = mleft + mright; |
5963 | 478 | break; |
5964 | 763 | case SUBTRACT: |
5965 | 763 | result = mleft - mright; |
5966 | 763 | break; |
5967 | 654 | case MULTIPLY: |
5968 | 654 | result = mleft * mright; |
5969 | 654 | break; |
5970 | 1.33k | case DIVIDE: |
5971 | 1.33k | result = mleft / mright; |
5972 | 1.33k | break; |
5973 | 1.10k | case MODULUS: |
5974 | 1.10k | result = fmod(mleft, mright); |
5975 | 1.10k | break; |
5976 | 3.60k | case EQUAL: |
5977 | 3.60k | result = fabs(mleft - mright) < 1e-9; |
5978 | 3.60k | break; |
5979 | 769 | case NOT_EQUAL: |
5980 | 769 | result = fabs(mleft - mright) > 1e-9; |
5981 | 769 | break; |
5982 | 10.9k | case GREATER_THAN: |
5983 | 10.9k | result = (mleft > mright); |
5984 | 10.9k | break; |
5985 | 450 | case GREATER_THAN_EQUAL: |
5986 | 450 | result = (mleft >= mright); |
5987 | 450 | break; |
5988 | 595 | case LESS_THAN: |
5989 | 595 | result = (mleft < mright); |
5990 | 595 | break; |
5991 | 680 | case LESS_THAN_EQUAL: |
5992 | 680 | result = (mleft <= mright); |
5993 | 680 | break; |
5994 | 21.3k | } |
5995 | 21.3k | if (!use_fp && format_truncate_integer(&result) != 0) { |
5996 | 1.90k | format_log(es, "expression result is out of range"); |
5997 | 1.90k | goto fail; |
5998 | 1.90k | } |
5999 | 19.4k | xasprintf(&value, "%.*f", prec, result); |
6000 | 19.4k | format_log(es, "expression result is %s", value); |
6001 | | |
6002 | 19.4k | free(right); |
6003 | 19.4k | free(left); |
6004 | 19.4k | return (value); |
6005 | | |
6006 | 81.2k | fail: |
6007 | 81.2k | free(right); |
6008 | 81.2k | free(left); |
6009 | 81.2k | return (NULL); |
6010 | 21.3k | } |
6011 | | |
6012 | | /* Callback for the cycle timer; redraw the status line. */ |
6013 | | static void |
6014 | | format_cycle_callback(__unused int fd, __unused short events, void *arg) |
6015 | 0 | { |
6016 | 0 | struct client *c = arg; |
6017 | |
|
6018 | 0 | if (c->message_string == NULL && c->prompt == NULL) |
6019 | 0 | c->flags |= CLIENT_REDRAWSTATUS; |
6020 | 0 | } |
6021 | | |
6022 | | /* Arm the cycle timer to redraw the status line if it is not already. */ |
6023 | | static void |
6024 | | format_cycle_start_timer(struct client *c) |
6025 | 0 | { |
6026 | 0 | struct timeval tv; |
6027 | |
|
6028 | 0 | tv.tv_sec = FORMAT_CYCLE_PERIOD / 1000; |
6029 | 0 | tv.tv_usec = (FORMAT_CYCLE_PERIOD % 1000) * 1000L; |
6030 | |
|
6031 | 0 | if (!event_initialized(&c->cycle_timer)) |
6032 | 0 | evtimer_set(&c->cycle_timer, format_cycle_callback, c); |
6033 | 0 | if (!evtimer_pending(&c->cycle_timer, NULL)) |
6034 | 0 | evtimer_add(&c->cycle_timer, &tv); |
6035 | 0 | } |
6036 | | |
6037 | | /* Expand the "A" animation modifier; see the manual for the syntax. */ |
6038 | | static char * |
6039 | | format_cycle(struct format_expand_state *es, const char *frames, u_int count) |
6040 | 19.2k | { |
6041 | 19.2k | struct format_tree *ft = es->ft; |
6042 | 19.2k | const char *start, *end, *cp; |
6043 | 19.2k | u_int n, index, i; |
6044 | | |
6045 | | /* |
6046 | | * A cycle is only expanded in a status format, and never in the |
6047 | | * command or output of #() where it would change on every frame and |
6048 | | * make the job run again. |
6049 | | */ |
6050 | 19.2k | if (!(ft->flags & FORMAT_STATUS) || (es->flags & FORMAT_EXPAND_NOCYCLE)) |
6051 | 19.2k | return (xstrdup("")); |
6052 | 0 | if (*frames == '\0') |
6053 | 0 | return (xstrdup("")); |
6054 | | |
6055 | | /* Count the comma-separated frames (there is at least one). */ |
6056 | 0 | n = 1; |
6057 | 0 | for (cp = frames; *cp != '\0'; cp++) { |
6058 | 0 | if (*cp == ',') |
6059 | 0 | n++; |
6060 | 0 | } |
6061 | 0 | index = (es->start_time / (count * FORMAT_CYCLE_PERIOD)) % n; |
6062 | | |
6063 | | /* |
6064 | | * Redraw the status line so the frames advance on their own; a |
6065 | | * single frame never changes so there is nothing to redraw for. |
6066 | | */ |
6067 | 0 | if (n > 1 && ft->client != NULL) |
6068 | 0 | format_cycle_start_timer(ft->client); |
6069 | | |
6070 | | /* Walk to the chosen frame and return a copy of it. */ |
6071 | 0 | start = frames; |
6072 | 0 | for (i = 0; i < index; i++) |
6073 | 0 | start = strchr(start, ',') + 1; |
6074 | 0 | end = strchr(start, ','); |
6075 | 0 | if (end == NULL) |
6076 | 0 | end = start + strlen(start); |
6077 | 0 | return (xstrndup(start, end - start)); |
6078 | 0 | } |
6079 | | |
6080 | | /* Replace a key. */ |
6081 | | static int |
6082 | | format_replace(struct format_expand_state *es, const char *key, size_t keylen, |
6083 | | char **buf, size_t *len, size_t *off) |
6084 | 6.21M | { |
6085 | 6.21M | struct sort_criteria *sc = &sort_crit; |
6086 | 6.21M | struct format_tree *ft = es->ft; |
6087 | 6.21M | struct window_pane *wp = ft->wp; |
6088 | 6.21M | const char *errstr, *copy, *cp, *cp2; |
6089 | 6.21M | const char *marker = NULL; |
6090 | 6.21M | char *time_format = NULL; |
6091 | 6.21M | char *copy0, *condition, *found, *new; |
6092 | 6.21M | char *value, *left, *right; |
6093 | 6.21M | size_t n; |
6094 | 6.21M | uint64_t modifiers = 0; |
6095 | 6.21M | int limit = 0, width = 0; |
6096 | 6.21M | int j, c; |
6097 | 6.21M | struct format_modifier *list, *cmp = NULL, *search = NULL; |
6098 | 6.21M | struct format_modifier **sub = NULL, *mexp = NULL, *fm; |
6099 | 6.21M | struct format_modifier *bool_op_n = NULL; |
6100 | 6.21M | u_int cycle_count = 1; |
6101 | 6.21M | u_int i, count, nsub = 0, nrep; |
6102 | 6.21M | const char *loop_flags = ""; |
6103 | 6.21M | struct format_expand_state next; |
6104 | 6.21M | struct environ_entry *envent; |
6105 | | |
6106 | | /* Set sorting defaults. */ |
6107 | 6.21M | sc->order = SORT_ORDER; |
6108 | 6.21M | sc->reversed = 0; |
6109 | | |
6110 | | /* Make a copy of the key. */ |
6111 | 6.21M | copy = copy0 = xstrndup(key, keylen); |
6112 | | |
6113 | | /* Process modifier list. */ |
6114 | 6.21M | list = format_build_modifiers(es, ©, &count); |
6115 | 8.37M | for (i = 0; i < count; i++) { |
6116 | 2.15M | fm = &list[i]; |
6117 | 2.15M | if (format_logging(ft)) { |
6118 | 0 | format_log(es, "modifier %u is %s", i, fm->modifier); |
6119 | 0 | for (j = 0; j < fm->argc; j++) { |
6120 | 0 | format_log(es, "modifier %u argument %d: %s", i, |
6121 | 0 | j, fm->argv[j]); |
6122 | 0 | } |
6123 | 0 | } |
6124 | 2.15M | if (fm->size == 1) { |
6125 | 2.10M | switch (fm->modifier[0]) { |
6126 | 184k | case 'm': |
6127 | 186k | case '<': |
6128 | 188k | case '>': |
6129 | 188k | cmp = fm; |
6130 | 188k | break; |
6131 | 54.3k | case '!': |
6132 | 54.3k | modifiers |= FORMAT_NOT; |
6133 | 54.3k | break; |
6134 | 7.71k | case 'C': |
6135 | 7.71k | search = fm; |
6136 | 7.71k | break; |
6137 | 29.7k | case 's': |
6138 | 29.7k | if (fm->argc < 2) |
6139 | 12.2k | break; |
6140 | 17.5k | sub = xreallocarray(sub, nsub + 1, sizeof *sub); |
6141 | 17.5k | sub[nsub++] = fm; |
6142 | 17.5k | break; |
6143 | 34.2k | case '=': |
6144 | 34.2k | if (fm->argc < 1) |
6145 | 1.84k | break; |
6146 | 32.3k | limit = strtonum(fm->argv[0], -FORMAT_MAX_WIDTH, |
6147 | 32.3k | FORMAT_MAX_WIDTH, &errstr); |
6148 | 32.3k | if (errstr != NULL) |
6149 | 18.9k | limit = 0; |
6150 | 32.3k | if (fm->argc >= 2 && fm->argv[1] != NULL) |
6151 | 15.6k | marker = fm->argv[1]; |
6152 | 32.3k | break; |
6153 | 245k | case 'p': |
6154 | 245k | if (fm->argc < 1) |
6155 | 1.24k | break; |
6156 | 243k | width = strtonum(fm->argv[0], -FORMAT_MAX_WIDTH, |
6157 | 243k | FORMAT_MAX_WIDTH, &errstr); |
6158 | 243k | if (errstr != NULL) |
6159 | 43.7k | width = 0; |
6160 | 243k | break; |
6161 | 19.2k | case 'A': |
6162 | 19.2k | modifiers |= FORMAT_CYCLE; |
6163 | 19.2k | if (fm->argc < 1) |
6164 | 559 | break; |
6165 | 18.7k | cycle_count = strtonum(fm->argv[0], 1, 100, |
6166 | 18.7k | &errstr); |
6167 | 18.7k | if (errstr != NULL) |
6168 | 18.2k | cycle_count = 1; |
6169 | 18.7k | break; |
6170 | 1.67k | case 'w': |
6171 | 1.67k | modifiers |= FORMAT_WIDTH; |
6172 | 1.67k | break; |
6173 | 103k | case 'e': |
6174 | 103k | if (fm->argc < 1 || fm->argc > 3) |
6175 | 2.21k | break; |
6176 | 101k | mexp = fm; |
6177 | 101k | break; |
6178 | 2.27k | case 'l': |
6179 | 2.27k | modifiers |= FORMAT_LITERAL; |
6180 | 2.27k | break; |
6181 | 4.91k | case 'a': |
6182 | 4.91k | modifiers |= FORMAT_CHARACTER; |
6183 | 4.91k | break; |
6184 | 962 | case 'b': |
6185 | 962 | modifiers |= FORMAT_BASENAME; |
6186 | 962 | break; |
6187 | 163k | case 'c': |
6188 | 163k | modifiers |= FORMAT_COLOUR; |
6189 | 163k | if (fm->argc < 1) |
6190 | 84.7k | break; |
6191 | 78.9k | if (strchr(fm->argv[0], 'f') != NULL) |
6192 | 4.28k | modifiers |= FORMAT_COLOUR_ESC_FG; |
6193 | 78.9k | if (strchr(fm->argv[0], 'b') != NULL) |
6194 | 20.8k | modifiers |= FORMAT_COLOUR_ESC_BG; |
6195 | 78.9k | break; |
6196 | 1.40k | case 'd': |
6197 | 1.40k | modifiers |= FORMAT_DIRNAME; |
6198 | 1.40k | break; |
6199 | 12.8k | case 'n': |
6200 | 12.8k | modifiers |= FORMAT_LENGTH; |
6201 | 12.8k | break; |
6202 | 5.98k | case 'I': |
6203 | 5.98k | if (fm->argc < 1) |
6204 | 3.68k | break; |
6205 | 2.29k | if (strchr(fm->argv[0], 'f') != NULL) |
6206 | 779 | modifiers |= FORMAT_CLIENT_TERMFEAT; |
6207 | 2.29k | if (strchr(fm->argv[0], 'c') != NULL) |
6208 | 615 | modifiers |= FORMAT_CLIENT_TERMCAP; |
6209 | 2.29k | if (strchr(fm->argv[0], 'e') != NULL) |
6210 | 777 | modifiers |= FORMAT_CLIENT_ENVIRON; |
6211 | 2.29k | break; |
6212 | 88.1k | case 't': |
6213 | 88.1k | modifiers |= FORMAT_TIMESTRING; |
6214 | 88.1k | if (fm->argc < 1) |
6215 | 17.4k | break; |
6216 | 70.7k | if (strchr(fm->argv[0], 'p') != NULL) |
6217 | 3.73k | modifiers |= FORMAT_PRETTY; |
6218 | 66.9k | else if (strchr(fm->argv[0], 'r') != NULL) |
6219 | 15.4k | modifiers |= FORMAT_RELATIVE; |
6220 | 51.5k | else if (strchr(fm->argv[0], 'd') != NULL) |
6221 | 1.03k | modifiers |= FORMAT_DIFFERENCE; |
6222 | 50.5k | else if (fm->argc >= 2 && |
6223 | 3.55k | strchr(fm->argv[0], 'f') != NULL) { |
6224 | 3.09k | free(time_format); |
6225 | 3.09k | time_format = format_strip(es, |
6226 | 3.09k | fm->argv[1]); |
6227 | 3.09k | } |
6228 | 70.7k | break; |
6229 | 36.3k | case 'q': |
6230 | 36.3k | if (fm->argc < 1) |
6231 | 1.45k | modifiers |= FORMAT_QUOTE_SHELL; |
6232 | 34.8k | else if (strchr(fm->argv[0], 's') != NULL) |
6233 | 2.06k | modifiers |= FORMAT_QUOTE_SHELL_SQ; |
6234 | 32.8k | else if (strchr(fm->argv[0], 'e') != NULL || |
6235 | 31.5k | strchr(fm->argv[0], 'h') != NULL) |
6236 | 2.88k | modifiers |= FORMAT_QUOTE_STYLE; |
6237 | 29.9k | else if (strchr(fm->argv[0], 'a') != NULL) |
6238 | 28.2k | modifiers |= FORMAT_QUOTE_ARGUMENTS; |
6239 | 36.3k | break; |
6240 | 35.4k | case 'E': |
6241 | 35.4k | modifiers |= FORMAT_EXPAND; |
6242 | 35.4k | break; |
6243 | 6.12k | case 'T': |
6244 | 6.12k | modifiers |= FORMAT_EXPANDTIME; |
6245 | 6.12k | break; |
6246 | 2.27k | case 'N': |
6247 | 2.27k | if (fm->argc < 1 || |
6248 | 1.82k | strchr(fm->argv[0], 'w') != NULL) |
6249 | 871 | modifiers |= FORMAT_WINDOW_NAME; |
6250 | 1.40k | else if (strchr(fm->argv[0], 's') != NULL) |
6251 | 690 | modifiers |= FORMAT_SESSION_NAME; |
6252 | 2.27k | break; |
6253 | 37.2k | case 'S': |
6254 | 37.2k | modifiers |= FORMAT_SESSIONS; |
6255 | 37.2k | if (fm->argc < 1) { |
6256 | 8.95k | sc->order= SORT_INDEX; |
6257 | 8.95k | sc->reversed = 0; |
6258 | 8.95k | break; |
6259 | 8.95k | } |
6260 | 28.3k | if (strchr(fm->argv[0], 'i') != NULL) |
6261 | 552 | sc->order = SORT_INDEX; |
6262 | 27.7k | else if (strchr(fm->argv[0], 'n') != NULL) |
6263 | 629 | sc->order = SORT_NAME; |
6264 | 27.1k | else if (strchr(fm->argv[0], 't') != NULL) |
6265 | 582 | sc->order = SORT_ACTIVITY; |
6266 | 26.5k | else |
6267 | 26.5k | sc->order = SORT_INDEX; |
6268 | 28.3k | if (strchr(fm->argv[0], 'r') != NULL) |
6269 | 18.0k | sc->reversed = 1; |
6270 | 10.2k | else |
6271 | 10.2k | sc->reversed = 0; |
6272 | 28.3k | break; |
6273 | 3.53k | case 'W': |
6274 | 3.53k | modifiers |= FORMAT_WINDOWS; |
6275 | 3.53k | if (fm->argc < 1) { |
6276 | 1.07k | sc->order = SORT_ORDER; |
6277 | 1.07k | sc->reversed = 0; |
6278 | 1.07k | break; |
6279 | 1.07k | } |
6280 | 2.46k | if (strchr(fm->argv[0], 'i') != NULL) |
6281 | 686 | sc->order = SORT_ORDER; |
6282 | 1.77k | else if (strchr(fm->argv[0], 'n') != NULL) |
6283 | 436 | sc->order = SORT_NAME; |
6284 | 1.33k | else if (strchr(fm->argv[0], 't') != NULL) |
6285 | 529 | sc->order = SORT_ACTIVITY; |
6286 | 809 | else |
6287 | 809 | sc->order = SORT_ORDER; |
6288 | 2.46k | if (strchr(fm->argv[0], 'r') != NULL) |
6289 | 663 | sc->reversed = 1; |
6290 | 1.79k | else |
6291 | 1.79k | sc->reversed = 0; |
6292 | 2.46k | break; |
6293 | 51.4k | case 'P': |
6294 | 51.4k | modifiers |= FORMAT_PANES; |
6295 | 51.4k | sc->order = SORT_CREATION; |
6296 | 51.4k | if (fm->argc < 1) { |
6297 | 841 | sc->reversed = 0; |
6298 | 841 | break; |
6299 | 841 | } |
6300 | 50.5k | if (strchr(fm->argv[0], 'i') != NULL) |
6301 | 4.20k | sc->order = SORT_INDEX; |
6302 | 46.3k | else if (strchr(fm->argv[0], 'z') != NULL) |
6303 | 498 | sc->order = SORT_Z; |
6304 | 45.8k | else |
6305 | 45.8k | sc->order = SORT_CREATION; |
6306 | 50.5k | if (strchr(fm->argv[0], 'r') != NULL) |
6307 | 1.39k | sc->reversed = 1; |
6308 | 49.1k | else |
6309 | 49.1k | sc->reversed = 0; |
6310 | 50.5k | break; |
6311 | 91.5k | case 'O': |
6312 | 91.5k | modifiers |= FORMAT_OPTIONS; |
6313 | 91.5k | if (fm->argc == 1) |
6314 | 90.7k | loop_flags = fm->argv[0]; |
6315 | 91.5k | break; |
6316 | 298k | case 'V': |
6317 | 298k | modifiers |= FORMAT_ENVIRON; |
6318 | 298k | if (fm->argc == 1) |
6319 | 180k | loop_flags = fm->argv[0]; |
6320 | 298k | break; |
6321 | 3.30k | case 'L': |
6322 | 3.30k | modifiers |= FORMAT_CLIENTS; |
6323 | 3.30k | if (fm->argc < 1) { |
6324 | 758 | sc->order = SORT_ORDER; |
6325 | 758 | sc->reversed = 0; |
6326 | 758 | break; |
6327 | 758 | } |
6328 | 2.54k | if (strchr(fm->argv[0], 'i') != NULL) |
6329 | 420 | sc->order = SORT_ORDER; |
6330 | 2.12k | else if (strchr(fm->argv[0], 'n') != NULL) |
6331 | 687 | sc->order = SORT_NAME; |
6332 | 1.44k | else if (strchr(fm->argv[0], 't') != NULL) |
6333 | 404 | sc->order = SORT_ACTIVITY; |
6334 | 1.03k | else |
6335 | 1.03k | sc->order = SORT_ORDER; |
6336 | 2.54k | if (strchr(fm->argv[0], 'r') != NULL) |
6337 | 476 | sc->reversed = 1; |
6338 | 2.07k | else |
6339 | 2.07k | sc->reversed = 0; |
6340 | 2.54k | break; |
6341 | 571k | case 'R': |
6342 | 571k | modifiers |= FORMAT_REPEAT; |
6343 | 571k | break; |
6344 | 2.10M | } |
6345 | 2.10M | } else if (fm->size == 2) { |
6346 | 56.5k | if (strcmp(fm->modifier, "||") == 0 || |
6347 | 29.9k | strcmp(fm->modifier, "&&") == 0) |
6348 | 30.7k | bool_op_n = fm; |
6349 | 25.8k | else if (strcmp(fm->modifier, "!!") == 0) |
6350 | 473 | modifiers |= FORMAT_NOT_NOT; |
6351 | 25.3k | else if (strcmp(fm->modifier, "==") == 0 || |
6352 | 15.2k | strcmp(fm->modifier, "!=") == 0 || |
6353 | 6.86k | strcmp(fm->modifier, ">=") == 0 || |
6354 | 5.60k | strcmp(fm->modifier, "<=") == 0) |
6355 | 25.3k | cmp = fm; |
6356 | 56.5k | } |
6357 | 2.15M | } |
6358 | | |
6359 | | /* Look up client capability, feature or environment. */ |
6360 | 6.21M | if ((modifiers & FORMAT_CLIENT_TERMCAP) || |
6361 | 6.21M | (modifiers & FORMAT_CLIENT_TERMFEAT) || |
6362 | 6.21M | (modifiers & FORMAT_CLIENT_ENVIRON)) { |
6363 | 1.68k | if (ft->c == NULL || |
6364 | 0 | ft->c->tty.term == NULL || |
6365 | 1.68k | ft->c->flags & CLIENT_UNATTACHEDFLAGS) { |
6366 | 1.68k | value = xstrdup(""); |
6367 | 1.68k | goto done; |
6368 | 1.68k | } |
6369 | 0 | if (modifiers & FORMAT_CLIENT_TERMCAP) { |
6370 | 0 | if (tty_term_has_name(ft->c->tty.term, copy)) |
6371 | 0 | value = xstrdup("1"); |
6372 | 0 | else |
6373 | 0 | value = xstrdup("0"); |
6374 | 0 | } |
6375 | 0 | if (modifiers & FORMAT_CLIENT_TERMFEAT) { |
6376 | 0 | if (tty_feature_present(ft->c->tty.term, copy)) |
6377 | 0 | value = xstrdup("1"); |
6378 | 0 | else |
6379 | 0 | value = xstrdup("0"); |
6380 | 0 | } |
6381 | 0 | if (modifiers & FORMAT_CLIENT_ENVIRON) { |
6382 | 0 | envent = environ_find(ft->c->environ, copy); |
6383 | 0 | if (envent != NULL && envent->value != NULL) |
6384 | 0 | value = xstrdup(envent->value); |
6385 | 0 | else |
6386 | 0 | value = xstrdup(""); |
6387 | 0 | } |
6388 | 0 | goto done; |
6389 | 1.68k | } |
6390 | | |
6391 | | /* Is this an animation cycle? */ |
6392 | 6.21M | if (modifiers & FORMAT_CYCLE) { |
6393 | 19.2k | value = format_cycle(es, copy, cycle_count); |
6394 | 19.2k | format_log(es, "cycle '%s' is: %s", copy, value); |
6395 | 19.2k | goto done; |
6396 | 19.2k | } |
6397 | | |
6398 | | /* Is this a literal string? */ |
6399 | 6.19M | if (modifiers & FORMAT_LITERAL) { |
6400 | 2.26k | format_log(es, "literal string is '%s'", copy); |
6401 | 2.26k | value = format_unescape(es, copy, strlen(copy)); |
6402 | 2.26k | goto done; |
6403 | 2.26k | } |
6404 | | |
6405 | | /* Is this a character? */ |
6406 | 6.18M | if (modifiers & FORMAT_CHARACTER) { |
6407 | 4.91k | new = format_expand1(es, copy); |
6408 | 4.91k | c = strtonum(new, 32, 126, &errstr); |
6409 | 4.91k | if (errstr != NULL) |
6410 | 4.46k | value = xstrdup(""); |
6411 | 451 | else |
6412 | 451 | xasprintf(&value, "%c", c); |
6413 | 4.91k | free(new); |
6414 | 4.91k | goto done; |
6415 | 4.91k | } |
6416 | | |
6417 | | /* Is this a colour? */ |
6418 | 6.18M | if (modifiers & FORMAT_COLOUR) { |
6419 | 163k | new = format_expand1(es, copy); |
6420 | 163k | if (modifiers & (FORMAT_COLOUR_ESC_FG|FORMAT_COLOUR_ESC_BG)) { |
6421 | 23.7k | if (strcasecmp(new, "none") == 0) |
6422 | 396 | value = xstrdup("\033[0m"); |
6423 | 23.3k | else if ((c = colour_fromstring(new)) == -1) |
6424 | 5.91k | value = xstrdup(""); |
6425 | 17.4k | else { |
6426 | 17.4k | if (modifiers & FORMAT_COLOUR_ESC_BG) |
6427 | 15.9k | cp = colour_toescape(ft->c, c, 1); |
6428 | 1.51k | else |
6429 | 1.51k | cp = colour_toescape(ft->c, c, 0); |
6430 | 17.4k | if (cp == NULL) |
6431 | 0 | value = xstrdup(""); |
6432 | 17.4k | else |
6433 | 17.4k | value = xstrdup(cp); |
6434 | 17.4k | } |
6435 | 139k | } else { |
6436 | 139k | c = colour_fromstring(new); |
6437 | 139k | if (c == -1 || (c = colour_force_rgb(c)) == -1) |
6438 | 16.2k | value = xstrdup(""); |
6439 | 123k | else |
6440 | 123k | xasprintf(&value, "%06x", c & 0xffffff); |
6441 | 139k | } |
6442 | 163k | free(new); |
6443 | 163k | goto done; |
6444 | 163k | } |
6445 | | |
6446 | | /* Is this a loop, operator, comparison or condition? */ |
6447 | 6.02M | if (modifiers & FORMAT_SESSIONS) { |
6448 | 37.2k | value = format_loop_sessions(es, copy); |
6449 | 37.2k | if (value == NULL) |
6450 | 0 | goto fail; |
6451 | 5.98M | } else if (modifiers & FORMAT_WINDOWS) { |
6452 | 3.02k | value = format_loop_windows(es, copy); |
6453 | 3.02k | if (value == NULL) |
6454 | 3.02k | goto fail; |
6455 | 5.98M | } else if (modifiers & FORMAT_PANES) { |
6456 | 51.3k | value = format_loop_panes(es, copy); |
6457 | 51.3k | if (value == NULL) |
6458 | 51.3k | goto fail; |
6459 | 5.92M | } else if (modifiers & FORMAT_CLIENTS) { |
6460 | 3.22k | value = format_loop_clients(es, copy); |
6461 | 3.22k | if (value == NULL) |
6462 | 0 | goto fail; |
6463 | 5.92M | } else if (modifiers & FORMAT_OPTIONS) { |
6464 | 91.0k | value = format_loop_options(es, copy, loop_flags); |
6465 | 91.0k | if (value == NULL) |
6466 | 0 | goto fail; |
6467 | 5.83M | } else if (modifiers & FORMAT_ENVIRON) { |
6468 | 297k | value = format_loop_environ(es, copy, loop_flags); |
6469 | 297k | if (value == NULL) |
6470 | 0 | goto fail; |
6471 | 5.53M | } else if (modifiers & FORMAT_WINDOW_NAME) { |
6472 | 697 | value = format_window_name(es, copy); |
6473 | 697 | if (value == NULL) |
6474 | 697 | goto fail; |
6475 | 5.53M | } else if (modifiers & FORMAT_SESSION_NAME) { |
6476 | 690 | value = format_session_name(es, copy); |
6477 | 690 | if (value == NULL) |
6478 | 0 | goto fail; |
6479 | 5.53M | } else if (search != NULL) { |
6480 | | /* Search in pane. */ |
6481 | 7.70k | new = format_expand1(es, copy); |
6482 | 7.70k | if (wp == NULL) { |
6483 | 7.70k | format_log(es, "search '%s' but no pane", new); |
6484 | 7.70k | value = xstrdup("0"); |
6485 | 7.70k | } else { |
6486 | 0 | format_log(es, "search '%s' pane %%%u", new, wp->id); |
6487 | 0 | value = format_search(search, wp, new); |
6488 | 0 | } |
6489 | 7.70k | free(new); |
6490 | 5.52M | } else if (modifiers & FORMAT_REPEAT) { |
6491 | | /* Repeat multiple times. */ |
6492 | 571k | if (format_choose(es, copy, &left, &right, 1) != 0) { |
6493 | 20.8k | format_log(es, "repeat syntax error: %s", copy); |
6494 | 20.8k | goto fail; |
6495 | 20.8k | } |
6496 | 550k | nrep = strtonum(right, 1, FORMAT_MAX_REPEAT, &errstr); |
6497 | 550k | if (errstr != NULL) |
6498 | 71.7k | value = xstrdup(""); |
6499 | 478k | else { |
6500 | 478k | n = strlen(left); |
6501 | 478k | if (n != 0 && nrep > FORMAT_MAX_REPEAT_SIZE / n) { |
6502 | 4.45k | format_log(es, "repeat is too long: %s", copy); |
6503 | 4.45k | value = xstrdup(""); |
6504 | 474k | } else { |
6505 | 474k | value = xmalloc((nrep * n) + 1); |
6506 | 396M | for (i = 0; i < nrep; i++) |
6507 | 396M | memcpy(value + (i * n), left, n); |
6508 | 474k | value[nrep * n] = '\0'; |
6509 | 474k | } |
6510 | 478k | } |
6511 | 550k | free(right); |
6512 | 550k | free(left); |
6513 | 4.95M | } else if (modifiers & FORMAT_NOT) { |
6514 | 54.0k | value = format_bool_op_1(es, copy, 1); |
6515 | 4.90M | } else if (modifiers & FORMAT_NOT_NOT) { |
6516 | 473 | value = format_bool_op_1(es, copy, 0); |
6517 | 4.90M | } else if (bool_op_n != NULL) { |
6518 | | /* n-ary boolean operator. */ |
6519 | 30.7k | if (strcmp(bool_op_n->modifier, "||") == 0) |
6520 | 26.6k | value = format_bool_op_n(es, copy, 0); |
6521 | 4.08k | else if (strcmp(bool_op_n->modifier, "&&") == 0) |
6522 | 4.08k | value = format_bool_op_n(es, copy, 1); |
6523 | 4.87M | } else if (cmp != NULL) { |
6524 | | /* Comparison of left and right. */ |
6525 | 212k | if (format_choose(es, copy, &left, &right, 1) != 0) { |
6526 | 24.1k | format_log(es, "compare %s syntax error: %s", |
6527 | 24.1k | cmp->modifier, copy); |
6528 | 24.1k | goto fail; |
6529 | 24.1k | } |
6530 | 188k | format_log(es, "compare %s left is: %s", cmp->modifier, left); |
6531 | 188k | format_log(es, "compare %s right is: %s", cmp->modifier, right); |
6532 | | |
6533 | 188k | if (strcmp(cmp->modifier, "==") == 0) { |
6534 | 9.17k | if (strcmp(left, right) == 0) |
6535 | 455 | value = xstrdup("1"); |
6536 | 8.72k | else |
6537 | 8.72k | value = xstrdup("0"); |
6538 | 179k | } else if (strcmp(cmp->modifier, "!=") == 0) { |
6539 | 8.21k | if (strcmp(left, right) != 0) |
6540 | 7.40k | value = xstrdup("1"); |
6541 | 808 | else |
6542 | 808 | value = xstrdup("0"); |
6543 | 170k | } else if (strcmp(cmp->modifier, "<") == 0) { |
6544 | 1.07k | if (strcmp(left, right) < 0) |
6545 | 578 | value = xstrdup("1"); |
6546 | 499 | else |
6547 | 499 | value = xstrdup("0"); |
6548 | 169k | } else if (strcmp(cmp->modifier, ">") == 0) { |
6549 | 1.06k | if (strcmp(left, right) > 0) |
6550 | 528 | value = xstrdup("1"); |
6551 | 536 | else |
6552 | 536 | value = xstrdup("0"); |
6553 | 168k | } else if (strcmp(cmp->modifier, "<=") == 0) { |
6554 | 5.29k | if (strcmp(left, right) <= 0) |
6555 | 548 | value = xstrdup("1"); |
6556 | 4.75k | else |
6557 | 4.75k | value = xstrdup("0"); |
6558 | 163k | } else if (strcmp(cmp->modifier, ">=") == 0) { |
6559 | 1.18k | if (strcmp(left, right) >= 0) |
6560 | 619 | value = xstrdup("1"); |
6561 | 562 | else |
6562 | 562 | value = xstrdup("0"); |
6563 | 162k | } else if (strcmp(cmp->modifier, "m") == 0) |
6564 | 162k | value = format_match(cmp, left, right); |
6565 | | |
6566 | 188k | free(right); |
6567 | 188k | free(left); |
6568 | 4.65M | } else if (*copy == '?') { |
6569 | | /* |
6570 | | * Conditional: For each pair of (condition, value), check the |
6571 | | * condition and return the value if true. If no condition |
6572 | | * matches, return the last unpaired arg if there is one, or the |
6573 | | * empty string if not. |
6574 | | */ |
6575 | 146k | cp = copy + 1; |
6576 | 203k | while (1) { |
6577 | 203k | cp2 = format_skip1(es, cp, ","); |
6578 | 203k | if (cp2 == NULL) { |
6579 | 97.1k | format_log(es, |
6580 | 97.1k | "no condition matched in '%s'; using last " |
6581 | 97.1k | "arg", copy + 1); |
6582 | 97.1k | value = format_expand1(es, cp); |
6583 | 97.1k | break; |
6584 | 97.1k | } |
6585 | | |
6586 | 106k | condition = xstrndup(cp, cp2 - cp); |
6587 | 106k | format_log(es, "condition is: %s", condition); |
6588 | | |
6589 | 106k | found = format_find(ft, condition, modifiers, |
6590 | 106k | time_format); |
6591 | 106k | if (found == NULL) { |
6592 | | /* |
6593 | | * If the condition not found, try to expand it. |
6594 | | * If the expansion doesn't have any effect, |
6595 | | * then assume false. |
6596 | | */ |
6597 | 105k | found = format_expand1(es, condition); |
6598 | 105k | if (strcmp(found, condition) == 0) { |
6599 | 81.1k | free(found); |
6600 | 81.1k | found = xstrdup(""); |
6601 | 81.1k | format_log(es, |
6602 | 81.1k | "condition '%s' not found; " |
6603 | 81.1k | "assuming false", |
6604 | 81.1k | condition); |
6605 | 81.1k | } |
6606 | 105k | } else { |
6607 | 1.37k | format_log(es, "condition '%s' found: %s", |
6608 | 1.37k | condition, found); |
6609 | 1.37k | } |
6610 | | |
6611 | 106k | cp = cp2 + 1; |
6612 | 106k | cp2 = format_skip1(es, cp, ","); |
6613 | 106k | if (format_true(found)) { |
6614 | 21.5k | format_log(es, "condition '%s' is true", |
6615 | 21.5k | condition); |
6616 | 21.5k | if (cp2 == NULL) |
6617 | 20.2k | value = format_expand1(es, cp); |
6618 | 1.23k | else { |
6619 | 1.23k | right = xstrndup(cp, cp2 - cp); |
6620 | 1.23k | value = format_expand1(es, right); |
6621 | 1.23k | free(right); |
6622 | 1.23k | } |
6623 | 21.5k | free(condition); |
6624 | 21.5k | free(found); |
6625 | 21.5k | break; |
6626 | 85.1k | } else { |
6627 | 85.1k | format_log(es, "condition '%s' is false", |
6628 | 85.1k | condition); |
6629 | 85.1k | } |
6630 | | |
6631 | 85.1k | free(condition); |
6632 | 85.1k | free(found); |
6633 | | |
6634 | 85.1k | if (cp2 == NULL) { |
6635 | 28.1k | format_log(es, |
6636 | 28.1k | "no condition matched in '%s'; using empty " |
6637 | 28.1k | "string", copy + 1); |
6638 | 28.1k | value = xstrdup(""); |
6639 | 28.1k | break; |
6640 | 28.1k | } |
6641 | | |
6642 | 57.0k | cp = cp2 + 1; |
6643 | 57.0k | } |
6644 | 4.51M | } else if (mexp != NULL) { |
6645 | 100k | value = format_replace_expression(mexp, es, copy); |
6646 | 100k | if (value == NULL) |
6647 | 81.2k | value = xstrdup(""); |
6648 | 4.41M | } else { |
6649 | 4.41M | if (strstr(copy, "#{") != 0) { |
6650 | 947k | format_log(es, "expanding inner format '%s'", copy); |
6651 | 947k | value = format_expand1(es, copy); |
6652 | 3.46M | } else { |
6653 | 3.46M | value = format_find(ft, copy, modifiers, time_format); |
6654 | 3.46M | if (value == NULL) { |
6655 | 2.74M | format_log(es, "format '%s' not found", copy); |
6656 | 2.74M | value = xstrdup(""); |
6657 | 2.74M | } else { |
6658 | 716k | format_log(es, "format '%s' found: %s", copy, |
6659 | 716k | value); |
6660 | 716k | } |
6661 | 3.46M | } |
6662 | 4.41M | } |
6663 | | |
6664 | 6.11M | done: |
6665 | | /* Expand again if required. */ |
6666 | 6.11M | if (modifiers & FORMAT_EXPAND) { |
6667 | 35.4k | new = format_expand1(es, value); |
6668 | 35.4k | free(value); |
6669 | 35.4k | value = new; |
6670 | 6.07M | } else if (modifiers & FORMAT_EXPANDTIME) { |
6671 | 6.12k | format_copy_state(&next, es, FORMAT_EXPAND_TIME); |
6672 | 6.12k | new = format_expand1(&next, value); |
6673 | 6.12k | free(value); |
6674 | 6.12k | value = new; |
6675 | 6.12k | } |
6676 | | |
6677 | | /* Perform substitution if any. */ |
6678 | 6.12M | for (i = 0; i < nsub; i++) { |
6679 | 16.9k | left = format_expand1(es, sub[i]->argv[0]); |
6680 | 16.9k | right = format_expand1(es, sub[i]->argv[1]); |
6681 | 16.9k | new = format_sub(sub[i], value, left, right); |
6682 | 16.9k | format_log(es, "substitute '%s' to '%s': %s", left, right, new); |
6683 | 16.9k | free(value); |
6684 | 16.9k | value = new; |
6685 | 16.9k | free(right); |
6686 | 16.9k | free(left); |
6687 | 16.9k | } |
6688 | | |
6689 | | /* Truncate the value if needed. */ |
6690 | 6.11M | if (limit > 0) { |
6691 | 7.36k | new = format_trim_left(value, limit); |
6692 | 7.36k | if (marker != NULL && strcmp(new, value) != 0) { |
6693 | 1.94k | free(value); |
6694 | 1.94k | xasprintf(&value, "%s%s", new, marker); |
6695 | 1.94k | free(new); |
6696 | 5.41k | } else { |
6697 | 5.41k | free(value); |
6698 | 5.41k | value = new; |
6699 | 5.41k | } |
6700 | 7.36k | format_log(es, "applied length limit %d: %s", limit, value); |
6701 | 6.10M | } else if (limit < 0) { |
6702 | 5.73k | new = format_trim_right(value, -limit); |
6703 | 5.73k | if (marker != NULL && strcmp(new, value) != 0) { |
6704 | 1.50k | free(value); |
6705 | 1.50k | xasprintf(&value, "%s%s", marker, new); |
6706 | 1.50k | free(new); |
6707 | 4.22k | } else { |
6708 | 4.22k | free(value); |
6709 | 4.22k | value = new; |
6710 | 4.22k | } |
6711 | 5.73k | format_log(es, "applied length limit %d: %s", limit, value); |
6712 | 5.73k | } |
6713 | | |
6714 | | /* Pad the value if needed. */ |
6715 | 6.11M | if (width > 0) { |
6716 | 135k | new = utf8_padcstr(value, width); |
6717 | 135k | free(value); |
6718 | 135k | value = new; |
6719 | 135k | format_log(es, "applied padding width %d: %s", width, value); |
6720 | 5.97M | } else if (width < 0) { |
6721 | 64.5k | new = utf8_rpadcstr(value, -width); |
6722 | 64.5k | free(value); |
6723 | 64.5k | value = new; |
6724 | 64.5k | format_log(es, "applied padding width %d: %s", width, value); |
6725 | 64.5k | } |
6726 | | |
6727 | | /* Replace with the length or width if needed. */ |
6728 | 6.11M | if (modifiers & FORMAT_LENGTH) { |
6729 | 12.8k | xasprintf(&new, "%zu", strlen(value)); |
6730 | 12.8k | free(value); |
6731 | 12.8k | value = new; |
6732 | 12.8k | format_log(es, "replacing with length: %s", new); |
6733 | 12.8k | } |
6734 | 6.11M | if (modifiers & FORMAT_WIDTH) { |
6735 | 1.66k | xasprintf(&new, "%u", format_width(value)); |
6736 | 1.66k | free(value); |
6737 | 1.66k | value = new; |
6738 | 1.66k | format_log(es, "replacing with width: %s", new); |
6739 | 1.66k | } |
6740 | | |
6741 | | /* Expand the buffer and copy in the value. */ |
6742 | 6.11M | n = strlen(value); |
6743 | 10.8M | while (*len - *off < n + 1) { |
6744 | 4.73M | *buf = xreallocarray(*buf, 2, *len); |
6745 | 4.73M | *len *= 2; |
6746 | 4.73M | } |
6747 | 6.11M | memcpy(*buf + *off, value, n); |
6748 | 6.11M | *off += n; |
6749 | | |
6750 | 6.11M | format_log(es, "replaced '%s' with '%s'", copy0, value); |
6751 | 6.11M | free(value); |
6752 | | |
6753 | 6.11M | free(sub); |
6754 | 6.11M | format_free_modifiers(list, count); |
6755 | 6.11M | free(copy0); |
6756 | 6.11M | free(time_format); |
6757 | 6.11M | return (0); |
6758 | | |
6759 | 100k | fail: |
6760 | 100k | format_log(es, "failed %s", copy0); |
6761 | | |
6762 | 100k | free(sub); |
6763 | 100k | format_free_modifiers(list, count); |
6764 | 100k | free(copy0); |
6765 | 100k | free(time_format); |
6766 | 100k | return (-1); |
6767 | 6.02M | } |
6768 | | |
6769 | | /* Expand keys in a template. */ |
6770 | | static char * |
6771 | | format_expand1(struct format_expand_state *es, const char *fmt) |
6772 | 11.5M | { |
6773 | 11.5M | struct format_tree *ft = es->ft; |
6774 | 11.5M | char *buf, *out, *name; |
6775 | 11.5M | const char *ptr, *s, *style_end = NULL; |
6776 | 11.5M | size_t off, len, n, outlen; |
6777 | 11.5M | int ch, brackets; |
6778 | 11.5M | char expanded[8192]; |
6779 | | |
6780 | 11.5M | if (fmt == NULL || *fmt == '\0' || !format_check_time(es, NULL)) |
6781 | 1.49M | return (xstrdup("")); |
6782 | | |
6783 | 10.0M | if (es->loop == FORMAT_LOOP_LIMIT) { |
6784 | 113k | format_log(es, "reached loop limit (%u)", FORMAT_LOOP_LIMIT); |
6785 | 113k | return (xstrdup("")); |
6786 | 113k | } |
6787 | 9.91M | es->loop++; |
6788 | | |
6789 | 9.91M | format_log(es, "expanding format: %s", fmt); |
6790 | | |
6791 | 9.91M | if ((es->flags & FORMAT_EXPAND_TIME) && strchr(fmt, '%') != NULL) { |
6792 | 2.20k | if (es->time == 0) { |
6793 | 1.58k | es->time = time(NULL); |
6794 | 1.58k | localtime_r(&es->time, &es->tm); |
6795 | 1.58k | } |
6796 | 2.20k | if (format_strftime(expanded, sizeof expanded, fmt, |
6797 | 2.20k | &es->tm) == 0) { |
6798 | 397 | format_log(es, "format is too long"); |
6799 | 397 | return (xstrdup("")); |
6800 | 397 | } |
6801 | 1.80k | if (format_logging(ft) && strcmp(expanded, fmt) != 0) |
6802 | 0 | format_log(es, "after time expanded: %s", expanded); |
6803 | 1.80k | fmt = expanded; |
6804 | 1.80k | } |
6805 | | |
6806 | 9.91M | len = 64; |
6807 | 9.91M | buf = xmalloc(len); |
6808 | 9.91M | off = 0; |
6809 | | |
6810 | 265M | while (*fmt != '\0') { |
6811 | 256M | if (*fmt != '#') { |
6812 | 238M | while (len - off < 2) { |
6813 | 707k | buf = xreallocarray(buf, 2, len); |
6814 | 707k | len *= 2; |
6815 | 707k | } |
6816 | 238M | buf[off++] = *fmt++; |
6817 | 238M | continue; |
6818 | 238M | } |
6819 | 18.0M | if (*++fmt == '\0') |
6820 | 12.3k | break; |
6821 | | |
6822 | 18.0M | ch = (u_char)*fmt++; |
6823 | 18.0M | switch (ch) { |
6824 | 46.5k | case '(': |
6825 | 46.5k | brackets = 1; |
6826 | 643k | for (ptr = fmt; *ptr != '\0'; ptr++) { |
6827 | 628k | if (*ptr == '(') |
6828 | 106k | brackets++; |
6829 | 628k | if (*ptr == ')' && --brackets == 0) |
6830 | 31.8k | break; |
6831 | 628k | } |
6832 | 46.5k | if (*ptr != ')' || brackets != 0) |
6833 | 14.7k | break; |
6834 | 31.8k | n = ptr - fmt; |
6835 | | |
6836 | 31.8k | name = xstrndup(fmt, n); |
6837 | 31.8k | format_log(es, "found #(): %s", name); |
6838 | | |
6839 | 31.8k | if ((ft->flags & FORMAT_NOJOBS) || |
6840 | 31.8k | (es->flags & FORMAT_EXPAND_NOJOBS)) { |
6841 | 31.8k | out = xstrdup(""); |
6842 | 31.8k | format_log(es, "#() is disabled"); |
6843 | 31.8k | } else { |
6844 | 0 | out = format_job_get(es, name); |
6845 | 0 | format_log(es, "#() result: %s", out); |
6846 | 0 | } |
6847 | 31.8k | free(name); |
6848 | | |
6849 | 31.8k | outlen = strlen(out); |
6850 | 31.8k | while (len - off < outlen + 1) { |
6851 | 0 | buf = xreallocarray(buf, 2, len); |
6852 | 0 | len *= 2; |
6853 | 0 | } |
6854 | 31.8k | memcpy(buf + off, out, outlen); |
6855 | 31.8k | off += outlen; |
6856 | | |
6857 | 31.8k | free(out); |
6858 | | |
6859 | 31.8k | fmt += n + 1; |
6860 | 31.8k | continue; |
6861 | 4.68M | case '{': |
6862 | 4.68M | ptr = format_skip1(es, (char *)fmt - 2, "}"); |
6863 | 4.68M | if (ptr == NULL) |
6864 | 1.43k | break; |
6865 | 4.67M | n = ptr - fmt; |
6866 | | |
6867 | 4.67M | format_log(es, "found #{}: %.*s", (int)n, fmt); |
6868 | 4.67M | if (format_replace(es, fmt, n, &buf, &len, &off) != 0) |
6869 | 100k | break; |
6870 | 4.57M | fmt += n + 1; |
6871 | 4.57M | continue; |
6872 | 1.79M | case '[': |
6873 | 6.61M | case '#': |
6874 | | /* |
6875 | | * If ##[ (with two or more #s), then it is a style and |
6876 | | * can be left for format_draw to handle. |
6877 | | */ |
6878 | 6.61M | ptr = fmt - (ch == '['); |
6879 | 6.61M | n = 2 - (ch == '['); |
6880 | 9.36M | while (*ptr == '#') { |
6881 | 2.74M | ptr++; |
6882 | 2.74M | n++; |
6883 | 2.74M | } |
6884 | 6.61M | if (*ptr == '[') { |
6885 | 3.02M | style_end = format_skip1(es, fmt - 2, "]"); |
6886 | 3.02M | format_log(es, "found #*%zu[", n); |
6887 | 3.02M | while (len - off < n + 2) { |
6888 | 7.29k | buf = xreallocarray(buf, 2, len); |
6889 | 7.29k | len *= 2; |
6890 | 7.29k | } |
6891 | 3.02M | memcpy(buf + off, fmt - 2, n + 1); |
6892 | 3.02M | off += n + 1; |
6893 | 3.02M | fmt = ptr + 1; |
6894 | 3.02M | continue; |
6895 | 3.02M | } |
6896 | 3.59M | if (ch == '#') { |
6897 | 3.66M | while (len - off < (n / 2) + 1) { |
6898 | 69.0k | buf = xreallocarray(buf, 2, len); |
6899 | 69.0k | len *= 2; |
6900 | 69.0k | } |
6901 | 3.59M | memset(buf + off, '#', n / 2); |
6902 | 3.59M | off += (n / 2); |
6903 | 3.59M | if (n % 2 != 0) |
6904 | 630k | fmt = ptr - 1; |
6905 | 2.96M | else |
6906 | 2.96M | fmt = ptr; |
6907 | 3.59M | continue; |
6908 | 3.59M | } |
6909 | | /* FALLTHROUGH */ |
6910 | 215k | case '}': |
6911 | 243k | case ',': |
6912 | 243k | format_log(es, "found #%c", ch); |
6913 | 243k | while (len - off < 2) { |
6914 | 32 | buf = xreallocarray(buf, 2, len); |
6915 | 32 | len *= 2; |
6916 | 32 | } |
6917 | 243k | buf[off++] = ch; |
6918 | 243k | continue; |
6919 | 6.43M | default: |
6920 | 6.43M | s = NULL; |
6921 | 6.43M | if (fmt > style_end) { /* skip inside #[] */ |
6922 | 6.05M | if (ch >= 'A' && ch <= 'Z') |
6923 | 1.57M | s = format_upper[ch - 'A']; |
6924 | 4.48M | else if (ch >= 'a' && ch <= 'z') |
6925 | 1.52M | s = format_lower[ch - 'a']; |
6926 | 6.05M | } |
6927 | 6.43M | if (s == NULL) { |
6928 | 4.92M | while (len - off < 3) { |
6929 | 22.8k | buf = xreallocarray(buf, 2, len); |
6930 | 22.8k | len *= 2; |
6931 | 22.8k | } |
6932 | 4.90M | buf[off++] = '#'; |
6933 | 4.90M | buf[off++] = ch; |
6934 | 4.90M | continue; |
6935 | 4.90M | } |
6936 | 1.53M | n = strlen(s); |
6937 | 1.53M | format_log(es, "found #%c: %s", ch, s); |
6938 | 1.53M | if (format_replace(es, s, n, &buf, &len, &off) != 0) |
6939 | 0 | break; |
6940 | 1.53M | continue; |
6941 | 18.0M | } |
6942 | | |
6943 | 116k | break; |
6944 | 18.0M | } |
6945 | 9.91M | buf[off] = '\0'; |
6946 | | |
6947 | 9.91M | format_log(es, "result is: %s", buf); |
6948 | 9.91M | es->loop--; |
6949 | | |
6950 | 9.91M | return (buf); |
6951 | 9.91M | } |
6952 | | |
6953 | | /* Expand keys in a template, passing through strftime first. */ |
6954 | | char * |
6955 | | format_expand_time(struct format_tree *ft, const char *fmt) |
6956 | 0 | { |
6957 | 0 | struct format_expand_state es; |
6958 | |
|
6959 | 0 | memset(&es, 0, sizeof es); |
6960 | 0 | es.ft = ft; |
6961 | 0 | es.flags = FORMAT_EXPAND_TIME; |
6962 | 0 | es.start_time = get_timer(); |
6963 | 0 | return (format_expand1(&es, fmt)); |
6964 | 0 | } |
6965 | | |
6966 | | /* Expand keys in a template. */ |
6967 | | char * |
6968 | | format_expand(struct format_tree *ft, const char *fmt) |
6969 | 84.3k | { |
6970 | 84.3k | struct format_expand_state es; |
6971 | | |
6972 | 84.3k | memset(&es, 0, sizeof es); |
6973 | 84.3k | es.ft = ft; |
6974 | 84.3k | es.flags = 0; |
6975 | 84.3k | es.start_time = get_timer(); |
6976 | 84.3k | return (format_expand1(&es, fmt)); |
6977 | 84.3k | } |
6978 | | |
6979 | | /* Expand a single string. */ |
6980 | | char * |
6981 | | format_single(struct cmdq_item *item, const char *fmt, struct client *c, |
6982 | | struct session *s, struct winlink *wl, struct window_pane *wp) |
6983 | 54.6k | { |
6984 | 54.6k | struct format_tree *ft; |
6985 | 54.6k | char *expanded; |
6986 | | |
6987 | 54.6k | ft = format_create_defaults(item, c, s, wl, wp); |
6988 | 54.6k | expanded = format_expand(ft, fmt); |
6989 | 54.6k | format_free(ft); |
6990 | 54.6k | return (expanded); |
6991 | 54.6k | } |
6992 | | |
6993 | | /* Expand a single string using state. */ |
6994 | | char * |
6995 | | format_single_from_state(struct cmdq_item *item, const char *fmt, |
6996 | | struct client *c, struct cmd_find_state *fs) |
6997 | 0 | { |
6998 | 0 | return (format_single(item, fmt, c, fs->s, fs->wl, fs->wp)); |
6999 | 0 | } |
7000 | | |
7001 | | /* Expand a single string using target. */ |
7002 | | char * |
7003 | | format_single_from_target(struct cmdq_item *item, const char *fmt) |
7004 | 0 | { |
7005 | 0 | struct client *tc = cmdq_get_target_client(item); |
7006 | |
|
7007 | 0 | return (format_single_from_state(item, fmt, tc, cmdq_get_target(item))); |
7008 | 0 | } |
7009 | | |
7010 | | /* Create and add defaults. */ |
7011 | | struct format_tree * |
7012 | | format_create_defaults(struct cmdq_item *item, struct client *c, |
7013 | | struct session *s, struct winlink *wl, struct window_pane *wp) |
7014 | 54.6k | { |
7015 | 54.6k | struct format_tree *ft; |
7016 | | |
7017 | 54.6k | if (item != NULL) |
7018 | 0 | ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0); |
7019 | 54.6k | else |
7020 | 54.6k | ft = format_create(NULL, item, FORMAT_NONE, 0); |
7021 | 54.6k | format_defaults(ft, c, s, wl, wp); |
7022 | 54.6k | return (ft); |
7023 | 54.6k | } |
7024 | | |
7025 | | /* Create and add defaults using state. */ |
7026 | | struct format_tree * |
7027 | | format_create_from_state(struct cmdq_item *item, struct client *c, |
7028 | | struct cmd_find_state *fs) |
7029 | 0 | { |
7030 | 0 | return (format_create_defaults(item, c, fs->s, fs->wl, fs->wp)); |
7031 | 0 | } |
7032 | | |
7033 | | /* Create and add defaults using target. */ |
7034 | | struct format_tree * |
7035 | | format_create_from_target(struct cmdq_item *item) |
7036 | 0 | { |
7037 | 0 | struct client *tc = cmdq_get_target_client(item); |
7038 | |
|
7039 | 0 | return (format_create_from_state(item, tc, cmdq_get_target(item))); |
7040 | 0 | } |
7041 | | |
7042 | | /* Set defaults for any of arguments that are not NULL. */ |
7043 | | void |
7044 | | format_defaults(struct format_tree *ft, struct client *c, struct session *s, |
7045 | | struct winlink *wl, struct window_pane *wp) |
7046 | 6.64M | { |
7047 | 6.64M | struct paste_buffer *pb; |
7048 | | |
7049 | 6.64M | if (c != NULL && c->name != NULL) |
7050 | 0 | log_debug("%s: c=%s", __func__, c->name); |
7051 | 6.64M | else |
7052 | 6.64M | log_debug("%s: c=none", __func__); |
7053 | 6.64M | if (s != NULL) |
7054 | 0 | log_debug("%s: s=$%u", __func__, s->id); |
7055 | 6.64M | else |
7056 | 6.64M | log_debug("%s: s=none", __func__); |
7057 | 6.64M | if (wl != NULL) |
7058 | 0 | log_debug("%s: wl=%u", __func__, wl->idx); |
7059 | 6.64M | else |
7060 | 6.64M | log_debug("%s: wl=none", __func__); |
7061 | 6.64M | if (wp != NULL) |
7062 | 3.99k | log_debug("%s: wp=%%%u", __func__, wp->id); |
7063 | 6.64M | else |
7064 | 6.64M | log_debug("%s: wp=none", __func__); |
7065 | | |
7066 | 6.64M | if (c != NULL && s != NULL && c->session != s) |
7067 | 0 | log_debug("%s: session does not match", __func__); |
7068 | | |
7069 | 6.64M | if (wp != NULL) |
7070 | 3.99k | ft->type = FORMAT_TYPE_PANE; |
7071 | 6.64M | else if (wl != NULL) |
7072 | 0 | ft->type = FORMAT_TYPE_WINDOW; |
7073 | 6.64M | else if (s != NULL) |
7074 | 0 | ft->type = FORMAT_TYPE_SESSION; |
7075 | 6.64M | else |
7076 | 6.64M | ft->type = FORMAT_TYPE_UNKNOWN; |
7077 | | |
7078 | 6.64M | if (s == NULL && c != NULL) |
7079 | 0 | s = c->session; |
7080 | 6.64M | if (wl == NULL && s != NULL) |
7081 | 0 | wl = s->curw; |
7082 | 6.64M | if (wp == NULL && wl != NULL) |
7083 | 0 | wp = wl->window->active; |
7084 | | |
7085 | 6.64M | if (c != NULL) |
7086 | 0 | format_defaults_client(ft, c); |
7087 | 6.64M | if (s != NULL) |
7088 | 0 | format_defaults_session(ft, s); |
7089 | 6.64M | if (wl != NULL) |
7090 | 0 | format_defaults_winlink(ft, wl); |
7091 | 6.64M | if (wp != NULL) |
7092 | 3.99k | format_defaults_pane(ft, wp); |
7093 | | |
7094 | 6.64M | pb = paste_get_top(NULL); |
7095 | 6.64M | if (pb != NULL) |
7096 | 19.6k | format_defaults_paste_buffer(ft, pb); |
7097 | 6.64M | } |
7098 | | |
7099 | | /* Set default format keys for a session. */ |
7100 | | static void |
7101 | | format_defaults_session(struct format_tree *ft, struct session *s) |
7102 | 0 | { |
7103 | 0 | ft->s = s; |
7104 | 0 | } |
7105 | | |
7106 | | /* Set default format keys for a client. */ |
7107 | | static void |
7108 | | format_defaults_client(struct format_tree *ft, struct client *c) |
7109 | 0 | { |
7110 | 0 | if (ft->s == NULL) |
7111 | 0 | ft->s = c->session; |
7112 | 0 | ft->c = c; |
7113 | 0 | } |
7114 | | |
7115 | | /* Set default format keys for a window. */ |
7116 | | void |
7117 | | format_defaults_window(struct format_tree *ft, struct window *w) |
7118 | 3.99k | { |
7119 | 3.99k | ft->w = w; |
7120 | 3.99k | } |
7121 | | |
7122 | | /* Set default format keys for a winlink. */ |
7123 | | static void |
7124 | | format_defaults_winlink(struct format_tree *ft, struct winlink *wl) |
7125 | 0 | { |
7126 | 0 | if (ft->w == NULL) |
7127 | 0 | format_defaults_window(ft, wl->window); |
7128 | 0 | ft->wl = wl; |
7129 | 0 | } |
7130 | | |
7131 | | /* Set default format keys for a window pane. */ |
7132 | | void |
7133 | | format_defaults_pane(struct format_tree *ft, struct window_pane *wp) |
7134 | 3.99k | { |
7135 | 3.99k | struct window_mode_entry *wme; |
7136 | | |
7137 | 3.99k | if (ft->w == NULL) |
7138 | 3.99k | format_defaults_window(ft, wp->window); |
7139 | 3.99k | ft->wp = wp; |
7140 | | |
7141 | 3.99k | wme = TAILQ_FIRST(&wp->modes); |
7142 | 3.99k | if (wme != NULL && wme->mode->formats != NULL) |
7143 | 0 | wme->mode->formats(wme, ft); |
7144 | 3.99k | } |
7145 | | |
7146 | | /* Set default format keys for paste buffer. */ |
7147 | | void |
7148 | | format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb) |
7149 | 19.6k | { |
7150 | 19.6k | ft->pb = pb; |
7151 | 19.6k | } |
7152 | | |
7153 | | static int |
7154 | | format_is_word_separator(const char *ws, const struct grid_cell *gc) |
7155 | 0 | { |
7156 | 0 | if (utf8_cstrhas(ws, &gc->data)) |
7157 | 0 | return (1); |
7158 | 0 | if (gc->flags & GRID_FLAG_TAB) |
7159 | 0 | return (1); |
7160 | 0 | return gc->data.size == 1 && *gc->data.data == ' '; |
7161 | 0 | } |
7162 | | |
7163 | | /* Return word at given coordinates. Caller frees. */ |
7164 | | char * |
7165 | | format_grid_word(struct grid *gd, u_int x, u_int y) |
7166 | 0 | { |
7167 | 0 | const struct grid_line *gl; |
7168 | 0 | struct grid_cell gc; |
7169 | 0 | const char *ws; |
7170 | 0 | struct utf8_data *ud = NULL; |
7171 | 0 | u_int end; |
7172 | 0 | size_t size = 0; |
7173 | 0 | int found = 0; |
7174 | 0 | char *s = NULL; |
7175 | |
|
7176 | 0 | ws = options_get_string(global_s_options, "word-separators"); |
7177 | |
|
7178 | 0 | for (;;) { |
7179 | 0 | grid_get_cell(gd, x, y, &gc); |
7180 | 0 | if ((~gc.flags & GRID_FLAG_PADDING) && |
7181 | 0 | format_is_word_separator(ws, &gc)) { |
7182 | 0 | found = 1; |
7183 | 0 | break; |
7184 | 0 | } |
7185 | | |
7186 | 0 | if (x == 0) { |
7187 | 0 | if (y == 0) |
7188 | 0 | break; |
7189 | 0 | gl = grid_peek_line(gd, y - 1); |
7190 | 0 | if (~gl->flags & GRID_LINE_WRAPPED) |
7191 | 0 | break; |
7192 | 0 | y--; |
7193 | 0 | x = grid_line_length(gd, y); |
7194 | 0 | if (x == 0) |
7195 | 0 | break; |
7196 | 0 | } |
7197 | 0 | x--; |
7198 | 0 | } |
7199 | 0 | for (;;) { |
7200 | 0 | if (found) { |
7201 | 0 | end = grid_line_length(gd, y); |
7202 | 0 | if (end == 0 || x == end - 1) { |
7203 | 0 | if (y == gd->hsize + gd->sy - 1) |
7204 | 0 | break; |
7205 | 0 | gl = grid_peek_line(gd, y); |
7206 | 0 | if (~gl->flags & GRID_LINE_WRAPPED) |
7207 | 0 | break; |
7208 | 0 | y++; |
7209 | 0 | x = 0; |
7210 | 0 | } else |
7211 | 0 | x++; |
7212 | 0 | } |
7213 | 0 | found = 1; |
7214 | |
|
7215 | 0 | grid_get_cell(gd, x, y, &gc); |
7216 | 0 | if (gc.flags & GRID_FLAG_PADDING) |
7217 | 0 | continue; |
7218 | 0 | if (format_is_word_separator(ws, &gc)) |
7219 | 0 | break; |
7220 | | |
7221 | 0 | ud = xreallocarray(ud, size + 2, sizeof *ud); |
7222 | 0 | memcpy(&ud[size++], &gc.data, sizeof *ud); |
7223 | 0 | } |
7224 | 0 | if (size != 0) { |
7225 | 0 | ud[size].size = 0; |
7226 | 0 | s = utf8_tocstr(ud); |
7227 | 0 | free(ud); |
7228 | 0 | } |
7229 | 0 | return (s); |
7230 | 0 | } |
7231 | | |
7232 | | /* Return line at given coordinates. Caller frees. */ |
7233 | | char * |
7234 | | format_grid_line(struct grid *gd, u_int y) |
7235 | 0 | { |
7236 | 0 | struct grid_cell gc; |
7237 | 0 | struct utf8_data *ud = NULL; |
7238 | 0 | u_int x; |
7239 | 0 | size_t size = 0; |
7240 | 0 | char *s = NULL; |
7241 | |
|
7242 | 0 | for (x = 0; x < grid_line_length(gd, y); x++) { |
7243 | 0 | grid_get_cell(gd, x, y, &gc); |
7244 | 0 | if (gc.flags & GRID_FLAG_PADDING) |
7245 | 0 | continue; |
7246 | | |
7247 | 0 | ud = xreallocarray(ud, size + 2, sizeof *ud); |
7248 | 0 | if (gc.flags & GRID_FLAG_TAB) |
7249 | 0 | utf8_set(&ud[size++], '\t'); |
7250 | 0 | else |
7251 | 0 | memcpy(&ud[size++], &gc.data, sizeof *ud); |
7252 | 0 | } |
7253 | 0 | if (size != 0) { |
7254 | 0 | ud[size].size = 0; |
7255 | 0 | s = utf8_tocstr(ud); |
7256 | 0 | free(ud); |
7257 | 0 | } |
7258 | 0 | return (s); |
7259 | 0 | } |
7260 | | |
7261 | | /* Return hyperlink at given coordinates. Caller frees. */ |
7262 | | char * |
7263 | | format_grid_hyperlink(struct grid *gd, u_int x, u_int y, struct screen* s) |
7264 | 0 | { |
7265 | 0 | const char *uri; |
7266 | 0 | struct grid_cell gc; |
7267 | |
|
7268 | 0 | for (;;) { |
7269 | 0 | grid_get_cell(gd, x, y, &gc); |
7270 | 0 | if (~gc.flags & GRID_FLAG_PADDING) |
7271 | 0 | break; |
7272 | 0 | if (x == 0) |
7273 | 0 | return (NULL); |
7274 | 0 | x--; |
7275 | 0 | } |
7276 | 0 | if (s->hyperlinks == NULL || gc.link == 0) |
7277 | 0 | return (NULL); |
7278 | 0 | if (!hyperlinks_get(s->hyperlinks, gc.link, &uri, NULL, NULL)) |
7279 | 0 | return (NULL); |
7280 | 0 | return (xstrdup(uri)); |
7281 | 0 | } |