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