This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Start of `>' and `>>' commands


For fun I wrote a simple version of the `>' and `>>' redirection
commands.  The current patch is appended.  It turns out to be pretty
easy to do.

I didn't implement the full `> file command' syntax, because that
would require thinking up a quoting syntax for the file name.  I also
didn't implement a `|' command (but that would be pretty easy).

One other idea would be to have `>' print to both the terminal and
some file.  I think that wouldn't be very hard, but it would be
time-consuming.  As it is the current approach is good enough for when
you want to redirect voluminous output to some file; I find when I
typically want to do that I don't really care if I see the output
right away or not (I can always run tail -f in another window).

I doubt I'll be hacking on this patch any more in the near future, but
I know there's been some interest in having something like this, so I
thought I'd throw it out there in case anybody is interested in
picking it up.

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>

	* cli/cli-decode.c (lookup_cmd): Allow `>' in command name.
	(lookup_cmd_1): Likewise.
	* top.c (redirect_output): New function.
	(redirect_output): Likewise.
	(handle_redirections): Likewise.
	(pop_output_files): Likewise.
	(init_main): Create ">" and ">>" commands.

Index: top.c
===================================================================
RCS file: /cvs/src/src/gdb/top.c,v
retrieving revision 1.45
diff -u -r1.45 top.c
--- top.c 2001/09/07 21:33:08 1.45
+++ top.c 2001/10/06 00:28:13
@@ -1668,6 +1668,88 @@
 				   necessarily reading from stdin.  */
 }
 
+/* Functions and variables for gdb output redirection.  */
+
+/* These hold the pushed copies of the gdb output files.
+   If NULL then nothing has yet been pushed.  */
+static struct ui_file *saved_stdout;
+static struct ui_file *saved_stderr;
+static struct ui_file *saved_stdlog;
+static struct ui_file *saved_stdtarg;
+
+/* If we've pushed output files, close them and pop them.  */
+static void
+pop_output_files ()
+{
+  if (saved_stdout != NULL)
+    {
+      /* Only delete one of the files -- they are all set to the same
+	 value.  */
+      ui_file_delete (gdb_stdout);
+      gdb_stdout = saved_stdout;
+      gdb_stderr = saved_stderr;
+      gdb_stdlog = saved_stdlog;
+      gdb_stdtarg = saved_stdtarg;
+      saved_stdout = NULL;
+      saved_stderr = NULL;
+      saved_stdlog = NULL;
+      saved_stdtarg = NULL;
+    }
+}
+
+/* This is a helper for the `>' and `>>' redirection commands.  */
+static void
+handle_redirections (char *command, char *filename, char *mode)
+{
+  struct ui_file *output;
+
+  pop_output_files ();
+
+  if (! filename)
+    return;
+
+  /* Skip whitespace.  */
+  while (*filename && isspace (*filename))
+    ++filename;
+
+  if (! *filename)
+    return;
+
+  /* It would be nice if the syntax was:
+     > FILE [COMMAND]
+     .. if COMMAND is specified then it (and only it) is run with
+     redirections.  This means inventing a quoting syntax for the file
+     name though.  */
+  output = gdb_fopen (filename, mode);
+  if (output == NULL)
+    perror_with_name (command);
+
+  saved_stdout = gdb_stdout;
+  saved_stderr = gdb_stderr;
+  saved_stdlog = gdb_stdlog;
+  saved_stdtarg = gdb_stdtarg;
+  gdb_stdout = output;
+  gdb_stderr = output;
+  gdb_stdlog = output;
+  gdb_stdtarg = output;
+}
+
+/* Redirect output by overwriting a file.  */
+/* ARGSUSED */
+static void
+redirect_output (char *args, int from_tty)
+{
+  handle_redirections (">", args, "w");
+}
+
+/* Redirect output by appending to a file.  */
+/* ARGSUSED */
+static void
+append_output (char *args, int from_tty)
+{
+  handle_redirections (">>", args, "a");
+}
+
 /* Functions to manipulate command line editing control variables.  */
 
 /* Number of commands to print in each call to show_commands.  */
@@ -1997,6 +2079,13 @@
 Use \"on\" to enable the notification, and \"off\" to disable it.", &setlist),
 	 &showlist);
     }
+
+  add_com (">", no_class, redirect_output,
+	   "Redirect further gdb output to a file.\n\
+If no filename is given, any previous redirection is stopped.");
+  add_com (">>", no_class, append_output,
+	   "Append further gdb output to a file.\n\
+If no filename is given, any previous redirection is stopped.");
 }
 
 void
Index: cli/cli-decode.c
===================================================================
RCS file: /cvs/src/src/gdb/cli/cli-decode.c,v
retrieving revision 1.9
diff -u -r1.9 cli-decode.c
--- cli/cli-decode.c 2001/09/30 16:16:16 1.9
+++ cli/cli-decode.c 2001/10/06 00:28:14
@@ -780,7 +780,7 @@
      so that "set args_foo()" doesn't get interpreted as
      "set args _foo()".  */
   for (p = *text;
-       *p && (isalnum (*p) || *p == '-' || *p == '_' ||
+       *p && (isalnum (*p) || *p == '-' || *p == '_' || *p == '>' || 
 	      (tui_version &&
 	       (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
 	      (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
@@ -943,7 +943,7 @@
 	    {
 	      char *p = *line, *q;
 
-	      while (isalnum (*p) || *p == '-')
+	      while (isalnum (*p) || *p == '-' || *p == '>')
 		p++;
 
 	      q = (char *) alloca (p - *line + 1);


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]