GDB (API)
/home/stan/gdb/src/gdb/tui/tui-source.c
Go to the documentation of this file.
00001 /* TUI display source window.
00002 
00003    Copyright (C) 1998-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Hewlett-Packard Company.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #include "defs.h"
00023 #include <ctype.h>
00024 #include "symtab.h"
00025 #include "frame.h"
00026 #include "breakpoint.h"
00027 #include "source.h"
00028 #include "symtab.h"
00029 #include "objfiles.h"
00030 #include "filenames.h"
00031 
00032 #include "tui/tui.h"
00033 #include "tui/tui-data.h"
00034 #include "tui/tui-stack.h"
00035 #include "tui/tui-winsource.h"
00036 #include "tui/tui-source.h"
00037 
00038 #include "gdb_string.h"
00039 #include "gdb_curses.h"
00040 
00041 /* Function to display source in the source window.  */
00042 enum tui_status
00043 tui_set_source_content (struct symtab *s, 
00044                         int line_no,
00045                         int noerror)
00046 {
00047   enum tui_status ret = TUI_FAILURE;
00048 
00049   if (s != (struct symtab *) NULL)
00050     {
00051       FILE *stream;
00052       int i, desc, c, line_width, nlines;
00053       char *src_line = 0;
00054 
00055       if ((ret = tui_alloc_source_buffer (TUI_SRC_WIN)) == TUI_SUCCESS)
00056         {
00057           line_width = TUI_SRC_WIN->generic.width - 1;
00058           /* Take hilite (window border) into account, when
00059              calculating the number of lines.  */
00060           nlines = (line_no + (TUI_SRC_WIN->generic.height - 2)) - line_no;
00061           desc = open_source_file (s);
00062           if (desc < 0)
00063             {
00064               if (!noerror)
00065                 {
00066                   const char *filename = symtab_to_filename_for_display (s);
00067                   char *name = alloca (strlen (filename) + 100);
00068 
00069                   sprintf (name, "%s:%d", filename, line_no);
00070                   print_sys_errmsg (name, errno);
00071                 }
00072               ret = TUI_FAILURE;
00073             }
00074           else
00075             {
00076               if (s->line_charpos == 0)
00077                 find_source_lines (s, desc);
00078 
00079               if (line_no < 1 || line_no > s->nlines)
00080                 {
00081                   close (desc);
00082                   printf_unfiltered ("Line number %d out of range; "
00083                                      "%s has %d lines.\n",
00084                                      line_no,
00085                                      symtab_to_filename_for_display (s),
00086                                      s->nlines);
00087                 }
00088               else if (lseek (desc, s->line_charpos[line_no - 1], 0) < 0)
00089                 {
00090                   close (desc);
00091                   perror_with_name (symtab_to_filename_for_display (s));
00092                 }
00093               else
00094                 {
00095                   int offset, cur_line_no, cur_line, cur_len, threshold;
00096                   struct tui_gen_win_info *locator
00097                     = tui_locator_win_info_ptr ();
00098                   struct tui_source_info *src
00099                     = &TUI_SRC_WIN->detail.source_info;
00100                   const char *s_filename = symtab_to_filename_for_display (s);
00101 
00102                   if (TUI_SRC_WIN->generic.title)
00103                     xfree (TUI_SRC_WIN->generic.title);
00104                   TUI_SRC_WIN->generic.title = xstrdup (s_filename);
00105 
00106                   xfree (src->fullname);
00107                   src->fullname = xstrdup (symtab_to_fullname (s));
00108 
00109                   /* Determine the threshold for the length of the
00110                      line and the offset to start the display.  */
00111                   offset = src->horizontal_offset;
00112                   threshold = (line_width - 1) + offset;
00113                   stream = fdopen (desc, FOPEN_RT);
00114                   clearerr (stream);
00115                   cur_line = 0;
00116                   src->gdbarch = get_objfile_arch (s->objfile);
00117                   src->start_line_or_addr.loa = LOA_LINE;
00118                   cur_line_no = src->start_line_or_addr.u.line_no = line_no;
00119                   if (offset > 0)
00120                     src_line = (char *) xmalloc (
00121                                            (threshold + 1) * sizeof (char));
00122                   while (cur_line < nlines)
00123                     {
00124                       struct tui_win_element *element
00125                         = (struct tui_win_element *)
00126                         TUI_SRC_WIN->generic.content[cur_line];
00127 
00128                       /* Get the first character in the line.  */
00129                       c = fgetc (stream);
00130 
00131                       if (offset == 0)
00132                         src_line = ((struct tui_win_element *)
00133                                    TUI_SRC_WIN->generic.content[
00134                                         cur_line])->which_element.source.line;
00135                       /* Init the line with the line number.  */
00136                       sprintf (src_line, "%-6d", cur_line_no);
00137                       cur_len = strlen (src_line);
00138                       i = cur_len - ((cur_len / tui_default_tab_len ())
00139                                      * tui_default_tab_len ());
00140                       while (i < tui_default_tab_len ())
00141                         {
00142                           src_line[cur_len] = ' ';
00143                           i++;
00144                           cur_len++;
00145                         }
00146                       src_line[cur_len] = (char) 0;
00147 
00148                       /* Set whether element is the execution point
00149                          and whether there is a break point on it.  */
00150                       element->which_element.source.line_or_addr.loa =
00151                         LOA_LINE;
00152                       element->which_element.source.line_or_addr.u.line_no =
00153                         cur_line_no;
00154                       element->which_element.source.is_exec_point =
00155                         (filename_cmp (((struct tui_win_element *)
00156                                        locator->content[0])->which_element.locator.full_name,
00157                                        symtab_to_fullname (s)) == 0
00158                          && cur_line_no == ((struct tui_win_element *)
00159                                             locator->content[0])->which_element.locator.line_no);
00160                       if (c != EOF)
00161                         {
00162                           i = strlen (src_line) - 1;
00163                           do
00164                             {
00165                               if ((c != '\n') && (c != '\r') 
00166                                   && (++i < threshold))
00167                                 {
00168                                   if (c < 040 && c != '\t')
00169                                     {
00170                                       src_line[i++] = '^';
00171                                       src_line[i] = c + 0100;
00172                                     }
00173                                   else if (c == 0177)
00174                                     {
00175                                       src_line[i++] = '^';
00176                                       src_line[i] = '?';
00177                                     }
00178                                   else
00179                                     { /* Store the charcter in the
00180                                          line buffer.  If it is a tab,
00181                                          then translate to the correct
00182                                          number of chars so we don't
00183                                          overwrite our buffer.  */
00184                                       if (c == '\t')
00185                                         {
00186                                           int j, max_tab_len
00187                                             = tui_default_tab_len ();
00188 
00189                                           for (j = i - ((i / max_tab_len)
00190                                                         * max_tab_len);
00191                                                j < max_tab_len
00192                                                  && i < threshold;
00193                                                i++, j++)
00194                                             src_line[i] = ' ';
00195                                           i--;
00196                                         }
00197                                       else
00198                                         src_line[i] = c;
00199                                     }
00200                                   src_line[i + 1] = 0;
00201                                 }
00202                               else
00203                                 { /* If we have not reached EOL, then
00204                                      eat chars until we do.  */
00205                                   while (c != EOF && c != '\n' && c != '\r')
00206                                     c = fgetc (stream);
00207                                   /* Handle non-'\n' end-of-line.  */
00208                                   if (c == '\r' 
00209                                       && (c = fgetc (stream)) != '\n' 
00210                                       && c != EOF)
00211                                     {
00212                                        ungetc (c, stream);
00213                                        c = '\r';
00214                                     }
00215                                   
00216                                 }
00217                             }
00218                           while (c != EOF && c != '\n' && c != '\r' 
00219                                  && i < threshold 
00220                                  && (c = fgetc (stream)));
00221                         }
00222                       /* Now copy the line taking the offset into
00223                          account.  */
00224                       if (strlen (src_line) > offset)
00225                         strcpy (((struct tui_win_element *)
00226                                  TUI_SRC_WIN->generic.content[cur_line])->which_element.source.line,
00227                                 &src_line[offset]);
00228                       else
00229                         ((struct tui_win_element *)
00230                          TUI_SRC_WIN->generic.content[
00231                           cur_line])->which_element.source.line[0] = (char) 0;
00232                       cur_line++;
00233                       cur_line_no++;
00234                     }
00235                   if (offset > 0)
00236                     xfree (src_line);
00237                   fclose (stream);
00238                   TUI_SRC_WIN->generic.content_size = nlines;
00239                   ret = TUI_SUCCESS;
00240                 }
00241             }
00242         }
00243     }
00244   return ret;
00245 }
00246 
00247 
00248 /* elz: This function sets the contents of the source window to empty
00249    except for a line in the middle with a warning message about the
00250    source not being available.  This function is called by
00251    tui_erase_source_contents(), which in turn is invoked when the
00252    source files cannot be accessed.  */
00253 
00254 void
00255 tui_set_source_content_nil (struct tui_win_info *win_info, 
00256                             char *warning_string)
00257 {
00258   int line_width;
00259   int n_lines;
00260   int curr_line = 0;
00261 
00262   line_width = win_info->generic.width - 1;
00263   n_lines = win_info->generic.height - 2;
00264 
00265   /* Set to empty each line in the window, except for the one which
00266      contains the message.  */
00267   while (curr_line < win_info->generic.content_size)
00268     {
00269       /* Set the information related to each displayed line to null:
00270          i.e. the line number is 0, there is no bp, it is not where
00271          the program is stopped.  */
00272 
00273       struct tui_win_element *element =
00274         (struct tui_win_element *) win_info->generic.content[curr_line];
00275 
00276       element->which_element.source.line_or_addr.loa = LOA_LINE;
00277       element->which_element.source.line_or_addr.u.line_no = 0;
00278       element->which_element.source.is_exec_point = FALSE;
00279       element->which_element.source.has_break = FALSE;
00280 
00281       /* Set the contents of the line to blank.  */
00282       element->which_element.source.line[0] = (char) 0;
00283 
00284       /* If the current line is in the middle of the screen, then we
00285          want to display the 'no source available' message in it.
00286          Note: the 'weird' arithmetic with the line width and height
00287          comes from the function tui_erase_source_content().  We need
00288          to keep the screen and the window's actual contents in
00289          synch.  */
00290 
00291       if (curr_line == (n_lines / 2 + 1))
00292         {
00293           int i;
00294           int xpos;
00295           int warning_length = strlen (warning_string);
00296           char *src_line;
00297 
00298           src_line = element->which_element.source.line;
00299 
00300           if (warning_length >= ((line_width - 1) / 2))
00301             xpos = 1;
00302           else
00303             xpos = (line_width - 1) / 2 - warning_length;
00304 
00305           for (i = 0; i < xpos; i++)
00306             src_line[i] = ' ';
00307 
00308           sprintf (src_line + i, "%s", warning_string);
00309 
00310           for (i = xpos + warning_length; i < line_width; i++)
00311             src_line[i] = ' ';
00312 
00313           src_line[i] = '\n';
00314 
00315         }                       /* end if */
00316 
00317       curr_line++;
00318 
00319     }                           /* end while */
00320 }
00321 
00322 
00323 /* Function to display source in the source window.  This function
00324    initializes the horizontal scroll to 0.  */
00325 void
00326 tui_show_symtab_source (struct gdbarch *gdbarch, struct symtab *s,
00327                         struct tui_line_or_address line, 
00328                         int noerror)
00329 {
00330   TUI_SRC_WIN->detail.source_info.horizontal_offset = 0;
00331   tui_update_source_window_as_is (TUI_SRC_WIN, gdbarch, s, line, noerror);
00332 }
00333 
00334 
00335 /* Answer whether the source is currently displayed in the source
00336    window.  */
00337 int
00338 tui_source_is_displayed (const char *fullname)
00339 {
00340   return (TUI_SRC_WIN != NULL
00341           && TUI_SRC_WIN->generic.content_in_use 
00342           && (filename_cmp (((struct tui_win_element *)
00343                              (tui_locator_win_info_ptr ())->
00344                              content[0])->which_element.locator.full_name,
00345                             fullname) == 0));
00346 }
00347 
00348 
00349 /* Scroll the source forward or backward vertically.  */
00350 void
00351 tui_vertical_source_scroll (enum tui_scroll_direction scroll_direction,
00352                             int num_to_scroll)
00353 {
00354   if (TUI_SRC_WIN->generic.content != NULL)
00355     {
00356       struct tui_line_or_address l;
00357       struct symtab *s;
00358       tui_win_content content = (tui_win_content) TUI_SRC_WIN->generic.content;
00359       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
00360 
00361       if (cursal.symtab == (struct symtab *) NULL)
00362         s = find_pc_symtab (get_frame_pc (get_selected_frame (NULL)));
00363       else
00364         s = cursal.symtab;
00365 
00366       l.loa = LOA_LINE;
00367       if (scroll_direction == FORWARD_SCROLL)
00368         {
00369           l.u.line_no = content[0]->which_element.source.line_or_addr.u.line_no
00370             + num_to_scroll;
00371           if (l.u.line_no > s->nlines)
00372             /* line = s->nlines - win_info->generic.content_size + 1; */
00373             /* elz: fix for dts 23398.  */
00374             l.u.line_no
00375               = content[0]->which_element.source.line_or_addr.u.line_no;
00376         }
00377       else
00378         {
00379           l.u.line_no = content[0]->which_element.source.line_or_addr.u.line_no
00380             - num_to_scroll;
00381           if (l.u.line_no <= 0)
00382             l.u.line_no = 1;
00383         }
00384 
00385       print_source_lines (s, l.u.line_no, l.u.line_no + 1, 0);
00386     }
00387 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines