Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [RFA] Add -s option to source command.
@ 2010-04-06 21:58 Doug Evans
  2010-04-06 23:17 ` Doug Evans
                   ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: Doug Evans @ 2010-04-06 21:58 UTC (permalink / raw)
  To: gdb-patches

Hi.

In a big source tree with multiple disparate components it's useful to be
able to load scripts without having to type full path names.

This patch adds a -s option to the source command to make it search
for the script in the source search path even if the script name specifies
a directory.
For example, adding the top level source directory to the source search path
lets one load scripts from any component by only typing the path from
the top directory.

I thought of simply extending the source command to do the search anyway
but the current code explicitly doesn't do that to make "./foo" only mean
"foo" in the current directory.  Granted, the comment (see openp) is
referring to executables, but it's not clear to me it shouldn't also
apply to scripts.  Plus, "source /absolute/path" should probably not
scan the search path by default.
So I went with adding an option.

This patch also does a minor reorg of find_and_open_script so that
it's useful in a subsequent patch I'll be submitting.
I think the reorg is useful in itself, e.g. removing the from_tty
arg to find_and_open_script, so I've including it here.

Included are doc and testcase additions.

Ok to check in?

2010-04-06  Doug Evans  <dje@google.com>

	Add -s option to source command.
	* cli/cli-cmds.c (find_and_open_script): Add function comment.
	Delete from_tty and cleanupp args.  Split filep arg into file and
	full_pathp.  New arg search_path.
	(source_script_from_stream): New function.
	(source_script_with_search): New function.
	(source_script): Rewrite.
	(source_command): Parse "-s" option.
	(init_cli_cmds): Add "-s" docs to source command help.
	* python/python.c (source_python_script): Make file arg a const char *.
	Don't call fclose, leave for caller.
	* python/python.h (source_python_script): Update.

	testsuite/
	* gdb.base/source-test.gdb: New file.
	* gdb.base/source.exp: Add tests for "source -v" and "source -s".

	doc/
	* gdb.texinfo (Command Files): Add docs for new "source -s" option.

Index: cli/cli-cmds.c
===================================================================
RCS file: /cvs/src/src/gdb/cli/cli-cmds.c,v
retrieving revision 1.98
diff -u -p -r1.98 cli-cmds.c
--- cli/cli-cmds.c	5 Mar 2010 20:18:15 -0000	1.98
+++ cli/cli-cmds.c	6 Apr 2010 21:23:23 -0000
@@ -470,62 +470,59 @@ Script filename extension recognition is
 		    value);
 }
 
+/* Try to open SCRIPT_FILE.
+   If successful, the full path name is stored in *FULL_PATHP,
+   the stream is stored in *STREAMP, and return 1.
+   The caller is responsible for freeing *FULL_PATHP.
+   If not successful, return 0; errno is set for the last file
+   we tried to open.
+
+   If SEARCH_PATH is non-zero, and the file isn't found in cwd,
+   search for it in the source search path.
+
+   NOTE: This calls openp which uses xfullpath to compute the full path
+   instead of gdb_realpath.  Symbolic links are not resolved.  */
+
 static int
-find_and_open_script (int from_tty, char **filep, FILE **streamp,
-		      struct cleanup **cleanupp)
+find_and_open_script (const char *script_file, int search_path,
+		      FILE **streamp, char **full_pathp)
 {
-  char *file = *filep;
-  char *full_pathname = NULL;
+  char *file;
   int fd;
   struct cleanup *old_cleanups;
+  int search_flags = OPF_TRY_CWD_FIRST;
 
-  file = tilde_expand (file);
+  file = tilde_expand (script_file);
   old_cleanups = make_cleanup (xfree, file);
 
+  if (search_path)
+    search_flags |= OPF_SEARCH_IN_PATH;
+
   /* Search for and open 'file' on the search path used for source
      files.  Put the full location in 'full_pathname'.  */
-  fd = openp (source_path, OPF_TRY_CWD_FIRST,
-	      file, O_RDONLY, &full_pathname);
-  make_cleanup (xfree, full_pathname);
-
-  /* Use the full path name, if it is found.  */
-  if (full_pathname != NULL && fd != -1)
-    {
-      file = full_pathname;
-    }
+  fd = openp (source_path, search_flags,
+	      file, O_RDONLY, full_pathp);
 
   if (fd == -1)
     {
-      if (from_tty)
-	perror_with_name (file);
-      else
-	{
-	  do_cleanups (old_cleanups);
-	  return 0;
-	}
+      int save_errno = errno;
+      do_cleanups (old_cleanups);
+      errno = save_errno;
+      return 0;
     }
 
-  *streamp = fdopen (fd, FOPEN_RT);
-  *filep = file;
-  *cleanupp = old_cleanups;
+  do_cleanups (old_cleanups);
 
+  *streamp = fdopen (fd, FOPEN_RT);
   return 1;
 }
 
-void
-source_script (char *file, int from_tty)
-{
-  FILE *stream;
-  struct cleanup *old_cleanups;
-
-  if (file == NULL || *file == 0)
-    {
-      error (_("source command requires file name of file to source."));
-    }
-
-  if (!find_and_open_script (from_tty, &file, &stream, &old_cleanups))
-    return;
+/* Load script FILE, which has already been opened as STREAM.
+   STREAM is closed before we return.  */
 
+static void
+source_script_from_stream (FILE *stream, const char *file)
+{
   if (script_ext_mode != script_ext_off
       && strlen (file) > 3 && !strcmp (&file[strlen (file) - 3], ".py"))
     {
@@ -541,22 +538,64 @@ source_script (char *file, int from_tty)
 	  if (script_ext_mode == script_ext_soft
 	      && e.reason == RETURN_ERROR && e.error == UNSUPPORTED_ERROR)
 	    {
-	      if (!find_and_open_script (from_tty, &file, &stream, &old_cleanups))
-		return;
-
-	      script_from_file (stream, file);
+	      fseek (stream, 0, SEEK_SET);
+	      script_from_file (stream, (char*) file);
 	    }
 	  else
-	    /* Nope, just punt.  */
-	    throw_exception (e);
+	    {
+	      /* Nope, just punt.  */
+	      fclose (stream);
+	      throw_exception (e);
+	    }
 	}
+      else
+	fclose (stream);
     }
   else
     script_from_file (stream, file);
+}
 
+/* Worker to perform the "source" command.
+   Load script FILE.
+   If SEARCH_PATH is non-zero, and the file isn't found in cwd,
+   search for it in the source search path.  */
+
+static void
+source_script_with_search (const char *file, int from_tty, int search_path)
+{
+  FILE *stream;
+  char *full_path;
+  struct cleanup *old_cleanups;
+
+  if (file == NULL || *file == 0)
+    error (_("source command requires file name of file to source."));
+
+  if (!find_and_open_script (file, search_path, &stream, &full_path))
+    {
+      /* The script wasn't found, or was otherwise inaccessible.
+	 If the source command was invoked interactively, throw an error.
+	 Otherwise (e.g. if it was invoked by a script), silently ignore
+	 the error.  */
+      if (from_tty)
+	perror_with_name (file);
+      else
+	return;
+    }
+
+  old_cleanups = make_cleanup (xfree, full_path);
+  source_script_from_stream (stream, file);
   do_cleanups (old_cleanups);
 }
 
+/* Wrapper around source_script_with_search to export it to main.c
+   for use in loading .gdbinit scripts.  */
+
+void
+source_script (char *file, int from_tty)
+{
+  source_script_with_search (file, from_tty, 0);
+}
+
 /* Return the source_verbose global variable to its previous state
    on exit from the source command, by whatever means.  */
 static void
@@ -572,33 +611,52 @@ source_command (char *args, int from_tty
   struct cleanup *old_cleanups;
   char *file = args;
   int *old_source_verbose = xmalloc (sizeof(int));
+  int search_path = 0;
 
   *old_source_verbose = source_verbose;
   old_cleanups = make_cleanup (source_verbose_cleanup, old_source_verbose);
 
   /* -v causes the source command to run in verbose mode.
+     -s causes the file to be searched in the source search path,
+     even if the file name contains a '/'.
      We still have to be able to handle filenames with spaces in a
      backward compatible way, so buildargv is not appropriate.  */
 
   if (args)
     {
-      /* Make sure leading white space does not break the comparisons.  */
-      while (isspace(args[0]))
-	args++;
-
-      /* Is -v the first thing in the string?  */
-      if (args[0] == '-' && args[1] == 'v' && isspace (args[2]))
+      while (args[0] != '\0')
 	{
-	  source_verbose = 1;
+	  /* Make sure leading white space does not break the comparisons.  */
+	  while (isspace(args[0]))
+	    args++;
+
+	  if (args[0] != '-')
+	    break;
+
+	  if (args[1] == 'v' && isspace (args[2]))
+	    {
+	      source_verbose = 1;
+
+	      /* Skip passed -v.  */
+	      args = &args[3];
+	    }
+	  else if (args[1] == 's' && isspace (args[2]))
+	    {
+	      search_path = 1;
 
-	  /* Trim -v and whitespace from the filename.  */
-	  file = &args[3];
-	  while (isspace (file[0]))
-	    file++;
+	      /* Skip passed -s.  */
+	      args = &args[3];
+	    }
+	  else
+	    break;
 	}
+
+      while (isspace (args[0]))
+	args++;
+      file = args;
     }
 
-  source_script (file, from_tty);
+  source_script_with_search (file, from_tty, search_path);
 }
 
 
@@ -1379,6 +1437,8 @@ Commands defined in this way may have up
 Read commands from a file named FILE.\n\
 Optional -v switch (before the filename) causes each command in\n\
 FILE to be echoed as it is executed.\n\
+Optional -s switch (before the filename) causes gdb to search for\n\
+the script in the source search path, even if FILE contains directories.\n\
 Note that the file \"%s\" is read automatically in this way\n\
 when GDB is started."), gdbinit);
   c = add_cmd ("source", class_support, source_command,
Index: python/python.c
===================================================================
RCS file: /cvs/src/src/gdb/python/python.c,v
retrieving revision 1.27
diff -u -p -r1.27 python.c
--- python/python.c	5 Mar 2010 20:18:17 -0000	1.27
+++ python/python.c	6 Apr 2010 21:23:24 -0000
@@ -362,10 +362,11 @@ gdbpy_parse_and_eval (PyObject *self, Py
 }
 
 /* Read a file as Python code.  STREAM is the input file; FILE is the
-   name of the file.  */
+   name of the file.
+   STREAM is not closed, that is the caller's responsibility.  */
 
 void
-source_python_script (FILE *stream, char *file)
+source_python_script (FILE *stream, const char *file)
 {
   PyGILState_STATE state;
 
@@ -373,7 +374,6 @@ source_python_script (FILE *stream, char
 
   PyRun_SimpleFile (stream, file);
 
-  fclose (stream);
   PyGILState_Release (state);
 }
 
@@ -560,9 +560,8 @@ eval_python_from_control_command (struct
 }
 
 void
-source_python_script (FILE *stream, char *file)
+source_python_script (FILE *stream, const char *file)
 {
-  fclose (stream);
   throw_error (UNSUPPORTED_ERROR,
 	       _("Python scripting is not supported in this copy of GDB."));
 }
Index: python/python.h
===================================================================
RCS file: /cvs/src/src/gdb/python/python.h,v
retrieving revision 1.7
diff -u -p -r1.7 python.h
--- python/python.h	18 Jan 2010 06:25:22 -0000	1.7
+++ python/python.h	6 Apr 2010 21:23:24 -0000
@@ -24,7 +24,7 @@
 
 void eval_python_from_control_command (struct command_line *);
 
-void source_python_script (FILE *stream, char *file);
+void source_python_script (FILE *stream, const char *file);
 
 int apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
 			      int embedded_offset, CORE_ADDR address,
Index: testsuite/gdb.base/source-test.gdb
===================================================================
RCS file: testsuite/gdb.base/source-test.gdb
diff -N testsuite/gdb.base/source-test.gdb
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ testsuite/gdb.base/source-test.gdb	6 Apr 2010 21:23:24 -0000
@@ -0,0 +1,20 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2010 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test GDB's "source -v" and "source -s" commands.
+
+echo test source options\n
Index: testsuite/gdb.base/source.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.base/source.exp,v
retrieving revision 1.9
diff -u -p -r1.9 source.exp
--- testsuite/gdb.base/source.exp	7 Jan 2010 20:07:18 -0000	1.9
+++ testsuite/gdb.base/source.exp	6 Apr 2010 21:23:24 -0000
@@ -34,5 +34,14 @@ gdb_start
 gdb_test "source ${srcdir}/${subdir}/source-error.gdb" \
     "source-error.gdb:21: Error in sourced command file:\[\r\n\]*Cannot access memory at address 0x0.*" \
     "script contains error"
+
+gdb_test "source -v ${srcdir}/${subdir}/source-test.gdb" \
+    "echo test source options.*" \
+    "source -v"
+
+gdb_test "dir ${srcdir}/${subdir}" ""
+gdb_test "source -s ./source-test.gdb" \
+    "test source options" \
+    "source -s"
     
 gdb_exit
Index: doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.696
diff -u -p -r1.696 gdb.texinfo
--- doc/gdb.texinfo	5 Apr 2010 17:14:57 -0000	1.696
+++ doc/gdb.texinfo	6 Apr 2010 21:28:53 -0000
@@ -19373,7 +19373,7 @@ using the @code{script-extension} settin
 @table @code
 @kindex source
 @cindex execute commands from a file
-@item source [@code{-v}] @var{filename}
+@item source [@code{-s}] [@code{-v}] @var{filename}
 Execute the command file @var{filename}.
 @end table
 
@@ -19390,6 +19390,9 @@ directory, then @value{GDBN} also looks 
 except that @file{$cdir} is not searched because the compilation directory
 is not relevant to scripts.
 
+If @code{-s} is specified, then @value{GDBN} searches for @var{filename}
+on the search path even if @var{filename} specifies a directory.
+
 If @code{-v}, for verbose mode, is given then @value{GDBN} displays
 each command as it is executed.  The option must be given before
 @var{filename}, and is interpreted as part of the filename anywhere else.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-06 21:58 [RFA] Add -s option to source command Doug Evans
@ 2010-04-06 23:17 ` Doug Evans
  2010-04-07 20:14 ` Tom Tromey
  2010-04-17 15:20 ` H.J. Lu
  2 siblings, 0 replies; 26+ messages in thread
