changeset 79:04c30624aaf9

[project @ 1993-08-30 19:43:25 by jwe]
author jwe
date Mon, 30 Aug 1993 19:43:57 +0000
parents 0fda6e1f90e0
children 74f0d57ee66c
files src/builtins.cc src/t-builtins.cc
diffstat 2 files changed, 62 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/builtins.cc
+++ b/src/builtins.cc
@@ -154,7 +154,7 @@
     "change current working directory\n", },
 
   { "clear", -1, builtin_clear,
-    "clear symbol(s) from symbol table\n", },
+    "clear symbol(s) matching a list of regular expressions\n", },
 
   { "dir", -1, builtin_ls,
     "print a directory listing\n", },
--- a/src/t-builtins.cc
+++ b/src/t-builtins.cc
@@ -50,6 +50,7 @@
 #include <sys/stat.h>
 #include <time.h>
 #include <errno.h>
+#include <String.h>
 
 #include "procstream.h"
 
@@ -206,8 +207,22 @@
   return retval;
 }
 
+static int
+in_list (char *s, char **list)
+{
+  while (*list != (char *) NULL)
+    {
+      if (strcmp (s, *list) == 0)
+	return 1;
+      list++;
+    }
+
+  return 0;
+}
+
 /*
- * Wipe out user-defined variables and functions.
+ * Wipe out user-defined variables and functions given a list of
+ * regular expressions. 
  */
 tree_constant
 builtin_clear (int argc, char **argv)
@@ -220,12 +235,55 @@
     }
   else
     {
+      int count;
+      char **names = curr_sym_tab->list (count);
+
+      int g_count;
+      char **g_names = global_sym_tab->list (g_count);
+
+      int num_cleared = 0;
+      char **locals_cleared = new char * [count+1];
+      locals_cleared[num_cleared] = (char *) NULL;
+
       while (--argc > 0)
 	{
 	  argv++;
-	  if (*argv != (char *) NULL && ! curr_sym_tab->clear (*argv))
-	    global_sym_tab->clear (*argv);
+	  if (*argv != (char *) NULL)
+	    {
+	      Regex rx (*argv);
+
+	      int i;
+	      for (i = 0; i < count; i++)
+		{
+		  String nm (names[i]);
+		  if (nm.matches (rx) && curr_sym_tab->clear (names[i]))
+		    {
+		      locals_cleared[num_cleared++] = strsave (names[i]);
+		      locals_cleared[num_cleared] = (char *) NULL;
+		    }
+		}
+
+	      for (i = 0; i < g_count; i++)
+		{
+		  String nm (g_names[i]);
+		  if (nm.matches (rx)
+		      && ! in_list (g_names[i], locals_cleared))
+		    {
+		      global_sym_tab->clear (g_names[i]);
+		    }
+		}
+	    }
 	}
+
+      int i = 0;
+      while (locals_cleared[i] != (char *) NULL)
+	delete [] locals_cleared[i++];
+      delete [] locals_cleared;
+
+      delete [] names;
+      delete [] g_names;
+      delete [] g_names;
+
     }
   return retval;
 }