Bug Summary

File:src/usr.bin/tmux/mode-tree.c
Warning:line 751, column 40
Access to field 'name' results in a dereference of a null pointer (loaded from variable 'mti')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name mode-tree.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.bin/tmux/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/usr.bin/tmux -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.bin/tmux/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/usr.bin/tmux/mode-tree.c
1/* $OpenBSD: mode-tree.c,v 1.61 2021/12/06 10:10:52 nicm Exp $ */
2
3/*
4 * Copyright (c) 2017 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
21#include <ctype.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "tmux.h"
27
28struct mode_tree_item;
29TAILQ_HEAD(mode_tree_list, mode_tree_item)struct mode_tree_list { struct mode_tree_item *tqh_first; struct
mode_tree_item **tqh_last; }
;
30
31struct mode_tree_data {
32 int dead;
33 u_int references;
34 int zoomed;
35
36 struct window_pane *wp;
37 void *modedata;
38 const struct menu_item *menu;
39
40 const char **sort_list;
41 u_int sort_size;
42 struct mode_tree_sort_criteria sort_crit;
43
44 mode_tree_build_cb buildcb;
45 mode_tree_draw_cb drawcb;
46 mode_tree_search_cb searchcb;
47 mode_tree_menu_cb menucb;
48 mode_tree_height_cb heightcb;
49 mode_tree_key_cb keycb;
50
51 struct mode_tree_list children;
52 struct mode_tree_list saved;
53
54 struct mode_tree_line *line_list;
55 u_int line_size;
56
57 u_int depth;
58
59 u_int width;
60 u_int height;
61
62 u_int offset;
63 u_int current;
64
65 struct screen screen;
66
67 int preview;
68 char *search;
69 char *filter;
70 int no_matches;
71};
72
73struct mode_tree_item {
74 struct mode_tree_item *parent;
75 void *itemdata;
76 u_int line;
77
78 key_code key;
79 const char *keystr;
80 size_t keylen;
81
82 uint64_t tag;
83 const char *name;
84 const char *text;
85
86 int expanded;
87 int tagged;
88
89 int draw_as_parent;
90 int no_tag;
91
92 struct mode_tree_list children;
93 TAILQ_ENTRY(mode_tree_item)struct { struct mode_tree_item *tqe_next; struct mode_tree_item
**tqe_prev; }
entry;
94};
95
96struct mode_tree_line {
97 struct mode_tree_item *item;
98 u_int depth;
99 int last;
100 int flat;
101};
102
103struct mode_tree_menu {
104 struct mode_tree_data *data;
105 struct client *c;
106 u_int line;
107 void *itemdata;
108};
109
110static void mode_tree_free_items(struct mode_tree_list *);
111
112static const struct menu_item mode_tree_menu_items[] = {
113 { "Scroll Left", '<', NULL((void *)0) },
114 { "Scroll Right", '>', NULL((void *)0) },
115 { "", KEYC_NONE0x000ff000000000ULL, NULL((void *)0) },
116 { "Cancel", 'q', NULL((void *)0) },
117
118 { NULL((void *)0), KEYC_NONE0x000ff000000000ULL, NULL((void *)0) }
119};
120
121static struct mode_tree_item *
122mode_tree_find_item(struct mode_tree_list *mtl, uint64_t tag)
123{
124 struct mode_tree_item *mti, *child;
125
126 TAILQ_FOREACH(mti, mtl, entry)for((mti) = ((mtl)->tqh_first); (mti) != ((void *)0); (mti
) = ((mti)->entry.tqe_next))
{
127 if (mti->tag == tag)
128 return (mti);
129 child = mode_tree_find_item(&mti->children, tag);
130 if (child != NULL((void *)0))
131 return (child);
132 }
133 return (NULL((void *)0));
134}
135
136static void
137mode_tree_free_item(struct mode_tree_item *mti)
138{
139 mode_tree_free_items(&mti->children);
140
141 free((void *)mti->name);
142 free((void *)mti->text);
143 free((void *)mti->keystr);
144
145 free(mti);
146}
147
148static void
149mode_tree_free_items(struct mode_tree_list *mtl)
150{
151 struct mode_tree_item *mti, *mti1;
152
153 TAILQ_FOREACH_SAFE(mti, mtl, entry, mti1)for ((mti) = ((mtl)->tqh_first); (mti) != ((void *)0) &&
((mti1) = ((mti)->entry.tqe_next), 1); (mti) = (mti1))
{
154 TAILQ_REMOVE(mtl, mti, entry)do { if (((mti)->entry.tqe_next) != ((void *)0)) (mti)->
entry.tqe_next->entry.tqe_prev = (mti)->entry.tqe_prev;
else (mtl)->tqh_last = (mti)->entry.tqe_prev; *(mti)->
entry.tqe_prev = (mti)->entry.tqe_next; ; ; } while (0)
;
155 mode_tree_free_item(mti);
156 }
157}
158
159static void
160mode_tree_check_selected(struct mode_tree_data *mtd)
161{
162 /*
163 * If the current line would now be off screen reset the offset to the
164 * last visible line.
165 */
166 if (mtd->current > mtd->height - 1)
167 mtd->offset = mtd->current - mtd->height + 1;
168}
169
170static void
171mode_tree_clear_lines(struct mode_tree_data *mtd)
172{
173 free(mtd->line_list);
174 mtd->line_list = NULL((void *)0);
175 mtd->line_size = 0;
176}
177
178static void
179mode_tree_build_lines(struct mode_tree_data *mtd,
180 struct mode_tree_list *mtl, u_int depth)
181{
182 struct mode_tree_item *mti;
183 struct mode_tree_line *line;
184 u_int i;
185 int flat = 1;
186
187 mtd->depth = depth;
188 TAILQ_FOREACH(mti, mtl, entry)for((mti) = ((mtl)->tqh_first); (mti) != ((void *)0); (mti
) = ((mti)->entry.tqe_next))
{
189 mtd->line_list = xreallocarray(mtd->line_list,
190 mtd->line_size + 1, sizeof *mtd->line_list);
191
192 line = &mtd->line_list[mtd->line_size++];
193 line->item = mti;
194 line->depth = depth;
195 line->last = (mti == TAILQ_LAST(mtl, mode_tree_list)(*(((struct mode_tree_list *)((mtl)->tqh_last))->tqh_last
))
);
196
197 mti->line = (mtd->line_size - 1);
198 if (!TAILQ_EMPTY(&mti->children)(((&mti->children)->tqh_first) == ((void *)0)))
199 flat = 0;
200 if (mti->expanded)
201 mode_tree_build_lines(mtd, &mti->children, depth + 1);
202
203 if (mtd->keycb != NULL((void *)0)) {
204 mti->key = mtd->keycb(mtd->modedata, mti->itemdata,
205 mti->line);
206 if (mti->key == KEYC_UNKNOWN0x000fe000000000ULL)
207 mti->key = KEYC_NONE0x000ff000000000ULL;
208 } else if (mti->line < 10)
209 mti->key = '0' + mti->line;
210 else if (mti->line < 36)
211 mti->key = KEYC_META0x00100000000000ULL|('a' + mti->line - 10);
212 else
213 mti->key = KEYC_NONE0x000ff000000000ULL;
214 if (mti->key != KEYC_NONE0x000ff000000000ULL) {
215 mti->keystr = xstrdup(key_string_lookup_key(mti->key,
216 0));
217 mti->keylen = strlen(mti->keystr);
218 } else {
219 mti->keystr = NULL((void *)0);
220 mti->keylen = 0;
221 }
222 }
223 TAILQ_FOREACH(mti, mtl, entry)for((mti) = ((mtl)->tqh_first); (mti) != ((void *)0); (mti
) = ((mti)->entry.tqe_next))
{
224 for (i = 0; i < mtd->line_size; i++) {
225 line = &mtd->line_list[i];
226 if (line->item == mti)
227 line->flat = flat;
228 }
229 }
230}
231
232static void
233mode_tree_clear_tagged(struct mode_tree_list *mtl)
234{
235 struct mode_tree_item *mti;
236
237 TAILQ_FOREACH(mti, mtl, entry)for((mti) = ((mtl)->tqh_first); (mti) != ((void *)0); (mti
) = ((mti)->entry.tqe_next))
{
238 mti->tagged = 0;
239 mode_tree_clear_tagged(&mti->children);
240 }
241}
242
243void
244mode_tree_up(struct mode_tree_data *mtd, int wrap)
245{
246 if (mtd->current == 0) {
247 if (wrap) {
248 mtd->current = mtd->line_size - 1;
249 if (mtd->line_size >= mtd->height)
250 mtd->offset = mtd->line_size - mtd->height;
251 }
252 } else {
253 mtd->current--;
254 if (mtd->current < mtd->offset)
255 mtd->offset--;
256 }
257}
258
259void
260mode_tree_down(struct mode_tree_data *mtd, int wrap)
261{
262 if (mtd->current == mtd->line_size - 1) {
263 if (wrap) {
264 mtd->current = 0;
265 mtd->offset = 0;
266 }
267 } else {
268 mtd->current++;
269 if (mtd->current > mtd->offset + mtd->height - 1)
270 mtd->offset++;
271 }
272}
273
274void *
275mode_tree_get_current(struct mode_tree_data *mtd)
276{
277 return (mtd->line_list[mtd->current].item->itemdata);
278}
279
280const char *
281mode_tree_get_current_name(struct mode_tree_data *mtd)
282{
283 return (mtd->line_list[mtd->current].item->name);
284}
285
286void
287mode_tree_expand_current(struct mode_tree_data *mtd)
288{
289 if (!mtd->line_list[mtd->current].item->expanded) {
290 mtd->line_list[mtd->current].item->expanded = 1;
291 mode_tree_build(mtd);
292 }
293}
294
295void
296mode_tree_collapse_current(struct mode_tree_data *mtd)
297{
298 if (mtd->line_list[mtd->current].item->expanded) {
299 mtd->line_list[mtd->current].item->expanded = 0;
300 mode_tree_build(mtd);
301 }
302}
303
304static int
305mode_tree_get_tag(struct mode_tree_data *mtd, uint64_t tag, u_int *found)
306{
307 u_int i;
308
309 for (i = 0; i < mtd->line_size; i++) {
310 if (mtd->line_list[i].item->tag == tag)
311 break;
312 }
313 if (i != mtd->line_size) {
314 *found = i;
315 return (1);
316 }
317 return (0);
318}
319
320void
321mode_tree_expand(struct mode_tree_data *mtd, uint64_t tag)
322{
323 u_int found;
324
325 if (!mode_tree_get_tag(mtd, tag, &found))
326 return;
327 if (!mtd->line_list[found].item->expanded) {
328 mtd->line_list[found].item->expanded = 1;
329 mode_tree_build(mtd);
330 }
331}
332
333int
334mode_tree_set_current(struct mode_tree_data *mtd, uint64_t tag)
335{
336 u_int found;
337
338 if (mode_tree_get_tag(mtd, tag, &found)) {
339 mtd->current = found;
340 if (mtd->current > mtd->height - 1)
341 mtd->offset = mtd->current - mtd->height + 1;
342 else
343 mtd->offset = 0;
344 return (1);
345 }
346 mtd->current = 0;
347 mtd->offset = 0;
348 return (0);
349}
350
351u_int
352mode_tree_count_tagged(struct mode_tree_data *mtd)
353{
354 struct mode_tree_item *mti;
355 u_int i, tagged;
356
357 tagged = 0;
358 for (i = 0; i < mtd->line_size; i++) {
359 mti = mtd->line_list[i].item;
360 if (mti->tagged)
361 tagged++;
362 }
363 return (tagged);
364}
365
366void
367mode_tree_each_tagged(struct mode_tree_data *mtd, mode_tree_each_cb cb,
368 struct client *c, key_code key, int current)
369{
370 struct mode_tree_item *mti;
371 u_int i;
372 int fired;
373
374 fired = 0;
375 for (i = 0; i < mtd->line_size; i++) {
376 mti = mtd->line_list[i].item;
377 if (mti->tagged) {
378 fired = 1;
379 cb(mtd->modedata, mti->itemdata, c, key);
380 }
381 }
382 if (!fired && current) {
383 mti = mtd->line_list[mtd->current].item;
384 cb(mtd->modedata, mti->itemdata, c, key);
385 }
386}
387
388struct mode_tree_data *
389mode_tree_start(struct window_pane *wp, struct args *args,
390 mode_tree_build_cb buildcb, mode_tree_draw_cb drawcb,
391 mode_tree_search_cb searchcb, mode_tree_menu_cb menucb,
392 mode_tree_height_cb heightcb, mode_tree_key_cb keycb, void *modedata,
393 const struct menu_item *menu, const char **sort_list, u_int sort_size,
394 struct screen **s)
395{
396 struct mode_tree_data *mtd;
397 const char *sort;
398 u_int i;
399
400 mtd = xcalloc(1, sizeof *mtd);
401 mtd->references = 1;
402
403 mtd->wp = wp;
404 mtd->modedata = modedata;
405 mtd->menu = menu;
406
407 mtd->sort_list = sort_list;
408 mtd->sort_size = sort_size;
409
410 mtd->preview = !args_has(args, 'N');
411
412 sort = args_get(args, 'O');
413 if (sort != NULL((void *)0)) {
414 for (i = 0; i < sort_size; i++) {
415 if (strcasecmp(sort, sort_list[i]) == 0)
416 mtd->sort_crit.field = i;
417 }
418 }
419 mtd->sort_crit.reversed = args_has(args, 'r');
420
421 if (args_has(args, 'f'))
422 mtd->filter = xstrdup(args_get(args, 'f'));
423 else
424 mtd->filter = NULL((void *)0);
425
426 mtd->buildcb = buildcb;
427 mtd->drawcb = drawcb;
428 mtd->searchcb = searchcb;
429 mtd->menucb = menucb;
430 mtd->heightcb = heightcb;
431 mtd->keycb = keycb;
432
433 TAILQ_INIT(&mtd->children)do { (&mtd->children)->tqh_first = ((void *)0); (&
mtd->children)->tqh_last = &(&mtd->children)
->tqh_first; } while (0)
;
434
435 *s = &mtd->screen;
436 screen_init(*s, screen_size_x(&wp->base)((&wp->base)->grid->sx), screen_size_y(&wp->base)((&wp->base)->grid->sy), 0);
437 (*s)->mode &= ~MODE_CURSOR0x1;
438
439 return (mtd);
440}
441
442void
443mode_tree_zoom(struct mode_tree_data *mtd, struct args *args)
444{
445 struct window_pane *wp = mtd->wp;
446
447 if (args_has(args, 'Z')) {
448 mtd->zoomed = (wp->window->flags & WINDOW_ZOOMED0x8);
449 if (!mtd->zoomed && window_zoom(wp) == 0)
450 server_redraw_window(wp->window);
451 } else
452 mtd->zoomed = -1;
453}
454
455static void
456mode_tree_set_height(struct mode_tree_data *mtd)
457{
458 struct screen *s = &mtd->screen;
459 u_int height;
460
461 if (mtd->heightcb != NULL((void *)0)) {
462 height = mtd->heightcb(mtd, screen_size_y(s)((s)->grid->sy));
463 if (height < screen_size_y(s)((s)->grid->sy))
464 mtd->height = screen_size_y(s)((s)->grid->sy) - height;
465 } else {
466 mtd->height = (screen_size_y(s)((s)->grid->sy) / 3) * 2;
467 if (mtd->height > mtd->line_size)
468 mtd->height = screen_size_y(s)((s)->grid->sy) / 2;
469 }
470 if (mtd->height < 10)
471 mtd->height = screen_size_y(s)((s)->grid->sy);
472 if (screen_size_y(s)((s)->grid->sy) - mtd->height < 2)
473 mtd->height = screen_size_y(s)((s)->grid->sy);
474}
475
476void
477mode_tree_build(struct mode_tree_data *mtd)
478{
479 struct screen *s = &mtd->screen;
480 uint64_t tag;
481
482 if (mtd->line_list != NULL((void *)0))
483 tag = mtd->line_list[mtd->current].item->tag;
484 else
485 tag = UINT64_MAX0xffffffffffffffffULL;
486
487 TAILQ_CONCAT(&mtd->saved, &mtd->children, entry)do { if (!(((&mtd->children)->tqh_first) == ((void *
)0))) { *(&mtd->saved)->tqh_last = (&mtd->children
)->tqh_first; (&mtd->children)->tqh_first->entry
.tqe_prev = (&mtd->saved)->tqh_last; (&mtd->
saved)->tqh_last = (&mtd->children)->tqh_last; do
{ ((&mtd->children))->tqh_first = ((void *)0); ((&
mtd->children))->tqh_last = &((&mtd->children
))->tqh_first; } while (0); } } while (0)
;
488 TAILQ_INIT(&mtd->children)do { (&mtd->children)->tqh_first = ((void *)0); (&
mtd->children)->tqh_last = &(&mtd->children)
->tqh_first; } while (0)
;
489
490 mtd->buildcb(mtd->modedata, &mtd->sort_crit, &tag, mtd->filter);
491 mtd->no_matches = TAILQ_EMPTY(&mtd->children)(((&mtd->children)->tqh_first) == ((void *)0));
492 if (mtd->no_matches)
493 mtd->buildcb(mtd->modedata, &mtd->sort_crit, &tag, NULL((void *)0));
494
495 mode_tree_free_items(&mtd->saved);
496 TAILQ_INIT(&mtd->saved)do { (&mtd->saved)->tqh_first = ((void *)0); (&
mtd->saved)->tqh_last = &(&mtd->saved)->tqh_first
; } while (0)
;
497
498 mode_tree_clear_lines(mtd);
499 mode_tree_build_lines(mtd, &mtd->children, 0);
500
501 if (tag == UINT64_MAX0xffffffffffffffffULL)
502 tag = mtd->line_list[mtd->current].item->tag;
503 mode_tree_set_current(mtd, tag);
504
505 mtd->width = screen_size_x(s)((s)->grid->sx);
506 if (mtd->preview)
507 mode_tree_set_height(mtd);
508 else
509 mtd->height = screen_size_y(s)((s)->grid->sy);
510 mode_tree_check_selected(mtd);
511}
512
513static void
514mode_tree_remove_ref(struct mode_tree_data *mtd)
515{
516 if (--mtd->references == 0)
517 free(mtd);
518}
519
520void
521mode_tree_free(struct mode_tree_data *mtd)
522{
523 struct window_pane *wp = mtd->wp;
524
525 if (mtd->zoomed == 0)
526 server_unzoom_window(wp->window);
527
528 mode_tree_free_items(&mtd->children);
529 mode_tree_clear_lines(mtd);
530 screen_free(&mtd->screen);
531
532 free(mtd->search);
533 free(mtd->filter);
534
535 mtd->dead = 1;
536 mode_tree_remove_ref(mtd);
537}
538
539void
540mode_tree_resize(struct mode_tree_data *mtd, u_int sx, u_int sy)
541{
542 struct screen *s = &mtd->screen;
543
544 screen_resize(s, sx, sy, 0);
545
546 mode_tree_build(mtd);
547 mode_tree_draw(mtd);
548
549 mtd->wp->flags |= PANE_REDRAW0x1;
550}
551
552struct mode_tree_item *
553mode_tree_add(struct mode_tree_data *mtd, struct mode_tree_item *parent,
554 void *itemdata, uint64_t tag, const char *name, const char *text,
555 int expanded)
556{
557 struct mode_tree_item *mti, *saved;
558
559 log_debug("%s: %llu, %s %s", __func__, (unsigned long long)tag,
560 name, (text == NULL((void *)0) ? "" : text));
561
562 mti = xcalloc(1, sizeof *mti);
563 mti->parent = parent;
564 mti->itemdata = itemdata;
565
566 mti->tag = tag;
567 mti->name = xstrdup(name);
568 if (text != NULL((void *)0))
569 mti->text = xstrdup(text);
570
571 saved = mode_tree_find_item(&mtd->saved, tag);
572 if (saved != NULL((void *)0)) {
573 if (parent == NULL((void *)0) || parent->expanded)
574 mti->tagged = saved->tagged;
575 mti->expanded = saved->expanded;
576 } else if (expanded == -1)
577 mti->expanded = 1;
578 else
579 mti->expanded = expanded;
580
581 TAILQ_INIT(&mti->children)do { (&mti->children)->tqh_first = ((void *)0); (&
mti->children)->tqh_last = &(&mti->children)
->tqh_first; } while (0)
;
582
583 if (parent != NULL((void *)0))
584 TAILQ_INSERT_TAIL(&parent->children, mti, entry)do { (mti)->entry.tqe_next = ((void *)0); (mti)->entry.
tqe_prev = (&parent->children)->tqh_last; *(&parent
->children)->tqh_last = (mti); (&parent->children
)->tqh_last = &(mti)->entry.tqe_next; } while (0)
;
585 else
586 TAILQ_INSERT_TAIL(&mtd->children, mti, entry)do { (mti)->entry.tqe_next = ((void *)0); (mti)->entry.
tqe_prev = (&mtd->children)->tqh_last; *(&mtd->
children)->tqh_last = (mti); (&mtd->children)->tqh_last
= &(mti)->entry.tqe_next; } while (0)
;
587
588 return (mti);
589}
590
591void
592mode_tree_draw_as_parent(struct mode_tree_item *mti)
593{
594 mti->draw_as_parent = 1;
595}
596
597void
598mode_tree_no_tag(struct mode_tree_item *mti)
599{
600 mti->no_tag = 1;
601}
602
603void
604mode_tree_remove(struct mode_tree_data *mtd, struct mode_tree_item *mti)
605{
606 struct mode_tree_item *parent = mti->parent;
607
608 if (parent != NULL((void *)0))
609 TAILQ_REMOVE(&parent->children, mti, entry)do { if (((mti)->entry.tqe_next) != ((void *)0)) (mti)->
entry.tqe_next->entry.tqe_prev = (mti)->entry.tqe_prev;
else (&parent->children)->tqh_last = (mti)->entry
.tqe_prev; *(mti)->entry.tqe_prev = (mti)->entry.tqe_next
; ; ; } while (0)
;
610 else
611 TAILQ_REMOVE(&mtd->children, mti, entry)do { if (((mti)->entry.tqe_next) != ((void *)0)) (mti)->
entry.tqe_next->entry.tqe_prev = (mti)->entry.tqe_prev;
else (&mtd->children)->tqh_last = (mti)->entry.
tqe_prev; *(mti)->entry.tqe_prev = (mti)->entry.tqe_next
; ; ; } while (0)
;
612 mode_tree_free_item(mti);
613}
614
615void
616mode_tree_draw(struct mode_tree_data *mtd)
617{
618 struct window_pane *wp = mtd->wp;
619 struct screen *s = &mtd->screen;
620 struct mode_tree_line *line;
621 struct mode_tree_item *mti;
622 struct options *oo = wp->window->options;
623 struct screen_write_ctx ctx;
624 struct grid_cell gc0, gc;
625 u_int w, h, i, j, sy, box_x, box_y, width;
626 char *text, *start, *key;
627 const char *tag, *symbol;
628 size_t size, n;
629 int keylen, pad;
630
631 if (mtd->line_size == 0)
7
Assuming field 'line_size' is not equal to 0
8
Taking false branch
632 return;
633
634 memcpy(&gc0, &grid_default_cell, sizeof gc0);
635 memcpy(&gc, &grid_default_cell, sizeof gc);
636 style_apply(&gc, oo, "mode-style", NULL((void *)0));
637
638 w = mtd->width;
639 h = mtd->height;
640
641 screen_write_start(&ctx, s);
642 screen_write_clearscreen(&ctx, 8);
643
644 keylen = 0;
645 for (i = 0; i < mtd->line_size; i++) {
9
Assuming 'i' is < field 'line_size'
10
Loop condition is true. Entering loop body
15
Assuming 'i' is >= field 'line_size'
16
Loop condition is false. Execution continues on line 653
646 mti = mtd->line_list[i].item;
647 if (mti->key == KEYC_NONE0x000ff000000000ULL)
11
Assuming field 'key' is not equal to KEYC_NONE
12
Taking false branch
648 continue;
649 if ((int)mti->keylen + 3 > keylen)
13
Assuming the condition is false
14
Taking false branch
650 keylen = mti->keylen + 3;
651 }
652
653 for (i = 0; i < mtd->line_size; i++) {
17
Loop condition is true. Entering loop body
48
Loop condition is false. Execution continues on line 738
654 if (i < mtd->offset)
18
Assuming 'i' is >= field 'offset'
19
Taking false branch
655 continue;
656 if (i > mtd->offset + h - 1)
20
Taking false branch
657 break;
658 line = &mtd->line_list[i];
659 mti = line->item;
660
661 screen_write_cursormove(&ctx, 0, i - mtd->offset, 0);
662
663 pad = keylen - 2 - mti->keylen;
664 if (mti->key
20.1
Field 'key' is not equal to KEYC_NONE
!= KEYC_NONE0x000ff000000000ULL)
21
Taking true branch
665 xasprintf(&key, "(%s)%*s", mti->keystr, pad, "");
666 else
667 key = xstrdup("");
668
669 if (line->flat)
22
Assuming field 'flat' is 0
23
Taking false branch
670 symbol = "";
671 else if (TAILQ_EMPTY(&mti->children)(((&mti->children)->tqh_first) == ((void *)0)))
24
Assuming field 'tqh_first' is not equal to null
25
Taking false branch
672 symbol = " ";
673 else if (mti->expanded)
26
Assuming field 'expanded' is 0
27
Taking false branch
674 symbol = "- ";
675 else
676 symbol = "+ ";
677
678 if (line->depth == 0)
28
Assuming field 'depth' is not equal to 0
29
Taking false branch
679 start = xstrdup(symbol);
680 else {
681 size = (4 * line->depth) + 32;
682
683 start = xcalloc(1, size);
684 for (j = 1; j < line->depth; j++) {
30
Assuming 'j' is < field 'depth'
31
Loop condition is true. Entering loop body
33
Assuming 'j' is >= field 'depth'
34
Loop condition is false. Execution continues on line 691
685 if (mti->parent != NULL((void *)0) &&
32
Assuming field 'parent' is equal to NULL
686 mtd->line_list[mti->parent->line].last)
687 strlcat(start, " ", size);
688 else
689 strlcat(start, "\001x\001 ", size);
690 }
691 if (line->last)
35
Assuming field 'last' is 0
36
Taking false branch
692 strlcat(start, "\001mq\001> ", size);
693 else
694 strlcat(start, "\001tq\001> ", size);
695 strlcat(start, symbol, size);
696 }
697
698 if (mti->tagged)
37
Assuming field 'tagged' is 0
38
Taking false branch
699 tag = "*";
700 else
701 tag = "";
702 xasprintf(&text, "%-*s%s%s%s%s", keylen, key, start, mti->name,
703 tag, (mti->text != NULL((void *)0)) ? ": " : "" );
39
Assuming field 'text' is equal to NULL
40
'?' condition is false
704 width = utf8_cstrwidth(text);
705 if (width > w)
41
Assuming 'width' is <= 'w'
42
Taking false branch
706 width = w;
707 free(start);
708
709 if (mti->tagged
42.1
Field 'tagged' is 0
) {
43
Taking false branch
710 gc.attr ^= GRID_ATTR_BRIGHT0x1;
711 gc0.attr ^= GRID_ATTR_BRIGHT0x1;
712 }
713
714 if (i != mtd->current) {
44
Assuming 'i' is equal to field 'current'
45
Taking false branch
715 screen_write_clearendofline(&ctx, 8);
716 screen_write_nputs(&ctx, w, &gc0, "%s", text);
717 if (mti->text != NULL((void *)0)) {
718 format_draw(&ctx, &gc0, w - width, mti->text,
719 NULL((void *)0), 0);
720 }
721 } else {
722 screen_write_clearendofline(&ctx, gc.bg);
723 screen_write_nputs(&ctx, w, &gc, "%s", text);
724 if (mti->text
45.1
Field 'text' is equal to NULL
!= NULL((void *)0)) {
46
Taking false branch
725 format_draw(&ctx, &gc, w - width, mti->text,
726 NULL((void *)0), 0);
727 }
728 }
729 free(text);
730 free(key);
731
732 if (mti->tagged
46.1
Field 'tagged' is 0
) {
47
Taking false branch
733 gc.attr ^= GRID_ATTR_BRIGHT0x1;
734 gc0.attr ^= GRID_ATTR_BRIGHT0x1;
735 }
736 }
737
738 sy = screen_size_y(s)((s)->grid->sy);
739 if (!mtd->preview || sy <= 4 || h <= 4 || sy - h <= 4 || w <= 4)
49
Assuming field 'preview' is not equal to 0
50
Assuming 'sy' is > 4
51
Assuming 'h' is > 4
52
Assuming the condition is false
53
Assuming 'w' is > 4
54
Taking false branch
740 goto done;
741
742 line = &mtd->line_list[mtd->current];
743 mti = line->item;
744 if (mti->draw_as_parent)
55
Assuming field 'draw_as_parent' is not equal to 0
56
Taking true branch
745 mti = mti->parent;
57
Null pointer value stored to 'mti'
746
747 screen_write_cursormove(&ctx, 0, h, 0);
748 screen_write_box(&ctx, w, sy - h, BOX_LINES_DEFAULT, NULL((void *)0), NULL((void *)0));
749
750 if (mtd->sort_list != NULL((void *)0)) {
58
Assuming field 'sort_list' is not equal to NULL
59
Taking true branch
751 xasprintf(&text, " %s (sort: %s%s)", mti->name,
60
Access to field 'name' results in a dereference of a null pointer (loaded from variable 'mti')
752 mtd->sort_list[mtd->sort_crit.field],
753 mtd->sort_crit.reversed ? ", reversed" : "");
754 } else
755 xasprintf(&text, " %s", mti->name);
756 if (w - 2 >= strlen(text)) {
757 screen_write_cursormove(&ctx, 1, h, 0);
758 screen_write_puts(&ctx, &gc0, "%s", text);
759
760 if (mtd->no_matches)
761 n = (sizeof "no matches") - 1;
762 else
763 n = (sizeof "active") - 1;
764 if (mtd->filter != NULL((void *)0) && w - 2 >= strlen(text) + 10 + n + 2) {
765 screen_write_puts(&ctx, &gc0, " (filter: ");
766 if (mtd->no_matches)
767 screen_write_puts(&ctx, &gc, "no matches");
768 else
769 screen_write_puts(&ctx, &gc0, "active");
770 screen_write_puts(&ctx, &gc0, ") ");
771 } else
772 screen_write_puts(&ctx, &gc0, " ");
773 }
774 free(text);
775
776 box_x = w - 4;
777 box_y = sy - h - 2;
778
779 if (box_x != 0 && box_y != 0) {
780 screen_write_cursormove(&ctx, 2, h + 1, 0);
781 mtd->drawcb(mtd->modedata, mti->itemdata, &ctx, box_x, box_y);
782 }
783
784done:
785 screen_write_cursormove(&ctx, 0, mtd->current - mtd->offset, 0);
786 screen_write_stop(&ctx);
787}
788
789static struct mode_tree_item *
790mode_tree_search_for(struct mode_tree_data *mtd)
791{
792 struct mode_tree_item *mti, *last, *next;
793
794 if (mtd->search == NULL((void *)0))
795 return (NULL((void *)0));
796
797 mti = last = mtd->line_list[mtd->current].item;
798 for (;;) {
799 if (!TAILQ_EMPTY(&mti->children)(((&mti->children)->tqh_first) == ((void *)0)))
800 mti = TAILQ_FIRST(&mti->children)((&mti->children)->tqh_first);
801 else if ((next = TAILQ_NEXT(mti, entry)((mti)->entry.tqe_next)) != NULL((void *)0))
802 mti = next;
803 else {
804 for (;;) {
805 mti = mti->parent;
806 if (mti == NULL((void *)0))
807 break;
808 if ((next = TAILQ_NEXT(mti, entry)((mti)->entry.tqe_next)) != NULL((void *)0)) {
809 mti = next;
810 break;
811 }
812 }
813 }
814 if (mti == NULL((void *)0))
815 mti = TAILQ_FIRST(&mtd->children)((&mtd->children)->tqh_first);
816 if (mti == last)
817 break;
818
819 if (mtd->searchcb == NULL((void *)0)) {
820 if (strstr(mti->name, mtd->search) != NULL((void *)0))
821 return (mti);
822 continue;
823 }
824 if (mtd->searchcb(mtd->modedata, mti->itemdata, mtd->search))
825 return (mti);
826 }
827 return (NULL((void *)0));
828}
829
830static void
831mode_tree_search_set(struct mode_tree_data *mtd)
832{
833 struct mode_tree_item *mti, *loop;
834 uint64_t tag;
835
836 mti = mode_tree_search_for(mtd);
837 if (mti == NULL((void *)0))
838 return;
839 tag = mti->tag;
840
841 loop = mti->parent;
842 while (loop != NULL((void *)0)) {
843 loop->expanded = 1;
844 loop = loop->parent;
845 }
846
847 mode_tree_build(mtd);
848 mode_tree_set_current(mtd, tag);
849 mode_tree_draw(mtd);
850 mtd->wp->flags |= PANE_REDRAW0x1;
851}
852
853static int
854mode_tree_search_callback(__unused__attribute__((__unused__)) struct client *c, void *data, const char *s,
855 __unused__attribute__((__unused__)) int done)
856{
857 struct mode_tree_data *mtd = data;
858
859 if (mtd->dead)
860 return (0);
861
862 free(mtd->search);
863 if (s == NULL((void *)0) || *s == '\0') {
864 mtd->search = NULL((void *)0);
865 return (0);
866 }
867 mtd->search = xstrdup(s);
868 mode_tree_search_set(mtd);
869
870 return (0);
871}
872
873static void
874mode_tree_search_free(void *data)
875{
876 mode_tree_remove_ref(data);
877}
878
879static int
880mode_tree_filter_callback(__unused__attribute__((__unused__)) struct client *c, void *data, const char *s,
881 __unused__attribute__((__unused__)) int done)
882{
883 struct mode_tree_data *mtd = data;
884
885 if (mtd->dead)
1
Assuming field 'dead' is 0
2
Taking false branch
886 return (0);
887
888 if (mtd->filter != NULL((void *)0))
3
Assuming field 'filter' is equal to NULL
4
Taking false branch
889 free(mtd->filter);
890 if (s == NULL((void *)0) || *s == '\0')
5
Assuming 's' is equal to NULL
891 mtd->filter = NULL((void *)0);
892 else
893 mtd->filter = xstrdup(s);
894
895 mode_tree_build(mtd);
896 mode_tree_draw(mtd);
6
Calling 'mode_tree_draw'
897 mtd->wp->flags |= PANE_REDRAW0x1;
898
899 return (0);
900}
901
902static void
903mode_tree_filter_free(void *data)
904{
905 mode_tree_remove_ref(data);
906}
907
908static void
909mode_tree_menu_callback(__unused__attribute__((__unused__)) struct menu *menu, __unused__attribute__((__unused__)) u_int idx,
910 key_code key, void *data)
911{
912 struct mode_tree_menu *mtm = data;
913 struct mode_tree_data *mtd = mtm->data;
914 struct mode_tree_item *mti;
915
916 if (mtd->dead || key == KEYC_NONE0x000ff000000000ULL)
917 goto out;
918
919 if (mtm->line >= mtd->line_size)
920 goto out;
921 mti = mtd->line_list[mtm->line].item;
922 if (mti->itemdata != mtm->itemdata)
923 goto out;
924 mtd->current = mtm->line;
925 mtd->menucb(mtd->modedata, mtm->c, key);
926
927out:
928 mode_tree_remove_ref(mtd);
929 free(mtm);
930}
931
932static void
933mode_tree_display_menu(struct mode_tree_data *mtd, struct client *c, u_int x,
934 u_int y, int outside)
935{
936 struct mode_tree_item *mti;
937 struct menu *menu;
938 const struct menu_item *items;
939 struct mode_tree_menu *mtm;
940 char *title;
941 u_int line;
942
943 if (mtd->offset + y > mtd->line_size - 1)
944 line = mtd->current;
945 else
946 line = mtd->offset + y;
947 mti = mtd->line_list[line].item;
948
949 if (!outside) {
950 items = mtd->menu;
951 xasprintf(&title, "#[align=centre]%s", mti->name);
952 } else {
953 items = mode_tree_menu_items;
954 title = xstrdup("");
955 }
956 menu = menu_create(title);
957 menu_add_items(menu, items, NULL((void *)0), NULL((void *)0), NULL((void *)0));
958 free(title);
959
960 mtm = xmalloc(sizeof *mtm);
961 mtm->data = mtd;
962 mtm->c = c;
963 mtm->line = line;
964 mtm->itemdata = mti->itemdata;
965 mtd->references++;
966
967 if (x >= (menu->width + 4) / 2)
968 x -= (menu->width + 4) / 2;
969 else
970 x = 0;
971 if (menu_display(menu, 0, NULL((void *)0), x, y, c, NULL((void *)0), mode_tree_menu_callback,
972 mtm) != 0)
973 menu_free(menu);
974}
975
976int
977mode_tree_key(struct mode_tree_data *mtd, struct client *c, key_code *key,
978 struct mouse_event *m, u_int *xp, u_int *yp)
979{
980 struct mode_tree_line *line;
981 struct mode_tree_item *current, *parent, *mti;
982 u_int i, x, y;
983 int choice;
984
985 if (KEYC_IS_MOUSE(*key)(((*key) & 0x000fffffffffffULL) >= KEYC_MOUSE &&
((*key) & 0x000fffffffffffULL) < KEYC_BSPACE)
&& m != NULL((void *)0)) {
986 if (cmd_mouse_at(mtd->wp, m, &x, &y, 0) != 0) {
987 *key = KEYC_NONE0x000ff000000000ULL;
988 return (0);
989 }
990 if (xp != NULL((void *)0))
991 *xp = x;
992 if (yp != NULL((void *)0))
993 *yp = y;
994 if (x > mtd->width || y > mtd->height) {
995 if (*key == KEYC_MOUSEDOWN3_PANE)
996 mode_tree_display_menu(mtd, c, x, y, 1);
997 if (!mtd->preview)
998 *key = KEYC_NONE0x000ff000000000ULL;
999 return (0);
1000 }
1001 if (mtd->offset + y < mtd->line_size) {
1002 if (*key == KEYC_MOUSEDOWN1_PANE ||
1003 *key == KEYC_MOUSEDOWN3_PANE ||
1004 *key == KEYC_DOUBLECLICK1_PANE)
1005 mtd->current = mtd->offset + y;
1006 if (*key == KEYC_DOUBLECLICK1_PANE)
1007 *key = '\r';
1008 else {
1009 if (*key == KEYC_MOUSEDOWN3_PANE)
1010 mode_tree_display_menu(mtd, c, x, y, 0);
1011 *key = KEYC_NONE0x000ff000000000ULL;
1012 }
1013 } else {
1014 if (*key == KEYC_MOUSEDOWN3_PANE)
1015 mode_tree_display_menu(mtd, c, x, y, 0);
1016 *key = KEYC_NONE0x000ff000000000ULL;
1017 }
1018 return (0);
1019 }
1020
1021 line = &mtd->line_list[mtd->current];
1022 current = line->item;
1023
1024 choice = -1;
1025 for (i = 0; i < mtd->line_size; i++) {
1026 if (*key == mtd->line_list[i].item->key) {
1027 choice = i;
1028 break;
1029 }
1030 }
1031 if (choice != -1) {
1032 if ((u_int)choice > mtd->line_size - 1) {
1033 *key = KEYC_NONE0x000ff000000000ULL;
1034 return (0);
1035 }
1036 mtd->current = choice;
1037 *key = '\r';
1038 return (0);
1039 }
1040
1041 switch (*key) {
1042 case 'q':
1043 case '\033': /* Escape */
1044 case '\007': /* C-g */
1045 return (1);
1046 case KEYC_UP:
1047 case 'k':
1048 case KEYC_WHEELUP_PANE:
1049 case '\020': /* C-p */
1050 mode_tree_up(mtd, 1);
1051 break;
1052 case KEYC_DOWN:
1053 case 'j':
1054 case KEYC_WHEELDOWN_PANE:
1055 case '\016': /* C-n */
1056 mode_tree_down(mtd, 1);
1057 break;
1058 case KEYC_PPAGE:
1059 case '\002': /* C-b */
1060 for (i = 0; i < mtd->height; i++) {
1061 if (mtd->current == 0)
1062 break;
1063 mode_tree_up(mtd, 1);
1064 }
1065 break;
1066 case KEYC_NPAGE:
1067 case '\006': /* C-f */
1068 for (i = 0; i < mtd->height; i++) {
1069 if (mtd->current == mtd->line_size - 1)
1070 break;
1071 mode_tree_down(mtd, 1);
1072 }
1073 break;
1074 case 'g':
1075 case KEYC_HOME:
1076 mtd->current = 0;
1077 mtd->offset = 0;
1078 break;
1079 case 'G':
1080 case KEYC_END:
1081 mtd->current = mtd->line_size - 1;
1082 if (mtd->current > mtd->height - 1)
1083 mtd->offset = mtd->current - mtd->height + 1;
1084 else
1085 mtd->offset = 0;
1086 break;
1087 case 't':
1088 /*
1089 * Do not allow parents and children to both be tagged: untag
1090 * all parents and children of current.
1091 */
1092 if (current->no_tag)
1093 break;
1094 if (!current->tagged) {
1095 parent = current->parent;
1096 while (parent != NULL((void *)0)) {
1097 parent->tagged = 0;
1098 parent = parent->parent;
1099 }
1100 mode_tree_clear_tagged(&current->children);
1101 current->tagged = 1;
1102 } else
1103 current->tagged = 0;
1104 if (m != NULL((void *)0))
1105 mode_tree_down(mtd, 0);
1106 break;
1107 case 'T':
1108 for (i = 0; i < mtd->line_size; i++)
1109 mtd->line_list[i].item->tagged = 0;
1110 break;
1111 case '\024': /* C-t */
1112 for (i = 0; i < mtd->line_size; i++) {
1113 if ((mtd->line_list[i].item->parent == NULL((void *)0) &&
1114 !mtd->line_list[i].item->no_tag) ||
1115 (mtd->line_list[i].item->parent != NULL((void *)0) &&
1116 mtd->line_list[i].item->parent->no_tag))
1117 mtd->line_list[i].item->tagged = 1;
1118 else
1119 mtd->line_list[i].item->tagged = 0;
1120 }
1121 break;
1122 case 'O':
1123 mtd->sort_crit.field++;
1124 if (mtd->sort_crit.field >= mtd->sort_size)
1125 mtd->sort_crit.field = 0;
1126 mode_tree_build(mtd);
1127 break;
1128 case 'r':
1129 mtd->sort_crit.reversed = !mtd->sort_crit.reversed;
1130 mode_tree_build(mtd);
1131 break;
1132 case KEYC_LEFT:
1133 case 'h':
1134 case '-':
1135 if (line->flat || !current->expanded)
1136 current = current->parent;
1137 if (current == NULL((void *)0))
1138 mode_tree_up(mtd, 0);
1139 else {
1140 current->expanded = 0;
1141 mtd->current = current->line;
1142 mode_tree_build(mtd);
1143 }
1144 break;
1145 case KEYC_RIGHT:
1146 case 'l':
1147 case '+':
1148 if (line->flat || current->expanded)
1149 mode_tree_down(mtd, 0);
1150 else if (!line->flat) {
1151 current->expanded = 1;
1152 mode_tree_build(mtd);
1153 }
1154 break;
1155 case '-'|KEYC_META0x00100000000000ULL:
1156 TAILQ_FOREACH(mti, &mtd->children, entry)for((mti) = ((&mtd->children)->tqh_first); (mti) !=
((void *)0); (mti) = ((mti)->entry.tqe_next))
1157 mti->expanded = 0;
1158 mode_tree_build(mtd);
1159 break;
1160 case '+'|KEYC_META0x00100000000000ULL:
1161 TAILQ_FOREACH(mti, &mtd->children, entry)for((mti) = ((&mtd->children)->tqh_first); (mti) !=
((void *)0); (mti) = ((mti)->entry.tqe_next))
1162 mti->expanded = 1;
1163 mode_tree_build(mtd);
1164 break;
1165 case '?':
1166 case '/':
1167 case '\023': /* C-s */
1168 mtd->references++;
1169 status_prompt_set(c, NULL((void *)0), "(search) ", "",
1170 mode_tree_search_callback, mode_tree_search_free, mtd,
1171 PROMPT_NOFORMAT0x8, PROMPT_TYPE_SEARCH);
1172 break;
1173 case 'n':
1174 mode_tree_search_set(mtd);
1175 break;
1176 case 'f':
1177 mtd->references++;
1178 status_prompt_set(c, NULL((void *)0), "(filter) ", mtd->filter,
1179 mode_tree_filter_callback, mode_tree_filter_free, mtd,
1180 PROMPT_NOFORMAT0x8, PROMPT_TYPE_SEARCH);
1181 break;
1182 case 'v':
1183 mtd->preview = !mtd->preview;
1184 mode_tree_build(mtd);
1185 if (mtd->preview)
1186 mode_tree_check_selected(mtd);
1187 break;
1188 }
1189 return (0);
1190}
1191
1192void
1193mode_tree_run_command(struct client *c, struct cmd_find_state *fs,
1194 const char *template, const char *name)
1195{
1196 struct cmdq_state *state;
1197 char *command, *error;
1198 enum cmd_parse_status status;
1199
1200 command = cmd_template_replace(template, name, 1);
1201 if (command != NULL((void *)0) && *command != '\0') {
1202 state = cmdq_new_state(fs, NULL((void *)0), 0);
1203 status = cmd_parse_and_append(command, NULL((void *)0), c, state, &error);
1204 if (status == CMD_PARSE_ERROR) {
1205 if (c != NULL((void *)0)) {
1206 *error = toupper((u_char)*error);
1207 status_message_set(c, -1, 1, 0, "%s", error);
1208 }
1209 free(error);
1210 }
1211 cmdq_free_state(state);
1212 }
1213 free(command);
1214}