From: Doug Evans @ 2010-04-06 23:17 UTC (permalink / raw)
  To: gdb-patches

On Tue, Apr 6, 2010 at 2:57 PM, Doug Evans <dje@google.com> wrote:
> Hi.
>
> In a big source tree with multiple disparate components it's useful to be
> able to load scripts without having to type full path names.
>
> This patch adds a -s option to the source command to make it search
> for the script in the source search path even if the script name specifies
> a directory.

fyi,
I figured it would be useful to test "source -s -v" and "source -v -s"
so I added that to the patch.  Then I noticed that "source -v" leaves
-v stuck in the "on" position.  I'll submit a separate patch to fix
that, and then update my source -s patch.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-06 21:58 [RFA] Add -s option to source command Doug Evans
  2010-04-06 23:17 ` Doug Evans
@ 2010-04-07 20:14 ` Tom Tromey
  2010-04-08 22:49   ` Doug Evans
  2010-04-17 15:20 ` H.J. Lu
  2 siblings, 1 reply; 26+ messages in thread
From: Tom Tromey @ 2010-04-07 20:14 UTC (permalink / raw)
  To: Doug Evans; +Cc: gdb-patches

>>>>> "Doug" == Doug Evans <dje@google.com> writes:

Doug> This patch adds a -s option to the source command to make it
Doug> search for the script in the source search path even if the script
Doug> name specifies a directory.

It seems reasonable to me.

Doug>  Read commands from a file named FILE.\n\
Doug>  Optional -v switch (before the filename) causes each command in\n\
Doug>  FILE to be echoed as it is executed.\n\
Doug> +Optional -s switch (before the filename) causes gdb to search for\n\
Doug> +the script in the source search path, even if FILE contains directories.\n\

I often wish that gdb help strings were formatted more like --help
output.

