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