1 /* TUI display source window.
3 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
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. */
25 /* FIXME: cagney/2002-02-28: The GDB coding standard indicates that
26 "defs.h" should be included first. Unfortunatly some systems
27 (currently Debian GNU/Linux) include the <stdbool.h> via <curses.h>
28 and they clash with "bfd.h"'s definiton of true/false. The correct
29 fix is to remove true/false from "bfd.h", however, until that
30 happens, hack around it by including "config.h" and <curses.h>
46 #include "breakpoint.h"
53 #include "tuiSourceWin.h"
54 #include "tuiSource.h"
57 /* Function to display source in the source window. */
59 tuiSetSourceContent (struct symtab
*s
, int lineNo
, int noerror
)
61 TuiStatus ret
= TUI_FAILURE
;
63 if (s
!= (struct symtab
*) NULL
&& s
->filename
!= (char *) NULL
)
65 register FILE *stream
;
66 register int i
, desc
, c
, lineWidth
, nlines
;
67 register char *srcLine
= 0;
69 if ((ret
= tuiAllocSourceBuffer (srcWin
)) == TUI_SUCCESS
)
71 lineWidth
= srcWin
->generic
.width
- 1;
72 /* Take hilite (window border) into account, when calculating
73 the number of lines */
74 nlines
= (lineNo
+ (srcWin
->generic
.height
- 2)) - lineNo
;
75 desc
= open_source_file (s
);
80 char *name
= alloca (strlen (s
->filename
) + 100);
81 sprintf (name
, "%s:%d", s
->filename
, lineNo
);
82 print_sys_errmsg (name
, errno
);
88 if (s
->line_charpos
== 0)
89 find_source_lines (s
, desc
);
91 if (lineNo
< 1 || lineNo
> s
->nlines
)
95 "Line number %d out of range; %s has %d lines.\n",
96 lineNo
, s
->filename
, s
->nlines
);
98 else if (lseek (desc
, s
->line_charpos
[lineNo
- 1], 0) < 0)
101 perror_with_name (s
->filename
);
105 register int offset
, curLineNo
, curLine
, curLen
, threshold
;
106 TuiGenWinInfoPtr locator
= locatorWinInfoPtr ();
107 TuiSourceInfoPtr src
= &srcWin
->detail
.sourceInfo
;
109 if (srcWin
->generic
.title
)
110 xfree (srcWin
->generic
.title
);
111 srcWin
->generic
.title
= xstrdup (s
->filename
);
114 xfree (src
->filename
);
115 src
->filename
= xstrdup (s
->filename
);
117 /* Determine the threshold for the length of the line
118 and the offset to start the display. */
119 offset
= src
->horizontalOffset
;
120 threshold
= (lineWidth
- 1) + offset
;
121 stream
= fdopen (desc
, FOPEN_RT
);
124 curLineNo
= src
->startLineOrAddr
.lineNo
= lineNo
;
126 srcLine
= (char *) xmalloc (
127 (threshold
+ 1) * sizeof (char));
128 while (curLine
< nlines
)
130 TuiWinElementPtr element
= (TuiWinElementPtr
)
131 srcWin
->generic
.content
[curLine
];
133 /* get the first character in the line */
137 srcLine
= ((TuiWinElementPtr
)
138 srcWin
->generic
.content
[
139 curLine
])->whichElement
.source
.line
;
140 /* Init the line with the line number */
141 sprintf (srcLine
, "%-6d", curLineNo
);
142 curLen
= strlen (srcLine
);
144 ((curLen
/ tuiDefaultTabLen ()) * tuiDefaultTabLen ());
145 while (i
< tuiDefaultTabLen ())
147 srcLine
[curLen
] = ' ';
151 srcLine
[curLen
] = (char) 0;
153 /* Set whether element is the execution point and
154 whether there is a break point on it. */
155 element
->whichElement
.source
.lineOrAddr
.lineNo
=
157 element
->whichElement
.source
.isExecPoint
=
158 (strcmp (((TuiWinElementPtr
)
159 locator
->content
[0])->whichElement
.locator
.fileName
,
161 && curLineNo
== ((TuiWinElementPtr
)
162 locator
->content
[0])->whichElement
.locator
.lineNo
);
165 i
= strlen (srcLine
) - 1;
169 (c
!= '\r') && (++i
< threshold
))
171 if (c
< 040 && c
!= '\t')
174 srcLine
[i
] = c
+ 0100;
182 { /* Store the charcter in the line
183 buffer. If it is a tab, then
184 translate to the correct number of
185 chars so we don't overwrite our
189 int j
, maxTabLen
= tuiDefaultTabLen ();
192 (i
/ maxTabLen
) * maxTabLen
);
205 { /* If we have not reached EOL, then eat
207 while (c
!= EOF
&& c
!= '\n' && c
!= '\r')
211 while (c
!= EOF
&& c
!= '\n' && c
!= '\r' &&
212 i
< threshold
&& (c
= fgetc (stream
)));
214 /* Now copy the line taking the offset into account */
215 if (strlen (srcLine
) > offset
)
216 strcpy (((TuiWinElementPtr
) srcWin
->generic
.content
[
217 curLine
])->whichElement
.source
.line
,
221 srcWin
->generic
.content
[
222 curLine
])->whichElement
.source
.line
[0] = (char) 0;
229 srcWin
->generic
.contentSize
= nlines
;
239 /* elz: this function sets the contents of the source window to empty
240 except for a line in the middle with a warning message about the
241 source not being available. This function is called by
242 tuiEraseSourceContents, which in turn is invoked when the source files
243 cannot be accessed */
246 tuiSetSourceContentNil (TuiWinInfoPtr winInfo
, char *warning_string
)
252 lineWidth
= winInfo
->generic
.width
- 1;
253 nLines
= winInfo
->generic
.height
- 2;
255 /* set to empty each line in the window, except for the one
256 which contains the message */
257 while (curr_line
< winInfo
->generic
.contentSize
)
259 /* set the information related to each displayed line
260 to null: i.e. the line number is 0, there is no bp,
261 it is not where the program is stopped */
263 TuiWinElementPtr element
=
264 (TuiWinElementPtr
) winInfo
->generic
.content
[curr_line
];
265 element
->whichElement
.source
.lineOrAddr
.lineNo
= 0;
266 element
->whichElement
.source
.isExecPoint
= FALSE
;
267 element
->whichElement
.source
.hasBreak
= FALSE
;
269 /* set the contents of the line to blank */
270 element
->whichElement
.source
.line
[0] = (char) 0;
272 /* if the current line is in the middle of the screen, then we want to
273 display the 'no source available' message in it.
274 Note: the 'weird' arithmetic with the line width and height comes from
275 the function tuiEraseSourceContent. We need to keep the screen and the
276 window's actual contents in synch */
278 if (curr_line
== (nLines
/ 2 + 1))
282 int warning_length
= strlen (warning_string
);
285 srcLine
= element
->whichElement
.source
.line
;
287 if (warning_length
>= ((lineWidth
- 1) / 2))
290 xpos
= (lineWidth
- 1) / 2 - warning_length
;
292 for (i
= 0; i
< xpos
; i
++)
295 sprintf (srcLine
+ i
, "%s", warning_string
);
297 for (i
= xpos
+ warning_length
; i
< lineWidth
; i
++)
310 /* Function to display source in the source window. This function
311 initializes the horizontal scroll to 0. */
313 tuiShowSource (struct symtab
*s
, TuiLineOrAddress line
, int noerror
)
315 srcWin
->detail
.sourceInfo
.horizontalOffset
= 0;
316 tuiUpdateSourceWindowAsIs(srcWin
, s
, line
, noerror
);
320 /* Answer whether the source is currently displayed in the source window. */
322 tuiSourceIsDisplayed (char *fname
)
324 return (srcWin
->generic
.contentInUse
&&
325 (strcmp (((TuiWinElementPtr
) (locatorWinInfoPtr ())->
326 content
[0])->whichElement
.locator
.fileName
, fname
) == 0));
330 /* Scroll the source forward or backward vertically. */
332 tuiVerticalSourceScroll (TuiScrollDirection scrollDirection
,
335 if (srcWin
->generic
.content
!= (OpaquePtr
) NULL
)
339 TuiWinContent content
= (TuiWinContent
) srcWin
->generic
.content
;
340 struct symtab_and_line cursal
= get_current_source_symtab_and_line ();
342 if (cursal
.symtab
== (struct symtab
*) NULL
)
343 s
= find_pc_symtab (deprecated_selected_frame
->pc
);
347 if (scrollDirection
== FORWARD_SCROLL
)
349 l
.lineNo
= content
[0]->whichElement
.source
.lineOrAddr
.lineNo
+
351 if (l
.lineNo
> s
->nlines
)
352 /*line = s->nlines - winInfo->generic.contentSize + 1; */
353 /*elz: fix for dts 23398 */
354 l
.lineNo
= content
[0]->whichElement
.source
.lineOrAddr
.lineNo
;
358 l
.lineNo
= content
[0]->whichElement
.source
.lineOrAddr
.lineNo
-
364 print_source_lines (s
, l
.lineNo
, l
.lineNo
+ 1, 0);