commit 8e104117a44ca91b74f618a25a53f9223cae22f4
parent b8b71399e883257ba0c3f38837d4536e2c5f3c52
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Mon, 27 May 2024 07:23:43 +0200
new feature: exit with an error status (2) for ^X^Q and ^O^Q
Nano used to exit always with a success status (0). But now, if
the user wants to make nano report failure to a calling program,
they can use ^X^Q (when the buffer is modified) or ^O^Q (always).
This fulfills https://savannah.gnu.org/bugs/?65755.
Diffstat:
5 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/src/files.c b/src/files.c
@@ -2195,6 +2195,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
@@ -58,6 +58,9 @@ char *foretext = NULL;
/* What was typed at the Execute prompt before invoking a tool. */
#endif
+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
@@ -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. */
@@ -2717,6 +2717,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
@@ -788,10 +788,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;
/* Also, interpret ^Y as "Yes, and ^A as "All". */
- else if (kbinput == '\x19')
+ } else if (kbinput == '\x19')
choice = YES;
else if (kbinput == '\x01' && withall)
choice = ALL;
diff --git a/src/prototypes.h b/src/prototypes.h
@@ -40,6 +40,8 @@ extern bool report_size;
extern bool ran_a_tool;
extern char *foretext;
+extern int final_status;
+
extern bool inhelp;
extern char *title;