Tom


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-07 20:14 ` Tom Tromey
@ 2010-04-08 22:49   ` Doug Evans
  2010-04-08 23:09     ` Sergio Durigan Junior
                       ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: Doug Evans @ 2010-04-08 22:49 UTC (permalink / raw)
  To: tromey; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 2300 bytes --]

On Wed, Apr 7, 2010 at 1:14 PM, Tom Tromey <tromey@redhat.com> wrote:
>>>>>> "Doug" == Doug Evans <dje@google.com> writes:
>
> Doug> This patch adds a -s option to the source command to make it
> Doug> search for the script in the source search path even if the script
> Doug> name specifies a directory.
>
> It seems reasonable to me.
>
> Doug>  Read commands from a file named FILE.\n\
> Doug>  Optional -v switch (before the filename) causes each command in\n\
> Doug>  FILE to be echoed as it is executed.\n\
> Doug> +Optional -s switch (before the filename) causes gdb to search for\n\
> Doug> +the script in the source search path, even if FILE contains directories.\n\
>
> I often wish that gdb help strings were formatted more like --help
> output.
>
> Tom
>

Something like this?

@@ -1379,8 +1437,12 @@ Commands defined in this way may have up

   source_help_text = xstrprintf (_("\
 Read commands from a file named FILE.\n\
-Optional -v switch (before the filename) causes each command in\n\
-FILE to be echoed as it is executed.\n\
+\n\
+Usage: source [-s] [-v] FILE\n\
+-s: search for the script in the source search path,\n\
+    even if FILE contains directories.\n\
+-v: each command in FILE is echoed as it is executed.\n\
+\n\
 Note that the file \"%s\" is read automatically in this way\n\
 when GDB is started."), gdbinit);
   c = add_cmd ("source", class_support, source_command,

2010-04-08  Doug Evans  <dje@google.com>

	Add -s option to source command.
	* NEWS: Document new option.
	* cli/cli-cmds.c (find_and_open_script): Add function comment.
	Delete from_tty and cleanupp args.  Split filep arg into file and
	full_pathp.  New arg search_path.
	(source_script_from_stream): New function.
	(source_script_with_search): New function.
	(source_script): Rewrite.
	(source_command): Parse "-s" option.
	(init_cli_cmds): Add "-s" docs to source command help, and reformat.
	* python/python.c (source_python_script): Make file arg a const char *.
	Don't call fclose, leave for caller.
	* python/python.h (source_python_script): Update.

	testsuite/
	* gdb.base/source-test.gdb: New file.
	* gdb.base/source.exp: Add tests for "source -s".

	doc/
	* gdb.texinfo (Command Files): Add docs for new "source -s" option.

[-- Attachment #2: gdb-100408-source-s-2.patch.txt --]
[-- Type: text/plain, Size: 12510 bytes --]

2010-04-08  Doug Evans  <dje@google.com>

	Add -s option to source command.
	* NEWS: Document new option.
	* cli/cli-cmds.c (find_and_open_script): Add function comment.
	Delete from_tty and cleanupp args.  Split filep arg into file and
	full_pathp.  New arg search_path.
	(source_script_from_stream): New function.
	(source_script_with_search): New function.
	(source_script): Rewrite.
	(source_command): Parse "-s" option.
	(init_cli_cmds): Add "-s" docs to source command help, and reformat.
	* python/python.c (source_python_script): Make file arg a const char *.
	Don't call fclose, leave for caller.
	* python/python.h (source_python_script): Update.

	testsuite/
	* gdb.base/source-test.gdb: New file.
	* gdb.base/source.exp: Add tests for "source -s".

	doc/
	* gdb.texinfo (Command Files): Add docs for new "source -s" option.

Index: NEWS
===================================================================
RCS file: /cvs/src/src/gdb/NEWS,v
retrieving revision 1.367
diff -u -p -r1.367 NEWS
--- NEWS	1 Apr 2010 14:11:22 -0000	1.367
+++ NEWS	8 Apr 2010 22:40:59 -0000
@@ -3,6 +3,10 @@
 
 *** Changes since GDB 7.1
 
+* The source command now accepts a -s option to force searching for the
+  script in the source search path even if the script name specifies
+  a directory.
+
 * GDB now sends xmlRegisters= in qSupported packet to indicate that
   it understands register description.
 
Index: cli/cli-cmds.c
===================================================================
RCS file: /cvs/src/src/gdb/cli/cli-cmds.c,v
retrieving revision 1.99
diff -u -p -r1.99 cli-cmds.c
--- cli/cli-cmds.c	7 Apr 2010 03:42:55 -0000	1.99
+++ cli/cli-cmds.c	8 Apr 2010 22:40:59 -0000
@@ -470,62 +470,59 @@ Script filename extension recognition is
 		    value);
 }
 
+/* Try to open SCRIPT_FILE.
+   If successful, the full path name is stored in *FULL_PATHP,
+   the stream is stored in *STREAMP, and return 1.
+   The caller is responsible for freeing *FULL_PATHP.
+   If not successful, return 0; errno is set for the last file
+   we tried to open.
+
+   If SEARCH_PATH is non-zero, and the file isn't found in cwd,
+   search for it in the source search path.
+
+   NOTE: This calls openp which uses xfullpath to compute the full path
+   instead of gdb_realpath.  Symbolic links are not resolved.  */
+
 static int
-find_and_open_script (int from_tty, char **filep, FILE **streamp,
-		      struct cleanup **cleanupp)
+find_and_open_script (const char *script_file, int search_path,
+		      FILE **streamp, char **full_pathp)
 {
-  char *file = *filep;
-  char *full_pathname = NULL;
+  char *file;
   int fd;
   struct cleanup *old_cleanups;
+  int search_flags = OPF_TRY_CWD_FIRST;
 
-  file = tilde_expand (file);
+  file = tilde_expand (script_file);
   old_cleanups = make_cleanup (xfree, file);
 
-  /* Search for and open 'file' on the search path used for source
-     files.  Put the full location in 'full_pathname'.  */
-  fd = openp (source_path, OPF_TRY_CWD_FIRST,
-	      file, O_RDONLY, &full_pathname);
-  make_cleanup (xfree, full_pathname);
+  if (search_path)
+    search_flags |= OPF_SEARCH_IN_PATH;
 
-  /* Use the full path name, if it is found.  */
-  if (full_pathname != NULL && fd != -1)
-    {
-      file = full_pathname;
-    }
+  /* Search for and open 'file' on the search path used for source
+     files.  Put the full location in *FULL_PATHP.  */
+  fd = openp (source_path, search_flags,
+	      file, O_RDONLY, full_pathp);
 
   if (fd == -1)
     {
-      if (from_tty)
-	perror_with_name (file);
-      else
-	{
-	  do_cleanups (old_cleanups);
-	  return 0;
-	}
+      int save_errno = errno;
+      do_cleanups (old_cleanups);
+      errno = save_errno;
+      return 0;
     }
 
-  *streamp = fdopen (fd, FOPEN_RT);
-  *filep = file;
-  *cleanupp = old_cleanups;
+  do_cleanups (old_cleanups);
 
+  *streamp = fdopen (fd, FOPEN_RT);
   return 1;
 }
 
-void
-source_script (char *file, int from_tty)
-{
-  FILE *stream;
-  struct cleanup *old_cleanups;
-
-  if (file == NULL || *file == 0)
-    {
-      error (_("source command requires file name of file to source."));
-    }
-
-  if (!find_and_open_script (from_tty, &file, &stream, &old_cleanups))
-    return;
+/* Load script FILE, which has already been opened as STREAM.
+   STREAM is closed before we return.  */
 
+static void
+source_script_from_stream (FILE *stream, const char *file)
+{
   if (script_ext_mode != script_ext_off
       && strlen (file) > 3 && !strcmp (&file[strlen (file) - 3], ".py"))
     {
@@ -541,22 +538,64 @@ source_script (char *file, int from_tty)
 	  if (script_ext_mode == script_ext_soft
 	      && e.reason == RETURN_ERROR && e.error == UNSUPPORTED_ERROR)
 	    {
-	      if (!find_and_open_script (from_tty, &file, &stream, &old_cleanups))
-		return;
-
-	      script_from_file (stream, file);
+	      fseek (stream, 0, SEEK_SET);
+	      script_from_file (stream, (char*) file);
 	    }
 	  else
-	    /* Nope, just punt.  */
-	    throw_exception (e);
+	    {
+	      /* Nope, just punt.  */
+	      fclose (stream);
+	      throw_exception (e);
+	    }
 	}
+      else
+	fclose (stream);
     }
   else
     script_from_file (stream, file);
+}
 
+/* Worker to perform the "source" command.
+   Load script FILE.
+   If SEARCH_PATH is non-zero, and the file isn't found in cwd,
+   search for it in the source search path.  */
+
+static void
+source_script_with_search (const char *file, int from_tty, int search_path)
+{
+  FILE *stream;
+  char *full_path;
+  struct cleanup *old_cleanups;
+
+  if (file == NULL || *file == 0)
+    error (_("source command requires file name of file to source."));
+
+  if (!find_and_open_script (file, search_path, &stream, &full_path))
+    {
+      /* The script wasn't found, or was otherwise inaccessible.
+	 If the source command was invoked interactively, throw an error.
+	 Otherwise (e.g. if it was invoked by a script), silently ignore
+	 the error.  */
+      if (from_tty)
+	perror_with_name (file);
+      else
+	return;
+    }
+
+  old_cleanups = make_cleanup (xfree, full_path);
+  source_script_from_stream (stream, file);
   do_cleanups (old_cleanups);
 }
 
+/* Wrapper around source_script_with_search to export it to main.c
+   for use in loading .gdbinit scripts.  */
+
+void
+source_script (char *file, int from_tty)
+{
+  source_script_with_search (file, from_tty, 0);
+}
+
 /* Return the source_verbose global variable to its previous state
    on exit from the source command, by whatever means.  */
 static void
@@ -572,33 +611,52 @@ source_command (char *args, int from_tty
   struct cleanup *old_cleanups;
   char *file = args;
   int *old_source_verbose = xmalloc (sizeof(int));
+  int search_path = 0;
 
   *old_source_verbose = source_verbose;
   old_cleanups = make_cleanup (source_verbose_cleanup, old_source_verbose);
 
   /* -v causes the source command to run in verbose mode.
+     -s causes the file to be searched in the source search path,
+     even if the file name contains a '/'.
      We still have to be able to handle filenames with spaces in a
      backward compatible way, so buildargv is not appropriate.  */
 
   if (args)
     {
-      /* Make sure leading white space does not break the comparisons.  */
-      while (isspace(args[0]))
-	args++;
-
-      /* Is -v the first thing in the string?  */
-      if (args[0] == '-' && args[1] == 'v' && isspace (args[2]))
+      while (args[0] != '\0')
 	{
-	  source_verbose = 1;
+	  /* Make sure leading white space does not break the comparisons.  */
+	  while (isspace(args[0]))
+	    args++;
+
+	  if (args[0] != '-')
+	    break;
+
+	  if (args[1] == 'v' && isspace (args[2]))
+	    {
+	      source_verbose = 1;
+
+	      /* Skip passed -v.  */
+	      args = &args[3];
+	    }
+	  else if (args[1] == 's' && isspace (args[2]))
+	    {
+	      search_path = 1;
 
-	  /* Trim -v and whitespace from the filename.  */
-	  file = &args[3];
-	  while (isspace (file[0]))
-	    file++;
+	      /* Skip passed -s.  */
+	      args = &args[3];
+	    }
+	  else
+	    break;
 	}
+
+      while (isspace (args[0]))
+	args++;
+      file = args;
     }
 
-  source_script (file, from_tty);
+  source_script_with_search (file, from_tty, search_path);
 
   do_cleanups (old_cleanups);
 }
@@ -1379,8 +1437,12 @@ Commands defined in this way may have up
 
   source_help_text = xstrprintf (_("\
 Read commands from a file named FILE.\n\
-Optional -v switch (before the filename) causes each command in\n\
-FILE to be echoed as it is executed.\n\
+\n\
+Usage: source [-s] [-v] FILE\n\
+-s: search for the script in the source search path,\n\
+    even if FILE contains directories.\n\
+-v: each command in FILE is echoed as it is executed.\n\
+\n\
 Note that the file \"%s\" is read automatically in this way\n\
 when GDB is started."), gdbinit);
   c = add_cmd ("source", class_support, source_command,
Index: doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.697
diff -u -p -r1.697 gdb.texinfo
--- doc/gdb.texinfo	8 Apr 2010 21:08:40 -0000	1.697
+++ doc/gdb.texinfo	8 Apr 2010 22:41:00 -0000
@@ -19373,7 +19373,7 @@ using the @code{script-extension} settin
 @table @code
 @kindex source
 @cindex execute commands from a file
-@item source [@code{-v}] @var{filename}
+@item source [@code{-s}] [@code{-v}] @var{filename}
 Execute the command file @var{filename}.
 @end table
 
@@ -19390,6 +19390,9 @@ directory, then @value{GDBN} also looks 
 except that @file{$cdir} is not searched because the compilation directory
 is not relevant to scripts.
 
+If @code{-s} is specified, then @value{GDBN} searches for @var{filename}
+on the search path even if @var{filename} specifies a directory.
+
 If @code{-v}, for verbose mode, is given then @value{GDBN} displays
 each command as it is executed.  The option must be given before
 @var{filename}, and is interpreted as part of the filename anywhere else.
Index: python/python.c
===================================================================
RCS file: /cvs/src/src/gdb/python/python.c,v
retrieving revision 1.28
diff -u -p -r1.28 python.c
--- python/python.c	8 Apr 2010 04:57:25 -0000	1.28
+++ python/python.c	8 Apr 2010 22:41:00 -0000
@@ -362,10 +362,11 @@ gdbpy_parse_and_eval (PyObject *self, Py
 }
 
 /* Read a file as Python code.  STREAM is the input file; FILE is the
-   name of the file.  */
+   name of the file.
+   STREAM is not closed, that is the caller's responsibility.  */
 
 void
-source_python_script (FILE *stream, char *file)
+source_python_script (FILE *stream, const char *file)
 {
   struct cleanup *cleanup;
 
@@ -373,7 +374,6 @@ source_python_script (FILE *stream, char
 
   PyRun_SimpleFile (stream, file);
 
-  fclose (stream);
   do_cleanups (cleanup);
 }
 
@@ -560,9 +560,8 @@ eval_python_from_control_command (struct
 }
 
 void
-source_python_script (FILE *stream, char *file)
+source_python_script (FILE *stream, const char *file)
 {
-  fclose (stream);
   throw_error (UNSUPPORTED_ERROR,
 	       _("Python scripting is not supported in this copy of GDB."));
 }
Index: python/python.h
===================================================================
RCS file: /cvs/src/src/gdb/python/python.h,v
retrieving revision 1.7
diff -u -p -r1.7 python.h
--- python/python.h	18 Jan 2010 06:25:22 -0000	1.7
+++ python/python.h	8 Apr 2010 22:41:00 -0000
@@ -24,7 +24,7 @@
 
 void eval_python_from_control_command (struct command_line *);
 
-void source_python_script (FILE *stream, char *file);
+void source_python_script (FILE *stream, const char *file);
 
 int apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
 			      int embedded_offset, CORE_ADDR address,
Index: testsuite/gdb.base/source.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.base/source.exp,v
retrieving revision 1.11
diff -u -p -r1.11 source.exp
--- testsuite/gdb.base/source.exp	7 Apr 2010 03:47:22 -0000	1.11
+++ testsuite/gdb.base/source.exp	8 Apr 2010 22:41:00 -0000
@@ -51,4 +51,17 @@ gdb_test_multiple "source ${srcdir}/${su
     }
 }
 
+gdb_test "dir ${srcdir}/${subdir}" ""
+gdb_test "source -s ./source-test.gdb" \
+    "test source options" \
+    "source -s"
+
+# Test -v and -s in either order.
+gdb_test "source -s -v ./source-test.gdb" \
+    "echo test source options.*" \
+    "source -s -v"
+gdb_test "source -v -s ./source-test.gdb" \
+    "echo test source options.*" \
+    "source -v -s"
+
 gdb_exit

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-08 22:49   ` Doug Evans
@ 2010-04-08 23:09     ` Sergio Durigan Junior
  2010-04-09  0:13       ` Doug Evans
  2010-04-09 17:15       ` Tom Tromey
  2010-04-09  7:49     ` Eli Zaretskii
  2010-04-09 17:13     ` Tom Tromey
  2 siblings, 2 replies; 26+ messages in thread
From: Sergio Durigan Junior @ 2010-04-08 23:09 UTC (permalink / raw)
  To: gdb-patches; +Cc: Doug Evans, tromey

On Thursday 08 April 2010 19:49:41, Doug Evans wrote:
> On Wed, Apr 7, 2010 at 1:14 PM, Tom Tromey <tromey@redhat.com> wrote:
> > I often wish that gdb help strings were formatted more like --help
> > output.
> > 
> > Tom
> 
> Something like this?
> 
> @@ -1379,8 +1437,12 @@ Commands defined in this way may have up
> 
>    source_help_text = xstrprintf (_("\
>  Read commands from a file named FILE.\n\
> -Optional -v switch (before the filename) causes each command in\n\
> -FILE to be echoed as it is executed.\n\
> +\n\
> +Usage: source [-s] [-v] FILE\n\
> +-s: search for the script in the source search path,\n\
> +    even if FILE contains directories.\n\
> +-v: each command in FILE is echoed as it is executed.\n\
> +\n\
>  Note that the file \"%s\" is read automatically in this way\n\
>  when GDB is started."), gdbinit);
>    c = add_cmd ("source", class_support, source_command,

I totally agree with Tom here, but I don't know if it's a good thing to change 
the "standard" only in this feature without converting the other GDB help 
strings as well.  IMHO if we are going to start formatting the help strings 
like `--help' output, then we should do it for the majority (if not all) of 
the strings.

