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 /*****************************************
58 ** STATIC LOCAL FUNCTIONS FORWARD DECLS **
59 ******************************************/
61 static struct breakpoint
*_hasBreak (char *, int);
65 ** tuiSetSourceContent().
66 ** Function to display source in the source window.
69 tuiSetSourceContent (struct symtab
*s
, int lineNo
, int noerror
)
71 TuiStatus ret
= TUI_FAILURE
;
73 if (s
!= (struct symtab
*) NULL
&& s
->filename
!= (char *) NULL
)
75 register FILE *stream
;
76 register int i
, desc
, c
, lineWidth
, nlines
;
77 register char *srcLine
= 0;
79 if ((ret
= tuiAllocSourceBuffer (srcWin
)) == TUI_SUCCESS
)
81 lineWidth
= srcWin
->generic
.width
- 1;
83 ** Take hilite (window border) into account, when calculating
84 ** the number of lines
86 nlines
= (lineNo
+ (srcWin
->generic
.height
- 2)) - lineNo
;
87 desc
= open_source_file (s
);
92 char *name
= alloca (strlen (s
->filename
) + 100);
93 sprintf (name
, "%s:%d", s
->filename
, lineNo
);
94 print_sys_errmsg (name
, errno
);
100 if (s
->line_charpos
== 0)
101 find_source_lines (s
, desc
);
103 if (lineNo
< 1 || lineNo
> s
->nlines
)
107 "Line number %d out of range; %s has %d lines.\n",
108 lineNo
, s
->filename
, s
->nlines
);
110 else if (lseek (desc
, s
->line_charpos
[lineNo
- 1], 0) < 0)
113 perror_with_name (s
->filename
);
117 register int offset
, curLineNo
, curLine
, curLen
, threshold
;
118 TuiGenWinInfoPtr locator
= locatorWinInfoPtr ();
119 TuiSourceInfoPtr src
= &srcWin
->detail
.sourceInfo
;
121 if (srcWin
->generic
.title
)
122 xfree (srcWin
->generic
.title
);
123 srcWin
->generic
.title
= xstrdup (s
->filename
);
126 xfree (src
->filename
);
127 src
->filename
= xstrdup (s
->filename
);
129 /* Determine the threshold for the length of the line
130 and the offset to start the display. */
131 offset
= src
->horizontalOffset
;
132 threshold
= (lineWidth
- 1) + offset
;
133 stream
= fdopen (desc
, FOPEN_RT
);
136 curLineNo
= src
->startLineOrAddr
.lineNo
= lineNo
;
138 srcLine
= (char *) xmalloc (
139 (threshold
+ 1) * sizeof (char));
140 while (curLine
< nlines
)
142 TuiWinElementPtr element
= (TuiWinElementPtr
)
143 srcWin
->generic
.content
[curLine
];
144 struct breakpoint
*bp
;
146 /* get the first character in the line */
150 srcLine
= ((TuiWinElementPtr
)
151 srcWin
->generic
.content
[
152 curLine
])->whichElement
.source
.line
;
153 /* Init the line with the line number */
154 sprintf (srcLine
, "%-6d", curLineNo
);
155 curLen
= strlen (srcLine
);
157 ((curLen
/ tuiDefaultTabLen ()) * tuiDefaultTabLen ());
158 while (i
< tuiDefaultTabLen ())
160 srcLine
[curLen
] = ' ';
164 srcLine
[curLen
] = (char) 0;
167 ** Set whether element is the execution point and
168 ** whether there is a break point on it.
170 element
->whichElement
.source
.lineOrAddr
.lineNo
=
172 element
->whichElement
.source
.isExecPoint
=
173 (strcmp (((TuiWinElementPtr
)
174 locator
->content
[0])->whichElement
.locator
.fileName
,
176 && curLineNo
== ((TuiWinElementPtr
)
177 locator
->content
[0])->whichElement
.locator
.lineNo
);
178 bp
= _hasBreak (s
->filename
, curLineNo
);
179 element
->whichElement
.source
.hasBreak
=
180 (bp
!= (struct breakpoint
*) NULL
&&
181 (!element
->whichElement
.source
.isExecPoint
||
182 (bp
->disposition
!= disp_del
|| bp
->hit_count
<= 0)));
185 i
= strlen (srcLine
) - 1;
189 (c
!= '\r') && (++i
< threshold
))
191 if (c
< 040 && c
!= '\t')
194 srcLine
[i
] = c
+ 0100;
203 ** Store the charcter in the line
204 ** buffer. If it is a tab, then
205 ** translate to the correct number of
206 ** chars so we don't overwrite our
211 int j
, maxTabLen
= tuiDefaultTabLen ();
214 (i
/ maxTabLen
) * maxTabLen
);
228 ** if we have not reached EOL, then eat
231 while (c
!= EOF
&& c
!= '\n' && c
!= '\r')
235 while (c
!= EOF
&& c
!= '\n' && c
!= '\r' &&
236 i
< threshold
&& (c
= fgetc (stream
)));
238 /* Now copy the line taking the offset into account */
239 if (strlen (srcLine
) > offset
)
240 strcpy (((TuiWinElementPtr
) srcWin
->generic
.content
[
241 curLine
])->whichElement
.source
.line
,
245 srcWin
->generic
.content
[
246 curLine
])->whichElement
.source
.line
[0] = (char) 0;
253 srcWin
->generic
.contentSize
= nlines
;
260 } /* tuiSetSourceContent */
263 /* elz: this function sets the contents of the source window to empty
264 except for a line in the middle with a warning message about the
265 source not being available. This function is called by
266 tuiEraseSourceContents, which in turn is invoked when the source files
267 cannot be accessed */
270 tuiSetSourceContentNil (TuiWinInfoPtr winInfo
, char *warning_string
)
276 lineWidth
= winInfo
->generic
.width
- 1;
277 nLines
= winInfo
->generic
.height
- 2;
279 /* set to empty each line in the window, except for the one
280 which contains the message */
281 while (curr_line
< winInfo
->generic
.contentSize
)
283 /* set the information related to each displayed line
284 to null: i.e. the line number is 0, there is no bp,
285 it is not where the program is stopped */
287 TuiWinElementPtr element
=
288 (TuiWinElementPtr
) winInfo
->generic
.content
[curr_line
];
289 element
->whichElement
.source
.lineOrAddr
.lineNo
= 0;
290 element
->whichElement
.source
.isExecPoint
= FALSE
;
291 element
->whichElement
.source
.hasBreak
= FALSE
;
293 /* set the contents of the line to blank */
294 element
->whichElement
.source
.line
[0] = (char) 0;
296 /* if the current line is in the middle of the screen, then we want to
297 display the 'no source available' message in it.
298 Note: the 'weird' arithmetic with the line width and height comes from
299 the function tuiEraseSourceContent. We need to keep the screen and the
300 window's actual contents in synch */
302 if (curr_line
== (nLines
/ 2 + 1))
306 int warning_length
= strlen (warning_string
);
309 srcLine
= element
->whichElement
.source
.line
;
311 if (warning_length
>= ((lineWidth
- 1) / 2))
314 xpos
= (lineWidth
- 1) / 2 - warning_length
;
316 for (i
= 0; i
< xpos
; i
++)
319 sprintf (srcLine
+ i
, "%s", warning_string
);
321 for (i
= xpos
+ warning_length
; i
< lineWidth
; i
++)
332 } /*tuiSetSourceContentNil */
339 ** Function to display source in the source window. This function
340 ** initializes the horizontal scroll to 0.
343 tuiShowSource (struct symtab
*s
, TuiLineOrAddress line
, int noerror
)
345 srcWin
->detail
.sourceInfo
.horizontalOffset
= 0;
346 tuiUpdateSourceWindowAsIs(srcWin
, s
, line
, noerror
);
349 } /* tuiShowSource */
353 ** tuiSourceIsDisplayed().
354 ** Answer whether the source is currently displayed in the source window.
357 tuiSourceIsDisplayed (char *fname
)
359 return (srcWin
->generic
.contentInUse
&&
360 (strcmp (((TuiWinElementPtr
) (locatorWinInfoPtr ())->
361 content
[0])->whichElement
.locator
.fileName
, fname
) == 0));
362 } /* tuiSourceIsDisplayed */
366 ** tuiVerticalSourceScroll().
367 ** Scroll the source forward or backward vertically
370 tuiVerticalSourceScroll (TuiScrollDirection scrollDirection
,
373 if (srcWin
->generic
.content
!= (OpaquePtr
) NULL
)
377 TuiWinContent content
= (TuiWinContent
) srcWin
->generic
.content
;
379 if (current_source_symtab
== (struct symtab
*) NULL
)
380 s
= find_pc_symtab (selected_frame
->pc
);
382 s
= current_source_symtab
;
384 if (scrollDirection
== FORWARD_SCROLL
)
386 l
.lineNo
= content
[0]->whichElement
.source
.lineOrAddr
.lineNo
+
388 if (l
.lineNo
> s
->nlines
)
389 /*line = s->nlines - winInfo->generic.contentSize + 1; */
390 /*elz: fix for dts 23398 */
391 l
.lineNo
= content
[0]->whichElement
.source
.lineOrAddr
.lineNo
;
395 l
.lineNo
= content
[0]->whichElement
.source
.lineOrAddr
.lineNo
-
400 if (identify_source_line (s
, l
.lineNo
, 0, -1) == 1)
401 tuiUpdateSourceWindowAsIs (srcWin
, s
, l
, FALSE
);
405 } /* tuiVerticalSourceScroll */
408 /*****************************************
409 ** STATIC LOCAL FUNCTIONS **
410 ******************************************/
414 ** Answer whether there is a break point at the input line in
415 ** the source file indicated
417 static struct breakpoint
*
418 _hasBreak (char *sourceFileName
, int lineNo
)
420 struct breakpoint
*bpWithBreak
= (struct breakpoint
*) NULL
;
421 struct breakpoint
*bp
;
422 extern struct breakpoint
*breakpoint_chain
;
425 for (bp
= breakpoint_chain
;
426 (bp
!= (struct breakpoint
*) NULL
&&
427 bpWithBreak
== (struct breakpoint
*) NULL
);
430 && (strcmp (sourceFileName
, bp
->source_file
) == 0)
431 && (lineNo
== bp
->line_number
))