Subject: bug#77389: 31.0.50; Restarting Emacs with (kill-emacs ... t) looses noninteractivity

From: Jens Schmidt <[email-masked]>
Date: Wed, 2 Apr 2025 23:20:36 +0200

Forgot to attach the alternative patch, sorry.

From 70414967c6030520f9535c446672d8ba6a72e49a Mon Sep 17 00:00:00 2001
From: Jens Schmidt <[email-masked]>
Date: Wed, 2 Apr 2025 22:48:31 +0200
Subject: [PATCH] Use raw argv as passed into main to restart Emacs

---
 src/emacs.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/src/emacs.c b/src/emacs.c
index 6ff7b632c0f..92553e525ea 100644
--- a/src/emacs.c
+++ b/src/emacs.c
@@ -246,6 +246,7 @@ #define MAIN_PROGRAM
 /* The name of the working directory, or NULL if this info is unavailable.  */
 char const *emacs_wd;
 
+static void copy_raw_args (int argc, char **argv);
 static void sort_args (int argc, char **argv);
 static void syms_of_emacs (void);
 
@@ -476,9 +477,6 @@ init_cmdargs (int argc, char **argv, int skip_args, char const *original_pwd)
   Lisp_Object raw_name;
   AUTO_STRING (slash_colon, "/:");
 
-  initial_argv = argv;
-  initial_argc = argc;
-
 #ifdef WINDOWSNT
   /* Must use argv[0] converted to UTF-8, as it begets many standard
      file and directory names.  */
@@ -1430,6 +1428,7 @@ android_emacs_init (int argc, char **argv, char *dump_file)
      'command-line-args-left' in 'command-line-1'.  */
 
   bool only_version = false;
+  copy_raw_args (argc, argv);
   sort_args (argc, argv);
   old_argc = argc, argc = 0;
   /* Don't allow going past argv.  */
@@ -2696,6 +2695,19 @@ android_emacs_init (int argc, char **argv, char *dump_file)
   { "-kill", "--kill", -10, 0 },
 };
 
+static void
+copy_raw_args (int argc, char **argv)
+{
+  char **new = xmalloc ((argc + 1) * sizeof *new);
+  int i;
+  new[0] = argv[0];
+  for (i = 1; i < argc; i++)
+    new[i] = xstrdup (argv[i]);
+  new[argc] = argv[argc];
+  initial_argv = new;
+  initial_argc = argc;
+}
+
 /* Reorder the elements of ARGV (assumed to have ARGC elements)
    so that the highest priority ones come first.
    Do not change the order of elements of equal priority.
-- 
2.39.5