1 /* TUI display source window.
3 Copyright (C) 1998-2018 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/>. */
26 #include "breakpoint.h"
30 #include "filenames.h"
33 #include "tui/tui-data.h"
34 #include "tui/tui-stack.h"
35 #include "tui/tui-winsource.h"
36 #include "tui/tui-source.h"
37 #include "gdb_curses.h"
39 /* Function to display source in the source window. */
41 tui_set_source_content (struct symtab
*s
,
45 enum tui_status ret
= TUI_FAILURE
;
47 if (s
!= (struct symtab
*) NULL
)
49 int i
, c
, line_width
, nlines
;
52 if ((ret
= tui_alloc_source_buffer (TUI_SRC_WIN
)) == TUI_SUCCESS
)
54 line_width
= TUI_SRC_WIN
->generic
.width
- 1;
55 /* Take hilite (window border) into account, when
56 calculating the number of lines. */
57 nlines
= (line_no
+ (TUI_SRC_WIN
->generic
.height
- 2)) - line_no
;
58 scoped_fd desc
= open_source_file (s
);
63 const char *filename
= symtab_to_filename_for_display (s
);
64 char *name
= (char *) alloca (strlen (filename
) + 100);
66 sprintf (name
, "%s:%d", filename
, line_no
);
67 print_sys_errmsg (name
, errno
);
73 if (s
->line_charpos
== 0)
74 find_source_lines (s
, desc
.get ());
76 if (line_no
< 1 || line_no
> s
->nlines
)
77 printf_unfiltered ("Line number %d out of range; "
80 symtab_to_filename_for_display (s
),
82 else if (lseek (desc
.get (), s
->line_charpos
[line_no
- 1], 0)
84 perror_with_name (symtab_to_filename_for_display (s
));
87 int offset
, cur_line_no
, cur_line
, cur_len
, threshold
;
88 struct tui_gen_win_info
*locator
89 = tui_locator_win_info_ptr ();
90 struct tui_source_info
*src
91 = &TUI_SRC_WIN
->detail
.source_info
;
92 const char *s_filename
= symtab_to_filename_for_display (s
);
94 if (TUI_SRC_WIN
->generic
.title
)
95 xfree (TUI_SRC_WIN
->generic
.title
);
96 TUI_SRC_WIN
->generic
.title
= xstrdup (s_filename
);
98 xfree (src
->fullname
);
99 src
->fullname
= xstrdup (symtab_to_fullname (s
));
101 /* Determine the threshold for the length of the
102 line and the offset to start the display. */
103 offset
= src
->horizontal_offset
;
104 threshold
= (line_width
- 1) + offset
;
105 gdb_file_up stream
= desc
.to_file (FOPEN_RT
);
106 clearerr (stream
.get ());
108 src
->gdbarch
= get_objfile_arch (SYMTAB_OBJFILE (s
));
109 src
->start_line_or_addr
.loa
= LOA_LINE
;
110 cur_line_no
= src
->start_line_or_addr
.u
.line_no
= line_no
;
112 src_line
= (char *) xmalloc (
113 (threshold
+ 1) * sizeof (char));
114 while (cur_line
< nlines
)
116 struct tui_win_element
*element
117 = TUI_SRC_WIN
->generic
.content
[cur_line
];
119 /* Get the first character in the line. */
120 c
= fgetc (stream
.get ());
123 src_line
= TUI_SRC_WIN
->generic
.content
[cur_line
]
124 ->which_element
.source
.line
;
125 /* Init the line with the line number. */
126 sprintf (src_line
, "%-6d", cur_line_no
);
127 cur_len
= strlen (src_line
);
128 i
= cur_len
- ((cur_len
/ tui_tab_width
)
130 while (i
< tui_tab_width
)
132 src_line
[cur_len
] = ' ';
136 src_line
[cur_len
] = (char) 0;
138 /* Set whether element is the execution point
139 and whether there is a break point on it. */
140 element
->which_element
.source
.line_or_addr
.loa
=
142 element
->which_element
.source
.line_or_addr
.u
.line_no
=
144 element
->which_element
.source
.is_exec_point
=
145 (filename_cmp (locator
->content
[0]
146 ->which_element
.locator
.full_name
,
147 symtab_to_fullname (s
)) == 0
149 == locator
->content
[0]
150 ->which_element
.locator
.line_no
);
153 i
= strlen (src_line
) - 1;
156 if ((c
!= '\n') && (c
!= '\r')
157 && (++i
< threshold
))
159 if (c
< 040 && c
!= '\t')
162 src_line
[i
] = c
+ 0100;
170 { /* Store the charcter in the
171 line buffer. If it is a tab,
172 then translate to the correct
173 number of chars so we don't
174 overwrite our buffer. */
180 for (j
= i
- ((i
/ max_tab_len
)
194 { /* If we have not reached EOL, then
195 eat chars until we do. */
196 while (c
!= EOF
&& c
!= '\n' && c
!= '\r')
197 c
= fgetc (stream
.get ());
198 /* Handle non-'\n' end-of-line. */
200 && (c
= fgetc (stream
.get ())) != '\n'
203 ungetc (c
, stream
.get ());
209 while (c
!= EOF
&& c
!= '\n' && c
!= '\r'
211 && (c
= fgetc (stream
.get ())));
213 /* Now copy the line taking the offset into
217 else if (strlen (src_line
) > offset
)
218 strcpy (TUI_SRC_WIN
->generic
.content
[cur_line
]
219 ->which_element
.source
.line
,
222 TUI_SRC_WIN
->generic
.content
[cur_line
]
223 ->which_element
.source
.line
[0] = (char) 0;
229 TUI_SRC_WIN
->generic
.content_size
= nlines
;
239 /* elz: This function sets the contents of the source window to empty
240 except for a line in the middle with a warning message about the
241 source not being available. This function is called by
242 tui_erase_source_contents(), which in turn is invoked when the
243 source files cannot be accessed. */
246 tui_set_source_content_nil (struct tui_win_info
*win_info
,
247 const char *warning_string
)
253 line_width
= win_info
->generic
.width
- 1;
254 n_lines
= win_info
->generic
.height
- 2;
256 /* Set to empty each line in the window, except for the one which
257 contains the message. */
258 while (curr_line
< win_info
->generic
.content_size
)
260 /* Set the information related to each displayed line to null:
261 i.e. the line number is 0, there is no bp, it is not where
262 the program is stopped. */
264 struct tui_win_element
*element
= win_info
->generic
.content
[curr_line
];
266 element
->which_element
.source
.line_or_addr
.loa
= LOA_LINE
;
267 element
->which_element
.source
.line_or_addr
.u
.line_no
= 0;
268 element
->which_element
.source
.is_exec_point
= FALSE
;
269 element
->which_element
.source
.has_break
= FALSE
;
271 /* Set the contents of the line to blank. */
272 element
->which_element
.source
.line
[0] = (char) 0;
274 /* If the current line is in the middle of the screen, then we
275 want to display the 'no source available' message in it.
276 Note: the 'weird' arithmetic with the line width and height
277 comes from the function tui_erase_source_content(). We need
278 to keep the screen and the window's actual contents in
281 if (curr_line
== (n_lines
/ 2 + 1))
285 int warning_length
= strlen (warning_string
);
288 src_line
= element
->which_element
.source
.line
;
290 if (warning_length
>= ((line_width
- 1) / 2))
293 xpos
= (line_width
- 1) / 2 - warning_length
;
295 for (i
= 0; i
< xpos
; i
++)
298 sprintf (src_line
+ i
, "%s", warning_string
);
300 for (i
= xpos
+ warning_length
; i
< line_width
; i
++)
313 /* Function to display source in the source window. This function
314 initializes the horizontal scroll to 0. */
316 tui_show_symtab_source (struct gdbarch
*gdbarch
, struct symtab
*s
,
317 struct tui_line_or_address line
,
320 TUI_SRC_WIN
->detail
.source_info
.horizontal_offset
= 0;
321 tui_update_source_window_as_is (TUI_SRC_WIN
, gdbarch
, s
, line
, noerror
);
325 /* Answer whether the source is currently displayed in the source
328 tui_source_is_displayed (const char *fullname
)
330 return (TUI_SRC_WIN
!= NULL
331 && TUI_SRC_WIN
->generic
.content_in_use
332 && (filename_cmp (tui_locator_win_info_ptr ()->content
[0]
333 ->which_element
.locator
.full_name
,
338 /* Scroll the source forward or backward vertically. */
340 tui_vertical_source_scroll (enum tui_scroll_direction scroll_direction
,
343 if (TUI_SRC_WIN
->generic
.content
!= NULL
)
345 struct tui_line_or_address l
;
347 tui_win_content content
= TUI_SRC_WIN
->generic
.content
;
348 struct symtab_and_line cursal
= get_current_source_symtab_and_line ();
350 if (cursal
.symtab
== (struct symtab
*) NULL
)
351 s
= find_pc_line_symtab (get_frame_pc (get_selected_frame (NULL
)));
356 if (scroll_direction
== FORWARD_SCROLL
)
358 l
.u
.line_no
= content
[0]->which_element
.source
.line_or_addr
.u
.line_no
360 if (l
.u
.line_no
> s
->nlines
)
361 /* line = s->nlines - win_info->generic.content_size + 1; */
362 /* elz: fix for dts 23398. */
364 = content
[0]->which_element
.source
.line_or_addr
.u
.line_no
;
368 l
.u
.line_no
= content
[0]->which_element
.source
.line_or_addr
.u
.line_no
370 if (l
.u
.line_no
<= 0)
374 print_source_lines (s
, l
.u
.line_no
, l
.u
.line_no
+ 1, 0);