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