1 /* TUI data manipulation routines.
3 Copyright (C) 1998-2015 Free Software Foundation, Inc.
5 Contributed by Hewlett-Packard Company.
7 This file is part of GDB.
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.
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.
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/>. */
25 #include "tui/tui-data.h"
26 #include "tui/tui-wingeneral.h"
27 #include "gdb_curses.h"
29 /****************************
30 ** GLOBAL DECLARATIONS
31 ****************************/
32 struct tui_win_info
*(tui_win_list
[MAX_MAJOR_WINDOWS
]);
34 /***************************
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 int default_tab_len
= DEFAULT_TAB_LEN
;
44 static struct tui_win_info
*win_with_focus
= (struct tui_win_info
*) NULL
;
45 static struct tui_layout_def layout_def
= {
46 SRC_WIN
, /* DISPLAY_MODE */
49 static int win_resized
= FALSE
;
52 /*********************************
53 ** Static function forward decls
54 **********************************/
55 static void free_content (tui_win_content
,
58 static void free_content_elements (tui_win_content
,
64 /*********************************
66 **********************************/
69 tui_win_is_source_type (enum tui_win_type win_type
)
71 return (win_type
== SRC_WIN
|| win_type
== DISASSEM_WIN
);
75 tui_win_is_auxillary (enum tui_win_type win_type
)
77 return (win_type
> MAX_MAJOR_WINDOWS
);
81 tui_win_has_locator (struct tui_win_info
*win_info
)
83 return (win_info
!= NULL
84 && win_info
->detail
.source_info
.has_locator
);
88 tui_set_win_highlight (struct tui_win_info
*win_info
,
92 win_info
->is_highlighted
= highlight
;
95 /******************************************
96 ** ACCESSORS & MUTATORS FOR PRIVATE DATA
97 ******************************************/
99 /* Answer a whether the terminal window has been resized or not. */
101 tui_win_resized (void)
107 /* Set a whether the terminal window has been resized or not. */
109 tui_set_win_resized_to (int resized
)
111 win_resized
= resized
;
115 /* Answer a pointer to the current layout definition. */
116 struct tui_layout_def
*
117 tui_layout_def (void)
123 /* Answer the window with the logical focus. */
124 struct tui_win_info
*
125 tui_win_with_focus (void)
127 return win_with_focus
;
131 /* Set the window that has the logical focus. */
133 tui_set_win_with_focus (struct tui_win_info
*win_info
)
135 win_with_focus
= win_info
;
139 /* Answer the length in chars, of tabs. */
141 tui_default_tab_len (void)
143 return default_tab_len
;
147 /* Set the length in chars, of tabs. */
149 tui_set_default_tab_len (int len
)
151 default_tab_len
= len
;
155 /* Accessor for the current source window. Usually there is only one
156 source window (either source or disassembly), but both can be
157 displayed at the same time. */
159 tui_source_windows (void)
161 return &source_windows
;
165 /* Clear the list of source windows. Usually there is only one source
166 window (either source or disassembly), but both can be displayed at
169 tui_clear_source_windows (void)
171 source_windows
.list
[0] = NULL
;
172 source_windows
.list
[1] = NULL
;
173 source_windows
.count
= 0;
177 /* Clear the pertinant detail in the source windows. */
179 tui_clear_source_windows_detail (void)
183 for (i
= 0; i
< (tui_source_windows ())->count
; i
++)
184 tui_clear_win_detail ((tui_source_windows ())->list
[i
]);
188 /* Add a window to the list of source windows. Usually there is only
189 one source window (either source or disassembly), but both can be
190 displayed at the same time. */
192 tui_add_to_source_windows (struct tui_win_info
*win_info
)
194 if (source_windows
.count
< 2)
195 source_windows
.list
[source_windows
.count
++] = (void *) win_info
;
199 /* Clear the pertinant detail in the windows. */
201 tui_clear_win_detail (struct tui_win_info
*win_info
)
203 if (win_info
!= NULL
)
205 switch (win_info
->generic
.type
)
209 win_info
->detail
.source_info
.gdbarch
= NULL
;
210 win_info
->detail
.source_info
.start_line_or_addr
.loa
= LOA_ADDRESS
;
211 win_info
->detail
.source_info
.start_line_or_addr
.u
.addr
= 0;
212 win_info
->detail
.source_info
.horizontal_offset
= 0;
215 win_info
->detail
.command_info
.cur_line
=
216 win_info
->detail
.command_info
.curch
= 0;
219 win_info
->detail
.data_display_info
.data_content
=
220 (tui_win_content
) NULL
;
221 win_info
->detail
.data_display_info
.data_content_count
= 0;
222 win_info
->detail
.data_display_info
.regs_content
=
223 (tui_win_content
) NULL
;
224 win_info
->detail
.data_display_info
.regs_content_count
= 0;
225 win_info
->detail
.data_display_info
.regs_column_count
= 1;
226 win_info
->detail
.data_display_info
.display_regs
= FALSE
;
235 /* Accessor for the source execution info ptr. */
236 struct tui_gen_win_info
*
237 tui_source_exec_info_win_ptr (void)
239 return &exec_info
[0];
243 /* Accessor for the disassem execution info ptr. */
244 struct tui_gen_win_info
*
245 tui_disassem_exec_info_win_ptr (void)
247 return &exec_info
[1];
251 /* Accessor for the locator win info. Answers a pointer to the static
252 locator win info struct. */
253 struct tui_gen_win_info
*
254 tui_locator_win_info_ptr (void)
260 /* Accessor for the term_height. */
262 tui_term_height (void)
268 /* Mutator for the term height. */
270 tui_set_term_height_to (int h
)
276 /* Accessor for the term_width. */
278 tui_term_width (void)
284 /* Mutator for the term_width. */
286 tui_set_term_width_to (int w
)
292 /* Accessor for the current layout. */
294 tui_current_layout (void)
296 return current_layout
;
300 /* Mutator for the current layout. */
302 tui_set_current_layout_to (enum tui_layout_type new_layout
)
304 current_layout
= new_layout
;
308 /*****************************
309 ** OTHER PUBLIC FUNCTIONS
310 *****************************/
313 /* Answer the next window in the list, cycling back to the top if
315 struct tui_win_info
*
316 tui_next_win (struct tui_win_info
*cur_win
)
318 int type
= cur_win
->generic
.type
;
319 struct tui_win_info
*next_win
= (struct tui_win_info
*) NULL
;
321 if (cur_win
->generic
.type
== CMD_WIN
)
324 type
= cur_win
->generic
.type
+ 1;
325 while (type
!= cur_win
->generic
.type
&& (next_win
== NULL
))
327 if (tui_win_list
[type
]
328 && tui_win_list
[type
]->generic
.is_visible
)
329 next_win
= tui_win_list
[type
];
343 /* Answer the prev window in the list, cycling back to the bottom if
345 struct tui_win_info
*
346 tui_prev_win (struct tui_win_info
*cur_win
)
348 int type
= cur_win
->generic
.type
;
349 struct tui_win_info
*prev
= (struct tui_win_info
*) NULL
;
351 if (cur_win
->generic
.type
== SRC_WIN
)
354 type
= cur_win
->generic
.type
- 1;
355 while (type
!= cur_win
->generic
.type
&& (prev
== NULL
))
357 if (tui_win_list
[type
]
358 && tui_win_list
[type
]->generic
.is_visible
)
359 prev
= tui_win_list
[type
];
373 /* Answer the window represented by name. */
374 struct tui_win_info
*
375 tui_partial_win_by_name (char *name
)
377 struct tui_win_info
*win_info
= (struct tui_win_info
*) NULL
;
379 if (name
!= (char *) NULL
)
383 while (i
< MAX_MAJOR_WINDOWS
&& win_info
== NULL
)
385 if (tui_win_list
[i
] != 0)
387 char *cur_name
= tui_win_name (&tui_win_list
[i
]->generic
);
389 if (strlen (name
) <= strlen (cur_name
)
390 && startswith (cur_name
, name
))
391 win_info
= tui_win_list
[i
];
401 /* Answer the name of the window. */
403 tui_win_name (struct tui_gen_win_info
*win_info
)
405 char *name
= (char *) NULL
;
407 switch (win_info
->type
)
416 name
= DISASSEM_NAME
;
431 tui_initialize_static_data (void)
433 tui_init_generic_part (tui_source_exec_info_win_ptr ());
434 tui_init_generic_part (tui_disassem_exec_info_win_ptr ());
435 tui_init_generic_part (tui_locator_win_info_ptr ());
439 struct tui_gen_win_info
*
440 tui_alloc_generic_win_info (void)
442 struct tui_gen_win_info
*win
;
444 if ((win
= XNEW (struct tui_gen_win_info
)) != NULL
)
445 tui_init_generic_part (win
);
452 tui_init_generic_part (struct tui_gen_win_info
*win
)
458 win
->viewport_height
=
460 win
->last_visible_line
= 0;
461 win
->handle
= (WINDOW
*) NULL
;
463 win
->content_in_use
=
464 win
->is_visible
= FALSE
;
469 /* init_content_element().
472 init_content_element (struct tui_win_element
*element
,
473 enum tui_win_type type
)
475 element
->highlight
= FALSE
;
480 element
->which_element
.source
.line
= (char *) NULL
;
481 element
->which_element
.source
.line_or_addr
.loa
= LOA_LINE
;
482 element
->which_element
.source
.line_or_addr
.u
.line_no
= 0;
483 element
->which_element
.source
.is_exec_point
= FALSE
;
484 element
->which_element
.source
.has_break
= FALSE
;
487 tui_init_generic_part (&element
->which_element
.data_window
);
488 element
->which_element
.data_window
.type
= DATA_ITEM_WIN
;
489 element
->which_element
.data_window
.content
=
490 tui_alloc_content (1, DATA_ITEM_WIN
);
491 element
->which_element
.data_window
.content_size
= 1;
494 element
->which_element
.command
.line
= (char *) NULL
;
497 element
->which_element
.data
.name
= (char *) NULL
;
498 element
->which_element
.data
.type
= TUI_REGISTER
;
499 element
->which_element
.data
.item_no
= UNDEFINED_ITEM
;
500 element
->which_element
.data
.value
= NULL
;
501 element
->which_element
.data
.highlight
= FALSE
;
502 element
->which_element
.data
.content
= (char*) NULL
;
505 element
->which_element
.locator
.full_name
[0] =
506 element
->which_element
.locator
.proc_name
[0] = (char) 0;
507 element
->which_element
.locator
.line_no
= 0;
508 element
->which_element
.locator
.addr
= 0;
511 memset(element
->which_element
.simple_string
, ' ',
512 sizeof(element
->which_element
.simple_string
));
520 init_win_info (struct tui_win_info
*win_info
)
522 tui_init_generic_part (&win_info
->generic
);
523 win_info
->can_highlight
=
524 win_info
->is_highlighted
= FALSE
;
525 switch (win_info
->generic
.type
)
529 win_info
->detail
.source_info
.execution_info
530 = (struct tui_gen_win_info
*) NULL
;
531 win_info
->detail
.source_info
.has_locator
= FALSE
;
532 win_info
->detail
.source_info
.horizontal_offset
= 0;
533 win_info
->detail
.source_info
.gdbarch
= NULL
;
534 win_info
->detail
.source_info
.start_line_or_addr
.loa
= LOA_ADDRESS
;
535 win_info
->detail
.source_info
.start_line_or_addr
.u
.addr
= 0;
536 win_info
->detail
.source_info
.fullname
= NULL
;
539 win_info
->detail
.data_display_info
.data_content
= (tui_win_content
) NULL
;
540 win_info
->detail
.data_display_info
.data_content_count
= 0;
541 win_info
->detail
.data_display_info
.regs_content
= (tui_win_content
) NULL
;
542 win_info
->detail
.data_display_info
.regs_content_count
= 0;
543 win_info
->detail
.data_display_info
.regs_column_count
= 1;
544 win_info
->detail
.data_display_info
.display_regs
= FALSE
;
545 win_info
->detail
.data_display_info
.current_group
= 0;
548 win_info
->detail
.command_info
.cur_line
= 0;
549 win_info
->detail
.command_info
.curch
= 0;
552 win_info
->detail
.opaque
= NULL
;
558 struct tui_win_info
*
559 tui_alloc_win_info (enum tui_win_type type
)
561 struct tui_win_info
*win_info
;
563 win_info
= XNEW (struct tui_win_info
);
564 if (win_info
!= NULL
)
566 win_info
->generic
.type
= type
;
567 init_win_info (win_info
);
574 /* Allocates the content and elements in a block. */
576 tui_alloc_content (int num_elements
, enum tui_win_type type
)
578 tui_win_content content
;
579 char *element_block_ptr
;
582 content
= xmalloc (sizeof (struct tui_win_element
*) *num_elements
);
586 * All windows, except the data window, can allocate the
587 * elements in a chunk. The data window cannot because items
588 * can be added/removed from the data display by the user at any
591 if (type
!= DATA_WIN
)
594 xmalloc (sizeof (struct tui_win_element
) * num_elements
);
595 if (element_block_ptr
!= NULL
)
597 for (i
= 0; i
< num_elements
; i
++)
599 content
[i
] = (struct tui_win_element
*) element_block_ptr
;
600 init_content_element (content
[i
], type
);
601 element_block_ptr
+= sizeof (struct tui_win_element
);
607 content
= (tui_win_content
) NULL
;
616 /* Adds the input number of elements to the windows's content. If no
617 content has been allocated yet, alloc_content() is called to do
618 this. The index of the first element added is returned, unless
619 there is a memory allocation error, in which case, (-1) is
622 tui_add_content_elements (struct tui_gen_win_info
*win_info
,
625 struct tui_win_element
*element_ptr
;
628 if (win_info
->content
== NULL
)
630 win_info
->content
= tui_alloc_content (num_elements
, win_info
->type
);
634 index_start
= win_info
->content_size
;
635 if (win_info
->content
!= NULL
)
637 for (i
= index_start
; (i
< num_elements
+ index_start
); i
++)
639 if ((element_ptr
= XNEW (struct tui_win_element
)) != NULL
)
641 win_info
->content
[i
] = (void *) element_ptr
;
642 init_content_element (element_ptr
, win_info
->type
);
643 win_info
->content_size
++;
645 else /* Things must be really hosed now! We ran out of
655 /* Delete all curses windows associated with win_info, leaving
656 everything else intact. */
658 tui_del_window (struct tui_win_info
*win_info
)
660 struct tui_gen_win_info
*generic_win
;
662 switch (win_info
->generic
.type
)
666 generic_win
= tui_locator_win_info_ptr ();
667 if (generic_win
!= (struct tui_gen_win_info
*) NULL
)
669 tui_delete_win (generic_win
->handle
);
670 generic_win
->handle
= (WINDOW
*) NULL
;
671 generic_win
->is_visible
= FALSE
;
673 if (win_info
->detail
.source_info
.fullname
)
675 xfree (win_info
->detail
.source_info
.fullname
);
676 win_info
->detail
.source_info
.fullname
= NULL
;
678 generic_win
= win_info
->detail
.source_info
.execution_info
;
679 if (generic_win
!= (struct tui_gen_win_info
*) NULL
)
681 tui_delete_win (generic_win
->handle
);
682 generic_win
->handle
= (WINDOW
*) NULL
;
683 generic_win
->is_visible
= FALSE
;
687 if (win_info
->generic
.content
!= NULL
)
689 tui_del_data_windows (win_info
->detail
.data_display_info
.regs_content
,
690 win_info
->detail
.data_display_info
.regs_content_count
);
691 tui_del_data_windows (win_info
->detail
.data_display_info
.data_content
,
692 win_info
->detail
.data_display_info
.data_content_count
);
698 if (win_info
->generic
.handle
!= (WINDOW
*) NULL
)
700 tui_delete_win (win_info
->generic
.handle
);
701 win_info
->generic
.handle
= (WINDOW
*) NULL
;
702 win_info
->generic
.is_visible
= FALSE
;
708 tui_free_window (struct tui_win_info
*win_info
)
710 struct tui_gen_win_info
*generic_win
;
712 switch (win_info
->generic
.type
)
716 if (win_info
->detail
.source_info
.fullname
)
718 xfree (win_info
->detail
.source_info
.fullname
);
719 win_info
->detail
.source_info
.fullname
= NULL
;
721 generic_win
= win_info
->detail
.source_info
.execution_info
;
722 if (generic_win
!= (struct tui_gen_win_info
*) NULL
)
724 tui_delete_win (generic_win
->handle
);
725 generic_win
->handle
= (WINDOW
*) NULL
;
726 tui_free_win_content (generic_win
);
730 if (win_info
->generic
.content
!= NULL
)
732 tui_free_data_content (win_info
->detail
.data_display_info
.regs_content
,
733 win_info
->detail
.data_display_info
.regs_content_count
);
734 win_info
->detail
.data_display_info
.regs_content
=
735 (tui_win_content
) NULL
;
736 win_info
->detail
.data_display_info
.regs_content_count
= 0;
737 tui_free_data_content (win_info
->detail
.data_display_info
.data_content
,
738 win_info
->detail
.data_display_info
.data_content_count
);
739 win_info
->detail
.data_display_info
.data_content
=
740 (tui_win_content
) NULL
;
741 win_info
->detail
.data_display_info
.data_content_count
= 0;
742 win_info
->detail
.data_display_info
.regs_column_count
= 1;
743 win_info
->detail
.data_display_info
.display_regs
= FALSE
;
744 win_info
->generic
.content
= NULL
;
745 win_info
->generic
.content_size
= 0;
751 if (win_info
->generic
.handle
!= (WINDOW
*) NULL
)
753 tui_delete_win (win_info
->generic
.handle
);
754 win_info
->generic
.handle
= (WINDOW
*) NULL
;
755 tui_free_win_content (&win_info
->generic
);
757 if (win_info
->generic
.title
)
758 xfree (win_info
->generic
.title
);
764 tui_free_all_source_wins_content (void)
768 for (i
= 0; i
< (tui_source_windows ())->count
; i
++)
770 struct tui_win_info
*win_info
= (tui_source_windows ())->list
[i
];
772 if (win_info
!= NULL
)
774 tui_free_win_content (&(win_info
->generic
));
775 tui_free_win_content (win_info
->detail
.source_info
.execution_info
);
782 tui_free_win_content (struct tui_gen_win_info
*win_info
)
784 if (win_info
->content
!= NULL
)
786 free_content ((tui_win_content
) win_info
->content
,
787 win_info
->content_size
,
789 win_info
->content
= NULL
;
791 win_info
->content_size
= 0;
796 tui_del_data_windows (tui_win_content content
,
801 /* Remember that data window content elements are of type struct
802 tui_gen_win_info *, each of which whose single element is a data
804 for (i
= 0; i
< content_size
; i
++)
806 struct tui_gen_win_info
*generic_win
807 = &content
[i
]->which_element
.data_window
;
809 if (generic_win
!= (struct tui_gen_win_info
*) NULL
)
811 tui_delete_win (generic_win
->handle
);
812 generic_win
->handle
= (WINDOW
*) NULL
;
813 generic_win
->is_visible
= FALSE
;
820 tui_free_data_content (tui_win_content content
,
825 /* Remember that data window content elements are of type struct
826 tui_gen_win_info *, each of which whose single element is a data
828 for (i
= 0; i
< content_size
; i
++)
830 struct tui_gen_win_info
*generic_win
831 = &content
[i
]->which_element
.data_window
;
833 if (generic_win
!= (struct tui_gen_win_info
*) NULL
)
835 tui_delete_win (generic_win
->handle
);
836 generic_win
->handle
= (WINDOW
*) NULL
;
837 tui_free_win_content (generic_win
);
840 free_content (content
,
846 /**********************************
847 ** LOCAL STATIC FUNCTIONS **
848 **********************************/
852 free_content (tui_win_content content
,
854 enum tui_win_type win_type
)
856 if (content
!= (tui_win_content
) NULL
)
858 free_content_elements (content
, content_size
, win_type
);
864 /* free_content_elements().
867 free_content_elements (tui_win_content content
,
869 enum tui_win_type type
)
871 if (content
!= (tui_win_content
) NULL
)
875 if (type
== SRC_WIN
|| type
== DISASSEM_WIN
)
877 /* Free whole source block. */
878 xfree (content
[0]->which_element
.source
.line
);
882 for (i
= 0; i
< content_size
; i
++)
884 struct tui_win_element
*element
;
886 element
= content
[i
];
887 if (element
!= (struct tui_win_element
*) NULL
)
895 /* Note that data elements are not allocated in
896 a single block, but individually, as
898 if (element
->which_element
.data
.type
!= TUI_REGISTER
)
899 xfree ((void *)element
->which_element
.data
.name
);
900 xfree (element
->which_element
.data
.value
);
901 xfree (element
->which_element
.data
.content
);
905 xfree (element
->which_element
.command
.line
);
913 if (type
!= DATA_WIN
&& type
!= DATA_ITEM_WIN
)
914 xfree (content
[0]); /* Free the element block. */