Don't cast a tui_win_info directly to tui_gen_win_info
[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 struct tui_win_info *src_win_list[2];
42 static struct tui_list source_windows = {src_win_list, 0};
43 static struct tui_win_info *win_with_focus = NULL;
44 static struct tui_layout_def layout_def = {
45 SRC_WIN, /* DISPLAY_MODE */
46 FALSE}; /* SPLIT */
47
48 static int win_resized = FALSE;
49
50
51 /*********************************
52 ** Static function forward decls
53 **********************************/
54 static void free_content (tui_win_content,
55 int,
56 enum tui_win_type);
57 static void free_content_elements (tui_win_content,
58 int,
59 enum tui_win_type);
60
61
62
63 /*********************************
64 ** PUBLIC FUNCTIONS
65 **********************************/
66
67 int
68 tui_win_is_source_type (enum tui_win_type win_type)
69 {
70 return (win_type == SRC_WIN || win_type == DISASSEM_WIN);
71 }
72
73 int
74 tui_win_is_auxillary (enum tui_win_type win_type)
75 {
76 return (win_type > MAX_MAJOR_WINDOWS);
77 }
78
79 int
80 tui_win_has_locator (struct tui_win_info *win_info)
81 {
82 return (win_info != NULL
83 && win_info->detail.source_info.has_locator);
84 }
85
86 void
87 tui_set_win_highlight (struct tui_win_info *win_info,
88 int highlight)
89 {
90 if (win_info != NULL)
91 win_info->is_highlighted = highlight;
92 }
93
94 /******************************************
95 ** ACCESSORS & MUTATORS FOR PRIVATE DATA
96 ******************************************/
97
98 /* Answer a whether the terminal window has been resized or not. */
99 int
100 tui_win_resized (void)
101 {
102 return win_resized;
103 }
104
105
106 /* Set a whether the terminal window has been resized or not. */
107 void
108 tui_set_win_resized_to (int resized)
109 {
110 win_resized = resized;
111 }
112
113
114 /* Answer a pointer to the current layout definition. */
115 struct tui_layout_def *
116 tui_layout_def (void)
117 {
118 return &layout_def;
119 }
120
121
122 /* Answer the window with the logical focus. */
123 struct tui_win_info *
124 tui_win_with_focus (void)
125 {
126 return win_with_focus;
127 }
128
129
130 /* Set the window that has the logical focus. */
131 void
132 tui_set_win_with_focus (struct tui_win_info *win_info)
133 {
134 win_with_focus = win_info;
135 }
136
137
138 /* Accessor for the current source window. Usually there is only one
139 source window (either source or disassembly), but both can be
140 displayed at the same time. */
141 struct tui_list *
142 tui_source_windows (void)
143 {
144 return &source_windows;
145 }
146
147
148 /* Clear the list of source windows. Usually there is only one source
149 window (either source or disassembly), but both can be displayed at
150 the same time. */
151 void
152 tui_clear_source_windows (void)
153 {
154 source_windows.list[0] = NULL;
155 source_windows.list[1] = NULL;
156 source_windows.count = 0;
157 }
158
159
160 /* Clear the pertinant detail in the source windows. */
161 void
162 tui_clear_source_windows_detail (void)
163 {
164 int i;
165
166 for (i = 0; i < (tui_source_windows ())->count; i++)
167 tui_clear_win_detail ((tui_source_windows ())->list[i]);
168 }
169
170
171 /* Add a window to the list of source windows. Usually there is only
172 one source window (either source or disassembly), but both can be
173 displayed at the same time. */
174 void
175 tui_add_to_source_windows (struct tui_win_info *win_info)
176 {
177 if (source_windows.count < 2)
178 source_windows.list[source_windows.count++] = win_info;
179 }
180
181
182 /* Clear the pertinant detail in the windows. */
183 void
184 tui_clear_win_detail (struct tui_win_info *win_info)
185 {
186 if (win_info != NULL)
187 {
188 switch (win_info->generic.type)
189 {
190 case SRC_WIN:
191 case DISASSEM_WIN:
192 win_info->detail.source_info.gdbarch = NULL;
193 win_info->detail.source_info.start_line_or_addr.loa = LOA_ADDRESS;
194 win_info->detail.source_info.start_line_or_addr.u.addr = 0;
195 win_info->detail.source_info.horizontal_offset = 0;
196 break;
197 case CMD_WIN:
198 wmove (win_info->generic.handle, 0, 0);
199 break;
200 case DATA_WIN:
201 win_info->detail.data_display_info.data_content = NULL;
202 win_info->detail.data_display_info.data_content_count = 0;
203 win_info->detail.data_display_info.regs_content = NULL;
204 win_info->detail.data_display_info.regs_content_count = 0;
205 win_info->detail.data_display_info.regs_column_count = 1;
206 win_info->detail.data_display_info.display_regs = FALSE;
207 break;
208 default:
209 break;
210 }
211 }
212 }
213
214
215 /* Accessor for the source execution info ptr. */
216 struct tui_gen_win_info *
217 tui_source_exec_info_win_ptr (void)
218 {
219 return &exec_info[0];
220 }
221
222
223 /* Accessor for the disassem execution info ptr. */
224 struct tui_gen_win_info *
225 tui_disassem_exec_info_win_ptr (void)
226 {
227 return &exec_info[1];
228 }
229
230
231 /* Accessor for the locator win info. Answers a pointer to the static
232 locator win info struct. */
233 struct tui_gen_win_info *
234 tui_locator_win_info_ptr (void)
235 {
236 return &_locator;
237 }
238
239
240 /* Accessor for the term_height. */
241 int
242 tui_term_height (void)
243 {
244 return term_height;
245 }
246
247
248 /* Mutator for the term height. */
249 void
250 tui_set_term_height_to (int h)
251 {
252 term_height = h;
253 }
254
255
256 /* Accessor for the term_width. */
257 int
258 tui_term_width (void)
259 {
260 return term_width;
261 }
262
263
264 /* Mutator for the term_width. */
265 void
266 tui_set_term_width_to (int w)
267 {
268 term_width = w;
269 }
270
271
272 /* Accessor for the current layout. */
273 enum tui_layout_type
274 tui_current_layout (void)
275 {
276 return current_layout;
277 }
278
279
280 /* Mutator for the current layout. */
281 void
282 tui_set_current_layout_to (enum tui_layout_type new_layout)
283 {
284 current_layout = new_layout;
285 }
286
287
288 /*****************************
289 ** OTHER PUBLIC FUNCTIONS
290 *****************************/
291
292
293 /* Answer the next window in the list, cycling back to the top if
294 necessary. */
295 struct tui_win_info *
296 tui_next_win (struct tui_win_info *cur_win)
297 {
298 int type = cur_win->generic.type;
299 struct tui_win_info *next_win = NULL;
300
301 if (cur_win->generic.type == CMD_WIN)
302 type = SRC_WIN;
303 else
304 type = cur_win->generic.type + 1;
305 while (type != cur_win->generic.type && (next_win == NULL))
306 {
307 if (tui_win_list[type]
308 && tui_win_list[type]->generic.is_visible)
309 next_win = tui_win_list[type];
310 else
311 {
312 if (type == CMD_WIN)
313 type = SRC_WIN;
314 else
315 type++;
316 }
317 }
318
319 return next_win;
320 }
321
322
323 /* Answer the prev window in the list, cycling back to the bottom if
324 necessary. */
325 struct tui_win_info *
326 tui_prev_win (struct tui_win_info *cur_win)
327 {
328 int type = cur_win->generic.type;
329 struct tui_win_info *prev = NULL;
330
331 if (cur_win->generic.type == SRC_WIN)
332 type = CMD_WIN;
333 else
334 type = cur_win->generic.type - 1;
335 while (type != cur_win->generic.type && (prev == NULL))
336 {
337 if (tui_win_list[type]
338 && tui_win_list[type]->generic.is_visible)
339 prev = tui_win_list[type];
340 else
341 {
342 if (type == SRC_WIN)
343 type = CMD_WIN;
344 else
345 type--;
346 }
347 }
348
349 return prev;
350 }
351
352
353 /* Answer the window represented by name. */
354 struct tui_win_info *
355 tui_partial_win_by_name (const char *name)
356 {
357 struct tui_win_info *win_info = NULL;
358
359 if (name != NULL)
360 {
361 int i = 0;
362
363 while (i < MAX_MAJOR_WINDOWS && win_info == NULL)
364 {
365 if (tui_win_list[i] != 0)
366 {
367 const char *cur_name =
368 tui_win_name (&tui_win_list[i]->generic);
369
370 if (strlen (name) <= strlen (cur_name)
371 && startswith (cur_name, name))
372 win_info = tui_win_list[i];
373 }
374 i++;
375 }
376 }
377
378 return win_info;
379 }
380
381
382 /* Answer the name of the window. */
383 const char *
384 tui_win_name (const struct tui_gen_win_info *win_info)
385 {
386 const char *name = NULL;
387
388 switch (win_info->type)
389 {
390 case SRC_WIN:
391 name = SRC_NAME;
392 break;
393 case CMD_WIN:
394 name = CMD_NAME;
395 break;
396 case DISASSEM_WIN:
397 name = DISASSEM_NAME;
398 break;
399 case DATA_WIN:
400 name = DATA_NAME;
401 break;
402 default:
403 name = "";
404 break;
405 }
406
407 return name;
408 }
409
410
411 void
412 tui_initialize_static_data (void)
413 {
414 tui_init_generic_part (tui_source_exec_info_win_ptr ());
415 tui_init_generic_part (tui_disassem_exec_info_win_ptr ());
416 tui_init_generic_part (tui_locator_win_info_ptr ());
417 }
418
419
420 struct tui_gen_win_info *
421 tui_alloc_generic_win_info (void)
422 {
423 struct tui_gen_win_info *win = XNEW (struct tui_gen_win_info);
424
425 tui_init_generic_part (win);
426
427 return win;
428 }
429
430
431 void
432 tui_init_generic_part (struct tui_gen_win_info *win)
433 {
434 win->width =
435 win->height =
436 win->origin.x =
437 win->origin.y =
438 win->viewport_height =
439 win->content_size =
440 win->last_visible_line = 0;
441 win->handle = NULL;
442 win->content = NULL;
443 win->content_in_use =
444 win->is_visible = FALSE;
445 win->title = 0;
446 }
447
448
449 /* init_content_element().
450 */
451 static void
452 init_content_element (struct tui_win_element *element,
453 enum tui_win_type type)
454 {
455 switch (type)
456 {
457 case SRC_WIN:
458 case DISASSEM_WIN:
459 element->which_element.source.line = NULL;
460 element->which_element.source.line_or_addr.loa = LOA_LINE;
461 element->which_element.source.line_or_addr.u.line_no = 0;
462 element->which_element.source.is_exec_point = FALSE;
463 element->which_element.source.has_break = FALSE;
464 break;
465 case DATA_WIN:
466 tui_init_generic_part (&element->which_element.data_window);
467 element->which_element.data_window.type = DATA_ITEM_WIN;
468 element->which_element.data_window.content =
469 tui_alloc_content (1, DATA_ITEM_WIN);
470 element->which_element.data_window.content_size = 1;
471 break;
472 case CMD_WIN:
473 element->which_element.command.line = NULL;
474 break;
475 case DATA_ITEM_WIN:
476 element->which_element.data.name = NULL;
477 element->which_element.data.type = TUI_REGISTER;
478 element->which_element.data.item_no = UNDEFINED_ITEM;
479 element->which_element.data.value = NULL;
480 element->which_element.data.highlight = FALSE;
481 element->which_element.data.content = NULL;
482 break;
483 case LOCATOR_WIN:
484 element->which_element.locator.full_name[0] =
485 element->which_element.locator.proc_name[0] = (char) 0;
486 element->which_element.locator.line_no = 0;
487 element->which_element.locator.addr = 0;
488 break;
489 case EXEC_INFO_WIN:
490 memset(element->which_element.simple_string, ' ',
491 sizeof(element->which_element.simple_string));
492 break;
493 default:
494 break;
495 }
496 }
497
498 static void
499 init_win_info (struct tui_win_info *win_info)
500 {
501 tui_init_generic_part (&win_info->generic);
502 win_info->can_highlight =
503 win_info->is_highlighted = FALSE;
504 switch (win_info->generic.type)
505 {
506 case SRC_WIN:
507 case DISASSEM_WIN:
508 win_info->detail.source_info.execution_info = NULL;
509 win_info->detail.source_info.has_locator = FALSE;
510 win_info->detail.source_info.horizontal_offset = 0;
511 win_info->detail.source_info.gdbarch = NULL;
512 win_info->detail.source_info.start_line_or_addr.loa = LOA_ADDRESS;
513 win_info->detail.source_info.start_line_or_addr.u.addr = 0;
514 win_info->detail.source_info.fullname = NULL;
515 break;
516 case DATA_WIN:
517 win_info->detail.data_display_info.data_content = NULL;
518 win_info->detail.data_display_info.data_content_count = 0;
519 win_info->detail.data_display_info.regs_content = NULL;
520 win_info->detail.data_display_info.regs_content_count = 0;
521 win_info->detail.data_display_info.regs_column_count = 1;
522 win_info->detail.data_display_info.display_regs = FALSE;
523 win_info->detail.data_display_info.current_group = 0;
524 break;
525 case CMD_WIN:
526 break;
527 }
528 }
529
530
531 struct tui_win_info *
532 tui_alloc_win_info (enum tui_win_type type)
533 {
534 struct tui_win_info *win_info = XNEW (struct tui_win_info);
535
536 win_info->generic.type = type;
537 init_win_info (win_info);
538
539 return win_info;
540 }
541
542
543 /* Allocates the content and elements in a block. */
544 tui_win_content
545 tui_alloc_content (int num_elements, enum tui_win_type type)
546 {
547 tui_win_content content;
548 struct tui_win_element *element_block_ptr;
549 int i;
550
551 content = XNEWVEC (struct tui_win_element *, num_elements);
552
553 /*
554 * All windows, except the data window, can allocate the
555 * elements in a chunk. The data window cannot because items
556 * can be added/removed from the data display by the user at any
557 * time.
558 */
559 if (type != DATA_WIN)
560 {
561 element_block_ptr = XNEWVEC (struct tui_win_element, num_elements);
562 for (i = 0; i < num_elements; i++)
563 {
564 content[i] = element_block_ptr;
565 init_content_element (content[i], type);
566 element_block_ptr++;
567 }
568 }
569
570 return content;
571 }
572
573
574 /* Adds the input number of elements to the windows's content. If no
575 content has been allocated yet, alloc_content() is called to do
576 this. The index of the first element added is returned, unless
577 there is a memory allocation error, in which case, (-1) is
578 returned. */
579 int
580 tui_add_content_elements (struct tui_gen_win_info *win_info,
581 int num_elements)
582 {
583 struct tui_win_element *element_ptr;
584 int i, index_start;
585
586 if (win_info->content == NULL)
587 {
588 win_info->content = tui_alloc_content (num_elements, win_info->type);
589 index_start = 0;
590 }
591 else
592 index_start = win_info->content_size;
593 if (win_info->content != NULL)
594 {
595 for (i = index_start; (i < num_elements + index_start); i++)
596 {
597 element_ptr = XNEW (struct tui_win_element);
598 win_info->content[i] = element_ptr;
599 init_content_element (element_ptr, win_info->type);
600 win_info->content_size++;
601 }
602 }
603
604 return index_start;
605 }
606
607
608 /* Delete all curses windows associated with win_info, leaving
609 everything else intact. */
610 void
611 tui_del_window (struct tui_win_info *win_info)
612 {
613 struct tui_gen_win_info *generic_win;
614
615 switch (win_info->generic.type)
616 {
617 case SRC_WIN:
618 case DISASSEM_WIN:
619 generic_win = tui_locator_win_info_ptr ();
620 if (generic_win != NULL)
621 {
622 tui_delete_win (generic_win->handle);
623 generic_win->handle = NULL;
624 generic_win->is_visible = FALSE;
625 }
626 if (win_info->detail.source_info.fullname)
627 {
628 xfree (win_info->detail.source_info.fullname);
629 win_info->detail.source_info.fullname = NULL;
630 }
631 generic_win = win_info->detail.source_info.execution_info;
632 if (generic_win != NULL)
633 {
634 tui_delete_win (generic_win->handle);
635 generic_win->handle = NULL;
636 generic_win->is_visible = FALSE;
637 }
638 break;
639 case DATA_WIN:
640 if (win_info->generic.content != NULL)
641 {
642 tui_del_data_windows (win_info->detail.data_display_info.regs_content,
643 win_info->detail.data_display_info.regs_content_count);
644 tui_del_data_windows (win_info->detail.data_display_info.data_content,
645 win_info->detail.data_display_info.data_content_count);
646 }
647 break;
648 default:
649 break;
650 }
651 if (win_info->generic.handle != NULL)
652 {
653 tui_delete_win (win_info->generic.handle);
654 win_info->generic.handle = NULL;
655 win_info->generic.is_visible = FALSE;
656 }
657 }
658
659
660 void
661 tui_free_window (struct tui_win_info *win_info)
662 {
663 struct tui_gen_win_info *generic_win;
664
665 switch (win_info->generic.type)
666 {
667 case SRC_WIN:
668 case DISASSEM_WIN:
669 if (win_info->detail.source_info.fullname)
670 {
671 xfree (win_info->detail.source_info.fullname);
672 win_info->detail.source_info.fullname = NULL;
673 }
674 generic_win = win_info->detail.source_info.execution_info;
675 if (generic_win != NULL)
676 {
677 tui_delete_win (generic_win->handle);
678 generic_win->handle = NULL;
679 tui_free_win_content (generic_win);
680 }
681 break;
682 case DATA_WIN:
683 if (win_info->generic.content != NULL)
684 {
685 tui_free_data_content (win_info->detail.data_display_info.regs_content,
686 win_info->detail.data_display_info.regs_content_count);
687 win_info->detail.data_display_info.regs_content = NULL;
688 win_info->detail.data_display_info.regs_content_count = 0;
689 tui_free_data_content (win_info->detail.data_display_info.data_content,
690 win_info->detail.data_display_info.data_content_count);
691 win_info->detail.data_display_info.data_content = NULL;
692 win_info->detail.data_display_info.data_content_count = 0;
693 win_info->detail.data_display_info.regs_column_count = 1;
694 win_info->detail.data_display_info.display_regs = FALSE;
695 win_info->generic.content = NULL;
696 win_info->generic.content_size = 0;
697 }
698 break;
699 default:
700 break;
701 }
702 if (win_info->generic.handle != NULL)
703 {
704 tui_delete_win (win_info->generic.handle);
705 win_info->generic.handle = NULL;
706 tui_free_win_content (&win_info->generic);
707 }
708 if (win_info->generic.title)
709 xfree (win_info->generic.title);
710 xfree (win_info);
711 }
712
713
714 void
715 tui_free_all_source_wins_content (void)
716 {
717 int i;
718
719 for (i = 0; i < (tui_source_windows ())->count; i++)
720 {
721 struct tui_win_info *win_info = (tui_source_windows ())->list[i];
722
723 if (win_info != NULL)
724 {
725 tui_free_win_content (&(win_info->generic));
726 tui_free_win_content (win_info->detail.source_info.execution_info);
727 }
728 }
729 }
730
731
732 void
733 tui_free_win_content (struct tui_gen_win_info *win_info)
734 {
735 if (win_info->content != NULL)
736 {
737 free_content (win_info->content,
738 win_info->content_size,
739 win_info->type);
740 win_info->content = NULL;
741 }
742 win_info->content_size = 0;
743 }
744
745
746 void
747 tui_del_data_windows (tui_win_content content,
748 int content_size)
749 {
750 int i;
751
752 /* Remember that data window content elements are of type struct
753 tui_gen_win_info *, each of which whose single element is a data
754 element. */
755 for (i = 0; i < content_size; i++)
756 {
757 struct tui_gen_win_info *generic_win
758 = &content[i]->which_element.data_window;
759
760 if (generic_win != NULL)
761 {
762 tui_delete_win (generic_win->handle);
763 generic_win->handle = NULL;
764 generic_win->is_visible = FALSE;
765 }
766 }
767 }
768
769
770 void
771 tui_free_data_content (tui_win_content content,
772 int content_size)
773 {
774 int i;
775
776 /* Remember that data window content elements are of type struct
777 tui_gen_win_info *, each of which whose single element is a data
778 element. */
779 for (i = 0; i < content_size; i++)
780 {
781 struct tui_gen_win_info *generic_win
782 = &content[i]->which_element.data_window;
783
784 if (generic_win != NULL)
785 {
786 tui_delete_win (generic_win->handle);
787 generic_win->handle = NULL;
788 tui_free_win_content (generic_win);
789 }
790 }
791 free_content (content,
792 content_size,
793 DATA_WIN);
794 }
795
796
797 /**********************************
798 ** LOCAL STATIC FUNCTIONS **
799 **********************************/
800
801
802 static void
803 free_content (tui_win_content content,
804 int content_size,
805 enum tui_win_type win_type)
806 {
807 if (content != NULL)
808 {
809 free_content_elements (content, content_size, win_type);
810 xfree (content);
811 }
812 }
813
814
815 /* free_content_elements().
816 */
817 static void
818 free_content_elements (tui_win_content content,
819 int content_size,
820 enum tui_win_type type)
821 {
822 if (content != NULL)
823 {
824 int i;
825
826 if (type == DISASSEM_WIN)
827 {
828 /* Free whole source block. */
829 xfree (content[0]->which_element.source.line);
830 }
831 else
832 {
833 for (i = 0; i < content_size; i++)
834 {
835 struct tui_win_element *element;
836
837 element = content[i];
838 if (element != NULL)
839 {
840 switch (type)
841 {
842 case SRC_WIN:
843 xfree (element->which_element.source.line);
844 break;
845 case DATA_WIN:
846 xfree (element);
847 break;
848 case DATA_ITEM_WIN:
849 /* Note that data elements are not allocated in
850 a single block, but individually, as
851 needed. */
852 if (element->which_element.data.type != TUI_REGISTER)
853 xfree ((void *)element->which_element.data.name);
854 xfree (element->which_element.data.value);
855 xfree (element->which_element.data.content);
856 xfree (element);
857 break;
858 case CMD_WIN:
859 xfree (element->which_element.command.line);
860 break;
861 default:
862 break;
863 }
864 }
865 }
866 }
867 if (type != DATA_WIN && type != DATA_ITEM_WIN)
868 xfree (content[0]); /* Free the element block. */
869 }
870 }
This page took 0.046649 seconds and 4 git commands to generate.