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