# HG changeset patch # User jwe # Date 1141452134 0 # Node ID 426719471ac6ae27a67a3a3c28a27bdded5a5f8b # Parent acbcb9f164ca76568b631300f31ae76a54f03b85 [project @ 2006-03-04 06:02:14 by jwe] diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,6 +1,14 @@ 2006-03-03 John W. Eaton - * 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 diff --git a/src/dirfns.cc b/src/dirfns.cc --- a/src/dirfns.cc +++ b/src/dirfns.cc @@ -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\ diff --git a/src/input.cc b/src/input.cc --- a/src/input.cc +++ b/src/input.cc @@ -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 *) { diff --git a/src/input.h b/src/input.h --- a/src/input.h +++ b/src/input.h @@ -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;