Index: trunk/Ohana/src/opihi/cmd.basic/list.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.basic/list.c	(revision 38971)
+++ trunk/Ohana/src/opihi/cmd.basic/list.c	(revision 38972)
@@ -1,22 +1,34 @@
 # include "basic.h"
+# include <glob.h>
 # define D_NLINES 100
 static char prompt[] = ">> ";
 
+int SplitByCharToList (int argc, char **argv, int EXCEL_STYLE);
+int GlobToList (char *listname, char *Glob, int EXCEL_STYLE);
+int CommandToList (char *listname, char *Command, int EXCEL_STYLE);
+
 int list (int argc, char **argv) {
 
   int ThisList, depth, i, done, found;
   char *input, line[1024];
-  int N, Nbytes, NBYTES, Nread, status;
-  int RunCommand;
-  char *A, *B, *val, *Cmd;
-  FILE *f;
-
-  Cmd = NULL;
-  RunCommand = FALSE;
+  int N;
+
+  char *Command = NULL;
   if ((N = get_argument (argc, argv, "-x"))) {
     remove_argument (N, &argc, argv);
-    Cmd = strcreate (argv[N]);
-    remove_argument (N, &argc, argv);
-    RunCommand = TRUE;
+    Command = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-exec"))) {
+    remove_argument (N, &argc, argv);
+    Command = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  char *Glob = NULL;
+  if ((N = get_argument (argc, argv, "-glob"))) {
+    remove_argument (N, &argc, argv);
+    Glob = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
   }
 
@@ -37,5 +49,5 @@
       return (FALSE);
     }
-    ListVectorsToList (argv[1]);
+    ListVectorsToList (argv[1]); // <-- generate the list
     return TRUE;
   }
@@ -47,6 +59,25 @@
       return (FALSE);
     }
-    ListBuffersToList (argv[1]);
+    ListBuffersToList (argv[1]); // <-- generate the list
     return TRUE;
+  }
+
+  if (Command) {
+    if (argc != 2) {
+      gprint (GP_ERR, "USAGE: list (root) -exec (command)\n");
+      gprint (GP_ERR, "   OR: list (root) -x (command)\n");
+      return (FALSE);
+    }
+    int status = CommandToList (argv[1], Command, EXCEL_STYLE); // <-- generate the list
+    return status;
+  }
+
+  if (Glob) {
+    if (argc != 2) {
+      gprint (GP_ERR, "USAGE: list (root) -glob (fileglob)\n");
+      return (FALSE);
+    }
+    int status = GlobToList (argv[1], Glob, EXCEL_STYLE); // <-- generate the list
+    return status;
   }
 
@@ -74,37 +105,6 @@
       return (FALSE);
     }
-    
-    int j, nWords;
-    char splitter;
-    char *new, *word, *ptr;
-
-    nWords = 0;
-    splitter = argv[3][0];
-
-    for (i = 0; i < argc - 3; i++) {
-      new = strcreate (argv[i+3]);
-      for (j = 0; new[j]; j++) {
-	if (new[j] == splitter) new[j] = ' ';
-      }
-
-      ptr = new;
-      while (ptr) {
-	word = thisword (ptr);
-	if (!word) break;
-	
-	// sprintf (line, "%s:%d", argv[1], nWords);
-	set_list_varname (line, argv[1], nWords, EXCEL_STYLE);
-
-	set_str_variable (line, word);
-	FREE (word);
-	ptr = nextword (ptr);
-	nWords ++;
-      }
-      FREE (new);
-    }
-    sprintf (line, "%s:n", argv[1]);
-    set_int_variable (line, nWords);
-
-    return (TRUE);
+    int status = SplitByCharToList (argc, argv, EXCEL_STYLE); // <-- generate the list
+    return status;
   }
 
@@ -200,4 +200,5 @@
     gprint (GP_ERR, "USAGE: list (root)                			       : supply list data, terminate with 'END'\n");
     gprint (GP_ERR, "USAGE: list (root) -x (command)   			       : create list from shell output\n");
+    gprint (GP_ERR, "USAGE: list (root) -exec (command)   		       : create list from shell output\n");
     gprint (GP_ERR, "USAGE: list (root) -vectors   			       : create list from vector names\n");
     gprint (GP_ERR, "USAGE: list (root) -buffers   			       : create list from buffer names\n");
@@ -208,59 +209,4 @@
     gprint (GP_ERR, "USAGE: list (root) -del (word)   			       : delete the entry by value\n");
     return (FALSE);
