Repository: neovim/neovim
File: src/nvim/ex_docmd.c (C)

Code before:
    not_exiting();
    return;
  }
  getout(0);
}

/// ":restart": restart the Nvim server (using ":qall!").
/// ":restart +cmd": restart the Nvim server using ":cmd".
/// ":restart +cmd <command>": restart the Nvim server using ":cmd" and add -c <command> to the new server.
static void ex_restart(exarg_T *eap)
{
  // Patch v:argv to include "-c <arg>" when it restarts.
  if (eap->arg != NULL) {
    const list_T *l = get_vim_var_list(VV_ARGV);
    int argc = tv_list_len(l);
    list_T *argv_cpy = tv_list_alloc(argc + 2);
    bool added_startup_arg = false;
    TV_LIST_ITER_CONST(l, li, {
      const char *arg = tv_get_string(TV_LIST_ITEM_TV(li));
      size_t arg_size = strlen(arg);
      assert(arg_size <= (size_t)SSIZE_MAX);
      tv_list_append_string(argv_cpy, arg, (ssize_t)arg_size);
      if (!added_startup_arg) {
        tv_list_append_string(argv_cpy, "-c", 2);
        size_t cmd_size = strlen(eap->arg);
        // Handle command after TRLBAR ("|").
        if (eap->nextcmd != NULL) {
          size_t next_cmd_size = strlen(eap->nextcmd);
          assert(cmd_size + next_cmd_size + 3 <= SIZE_MAX);  // Include size of " | " as well.
          cmd_size += next_cmd_size + 3;
        }
        assert(cmd_size <= (size_t)SSIZE_MAX);
        char *cmd;
        if (eap->nextcmd != NULL) {
          cmd = xmalloc(sizeof(char) * (cmd_size + 1));
          snprintf(cmd, cmd_size + 1, "%s | %s", eap->arg, eap->nextcmd);
        } else {
          cmd = xstrdup(eap->arg);
        }
        tv_list_append_string(argv_cpy, cmd, (ssize_t)cmd_size);
        xfree(cmd);
        added_startup_arg = true;
      }
    });
    set_vim_var_list(VV_ARGV, argv_cpy);
  }
  char *quit_cmd = (eap->do_ecmd_cmd) ? eap->do_ecmd_cmd : "qall!";
  Error err = ERROR_INIT;
  if ((cmdmod.cmod_flags & CMOD_CONFIRM) && check_changed_any(false, false)) {
    return;
  }

Review comment (refactor):
why is handling needed? everything after the optional +cmd should just be a single `-c` arg.

how can we simplify this logic?

Code after:
    not_exiting();
    return;
  }
  getout(0);
}

/// ":restart": restart the Nvim server (using ":qall!").
/// ":restart +cmd": restart the Nvim server using ":cmd".
/// ":restart +cmd <command>": restart the Nvim server using ":cmd" and add -c <command> to the new server.
static void ex_restart(exarg_T *eap)
{
  // Patch v:argv to include "-c <arg>" when it restarts.
  if (eap->arg != NULL) {
    const list_T *l = get_vim_var_list(VV_ARGV);
    int argc = tv_list_len(l);
    list_T *argv_cpy = tv_list_alloc(argc + 2);
    bool added_startup_arg = false;
    TV_LIST_ITER_CONST(l, li, {
      const char *arg = tv_get_string(TV_LIST_ITEM_TV(li));
      size_t arg_size = strlen(arg);
      assert(arg_size <= (size_t)SSIZE_MAX);
      tv_list_append_string(argv_cpy, arg, (ssize_t)arg_size);
      if (!added_startup_arg) {
        tv_list_append_string(argv_cpy, "-c", 2);
        size_t cmd_size = strlen(eap->arg);
        assert(cmd_size <= (size_t)SSIZE_MAX);
        tv_list_append_string(argv_cpy, eap->arg, (ssize_t)cmd_size);
        added_startup_arg = true;
      }
    });
    set_vim_var_list(VV_ARGV, argv_cpy);
  }
  char *quit_cmd = (eap->do_ecmd_cmd) ? eap->do_ecmd_cmd : "qall!";
  Error err = ERROR_INIT;
  if ((cmdmod.cmod_flags & CMOD_CONFIRM) && check_changed_any(false, false)) {
    return;
  }
  restarting = true;
  nvim_command(cstr_as_string(quit_cmd), &err);
  if (ERROR_SET(&err)) {
    emsg(err.msg);  // Could not exit
    api_clear_error(&err);
    not_restarting();
    return;
  }
  if (!exiting) {
    emsg("restart failed: +cmd did not quit the server");
    not_restarting();
  }
}