1 /* TUI display source window.
3 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
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 2 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, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
29 #include "breakpoint.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"
47 /* Function to display source in the source window. */
49 tui_set_source_content (struct symtab
*s
, int lineNo
, int noerror
)
51 TuiStatus ret
= TUI_FAILURE
;
53 if (s
!= (struct symtab
*) NULL
&& s
->filename
!= (char *) NULL
)
55 register FILE *stream
;
56 register int i
, desc
, c
, lineWidth
, nlines
;
57 register char *srcLine
= 0;
59 if ((ret
= tui_alloc_source_buffer (srcWin
)) == TUI_SUCCESS
)
61 lineWidth
= srcWin
->generic
.width
- 1;
62 /* Take hilite (window border) into account, when calculating
63 the number of lines */
64 nlines
= (lineNo
+ (srcWin
->generic
.height
- 2)) - lineNo
;
65 desc
= open_source_file (s
);
70 char *name
= alloca (strlen (s
->filename
) + 100);
71 sprintf (name
, "%s:%d", s
->filename
, lineNo
);
72 print_sys_errmsg (name
, errno
);
78 if (s
->line_charpos
== 0)
79 find_source_lines (s
, desc
);
81 if (lineNo
< 1 || lineNo
> s
->nlines
)
85 "Line number %d out of range; %s has %d lines.\n",
86 lineNo
, s
->filename
, s
->nlines
);
88 else if (lseek (desc
, s
->line_charpos
[lineNo
- 1], 0) < 0)
91 perror_with_name (s
->filename
);
95 register int offset
, curLineNo
, curLine
, curLen
, threshold
;
96 TuiGenWinInfoPtr locator
= tui_locator_win_info_ptr ();
97 TuiSourceInfoPtr src
= &srcWin
->detail
.sourceInfo
;
99 if (srcWin
->generic
.title
)
100 xfree (srcWin
->generic
.title
);
101 srcWin
->generic
.title
= xstrdup (s
->filename
);
104 xfree (src
->filename
);
105 src
->filename
= xstrdup (s
->filename
);
107 /* Determine the threshold for the length of the line
108 and the offset to start the display. */
109 offset
= src
->horizontalOffset
;
110 threshold
= (lineWidth
- 1) + offset
;
111 stream
= fdopen (desc
, FOPEN_RT
);
114 curLineNo
= src
->startLineOrAddr
.lineNo
= lineNo
;
116 srcLine
= (char *) xmalloc (
117 (threshold
+ 1) * sizeof (char));
118 while (curLine
< nlines
)
120 TuiWinElementPtr element
= (TuiWinElementPtr
)
121 srcWin
->generic
.content
[curLine
];
123 /* get the first character in the line */
127 srcLine
= ((TuiWinElementPtr
)
128 srcWin
->generic
.content
[
129 curLine
])->whichElement
.source
.line
;
130 /* Init the line with the line number */
131 sprintf (srcLine
, "%-6d", curLineNo
);
132 curLen
= strlen (srcLine
);
134 ((curLen
/ tui_default_tab_len ()) * tui_default_tab_len ());
135 while (i
< tui_default_tab_len ())
137 srcLine
[curLen
] = ' ';
141 srcLine
[curLen
] = (char) 0;
143 /* Set whether element is the execution point and
144 whether there is a break point on it. */
145 element
->whichElement
.source
.lineOrAddr
.lineNo
=
147 element
->whichElement
.source
.isExecPoint
=
148 (strcmp (((TuiWinElementPtr
)
149 locator
->content
[0])->whichElement
.locator
.fileName
,
151 && curLineNo
== ((TuiWinElementPtr
)
152 locator
->content
[0])->whichElement
.locator
.lineNo
);
155 i
= strlen (srcLine
) - 1;
159 (c
!= '\r') && (++i
< threshold
))
161 if (c
< 040 && c
!= '\t')
164 srcLine
[i
] = c
+ 0100;
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
179 int j
, maxTabLen
= tui_default_tab_len ();
182 (i
/ maxTabLen
) * maxTabLen
);
195 { /* If we have not reached EOL, then eat
197 while (c
!= EOF
&& c
!= '\n' && c
!= '\r')
201 while (c
!= EOF
&& c
!= '\n' && c
!= '\r' &&
202 i
< threshold
&& (c
= fgetc (stream
)));
204 /* Now copy the line taking the offset into account */
205 if (strlen (srcLine
) > offset
)
206 strcpy (((TuiWinElementPtr
) srcWin
->generic
.content
[
207 curLine
])->whichElement
.source
.line
,
211 srcWin
->generic
.content
[
212 curLine
])->whichElement
.source
.line
[0] = (char) 0;
219 srcWin
->generic
.contentSize
= nlines
;
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 tuiEraseSourceContents, which in turn is invoked when the source
233 files cannot be accessed. */
236 tui_set_source_content_nil (TuiWinInfoPtr winInfo
, char *warning_string
)
242 lineWidth
= winInfo
->generic
.width
- 1;
243 nLines
= winInfo
->generic
.height
- 2;
245 /* set to empty each line in the window, except for the one
246 which contains the message */
247 while (curr_line
< winInfo
->generic
.contentSize
)
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 */
253 TuiWinElementPtr element
=
254 (TuiWinElementPtr
) winInfo
->generic
.content
[curr_line
];
255 element
->whichElement
.source
.lineOrAddr
.lineNo
= 0;
256 element
->whichElement
.source
.isExecPoint
= FALSE
;
257 element
->whichElement
.source
.hasBreak
= FALSE
;
259 /* set the contents of the line to blank */
260 element
->whichElement
.source
.line
[0] = (char) 0;
262 /* if the current line is in the middle of the screen, then we want to
263 display the 'no source available' message in it.
264 Note: the 'weird' arithmetic with the line width and height comes from
265 the function tuiEraseSourceContent. We need to keep the screen and the
266 window's actual contents in synch */
268 if (curr_line
== (nLines
/ 2 + 1))
272 int warning_length
= strlen (warning_string
);
275 srcLine
= element
->whichElement
.source
.line
;
277 if (warning_length
>= ((lineWidth
- 1) / 2))
280 xpos
= (lineWidth
- 1) / 2 - warning_length
;
282 for (i
= 0; i
< xpos
; i
++)
285 sprintf (srcLine
+ i
, "%s", warning_string
);
287 for (i
= xpos
+ warning_length
; i
< lineWidth
; i
++)
300 /* Function to display source in the source window. This function
301 initializes the horizontal scroll to 0. */
303 tui_show_symtab_source (struct symtab
*s
, TuiLineOrAddress line
, int noerror
)
305 srcWin
->detail
.sourceInfo
.horizontalOffset
= 0;
306 tui_update_source_window_as_is (srcWin
, s
, line
, noerror
);
310 /* Answer whether the source is currently displayed in the source
313 tui_source_is_displayed (char *fname
)
315 return (srcWin
->generic
.contentInUse
&&
316 (strcmp (((TuiWinElementPtr
) (tui_locator_win_info_ptr ())->
317 content
[0])->whichElement
.locator
.fileName
, fname
) == 0));
321 /* Scroll the source forward or backward vertically. */
323 tui_vertical_source_scroll (TuiScrollDirection scrollDirection
,
326 if (srcWin
->generic
.content
!= (OpaquePtr
) NULL
)
330 TuiWinContent content
= (TuiWinContent
) srcWin
->generic
.content
;
331 struct symtab_and_line cursal
= get_current_source_symtab_and_line ();
333 if (cursal
.symtab
== (struct symtab
*) NULL
)
334 s
= find_pc_symtab (get_frame_pc (deprecated_selected_frame
));
338 if (scrollDirection
== FORWARD_SCROLL
)
340 l
.lineNo
= content
[0]->whichElement
.source
.lineOrAddr
.lineNo
+
342 if (l
.lineNo
> s
->nlines
)
343 /*line = s->nlines - winInfo->generic.contentSize + 1; */
344 /*elz: fix for dts 23398 */
345 l
.lineNo
= content
[0]->whichElement
.source
.lineOrAddr
.lineNo
;
349 l
.lineNo
= content
[0]->whichElement
.source
.lineOrAddr
.lineNo
-
355 print_source_lines (s
, l
.lineNo
, l
.lineNo
+ 1, 0);