1 /* TUI display source window.
2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Hewlett-Packard Company.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* If we need <curses.h>, we must include it before we get "bfd.h". */
36 #include "breakpoint.h"
43 #include "tuiSourceWin.h"
44 #include "tuiSource.h"
47 /*****************************************
48 ** EXTERNAL DATA DECLS **
49 ******************************************/
50 extern int current_source_line
;
51 extern struct symtab
*current_source_symtab
;
54 /*****************************************
55 ** STATIC LOCAL FUNCTIONS FORWARD DECLS **
56 ******************************************/
58 static struct breakpoint
*_hasBreak (char *, int);
61 /*****************************************
62 ** STATIC LOCAL DATA **
63 ******************************************/
66 /*****************************************
67 ** PUBLIC FUNCTIONS **
68 ******************************************/
70 /*********************************
71 ** SOURCE/DISASSEM FUNCTIONS **
72 *********************************/
75 ** tuiSetSourceContent().
76 ** Function to display source in the source window.
79 tuiSetSourceContent (struct symtab
*s
, int lineNo
, int noerror
)
81 TuiStatus ret
= TUI_FAILURE
;
83 if (s
!= (struct symtab
*) NULL
&& s
->filename
!= (char *) NULL
)
85 register FILE *stream
;
86 register int i
, desc
, c
, lineWidth
, nlines
;
87 register char *srcLine
;
89 if ((ret
= tuiAllocSourceBuffer (srcWin
)) == TUI_SUCCESS
)
91 lineWidth
= srcWin
->generic
.width
- 1;
93 ** Take hilite (window border) into account, when calculating
94 ** the number of lines
96 nlines
= (lineNo
+ (srcWin
->generic
.height
- 2)) - lineNo
;
97 desc
= open_source_file (s
);
102 char *name
= alloca (strlen (s
->filename
) + 100);
103 sprintf (name
, "%s:%d", s
->filename
, lineNo
);
104 print_sys_errmsg (name
, errno
);
110 if (s
->line_charpos
== 0)
111 find_source_lines (s
, desc
);
113 if (lineNo
< 1 || lineNo
> s
->nlines
)
117 "Line number %d out of range; %s has %d lines.\n",
118 lineNo
, s
->filename
, s
->nlines
);
120 else if (lseek (desc
, s
->line_charpos
[lineNo
- 1], 0) < 0)
123 perror_with_name (s
->filename
);
127 register int offset
, curLineNo
, curLine
, curLen
, threshold
;
128 TuiGenWinInfoPtr locator
= locatorWinInfoPtr ();
130 ** Determine the threshold for the length of the line
131 ** and the offset to start the display
133 offset
= srcWin
->detail
.sourceInfo
.horizontalOffset
;
134 threshold
= (lineWidth
- 1) + offset
;
135 stream
= fdopen (desc
, FOPEN_RT
);
139 srcWin
->detail
.sourceInfo
.startLineOrAddr
.lineNo
= lineNo
;
141 srcLine
= (char *) xmalloc (
142 (threshold
+ 1) * sizeof (char));
143 while (curLine
< nlines
)
145 TuiWinElementPtr element
= (TuiWinElementPtr
)
146 srcWin
->generic
.content
[curLine
];
147 struct breakpoint
*bp
;
149 /* get the first character in the line */
153 srcLine
= ((TuiWinElementPtr
)
154 srcWin
->generic
.content
[
155 curLine
])->whichElement
.source
.line
;
156 /* Init the line with the line number */
157 sprintf (srcLine
, "%-6d", curLineNo
);
158 curLen
= strlen (srcLine
);
160 ((curLen
/ tuiDefaultTabLen ()) * tuiDefaultTabLen ());
161 while (i
< tuiDefaultTabLen ())
163 srcLine
[curLen
] = ' ';
167 srcLine
[curLen
] = (char) 0;
170 ** Set whether element is the execution point and
171 ** whether there is a break point on it.
173 element
->whichElement
.source
.lineOrAddr
.lineNo
=
175 element
->whichElement
.source
.isExecPoint
=
176 (strcmp (((TuiWinElementPtr
)
177 locator
->content
[0])->whichElement
.locator
.fileName
,
179 && curLineNo
== ((TuiWinElementPtr
)
180 locator
->content
[0])->whichElement
.locator
.lineNo
);
181 bp
= _hasBreak (s
->filename
, curLineNo
);
182 element
->whichElement
.source
.hasBreak
=
183 (bp
!= (struct breakpoint
*) NULL
&&
184 (!element
->whichElement
.source
.isExecPoint
||
185 (bp
->disposition
!= disp_del
|| bp
->hit_count
<= 0)));
188 i
= strlen (srcLine
) - 1;
192 (c
!= '\r') && (++i
< threshold
))
194 if (c
< 040 && c
!= '\t')
197 srcLine
[i
] = c
+ 0100;
206 ** Store the charcter in the line
207 ** buffer. If it is a tab, then
208 ** translate to the correct number of
209 ** chars so we don't overwrite our
214 int j
, maxTabLen
= tuiDefaultTabLen ();
217 (i
/ maxTabLen
) * maxTabLen
);
231 ** if we have not reached EOL, then eat
234 while (c
!= EOF
&& c
!= '\n' && c
!= '\r')
238 while (c
!= EOF
&& c
!= '\n' && c
!= '\r' &&
239 i
< threshold
&& (c
= fgetc (stream
)));
241 /* Now copy the line taking the offset into account */
242 if (strlen (srcLine
) > offset
)
243 strcpy (((TuiWinElementPtr
) srcWin
->generic
.content
[
244 curLine
])->whichElement
.source
.line
,
248 srcWin
->generic
.content
[
249 curLine
])->whichElement
.source
.line
[0] = (char) 0;
256 srcWin
->generic
.contentSize
= nlines
;
263 } /* tuiSetSourceContent */
266 /* elz: this function sets the contents of the source window to empty
267 except for a line in the middle with a warning message about the
268 source not being available. This function is called by
269 tuiEraseSourceContents, which in turn is invoked when the source files
270 cannot be accessed */
273 tuiSetSourceContentNil (TuiWinInfoPtr winInfo
, char *warning_string
)
279 lineWidth
= winInfo
->generic
.width
- 1;
280 nLines
= winInfo
->generic
.height
- 2;
282 /* set to empty each line in the window, except for the one
283 which contains the message */
284 while (curr_line
< winInfo
->generic
.contentSize
)
286 /* set the information related to each displayed line
287 to null: i.e. the line number is 0, there is no bp,
288 it is not where the program is stopped */
290 TuiWinElementPtr element
=
291 (TuiWinElementPtr
) winInfo
->generic
.content
[curr_line
];
292 element
->whichElement
.source
.lineOrAddr
.lineNo
= 0;
293 element
->whichElement
.source
.isExecPoint
= FALSE
;
294 element
->whichElement
.source
.hasBreak
= FALSE
;
296 /* set the contents of the line to blank */
297 element
->whichElement
.source
.line
[0] = (char) 0;
299 /* if the current line is in the middle of the screen, then we want to
300 display the 'no source available' message in it.
301 Note: the 'weird' arithmetic with the line width and height comes from
302 the function tuiEraseSourceContent. We need to keep the screen and the
303 window's actual contents in synch */
305 if (curr_line
== (nLines
/ 2 + 1))
309 int warning_length
= strlen (warning_string
);
312 srcLine
= element
->whichElement
.source
.line
;
314 if (warning_length
>= ((lineWidth
- 1) / 2))
317 xpos
= (lineWidth
- 1) / 2 - warning_length
;
319 for (i
= 0; i
< xpos
; i
++)
322 sprintf (srcLine
+ i
, "%s", warning_string
);
324 for (i
= xpos
+ warning_length
; i
< lineWidth
; i
++)
335 } /*tuiSetSourceContentNil */
342 ** Function to display source in the source window. This function
343 ** initializes the horizontal scroll to 0.
346 tuiShowSource (struct symtab
*s
, TuiLineOrAddress line
, int noerror
)
348 srcWin
->detail
.sourceInfo
.horizontalOffset
= 0;
349 tuiUpdateSourceWindowAsIs(srcWin
, s
, line
, noerror
);
352 } /* tuiShowSource */
356 ** tuiSourceIsDisplayed().
357 ** Answer whether the source is currently displayed in the source window.
360 tuiSourceIsDisplayed (char *fname
)
362 return (srcWin
->generic
.contentInUse
&&
363 (strcmp (((TuiWinElementPtr
) (locatorWinInfoPtr ())->
364 content
[0])->whichElement
.locator
.fileName
, fname
) == 0));
365 } /* tuiSourceIsDisplayed */
369 ** tuiVerticalSourceScroll().
370 ** Scroll the source forward or backward vertically
373 tuiVerticalSourceScroll (TuiScrollDirection scrollDirection
,
376 if (srcWin
->generic
.content
!= (OpaquePtr
) NULL
)
380 TuiWinContent content
= (TuiWinContent
) srcWin
->generic
.content
;
382 if (current_source_symtab
== (struct symtab
*) NULL
)
383 s
= find_pc_symtab (selected_frame
->pc
);
385 s
= current_source_symtab
;
387 if (scrollDirection
== FORWARD_SCROLL
)
389 l
.lineNo
= content
[0]->whichElement
.source
.lineOrAddr
.lineNo
+
391 if (l
.lineNo
> s
->nlines
)
392 /*line = s->nlines - winInfo->generic.contentSize + 1; */
393 /*elz: fix for dts 23398 */
394 l
.lineNo
= content
[0]->whichElement
.source
.lineOrAddr
.lineNo
;
398 l
.lineNo
= content
[0]->whichElement
.source
.lineOrAddr
.lineNo
-
403 if (identify_source_line (s
, l
.lineNo
, 0, -1) == 1)
404 tuiUpdateSourceWindowAsIs (srcWin
, s
, l
, FALSE
);
408 } /* tuiVerticalSourceScroll */
411 /*****************************************
412 ** STATIC LOCAL FUNCTIONS **
413 ******************************************/
417 ** Answer whether there is a break point at the input line in
418 ** the source file indicated
420 static struct breakpoint
*
421 _hasBreak (char *sourceFileName
, int lineNo
)
423 struct breakpoint
*bpWithBreak
= (struct breakpoint
*) NULL
;
424 struct breakpoint
*bp
;
425 extern struct breakpoint
*breakpoint_chain
;
428 for (bp
= breakpoint_chain
;
429 (bp
!= (struct breakpoint
*) NULL
&&
430 bpWithBreak
== (struct breakpoint
*) NULL
);
433 && (strcmp (sourceFileName
, bp
->source_file
) == 0)
434 && (lineNo
== bp
->line_number
))