home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Atari / Gnu / gdb36p4s.zoo / source.c < prev    next >
C/C++ Source or Header  |  1993-06-09  |  26KB  |  1,036 lines

  1. /* List lines of source files for GDB, the GNU debugger.
  2.    Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "symtab.h"
  24.  
  25. #ifdef USG
  26. #include <sys/types.h>
  27. #include <fcntl.h>
  28. #endif
  29.  
  30. #include <sys/param.h>
  31. #include <sys/stat.h>
  32. #include <sys/file.h>
  33.  
  34. /* Path of directories to search for source files.
  35.    Same format as the PATH environment variable's value.  */
  36.  
  37. static char *source_path;
  38.  
  39. /* Symtab of default file for listing lines of.  */
  40.  
  41. struct symtab *current_source_symtab;
  42.  
  43. /* Default next line to list.  */
  44.  
  45. int current_source_line;
  46.  
  47. /* Line number of last line printed.  Default for various commands.
  48.    current_source_line is usually, but not always, the same as this.  */
  49.  
  50. static int last_line_listed;
  51.  
  52. /* First line number listed by last listing command.  */
  53.  
  54. static int first_line_listed;
  55.  
  56.  
  57. struct symtab *psymtab_to_symtab ();
  58.  
  59. /* Set the source file default for the "list" command, specifying a
  60.    symtab.  Sigh.  Behaivior specification: If it is called with a
  61.    non-zero argument, that is the symtab to select.  If it is not,
  62.    first lookup "main"; if it exists, use the symtab and line it
  63.    defines.  If not, take the last symtab in the symtab_list (if it
  64.    exists) or the last symtab in the psytab_list (if *it* exists).  If
  65.    none of this works, report an error.   */
  66.  
  67. void
  68. select_source_symtab (s)
  69.      register struct symtab *s;
  70. {
  71.   struct symtabs_and_lines sals;
  72.   struct symtab_and_line sal;
  73.   struct partial_symtab *ps, *cs_pst;
  74.   
  75.   if (s)
  76.     {
  77.       current_source_symtab = s;
  78.       current_source_line = 1;
  79.       return;
  80.     }
  81.  
  82.   /* Make the default place to list be the function `main'
  83.      if one exists.  */
  84.   if (lookup_symbol ("main", 0, VAR_NAMESPACE, 0))
  85.     {
  86.       sals = decode_line_spec ("main", 1);
  87.       sal = sals.sals[0];
  88.       free (sals.sals);
  89.       current_source_symtab = sal.symtab;
  90.       current_source_line = max (sal.line - 9, 1);
  91.       return;
  92.     }
  93.   
  94.   /* All right; find the last file in the symtab list (ignoring .h's).  */
  95.  
  96.   if (s = symtab_list)
  97.     {
  98.       do
  99.     {
  100.       char *name = s->filename;
  101.       int len = strlen (name);
  102.       if (! (len > 2 && !strcmp (&name[len - 2], ".h")))
  103.         current_source_symtab = s;
  104.       s = s->next;
  105.     }
  106.       while (s);
  107.       current_source_line = 1;
  108.     }
  109.   else if (partial_symtab_list)
  110.     {
  111.       ps = partial_symtab_list;
  112.       while (ps)
  113.     {
  114.       char *name = ps->filename;
  115.       int len = strlen (name);
  116.       if (! (len > 2 && !strcmp (&name[len - 2], ".h")))
  117.         cs_pst = ps;
  118.       ps = ps->next;
  119.     }
  120.       if (cs_pst)
  121.     if (cs_pst->readin)
  122.       fatal ("Internal: select_source_symtab: readin pst found and no symtabs.");
  123.     else
  124.       current_source_symtab = psymtab_to_symtab (cs_pst);
  125.       else
  126.     current_source_symtab = 0;
  127.       current_source_line = 1;
  128.     }
  129. }
  130.  
  131. static void
  132. directories_info ()
  133. {
  134.   printf ("Source directories searched: %s\n", source_path);
  135. }
  136.  
  137. void
  138. init_source_path ()
  139. {
  140.   register struct symtab *s;
  141.  
  142.   source_path = savestring (current_directory, strlen (current_directory));
  143.  
  144.   /* Forget what we learned about line positions in source files;
  145.      must check again now since files may be found in
  146.      a different directory now.  */
  147.   for (s = symtab_list; s; s = s->next)
  148.     if (s->line_charpos != 0)
  149.       {
  150.     free (s->line_charpos);
  151.     s->line_charpos = 0;
  152.       }
  153. }
  154.  
  155. void
  156. directory_command (dirname, from_tty)
  157.      char *dirname;
  158.      int from_tty;
  159. {
  160.   char *old = source_path;
  161.  
  162.   dont_repeat ();
  163.  
  164.   if (dirname == 0)
  165.     {
  166.       if (query ("Reinitialize source path to %s? ", current_directory))
  167.     {
  168.       init_source_path ();
  169.       free (old);
  170.     }
  171.     }
  172.   else
  173.     {
  174.       dirname = tilde_expand (dirname);
  175.       make_cleanup (free, dirname);
  176.  
  177.       do
  178.     {
  179.       extern char *index ();
  180.       char *name = dirname;
  181.       register char *p;
  182.       struct stat st;
  183.  
  184.       {
  185.         char *colon = index (name, ':');
  186.         char *space = index (name, ' ');
  187.         char *tab = index (name, '\t');
  188.         if (colon == 0 && space == 0 && tab ==  0)
  189.           p = dirname = name + strlen (name);
  190.         else
  191.           {
  192.         p = 0;
  193.         if (colon != 0 && (p == 0 || colon < p))
  194.           p = colon;
  195.         if (space != 0 && (p == 0 || space < p))
  196.           p = space;
  197.         if (tab != 0 && (p == 0 || tab < p))
  198.           p = tab;
  199.         dirname = p + 1;
  200.         while (*dirname == ':' || *dirname == ' ' || *dirname == '\t')
  201.           ++dirname;
  202.           }
  203.       }
  204.  
  205.       if (p[-1] == '/')
  206.         /* Sigh. "foo/" => "foo" */
  207.         --p;
  208.       *p = '\0';
  209.  
  210.       while (p[-1] == '.')
  211.         {
  212.           if (p - name == 1)
  213.         {
  214.           /* "." => getwd ().  */
  215.           name = current_directory;
  216.           goto append;
  217.         }
  218.           else if (p[-2] == '/')
  219.         {
  220.           if (p - name == 2)
  221.             {
  222.               /* "/." => "/".  */
  223.               *--p = '\0';
  224.               goto append;
  225.             }
  226.           else
  227.             {
  228.               /* "...foo/." => "...foo".  */
  229.               p -= 2;
  230.               *p = '\0';
  231.               continue;
  232.             }
  233.         }
  234.           else
  235.         break;
  236.         }
  237.  
  238. #ifdef atarist
  239.       if (*name != '/' && name[1] != ':')
  240. #else
  241.       if (*name != '/')
  242. #endif
  243.         name = concat (current_directory, "/", name);
  244.       else
  245.         name = savestring (name, p - name);
  246.       make_cleanup (free, name);
  247.  
  248.       if (stat (name, &st) < 0)
  249.         perror_with_name (name);
  250.       if ((st.st_mode & S_IFMT) != S_IFDIR)
  251.         error ("%s is not a directory.", name);
  252.  
  253.     append:
  254.       {
  255.         register unsigned int len = strlen (name);
  256.  
  257.         p = source_path;
  258.         while (1)
  259.           {
  260.         if (!strncmp (p, name, len)
  261.             && (p[len] == '\0' || p[len] == ':'))
  262.           {
  263.             if (from_tty)
  264.               printf ("\"%s\" is already in the source path.\n", name);
  265.             break;
  266.           }
  267.         p = index (p, ':');
  268.         if (p != 0)
  269.           ++p;
  270.         else
  271.           break;
  272.           }
  273.         if (p == 0)
  274.           {
  275.         source_path = concat (old, ":", name);
  276.         free (old);
  277.         old = source_path;
  278.           }
  279.       }
  280.     } while (*dirname != '\0');
  281.       if (from_tty)
  282.     directories_info ();
  283.     }
  284. }
  285.  
  286. /* Open a file named STRING, searching path PATH (dir names sep by colons)
  287.    using mode MODE and protection bits PROT in the calls to open.
  288.    If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
  289.    (ie pretend the first element of PATH is ".")
  290.    If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
  291.    the actual file opened (this string will always start with a "/"
  292.  
  293.    If a file is found, return the descriptor.
  294.    Otherwise, return -1, with errno set for the last name we tried to open.  */
  295.  
  296. /*  >>>> This should only allow files of certain types,
  297.     >>>>  eg executable, non-directory */
  298. int
  299. openp (path, try_cwd_first, string, mode, prot, filename_opened)
  300.      char *path;
  301.      int try_cwd_first;
  302.      char *string;
  303.      int mode;
  304.      int prot;
  305.      char **filename_opened;
  306. {
  307.   register int fd;
  308.   register char *filename;
  309.   register char *p, *p1;
  310.   register int len;
  311.  
  312.   if (!path)
  313.     path = ".";
  314.  
  315.   /* ./foo => foo */
  316.   while (string[0] == '.' && string[1] == '/')
  317.     string += 2;
  318.  
  319. #ifdef atarist
  320.   if (try_cwd_first || string[0] == '/' || string[1] == ':')
  321. #else
  322.   if (try_cwd_first || string[0] == '/')
  323. #endif
  324.     {
  325.       filename = string;
  326.       fd = open (filename, mode, prot);
  327. #ifdef atarist
  328.       if (fd >= 0 || string[0] == '/' || string[1] == ':')
  329. #else
  330.       if (fd >= 0 || string[0] == '/')
  331. #endif
  332.     goto done;
  333.     }
  334.  
  335.   filename = (char *) alloca (strlen (path) + strlen (string) + 2);
  336.   fd = -1;
  337.   for (p = path; p; p = p1 ? p1 + 1 : 0)
  338.     {
  339.       extern char *index ();
  340.       p1 = index (p, ':');
  341.       if (p1)
  342.     len = p1 - p;
  343.       else
  344.     len = strlen (p);
  345.  
  346.       strncpy (filename, p, len);
  347.       filename[len] = 0;
  348.       strcat (filename, "/");
  349.       strcat (filename, string);
  350.  
  351.       fd = open (filename, mode, prot);
  352.       if (fd >= 0) break;
  353.     }
  354.  
  355.  done:
  356.   if (filename_opened)
  357.     if (fd < 0)
  358.       *filename_opened = (char *) 0;
  359. #ifdef atarist
  360.     else if (filename[0] == '/' || filename[1] == ':')
  361. #else
  362.     else if (filename[0] == '/')
  363. #endif
  364.       *filename_opened = savestring (filename, strlen (filename));
  365.     else
  366.       {
  367.     *filename_opened = concat (current_directory, "/", filename);
  368.       }
  369.  
  370.   return fd;
  371. }
  372.  
  373. /* Create and initialize the table S->line_charpos that records
  374.    the positions of the lines in the source file, which is assumed
  375.    to be open on descriptor DESC.
  376.    All set S->nlines to the number of such lines.  */
  377.  
  378. static void
  379. find_source_lines (s, desc)
  380.      struct symtab *s;
  381.      int desc;
  382. {
  383.   struct stat st;
  384.   register char *data, *p, *end;
  385.   int nlines = 0;
  386.   int lines_allocated = 1000;
  387.   int *line_charpos = (int *) xmalloc (lines_allocated * sizeof (int));
  388.   extern int exec_mtime;
  389.  
  390.   if (fstat (desc, &st) < 0)
  391.     perror_with_name (s->filename);
  392.   if (get_exec_file (0) != 0 && exec_mtime < st.st_mtime)
  393.     printf ("Source file is more recent than executable.\n");
  394.  
  395. #ifdef atarist
  396.   /* Avoid large alloca.  */
  397.   data = (char *) xmalloc (st.st_size);
  398.   if (myread (desc, data, st.st_size) < 0)
  399.     {
  400.       free (data);
  401.       perror_with_name (s->filename);
  402.     }
  403. #else
  404.   data = (char *) alloca (st.st_size);
  405.   if (myread (desc, data, st.st_size) < 0)
  406.     perror_with_name (s->filename);
  407. #endif
  408.   end = data + st.st_size;
  409.   p = data;
  410.   line_charpos[0] = 0;
  411.   nlines = 1;
  412.   while (p != end)
  413.     {
  414.       if (*p++ == '\n'
  415.       /* A newline at the end does not start a new line.  */
  416.       && p != end)
  417.     {
  418.       if (nlines == lines_allocated)
  419.         {
  420.           lines_allocated *= 2;
  421.           line_charpos = (int *) xrealloc (line_charpos,
  422.                            sizeof (int) * lines_allocated);
  423.         }
  424.       line_charpos[nlines++] = p - data;
  425.     }
  426.     }
  427. #ifdef atarist
  428.   free (data);
  429. #endif
  430.   s->nlines = nlines;
  431.   s->line_charpos = (int *) xrealloc (line_charpos, nlines * sizeof (int));
  432. }
  433.  
  434. /* Return the character position of a line LINE in symtab S.
  435.    Return 0 if anything is invalid.  */
  436.  
  437. int
  438. source_line_charpos (s, line)
  439.      struct symtab *s;
  440.      int line;
  441. {
  442.   if (!s) return 0;
  443.   if (!s->line_charpos || line <= 0) return 0;
  444.   if (line > s->nlines)
  445.     line = s->nlines;
  446.   return s->line_charpos[line - 1];
  447. }
  448.  
  449. /* Return the line number of character position POS in symtab S.  */
  450.  
  451. int
  452. source_charpos_line (s, chr)
  453.     register struct symtab *s;
  454.     register int chr;
  455. {
  456.   register int line = 0;
  457.   register int *lnp;
  458.     
  459.   if (s == 0 || s->line_charpos == 0) return 0;
  460.   lnp = s->line_charpos;
  461.   /* Files are usually short, so sequential search is Ok */
  462.   while (line < s->nlines  && *lnp <= chr)
  463.     {
  464.       line++;
  465.       lnp++;
  466.     }
  467.   if (line >= s->nlines)
  468.     line = s->nlines;
  469.   return line;
  470. }
  471.  
  472. /* Get full pathname and line number positions for a symtab.
  473.    Return nonzero if line numbers may have changed.
  474.    Set *FULLNAME to actual name of the file as found by `openp',
  475.    or to 0 if the file is not found.  */
  476.  
  477. int
  478. get_filename_and_charpos (s, line, fullname)
  479.      struct symtab *s;
  480.      int line;
  481.      char **fullname;
  482. {
  483.   register int desc, linenums_changed = 0;
  484.   
  485.   desc = openp (source_path, 0, s->filename, O_RDONLY, 0, &s->fullname);
  486.   if (desc < 0)
  487.     {
  488.       if (fullname)
  489.     *fullname = NULL;
  490.       return 0;
  491.     }  
  492.   if (fullname)
  493.     *fullname = s->fullname;
  494.   if (s->line_charpos == 0) linenums_changed = 1;
  495.   if (linenums_changed) find_source_lines (s, desc);
  496.   close (desc);
  497.   return linenums_changed;
  498. }
  499.  
  500. /* Print text describing the full name of the source file S
  501.    and the line number LINE and its corresponding character position.
  502.    The text starts with two Ctrl-z so that the Emacs-GDB interface
  503.    can easily find it.
  504.  
  505.    MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
  506.  
  507.    Return 1 if successful, 0 if could not find the file.  */
  508.  
  509. int
  510. identify_source_line (s, line, mid_statement)
  511.      struct symtab *s;
  512.      int line;
  513.      int mid_statement;
  514. {
  515.   if (s->line_charpos == 0)
  516.     get_filename_and_charpos (s, line, 0);
  517.   if (s->fullname == 0)
  518.     return 0;
  519.   printf ("\032\032%s:%d:%d:%s:0x%x\n", s->fullname,
  520.       line, s->line_charpos[line - 1],
  521.       mid_statement ? "middle" : "beg",
  522.       get_frame_pc (get_current_frame()));
  523.   current_source_line = line;
  524.   first_line_listed = line;
  525.   last_line_listed = line;
  526.   current_source_symtab = s;
  527.   return 1;
  528. }
  529.  
  530. /* Print source lines from the file of symtab S,
  531.    starting with line number LINE and stopping before line number STOPLINE.  */
  532.  
  533. void
  534. print_source_lines (s, line, stopline, noerror)
  535.      struct symtab *s;
  536.      int line, stopline;
  537.      int noerror;
  538. {
  539.   register int c;
  540.   register int desc;
  541.   register FILE *stream;
  542.   int nlines = stopline - line;
  543.  
  544.   desc = openp (source_path, 0, s->filename, O_RDONLY, 0, &s->fullname);
  545.   if (desc < 0)
  546.     {
  547.       extern int errno;
  548.       if (! noerror)
  549.     perror_with_name (s->filename);
  550.       print_sys_errmsg (s->filename, errno);
  551.       return;
  552.     }
  553.  
  554.   if (s->line_charpos == 0)
  555.     find_source_lines (s, desc);
  556.  
  557.   if (line < 1 || line > s->nlines)
  558.     {
  559.       close (desc);
  560.       error ("Line number %d out of range; %s has %d lines.",
  561.          line, s->filename, s->nlines);
  562.     }
  563.  
  564.   if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
  565.     {
  566.       close (desc);
  567.       perror_with_name (s->filename);
  568.     }
  569.  
  570.   current_source_symtab = s;
  571.   current_source_line = line;
  572.   first_line_listed = line;
  573.   
  574.   stream = fdopen (desc, "r");
  575.   clearerr (stream);
  576.  
  577.   while (nlines-- > 0)
  578.     {
  579.       c = fgetc (stream);
  580.       if (c == EOF) break;
  581.       last_line_listed = current_source_line;
  582.       printf_filtered ("%d\t", current_source_line++);
  583.       do
  584.     {
  585.       if (c < 040 && c != '\t' && c != '\n')
  586.           printf_filtered ("^%c", c + 0100);
  587.       else if (c == 0177)
  588.         printf_filtered ("^?");
  589.       else
  590.         printf_filtered ("%c", c);
  591.     } while (c != '\n' && (c = fgetc (stream)) >= 0);
  592.     }
  593.  
  594.   fclose (stream);
  595. }
  596.  
  597.  
  598.  
  599. /* 
  600.   C++
  601.   Print a list of files and line numbers which a user may choose from
  602.   in order to list a function which was specified ambiguously
  603.   (as with `list classname::overloadedfuncname', for example).
  604.   The vector in SALS provides the filenames and line numbers.
  605.   */
  606. static void
  607. ambiguous_line_spec (sals)
  608.      struct symtabs_and_lines *sals;
  609. {
  610.   int i;
  611.  
  612.   for (i = 0; i < sals->nelts; ++i)
  613.     printf("file: \"%s\", line number: %d\n",
  614.        sals->sals[i].symtab->filename, sals->sals[i].line);
  615. }
  616.  
  617.  
  618. static void
  619. list_command (arg, from_tty)
  620.      char *arg;
  621.      int from_tty;
  622. {
  623.   struct symtabs_and_lines sals, sals_end;
  624.   struct symtab_and_line sal, sal_end;
  625.   struct symbol *sym;
  626.   char *arg1;
  627.   int no_end = 1;
  628.   int dummy_end = 0;
  629.   int dummy_beg = 0;
  630.   int linenum_beg = 0;
  631.   char *p;
  632.  
  633.   if (symtab_list == 0 && partial_symtab_list == 0)
  634.     error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
  635.  
  636.   /* Pull in a current source symtab if necessary */
  637.   if (current_source_symtab == 0 &&
  638.       (arg == 0 || arg[0] == '+' || arg[0] == '-'))
  639.     select_source_symtab (0);
  640.  
  641.   /* "l" or "l +" lists next ten lines.  */
  642.  
  643.   if (arg == 0 || !strcmp (arg, "+"))
  644.     {
  645.       if (current_source_symtab == 0)
  646.     error ("No default source file yet.  Do \"help list\".");
  647.       print_source_lines (current_source_symtab, current_source_line,
  648.               current_source_line + 10, 0);
  649.       return;
  650.     }
  651.  
  652.   /* "l -" lists previous ten lines, the ones before the ten just listed.  */
  653.   if (!strcmp (arg, "-"))
  654.     {
  655.       if (current_source_symtab == 0)
  656.     error ("No default source file yet.  Do \"help list\".");
  657.       print_source_lines (current_source_symtab,
  658.               max (first_line_listed - 10, 1),
  659.               first_line_listed, 0);
  660.       return;
  661.     }
  662.  
  663.   /* Now if there is only one argument, decode it in SAL
  664.      and set NO_END.
  665.      If there are two arguments, decode them in SAL and SAL_END
  666.      and clear NO_END; however, if one of the arguments is blank,
  667.      set DUMMY_BEG or DUMMY_END to record that fact.  */
  668.  
  669.   arg1 = arg;
  670.   if (*arg1 == ',')
  671.     dummy_beg = 1;
  672.   else
  673.     {
  674.       sals = decode_line_1 (&arg1, 0, 0, 0);
  675.  
  676.       if (! sals.nelts) return;  /*  C++  */
  677.       if (sals.nelts > 1)
  678.     {
  679.       ambiguous_line_spec (&sals);
  680.       free (sals.sals);
  681.       return;
  682.     }
  683.  
  684.       sal = sals.sals[0];
  685.       free (sals.sals);
  686.     }
  687.  
  688.   /* Record whether the BEG arg is all digits.  */
  689.  
  690.   for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
  691.   linenum_beg = (p == arg1);
  692.  
  693.   while (*arg1 == ' ' || *arg1 == '\t')
  694.     arg1++;
  695.   if (*arg1 == ',')
  696.     {
  697.       no_end = 0;
  698.       arg1++;
  699.       while (*arg1 == ' ' || *arg1 == '\t')
  700.     arg1++;
  701.       if (*arg1 == 0)
  702.     dummy_end = 1;
  703.       else
  704.     {
  705.       if (dummy_beg)
  706.         sals_end = decode_line_1 (&arg1, 0, 0, 0);
  707.       else
  708.         sals_end = decode_line_1 (&arg1, 0, sal.symtab, sal.line);
  709.       if (sals_end.nelts == 0) 
  710.         return;
  711.       if (sals_end.nelts > 1)
  712.         {
  713.           ambiguous_line_spec (&sals_end);
  714.           free (sals_end.sals);
  715.           return;
  716.         }
  717.       sal_end = sals_end.sals[0];
  718.       free (sals_end.sals);
  719.     }
  720.     }
  721.  
  722.   if (*arg1)
  723.     error ("Junk at end of line specification.");
  724.  
  725.   if (!no_end && !dummy_beg && !dummy_end
  726.       && sal.symtab != sal_end.symtab)
  727.     error ("Specified start and end are in different files.");
  728.   if (dummy_beg && dummy_end)
  729.     error ("Two empty args do not say what lines to list.");
  730.  
  731.   /* if line was specified by address,
  732.      first print exactly which line, and which file.
  733.      In this case, sal.symtab == 0 means address is outside
  734.      of all known source files, not that user failed to give a filename.  */
  735.   if (*arg == '*')
  736.     {
  737.       if (sal.symtab == 0)
  738.     error ("No source file for address 0x%x.", sal.pc);
  739.       sym = find_pc_function (sal.pc);
  740.       if (sym)
  741.     {
  742.       printf ("0x%x is in ", sal.pc);
  743.       print_demangled (SYMBOL_NAME (sym), stdout);
  744.       printf (" (%s, line %d).\n", sal.symtab->filename, sal.line);
  745.     }
  746.       else
  747.     printf ("0x%x is in %s, line %d.\n",
  748.         sal.pc, sal.symtab->filename, sal.line);
  749.     }
  750.  
  751.   /* If line was not specified by just a line number,
  752.      and it does not imply a symtab, it must be an undebuggable symbol
  753.      which means no source code.  */
  754.  
  755.   if (! linenum_beg && sal.symtab == 0)
  756.     error ("No line number known for %s.", arg);
  757.  
  758.   /* If this command is repeated with RET,
  759.      turn it into the no-arg variant.  */
  760.  
  761.   if (from_tty)
  762.     *arg = 0;
  763.  
  764.   if (dummy_beg && sal_end.symtab == 0)
  765.     error ("No default source file yet.  Do \"help list\".");
  766.   if (dummy_beg)
  767.     print_source_lines (sal_end.symtab, max (sal_end.line - 9, 1),
  768.             sal_end.line + 1, 0);
  769.   else if (sal.symtab == 0)
  770.     error ("No default source file yet.  Do \"help list\".");
  771.   else if (no_end)
  772.     print_source_lines (sal.symtab, max (sal.line - 5, 1), sal.line + 5, 0);
  773.   else
  774.     print_source_lines (sal.symtab, sal.line,
  775.             dummy_end ? sal.line + 10 : sal_end.line + 1,
  776.             0);
  777. }
  778.  
  779. /* Print info on range of pc's in a specified line.  */
  780.  
  781. static void
  782. line_info (arg, from_tty)
  783.      char *arg;
  784.      int from_tty;
  785. {
  786.   struct symtabs_and_lines sals;
  787.   struct symtab_and_line sal;
  788.   int start_pc, end_pc;
  789.   int i;
  790.  
  791.   if (arg == 0)
  792.     {
  793.       sal.symtab = current_source_symtab;
  794.       sal.line = last_line_listed;
  795.       sals.nelts = 1;
  796.       sals.sals = (struct symtab_and_line *)
  797.     xmalloc (sizeof (struct symtab_and_line));
  798.       sals.sals[0] = sal;
  799.     }
  800.   else
  801.     {
  802.       sals = decode_line_spec_1 (arg, 0);
  803.       
  804.       /* If this command is repeated with RET,
  805.      turn it into the no-arg variant.  */
  806.       if (from_tty)
  807.     *arg = 0;
  808.     }
  809.  
  810.   /* C++  More than one line may have been specified, as when the user
  811.      specifies an overloaded function name. Print info on them all. */
  812.   for (i = 0; i < sals.nelts; i++)
  813.     {
  814.       sal = sals.sals[i];
  815.       
  816.       if (sal.symtab == 0)
  817.     error ("No source file specified.");
  818.  
  819.       if (sal.line > 0
  820.       && find_line_pc_range (sal.symtab, sal.line, &start_pc, &end_pc))
  821.     {
  822.       if (start_pc == end_pc)
  823.         printf ("Line %d of \"%s\" is at pc 0x%x but contains no code.\n",
  824.             sal.line, sal.symtab->filename, start_pc);
  825.       else
  826.         printf ("Line %d of \"%s\" starts at pc 0x%x and ends at 0x%x.\n",
  827.             sal.line, sal.symtab->filename, start_pc, end_pc);
  828.       /* x/i should display this line's code.  */
  829.       set_next_address (start_pc);
  830.       /* Repeating "info line" should do the following line.  */
  831.       last_line_listed = sal.line + 1;
  832.     }
  833.       else
  834.     printf ("Line number %d is out of range for \"%s\".\n",
  835.         sal.line, sal.symtab->filename);
  836.     }
  837. }
  838.  
  839. /* Commands to search the source file for a regexp.  */
  840.  
  841. static void
  842. forward_search_command (regex, from_tty)
  843.      char *regex;
  844. {
  845.   register int c;
  846.   register int desc;
  847.   register FILE *stream;
  848.   int line = last_line_listed + 1;
  849.   char *msg;
  850.  
  851.   msg = (char *) re_comp (regex);
  852.   if (msg)
  853.     error (msg);
  854.  
  855.   if (current_source_symtab == 0)
  856.     select_source_symtab (0);
  857.  
  858.   /* Search from last_line_listed+1 in current_source_symtab */
  859.  
  860.   desc = openp (source_path, 0, current_source_symtab->filename,
  861.         O_RDONLY, 0, ¤t_source_symtab->fullname);
  862.   if (desc < 0)
  863.     perror_with_name (current_source_symtab->filename);
  864.  
  865.   if (current_source_symtab->line_charpos == 0)
  866.     find_source_lines (current_source_symtab, desc);
  867.  
  868.   if (line < 1 || line > current_source_symtab->nlines)
  869.     {
  870.       close (desc);
  871.       error ("Expression not found");
  872.     }
  873.  
  874.   if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
  875.     {
  876.       close (desc);
  877.       perror_with_name (current_source_symtab->filename);
  878.     }
  879.  
  880.   stream = fdopen (desc, "r");
  881.   clearerr (stream);
  882.   while (1) {
  883.     char buf[4096];        /* Should be reasonable??? */
  884.     register char *p = buf;
  885.  
  886.     c = fgetc (stream);
  887.     if (c == EOF)
  888.       break;
  889.     do {
  890.       *p++ = c;
  891.     } while (c != '\n' && (c = fgetc (stream)) >= 0);
  892.  
  893.     /* we now have a source line in buf, null terminate and match */
  894.     *p = 0;
  895.     if (re_exec (buf) > 0)
  896.       {
  897.     /* Match! */
  898.     fclose (stream);
  899.     print_source_lines (current_source_symtab,
  900.                line, line+1, 0);
  901.     current_source_line = max (line - 5, 1);
  902.     return;
  903.       }
  904.     line++;
  905.   }
  906.  
  907.   printf ("Expression not found\n");
  908.   fclose (stream);
  909. }
  910.  
  911. static void
  912. reverse_search_command (regex, from_tty)
  913.      char *regex;
  914. {
  915.   register int c;
  916.   register int desc;
  917.   register FILE *stream;
  918.   int line = last_line_listed - 1;
  919.   char *msg;
  920.  
  921.   msg = (char *) re_comp (regex);
  922.   if (msg)
  923.     error (msg);
  924.  
  925.   if (current_source_symtab == 0)
  926.     select_source_symtab (0);
  927.  
  928.   /* Search from last_line_listed-1 in current_source_symtab */
  929.  
  930.   desc = openp (source_path, 0, current_source_symtab->filename,
  931.         O_RDONLY, 0, ¤t_source_symtab->fullname);
  932.   if (desc < 0)
  933.     perror_with_name (current_source_symtab->filename);
  934.  
  935.   if (current_source_symtab->line_charpos == 0)
  936.     find_source_lines (current_source_symtab, desc);
  937.  
  938.   if (line < 1 || line > current_source_symtab->nlines)
  939.     {
  940.       close (desc);
  941.       error ("Expression not found");
  942.     }
  943.  
  944.   if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
  945.     {
  946.       close (desc);
  947.       perror_with_name (current_source_symtab->filename);
  948.     }
  949.  
  950.   stream = fdopen (desc, "r");
  951.   clearerr (stream);
  952.   while (1)
  953.     {
  954.       char buf[4096];        /* Should be reasonable??? */
  955.       register char *p = buf;
  956.  
  957.       c = fgetc (stream);
  958.       if (c == EOF)
  959.     break;
  960.       do {
  961.     *p++ = c;
  962.       } while (c != '\n' && (c = fgetc (stream)) >= 0);
  963.  
  964.       /* We now have a source line in buf; null terminate and match.  */
  965.       *p = 0;
  966.       if (re_exec (buf) > 0)
  967.     {
  968.       /* Match! */
  969.       fclose (stream);
  970.       print_source_lines (current_source_symtab,
  971.                   line, line+1, 0);
  972.       current_source_line = max (line - 5, 1);
  973.       return;
  974.     }
  975.       line--;
  976.       if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
  977.     {
  978.       fclose (stream);
  979.       perror_with_name (current_source_symtab->filename);
  980.     }
  981.     }
  982.  
  983.   printf ("Expression not found\n");
  984.   fclose (stream);
  985.   return;
  986. }
  987.  
  988. void
  989. _initialize_source ()
  990. {
  991.   current_source_symtab = 0;
  992.   init_source_path ();
  993.  
  994.   add_com ("directory", class_files, directory_command,
  995.        "Add directory DIR to end of search path for source files.\n\
  996. With no argument, reset the search path to just the working directory\n\
  997. and forget cached info on line positions in source files.");
  998.  
  999.   add_info ("directories", directories_info,
  1000.         "Current search path for finding source files.");
  1001.  
  1002.   add_info ("line", line_info,
  1003.         "Core addresses of the code for a source line.\n\
  1004. Line can be specified as\n\
  1005.   LINENUM, to list around that line in current file,\n\
  1006.   FILE:LINENUM, to list around that line in that file,\n\
  1007.   FUNCTION, to list around beginning of that function,\n\
  1008.   FILE:FUNCTION, to distinguish among like-named static functions.\n\
  1009. Default is to describe the last source line that was listed.\n\n\
  1010. This sets the default address for \"x\" to the line's first instruction\n\
  1011. so that \"x/i\" suffices to start examining the machine code.\n\
  1012. The address is also stored as the value of \"$_\".");
  1013.  
  1014.   add_com ("forward-search", class_files, forward_search_command,
  1015.        "Search for regular expression (see regex(3)) from last line listed.");
  1016.   add_com_alias ("search", "forward-search", class_files, 0);
  1017.  
  1018.   add_com ("reverse-search", class_files, reverse_search_command,
  1019.        "Search backward for regular expression (see regex(3)) from last line listed.");
  1020.  
  1021.   add_com ("list", class_files, list_command,
  1022.        "List specified function or line.\n\
  1023. With no argument, lists ten more lines after or around previous listing.\n\
  1024. \"list -\" lists the ten lines before a previous ten-line listing.\n\
  1025. One argument specifies a line, and ten lines are listed around that line.\n\
  1026. Two arguments with comma between specify starting and ending lines to list.\n\
  1027. Lines can be specified in these ways:\n\
  1028.   LINENUM, to list around that line in current file,\n\
  1029.   FILE:LINENUM, to list around that line in that file,\n\
  1030.   FUNCTION, to list around beginning of that function,\n\
  1031.   FILE:FUNCTION, to distinguish among like-named static functions.\n\
  1032.   *ADDRESS, to list around the line containing that address.\n\
  1033. With two args if one is empty it stands for ten lines away from the other arg.");
  1034. }
  1035.  
  1036.