mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
refactor: extract code to open stdin for reading
This commit is contained in:
parent
7089f33144
commit
628b717022
@ -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);
|
||||
|
@ -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,
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user