Index: /trunk/Ohana/src/opihi/cmd.basic/list.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.basic/list.c	(revision 10646)
+++ /trunk/Ohana/src/opihi/cmd.basic/list.c	(revision 10647)
@@ -98,5 +98,5 @@
   /* read in loop */
   depth = 0;
-  ThisList = Nlists;
+  ThisList = current_list_depth();
   for (i = 0, done = FALSE; !done; ) {
 
Index: /trunk/Ohana/src/opihi/cmd.basic/run_for.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.basic/run_for.c	(revision 10646)
+++ /trunk/Ohana/src/opihi/cmd.basic/run_for.c	(revision 10647)
@@ -27,5 +27,5 @@
   /* read in loop */
   depth = 0;
-  ThisList = Nlists;
+  ThisList = current_list_depth();
   for (i = 0, done = FALSE; !done; ) {
 
@@ -104,5 +104,5 @@
 
 /*
-  If we are entering at the keyboard (Nlists == 0), use readline.
+  If we are entering at the keyboard (ThisList == 0), use readline.
   Otherwise, read from the current list, remove list lines.
   execute when we hit the final "END" (the true END -- we count macro defines!!) 
Index: /trunk/Ohana/src/opihi/cmd.basic/run_if.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.basic/run_if.c	(revision 10646)
+++ /trunk/Ohana/src/opihi/cmd.basic/run_if.c	(revision 10647)
@@ -26,5 +26,5 @@
   nloop = 0;
   depth = 0;
-  ThisList = Nlists;
+  ThisList = current_list_depth();
 
   /* determine value of conditional expression */
@@ -108,5 +108,5 @@
 
 /*
-     If we are entering at the keyboard (Nlists == 0), use readline.
+     If we are entering at the keyboard (ThisList == 0), use readline.
      Otherwise, read from the current list, remove list lines.
      End when we hit the final "END" (the true END -- we count macro defines!!) 
Index: /trunk/Ohana/src/opihi/cmd.basic/run_while.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.basic/run_while.c	(revision 10646)
+++ /trunk/Ohana/src/opihi/cmd.basic/run_while.c	(revision 10647)
@@ -25,5 +25,5 @@
   /* read in loop */
   depth = 0;
-  ThisList = Nlists;
+  ThisList = current_list_depth();
   for (i = 0, done = FALSE; !done; ) {
 
Index: /trunk/Ohana/src/opihi/cmd.basic/test/for.sh
===================================================================
--- /trunk/Ohana/src/opihi/cmd.basic/test/for.sh	(revision 10646)
+++ /trunk/Ohana/src/opihi/cmd.basic/test/for.sh	(revision 10647)
@@ -8,4 +8,5 @@
  memtest1
  memtest2
+ memtest2a
  memtest3
 end
@@ -162,4 +163,58 @@
 end
 
+# check memleaks with many loop lines
+macro memtest2a
+
+ local i N
+
+ list word -x "ps -p $PID -o rss"
+ $startmem = $word:1
+
+ output /dev/null
+ for i 0 10000
+  echo "test line in loop"
+  echo "test line in loop"
+ end    
+ output stdout
+  
+ list word -x "ps -p $PID -o rss"
+ $endmem = $word:1
+
+ $PASS = 1
+
+ if ($endmem - $startmem > 10)
+   $PASS = 0
+   echo "growth: {$endmem-$startmem}"
+   echo "kB/loop: {($endmem-$startmem)/10000}"
+ end
+end
+
+# check memleaks with many loop lines
+macro memtest2b
+
+ local i N
+
+ list word -x "ps -p $PID -o rss"
+ $startmem = $word:1
+
+ output /dev/null
+ for i 0 1000
+  exec echo "test line in loop" > /dev/null
+  exec echo "test line in loop" > /dev/null
+ end    
+ output stdout
+  
+ list word -x "ps -p $PID -o rss"
+ $endmem = $word:1
+
+ $PASS = 1
+
+ if ($endmem - $startmem > 10)
+   $PASS = 0
+   echo "growth: {$endmem-$startmem}"
+   echo "kB/loop: {($endmem-$startmem)/1000}"
+ end
+end
+
 # check memleaks on break
 macro memtest3
Index: /trunk/Ohana/src/opihi/include/shell.h
===================================================================
--- /trunk/Ohana/src/opihi/include/shell.h	(revision 10646)
+++ /trunk/Ohana/src/opihi/include/shell.h	(revision 10647)
@@ -37,4 +37,5 @@
   int    n;
   int    Nlines;
+  int    Nalloc;
 } List;
 
@@ -50,7 +51,4 @@
 
 /*** globals used to track the shell language concepts  ***/
-List         *lists;			/* variable to store the list of all lists */
-int          Nlists;			/* number of currently available lists */
-
 int 	     interrupt;			/* true if C-C has been pressed */
 int 	     auto_break;		/* if true, zero exit status forces macros to escape */
@@ -82,4 +80,8 @@
 int           is_macro_create       	PROTO((char *line));
 void          InitLists                 PROTO(());
+int current_list_depth ();
+int increase_list_depth ();
+int decrease_list_depth ();
+char *get_next_listentry (int ThisList);
 
 void          InitCommands              PROTO(());
@@ -101,6 +103,7 @@
 
 int           exec_loop                 PROTO((Macro *loop));
-char         *get_next_listentry    	PROTO((int ThisList));
-char         *remove_listentry      	PROTO((int current));
+/* char         *get_next_listentry    	PROTO((int ThisList)); */
+/* char         *remove_listentry      	PROTO((int current)); */
+
 int           ConfigInit            	PROTO((int *argc, char **argv));
 char         *VarConfig             	PROTO((char *keyword, char *mode, void *ptr));
Index: /trunk/Ohana/src/opihi/lib.shell/ListOps.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.shell/ListOps.c	(revision 10646)
+++ /trunk/Ohana/src/opihi/lib.shell/ListOps.c	(revision 10647)
@@ -1,4 +1,8 @@
 # include "opihi.h"
 # include "macro.h"
+
+/*** local static variables used to track the command lists  ***/
+static List *lists;			/* variable to store the list of all lists */
+static int  Nlists;			/* number of currently available lists */
 
 void InitLists () {
@@ -8,20 +12,48 @@
 }
 
+int current_list_depth () {
+  return Nlists;
+}
+
+int increase_list_depth () {
+  Nlists ++;
+  REALLOCATE (lists, List, MAX (Nlists + 1, 0) + 1);
+  ALLOCATE (lists[Nlists].line, char *, 16);
+  lists[Nlists].Nalloc = 16;
+  lists[Nlists].Nlines = 0;
+  lists[Nlists].n = 0;
+  return Nlists;
+}
+
+int decrease_list_depth () {
+  
+  int i;
+
+  for (i = 0; i < lists[Nlists].Nlines; i++) {
+    free (lists[Nlists].line[i]);
+  }
+  free (lists[Nlists].line);
+  Nlists --;
+  REALLOCATE (lists, List, MAX (Nlists + 1, 0) + 1);
+  return Nlists;
+}
+
 /* return a new string consisting of the next line in the current list */
 char *get_next_listentry (int ThisList) {
 
+  int Nline;
   char *output;
 
-  lists[ThisList].n++;
-
-  if (lists[ThisList].n >= lists[Nlists].Nlines) 
-    return ((char *) NULL);
-
-  output = lists[Nlists].line[lists[ThisList].n];
+  Nline = lists[ThisList].n;
+
+  if (Nline >= lists[ThisList].Nlines) return (NULL);
+
+  output = strcreate (lists[ThisList].line[Nline]);
+  lists[ThisList].n ++;
   
   return (output);
-
-}
-
+}
+
+# if (0)
 char *remove_listentry (int current) {
 
@@ -41,4 +73,22 @@
   return (output);
 
+}
+# endif 
+
+int add_listentry (int ThisList, char *line) {
+
+  int Nlines;
+  char *output;
+
+  Nlines = lists[ThisList].Nlines;
+  lists[ThisList].line[Nlines] = strcreate (line);
+  lists[ThisList].Nlines ++;
+
+  if (lists[ThisList].Nlines == lists[ThisList].Nalloc) {
+    lists[ThisList].Nalloc += 16;
+    REALLOCATE (lists[ThisList].line, char *, lists[ThisList].Nalloc);
+  }
+    
+  return (lists[ThisList].Nlines);
 }
 
Index: /trunk/Ohana/src/opihi/lib.shell/exec_loop.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.shell/exec_loop.c	(revision 10646)
+++ /trunk/Ohana/src/opihi/lib.shell/exec_loop.c	(revision 10647)
@@ -8,13 +8,10 @@
   
   /* increase the shell level (Nlists) by one */
-  Nlists ++;
-  ThisList = Nlists;
-  REALLOCATE (lists, List, MAX (ThisList, 0) + 1);
-  
+  ThisList = increase_list_depth();
+  if (ThisList == 0) abort();
+
   /* copy the macro to the current list */
-  lists[ThisList].Nlines = loop[0].Nlines;
-  ALLOCATE (lists[ThisList].line, char *, MAX (lists[ThisList].Nlines, 1));
-  for (j = 0; j < lists[ThisList].Nlines; j++) {
-    lists[ThisList].line[j] = strcreate (loop[0].line[j]);
+  for (j = 0; j < loop[0].Nlines; j++) {
+    add_listentry (ThisList, loop[0].line[j]);
   }
 
@@ -26,14 +23,10 @@
   loop_continue = loop_break = FALSE;
   status = TRUE;
-  Nlines = lists[ThisList].Nlines;
-  for (n = 0; (n < Nlines) && !interrupt; n++) {
-    lists[ThisList].n = n; 
-    line = lists[ThisList].line[n];
 
+  while (!interrupt) {
+    line = get_next_listentry (ThisList);
+    if (line == NULL) break;
     status = multicommand (line);
-
-    if (line != NULL) free (line);
-    n = lists[ThisList].n;
-    Nlines = lists[ThisList].Nlines;
+    free (line);
     if (auto_break && !status) loop_break = TRUE;
     if (loop_break || loop_continue) break;
@@ -42,10 +35,6 @@
 
   /* free remaining lines on the list, free the list, decrement the shell level */
-  for (n ++; n < Nlines; n ++) {
-      free (lists[ThisList].line[n]);
-  }
-  free (lists[ThisList].line);
-  Nlists --;
-  REALLOCATE (lists, List, MAX (Nlists, 0) + 1);
+  /*** can we free a list which is not the bottom lists? */
+  decrease_list_depth();
 
   if (loop_break) return (FALSE);
Index: /trunk/Ohana/src/opihi/lib.shell/macro_create.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.shell/macro_create.c	(revision 10646)
+++ /trunk/Ohana/src/opihi/lib.shell/macro_create.c	(revision 10647)
@@ -60,10 +60,10 @@
 
   /* read in macro
-     If we are entering at the keyboard (Nlists == 0), use readline.
+     If we are entering at the keyboard (ThisList == 0), use readline.
      Otherwise, read from the current list
      End when we hit the final "END" (the true END -- we count macro defines!!) */
      
   depth = 0;
-  ThisList = Nlists;
+  ThisList = current_list_depth();
   for (i = 0, done = FALSE; !done; ) {
 
Index: /trunk/Ohana/src/opihi/mana/opihi.c
===================================================================
--- /trunk/Ohana/src/opihi/mana/opihi.c	(revision 10646)
+++ /trunk/Ohana/src/opihi/mana/opihi.c	(revision 10647)
@@ -20,7 +20,4 @@
 
   auto_break = TRUE;
-
-  Nlists = 0;
-  ALLOCATE (lists, List, 1); 
 
   /* init functions required by libraries */
Index: /trunk/Ohana/src/opihi/pantasks/LocalJob.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/LocalJob.c	(revision 10646)
+++ /trunk/Ohana/src/opihi/pantasks/LocalJob.c	(revision 10647)
@@ -29,4 +29,5 @@
       case 0:   /* pipe is closed, change child state? **/
       default:  /* data in pipe */
+	// fprintf (stderr, "read %d bytes (Nblock: %d, Nbuffer: %d)\n", Nread, job[0].stdout.Nblock, job[0].stdout.Nbuffer);
 	break;
     }
Index: /trunk/Ohana/src/opihi/pantasks/scheduler.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/scheduler.c	(revision 10646)
+++ /trunk/Ohana/src/opihi/pantasks/scheduler.c	(revision 10647)
@@ -11,7 +11,4 @@
   
   auto_break = TRUE;
-
-  Nlists = 0;
-  ALLOCATE (lists, List, 1); 
 
   /* init functions required by libraries */
Index: /trunk/Ohana/src/opihi/pantasks/task.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/task.c	(revision 10646)
+++ /trunk/Ohana/src/opihi/pantasks/task.c	(revision 10647)
@@ -45,5 +45,5 @@
   /* allowed tokens: command, host, stderr, periods, trange, nmax, task.exit, task.exec, end */
 
-  ThisList = Nlists;
+  ThisList = current_list_depth();
   while (1) {
 
Index: /trunk/Ohana/src/opihi/pantasks/task_macros.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/task_macros.c	(revision 10646)
+++ /trunk/Ohana/src/opihi/pantasks/task_macros.c	(revision 10647)
@@ -105,5 +105,5 @@
   depth = 0;
   done = FALSE;
-  ThisList = Nlists;
+  ThisList = current_list_depth();
   while (!done) {
 
Index: /trunk/Ohana/src/opihi/pantasks/test/local.sh
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/test/local.sh	(revision 10646)
+++ /trunk/Ohana/src/opihi/pantasks/test/local.sh	(revision 10647)
@@ -41,5 +41,5 @@
  list word -x "ps -p $PID -o rss"
  $endmem = $word:1
- echo growth: {$endmem - $startmem}
+ # echo growth: {$endmem - $startmem}
 end
 
Index: /trunk/Ohana/src/opihi/pcontrol/pcontrol.c
===================================================================
--- /trunk/Ohana/src/opihi/pcontrol/pcontrol.c	(revision 10646)
+++ /trunk/Ohana/src/opihi/pcontrol/pcontrol.c	(revision 10647)
@@ -65,5 +65,6 @@
 
 void gotsignal (int signum) {
-  gprint (GP_ERR, "got signal : %d\n", signum);
+  // this message is lost if we are connected to a pantasks
+  fprintf (stderr, "got signal : %d\n", signum);
   return;
 }
