1 /* TUI display source window.
3 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009,
4 2010 Free Software Foundation, Inc.
6 Contributed by Hewlett-Packard Company.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "breakpoint.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"
38 #include "gdb_string.h"
39 #include "gdb_curses.h"
41 /* Function to display source in the source window. */
43 tui_set_source_content (struct symtab *s,
47 enum tui_status ret = TUI_FAILURE;
49 if (s != (struct symtab *) NULL && s->filename != (char *) NULL)
52 int i, desc, c, line_width, nlines;
55 if ((ret = tui_alloc_source_buffer (TUI_SRC_WIN)) == TUI_SUCCESS)
57 line_width = TUI_SRC_WIN->generic.width - 1;
58 /* Take hilite (window border) into account, when
59 calculating the number of lines. */
60 nlines = (line_no + (TUI_SRC_WIN->generic.height - 2)) - line_no;
61 desc = open_source_file (s);
66 char *name = alloca (strlen (s->filename) + 100);
68 sprintf (name, "%s:%d", s->filename, line_no);
69 print_sys_errmsg (name, errno);
75 if (s->line_charpos == 0)
76 find_source_lines (s, desc);
78 if (line_no < 1 || line_no > s->nlines)
82 "Line number %d out of range; %s has %d lines.\n",
83 line_no, s->filename, s->nlines);
85 else if (lseek (desc, s->line_charpos[line_no - 1], 0) < 0)
88 perror_with_name (s->filename);
92 int offset, cur_line_no, cur_line, cur_len, threshold;
93 struct tui_gen_win_info *locator = tui_locator_win_info_ptr ();
94 struct tui_source_info *src = &TUI_SRC_WIN->detail.source_info;
96 if (TUI_SRC_WIN->generic.title)
97 xfree (TUI_SRC_WIN->generic.title);
98 TUI_SRC_WIN->generic.title = xstrdup (s->filename);
101 xfree (src->filename);
102 src->filename = xstrdup (s->filename);
104 /* Determine the threshold for the length of the
105 line and the offset to start the display. */
106 offset = src->horizontal_offset;
107 threshold = (line_width - 1) + offset;
108 stream = fdopen (desc, FOPEN_RT);
111 src->gdbarch = get_objfile_arch (s->objfile);
112 src->start_line_or_addr.loa = LOA_LINE;
113 cur_line_no = src->start_line_or_addr.u.line_no = line_no;
115 src_line = (char *) xmalloc (
116 (threshold + 1) * sizeof (char));
117 while (cur_line < nlines)
119 struct tui_win_element *element = (struct tui_win_element *)
120 TUI_SRC_WIN->generic.content[cur_line];
122 /* Get the first character in the line. */
126 src_line = ((struct tui_win_element *)
127 TUI_SRC_WIN->generic.content[
128 cur_line])->which_element.source.line;
129 /* Init the line with the line number. */
130 sprintf (src_line, "%-6d", cur_line_no);
131 cur_len = strlen (src_line);
133 ((cur_len / tui_default_tab_len ()) * tui_default_tab_len ());
134 while (i < tui_default_tab_len ())
136 src_line[cur_len] = ' ';
140 src_line[cur_len] = (char) 0;
142 /* Set whether element is the execution point
143 and whether there is a break point on it. */
144 element->which_element.source.line_or_addr.loa =
146 element->which_element.source.line_or_addr.u.line_no =
148 element->which_element.source.is_exec_point =
149 (strcmp (((struct tui_win_element *)
150 locator->content[0])->which_element.locator.file_name,
152 && cur_line_no == ((struct tui_win_element *)
153 locator->content[0])->which_element.locator.line_no);
156 i = strlen (src_line) - 1;
159 if ((c != '\n') && (c != '\r')
160 && (++i < threshold))
162 if (c < 040 && c != '\t')
165 src_line[i] = c + 0100;
173 { /* Store the charcter in the
174 line buffer. If it is a tab,
175 then translate to the correct
176 number of chars so we don't
177 overwrite our buffer. */
180 int j, max_tab_len = tui_default_tab_len ();
182 for (j = i - ((i / max_tab_len) * max_tab_len);
195 { /* If we have not reached EOL, then
196 eat chars until we do. */
197 while (c != EOF && c != '\n' && c != '\r')
199 /* Handle non-'\n' end-of-line. */
201 && (c = fgetc (stream)) != '\n'
210 while (c != EOF && c != '\n' && c != '\r'
212 && (c = fgetc (stream)));
214 /* Now copy the line taking the offset into
216 if (strlen (src_line) > offset)
217 strcpy (((struct tui_win_element *) TUI_SRC_WIN->generic.content[
218 cur_line])->which_element.source.line,
221 ((struct tui_win_element *)
222 TUI_SRC_WIN->generic.content[
223 cur_line])->which_element.source.line[0] = (char) 0;
230 TUI_SRC_WIN->generic.content_size = nlines;
240 /* elz: This function sets the contents of the source window to empty
241 except for a line in the middle with a warning message about the
242 source not being available. This function is called by
243 tui_erase_source_contents(), which in turn is invoked when the
244 source files cannot be accessed. */
247 tui_set_source_content_nil (struct tui_win_info *win_info,
248 char *warning_string)
254 line_width = win_info->generic.width - 1;
255 n_lines = win_info->generic.height - 2;
257 /* Set to empty each line in the window, except for the one which
258 contains the message. */
259 while (curr_line < win_info->generic.content_size)
261 /* Set the information related to each displayed line to null:
262 i.e. the line number is 0, there is no bp, it is not where
263 the program is stopped. */
265 struct tui_win_element *element =
266 (struct tui_win_element *) win_info->generic.content[curr_line];
268 element->which_element.source.line_or_addr.loa = LOA_LINE;
269 element->which_element.source.line_or_addr.u.line_no = 0;
270 element->which_element.source.is_exec_point = FALSE;
271 element->which_element.source.has_break = FALSE;
273 /* Set the contents of the line to blank. */
274 element->which_element.source.line[0] = (char) 0;
276 /* If the current line is in the middle of the screen, then we
277 want to display the 'no source available' message in it.
278 Note: the 'weird' arithmetic with the line width and height
279 comes from the function tui_erase_source_content(). We need
280 to keep the screen and the window's actual contents in
283 if (curr_line == (n_lines / 2 + 1))
287 int warning_length = strlen (warning_string);
290 src_line = element->which_element.source.line;
292 if (warning_length >= ((line_width - 1) / 2))
295 xpos = (line_width - 1) / 2 - warning_length;
297 for (i = 0; i < xpos; i++)
300 sprintf (src_line + i, "%s", warning_string);
302 for (i = xpos + warning_length; i < line_width; i++)
315 /* Function to display source in the source window. This function
316 initializes the horizontal scroll to 0. */
318 tui_show_symtab_source (struct gdbarch *gdbarch, struct symtab *s,
319 struct tui_line_or_address line,
322 TUI_SRC_WIN->detail.source_info.horizontal_offset = 0;
323 tui_update_source_window_as_is (TUI_SRC_WIN, gdbarch, s, line, noerror);
327 /* Answer whether the source is currently displayed in the source
330 tui_source_is_displayed (char *fname)
332 return (TUI_SRC_WIN->generic.content_in_use
333 && (strcmp (((struct tui_win_element *) (tui_locator_win_info_ptr ())->
334 content[0])->which_element.locator.file_name, fname) == 0));
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_win_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_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. */
363 l.u.line_no = content[0]->which_element.source.line_or_addr.u.line_no;
367 l.u.line_no = content[0]->which_element.source.line_or_addr.u.line_no
369 if (l.u.line_no <= 0)
373 print_source_lines (s, l.u.line_no, l.u.line_no + 1, 0);