[Ocfs2-tools-devel] [PATCH 1/3] libtools-internal: Add automatic 'yes' and 'no' to tools_interact().

Joel Becker joel.becker at oracle.com
Wed Sep 10 16:30:21 PDT 2008


Programs wishing to provide '-y' and '-n' options can now call
tools_interact_yes() and tools_interact_no().  Once that's done,
tools_interact() will use this answer rather than prompting stdin.

Signed-off-by: Joel Becker <joel.becker at oracle.com>
---
 include/tools-internal/verbose.h |    3 ++
 libtools-internal/verbose.c      |   19 +++++++++++++++-
 tunefs.ocfs2/ocfs2ne.c           |   44 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 65 insertions(+), 1 deletions(-)

diff --git a/include/tools-internal/verbose.h b/include/tools-internal/verbose.h
index d22d63c..4354c1a 100644
--- a/include/tools-internal/verbose.h
+++ b/include/tools-internal/verbose.h
@@ -52,6 +52,9 @@ void tools_quiet(void);
 
 /* Sets the process interactive */
 void tools_interactive(void);
+/* Sets automatic answers for interactive questions */
+void tools_interactive_yes(void);
+void tools_interactive_no(void);
 
 /*
  * Output that honors the verbosity level.  tcom_err() is for errcode_t
diff --git a/libtools-internal/verbose.c b/libtools-internal/verbose.c
index 15cade3..0ed7b64 100644
--- a/libtools-internal/verbose.c
+++ b/libtools-internal/verbose.c
@@ -32,6 +32,7 @@
 static char progname[PATH_MAX] = "(Unknown)";
 static int verbosity = 1;
 static int interactive = 0;
+static int interactive_answer = 0;
 
 void tools_setup_argv0(const char *argv0)
 {
@@ -123,7 +124,13 @@ static int vtools_interact(enum tools_verbosity_level level,
 
 	vfverbosef(stderr, level, fmt, args);
 
-	s = fgets(buffer, sizeof(buffer), stdin);
+	if (interactive_answer) {
+		fverbosef(stderr, level, "%c\n", interactive_answer);
+		sprintf(buffer, "%c", interactive_answer);
+		s = buffer;
+	} else
+		s = fgets(buffer, sizeof(buffer), stdin);
+
 	if (s && *s) {
 		*s = tolower(*s);
 		if (*s == 'y')
@@ -138,6 +145,16 @@ void tools_interactive(void)
 	interactive = 1;
 }
 
+void tools_interactive_yes(void)
+{
+	interactive_answer = 'y';
+}
+
+void tools_interactive_no(void)
+{
+	interactive_answer = 'n';
+}
+
 /* Pass this a question without a newline. */
 int tools_interact(const char *fmt, ...)
 {
diff --git a/tunefs.ocfs2/ocfs2ne.c b/tunefs.ocfs2/ocfs2ne.c
index 04022df..8bd12ad 100644
--- a/tunefs.ocfs2/ocfs2ne.c
+++ b/tunefs.ocfs2/ocfs2ne.c
@@ -200,6 +200,30 @@ static int handle_interactive(struct tunefs_option *opt, char *arg)
 	return 0;
 }
 
+static int handle_answer(struct tunefs_option *opt, char *arg)
+{
+	int rc = 0;
+
+	switch (opt->opt_option.val)
+	{
+		case 'y':
+			tools_interactive_yes();
+			break;
+
+		case 'n':
+			tools_interactive_no();
+			break;
+
+		default:
+			errorf("Invalid option to handle_answer: %c\n",
+			       opt->opt_option.val);
+			rc = 1;
+			break;
+	}
+
+	return rc;
+}
+
 /*
  * Plain operations just want to have their ->to_parse_option() called.
  * Their tunefs_option can use this function if they set opt_op to the
@@ -400,6 +424,24 @@ static struct tunefs_option interactive_option = {
 	.opt_handle	= handle_interactive,
 };
 
+static struct tunefs_option yes_option = {
+	.opt_option	= {
+		.name	= "yes",
+		.val	= 'y',
+	},
+	.opt_help	= "-y|--yes",
+	.opt_handle	= handle_answer,
+};
+
+static struct tunefs_option no_option = {
+	.opt_option	= {
+		.name	= "no",
+		.val	= 'n',
+	},
+	.opt_help	= "-n|--no",
+	.opt_handle	= handle_answer,
+};
+
 static struct tunefs_option query_option = {
 	.opt_option	= {
 		.name		= "query",
@@ -525,6 +567,8 @@ static struct tunefs_option *options[] = {
 	&backup_super_option,
 	&features_option,
 	&update_cluster_stack_option,
+	&yes_option,
+	&no_option,
 	NULL,
 };
 
-- 
1.5.6.3




More information about the Ocfs2-tools-devel mailing list