RFC [PATCH] possible new feature: exit with an error status of 2 for ^X^Q and for ^O^Q

Benno Schulenberg <[email protected]>
Newsgroups gmane.editors.nano.devel
Message-ID <[email protected]>
Normally, nano exits with a status of 0.  But if the user wants to
make nano exit with an error status (to signal failure to a calling
program), they can now use ^X^Q (when the buffer is modified), or
^O^Q (when option --saveonexit is in effect).

This fulfills https://savannah.gnu.org/bugs/?65755.
Requested-by: Frank Wolff
---
 src/files.c      | 1 +
 src/global.c     | 3 +++
 src/nano.c       | 3 ++-
 src/prompt.c     | 6 ++++--
 src/prototypes.h | 2 ++
 5 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/files.c b/src/files.c
index fef47b27..c41340f3 100644
--- a/src/files.c
+++ b/src/files.c
@@ -2189,6 +2189,7 @@ int write_it_out(bool exiting, bool withprompt)
 
 		/* Upon request, abandon the buffer. */
 		if (function == discard_buffer) {
+			final_status = 2;  /* ^O^Q makes nano exit with an error. */
 			free(given);
 			return 2;
 		}
diff --git a/src/global.c b/src/global.c
index 9c6b1e66..38afb6a8 100644
--- a/src/global.c
+++ b/src/global.c
@@ -55,6 +55,9 @@ bool report_size = TRUE;
 bool ran_a_tool = FALSE;
 		/* Whether a tool has been run at the Execute-Command prompt. */
 
+int final_status = 0;
+		/* The status value that nano returns upon exit. */
+
 bool inhelp = FALSE;
 		/* Whether we are in the help viewer. */
 char *title = NULL;
diff --git a/src/nano.c b/src/nano.c
index 123af18d..6640754a 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -260,7 +260,7 @@ void finish(void)
 #endif
 
 	/* Get out. */
-	exit(0);
+	exit(final_status);
 }
 
 /* Close the current buffer, and terminate nano if it is the only buffer. */
@@ -2652,6 +2652,7 @@ int main(int argc, char **argv)
 			wredrawln(midwin, editwinrows - 1, 1);
 #endif
 
+		final_status = 0;
 		errno = 0;
 		focusing = TRUE;
 
diff --git a/src/prompt.c b/src/prompt.c
index d427d1ea..09571dd2 100644
--- a/src/prompt.c
+++ b/src/prompt.c
@@ -759,10 +759,12 @@ int ask_user(bool withall, const char *question)
 #endif
 		/* Interpret ^N as "No", to allow exiting in anger, and ^Q or ^X too. */
 		else if (kbinput == '\x0E' || (kbinput == '\x11' && !ISSET(MODERN_BINDINGS)) ||
-									  (kbinput == '\x18' && ISSET(MODERN_BINDINGS)))
+									  (kbinput == '\x18' && ISSET(MODERN_BINDINGS))) {
 			choice = NO;
+			if (kbinput != '\x0E')  /* ^X^Q makes nano exit with an error. */
+				final_status = 2;
 		/* And interpret ^Y as "Yes". */
-		else if (kbinput == '\x19')
+		} else if (kbinput == '\x19')
 			choice = YES;
 #ifdef ENABLE_MOUSE
 		else if (kbinput == KEY_MOUSE) {
diff --git a/src/prototypes.h b/src/prototypes.h
index 55ab21c1..b6acbde7 100644
--- a/src/prototypes.h
+++ b/src/prototypes.h
@@ -39,6 +39,8 @@ extern bool more_than_one;
 extern bool report_size;
 extern bool ran_a_tool;
 
+extern int final_status;
+
 extern bool inhelp;
 extern char *title;
 
-- 
2.42.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.