]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/tui/tui-source.c
gdb/
[thirdparty/binutils-gdb.git] / gdb / tui / tui-source.c
1 /* TUI display source window.
2
3 Copyright (C) 1998-2013 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 <ctype.h>
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "source.h"
28 #include "symtab.h"
29 #include "objfiles.h"
30 #include "filenames.h"
31
32 #include "tui/tui.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
38 #include "gdb_string.h"
39 #include "gdb_curses.h"
40
41 /* Function to display source in the source window. */
42 enum tui_status
43 tui_set_source_content (struct symtab *s,
44 int line_no,
45 int noerror)
46 {
47 enum tui_status ret = TUI_FAILURE;
48
49 if (s != (struct symtab *) NULL)
50 {
51 FILE *stream;
52 int i, desc, c, line_width, nlines;
53 char *src_line = 0;
54
55 if ((ret = tui_alloc_source_buffer (TUI_SRC_WIN)) == TUI_SUCCESS)
56 {
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);
62 if (desc < 0)
63 {
64 if (!noerror)
65 {
66 char *name = alloca (strlen (s->filename) + 100);
67
68 sprintf (name, "%s:%d", s->filename, line_no);
69 print_sys_errmsg (name, errno);
70 }
71 ret = TUI_FAILURE;
72 }
73 else
74 {
75 if (s->line_charpos == 0)
76 find_source_lines (s, desc);
77
78 if (line_no < 1 || line_no > s->nlines)
79 {
80 close (desc);
81 printf_unfiltered (
82 "Line number %d out of range; %s has %d lines.\n",
83 line_no, s->filename, s->nlines);
84 }
85 else if (lseek (desc, s->line_charpos[line_no - 1], 0) < 0)
86 {
87 close (desc);
88 perror_with_name (s->filename);
89 }
90 else
91 {
92 int offset, cur_line_no, cur_line, cur_len, threshold;
93 struct tui_gen_win_info *locator
94 = tui_locator_win_info_ptr ();
95 struct tui_source_info *src
96 = &TUI_SRC_WIN->detail.source_info;
97
98 if (TUI_SRC_WIN->generic.title)
99 xfree (TUI_SRC_WIN->generic.title);
100 TUI_SRC_WIN->generic.title = xstrdup (s->filename);
101
102 xfree (src->fullname);
103 src->fullname = xstrdup (symtab_to_fullname (s));
104
105 /* Determine the threshold for the length of the
106 line and the offset to start the display. */
107 offset = src->horizontal_offset;
108 threshold = (line_width - 1) + offset;
109 stream = fdopen (desc, FOPEN_RT);
110 clearerr (stream);
111 cur_line = 0;
112 src->gdbarch = get_objfile_arch (s->objfile);
113 src->start_line_or_addr.loa = LOA_LINE;
114 cur_line_no = src->start_line_or_addr.u.line_no = line_no;
115 if (offset > 0)
116 src_line = (char *) xmalloc (
117 (threshold + 1) * sizeof (char));
118 while (cur_line < nlines)
119 {
120 struct tui_win_element *element
121 = (struct tui_win_element *)
122 TUI_SRC_WIN->generic.content[cur_line];
123
124 /* Get the first character in the line. */
125 c = fgetc (stream);
126
127 if (offset == 0)
128 src_line = ((struct tui_win_element *)
129 TUI_SRC_WIN->generic.content[
130 cur_line])->which_element.source.line;
131 /* Init the line with the line number. */
132 sprintf (src_line, "%-6d", cur_line_no);
133 cur_len = strlen (src_line);
134 i = cur_len - ((cur_len / tui_default_tab_len ())
135 * tui_default_tab_len ());
136 while (i < tui_default_tab_len ())
137 {
138 src_line[cur_len] = ' ';
139 i++;
140 cur_len++;
141 }
142 src_line[cur_len] = (char) 0;
143
144 /* Set whether element is the execution point
145 and whether there is a break point on it. */
146 element->which_element.source.line_or_addr.loa =
147 LOA_LINE;
148 element->which_element.source.line_or_addr.u.line_no =
149 cur_line_no;
150 element->which_element.source.is_exec_point =
151 (filename_cmp (((struct tui_win_element *)
152 locator->content[0])->which_element.locator.file_name,
153 s->filename) == 0
154 && cur_line_no == ((struct tui_win_element *)
155 locator->content[0])->which_element.locator.line_no);
156 if (c != EOF)
157 {
158 i = strlen (src_line) - 1;
159 do
160 {
161 if ((c != '\n') && (c != '\r')
162 && (++i < threshold))
163 {
164 if (c < 040 && c != '\t')
165 {
166 src_line[i++] = '^';
167 src_line[i] = c + 0100;
168 }
169 else if (c == 0177)
170 {
171 src_line[i++] = '^';
172 src_line[i] = '?';
173 }
174 else
175 { /* Store the charcter in the
176 line buffer. If it is a tab,
177 then translate to the correct
178 number of chars so we don't
179 overwrite our buffer. */
180 if (c == '\t')
181 {
182 int j, max_tab_len
183 = tui_default_tab_len ();
184
185 for (j = i - ((i / max_tab_len)
186 * max_tab_len);
187 j < max_tab_len
188 && i < threshold;
189 i++, j++)
190 src_line[i] = ' ';
191 i--;
192 }
193 else
194 src_line[i] = c;
195 }
196 src_line[i + 1] = 0;
197 }
198 else
199 { /* If we have not reached EOL, then
200 eat chars until we do. */
201 while (c != EOF && c != '\n' && c != '\r')
202 c = fgetc (stream);
203 /* Handle non-'\n' end-of-line. */
204 if (c == '\r'
205 && (c = fgetc (stream)) != '\n'
206 && c != EOF)
207 {
208 ungetc (c, stream);
209 c = '\r';
210 }
211
212 }
213 }
214 while (c != EOF && c != '\n' && c != '\r'
215 && i < threshold
216 && (c = fgetc (stream)));
217 }
218 /* Now copy the line taking the offset into
219 account. */
220 if (strlen (src_line) > offset)
221 strcpy (((struct tui_win_element *)
222 TUI_SRC_WIN->generic.content[cur_line])->which_element.source.line,
223 &src_line[offset]);
224 else
225 ((struct tui_win_element *)
226 TUI_SRC_WIN->generic.content[
227 cur_line])->which_element.source.line[0] = (char) 0;
228 cur_line++;
229 cur_line_no++;
230 }
231 if (offset > 0)
232 xfree (src_line);
233 fclose (stream);
234 TUI_SRC_WIN->generic.content_size = nlines;
235 ret = TUI_SUCCESS;
236 }
237 }
238 }
239 }
240 return ret;
241 }
242
243
244 /* elz: This function sets the contents of the source window to empty
245 except for a line in the middle with a warning message about the
246 source not being available. This function is called by
247 tui_erase_source_contents(), which in turn is invoked when the
248 source files cannot be accessed. */
249
250 void
251 tui_set_source_content_nil (struct tui_win_info *win_info,
252 char *warning_string)
253 {
254 int line_width;
255 int n_lines;
256 int curr_line = 0;
257
258 line_width = win_info->generic.width - 1;
259 n_lines = win_info->generic.height - 2;
260
261 /* Set to empty each line in the window, except for the one which
262 contains the message. */
263 while (curr_line < win_info->generic.content_size)
264 {
265 /* Set the information related to each displayed line to null:
266 i.e. the line number is 0, there is no bp, it is not where
267 the program is stopped. */
268
269 struct tui_win_element *element =
270 (struct tui_win_element *) win_info->generic.content[curr_line];
271
272 element->which_element.source.line_or_addr.loa = LOA_LINE;
273 element->which_element.source.line_or_addr.u.line_no = 0;
274 element->which_element.source.is_exec_point = FALSE;
275 element->which_element.source.has_break = FALSE;
276
277 /* Set the contents of the line to blank. */
278 element->which_element.source.line[0] = (char) 0;
279
280 /* If the current line is in the middle of the screen, then we
281 want to display the 'no source available' message in it.
282 Note: the 'weird' arithmetic with the line width and height
283 comes from the function tui_erase_source_content(). We need
284 to keep the screen and the window's actual contents in
285 synch. */
286
287 if (curr_line == (n_lines / 2 + 1))
288 {
289 int i;
290 int xpos;
291 int warning_length = strlen (warning_string);
292 char *src_line;
293
294 src_line = element->which_element.source.line;
295
296 if (warning_length >= ((line_width - 1) / 2))
297 xpos = 1;
298 else
299 xpos = (line_width - 1) / 2 - warning_length;
300
301 for (i = 0; i < xpos; i++)
302 src_line[i] = ' ';
303
304 sprintf (src_line + i, "%s", warning_string);
305
306 for (i = xpos + warning_length; i < line_width; i++)
307 src_line[i] = ' ';
308
309 src_line[i] = '\n';
310
311 } /* end if */
312
313 curr_line++;
314
315 } /* end while */
316 }
317
318
319 /* Function to display source in the source window. This function
320 initializes the horizontal scroll to 0. */
321 void
322 tui_show_symtab_source (struct gdbarch *gdbarch, struct symtab *s,
323 struct tui_line_or_address line,
324 int noerror)
325 {
326 TUI_SRC_WIN->detail.source_info.horizontal_offset = 0;
327 tui_update_source_window_as_is (TUI_SRC_WIN, gdbarch, s, line, noerror);
328 }
329
330
331 /* Answer whether the source is currently displayed in the source
332 window. */
333 int
334 tui_source_is_displayed (const char *fname)
335 {
336 return (TUI_SRC_WIN != NULL
337 && TUI_SRC_WIN->generic.content_in_use
338 && (filename_cmp (((struct tui_win_element *)
339 (tui_locator_win_info_ptr ())->
340 content[0])->which_element.locator.file_name,
341 fname) == 0));
342 }
343
344
345 /* Scroll the source forward or backward vertically. */
346 void
347 tui_vertical_source_scroll (enum tui_scroll_direction scroll_direction,
348 int num_to_scroll)
349 {
350 if (TUI_SRC_WIN->generic.content != NULL)
351 {
352 struct tui_line_or_address l;
353 struct symtab *s;
354 tui_win_content content = (tui_win_content) TUI_SRC_WIN->generic.content;
355 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
356
357 if (cursal.symtab == (struct symtab *) NULL)
358 s = find_pc_symtab (get_frame_pc (get_selected_frame (NULL)));
359 else
360 s = cursal.symtab;
361
362 l.loa = LOA_LINE;
363 if (scroll_direction == FORWARD_SCROLL)
364 {
365 l.u.line_no = content[0]->which_element.source.line_or_addr.u.line_no
366 + num_to_scroll;
367 if (l.u.line_no > s->nlines)
368 /* line = s->nlines - win_info->generic.content_size + 1; */
369 /* elz: fix for dts 23398. */
370 l.u.line_no
371 = content[0]->which_element.source.line_or_addr.u.line_no;
372 }
373 else
374 {
375 l.u.line_no = content[0]->which_element.source.line_or_addr.u.line_no
376 - num_to_scroll;
377 if (l.u.line_no <= 0)
378 l.u.line_no = 1;
379 }
380
381 print_source_lines (s, l.u.line_no, l.u.line_no + 1, 0);
382 }
383 }