* tui/tui-hooks.c: Include "readline/readline.h" after
[deliverable/binutils-gdb.git] / gdb / tui / tui-source.c
CommitLineData
f377b406 1/* TUI display source window.
f33c6cbf 2
a358af15
AC
3 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
4 Foundation, Inc.
f33c6cbf 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
SS
24
25#include "defs.h"
26#include <ctype.h>
27#include "symtab.h"
28#include "frame.h"
29#include "breakpoint.h"
c2c6d25f 30#include "source.h"
a4b99e53 31#include "symtab.h"
c906108c 32
d7b2e967
AC
33#include "tui/tui.h"
34#include "tui/tui-data.h"
35#include "tui/tui-stack.h"
36#include "tui/tui-winsource.h"
37#include "tui/tui-source.h"
c906108c 38
88289b6e 39#include "gdb_string.h"
6a83354a 40#include "gdb_curses.h"
c906108c 41
98427f35 42/* Function to display source in the source window. */
a358af15 43enum tui_status
6d012f14 44tui_set_source_content (struct symtab *s, int line_no, int noerror)
c906108c 45{
22940a24 46 enum tui_status ret = TUI_FAILURE;
c906108c
SS
47
48 if (s != (struct symtab *) NULL && s->filename != (char *) NULL)
49 {
d02c80cd
AC
50 FILE *stream;
51 int i, desc, c, line_width, nlines;
52 char *src_line = 0;
c906108c 53
6d012f14 54 if ((ret = tui_alloc_source_buffer (TUI_SRC_WIN)) == TUI_SUCCESS)
c906108c 55 {
6ba8e26f 56 line_width = TUI_SRC_WIN->generic.width - 1;
98427f35
SC
57 /* Take hilite (window border) into account, when calculating
58 the number of lines */
6d012f14 59 nlines = (line_no + (TUI_SRC_WIN->generic.height - 2)) - line_no;
c906108c
SS
60 desc = open_source_file (s);
61 if (desc < 0)
62 {
63 if (!noerror)
64 {
65 char *name = alloca (strlen (s->filename) + 100);
6d012f14 66 sprintf (name, "%s:%d", s->filename, line_no);
c906108c
SS
67 print_sys_errmsg (name, errno);
68 }
69 ret = TUI_FAILURE;
70 }
71 else
72 {
73 if (s->line_charpos == 0)
74 find_source_lines (s, desc);
75
6d012f14 76 if (line_no < 1 || line_no > s->nlines)
c906108c
SS
77 {
78 close (desc);
79 printf_unfiltered (
80 "Line number %d out of range; %s has %d lines.\n",
6d012f14 81 line_no, s->filename, s->nlines);
c906108c 82 }
6d012f14 83 else if (lseek (desc, s->line_charpos[line_no - 1], 0) < 0)
c906108c
SS
84 {
85 close (desc);
86 perror_with_name (s->filename);
87 }
88 else
89 {
d02c80cd 90 int offset, cur_line_no, cur_line, cur_len, threshold;
2a8854a7 91 struct tui_gen_win_info * locator = tui_locator_win_info_ptr ();
6d012f14 92 struct tui_source_info * src = &TUI_SRC_WIN->detail.source_info;
bc6b7f04 93
6d012f14
AC
94 if (TUI_SRC_WIN->generic.title)
95 xfree (TUI_SRC_WIN->generic.title);
96 TUI_SRC_WIN->generic.title = xstrdup (s->filename);
bc6b7f04
SC
97
98 if (src->filename)
99 xfree (src->filename);
100 src->filename = xstrdup (s->filename);
101
102 /* Determine the threshold for the length of the line
103 and the offset to start the display. */
6d012f14 104 offset = src->horizontal_offset;
6ba8e26f 105 threshold = (line_width - 1) + offset;
c906108c
SS
106 stream = fdopen (desc, FOPEN_RT);
107 clearerr (stream);
6ba8e26f
AC
108 cur_line = 0;
109 cur_line_no = src->start_line_or_addr.line_no = line_no;
c906108c 110 if (offset > 0)
6ba8e26f 111 src_line = (char *) xmalloc (
c906108c 112 (threshold + 1) * sizeof (char));
6ba8e26f 113 while (cur_line < nlines)
c906108c 114 {
2a8854a7 115 struct tui_win_element * element = (struct tui_win_element *)
6ba8e26f 116 TUI_SRC_WIN->generic.content[cur_line];
c906108c
SS
117
118 /* get the first character in the line */
119 c = fgetc (stream);
120
121 if (offset == 0)
6ba8e26f 122 src_line = ((struct tui_win_element *)
6d012f14 123 TUI_SRC_WIN->generic.content[
6ba8e26f 124 cur_line])->which_element.source.line;
c906108c 125 /* Init the line with the line number */
6ba8e26f
AC
126 sprintf (src_line, "%-6d", cur_line_no);
127 cur_len = strlen (src_line);
128 i = cur_len -
129 ((cur_len / tui_default_tab_len ()) * tui_default_tab_len ());
dd1abb8c 130 while (i < tui_default_tab_len ())
c906108c 131 {
6ba8e26f 132 src_line[cur_len] = ' ';
c906108c 133 i++;
6ba8e26f 134 cur_len++;
c906108c 135 }
6ba8e26f 136 src_line[cur_len] = (char) 0;
c906108c 137
98427f35
SC
138 /* Set whether element is the execution point and
139 whether there is a break point on it. */
6d012f14 140 element->which_element.source.line_or_addr.line_no =
6ba8e26f 141 cur_line_no;
6d012f14 142 element->which_element.source.is_exec_point =
2a8854a7 143 (strcmp (((struct tui_win_element *)
6d012f14 144 locator->content[0])->which_element.locator.file_name,
c906108c 145 s->filename) == 0
6ba8e26f 146 && cur_line_no == ((struct tui_win_element *)
6d012f14 147 locator->content[0])->which_element.locator.line_no);
c906108c
SS
148 if (c != EOF)
149 {
6ba8e26f 150 i = strlen (src_line) - 1;
c906108c
SS
151 do
152 {
153 if ((c != '\n') &&
154 (c != '\r') && (++i < threshold))
155 {
156 if (c < 040 && c != '\t')
157 {
6ba8e26f
AC
158 src_line[i++] = '^';
159 src_line[i] = c + 0100;
c906108c
SS
160 }
161 else if (c == 0177)
162 {
6ba8e26f
AC
163 src_line[i++] = '^';
164 src_line[i] = '?';
c906108c
SS
165 }
166 else
98427f35
SC
167 { /* Store the charcter in the line
168 buffer. If it is a tab, then
169 translate to the correct number of
170 chars so we don't overwrite our
171 buffer. */
c906108c
SS
172 if (c == '\t')
173 {
6ba8e26f 174 int j, max_tab_len = tui_default_tab_len ();
c906108c
SS
175
176 for (j = i - (
6ba8e26f
AC
177 (i / max_tab_len) * max_tab_len);
178 ((j < max_tab_len) &&
c906108c
SS
179 i < threshold);
180 i++, j++)
6ba8e26f 181 src_line[i] = ' ';
c906108c
SS
182 i--;
183 }
184 else
6ba8e26f 185 src_line[i] = c;
c906108c 186 }
6ba8e26f 187 src_line[i + 1] = 0;
c906108c
SS
188 }
189 else
98427f35
SC
190 { /* If we have not reached EOL, then eat
191 chars until we do */
c906108c
SS
192 while (c != EOF && c != '\n' && c != '\r')
193 c = fgetc (stream);
194 }
195 }
196 while (c != EOF && c != '\n' && c != '\r' &&
197 i < threshold && (c = fgetc (stream)));
198 }
199 /* Now copy the line taking the offset into account */
6ba8e26f 200 if (strlen (src_line) > offset)
6d012f14 201 strcpy (((struct tui_win_element *) TUI_SRC_WIN->generic.content[
6ba8e26f
AC
202 cur_line])->which_element.source.line,
203 &src_line[offset]);
c906108c 204 else
2a8854a7 205 ((struct tui_win_element *)
6d012f14 206 TUI_SRC_WIN->generic.content[
6ba8e26f
AC
207 cur_line])->which_element.source.line[0] = (char) 0;
208 cur_line++;
209 cur_line_no++;
c906108c
SS
210 }
211 if (offset > 0)
6ba8e26f 212 xfree (src_line);
c906108c 213 fclose (stream);
6d012f14 214 TUI_SRC_WIN->generic.content_size = nlines;
c906108c
SS
215 ret = TUI_SUCCESS;
216 }
217 }
218 }
219 }
220 return ret;
98427f35 221}
c906108c
SS
222
223
224/* elz: this function sets the contents of the source window to empty
225 except for a line in the middle with a warning message about the
226 source not being available. This function is called by
6ba8e26f
AC
227 tui_erase_source_contents(), which in turn is invoked when the
228 source files cannot be accessed. */
c906108c
SS
229
230void
6ba8e26f 231tui_set_source_content_nil (struct tui_win_info * win_info, char *warning_string)
c906108c 232{
6ba8e26f
AC
233 int line_width;
234 int n_lines;
c906108c
SS
235 int curr_line = 0;
236
6ba8e26f
AC
237 line_width = win_info->generic.width - 1;
238 n_lines = win_info->generic.height - 2;
c906108c
SS
239
240 /* set to empty each line in the window, except for the one
c5aa993b 241 which contains the message */
6ba8e26f 242 while (curr_line < win_info->generic.content_size)
c906108c
SS
243 {
244 /* set the information related to each displayed line
c5aa993b
JM
245 to null: i.e. the line number is 0, there is no bp,
246 it is not where the program is stopped */
c906108c 247
2a8854a7 248 struct tui_win_element * element =
6ba8e26f 249 (struct tui_win_element *) win_info->generic.content[curr_line];
6d012f14
AC
250 element->which_element.source.line_or_addr.line_no = 0;
251 element->which_element.source.is_exec_point = FALSE;
252 element->which_element.source.has_break = FALSE;
c906108c 253
c5aa993b 254 /* set the contents of the line to blank */
6d012f14 255 element->which_element.source.line[0] = (char) 0;
c906108c 256
6ba8e26f
AC
257 /* if the current line is in the middle of the screen, then we
258 want to display the 'no source available' message in it.
259 Note: the 'weird' arithmetic with the line width and height
260 comes from the function tui_erase_source_content(). We need
261 to keep the screen and the window's actual contents in synch. */
c906108c 262
6ba8e26f 263 if (curr_line == (n_lines / 2 + 1))
c906108c
SS
264 {
265 int i;
266 int xpos;
267 int warning_length = strlen (warning_string);
6ba8e26f 268 char *src_line;
c906108c 269
6ba8e26f 270 src_line = element->which_element.source.line;
c906108c 271
6ba8e26f 272 if (warning_length >= ((line_width - 1) / 2))
c906108c
SS
273 xpos = 1;
274 else
6ba8e26f 275 xpos = (line_width - 1) / 2 - warning_length;
c906108c
SS
276
277 for (i = 0; i < xpos; i++)
6ba8e26f 278 src_line[i] = ' ';
c906108c 279
6ba8e26f 280 sprintf (src_line + i, "%s", warning_string);
c906108c 281
6ba8e26f
AC
282 for (i = xpos + warning_length; i < line_width; i++)
283 src_line[i] = ' ';
c906108c 284
6ba8e26f 285 src_line[i] = '\n';
c906108c
SS
286
287 } /* end if */
288
289 curr_line++;
290
c5aa993b 291 } /* end while */
98427f35 292}
c906108c
SS
293
294
98427f35
SC
295/* Function to display source in the source window. This function
296 initializes the horizontal scroll to 0. */
c906108c 297void
2a8854a7 298tui_show_symtab_source (struct symtab *s, union tui_line_or_address line, int noerror)
c906108c 299{
6d012f14
AC
300 TUI_SRC_WIN->detail.source_info.horizontal_offset = 0;
301 tui_update_source_window_as_is (TUI_SRC_WIN, s, line, noerror);
98427f35 302}
c906108c 303
c906108c 304
a358af15
AC
305/* Answer whether the source is currently displayed in the source
306 window. */
c906108c 307int
a358af15 308tui_source_is_displayed (char *fname)
c906108c 309{
6d012f14 310 return (TUI_SRC_WIN->generic.content_in_use &&
2a8854a7 311 (strcmp (((struct tui_win_element *) (tui_locator_win_info_ptr ())->
6d012f14 312 content[0])->which_element.locator.file_name, fname) == 0));
98427f35 313}
c906108c
SS
314
315
98427f35 316/* Scroll the source forward or backward vertically. */
c906108c 317void
6ba8e26f
AC
318tui_vertical_source_scroll (enum tui_scroll_direction scroll_direction,
319 int num_to_scroll)
c906108c 320{
6d012f14 321 if (TUI_SRC_WIN->generic.content != NULL)
c906108c 322 {
2a8854a7 323 union tui_line_or_address l;
c906108c 324 struct symtab *s;
6d012f14 325 tui_win_content content = (tui_win_content) TUI_SRC_WIN->generic.content;
52575520 326 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
c906108c 327
52575520 328 if (cursal.symtab == (struct symtab *) NULL)
f70a7d61 329 s = find_pc_symtab (get_frame_pc (deprecated_selected_frame));
c906108c 330 else
52575520 331 s = cursal.symtab;
c906108c 332
6ba8e26f 333 if (scroll_direction == FORWARD_SCROLL)
c906108c 334 {
6d012f14 335 l.line_no = content[0]->which_element.source.line_or_addr.line_no +
6ba8e26f 336 num_to_scroll;
6d012f14 337 if (l.line_no > s->nlines)
6ba8e26f 338 /*line = s->nlines - win_info->generic.content_size + 1; */
c5aa993b 339 /*elz: fix for dts 23398 */
6d012f14 340 l.line_no = content[0]->which_element.source.line_or_addr.line_no;
c906108c
SS
341 }
342 else
343 {
6d012f14 344 l.line_no = content[0]->which_element.source.line_or_addr.line_no -
6ba8e26f 345 num_to_scroll;
6d012f14
AC
346 if (l.line_no <= 0)
347 l.line_no = 1;
c906108c 348 }
27229e99 349
6d012f14 350 print_source_lines (s, l.line_no, l.line_no + 1, 0);
c906108c 351 }
98427f35 352}
This page took 0.432561 seconds and 4 git commands to generate.