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