]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/tui/tuiSource.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / tui / tuiSource.c
1 /*
2 ** tuiSource.c
3 ** This module contains functions for displaying source in the source window
4 */
5
6 #include "defs.h"
7 #include <ctype.h>
8 #include "symtab.h"
9 #include "frame.h"
10 #include "breakpoint.h"
11
12 #include "tui.h"
13 #include "tuiData.h"
14 #include "tuiStack.h"
15 #include "tuiSourceWin.h"
16 #include "tuiSource.h"
17
18
19 /*****************************************
20 ** EXTERNAL FUNCTION DECLS **
21 ******************************************/
22
23 extern int open_source_file PARAMS ((struct symtab *));
24 extern void find_source_lines PARAMS ((struct symtab *, int));
25
26 /*****************************************
27 ** EXTERNAL DATA DECLS **
28 ******************************************/
29 extern int current_source_line;
30 extern struct symtab *current_source_symtab;
31
32
33 /*****************************************
34 ** STATIC LOCAL FUNCTIONS FORWARD DECLS **
35 ******************************************/
36
37 static struct breakpoint *_hasBreak PARAMS ((char *, int));
38
39
40 /*****************************************
41 ** STATIC LOCAL DATA **
42 ******************************************/
43
44
45 /*****************************************
46 ** PUBLIC FUNCTIONS **
47 ******************************************/
48
49 /*********************************
50 ** SOURCE/DISASSEM FUNCTIONS **
51 *********************************/
52
53 /*
54 ** tuiSetSourceContent().
55 ** Function to display source in the source window.
56 */
57 TuiStatus
58 #ifdef __STDC__
59 tuiSetSourceContent (
60 struct symtab *s,
61 int lineNo,
62 int noerror)
63 #else
64 tuiSetSourceContent (s, lineNo, noerror)
65 struct symtab *s;
66 int lineNo;
67 int noerror;
68 #endif
69 {
70 TuiStatus ret = TUI_FAILURE;
71
72 if (s != (struct symtab *) NULL && s->filename != (char *) NULL)
73 {
74 register FILE *stream;
75 register int i, desc, c, lineWidth, nlines;
76 register char *srcLine;
77
78 if ((ret = tuiAllocSourceBuffer (srcWin)) == TUI_SUCCESS)
79 {
80 lineWidth = srcWin->generic.width - 1;
81 /*
82 ** Take hilite (window border) into account, when calculating
83 ** the number of lines
84 */
85 nlines = (lineNo + (srcWin->generic.height - 2)) - lineNo;
86 desc = open_source_file (s);
87 if (desc < 0)
88 {
89 if (!noerror)
90 {
91 char *name = alloca (strlen (s->filename) + 100);
92 sprintf (name, "%s:%d", s->filename, lineNo);
93 print_sys_errmsg (name, errno);
94 }
95 ret = TUI_FAILURE;
96 }
97 else
98 {
99 if (s->line_charpos == 0)
100 find_source_lines (s, desc);
101
102 if (lineNo < 1 || lineNo > s->nlines)
103 {
104 close (desc);
105 printf_unfiltered (
106 "Line number %d out of range; %s has %d lines.\n",
107 lineNo, s->filename, s->nlines);
108 }
109 else if (lseek (desc, s->line_charpos[lineNo - 1], 0) < 0)
110 {
111 close (desc);
112 perror_with_name (s->filename);
113 }
114 else
115 {
116 register int offset, curLineNo, curLine, curLen, threshold;
117 TuiGenWinInfoPtr locator = locatorWinInfoPtr ();
118 /*
119 ** Determine the threshold for the length of the line
120 ** and the offset to start the display
121 */
122 offset = srcWin->detail.sourceInfo.horizontalOffset;
123 threshold = (lineWidth - 1) + offset;
124 stream = fdopen (desc, FOPEN_RT);
125 clearerr (stream);
126 curLine = 0;
127 curLineNo =
128 srcWin->detail.sourceInfo.startLineOrAddr.lineNo = lineNo;
129 if (offset > 0)
130 srcLine = (char *) xmalloc (
131 (threshold + 1) * sizeof (char));
132 while (curLine < nlines)
133 {
134 TuiWinElementPtr element = (TuiWinElementPtr)
135 srcWin->generic.content[curLine];
136 struct breakpoint *bp;
137
138 /* get the first character in the line */
139 c = fgetc (stream);
140
141 if (offset == 0)
142 srcLine = ((TuiWinElementPtr)
143 srcWin->generic.content[
144 curLine])->whichElement.source.line;
145 /* Init the line with the line number */
146 sprintf (srcLine, "%-6d", curLineNo);
147 curLen = strlen (srcLine);
148 i = curLen -
149 ((curLen / tuiDefaultTabLen ()) * tuiDefaultTabLen ());
150 while (i < tuiDefaultTabLen ())
151 {
152 srcLine[curLen] = ' ';
153 i++;
154 curLen++;
155 }
156 srcLine[curLen] = (char) 0;
157
158 /*
159 ** Set whether element is the execution point and
160 ** whether there is a break point on it.
161 */
162 element->whichElement.source.lineOrAddr.lineNo =
163 curLineNo;
164 element->whichElement.source.isExecPoint =
165 (strcmp (((TuiWinElementPtr)
166 locator->content[0])->whichElement.locator.fileName,
167 s->filename) == 0
168 && curLineNo == ((TuiWinElementPtr)
169 locator->content[0])->whichElement.locator.lineNo);
170 bp = _hasBreak (s->filename, curLineNo);
171 element->whichElement.source.hasBreak =
172 (bp != (struct breakpoint *) NULL &&
173 (!element->whichElement.source.isExecPoint ||
174 (bp->disposition != del || bp->hit_count <= 0)));
175 if (c != EOF)
176 {
177 i = strlen (srcLine) - 1;
178 do
179 {
180 if ((c != '\n') &&
181 (c != '\r') && (++i < threshold))
182 {
183 if (c < 040 && c != '\t')
184 {
185 srcLine[i++] = '^';
186 srcLine[i] = c + 0100;
187 }
188 else if (c == 0177)
189 {
190 srcLine[i++] = '^';
191 srcLine[i] = '?';
192 }
193 else
194 { /*
195 ** Store the charcter in the line
196 ** buffer. If it is a tab, then
197 ** translate to the correct number of
198 ** chars so we don't overwrite our
199 ** buffer.
200 */
201 if (c == '\t')
202 {
203 int j, maxTabLen = tuiDefaultTabLen ();
204
205 for (j = i - (
206 (i / maxTabLen) * maxTabLen);
207 ((j < maxTabLen) &&
208 i < threshold);
209 i++, j++)
210 srcLine[i] = ' ';
211 i--;
212 }
213 else
214 srcLine[i] = c;
215 }
216 srcLine[i + 1] = 0;
217 }
218 else
219 { /*
220 ** if we have not reached EOL, then eat
221 ** chars until we do
222 */
223 while (c != EOF && c != '\n' && c != '\r')
224 c = fgetc (stream);
225 }
226 }
227 while (c != EOF && c != '\n' && c != '\r' &&
228 i < threshold && (c = fgetc (stream)));
229 }
230 /* Now copy the line taking the offset into account */
231 if (strlen (srcLine) > offset)
232 strcpy (((TuiWinElementPtr) srcWin->generic.content[
233 curLine])->whichElement.source.line,
234 &srcLine[offset]);
235 else
236 ((TuiWinElementPtr)
237 srcWin->generic.content[
238 curLine])->whichElement.source.line[0] = (char) 0;
239 curLine++;
240 curLineNo++;
241 }
242 if (offset > 0)
243 tuiFree (srcLine);
244 fclose (stream);
245 srcWin->generic.contentSize = nlines;
246 ret = TUI_SUCCESS;
247 }
248 }
249 }
250 }
251 return ret;
252 } /* tuiSetSourceContent */
253
254
255 /* elz: this function sets the contents of the source window to empty
256 except for a line in the middle with a warning message about the
257 source not being available. This function is called by
258 tuiEraseSourceContents, which in turn is invoked when the source files
259 cannot be accessed */
260
261 void
262 #ifdef __STDC__
263 tuiSetSourceContentNil (
264 TuiWinInfoPtr winInfo,
265 char *warning_string)
266 #else
267 tuiSetSourceContentNil (winInfo, warning_string)
268 TuiWinInfoPtr winInfo;
269 char *warning_string;
270 #endif
271 {
272 int lineWidth;
273 int nLines;
274 int curr_line = 0;
275
276 lineWidth = winInfo->generic.width - 1;
277 nLines = winInfo->generic.height - 2;
278
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)
282 {
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 */
286
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;
292
293 /* set the contents of the line to blank */
294 element->whichElement.source.line[0] = (char) 0;
295
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 */
301
302 if (curr_line == (nLines / 2 + 1))
303 {
304 int i;
305 int xpos;
306 int warning_length = strlen (warning_string);
307 char *srcLine;
308
309 srcLine = element->whichElement.source.line;
310
311 if (warning_length >= ((lineWidth - 1) / 2))
312 xpos = 1;
313 else
314 xpos = (lineWidth - 1) / 2 - warning_length;
315
316 for (i = 0; i < xpos; i++)
317 srcLine[i] = ' ';
318
319 sprintf (srcLine + i, "%s", warning_string);
320
321 for (i = xpos + warning_length; i < lineWidth; i++)
322 srcLine[i] = ' ';
323
324 srcLine[i] = '\n';
325
326 } /* end if */
327
328 curr_line++;
329
330 } /* end while */
331
332 } /*tuiSetSourceContentNil */
333
334
335
336
337 /*
338 ** tuiShowSource().
339 ** Function to display source in the source window. This function
340 ** initializes the horizontal scroll to 0.
341 */
342 void
343 #ifdef __STDC__
344 tuiShowSource (
345 struct symtab *s,
346 Opaque line,
347 int noerror)
348 #else
349 tuiShowSource (s, line, noerror)
350 struct symtab *s;
351 Opaque line;
352 int noerror;
353 #endif
354 {
355 srcWin->detail.sourceInfo.horizontalOffset = 0;
356 m_tuiShowSourceAsIs (s, line, noerror);
357
358 return;
359 } /* tuiShowSource */
360
361
362 /*
363 ** tuiSourceIsDisplayed().
364 ** Answer whether the source is currently displayed in the source window.
365 */
366 int
367 #ifdef __STDC__
368 tuiSourceIsDisplayed (
369 char *fname)
370 #else
371 tuiSourceIsDisplayed (fname)
372 char *fname;
373 #endif
374 {
375 return (srcWin->generic.contentInUse &&
376 (strcmp (((TuiWinElementPtr) (locatorWinInfoPtr ())->
377 content[0])->whichElement.locator.fileName, fname) == 0));
378 } /* tuiSourceIsDisplayed */
379
380
381 /*
382 ** tuiVerticalSourceScroll().
383 ** Scroll the source forward or backward vertically
384 */
385 void
386 #ifdef __STDC__
387 tuiVerticalSourceScroll (
388 TuiScrollDirection scrollDirection,
389 int numToScroll)
390 #else
391 tuiVerticalSourceScroll (scrollDirection, numToScroll)
392 TuiScrollDirection scrollDirection;
393 int numToScroll;
394 #endif
395 {
396 if (srcWin->generic.content != (OpaquePtr) NULL)
397 {
398 int line;
399 Opaque addr;
400 struct symtab *s;
401 TuiWinContent content = (TuiWinContent) srcWin->generic.content;
402
403 if (current_source_symtab == (struct symtab *) NULL)
404 s = find_pc_symtab (selected_frame->pc);
405 else
406 s = current_source_symtab;
407
408 if (scrollDirection == FORWARD_SCROLL)
409 {
410 line = content[0]->whichElement.source.lineOrAddr.lineNo +
411 numToScroll;
412 if (line > s->nlines)
413 /*line = s->nlines - winInfo->generic.contentSize + 1; */
414 /*elz: fix for dts 23398 */
415 line = content[0]->whichElement.source.lineOrAddr.lineNo;
416 }
417 else
418 {
419 line = content[0]->whichElement.source.lineOrAddr.lineNo -
420 numToScroll;
421 if (line <= 0)
422 line = 1;
423 }
424 tuiUpdateSourceWindowAsIs (srcWin, s, (Opaque) line, FALSE);
425 }
426
427 return;
428 } /* tuiVerticalSourceScroll */
429
430
431 /*****************************************
432 ** STATIC LOCAL FUNCTIONS **
433 ******************************************/
434
435 /*
436 ** _hasBreak().
437 ** Answer whether there is a break point at the input line in
438 ** the source file indicated
439 */
440 static struct breakpoint *
441 #ifdef __STDC__
442 _hasBreak (
443 char *sourceFileName,
444 int lineNo)
445 #else
446 _hasBreak (sourceFileName, lineNo)
447 char *sourceFileName;
448 int lineNo;
449 #endif
450 {
451 struct breakpoint *bpWithBreak = (struct breakpoint *) NULL;
452 struct breakpoint *bp;
453 extern struct breakpoint *breakpoint_chain;
454
455
456 for (bp = breakpoint_chain;
457 (bp != (struct breakpoint *) NULL &&
458 bpWithBreak == (struct breakpoint *) NULL);
459 bp = bp->next)
460 if ((strcmp (sourceFileName, bp->source_file) == 0) &&
461 (lineNo == bp->line_number))
462 bpWithBreak = bp;
463
464 return bpWithBreak;
465 } /* _hasBreak */