Anyway, my half cent here.

-- 
Sergio Durigan Junior
Debugger Engineer
Red Hat Inc.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-08 23:09     ` Sergio Durigan Junior
@ 2010-04-09  0:13       ` Doug Evans
  2010-04-09 17:15       ` Tom Tromey
  1 sibling, 0 replies; 26+ messages in thread
From: Doug Evans @ 2010-04-09  0:13 UTC (permalink / raw)
  To: Sergio Durigan Junior; +Cc: gdb-patches, tromey

On Thu, Apr 8, 2010 at 4:08 PM, Sergio Durigan Junior
<sergiodj@redhat.com> wrote:
> On Thursday 08 April 2010 19:49:41, Doug Evans wrote:
>> On Wed, Apr 7, 2010 at 1:14 PM, Tom Tromey <tromey@redhat.com> wrote:
>> > I often wish that gdb help strings were formatted more like --help
>> > output.
>> >
>> > Tom
>>
>> Something like this?
>>
>> @@ -1379,8 +1437,12 @@ Commands defined in this way may have up
>>
>>    source_help_text = xstrprintf (_("\
>>  Read commands from a file named FILE.\n\
>> -Optional -v switch (before the filename) causes each command in\n\
>> -FILE to be echoed as it is executed.\n\
>> +\n\
>> +Usage: source [-s] [-v] FILE\n\
>> +-s: search for the script in the source search path,\n\
>> +    even if FILE contains directories.\n\
>> +-v: each command in FILE is echoed as it is executed.\n\
>> +\n\
>>  Note that the file \"%s\" is read automatically in this way\n\
>>  when GDB is started."), gdbinit);
>>    c = add_cmd ("source", class_support, source_command,
>
> I totally agree with Tom here, but I don't know if it's a good thing to change
> the "standard" only in this feature without converting the other GDB help
> strings as well.  IMHO if we are going to start formatting the help strings
> like `--help' output, then we should do it for the majority (if not all) of
> the strings.
>
> Anyway, my half cent here.

Sure.
That one might want to wait and do it en-masse I left as a given.
The powers that be can decide, and I'll submit what they want.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-08 22:49   ` Doug Evans
  2010-04-08 23:09     ` Sergio Durigan Junior
@ 2010-04-09  7:49     ` Eli Zaretskii
  2010-04-09 17:23       ` Doug Evans
  2010-04-09 17:13     ` Tom Tromey
  2 siblings, 1 reply; 26+ messages in thread
From: Eli Zaretskii @ 2010-04-09  7:49 UTC (permalink / raw)
  To: Doug Evans; +Cc: tromey, gdb-patches

> Date: Thu, 8 Apr 2010 15:49:41 -0700
> From: Doug Evans <dje@google.com>
> Cc: gdb-patches@sourceware.org
> 
> 
> --- NEWS	1 Apr 2010 14:11:22 -0000	1.367
> +++ NEWS	8 Apr 2010 22:40:59 -0000
> @@ -3,6 +3,10 @@
>  
>  *** Changes since GDB 7.1
>  
> +* The source command now accepts a -s option to force searching for the
> +  script in the source search path even if the script name specifies
> +  a directory.
> +

This part is okay.

> --- doc/gdb.texinfo	8 Apr 2010 21:08:40 -0000	1.697
> +++ doc/gdb.texinfo	8 Apr 2010 22:41:00 -0000

This part is okay, with a couple of comments:

> @@ -19373,7 +19373,7 @@ using the @code{script-extension} settin
>  @table @code
>  @kindex source
>  @cindex execute commands from a file
> -@item source [@code{-v}] @var{filename}
> +@item source [@code{-s}] [@code{-v}] @var{filename}

Please remove the @code markup from the switches, it is redundant
(because this is "@table @code" already, so every @item gets the @code
markup by default).  Yes, the old text was also wrong.

> +If @code{-s} is specified, then @value{GDBN} searches for @var{filename}
> +on the search path even if @var{filename} specifies a directory.

I presume it searches for the basename of @var{filename}, right?  If
so, please tell that explicitly.  Also, the comments in the
implementation say that symlinks are not resolved; if that's how we
want this to work, it should also be mentioned, I think.

Thanks.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-08 22:49   ` Doug Evans
  2010-04-08 23:09     ` Sergio Durigan Junior
  2010-04-09  7:49     ` Eli Zaretskii
@ 2010-04-09 17:13     ` Tom Tromey
  2010-04-09 17:15       ` Doug Evans
  2 siblings, 1 reply; 26+ messages in thread
From: Tom Tromey @ 2010-04-09 17:13 UTC (permalink / raw)
  To: Doug Evans; +Cc: gdb-patches

>>>>> "Doug" == Doug Evans <dje@google.com> writes:

>> I often wish that gdb help strings were formatted more like --help
>> output.

Doug> Something like this?

I didn't intend for you to actually do this, but I do like it :-)

Tom


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-09 17:13     ` Tom Tromey
@ 2010-04-09 17:15       ` Doug Evans
  2010-04-09 17:19         ` Tom Tromey
  0 siblings, 1 reply; 26+ messages in thread
From: Doug Evans @ 2010-04-09 17:15 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On Fri, Apr 9, 2010 at 10:13 AM, Tom Tromey <tromey@redhat.com> wrote:
>>>>>> "Doug" == Doug Evans <dje@google.com> writes:
>
>>> I often wish that gdb help strings were formatted more like --help
>>> output.
>
> Doug> Something like this?
>
> I didn't intend for you to actually do this, but I do like it :-)

I do too fwiw.
But there's still the open question of deferring making this change
until it can be made en-masse.
I don't have time to make an en-masse change right now, but I'm happy
to check in either version of the help text for the source command
today.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-08 23:09     ` Sergio Durigan Junior
  2010-04-09  0:13       ` Doug Evans
@ 2010-04-09 17:15       ` Tom Tromey
  2010-04-09 17:48         ` Eli Zaretskii
  2010-04-10  1:29         ` Joel Brobecker
  1 sibling, 2 replies; 26+ messages in thread
From: Tom Tromey @ 2010-04-09 17:15 UTC (permalink / raw)
  To: Sergio Durigan Junior; +Cc: gdb-patches, Doug Evans

>>>>> "Sergio" == Sergio Durigan Junior <sergiodj@redhat.com> writes:

Sergio> IMHO if we are going to start formatting the help strings like
Sergio> `--help' output, then we should do it for the majority (if not
Sergio> all) of the strings.

Yeah.  I'm curious to hear what other think of this idea.

The reason I like it is that it is already fairly well understood, and
existing gdb help strings are often less clear than the typical --help
output when it comes to usage details.

Tom


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-09 17:15       ` Doug Evans
@ 2010-04-09 17:19         ` Tom Tromey
  0 siblings, 0 replies; 26+ messages in thread
From: Tom Tromey @ 2010-04-09 17:19 UTC (permalink / raw)
  To: Doug Evans; +Cc: gdb-patches

>>>>> "Doug" == Doug Evans <dje@google.com> writes:

Doug> I do too fwiw.
Doug> But there's still the open question of deferring making this change
Doug> until it can be made en-masse.
Doug> I don't have time to make an en-masse change right now, but I'm happy
Doug> to check in either version of the help text for the source command
Doug> today.

Either way is fine with me.

I would expect an en masse change to potentially even change this text;
maybe a sweep across gdb would make us refine the format somehow.

Tom


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-09  7:49     ` Eli Zaretskii
@ 2010-04-09 17:23       ` Doug Evans
  2010-04-09 17:46         ` Eli Zaretskii
  0 siblings, 1 reply; 26+ messages in thread
From: Doug Evans @ 2010-04-09 17:23 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: tromey, gdb-patches

On Fri, Apr 9, 2010 at 12:48 AM, Eli Zaretskii <eliz@gnu.org> wrote:
>> Date: Thu, 8 Apr 2010 15:49:41 -0700
>> From: Doug Evans <dje@google.com>
>> Cc: gdb-patches@sourceware.org
>>
>>
>> --- NEWS      1 Apr 2010 14:11:22 -0000       1.367
>> +++ NEWS      8 Apr 2010 22:40:59 -0000
>> @@ -3,6 +3,10 @@
>>
>>  *** Changes since GDB 7.1
>>
>> +* The source command now accepts a -s option to force searching for the
>> +  script in the source search path even if the script name specifies
>> +  a directory.
>> +
>
> This part is okay.
>
>> --- doc/gdb.texinfo   8 Apr 2010 21:08:40 -0000       1.697
>> +++ doc/gdb.texinfo   8 Apr 2010 22:41:00 -0000
>
> This part is okay, with a couple of comments:
>
>> @@ -19373,7 +19373,7 @@ using the @code{script-extension} settin
>>  @table @code
>>  @kindex source
>>  @cindex execute commands from a file
>> -@item source [@code{-v}] @var{filename}
>> +@item source [@code{-s}] [@code{-v}] @var{filename}
>
> Please remove the @code markup from the switches, it is redundant
> (because this is "@table @code" already, so every @item gets the @code
> markup by default).  Yes, the old text was also wrong.

Righto.

>> +If @code{-s} is specified, then @value{GDBN} searches for @var{filename}
>> +on the search path even if @var{filename} specifies a directory.
>
> I presume it searches for the basename of @var{filename}, right?  If
> so, please tell that explicitly.

The search includes the full path that the user provided.

How's this text?

If @code{-s} is specified, then @value{GDBN} searches for @var{filename}
on the search path even if @var{filename} specifies a directory.
The search is done by appending @var{filename} to each element of the
search path.  So, for example, if @var{filename} is @file{mylib/myscript}
and the search path contains @file{/home/user} then @value{GDBN} will
look for the script @file{/home/user/mylib/myscript}.
The search is also done if @var{filename} is an absolute path.
For example, if @var{filename} is @file{/tmp/myscript} and
the search path contains @file{/home/user} then @value{GDBN} will
look for the script @file{/home/user/tmp/myscript}.

> Also, the comments in the
> implementation say that symlinks are not resolved; if that's how we
> want this to work, it should also be mentioned, I think.

The symlink comment is an implementation detail, it doesn't affect how
source -s behaves.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-09 17:23       ` Doug Evans
@ 2010-04-09 17:46         ` Eli Zaretskii
  2010-04-09 18:12           ` Doug Evans
  0 siblings, 1 reply; 26+ messages in thread
