help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Output from 'who' to a variable?


From: John W. Eaton
Subject: Output from 'who' to a variable?
Date: Mon, 23 Jun 2003 12:52:14 -0500

On 23-Jun-2003, Mats Jansson <address@hidden> wrote:

| Is it possible to get the output of the command 'who' to a variable?
| I would appreciate any way (even an awkward) of achieving this. 
| 
| I'm using octave 2.1.49
| 
| Thanks!
| Mats
| 
| octave:2> f = who -f
|  
| *** currently compiled functions:
|  
| rows
|  
| error: value on right hand side of assignment is undefined
| error: evaluating assignment expression near line 2, column 3
| octave:2>

Here is a patch.  It is a little ugly and only does part of the job
for whos.  What I would really like to see is all of this cleaned up.
Instead of having the functions in symtab.cc do the printing, there
should be some basic inquiry functions in symtab.cc and then some
M-files that do the work of who and whos.  Those functions should
probably also be fixed to handle arguments in a Matlab-compatible way.

jwe


2003-06-23  John W. Eaton  <address@hidden>

        * variables.cc (do_who): New arg, return_list.  If return_list is
        true, return values in cell or struct arrays instead of printing.
        (Fwho, Fwhos): If nargout is 1, ask do_who to return lists of values.
        * oct-map.h (Octave_map (const std::string&, const octave_value_list&):
        New constructor.


Index: src/oct-map.h
===================================================================
RCS file: /usr/local/cvsroot/octave/src/oct-map.h,v
retrieving revision 1.23
diff -u -r1.23 oct-map.h
--- src/oct-map.h       6 Dec 2002 21:29:18 -0000       1.23
+++ src/oct-map.h       23 Jun 2003 17:48:07 -0000
@@ -49,6 +49,12 @@
        map[key] = octave_value_list (value);
       }
 
+  Octave_map (const std::string& key, const octave_value_list& val_list)
+    : map (), array_len (val_list.length ())
+      {
+       map[key] = val_list;
+      }
+
   Octave_map (const Octave_map& m)
     : map (m.map), array_len (m.array_len) { }
 
Index: src/variables.cc
===================================================================
RCS file: /usr/local/cvsroot/octave/src/variables.cc,v
retrieving revision 1.247
diff -u -r1.247 variables.cc
--- src/variables.cc    22 Feb 2003 18:48:18 -0000      1.247
+++ src/variables.cc    23 Jun 2003 17:48:08 -0000
@@ -36,6 +36,7 @@
 #include "str-vec.h"
 
 #include <defaults.h>
+#include "Cell.h"
 #include "defun.h"
 #include "dirfns.h"
 #include "error.h"
@@ -1059,10 +1060,10 @@
   return retval;
 }
 
-static octave_value_list
-do_who (int argc, const string_vector& argv)
+static octave_value
+do_who (int argc, const string_vector& argv, int return_list)
 {
-  octave_value_list retval;
+  octave_value retval;
 
   bool show_builtins = false;
   bool show_functions = false;
@@ -1118,55 +1119,113 @@
       show_variables = 1;
     }
 
-  int pad_after = 0;
-
-  if (show_builtins)
+  if (return_list)
     {
-      pad_after += fbi_sym_tab->maybe_list
-       ("*** built-in constants:", pats, octave_stdout,
-        show_verbose, symbol_record::BUILTIN_CONSTANT, SYMTAB_ALL_SCOPES);
+      string_vector names;
 
-      pad_after += fbi_sym_tab->maybe_list
-       ("*** built-in variables:", pats, octave_stdout,
-        show_verbose, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES);
+      if (show_builtins)
+       {
+         names.append (fbi_sym_tab->name_list
+                       (pats, true, symbol_record::BUILTIN_CONSTANT,
+                        SYMTAB_ALL_SCOPES));
+
+         names.append (fbi_sym_tab->name_list
+                       (pats, true, symbol_record::BUILTIN_VARIABLE,
+                        SYMTAB_ALL_SCOPES));
+
+         names.append (fbi_sym_tab->name_list
+                       (pats, true, symbol_record::BUILTIN_FUNCTION,
+                        SYMTAB_ALL_SCOPES));
+       }
 
-      pad_after += fbi_sym_tab->maybe_list
-       ("*** built-in functions:", pats, octave_stdout,
-        show_verbose, symbol_record::BUILTIN_FUNCTION, SYMTAB_ALL_SCOPES);
-    }
+      if (show_functions)
+       {
+         names.append (fbi_sym_tab->name_list
+                       (pats, true, symbol_record::DLD_FUNCTION,
+                        SYMTAB_ALL_SCOPES));
+
+         names.append (fbi_sym_tab->name_list
+                       (pats, true, symbol_record::USER_FUNCTION,
+                        SYMTAB_ALL_SCOPES));
+       }
 
