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