From: Eli Zaretskii @ 2010-04-09 17:46 UTC (permalink / raw)
  To: Doug Evans; +Cc: tromey, gdb-patches

> Date: Fri, 9 Apr 2010 10:23:17 -0700
> From: Doug Evans <dje@google.com>
> Cc: tromey@redhat.com, gdb-patches@sourceware.org
> 
> If @code{-s} is specified, then @value{GDBN} searches for @var{filename}
> on the search path even if @var{filename} specifies a directory.
> The search is done by appending @var{filename} to each element of the
> search path.  So, for example, if @var{filename} is @file{mylib/myscript}
> and the search path contains @file{/home/user} then @value{GDBN} will
> look for the script @file{/home/user/mylib/myscript}.
> The search is also done if @var{filename} is an absolute path.
> For example, if @var{filename} is @file{/tmp/myscript} and
> the search path contains @file{/home/user} then @value{GDBN} will
> look for the script @file{/home/user/tmp/myscript}.

This is fine, but what if @var{filename} is @file{d:/foo/myscript} (on
Windows)?


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-09 17:15       ` Tom Tromey
@ 2010-04-09 17:48         ` Eli Zaretskii
  2010-04-10  1:29         ` Joel Brobecker
  1 sibling, 0 replies; 26+ messages in thread
From: Eli Zaretskii @ 2010-04-09 17:48 UTC (permalink / raw)
  To: Tom Tromey; +Cc: sergiodj, gdb-patches, dje

> From: Tom Tromey <tromey@redhat.com>
> Cc: gdb-patches@sourceware.org, Doug Evans <dje@google.com>
> Date: Fri, 09 Apr 2010 11:14:42 -0600
> 
> >>>>> "Sergio" == Sergio Durigan Junior <sergiodj@redhat.com> writes:
> 
> Sergio> IMHO if we are going to start formatting the help strings like
> Sergio> `--help' output, then we should do it for the majority (if not
> Sergio> all) of the strings.
> 
> Yeah.  I'm curious to hear what other think of this idea.
> 
> The reason I like it is that it is already fairly well understood, and
> existing gdb help strings are often less clear than the typical --help
> output when it comes to usage details.

The current doc strings are modeled after Emacs.  I actually like the
current format, while the suggested one will waste precious screen
space.  But if others want to switch, I won't object.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-09 17:46         ` Eli Zaretskii
@ 2010-04-09 18:12           ` Doug Evans
  2010-04-09 19:31             ` Eli Zaretskii
  0 siblings, 1 reply; 26+ messages in thread
From: Doug Evans @ 2010-04-09 18:12 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: tromey, gdb-patches

On Fri, Apr 9, 2010 at 10:46 AM, Eli Zaretskii <eliz@gnu.org> wrote:
>> Date: Fri, 9 Apr 2010 10:23:17 -0700
>> From: Doug Evans <dje@google.com>
>> Cc: tromey@redhat.com, gdb-patches@sourceware.org
>>
>> If @code{-s} is specified, then @value{GDBN} searches for @var{filename}
>> on the search path even if @var{filename} specifies a directory.
>> The search is done by appending @var{filename} to each element of the
>> search path.  So, for example, if @var{filename} is @file{mylib/myscript}
>> and the search path contains @file{/home/user} then @value{GDBN} will
>> look for the script @file{/home/user/mylib/myscript}.
>> The search is also done if @var{filename} is an absolute path.
>> For example, if @var{filename} is @file{/tmp/myscript} and
>> the search path contains @file{/home/user} then @value{GDBN} will
>> look for the script @file{/home/user/tmp/myscript}.
>
> This is fine, but what if @var{filename} is @file{d:/foo/myscript} (on
> Windows)?

