Remove an unneeded NULL check
[deliverable/binutils-gdb.git] / gdb / tui / tui-data.c
1 /* TUI data manipulation routines.
2
3 Copyright (C) 1998-2019 Free Software Foundation, Inc.
4
5 Contributed by Hewlett-Packard Company.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "tui/tui.h"
25 #include "tui/tui-data.h"
26 #include "tui/tui-wingeneral.h"
27 #include "gdb_curses.h"
28
29 /****************************
30 ** GLOBAL DECLARATIONS
31 ****************************/
32 struct tui_win_info *tui_win_list[MAX_MAJOR_WINDOWS];
33
34 /***************************
35 ** Private data
36 ****************************/
37 static enum tui_layout_type current_layout = UNDEFINED_LAYOUT;
38 static int term_height, term_width;
39 static struct tui_gen_win_info _locator;
40 static struct tui_gen_win_info exec_info[2];
41 static std::vector<tui_win_info *> source_windows;
42 static struct tui_win_info *win_with_focus = NULL;
43 static struct tui_layout_def layout_def = {
44 SRC_WIN, /* DISPLAY_MODE */
45 FALSE}; /* SPLIT */
46
47 static int win_resized = FALSE;
48
49
50 /*********************************
51 ** Static function forward decls
52 **********************************/
53 static void free_content (tui_win_content,
54 int,
55 enum tui_win_type);
56 static void free_content_elements (tui_win_content,
57 int,
58 enum tui_win_type);
59
60
61
62 /*********************************
63 ** PUBLIC FUNCTIONS
64 **********************************/
65
66 int
67 tui_win_is_source_type (enum tui_win_type win_type)
68 {
69 return (win_type == SRC_WIN || win_type == DISASSEM_WIN);
70 }
71
72 int
73 tui_win_is_auxillary (enum tui_win_type win_type)
74 {
75 return (win_type > MAX_MAJOR_WINDOWS);
76 }
77
78 void
79 tui_set_win_highlight (struct tui_win_info *win_info,
80 int highlight)
81 {
82 if (win_info != NULL)
83 win_info->is_highlighted = highlight;
84 }
85
86 /******************************************
87 ** ACCESSORS & MUTATORS FOR PRIVATE DATA
88 ******************************************/
89
90 /* Answer a whether the terminal window has been resized or not. */
91 int
92 tui_win_resized (void)
93 {
94 return win_resized;
95 }
96
97
98 /* Set a whether the terminal window has been resized or not. */
99 void
100 tui_set_win_resized_to (int resized)
101 {
102 win_resized = resized;
103 }
104
105
106 /* Answer a pointer to the current layout definition. */
107 struct tui_layout_def *
108 tui_layout_def (void)
109 {
110 return &layout_def;
111 }
112
113
114 /* Answer the window with the logical focus. */
115 struct tui_win_info *
116 tui_win_with_focus (void)
117 {
118 return win_with_focus;
119 }
120
121
122 /* Set the window that has the logical focus. */
123 void
124 tui_set_win_with_focus (struct tui_win_info *win_info)
125 {
126 win_with_focus = win_info;
127 }
128
129
130 /* Accessor for the current source window. Usually there is only one
131 source window (either source or disassembly), but both can be
132 displayed at the same time. */
133 std::vector<tui_win_info *> &
134 tui_source_windows ()
135 {
136 return source_windows;
137 }
138
139
140 /* Clear the list of source windows. Usually there is only one source
141 window (either source or disassembly), but both can be displayed at
142 the same time. */
143 void
144 tui_clear_source_windows ()
145 {
146 source_windows.clear ();
147 }
148
149
150 /* Clear the pertinant detail in the source windows. */
151 void
152 tui_clear_source_windows_detail ()
153 {
154 for (tui_win_info *win : tui_source_windows ())
155 win->clear_detail ();
156 }
157
158
159 /* Add a window to the list of source windows. Usually there is only
160 one source window (either source or disassembly), but both can be
161 displayed at the same time. */
162 void
163 tui_add_to_source_windows (struct tui_win_info *win_info)
164 {
165 if (source_windows.size () < 2)
166 source_windows.push_back (win_info);
167 }
168
169 /* See tui-data.h. */
170
171 void
172 tui_source_window_base::clear_detail ()
173 {
174 gdbarch = NULL;
175 start_line_or_addr.loa = LOA_ADDRESS;
176 start_line_or_addr.u.addr = 0;
177 horizontal_offset = 0;
178 }
179
180 /* See tui-data.h. */
181
182 void
183 tui_cmd_window::clear_detail ()
184 {
185 wmove (generic.handle, 0, 0);
186 }
187
188 /* See tui-data.h. */
189
190 void
191 tui_data_window::clear_detail ()
192 {
193 data_content = NULL;
194 data_content_count = 0;
195 regs_content = NULL;
196 regs_content_count = 0;
197 regs_column_count = 1;
198 display_regs = false;
199 }
200
201 /* Accessor for the source execution info ptr. */
202 struct tui_gen_win_info *
203 tui_source_exec_info_win_ptr (void)
204 {
205 return &exec_info[0];
206 }
207
208
209 /* Accessor for the disassem execution info ptr. */
210 struct tui_gen_win_info *
211 tui_disassem_exec_info_win_ptr (void)
212 {
213 return &exec_info[1];
214 }
215
216
217 /* Accessor for the locator win info. Answers a pointer to the static
218 locator win info struct. */
219 struct tui_gen_win_info *
220 tui_locator_win_info_ptr (void)
221 {
222 return &_locator;
223 }
224
225
226 /* Accessor for the term_height. */
227 int
228 tui_term_height (void)
229 {
230 return term_height;
231 }
232
233
234 /* Mutator for the term height. */
235 void
236 tui_set_term_height_to (int h)
237 {
238 term_height = h;
239 }
240
241
242 /* Accessor for the term_width. */
243 int
244 tui_term_width (void)
245 {
246 return term_width;
247 }
248
249
250 /* Mutator for the term_width. */
251 void
252 tui_set_term_width_to (int w)
253 {
254 term_width = w;
255 }
256
257
258 /* Accessor for the current layout. */
259 enum tui_layout_type
260 tui_current_layout (void)
261 {
262 return current_layout;
263 }
264
265
266 /* Mutator for the current layout. */
267 void
268 tui_set_current_layout_to (enum tui_layout_type new_layout)
269 {
270 current_layout = new_layout;
271 }
272
273
274 /*****************************
275 ** OTHER PUBLIC FUNCTIONS
276 *****************************/
277
278
279 /* Answer the next window in the list, cycling back to the top if
280 necessary. */
281 struct tui_win_info *
282 tui_next_win (struct tui_win_info *cur_win)
283 {
284 int type = cur_win->generic.type;
285 struct tui_win_info *next_win = NULL;
286
287 if (cur_win->generic.type == CMD_WIN)
288 type = SRC_WIN;
289 else
290 type = cur_win->generic.type + 1;
291 while (type != cur_win->generic.type && (next_win == NULL))
292 {
293 if (tui_win_list[type]
294 && tui_win_list[type]->generic.is_visible)
295 next_win = tui_win_list[type];
296 else
297 {
298 if (type == CMD_WIN)
299 type = SRC_WIN;
300 else
301 type++;
302 }
303 }
304
305 return next_win;
306 }
307
308
309 /* Answer the prev window in the list, cycling back to the bottom if
310 necessary. */
311 struct tui_win_info *
312 tui_prev_win (struct tui_win_info *cur_win)
313 {
314 int type = cur_win->generic.type;
315 struct tui_win_info *prev = NULL;
316
317 if (cur_win->generic.type == SRC_WIN)
318 type = CMD_WIN;
319 else
320 type = cur_win->generic.type - 1;
321 while (type != cur_win->generic.type && (prev == NULL))
322 {
323 if (tui_win_list[type]
324 && tui_win_list[type]->generic.is_visible)
325 prev = tui_win_list[type];
326 else
327 {
328 if (type == SRC_WIN)
329 type = CMD_WIN;
330 else
331 type--;
332 }
333 }
334
335 return prev;
336 }
337
338
339 /* Answer the window represented by name. */
340 struct tui_win_info *
341 tui_partial_win_by_name (const char *name)
342 {
343 struct tui_win_info *win_info = NULL;
344
345 if (name != NULL)
346 {
347 int i = 0;
348
349 while (i < MAX_MAJOR_WINDOWS && win_info == NULL)
350 {
351 if (tui_win_list[i] != 0)
352 {
353 const char *cur_name =
354 tui_win_name (&tui_win_list[i]->generic);
355
356 if (strlen (name) <= strlen (cur_name)
357 && startswith (cur_name, name))
358 win_info = tui_win_list[i];
359 }
360 i++;
361 }
362 }
363
364 return win_info;
365 }
366
367
368 /* Answer the name of the window. */
369 const char *
370 tui_win_name (const struct tui_gen_win_info *win_info)
371 {
372 const char *name = NULL;
373
374 switch (win_info->type)
375 {
376 case SRC_WIN:
377 name = SRC_NAME;
378 break;
379 case CMD_WIN:
380 name = CMD_NAME;
381 break;
382 case DISASSEM_WIN:
383 name = DISASSEM_NAME;
384 break;
385 case DATA_WIN:
386 name = DATA_NAME;
387 break;
388 default:
389 name = "";
390 break;
391 }
392
393 return name;
394 }
395
396
397 void
398 tui_initialize_static_data (void)
399 {
400 tui_init_generic_part (tui_source_exec_info_win_ptr ());
401 tui_init_generic_part (tui_disassem_exec_info_win_ptr ());
402 tui_init_generic_part (tui_locator_win_info_ptr ());
403 }
404
405
406 struct tui_gen_win_info *
407 tui_alloc_generic_win_info (void)
408 {
409 struct tui_gen_win_info *win = XNEW (struct tui_gen_win_info);
410
411 tui_init_generic_part (win);
412
413 return win;
414 }
415
416
417 void
418 tui_init_generic_part (struct tui_gen_win_info *win)
419 {
420 win->width =
421 win->height =
422 win->origin.x =
423 win->origin.y =
424 win->viewport_height =
425 win->content_size =
426 win->last_visible_line = 0;
427 win->handle = NULL;
428 win->content = NULL;
429 win->content_in_use =
430 win->is_visible = FALSE;
431 win->title = 0;
432 }
433
434
435 /* init_content_element().
436 */
437 static void
438 init_content_element (struct tui_win_element *element,
439 enum tui_win_type type)
440 {
441 switch (type)
442 {
443 case SRC_WIN:
444 case DISASSEM_WIN:
445 element->which_element.source.line = NULL;
446 element->which_element.source.line_or_addr.loa = LOA_LINE;
447 element->which_element.source.line_or_addr.u.line_no = 0;
448 element->which_element.source.is_exec_point = FALSE;
449 element->which_element.source.has_break = FALSE;
450 break;
451 case DATA_WIN:
452 tui_init_generic_part (&element->which_element.data_window);
453 element->which_element.data_window.type = DATA_ITEM_WIN;
454 element->which_element.data_window.content =
455 tui_alloc_content (1, DATA_ITEM_WIN);
456 element->which_element.data_window.content_size = 1;
457 break;
458 case CMD_WIN:
459 element->which_element.command.line = NULL;
460 break;
461 case DATA_ITEM_WIN:
462 element->which_element.data.name = NULL;
463 element->which_element.data.type = TUI_REGISTER;
464 element->which_element.data.item_no = UNDEFINED_ITEM;
465 element->which_element.data.value = NULL;
466 element->which_element.data.highlight = FALSE;
467 element->which_element.data.content = NULL;
468 break;
469 case LOCATOR_WIN:
470 element->which_element.locator.full_name[0] =
471 element->which_element.locator.proc_name[0] = (char) 0;
472 element->which_element.locator.line_no = 0;
473 element->which_element.locator.addr = 0;
474 break;
475 case EXEC_INFO_WIN:
476 memset(element->which_element.simple_string, ' ',
477 sizeof(element->which_element.simple_string));
478 break;
479 default:
480 break;
481 }
482 }
483
484 tui_win_info::tui_win_info (enum tui_win_type type)
485 {
486 generic.type = type;
487 tui_init_generic_part (&generic);
488 }
489
490 tui_source_window_base::tui_source_window_base (enum tui_win_type type)
491 : tui_win_info (type)
492 {
493 gdb_assert (type == SRC_WIN || type == DISASSEM_WIN);
494 start_line_or_addr.loa = LOA_ADDRESS;
495 start_line_or_addr.u.addr = 0;
496 }
497
498 struct tui_win_info *
499 tui_alloc_win_info (enum tui_win_type type)
500 {
501 switch (type)
502 {
503 case SRC_WIN:
504 return new tui_source_window ();
505
506 case DISASSEM_WIN:
507 return new tui_disasm_window ();
508
509 case DATA_WIN:
510 return new tui_data_window ();
511
512 case CMD_WIN:
513 return new tui_cmd_window ();
514 }
515
516 gdb_assert_not_reached (_("Unhandled window type"));
517 }
518
519
520 /* Allocates the content and elements in a block. */
521 tui_win_content
522 tui_alloc_content (int num_elements, enum tui_win_type type)
523 {
524 tui_win_content content;
525 struct tui_win_element *element_block_ptr;
526 int i;
527
528 content = XNEWVEC (struct tui_win_element *, num_elements);
529
530 /*
531 * All windows, except the data window, can allocate the
532 * elements in a chunk. The data window cannot because items
533 * can be added/removed from the data display by the user at any
534 * time.
535 */
536 if (type != DATA_WIN)
537 {
538 element_block_ptr = XNEWVEC (struct tui_win_element, num_elements);
539 for (i = 0; i < num_elements; i++)
540 {
541 content[i] = element_block_ptr;
542 init_content_element (content[i], type);
543 element_block_ptr++;
544 }
545 }
546
547 return content;
548 }
549
550
551 /* Adds the input number of elements to the windows's content. If no
552 content has been allocated yet, alloc_content() is called to do
553 this. The index of the first element added is returned, unless
554 there is a memory allocation error, in which case, (-1) is
555 returned. */
556 int
557 tui_add_content_elements (struct tui_gen_win_info *win_info,
558 int num_elements)
559 {
560 struct tui_win_element *element_ptr;
561 int i, index_start;
562
563 if (win_info->content == NULL)
564 {
565 win_info->content = tui_alloc_content (num_elements, win_info->type);
566 index_start = 0;
567 }
568 else
569 index_start = win_info->content_size;
570 if (win_info->content != NULL)
571 {
572 for (i = index_start; (i < num_elements + index_start); i++)
573 {
574 element_ptr = XNEW (struct tui_win_element);
575 win_info->content[i] = element_ptr;
576 init_content_element (element_ptr, win_info->type);
577 win_info->content_size++;
578 }
579 }
580
581 return index_start;
582 }
583
584 tui_source_window_base::~tui_source_window_base ()
585 {
586 xfree (fullname);
587 struct tui_gen_win_info *generic_win = execution_info;
588 if (generic_win != NULL)
589 {
590 tui_delete_win (generic_win->handle);
591 generic_win->handle = NULL;
592 tui_free_win_content (generic_win);
593 }
594 }
595
596 tui_data_window::~tui_data_window ()
597 {
598 if (generic.content != NULL)
599 {
600 tui_free_data_content (regs_content, regs_content_count);
601 regs_content = NULL;
602 regs_content_count = 0;
603 tui_free_data_content (data_content, data_content_count);
604 data_content = NULL;
605 data_content_count = 0;
606 regs_column_count = 1;
607 display_regs = false;
608 generic.content = NULL;
609 generic.content_size = 0;
610 }
611 }
612
613 tui_win_info::~tui_win_info ()
614 {
615 if (generic.handle != NULL)
616 {
617 tui_delete_win (generic.handle);
618 generic.handle = NULL;
619 tui_free_win_content (&generic);
620 }
621 if (generic.title)
622 xfree (generic.title);
623 }
624
625
626 void
627 tui_free_all_source_wins_content ()
628 {
629 for (tui_win_info *win_info : tui_source_windows ())
630 {
631 tui_free_win_content (&(win_info->generic));
632 tui_source_window_base *base = (tui_source_window_base *) win_info;
633 tui_free_win_content (base->execution_info);
634 }
635 }
636
637
638 void
639 tui_free_win_content (struct tui_gen_win_info *win_info)
640 {
641 if (win_info->content != NULL)
642 {
643 free_content (win_info->content,
644 win_info->content_size,
645 win_info->type);
646 win_info->content = NULL;
647 }
648 win_info->content_size = 0;
649 }
650
651
652 void
653 tui_free_data_content (tui_win_content content,
654 int content_size)
655 {
656 int i;
657
658 /* Remember that data window content elements are of type struct
659 tui_gen_win_info *, each of which whose single element is a data
660 element. */
661 for (i = 0; i < content_size; i++)
662 {
663 struct tui_gen_win_info *generic_win
664 = &content[i]->which_element.data_window;
665
666 if (generic_win != NULL)
667 {
668 tui_delete_win (generic_win->handle);
669 generic_win->handle = NULL;
670 tui_free_win_content (generic_win);
671 }
672 }
673 free_content (content,
674 content_size,
675 DATA_WIN);
676 }
677
678
679 /**********************************
680 ** LOCAL STATIC FUNCTIONS **
681 **********************************/
682
683
684 static void
685 free_content (tui_win_content content,
686 int content_size,
687 enum tui_win_type win_type)
688 {
689 if (content != NULL)
690 {
691 free_content_elements (content, content_size, win_type);
692 xfree (content);
693 }
694 }
695
696
697 /* free_content_elements().
698 */
699 static void
700 free_content_elements (tui_win_content content,
701 int content_size,
702 enum tui_win_type type)
703 {
704 if (content != NULL)
705 {
706 int i;
707
708 if (type == DISASSEM_WIN)
709 {
710 /* Free whole source block. */
711 xfree (content[0]->which_element.source.line);
712 }
713 else
714 {
715 for (i = 0; i < content_size; i++)
716 {
717 struct tui_win_element *element;
718
719 element = content[i];
720 if (element != NULL)
721 {
722 switch (type)
723 {
724 case SRC_WIN:
725 xfree (element->which_element.source.line);
726 break;
727 case DATA_WIN:
728 xfree (element);
729 break;
730 case DATA_ITEM_WIN:
731 /* Note that data elements are not allocated in
732 a single block, but individually, as
733 needed. */
734 if (element->which_element.data.type != TUI_REGISTER)
735 xfree ((void *)element->which_element.data.name);
736 xfree (element->which_element.data.value);
737 xfree (element->which_element.data.content);
738 xfree (element);
739 break;
740 case CMD_WIN:
741 xfree (element->which_element.command.line);
742 break;
743 default:
744 break;
745 }
746 }
747 }
748 }
749 if (type != DATA_WIN && type != DATA_ITEM_WIN)
750 xfree (content[0]); /* Free the element block. */
751 }
752 }
This page took 0.046746 seconds and 4 git commands to generate.