-  }
-
-  if (RunCommand) {
-    
-    /* val will hold the result */
-    NBYTES = 1024;
-    ALLOCATE (val, char, NBYTES);
-    
-    /* need to loop until command produces no more output, 
-       REALLOCATING as needed. */
-    f = popen (Cmd, "r");
-    done = FALSE;
-    Nbytes = 0;
-    while (!done) {
-      Nread = fread (&val[Nbytes], 1, 1023, f);
-      if (Nread < 0) { 
-	gprint (GP_ERR, "error reading from command\n");
-	done = TRUE;
-      }
-      if (Nread > 0) {
-	Nbytes += Nread;
-	NBYTES = 1024 + Nbytes;
-	REALLOCATE (val, char, NBYTES);
-      }
-      if (Nread == 0) {
-	done = TRUE;
-      }
-      
-    }
-    val[Nbytes] = 0;
-    status = pclose (f);
-    free (Cmd);
-    
-    if (status) {
-      gprint (GP_ERR, "warning: exit status of command %d\n", status);
-    }
-      
-    A = B = val;
-    for (i = 0; B != (char *) NULL;) {
-      while (isspace (*A) && (*A != 0)) A++;
-      B = strchr (A, '\n');
-      if (B != (char *) NULL) { *B = 0; }
-      if (*A != 0) {
-	// sprintf (line, "%s:%d", argv[1], i);
-	set_list_varname (line, argv[1], i, EXCEL_STYLE);
-	set_str_variable (line, A);
-	A = B + 1;
-	i++;
-      }
-    }      
-    free (val);
-    
-    sprintf (line, "%s:n", argv[1]);
-    set_int_variable (line, i);
-    return (TRUE);
   }
 
@@ -309,2 +255,123 @@
   return (TRUE);
 }
+
+int CommandToList (char *listname, char *Command, int EXCEL_STYLE) {
+
+  char line[1024];
+
+  /* val will hold the result */
+  int NBYTES = 1024;
+  char *val;
+  ALLOCATE (val, char, NBYTES);
+    
+  /* need to loop until command produces no more output, 
+     REALLOCATING as needed. */
+  FILE *f = popen (Command, "r");
+
+  int done = FALSE;
+  int Nbytes = 0;
+  while (!done) {
+    int Nread = fread (&val[Nbytes], 1, 1023, f);
+    if (Nread < 0) { 
+      gprint (GP_ERR, "error reading from command\n");
+      done = TRUE;
+    }
+    if (Nread > 0) {
+      Nbytes += Nread;
+      NBYTES = 1024 + Nbytes;
+      REALLOCATE (val, char, NBYTES);
+    }
+    if (Nread == 0) {
+      done = TRUE;
+    }
+  }
+  val[Nbytes] = 0;
+  int status = pclose (f);
+  free (Command);
+    
+  if (status) {
+    gprint (GP_ERR, "warning: exit status of command %d\n", status);
+  }
+      
+  int i;
+  char *A = val;
+  char *B = val;
+  for (i = 0; B != (char *) NULL;) {
+    while (isspace (*A) && (*A != 0)) A++;
+    B = strchr (A, '\n');
+    if (B != (char *) NULL) { *B = 0; }
+    if (*A != 0) {
+      set_list_varname (line, listname, i, EXCEL_STYLE);
+      set_str_variable (line, A);
+      A = B + 1;
+      i++;
+    }
+  }      
+  free (val);
+    
+  sprintf (line, "%s:n", listname);
+  set_int_variable (line, i);
+  return (TRUE);
+}
+
+int GlobToList (char *listname, char *Glob, int EXCEL_STYLE) {
+
+  int i;
+  char line[1024];
+  glob_t globList;
+
+  // parse the filename as a glob
+  globList.gl_offs = 0;
+  glob (Glob, 0, NULL, &globList);
+
+  // if the glob does not match, save the literal word:
+  // otherwise save all glob matches
+  if (globList.gl_pathc == 0) {
+    sprintf (line, "%s:n", listname);
+    set_int_variable (line, 0);
+    return TRUE;
+  }
+
+  int Nfile = globList.gl_pathc;
+  for (i = 0; i < Nfile; i++) {
+    set_list_varname (line, listname, i, EXCEL_STYLE);
+    set_str_variable (line, globList.gl_pathv[i]);
+  }
+  sprintf (line, "%s:n", listname);
+  set_int_variable (line, Nfile);
+  return (TRUE);
+}
+
+int SplitByCharToList (int argc, char **argv, int EXCEL_STYLE) {
+
+  int i, j;
+  char line[1024];
+
+  int nWords = 0;
+  char splitter = argv[3][0];
+
+  for (i = 0; i < argc - 3; i++) {
+    char *new = strcreate (argv[i+3]);
+    for (j = 0; new[j]; j++) {
+      if (new[j] == splitter) new[j] = ' ';
+    }
+
+    char *ptr = new;
+    while (ptr) {
+      char *word = thisword (ptr);
+      if (!word) break;
+	
+      set_list_varname (line, argv[1], nWords, EXCEL_STYLE);
+
+      set_str_variable (line, word);
+      FREE (word);
+      ptr = nextword (ptr);
+      nWords ++;
+    }
+    FREE (new);
+  }
+  sprintf (line, "%s:n", argv[1]);
+  set_int_variable (line, nWords);
+
+  return (TRUE);
+}