source.c:openp() doesn't handle that case, it just blindly concatenates.
[presumably because it hasn't needed to]

I don't have an opinion on what *should* happen here.
Possibilities are to either not try or remove the drive spec.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-09 18:12           ` Doug Evans
@ 2010-04-09 19:31             ` Eli Zaretskii
  2010-04-09 19:49               ` Doug Evans
  2010-04-14 22:01               ` Doug Evans
  0 siblings, 2 replies; 26+ messages in thread
From: Eli Zaretskii @ 2010-04-09 19:31 UTC (permalink / raw)
  To: Doug Evans; +Cc: tromey, gdb-patches

> Date: Fri, 9 Apr 2010 11:12:27 -0700
> From: Doug Evans <dje@google.com>
> Cc: tromey@redhat.com, gdb-patches@sourceware.org
> 
> > This is fine, but what if @var{filename} is @file{d:/foo/myscript} (on
> > Windows)?
> 
> source.c:openp() doesn't handle that case, it just blindly concatenates.
> [presumably because it hasn't needed to]
> 
> I don't have an opinion on what *should* happen here.
> Possibilities are to either not try or remove the drive spec.

My vote is for removing the drive letter.  The other alternative means
that absolute file names are handled inconsistently across platforms
(I assume that not trying to look for absolute file name on Posix
platforms will not be a useful behavior).


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-09 19:31             ` Eli Zaretskii
@ 2010-04-09 19:49               ` Doug Evans
  2010-04-12 17:31                 ` Doug Evans
  2010-04-14 22:01               ` Doug Evans
  1 sibling, 1 reply; 26+ messages in thread
From: Doug Evans @ 2010-04-09 19:49 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: tromey, gdb-patches

On Fri, Apr 9, 2010 at 12:31 PM, Eli Zaretskii <eliz@gnu.org> wrote:
>> Date: Fri, 9 Apr 2010 11:12:27 -0700
>> From: Doug Evans <dje@google.com>
>> Cc: tromey@redhat.com, gdb-patches@sourceware.org
>>
>> > This is fine, but what if @var{filename} is @file{d:/foo/myscript} (on
>> > Windows)?
>>
>> source.c:openp() doesn't handle that case, it just blindly concatenates.
>> [presumably because it hasn't needed to]
>>
>> I don't have an opinion on what *should* happen here.
>> Possibilities are to either not try or remove the drive spec.
>
> My vote is for removing the drive letter.  The other alternative means
> that absolute file names are handled inconsistently across platforms
> (I assume that not trying to look for absolute file name on Posix
> platforms will not be a useful behavior).
>

I can make a case for either choice on Posix platforms: ie. whether to
search or to not search.  I don't know which is better, but openp()
already does the search for absolute path names (if
OPF_SEARCH_IN_PATH is specified), so I lean towards not introducing
something new.

Does anyone know if there's existing code to strip a drive letter if present?
[It's trivial to do - but I fear the discussion involved in getting a
patch in that does it The Right Way.]


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-09 17:15       ` Tom Tromey
  2010-04-09 17:48         ` Eli Zaretskii
@ 2010-04-10  1:29         ` Joel Brobecker
  1 sibling, 0 replies; 26+ messages in thread
From: Joel Brobecker @ 2010-04-10  1:29 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Sergio Durigan Junior, gdb-patches, Doug Evans

> The reason I like it is that it is already fairly well understood, and
> existing gdb help strings are often less clear than the typical --help
> output when it comes to usage details.

I'm partial to your suggestion, which I find easier to read/scan
quickly.  But it's only a mild preference.

-- 
Joel


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-09 19:49               ` Doug Evans
@ 2010-04-12 17:31                 ` Doug Evans
  2010-04-12 17:33                   ` Doug Evans
  2010-04-12 17:54                   ` Eli Zaretskii
  0 siblings, 2 replies; 26+ messages in thread
From: Doug Evans @ 2010-04-12 17:31 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: tromey, gdb-patches

On Fri, Apr 9, 2010 at 12:49 PM, Doug Evans <dje@google.com> wrote:
> On Fri, Apr 9, 2010 at 12:31 PM, Eli Zaretskii <eliz@gnu.org> wrote:
>>> Date: Fri, 9 Apr 2010 11:12:27 -0700
>>> From: Doug Evans <dje@google.com>
>>> Cc: tromey@redhat.com, gdb-patches@sourceware.org
>>>
>>> > This is fine, but what if @var{filename} is @file{d:/foo/myscript} (on
>>> > Windows)?
>>>
>>> source.c:openp() doesn't handle that case, it just blindly concatenates.
>>> [presumably because it hasn't needed to]
>>>
>>> I don't have an opinion on what *should* happen here.
>>> Possibilities are to either not try or remove the drive spec.
>>
>> My vote is for removing the drive letter.  The other alternative means
>> that absolute file names are handled inconsistently across platforms
>> (I assume that not trying to look for absolute file name on Posix
>> platforms will not be a useful behavior).

[note: filenames.h uses the term "semi-absolute" for d:foo]
I don't have an opinion on whether to treat d:/foo vs d:foo
differently in this context.

> I can make a case for either choice on Posix platforms: ie. whether to
> search or to not search.  I don't know which is better, but openp()
> already does the search for absolute path names (if
> OPF_SEARCH_IN_PATH is specified), so I lean towards not introducing
> something new.
>
> Does anyone know if there's existing code to strip a drive letter if present?
> [It's trivial to do - but I fear the discussion involved in getting a
> patch in that does it The Right Way.]
>

I'm all for stripping the drive letter if it's agreeable to everyone else.
I will send this to the binutils list for approval tomorrow, unless
someone has a better suggestion.

openp would then check HAVE_DRIVE_SPEC (path) and if true call
STRIP_DRIVE_SPEC (path)
before concatenating the search path with the user-provided path.

I put these macros in filenames.h because that's where
IS_ABSOLUTE_PATH and IS_DIR_SEPARATOR live.

2010-04-12  Doug Evans  <dje@google.com>

        * filenames.h (HAVE_DRIVE_SPEC, STRIP_DRIVE_SPEC): New macros.

Index: filenames.h
===================================================================
RCS file: /cvs/src/src/include/filenames.h,v
retrieving revision 1.5
diff -u -p -r1.5 filenames.h
--- filenames.h 21 Mar 2008 23:40:18 -0000      1.5
+++ filenames.h 12 Apr 2010 17:19:32 -0000
@@ -5,7 +5,7 @@
    use forward- and back-slash in path names interchangeably, and
    some of them have case-insensitive file names.

-   Copyright 2000, 2001, 2007 Free Software Foundation, Inc.
+   Copyright 2000, 2001, 2007, 2010 Free Software Foundation, Inc.

 This file is part of BFD, the Binary File Descriptor library.

@@ -37,17 +37,27 @@ extern "C" {
 #endif

 #define IS_DIR_SEPARATOR(c)    ((c) == '/' || (c) == '\\')
+
+#define HAVE_DRIVE_SPEC(f)     (((f)[0]) && ((f)[1] == ':'))
+
+/* Remove the drive spec from F, assuming HAVE_DRIVE_SPEC (f).
+   The result is a pointer to the remainder of F.  */
+#define STRIP_DRIVE_SPEC(f)    ((f) + 2)
+
 /* Note that IS_ABSOLUTE_PATH accepts d:foo as well, although it is
    only semi-absolute.  This is because the users of IS_ABSOLUTE_PATH
    want to know whether to prepend the current working directory to
    a file name, which should not be done with a name like d:foo.  */
-#define IS_ABSOLUTE_PATH(f)    (IS_DIR_SEPARATOR((f)[0]) || (((f)[0])
&& ((f)[1] == ':')))
+#define IS_ABSOLUTE_PATH(f)    (IS_DIR_SEPARATOR((f)[0]) || HAVE_DRIVE_SPEC(f))

 #else  /* not DOSish */

 #define IS_DIR_SEPARATOR(c)    ((c) == '/')
 #define IS_ABSOLUTE_PATH(f)    (IS_DIR_SEPARATOR((f)[0]))

+#define HAVE_DRIVE_SPEC(f)     (0)
+#define STRIP_DRIVE_SPEC(f)    (f)
+
 #endif /* not DOSish */

 extern int filename_cmp (const char *s1, const char *s2);


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-12 17:31                 ` Doug Evans
@ 2010-04-12 17:33                   ` Doug Evans
  2010-04-12 17:47                     ` Tom Tromey
  2010-04-12 17:54                   ` Eli Zaretskii
  1 sibling, 1 reply; 26+ messages in thread
From: Doug Evans @ 2010-04-12 17:33 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: tromey, gdb-patches

On Mon, Apr 12, 2010 at 10:31 AM, Doug Evans <dje@google.com> wrote:
> I'm all for stripping the drive letter if it's agreeable to everyone else.
> I will send this to the binutils list for approval tomorrow, unless
> someone has a better suggestion.
>
> openp would then check HAVE_DRIVE_SPEC (path) and if true call
> STRIP_DRIVE_SPEC (path)
> before concatenating the search path with the user-provided path.
>
> I put these macros in filenames.h because that's where
> IS_ABSOLUTE_PATH and IS_DIR_SEPARATOR live.
>
> 2010-04-12  Doug Evans  <dje@google.com>
>
>        * filenames.h (HAVE_DRIVE_SPEC, STRIP_DRIVE_SPEC): New macros.

... or is this another file that gets migrated over from the gcc tree,
and thus needs approval from the gcc list ...


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-12 17:33                   ` Doug Evans
@ 2010-04-12 17:47                     ` Tom Tromey
  0 siblings, 0 replies; 26+ messages in thread
From: Tom Tromey @ 2010-04-12 17:47 UTC (permalink / raw)
  To: Doug Evans; +Cc: Eli Zaretskii, gdb-patches

>>>>> "Doug" == Doug Evans <dje@google.com> writes:

>> 2010-04-12  Doug Evans  <dje@google.com>
>>        * filenames.h (HAVE_DRIVE_SPEC, STRIP_DRIVE_SPEC): New macros.

Doug> ... or is this another file that gets migrated over from the gcc tree,
Doug> and thus needs approval from the gcc list ...

Yeah, I think anything in libiberty/ or include/ is handled that way.
Anyway, certainly anything appearing in both trees, like this.

Tom


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-12 17:31                 ` Doug Evans
  2010-04-12 17:33                   ` Doug Evans
@ 2010-04-12 17:54                   ` Eli Zaretskii
  1 sibling, 0 replies; 26+ messages in thread
From: Eli Zaretskii @ 2010-04-12 17:54 UTC (permalink / raw)
  To: Doug Evans; +Cc: tromey, gdb-patches

> Date: Mon, 12 Apr 2010 10:31:34 -0700
> From: Doug Evans <dje@google.com>
> Cc: tromey@redhat.com, gdb-patches@sourceware.org
> 
> [note: filenames.h uses the term "semi-absolute" for d:foo]
> I don't have an opinion on whether to treat d:/foo vs d:foo
> differently in this context.

I think they should be treated the same in this context.

Thanks.  The patch to filenames.h looks OK to me.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-09 19:31             ` Eli Zaretskii
  2010-04-09 19:49               ` Doug Evans
@ 2010-04-14 22:01               ` Doug Evans
  2010-04-15  3:10                 ` Eli Zaretskii
  1 sibling, 1 reply; 26+ messages in thread
From: Doug Evans @ 2010-04-14 22:01 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: tromey, gdb-patches

[-- Attachment #1: Type: text/plain, Size: 3734 bytes --]

On Fri, Apr 9, 2010 at 12:31 PM, Eli Zaretskii <eliz@gnu.org> wrote:
>> Date: Fri, 9 Apr 2010 11:12:27 -0700
>> From: Doug Evans <dje@google.com>
>> Cc: tromey@redhat.com, gdb-patches@sourceware.org
>>
>> > This is fine, but what if @var{filename} is @file{d:/foo/myscript} (on
>> > Windows)?
>>
>> source.c:openp() doesn't handle that case, it just blindly concatenates.
>> [presumably because it hasn't needed to]
>>
>> I don't have an opinion on what *should* happen here.
>> Possibilities are to either not try or remove the drive spec.
>
> My vote is for removing the drive letter.  The other alternative means
> that absolute file names are handled inconsistently across platforms
> (I assume that not trying to look for absolute file name on Posix
> platforms will not be a useful behavior).

Hi.  Here is the patch I will check in pending approval of the doc
changes to mention dos drive spec handling.

2010-04-08  Doug Evans  <dje@google.com>

	Add -s option to source command.
	* NEWS: Document new option.
	* cli/cli-cmds.c (find_and_open_script): Add function comment.
	Delete from_tty and cleanupp args.  Split filep arg into file and
	full_pathp.  New arg search_path.
	(source_script_from_stream): New function.
	(source_script_with_search): New function.
	(source_script): Rewrite.
	(source_command): Parse "-s" option.
	(init_cli_cmds): Add "-s" docs to source command help, and reformat.
	* python/python.c (source_python_script): Make file arg a const char *.
	Don't call fclose, leave for caller.
	* python/python.h (source_python_script): Update.

	testsuite/
	* gdb.base/source-test.gdb: New file.
	* gdb.base/source.exp: Add tests for "source -s".

	doc/
	* gdb.texinfo (Command Files): Add docs for new "source -s" option.

Index: doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.704
diff -u -p -r1.704 gdb.texinfo
--- doc/gdb.texinfo	14 Apr 2010 12:02:44 -0000	1.704
+++ doc/gdb.texinfo	14 Apr 2010 21:54:37 -0000
@@ -19373,7 +19373,7 @@ using the @code{script-extension} settin
 @table @code
 @kindex source
 @cindex execute commands from a file
-@item source [@code{-v}] @var{filename}
+@item source [-s] [-v] @var{filename}
 Execute the command file @var{filename}.
 @end table

@@ -19390,6 +19390,21 @@ directory, then @value{GDBN} also looks
 except that @file{$cdir} is not searched because the compilation directory
 is not relevant to scripts.

+If @code{-s} is specified, then @value{GDBN} searches for @var{filename}
+on the search path even if @var{filename} specifies a directory.
+The search is done by appending @var{filename} to each element of the
+search path.  So, for example, if @var{filename} is @file{mylib/myscript}
+and the search path contains @file{/home/user} then @value{GDBN} will
+look for the script @file{/home/user/mylib/myscript}.
+The search is also done if @var{filename} is an absolute path.
+For example, if @var{filename} is @file{/tmp/myscript} and
+the search path contains @file{/home/user} then @value{GDBN} will
+look for the script @file{/home/user/tmp/myscript}.
+For DOS-like systems, if @var{filename} contains a drive specification,
+it is stripped before concatenation.  For example, if @var{filename} is
+@file{d:myscript} and the search path contains @file{c:/tmp} then @value{GDBN}
+will look for the script @file{c:/tmp/myscript}.
+
 If @code{-v}, for verbose mode, is given then @value{GDBN} displays
 each command as it is executed.  The option must be given before
 @var{filename}, and is interpreted as part of the filename anywhere else.


Full patch attached.

[-- Attachment #2: gdb-100414-source-s-3.patch.txt --]
[-- Type: text/plain, Size: 13307 bytes --]

2010-04-08  Doug Evans  <dje@google.com>

	Add -s option to source command.
	* NEWS: Document new option.
	* cli/cli-cmds.c (find_and_open_script): Add function comment.
	Delete from_tty and cleanupp args.  Split filep arg into file and
	full_pathp.  New arg search_path.
	(source_script_from_stream): New function.
	(source_script_with_search): New function.
	(source_script): Rewrite.
	(source_command): Parse "-s" option.
	(init_cli_cmds): Add "-s" docs to source command help, and reformat.
	* python/python.c (source_python_script): Make file arg a const char *.
	Don't call fclose, leave for caller.
	* python/python.h (source_python_script): Update.

	testsuite/
	* gdb.base/source-test.gdb: New file.
	* gdb.base/source.exp: Add tests for "source -s".

	doc/
	* gdb.texinfo (Command Files): Add docs for new "source -s" option.

Index: NEWS
===================================================================
RCS file: /cvs/src/src/gdb/NEWS,v
retrieving revision 1.369
diff -u -p -r1.369 NEWS
--- NEWS	9 Apr 2010 15:26:54 -0000	1.369
+++ NEWS	14 Apr 2010 21:54:37 -0000
@@ -3,6 +3,10 @@
 
 *** Changes since GDB 7.1
 
+* The source command now accepts a -s option to force searching for the
+  script in the source search path even if the script name specifies
+  a directory.
+
 * New features in the GDB remote stub, GDBserver
 
   - GDBserver now support tracepoints.  The feature is currently
Index: cli/cli-cmds.c
===================================================================
RCS file: /cvs/src/src/gdb/cli/cli-cmds.c,v
retrieving revision 1.100
diff -u -p -r1.100 cli-cmds.c
--- cli/cli-cmds.c	9 Apr 2010 03:00:58 -0000	1.100
+++ cli/cli-cmds.c	14 Apr 2010 21:54:37 -0000
@@ -470,62 +470,59 @@ Script filename extension recognition is
 		    value);
 }
 
+/* Try to open SCRIPT_FILE.
+   If successful, the full path name is stored in *FULL_PATHP,
+   the stream is stored in *STREAMP, and return 1.
+   The caller is responsible for freeing *FULL_PATHP.
+   If not successful, return 0; errno is set for the last file
+   we tried to open.
+
+   If SEARCH_PATH is non-zero, and the file isn't found in cwd,
+   search for it in the source search path.
+
+   NOTE: This calls openp which uses xfullpath to compute the full path
+   instead of gdb_realpath.  Symbolic links are not resolved.  */
+
 static int
-find_and_open_script (int from_tty, char **filep, FILE **streamp,
-		      struct cleanup **cleanupp)
+find_and_open_script (const char *script_file, int search_path,
+		      FILE **streamp, char **full_pathp)
 {
-  char *file = *filep;
-  char *full_pathname = NULL;
+  char *file;
   int fd;
   struct cleanup *old_cleanups;
+  int search_flags = OPF_TRY_CWD_FIRST;
 
-  file = tilde_expand (file);
+  file = tilde_expand (script_file);
   old_cleanups = make_cleanup (xfree, file);
 
-  /* Search for and open 'file' on the search path used for source
-     files.  Put the full location in 'full_pathname'.  */
-  fd = openp (source_path, OPF_TRY_CWD_FIRST,
-	      file, O_RDONLY, &full_pathname);
-  make_cleanup (xfree, full_pathname);
+  if (search_path)
+    search_flags |= OPF_SEARCH_IN_PATH;
 
-  /* Use the full path name, if it is found.  */
-  if (full_pathname != NULL && fd != -1)
-    {
-      file = full_pathname;
-    }
+  /* Search for and open 'file' on the search path used for source
+     files.  Put the full location in *FULL_PATHP.  */
+  fd = openp (source_path, search_flags,
+	      file, O_RDONLY, full_pathp);
 
   if (fd == -1)
     {
-      if (from_tty)
-	perror_with_name (file);
-      else
-	{
-	  do_cleanups (old_cleanups);
-	  return 0;
-	}
+      int save_errno = errno;
+      do_cleanups (old_cleanups);
+      errno = save_errno;
+      return 0;
     }
 
-  *streamp = fdopen (fd, FOPEN_RT);
-  *filep = file;
-  *cleanupp = old_cleanups;
+  do_cleanups (old_cleanups);
 
+  *streamp = fdopen (fd, FOPEN_RT);
   return 1;
 }
 
-void
-source_script (char *file, int from_tty)
-{
-  FILE *stream;
-  struct cleanup *old_cleanups;
-
-  if (file == NULL || *file == 0)
-    {
-      error (_("source command requires file name of file to source."));
-    }
-
-  if (!find_and_open_script (from_tty, &file, &stream, &old_cleanups))
-    return;
+/* Load script FILE, which has already been opened as STREAM.
+   STREAM is closed before we return.  */
 
+static void
+source_script_from_stream (FILE *stream, const char *file)
+{
   if (script_ext_mode != script_ext_off
       && strlen (file) > 3 && !strcmp (&file[strlen (file) - 3], ".py"))
     {
@@ -541,22 +538,64 @@ source_script (char *file, int from_tty)
 	  if (script_ext_mode == script_ext_soft
 	      && e.reason == RETURN_ERROR && e.error == UNSUPPORTED_ERROR)
 	    {
-	      if (!find_and_open_script (from_tty, &file, &stream, &old_cleanups))
-		return;
-
-	      script_from_file (stream, file);
+	      fseek (stream, 0, SEEK_SET);
+	      script_from_file (stream, (char*) file);
 	    }
 	  else
-	    /* Nope, just punt.  */
-	    throw_exception (e);
+	    {
+	      /* Nope, just punt.  */
+	      fclose (stream);
+	      throw_exception (e);
+	    }
 	}
+      else
+	fclose (stream);
     }
   else
     script_from_file (stream, file);
+}
 
+/* Worker to perform the "source" command.
+   Load script FILE.
+   If SEARCH_PATH is non-zero, and the file isn't found in cwd,
+   search for it in the source search path.  */
+
+static void
+source_script_with_search (const char *file, int from_tty, int search_path)
+{
+  FILE *stream;
+  char *full_path;
+  struct cleanup *old_cleanups;
+
+  if (file == NULL || *file == 0)
+    error (_("source command requires file name of file to source."));
+
+  if (!find_and_open_script (file, search_path, &stream, &full_path))
+    {
+      /* The script wasn't found, or was otherwise inaccessible.
+	 If the source command was invoked interactively, throw an error.
+	 Otherwise (e.g. if it was invoked by a script), silently ignore
+	 the error.  */
+      if (from_tty)
+	perror_with_name (file);
+      else
+	return;
+    }
+
+  old_cleanups = make_cleanup (xfree, full_path);
+  source_script_from_stream (stream, file);
   do_cleanups (old_cleanups);
 }
 
+/* Wrapper around source_script_with_search to export it to main.c
+   for use in loading .gdbinit scripts.  */
+
+void
+source_script (char *file, int from_tty)
+{
+  source_script_with_search (file, from_tty, 0);
+}
+
 /* Return the source_verbose global variable to its previous state
    on exit from the source command, by whatever means.  */
 static void
@@ -572,33 +611,52 @@ source_command (char *args, int from_tty
   struct cleanup *old_cleanups;
   char *file = args;
   int *old_source_verbose = xmalloc (sizeof(int));
+  int search_path = 0;
 
   *old_source_verbose = source_verbose;
   old_cleanups = make_cleanup (source_verbose_cleanup, old_source_verbose);
 
   /* -v causes the source command to run in verbose mode.
+     -s causes the file to be searched in the source search path,
+     even if the file name contains a '/'.
      We still have to be able to handle filenames with spaces in a
      backward compatible way, so buildargv is not appropriate.  */
 
   if (args)
     {
-      /* Make sure leading white space does not break the comparisons.  */
-      while (isspace(args[0]))
-	args++;
-
-      /* Is -v the first thing in the string?  */
-      if (args[0] == '-' && args[1] == 'v' && isspace (args[2]))
+      while (args[0] != '\0')
 	{
-	  source_verbose = 1;
+	  /* Make sure leading white space does not break the comparisons.  */
+	  while (isspace(args[0]))
+	    args++;
+
+	  if (args[0] != '-')
+	    break;
+
+	  if (args[1] == 'v' && isspace (args[2]))
+	    {
+	      source_verbose = 1;
+
+	      /* Skip passed -v.  */
+	      args = &args[3];
+	    }
+	  else if (args[1] == 's' && isspace (args[2]))
+	    {
+	      search_path = 1;
 
-	  /* Trim -v and whitespace from the filename.  */
-	  file = &args[3];
-	  while (isspace (file[0]))
-	    file++;
+	      /* Skip passed -s.  */
+	      args = &args[3];
+	    }
+	  else
+	    break;
 	}
+
+      while (isspace (args[0]))
+	args++;
+      file = args;
     }
 
-  source_script (file, from_tty);
+  source_script_with_search (file, from_tty, search_path);
 
   do_cleanups (old_cleanups);
 }
@@ -1379,8 +1437,12 @@ Commands defined in this way may have up
 
   source_help_text = xstrprintf (_("\
 Read commands from a file named FILE.\n\
-Optional -v switch (before the filename) causes each command in\n\
-FILE to be echoed as it is executed.\n\
+\n\
+Usage: source [-s] [-v] FILE\n\
+-s: search for the script in the source search path,\n\
+    even if FILE contains directories.\n\
+-v: each command in FILE is echoed as it is executed.\n\
+\n\
 Note that the file \"%s\" is read automatically in this way\n\
 when GDB is started."), gdbinit);
   c = add_cmd ("source", class_support, source_command,
Index: doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.704
diff -u -p -r1.704 gdb.texinfo
--- doc/gdb.texinfo	14 Apr 2010 12:02:44 -0000	1.704
+++ doc/gdb.texinfo	14 Apr 2010 21:54:37 -0000
@@ -19373,7 +19373,7 @@ using the @code{script-extension} settin
 @table @code
 @kindex source
 @cindex execute commands from a file
-@item source [@code{-v}] @var{filename}
+@item source [-s] [-v] @var{filename}
 Execute the command file @var{filename}.
 @end table
 
@@ -19390,6 +19390,21 @@ directory, then @value{GDBN} also looks 
 except that @file{$cdir} is not searched because the compilation directory
 is not relevant to scripts.
 
+If @code{-s} is specified, then @value{GDBN} searches for @var{filename}
+on the search path even if @var{filename} specifies a directory.
+The search is done by appending @var{filename} to each element of the
+search path.  So, for example, if @var{filename} is @file{mylib/myscript}
+and the search path contains @file{/home/user} then @value{GDBN} will
+look for the script @file{/home/user/mylib/myscript}.
+The search is also done if @var{filename} is an absolute path.
+For example, if @var{filename} is @file{/tmp/myscript} and
+the search path contains @file{/home/user} then @value{GDBN} will
+look for the script @file{/home/user/tmp/myscript}.
+For DOS-like systems, if @var{filename} contains a drive specification,
+it is stripped before concatenation.  For example, if @var{filename} is
+@file{d:myscript} and the search path contains @file{c:/tmp} then @value{GDBN}
+will look for the script @file{c:/tmp/myscript}.
+
 If @code{-v}, for verbose mode, is given then @value{GDBN} displays
 each command as it is executed.  The option must be given before
 @var{filename}, and is interpreted as part of the filename anywhere else.
Index: python/python.c
===================================================================
RCS file: /cvs/src/src/gdb/python/python.c,v
retrieving revision 1.30
diff -u -p -r1.30 python.c
--- python/python.c	14 Apr 2010 13:18:55 -0000	1.30
+++ python/python.c	14 Apr 2010 21:54:37 -0000
@@ -364,10 +364,11 @@ gdbpy_parse_and_eval (PyObject *self, Py
 }
 
 /* Read a file as Python code.  STREAM is the input file; FILE is the
-   name of the file.  */
+   name of the file.
+   STREAM is not closed, that is the caller's responsibility.  */
 
 void
-source_python_script (FILE *stream, char *file)
+source_python_script (FILE *stream, const char *file)
 {
   struct cleanup *cleanup;
 
@@ -375,7 +376,6 @@ source_python_script (FILE *stream, char
 
   PyRun_SimpleFile (stream, file);
 
-  fclose (stream);
   do_cleanups (cleanup);
 }
 
@@ -562,9 +562,8 @@ eval_python_from_control_command (struct
 }
 
 void
-source_python_script (FILE *stream, char *file)
+source_python_script (FILE *stream, const char *file)
 {
-  fclose (stream);
   throw_error (UNSUPPORTED_ERROR,
 	       _("Python scripting is not supported in this copy of GDB."));
 }
Index: python/python.h
===================================================================
RCS file: /cvs/src/src/gdb/python/python.h,v
retrieving revision 1.7
diff -u -p -r1.7 python.h
--- python/python.h	18 Jan 2010 06:25:22 -0000	1.7
+++ python/python.h	14 Apr 2010 21:54:37 -0000
@@ -24,7 +24,7 @@
 
 void eval_python_from_control_command (struct command_line *);
 
-void source_python_script (FILE *stream, char *file);
+void source_python_script (FILE *stream, const char *file);
 
 int apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
 			      int embedded_offset, CORE_ADDR address,
Index: testsuite/gdb.base/source.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.base/source.exp,v
retrieving revision 1.11
diff -u -p -r1.11 source.exp
--- testsuite/gdb.base/source.exp	7 Apr 2010 03:47:22 -0000	1.11
+++ testsuite/gdb.base/source.exp	14 Apr 2010 21:54:37 -0000
@@ -51,4 +51,17 @@ gdb_test_multiple "source ${srcdir}/${su
     }
 }
 
+gdb_test "dir ${srcdir}/${subdir}" ""
+gdb_test "source -s ./source-test.gdb" \
+    "test source options" \
+    "source -s"
+
+# Test -v and -s in either order.
+gdb_test "source -s -v ./source-test.gdb" \
+    "echo test source options.*" \
+    "source -s -v"
+gdb_test "source -v -s ./source-test.gdb" \
+    "echo test source options.*" \
+    "source -v -s"
+
 gdb_exit

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-14 22:01               ` Doug Evans
@ 2010-04-15  3:10                 ` Eli Zaretskii
  0 siblings, 0 replies; 26+ messages in thread
From: Eli Zaretskii @ 2010-04-15  3:10 UTC (permalink / raw)
  To: Doug Evans; +Cc: tromey, gdb-patches

> Date: Wed, 14 Apr 2010 15:00:45 -0700
> From: Doug Evans <dje@google.com>
> Cc: tromey@redhat.com, gdb-patches@sourceware.org
> 
> Hi.  Here is the patch I will check in pending approval of the doc
> changes to mention dos drive spec handling.

The doc part is okay.  Thanks.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-06 21:58 [RFA] Add -s option to source command Doug Evans
  2010-04-06 23:17 ` Doug Evans
  2010-04-07 20:14 ` Tom Tromey
@ 2010-04-17 15:20 ` H.J. Lu
  2010-04-20  5:38   ` Doug Evans
  2 siblings, 1 reply; 26+ messages in thread
From: H.J. Lu @ 2010-04-17 15:20 UTC (permalink / raw)
  To: Doug Evans; +Cc: gdb-patches

On Tue, Apr 6, 2010 at 2:57 PM, Doug Evans <dje@google.com> wrote:
> Hi.
>
> In a big source tree with multiple disparate components it's useful to be
> able to load scripts without having to type full path names.
>
> This patch adds a -s option to the source command to make it search
> for the script in the source search path even if the script name specifies
> a directory.
> For example, adding the top level source directory to the source search path
> lets one load scripts from any component by only typing the path from
> the top directory.
>
> I thought of simply extending the source command to do the search anyway
> but the current code explicitly doesn't do that to make "./foo" only mean
> "foo" in the current directory.  Granted, the comment (see openp) is
> referring to executables, but it's not clear to me it shouldn't also
> apply to scripts.  Plus, "source /absolute/path" should probably not
> scan the search path by default.
> So I went with adding an option.
>
> This patch also does a minor reorg of find_and_open_script so that
> it's useful in a subsequent patch I'll be submitting.
> I think the reorg is useful in itself, e.g. removing the from_tty
> arg to find_and_open_script, so I've including it here.
>
> Included are doc and testcase additions.
>
> Ok to check in?
>
> 2010-04-06  Doug Evans  <dje@google.com>
>
>        Add -s option to source command.
>        * cli/cli-cmds.c (find_and_open_script): Add function comment.
>        Delete from_tty and cleanupp args.  Split filep arg into file and
>        full_pathp.  New arg search_path.
>        (source_script_from_stream): New function.
>        (source_script_with_search): New function.
>        (source_script): Rewrite.
>        (source_command): Parse "-s" option.
>        (init_cli_cmds): Add "-s" docs to source command help.
>        * python/python.c (source_python_script): Make file arg a const char *.
>        Don't call fclose, leave for caller.
>        * python/python.h (source_python_script): Update.
>
>        testsuite/
>        * gdb.base/source-test.gdb: New file.
>        * gdb.base/source.exp: Add tests for "source -v" and "source -s".
>
>        doc/
>        * gdb.texinfo (Command Files): Add docs for new "source -s" option.
>

This caused:

help source^M
Read commands from a file named FILE.^M
^M
Usage: source [-s] [-v] FILE^M
-s: search for the script in the source search path,^M
    even if FILE contains directories.^M
-v: each command in FILE is echoed as it is executed.^M
^M
Note that the file ".gdbinit" is read automatically in this way^M
when GDB is started.^M
(gdb) FAIL: gdb.base/help.exp: help source



-- 
H.J.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [RFA] Add -s option to source command.
  2010-04-17 15:20 ` H.J. Lu
@ 2010-04-20  5:38   ` Doug Evans
  0 siblings, 0 replies; 26+ messages in thread
From: Doug Evans @ 2010-04-20  5:38 UTC (permalink / raw)
  To: H.J. Lu; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 1636 bytes --]

On Sat, Apr 17, 2010 at 8:19 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
>> 2010-04-06  Doug Evans  <dje@google.com>
>>
>>        Add -s option to source command.
>>        * cli/cli-cmds.c (find_and_open_script): Add function comment.
>>        Delete from_tty and cleanupp args.  Split filep arg into file and
>>        full_pathp.  New arg search_path.
>>        (source_script_from_stream): New function.
>>        (source_script_with_search): New function.
>>        (source_script): Rewrite.
>>        (source_command): Parse "-s" option.
>>        (init_cli_cmds): Add "-s" docs to source command help.
>>        * python/python.c (source_python_script): Make file arg a const char *.
>>        Don't call fclose, leave for caller.
>>        * python/python.h (source_python_script): Update.
>>
>>        testsuite/
>>        * gdb.base/source-test.gdb: New file.
>>        * gdb.base/source.exp: Add tests for "source -v" and "source -s".
>>
>>        doc/
>>        * gdb.texinfo (Command Files): Add docs for new "source -s" option.
>>
>
> This caused:
>
> help source^M
> Read commands from a file named FILE.^M
> ^M
> Usage: source [-s] [-v] FILE^M
> -s: search for the script in the source search path,^M
>    even if FILE contains directories.^M
> -v: each command in FILE is echoed as it is executed.^M
> ^M
> Note that the file ".gdbinit" is read automatically in this way^M
> when GDB is started.^M
> (gdb) FAIL: gdb.base/help.exp: help source

Thanks.  Fix checked in.

010-04-19  Doug Evans  <dje@google.com>

        * gdb.base/help.exp (help source): Update expected output.

[-- Attachment #2: help-1.patch.txt --]
[-- Type: text/plain, Size: 1493 bytes --]

2010-04-19  Doug Evans  <dje@google.com>

	* gdb.base/help.exp (help source): Update expected output.

Index: gdb.base/help.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.base/help.exp,v
retrieving revision 1.38
diff -u -p -r1.38 help.exp
--- gdb.base/help.exp	4 Apr 2010 23:47:16 -0000	1.38
+++ gdb.base/help.exp	20 Apr 2010 05:35:20 -0000
@@ -606,7 +606,9 @@ gdb_test "help stepi" "Step one instruct
 gdb_test "help signal" "Continue program giving it signal.*" "help signal"
 # test help source
 # vxgdb reads .vxgdbinit
-gdb_test "help source" "Read commands from a file named FILE\.\[\r\n\]+Optional -v switch \\(before the filename\\) causes each command in\[\r\n\]+FILE to be echoed as it is executed\.\[\r\n\]+Note that the file \"\[^\"\]*\" is read automatically in this way\[\r\n\]+when GDB is started\." "help source"
+# ".-s. .-v." is written that way to avoid the complications of trying
+# to get "[-s] [-v]" through expect and tcl.
+gdb_test "help source" "Read commands from a file named FILE\.\[\r\n\]+Usage: source .-s. .-v. FILE\[\r\n\]+-s: search for the script in the source search path,\[\r\n\]+\[ \]+even if FILE contains directories\.\[\r\n\]+-v: each command in FILE is echoed as it is executed\.\[\r\n\]+Note that the file \"\[^\"\]*\" is read automatically in this way\[\r\n\]+when GDB is started\." "help source"
 # test help stack
 test_class_help "stack" {
     "Examining the stack\..*\[\r\n\]+"

^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2010-04-20  5:38 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-06 21:58 [RFA] Add -s option to source command Doug Evans
2010-04-06 23:17 ` Doug Evans
2010-04-07 20:14 ` Tom Tromey
2010-04-08 22:49   ` Doug Evans
2010-04-08 23:09     ` Sergio Durigan Junior
2010-04-09  0:13       ` Doug Evans
2010-04-09 17:15       ` Tom Tromey
2010-04-09 17:48         ` Eli Zaretskii
2010-04-10  1:29         ` Joel Brobecker
2010-04-09  7:49     ` Eli Zaretskii
2010-04-09 17:23       ` Doug Evans
2010-04-09 17:46         ` Eli Zaretskii
2010-04-09 18:12           ` Doug Evans
2010-04-09 19:31             ` Eli Zaretskii
2010-04-09 19:49               ` Doug Evans
2010-04-12 17:31                 ` Doug Evans
2010-04-12 17:33                   ` Doug Evans
2010-04-12 17:47                     ` Tom Tromey
2010-04-12 17:54                   ` Eli Zaretskii
2010-04-14 22:01               ` Doug Evans
2010-04-15  3:10                 ` Eli Zaretskii
2010-04-09 17:13     ` Tom Tromey
2010-04-09 17:15       ` Doug Evans
2010-04-09 17:19         ` Tom Tromey
2010-04-17 15:20 ` H.J. Lu
2010-04-20  5:38   ` Doug Evans

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox