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