2002-09-29 Elena Zannoni <ezannoni@redhat.com>
[deliverable/binutils-gdb.git] / gdb / tui / tuiSource.c
1 /* TUI display source window.
2
3 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
4 Inc.
5
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. */
24
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
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
42 #include "defs.h"
43 #include <ctype.h>
44 #include "symtab.h"
45 #include "frame.h"
46 #include "breakpoint.h"
47 #include "source.h"
48 #include "symtab.h"
49
50 #include "tui.h"
51 #include "tuiData.h"
52 #include "tuiStack.h"
53 #include "tuiSourceWin.h"
54 #include "tuiSource.h"
55
56
57 /* Function to display source in the source window. */
58 TuiStatus
59 tuiSetSourceContent (struct symtab *s, int lineNo, int noerror)
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;
67 register char *srcLine = 0;
68
69 if ((ret = tuiAllocSourceBuffer (srcWin)) == TUI_SUCCESS)
70 {
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);
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 ();
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;
120 threshold = (lineWidth - 1) + offset;
121 stream = fdopen (desc, FOPEN_RT);
122 clearerr (stream);
123 curLine = 0;
124 curLineNo = src->startLineOrAddr.lineNo = lineNo;
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];
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
153 /* Set whether element is the execution point and
154 whether there is a break point on it. */
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);
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
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. */
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
205 { /* If we have not reached EOL, then eat
206 chars until we do */
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;
236 }
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
243 cannot be accessed */
244
245 void
246 tuiSetSourceContentNil (TuiWinInfoPtr winInfo, char *warning_string)
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
256 which contains the message */
257 while (curr_line < winInfo->generic.contentSize)
258 {
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 */
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
269 /* set the contents of the line to blank */
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
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 */
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
306 } /* end while */
307 }
308
309
310 /* Function to display source in the source window. This function
311 initializes the horizontal scroll to 0. */
312 void
313 tuiShowSource (struct symtab *s, TuiLineOrAddress line, int noerror)
314 {
315 srcWin->detail.sourceInfo.horizontalOffset = 0;
316 tuiUpdateSourceWindowAsIs(srcWin, s, line, noerror);
317 }
318
319
320 /* Answer whether the source is currently displayed in the source window. */
321 int
322 tuiSourceIsDisplayed (char *fname)
323 {
324 return (srcWin->generic.contentInUse &&
325 (strcmp (((TuiWinElementPtr) (locatorWinInfoPtr ())->
326 content[0])->whichElement.locator.fileName, fname) == 0));
327 }
328
329
330 /* Scroll the source forward or backward vertically. */
331 void
332 tuiVerticalSourceScroll (TuiScrollDirection scrollDirection,
333 int numToScroll)
334 {
335 if (srcWin->generic.content != (OpaquePtr) NULL)
336 {
337 TuiLineOrAddress l;
338 struct symtab *s;
339 TuiWinContent content = (TuiWinContent) srcWin->generic.content;
340 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
341
342 if (cursal.symtab == (struct symtab *) NULL)
343 s = find_pc_symtab (selected_frame->pc);
344 else
345 s = cursal.symtab;
346
347 if (scrollDirection == FORWARD_SCROLL)
348 {
349 l.lineNo = content[0]->whichElement.source.lineOrAddr.lineNo +
350 numToScroll;
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;
355 }
356 else
357 {
358 l.lineNo = content[0]->whichElement.source.lineOrAddr.lineNo -
359 numToScroll;
360 if (l.lineNo <= 0)
361 l.lineNo = 1;
362 }
363
364 print_source_lines (s, l.lineNo, l.lineNo + 1, 0);
365 }
366 }
This page took 0.041447 seconds and 4 git commands to generate.