-  if (show_functions)
-    {
-      pad_after += fbi_sym_tab->maybe_list
-       ("*** dynamically linked functions:", pats,
-        octave_stdout, show_verbose, symbol_record::DLD_FUNCTION,
-        SYMTAB_ALL_SCOPES);
+      if (show_variables)
+       {
+         names.append (curr_sym_tab->name_list
+                       (pats, true, symbol_record::USER_VARIABLE,
+                        SYMTAB_LOCAL_SCOPE));
+
+         names.append (curr_sym_tab->name_list
+                       (pats, true, symbol_record::USER_VARIABLE,
+                        SYMTAB_GLOBAL_SCOPE));
+       }
 
-      pad_after += fbi_sym_tab->maybe_list
-       ("*** currently compiled functions:", pats,
-        octave_stdout, show_verbose, symbol_record::USER_FUNCTION,
-        SYMTAB_ALL_SCOPES);
-    }
+      if (show_verbose)
+       {
+         int len = names.length ();
 
-  if (show_variables)
-    {
-      pad_after += curr_sym_tab->maybe_list
-       ("*** local user variables:", pats, octave_stdout,
-        show_verbose, symbol_record::USER_VARIABLE, SYMTAB_LOCAL_SCOPE);
+         octave_value_list ovl (len, octave_value ());
+
+         for (int i = 0; i < len; i++)
+           ovl(i) = names(i);
 
-      pad_after += curr_sym_tab->maybe_list
-       ("*** globally visible user variables:", pats,
-        octave_stdout, show_verbose, symbol_record::USER_VARIABLE,
-        SYMTAB_GLOBAL_SCOPE);
+         retval = Octave_map ("name", ovl);
+       }
+      else
+       retval = Cell (names);
     }
+  else
+    {
+      int pad_after = 0;
 
-  if (pad_after)
-    octave_stdout << "\n";
+      if (show_builtins)
+       {
+         pad_after += fbi_sym_tab->maybe_list
+           ("*** built-in constants:", pats, octave_stdout,
+            show_verbose, symbol_record::BUILTIN_CONSTANT, SYMTAB_ALL_SCOPES);
+
+         pad_after += fbi_sym_tab->maybe_list
+           ("*** built-in variables:", pats, octave_stdout,
+            show_verbose, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES);
+
+         pad_after += fbi_sym_tab->maybe_list
+           ("*** built-in functions:", pats, octave_stdout,
+            show_verbose, symbol_record::BUILTIN_FUNCTION, SYMTAB_ALL_SCOPES);
+       }
+
+      if (show_functions)
+       {
+         pad_after += fbi_sym_tab->maybe_list
+           ("*** dynamically linked functions:", pats,
+            octave_stdout, show_verbose, symbol_record::DLD_FUNCTION,
+            SYMTAB_ALL_SCOPES);
+
+         pad_after += fbi_sym_tab->maybe_list
+           ("*** currently compiled functions:", pats,
+            octave_stdout, show_verbose, symbol_record::USER_FUNCTION,
+            SYMTAB_ALL_SCOPES);
+       }
+
+      if (show_variables)
+       {
+         pad_after += curr_sym_tab->maybe_list
+           ("*** local user variables:", pats, octave_stdout,
+            show_verbose, symbol_record::USER_VARIABLE, SYMTAB_LOCAL_SCOPE);
+
+         pad_after += curr_sym_tab->maybe_list
+           ("*** globally visible user variables:", pats,
+            octave_stdout, show_verbose, symbol_record::USER_VARIABLE,
+            SYMTAB_GLOBAL_SCOPE);
+       }
+
+      if (pad_after)
+       octave_stdout << "\n";
+    }
 
   return retval;
 }
 
-DEFCMD (who, args, ,
+DEFCMD (who, args, nargout,
   "-*- texinfo -*-\n\
 @deffn {Command} who options pattern @dots{}\n\
 @deffnx {Command} whos options pattern @dots{}\n\
@@ -1204,43 +1263,55 @@
 The command @kbd{whos} is equivalent to @kbd{who -long}.\n\
 @end deffn")
 {
-  octave_value_list retval;
+  octave_value retval;
 
-  int argc = args.length () + 1;
+  if (nargout < 2)
+    {
+      int argc = args.length () + 1;
 
-  string_vector argv = args.make_argv ("who");
+      string_vector argv = args.make_argv ("who");
 
-  if (error_state)
-    return retval;
+      if (error_state)
+       return retval;
 
-  retval = do_who (argc, argv);
+      retval = do_who (argc, argv, nargout == 1);
+    }
+  else
+    print_usage ("who");
 
   return retval;
 }
 
-DEFCMD (whos, args, ,
+DEFCMD (whos, args, nargout,
   "-*- texinfo -*-\n\
 @deffn {Command} whos options pattern @dots{}\n\
 See who.\n\
 @end deffn")
 {
-  octave_value_list retval;
+  octave_value retval;
 
-  int nargin = args.length ();
+  if (nargout < 2)
+    {
+      int nargin = args.length ();
 
-  octave_value_list tmp_args;
-  for (int i = nargin; i > 0; i--)
-    tmp_args(i) = args(i-1);
-  tmp_args(0) = "-long";
+      octave_value_list tmp_args;
 
-  int argc = tmp_args.length () + 1;
+      for (int i = nargin; i > 0; i--)
+       tmp_args(i) = args(i-1);
 
-  string_vector argv = tmp_args.make_argv ("whos");
+      tmp_args(0) = "-long";
 
-  if (error_state)
-    return retval;
+      int argc = tmp_args.length () + 1;
+
+      string_vector argv = tmp_args.make_argv ("whos");
+
+      if (error_state)
+       return retval;
 
-  retval = do_who (argc, argv);
+      retval = do_who (argc, argv, nargout == 1);
+    }
+  else
+    print_usage ("whos");
 
   return retval;
 }



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]