aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2023-01-05 17:39:03 +0100
committerJustin M. Keyes <justinkz@gmail.com>2023-01-05 17:44:12 +0100
commit628b717022815a431ea0b980444d6115c644f8c8 (patch)
tree77f73c6e88134d702ca275e43b45f5316ba5b47e
parent7089f331447bf335696276e969649fb6ee360e07 (diff)
downloadrneovim-628b717022815a431ea0b980444d6115c644f8c8.tar.gz
rneovim-628b717022815a431ea0b980444d6115c644f8c8.tar.bz2
rneovim-628b717022815a431ea0b980444d6115c644f8c8.zip
refactor: extract code to open stdin for reading
-rw-r--r--src/nvim/lua/executor.c15
-rw-r--r--src/nvim/main.c14
-rw-r--r--src/nvim/os/fileio.c24
3 files changed, 26 insertions, 27 deletions
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c
index c756242817..307b34a55c 100644
--- a/src/nvim/lua/executor.c
+++ b/src/nvim/lua/executor.c
@@ -1724,20 +1724,7 @@ bool nlua_exec_file(const char *path)
lua_getglobal(lstate, "loadfile");
lua_pushstring(lstate, path);
} else {
- int error;
- int stdin_dup_fd;
- if (stdin_fd > 0) {
- stdin_dup_fd = stdin_fd;
- } else {
- stdin_dup_fd = os_dup(STDIN_FILENO);
-#ifdef MSWIN
- // Replace the original stdin with the console input handle.
- os_replace_stdin_to_conin();
-#endif
- }
- FileDescriptor *const stdin_dup = file_open_fd_new(&error, stdin_dup_fd,
- kFileReadOnly|kFileNonBlocking);
- assert(stdin_dup != NULL);
+ FileDescriptor *stdin_dup = file_open_stdin();
StringBuilder sb = KV_INITIAL_VALUE;
kv_resize(sb, 64);
diff --git a/src/nvim/main.c b/src/nvim/main.c
index 80640861cc..9bb9eea4f6 100644
--- a/src/nvim/main.c
+++ b/src/nvim/main.c
@@ -1567,19 +1567,7 @@ static void open_script_files(mparm_T *parmp)
if (parmp->scriptin) {
int error;
if (strequal(parmp->scriptin, "-")) {
- int stdin_dup_fd;
- if (stdin_fd > 0) {
- stdin_dup_fd = stdin_fd;
- } else {
- stdin_dup_fd = os_dup(STDIN_FILENO);
-#ifdef MSWIN
- // Replace the original stdin with the console input handle.
- os_replace_stdin_to_conin();
-#endif
- }
- FileDescriptor *const stdin_dup = file_open_fd_new(&error, stdin_dup_fd,
- kFileReadOnly|kFileNonBlocking);
- assert(stdin_dup != NULL);
+ FileDescriptor *stdin_dup = file_open_stdin();
scriptin[0] = stdin_dup;
} else {
scriptin[0] = file_open_new(&error, parmp->scriptin,
diff --git a/src/nvim/os/fileio.c b/src/nvim/os/fileio.c
index bdea82f1ff..e93e1febcb 100644
--- a/src/nvim/os/fileio.c
+++ b/src/nvim/os/fileio.c
@@ -167,6 +167,30 @@ FileDescriptor *file_open_fd_new(int *const error, const int fd, const int flags
return fp;
}
+/// Opens standard input as a FileDescriptor.
+FileDescriptor *file_open_stdin(void)
+ FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT
+{
+ int error;
+ int stdin_dup_fd;
+ if (stdin_fd > 0) {
+ stdin_dup_fd = stdin_fd;
+ } else {
+ stdin_dup_fd = os_dup(STDIN_FILENO);
+#ifdef MSWIN
+ // Replace the original stdin with the console input handle.
+ os_replace_stdin_to_conin();
+#endif
+ }
+ FileDescriptor *const stdin_dup = file_open_fd_new(&error, stdin_dup_fd,
+ kFileReadOnly|kFileNonBlocking);
+ assert(stdin_dup != NULL);
+ if (error != 0) {
+ ELOG("failed to open stdin: %s", os_strerror(error));
+ }
+ return stdin_dup;
+}
+
/// Close file and free its buffer
///
/// @param[in,out] fp File to close.