changeset 5640:426719471ac6

[project @ 2006-03-04 06:02:14 by jwe]
author jwe
date Sat, 04 Mar 2006 06:02:14 +0000
parents acbcb9f164ca
children eb998631a4aa
files src/ChangeLog src/dirfns.cc src/input.cc src/input.h
diffstat 4 files changed, 105 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Fri Mar 03 16:39:35 2006 +0000
+++ b/src/ChangeLog	Sat Mar 04 06:02:14 2006 +0000
@@ -1,6 +1,14 @@
 2006-03-03  John W. Eaton  <jwe@octave.org>
 
-	* dirfns.cc (Frmdir): Require second arg to be "s".
+	* dirfns.cc (Vconfirm_recursive_rmdir): New static variable.
+	(symbols_of_dirfns): DEFVAR it.
+	(confirm_recursive_rmdir): New function.
+	(Frmdir): Maybe ask for confirmation for recursive removal.
+	Require second arg to be "s".
+
+	* input.cc (octave_yes_or_no): New function.
+	(Fyes_or_no): New function.
+	* input.h (octave_yes_or_no): Provide decl.
 
 2006-03-02  John W. Eaton  <jwe@octave.org>
 
--- a/src/dirfns.cc	Fri Mar 03 16:39:35 2006 +0000
+++ b/src/dirfns.cc	Sat Mar 04 06:02:14 2006 +0000
@@ -53,6 +53,7 @@
 #include "dirfns.h"
 #include "error.h"
 #include "gripes.h"
+#include "input.h"
 #include "oct-obj.h"
 #include "pager.h"
 #include "procstream.h"
@@ -62,6 +63,10 @@
 #include "utils.h"
 #include "variables.h"
 
+// TRUE means we ask for confirmation before recursively removing a
+// directory tree.
+static bool Vconfirm_recursive_rmdir = true;
+
 // XXX FIXME XXX -- changing the plotter directory should be handled
 // by registering a function for octave_env::chdir to call so that
 // this function can be eliminated.
@@ -350,7 +355,7 @@
 \n\
 If the optional second parameter is suplied, recursively remove all\n\
 subdirectories as well.\n\
-@seealso{mkdir}\n\
+@seealso{mkdir, confirm_recursive_rmdir}\n\
 @end deftypefn")
 {
   octave_value_list retval;
@@ -369,19 +374,32 @@
 	gripe_wrong_type_arg ("rmdir", args(0));
       else
 	{
+	  std::string fulldir = file_ops::tilde_expand (dirname);
+	  int status = -1;
 	  std::string msg;
 
-	  std::string fulldir = file_ops::tilde_expand (dirname);
-
 	  if (nargin == 2)
 	    {
 	      if (args(1).string_value () == "s")
-		status = file_ops::recursive_rmdir (fulldir, msg);
+		{
+		  bool doit = true;
+
+		  if (interactive && Vconfirm_recursive_rmdir)
+		    {
+		      std::string prompt
+			= "remove entire contents of " + fulldir + "? ";
+
+		      doit = octave_yes_or_no (prompt);
+		    }
+
+		  if (doit)
+		    status = file_ops::recursive_rmdir (fulldir, msg);
+		}
 	      else
 		error ("rmdir: expecting second argument to be \"s\"");
 	    }
 	  else
-	    status = file_ops::rmdir (fulldir, msg)
+	    status = file_ops::rmdir (fulldir, msg);
 
 	  if (status < 0)
 	    {
@@ -669,9 +687,25 @@
   return retval;
 }
 
+static int
+confirm_recursive_rmdir (void)
+{
+  Vconfirm_recursive_rmdir = check_preference ("confirm_recursive_rmdir");
+
+  return 0;
+}
+
 void
 symbols_of_dirfns (void)
 {
+  DEFVAR (confirm_recursive_rmdir, true, confirm_recursive_rmdir,
+    "-*- texinfo -*-\n\
+@defvr {Built-in Variable} confirm_recursive_rmdir\n\
+If the value of @code{confirm_recursive_rmdir} is nonzero, Octave\n\
+will ask for confirmation before recursively removing a directory tree.\n\
+The default value is 0.\n\
+@end defvr");
+
   DEFCONST (filesep, file_ops::dir_sep_str,
     "-*- texinfo -*-\n\
 @defvr {Built-in Variable} filesep\n\
--- a/src/input.cc	Fri Mar 03 16:39:35 2006 +0000
+++ b/src/input.cc	Sat Mar 04 06:02:14 2006 +0000
@@ -737,6 +737,61 @@
   return retval;
 }
 
+bool
+octave_yes_or_no (const std::string& prompt)
+{
+  std::string prompt_string = prompt + "(yes or no) ";
+
+  while (1)
+    {
+      std::string input_buf = gnu_readline (prompt_string);
+
+      if (input_buf == "yes")
+	return true;
+      else if (input_buf == "no")
+	return false;
+      else
+	message (0, "Please answer yes or no.");
+    }
+}
+
+DEFUN (yes_or_no, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Built-in Function} {} yes_or_no (@var{prompt})\n\
+Ask the user a yes-or-no question.  Return 1 if the answer is yes.\n\
+Takes one argument, which is the string to display to ask the\n\
+question.  It should end in a space; @samp{yes-or-no-p} adds\n\
+@samp{(yes or no) } to it.  The user must confirm the answer with\n\
+RET and can edit it until it has been confirmed.\n\
+@end deftypefn")
+{
+  octave_value retval;
+
+  int nargin = args.length ();
+
+  if (nargin == 0 || nargin == 1)
+    {
+      std::string prompt;
+
+      if (nargin == 1)
+	{
+	  prompt = args(0).string_value ();
+
+	  if (error_state)
+	    {
+	      error ("yes_or_no: expecting argument to be character string");
+	      return retval;
+	    }
+	}
+
+      retval = octave_yes_or_no (prompt);
+    }
+  else
+    print_usage ("yes_or_no");
+
+  return retval;
+}
+
 static void
 restore_command_history (void *)
 {
--- a/src/input.h	Fri Mar 03 16:39:35 2006 +0000
+++ b/src/input.h	Sat Mar 04 06:02:14 2006 +0000
@@ -88,6 +88,8 @@
 
 extern void initialize_command_input (void);
 
+extern bool octave_yes_or_no (const std::string& prompt);
+
 extern octave_value do_keyboard (const octave_value_list& args = octave_value_list ());
 
 extern std::string Vps4;