Commit | Line | Data |
---|---|---|
f377b406 | 1 | /* TUI display source window. |
f33c6cbf AC |
2 | |
3 | Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation, | |
4 | Inc. | |
5 | ||
f377b406 SC |
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. */ | |
c906108c | 24 | |
f33c6cbf AC |
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> | |
31 | first. */ | |
32 | ||
4e8f7a8b DJ |
33 | #include "config.h" |
34 | #ifdef HAVE_NCURSES_H | |
35 | #include <ncurses.h> | |
36 | #else | |
37 | #ifdef HAVE_CURSES_H | |
38 | #include <curses.h> | |
39 | #endif | |
40 | #endif | |
41 | ||
c906108c SS |
42 | #include "defs.h" |
43 | #include <ctype.h> | |
44 | #include "symtab.h" | |
45 | #include "frame.h" | |
46 | #include "breakpoint.h" | |
c2c6d25f | 47 | #include "source.h" |
a4b99e53 | 48 | #include "symtab.h" |
c906108c SS |
49 | |
50 | #include "tui.h" | |
51 | #include "tuiData.h" | |
52 | #include "tuiStack.h" | |
53 | #include "tuiSourceWin.h" | |
54 | #include "tuiSource.h" | |
55 | ||
56 | ||
98427f35 | 57 | /* Function to display source in the source window. */ |
c906108c | 58 | TuiStatus |
eca6576c | 59 | tuiSetSourceContent (struct symtab *s, int lineNo, int noerror) |
c906108c SS |
60 | { |
61 | TuiStatus ret = TUI_FAILURE; | |
62 | ||
63 | if (s != (struct symtab *) NULL && s->filename != (char *) NULL) | |
64 | { | |
65 | register FILE *stream; | |
66 | register int i, desc, c, lineWidth, nlines; | |
bc6b7f04 | 67 | register char *srcLine = 0; |
c906108c SS |
68 | |
69 | if ((ret = tuiAllocSourceBuffer (srcWin)) == TUI_SUCCESS) | |
70 | { | |
71 | lineWidth = srcWin->generic.width - 1; | |
98427f35 SC |
72 | /* Take hilite (window border) into account, when calculating |
73 | the number of lines */ | |
c906108c SS |
74 | nlines = (lineNo + (srcWin->generic.height - 2)) - lineNo; |
75 | desc = open_source_file (s); | |
76 | if (desc < 0) | |
77 | { | |
78 | if (!noerror) | |
79 | { | |
80 | char *name = alloca (strlen (s->filename) + 100); | |
81 | sprintf (name, "%s:%d", s->filename, lineNo); | |
82 | print_sys_errmsg (name, errno); | |
83 | } | |
84 | ret = TUI_FAILURE; | |
85 | } | |
86 | else | |
87 | { | |
88 | if (s->line_charpos == 0) | |
89 | find_source_lines (s, desc); | |
90 | ||
91 | if (lineNo < 1 || lineNo > s->nlines) | |
92 | { | |
93 | close (desc); | |
94 | printf_unfiltered ( | |
95 | "Line number %d out of range; %s has %d lines.\n", | |
96 | lineNo, s->filename, s->nlines); | |
97 | } | |
98 | else if (lseek (desc, s->line_charpos[lineNo - 1], 0) < 0) | |
99 | { | |
100 | close (desc); | |
101 | perror_with_name (s->filename); | |
102 | } | |
103 | else | |
104 | { | |
105 | register int offset, curLineNo, curLine, curLen, threshold; | |
106 | TuiGenWinInfoPtr locator = locatorWinInfoPtr (); | |
bc6b7f04 SC |
107 | TuiSourceInfoPtr src = &srcWin->detail.sourceInfo; |
108 | ||
109 | if (srcWin->generic.title) | |
110 | xfree (srcWin->generic.title); | |
111 | srcWin->generic.title = xstrdup (s->filename); | |
112 | ||
113 | if (src->filename) | |
114 | xfree (src->filename); | |
115 | src->filename = xstrdup (s->filename); | |
116 | ||
117 | /* Determine the threshold for the length of the line | |
118 | and the offset to start the display. */ | |
119 | offset = src->horizontalOffset; | |
c906108c SS |
120 | threshold = (lineWidth - 1) + offset; |
121 | stream = fdopen (desc, FOPEN_RT); | |
122 | clearerr (stream); | |
123 | curLine = 0; | |
bc6b7f04 | 124 | curLineNo = src->startLineOrAddr.lineNo = lineNo; |
c906108c SS |
125 | if (offset > 0) |
126 | srcLine = (char *) xmalloc ( | |
127 | (threshold + 1) * sizeof (char)); | |
128 | while (curLine < nlines) | |
129 | { | |
130 | TuiWinElementPtr element = (TuiWinElementPtr) | |
131 | srcWin->generic.content[curLine]; | |
c906108c SS |
132 | |
133 | /* get the first character in the line */ | |
134 | c = fgetc (stream); | |
135 | ||
136 | if (offset == 0) | |
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); | |
143 | i = curLen - | |
144 | ((curLen / tuiDefaultTabLen ()) * tuiDefaultTabLen ()); | |
145 | while (i < tuiDefaultTabLen ()) | |
146 | { | |
147 | srcLine[curLen] = ' '; | |
148 | i++; | |
149 | curLen++; | |
150 | } | |
151 | srcLine[curLen] = (char) 0; | |
152 | ||
98427f35 SC |
153 | /* Set whether element is the execution point and |
154 | whether there is a break point on it. */ | |
c906108c SS |
155 | element->whichElement.source.lineOrAddr.lineNo = |
156 | curLineNo; | |
157 | element->whichElement.source.isExecPoint = | |
158 | (strcmp (((TuiWinElementPtr) | |
159 | locator->content[0])->whichElement.locator.fileName, | |
160 | s->filename) == 0 | |
161 | && curLineNo == ((TuiWinElementPtr) | |
162 | locator->content[0])->whichElement.locator.lineNo); | |
c906108c SS |
163 | if (c != EOF) |
164 | { | |
165 | i = strlen (srcLine) - 1; | |
166 | do | |
167 | { | |
168 | if ((c != '\n') && | |
169 | (c != '\r') && (++i < threshold)) | |
170 | { | |
171 | if (c < 040 && c != '\t') | |
172 | { | |
173 | srcLine[i++] = '^'; | |
174 | srcLine[i] = c + 0100; | |
175 | } | |
176 | else if (c == 0177) | |
177 | { | |
178 | srcLine[i++] = '^'; | |
179 | srcLine[i] = '?'; | |
180 | } | |
181 | else | |
98427f35 SC |
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 | |
186 | buffer. */ | |
c906108c SS |
187 | if (c == '\t') |
188 | { | |
189 | int j, maxTabLen = tuiDefaultTabLen (); | |
190 | ||
191 | for (j = i - ( | |
192 | (i / maxTabLen) * maxTabLen); | |
193 | ((j < maxTabLen) && | |
194 | i < threshold); | |
195 | i++, j++) | |
196 | srcLine[i] = ' '; | |
197 | i--; | |
198 | } | |
199 | else | |
200 | srcLine[i] = c; | |
201 | } | |
202 | srcLine[i + 1] = 0; | |
203 | } | |
204 | else | |
98427f35 SC |
205 | { /* If we have not reached EOL, then eat |
206 | chars until we do */ | |
c906108c SS |
207 | while (c != EOF && c != '\n' && c != '\r') |
208 | c = fgetc (stream); | |
209 | } | |
210 | } | |
211 | while (c != EOF && c != '\n' && c != '\r' && | |
212 | i < threshold && (c = fgetc (stream))); | |
213 | } | |
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, | |
218 | &srcLine[offset]); | |
219 | else | |
220 | ((TuiWinElementPtr) | |
221 | srcWin->generic.content[ | |
222 | curLine])->whichElement.source.line[0] = (char) 0; | |
223 | curLine++; | |
224 | curLineNo++; | |
225 | } | |
226 | if (offset > 0) | |
227 | tuiFree (srcLine); | |
228 | fclose (stream); | |
229 | srcWin->generic.contentSize = nlines; | |
230 | ret = TUI_SUCCESS; | |
231 | } | |
232 | } | |
233 | } | |
234 | } | |
235 | return ret; | |
98427f35 | 236 | } |
c906108c SS |
237 | |
238 | ||
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 | |
c5aa993b | 243 | cannot be accessed */ |
c906108c SS |
244 | |
245 | void | |
eca6576c | 246 | tuiSetSourceContentNil (TuiWinInfoPtr winInfo, char *warning_string) |
c906108c SS |
247 | { |
248 | int lineWidth; | |
249 | int nLines; | |
250 | int curr_line = 0; | |
251 | ||
252 | lineWidth = winInfo->generic.width - 1; | |
253 | nLines = winInfo->generic.height - 2; | |
254 | ||
255 | /* set to empty each line in the window, except for the one | |
c5aa993b | 256 | which contains the message */ |
c906108c SS |
257 | while (curr_line < winInfo->generic.contentSize) |
258 | { | |
259 | /* set the information related to each displayed line | |
c5aa993b JM |
260 | to null: i.e. the line number is 0, there is no bp, |
261 | it is not where the program is stopped */ | |
c906108c SS |
262 | |
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; | |
268 | ||
c5aa993b | 269 | /* set the contents of the line to blank */ |
c906108c SS |
270 | element->whichElement.source.line[0] = (char) 0; |
271 | ||
272 | /* if the current line is in the middle of the screen, then we want to | |
c5aa993b JM |
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 */ | |
c906108c SS |
277 | |
278 | if (curr_line == (nLines / 2 + 1)) | |
279 | { | |
280 | int i; | |
281 | int xpos; | |
282 | int warning_length = strlen (warning_string); | |
283 | char *srcLine; | |
284 | ||
285 | srcLine = element->whichElement.source.line; | |
286 | ||
287 | if (warning_length >= ((lineWidth - 1) / 2)) | |
288 | xpos = 1; | |
289 | else | |
290 | xpos = (lineWidth - 1) / 2 - warning_length; | |
291 | ||
292 | for (i = 0; i < xpos; i++) | |
293 | srcLine[i] = ' '; | |
294 | ||
295 | sprintf (srcLine + i, "%s", warning_string); | |
296 | ||
297 | for (i = xpos + warning_length; i < lineWidth; i++) | |
298 | srcLine[i] = ' '; | |
299 | ||
300 | srcLine[i] = '\n'; | |
301 | ||
302 | } /* end if */ | |
303 | ||
304 | curr_line++; | |
305 | ||
c5aa993b | 306 | } /* end while */ |
98427f35 | 307 | } |
c906108c SS |
308 | |
309 | ||
98427f35 SC |
310 | /* Function to display source in the source window. This function |
311 | initializes the horizontal scroll to 0. */ | |
c906108c | 312 | void |
a4b99e53 | 313 | tuiShowSource (struct symtab *s, TuiLineOrAddress line, int noerror) |
c906108c SS |
314 | { |
315 | srcWin->detail.sourceInfo.horizontalOffset = 0; | |
a4b99e53 | 316 | tuiUpdateSourceWindowAsIs(srcWin, s, line, noerror); |
98427f35 | 317 | } |
c906108c | 318 | |
c906108c | 319 | |
98427f35 | 320 | /* Answer whether the source is currently displayed in the source window. */ |
c906108c | 321 | int |
eca6576c | 322 | tuiSourceIsDisplayed (char *fname) |
c906108c SS |
323 | { |
324 | return (srcWin->generic.contentInUse && | |
325 | (strcmp (((TuiWinElementPtr) (locatorWinInfoPtr ())-> | |
326 | content[0])->whichElement.locator.fileName, fname) == 0)); | |
98427f35 | 327 | } |
c906108c SS |
328 | |
329 | ||
98427f35 | 330 | /* Scroll the source forward or backward vertically. */ |
c906108c | 331 | void |
a4b99e53 SC |
332 | tuiVerticalSourceScroll (TuiScrollDirection scrollDirection, |
333 | int numToScroll) | |
c906108c SS |
334 | { |
335 | if (srcWin->generic.content != (OpaquePtr) NULL) | |
336 | { | |
a4b99e53 | 337 | TuiLineOrAddress l; |
c906108c SS |
338 | struct symtab *s; |
339 | TuiWinContent content = (TuiWinContent) srcWin->generic.content; | |
52575520 | 340 | struct symtab_and_line cursal = get_current_source_symtab_and_line (); |
c906108c | 341 | |
52575520 | 342 | if (cursal.symtab == (struct symtab *) NULL) |
c906108c SS |
343 | s = find_pc_symtab (selected_frame->pc); |
344 | else | |
52575520 | 345 | s = cursal.symtab; |
c906108c SS |
346 | |
347 | if (scrollDirection == FORWARD_SCROLL) | |
348 | { | |
a4b99e53 | 349 | l.lineNo = content[0]->whichElement.source.lineOrAddr.lineNo + |
c906108c | 350 | numToScroll; |
a4b99e53 | 351 | if (l.lineNo > s->nlines) |
c5aa993b JM |
352 | /*line = s->nlines - winInfo->generic.contentSize + 1; */ |
353 | /*elz: fix for dts 23398 */ | |
a4b99e53 | 354 | l.lineNo = content[0]->whichElement.source.lineOrAddr.lineNo; |
c906108c SS |
355 | } |
356 | else | |
357 | { | |
a4b99e53 | 358 | l.lineNo = content[0]->whichElement.source.lineOrAddr.lineNo - |
c906108c | 359 | numToScroll; |
a4b99e53 SC |
360 | if (l.lineNo <= 0) |
361 | l.lineNo = 1; | |
c906108c | 362 | } |
27229e99 SC |
363 | |
364 | print_source_lines (s, l.lineNo, l.lineNo + 1, 0); | |
c906108c | 365 | } |
98427f35 | 366 | } |