aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/main.c
diff options
context:
space:
mode:
authorBjörn Linse <bjorn.linse@gmail.com>2021-12-09 21:29:01 +0100
committerBjörn Linse <bjorn.linse@gmail.com>2021-12-09 23:46:37 +0100
commita59589ca018425f672eb70bf7d2b54d24df49326 (patch)
tree8a0558d9ac90a111e78bb69df095410620dfd5a9 /src/nvim/main.c
parentd9c1669a5474ee0d09dbe60372a2959207187290 (diff)
downloadrneovim-a59589ca018425f672eb70bf7d2b54d24df49326.tar.gz
rneovim-a59589ca018425f672eb70bf7d2b54d24df49326.tar.bz2
rneovim-a59589ca018425f672eb70bf7d2b54d24df49326.zip
refactor(misc1): move preserve_exit() to related functions in main.c
Diffstat (limited to 'src/nvim/main.c')
-rw-r--r--src/nvim/main.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/nvim/main.c b/src/nvim/main.c
index 921bc883cf..29510e26ff 100644
--- a/src/nvim/main.c
+++ b/src/nvim/main.c
@@ -696,6 +696,50 @@ void getout(int exitval)
os_exit(exitval);
}
+/// Preserve files and exit.
+/// @note IObuff must contain a message.
+/// @note This may be called from deadly_signal() in a signal handler, avoid
+/// unsafe functions, such as allocating memory.
+void preserve_exit(void)
+ FUNC_ATTR_NORETURN
+{
+ // 'true' when we are sure to exit, e.g., after a deadly signal
+ static bool really_exiting = false;
+
+ // Prevent repeated calls into this method.
+ if (really_exiting) {
+ if (input_global_fd() >= 0) {
+ // normalize stream (#2598)
+ stream_set_blocking(input_global_fd(), true);
+ }
+ exit(2);
+ }
+
+ really_exiting = true;
+ // Ignore SIGHUP while we are already exiting. #9274
+ signal_reject_deadly();
+ mch_errmsg(IObuff);
+ mch_errmsg("\n");
+ ui_flush();
+
+ ml_close_notmod(); // close all not-modified buffers
+
+ FOR_ALL_BUFFERS(buf) {
+ if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) {
+ mch_errmsg("Vim: preserving files...\r\n");
+ ui_flush();
+ ml_sync_all(false, false, true); // preserve all swap files
+ break;
+ }
+ }
+
+ ml_close_all(false); // close all memfiles, without deleting
+
+ mch_errmsg("Vim: Finished.\r\n");
+
+ getout(1);
+}
+
/// Gets the integer value of a numeric command line argument if given,
/// such as '-o10'.
///