From 65c41e6c2b5015ff0b0485aadd362c0883a02a85 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 16 Mar 2017 23:24:20 +0300 Subject: main: Make `-s -` read from stdin --- src/nvim/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/main.c b/src/nvim/main.c index 4416bd067c..99dd9fc18f 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1056,7 +1056,9 @@ scripterror: mch_errmsg("\"\n"); mch_exit(2); } - if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL) { + if (STRCMP(argv[0], "-") == 0) { + scriptin[0] = stdin; + } else if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL) { mch_errmsg(_("Cannot open for reading: \"")); mch_errmsg(argv[0]); mch_errmsg("\"\n"); -- cgit From fdfa1ed578afd41a68f05c88dc419d88051b7240 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 16:09:48 +0300 Subject: main: Temporary fix assertion error This variant uses `fdopen()` which is not standard, but it fixes problem on my system. In next commit `scriptin` will use `FileDescriptor*` from os/fileio in place of `FILE*`. --- src/nvim/main.c | 4 +++- src/nvim/os/fs.c | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/main.c b/src/nvim/main.c index 99dd9fc18f..ce0426bd8e 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1057,7 +1057,9 @@ scripterror: mch_exit(2); } if (STRCMP(argv[0], "-") == 0) { - scriptin[0] = stdin; + const int stdin_dup_fd = os_dup(STDIN_FILENO); + FILE *const stdin_dup = fdopen(stdin_dup_fd, "r"); + scriptin[0] = stdin_dup; } else if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL) { mch_errmsg(_("Cannot open for reading: \"")); mch_errmsg(argv[0]); diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 2beeae7ec6..9ce50d4afd 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -430,6 +430,29 @@ int os_close(const int fd) return r; } +/// Duplicate file descriptor +/// +/// @param[in] fd File descriptor to duplicate. +/// +/// @return New file descriptor or libuv error code (< 0). +int os_dup(const int fd) + FUNC_ATTR_WARN_UNUSED_RESULT +{ + int ret; +os_dup_dup: + ret = dup(fd); + if (ret < 0) { + const int error = os_translate_sys_error(errno); + errno = 0; + if (error == UV_EINTR) { + goto os_dup_dup; + } else { + return error; + } + } + return ret; +} + /// Read from a file /// /// Handles EINTR and ENOMEM, but not other errors. -- cgit From bd798a3267a496c644b339c45189b09e2a952014 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 16:55:37 +0300 Subject: getchar: Use fileio instead of fdopen Problem: as fileio is cached and reads blocks this is going to wait until either EOF or reading enough characters to fill rbuffer. This is not good when reading user input from stdin as script. --- src/nvim/getchar.c | 90 ++++++++++++++++++++++++-------------------------- src/nvim/getchar.h | 8 +++++ src/nvim/globals.h | 4 +-- src/nvim/main.c | 14 +++++--- src/nvim/os/fileio.c | 48 +++++++++++++++++++++++++-- src/nvim/os/os_defs.h | 3 ++ src/nvim/os/win_defs.h | 4 +++ 7 files changed, 115 insertions(+), 56 deletions(-) (limited to 'src') diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index bae8ae6d91..b6e235146e 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -44,6 +44,12 @@ #include "nvim/event/loop.h" #include "nvim/os/input.h" #include "nvim/os/os.h" +#include "nvim/os/fileio.h" + + +/// Index in scriptin +static int curscript = 0; +FileDescriptor *scriptin[NSCRIPT] = { NULL }; /* * These buffers are used for storing: @@ -1244,10 +1250,13 @@ openscript ( ++curscript; /* use NameBuff for expanded name */ expand_env(name, NameBuff, MAXPATHL); - if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL) { - EMSG2(_(e_notopen), name); - if (curscript) - --curscript; + int error; + if ((scriptin[curscript] = file_open_new(&error, (char *)NameBuff, + kFileReadOnly, 0)) == NULL) { + emsgf(_(e_notopen_2), name, os_strerror(error)); + if (curscript) { + curscript--; + } return; } save_typebuf(); @@ -1297,7 +1306,7 @@ static void closescript(void) free_typebuf(); typebuf = saved_typebuf[curscript]; - fclose(scriptin[curscript]); + file_free(scriptin[curscript]); scriptin[curscript] = NULL; if (curscript > 0) --curscript; @@ -2346,7 +2355,6 @@ inchar ( { int len = 0; /* init for GCC */ int retesc = FALSE; /* return ESC with gotint */ - int script_char; if (wait_time == -1L || wait_time > 100L) { // flush output before waiting @@ -2364,45 +2372,38 @@ inchar ( } undo_off = FALSE; /* restart undo now */ - /* - * Get a character from a script file if there is one. - * If interrupted: Stop reading script files, close them all. - */ - script_char = -1; - while (scriptin[curscript] != NULL && script_char < 0 - && !ignore_script - ) { - - - if (got_int || (script_char = getc(scriptin[curscript])) < 0) { - /* Reached EOF. - * Careful: closescript() frees typebuf.tb_buf[] and buf[] may - * point inside typebuf.tb_buf[]. Don't use buf[] after this! */ + // Get a character from a script file if there is one. + // If interrupted: Stop reading script files, close them all. + ptrdiff_t read_size = -1; + while (scriptin[curscript] != NULL && read_size < 0 && !ignore_script) { + char script_char; + if (got_int + || (read_size = file_read(scriptin[curscript], &script_char, 1)) != 1) { + // Reached EOF or some error occurred. + // Careful: closescript() frees typebuf.tb_buf[] and buf[] may + // point inside typebuf.tb_buf[]. Don't use buf[] after this! closescript(); - /* - * When reading script file is interrupted, return an ESC to get - * back to normal mode. - * Otherwise return -1, because typebuf.tb_buf[] has changed. - */ - if (got_int) - retesc = TRUE; - else + // When reading script file is interrupted, return an ESC to get + // back to normal mode. + // Otherwise return -1, because typebuf.tb_buf[] has changed. + if (got_int) { + retesc = true; + } else { return -1; + } } else { buf[0] = (char_u)script_char; len = 1; } } - if (script_char < 0) { /* did not get a character from script */ - /* - * If we got an interrupt, skip all previously typed characters and - * return TRUE if quit reading script file. - * Stop reading typeahead when a single CTRL-C was read, - * fill_input_buf() returns this when not able to read from stdin. - * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[] - * and buf may be pointing inside typebuf.tb_buf[]. - */ + if (read_size < 0) { // Did not get a character from script. + // If we got an interrupt, skip all previously typed characters and + // return TRUE if quit reading script file. + // Stop reading typeahead when a single CTRL-C was read, + // fill_input_buf() returns this when not able to read from stdin. + // Don't use buf[] here, closescript() may have freed typebuf.tb_buf[] + // and buf may be pointing inside typebuf.tb_buf[]. if (got_int) { #define DUM_LEN MAXMAPLEN * 3 + 3 char_u dum[DUM_LEN + 1]; @@ -2415,21 +2416,18 @@ inchar ( return retesc; } - /* - * Always flush the output characters when getting input characters - * from the user. - */ + // Always flush the output characters when getting input characters + // from the user. ui_flush(); - /* - * Fill up to a third of the buffer, because each character may be - * tripled below. - */ + // Fill up to a third of the buffer, because each character may be + // tripled below. len = os_inchar(buf, maxlen / 3, (int)wait_time, tb_change_cnt); } - if (typebuf_changed(tb_change_cnt)) + if (typebuf_changed(tb_change_cnt)) { return 0; + } return fix_input_buffer(buf, len); } diff --git a/src/nvim/getchar.h b/src/nvim/getchar.h index bdf65909b6..04517866ec 100644 --- a/src/nvim/getchar.h +++ b/src/nvim/getchar.h @@ -1,6 +1,8 @@ #ifndef NVIM_GETCHAR_H #define NVIM_GETCHAR_H +#include "nvim/os/fileio.h" + /* Values for "noremap" argument of ins_typebuf(). Also used for * map->m_noremap and menu->noremap[]. */ #define REMAP_YES 0 /* allow remapping */ @@ -12,6 +14,12 @@ #define KEYLEN_PART_MAP -2 /* keylen value for incomplete mapping */ #define KEYLEN_REMOVED 9999 /* keylen value for removed sequence */ +/// Maximum number of streams to read script from +enum { NSCRIPT = 15 }; + +/// Streams to read script from +extern FileDescriptor *scriptin[NSCRIPT]; + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "getchar.h.generated.h" #endif diff --git a/src/nvim/globals.h b/src/nvim/globals.h index f8c7c9d330..8babb06d56 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -913,9 +913,6 @@ EXTERN int do_redraw INIT(= FALSE); /* extra redraw once */ EXTERN int need_highlight_changed INIT(= true); EXTERN char *used_shada_file INIT(= NULL); // name of the ShaDa file to use -#define NSCRIPT 15 -EXTERN FILE *scriptin[NSCRIPT]; /* streams to read script from */ -EXTERN int curscript INIT(= 0); /* index in scriptin[] */ EXTERN FILE *scriptout INIT(= NULL); /* stream to write script to */ // volatile because it is used in a signal handler. @@ -1151,6 +1148,7 @@ EXTERN char_u e_norange[] INIT(= N_("E481: No range allowed")); EXTERN char_u e_noroom[] INIT(= N_("E36: Not enough room")); EXTERN char_u e_notmp[] INIT(= N_("E483: Can't get temp file name")); EXTERN char_u e_notopen[] INIT(= N_("E484: Can't open file %s")); +EXTERN char_u e_notopen_2[] INIT(= N_("E484: Can't open file %s: %s")); EXTERN char_u e_notread[] INIT(= N_("E485: Can't read file %s")); EXTERN char_u e_nowrtmsg[] INIT(= N_( "E37: No write since last change (add ! to override)")); diff --git a/src/nvim/main.c b/src/nvim/main.c index ce0426bd8e..da3ec4381e 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1056,14 +1056,20 @@ scripterror: mch_errmsg("\"\n"); mch_exit(2); } + int error; if (STRCMP(argv[0], "-") == 0) { - const int stdin_dup_fd = os_dup(STDIN_FILENO); - FILE *const stdin_dup = fdopen(stdin_dup_fd, "r"); + const int stdin_dup_fd = os_dup(OS_STDIN_FILENO); + FileDescriptor *const stdin_dup = file_open_fd_new( + &error, stdin_dup_fd, false, 0); + assert(stdin_dup != NULL); scriptin[0] = stdin_dup; - } else if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL) { + } else if ((scriptin[0] = file_open_new( + &error, argv[0], kFileReadOnly, 0)) == NULL) { mch_errmsg(_("Cannot open for reading: \"")); mch_errmsg(argv[0]); - mch_errmsg("\"\n"); + mch_errmsg("\": "); + mch_errmsg(os_strerror(error)); + mch_errmsg("\n"); mch_exit(2); } save_typebuf(); diff --git a/src/nvim/os/fileio.c b/src/nvim/os/fileio.c index 775f2bd449..3742fd53de 100644 --- a/src/nvim/os/fileio.c +++ b/src/nvim/os/fileio.c @@ -45,7 +45,6 @@ int file_open(FileDescriptor *const ret_fp, const char *const fname, FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { int os_open_flags = 0; - int fd; TriState wr = kNone; #define FLAG(flags, flag, fcntl_flags, wrval, cond) \ do { \ @@ -70,13 +69,33 @@ int file_open(FileDescriptor *const ret_fp, const char *const fname, #endif #undef FLAG - fd = os_open(fname, os_open_flags, mode); + const int fd = os_open(fname, os_open_flags, mode); if (fd < 0) { return fd; } + return file_open_fd(ret_fp, fd, (wr == kTrue), mode); +} - ret_fp->wr = (wr == kTrue); +/// Wrap file descriptor with FileDescriptor structure +/// +/// @warning File descriptor wrapped like this must not be accessed by other +/// means. +/// +/// @param[out] ret_fp Address where information needed for reading from or +/// writing to a file is saved +/// @param[in] fd File descriptor to wrap. +/// @param[in] wr True if fd is opened for writing only, false if it is read +/// only. +/// @param[in] mode Permissions for the newly created file (ignored if flags +/// does not have FILE_CREATE\*). +/// +/// @return Error code (@see os_strerror()) or 0. Currently always returns 0. +int file_open_fd(FileDescriptor *const ret_fp, const int fd, + const bool wr, const int mode) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT +{ + ret_fp->wr = wr; ret_fp->fd = fd; ret_fp->eof = false; ret_fp->rv = rbuffer_new(kRWBufferSize); @@ -110,6 +129,29 @@ FileDescriptor *file_open_new(int *const error, const char *const fname, return fp; } +/// Like file_open_fd(), but allocate and return ret_fp +/// +/// @param[out] error Error code, @see os_strerror(). Is set to zero on +/// success. +/// @param[in] fd File descriptor to wrap. +/// @param[in] wr True if fd is opened for writing only, false if it is read +/// only. +/// @param[in] mode Permissions for the newly created file (ignored if flags +/// does not have FILE_CREATE\*). +/// +/// @return [allocated] Opened file or NULL in case of error. +FileDescriptor *file_open_fd_new(int *const error, const int fd, + const bool wr, const int mode) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT +{ + FileDescriptor *const fp = xmalloc(sizeof(*fp)); + if ((*error = file_open_fd(fp, fd, wr, mode)) != 0) { + xfree(fp); + return NULL; + } + return fp; +} + /// Close file and free its buffer /// /// @param[in,out] fp File to close. diff --git a/src/nvim/os/os_defs.h b/src/nvim/os/os_defs.h index 14c210c69c..88d8f4b750 100644 --- a/src/nvim/os/os_defs.h +++ b/src/nvim/os/os_defs.h @@ -13,6 +13,9 @@ # include "nvim/os/unix_defs.h" #endif +/// File descriptor number used for STDIN +enum { OS_STDIN_FILENO = STDIN_FILENO }; + #define BASENAMELEN (NAME_MAX - 5) // Use the system path length if it makes sense. diff --git a/src/nvim/os/win_defs.h b/src/nvim/os/win_defs.h index 8de896c490..f3493a7eed 100644 --- a/src/nvim/os/win_defs.h +++ b/src/nvim/os/win_defs.h @@ -87,4 +87,8 @@ typedef SSIZE_T ssize_t; # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) #endif +#ifndef STDIN_FILENO +# define STDIN_FILENO 0 +#endif + #endif // NVIM_OS_WIN_DEFS_H -- cgit From e78e75d85d91e9f14964465ea136b3899b774d6e Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 17:29:48 +0300 Subject: fileio,main: Do not restart syscall at EAGAIN when reading for -s --- src/nvim/getchar.c | 4 ++-- src/nvim/main.c | 4 ++-- src/nvim/os/fileio.c | 38 +++++++++++++++++++++++++------------- src/nvim/os/fileio.h | 3 +++ src/nvim/os/fs.c | 27 ++++++++++++++++++++------- 5 files changed, 52 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index b6e235146e..4ea8eb0513 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -2375,7 +2375,7 @@ inchar ( // Get a character from a script file if there is one. // If interrupted: Stop reading script files, close them all. ptrdiff_t read_size = -1; - while (scriptin[curscript] != NULL && read_size < 0 && !ignore_script) { + while (scriptin[curscript] != NULL && read_size <= 0 && !ignore_script) { char script_char; if (got_int || (read_size = file_read(scriptin[curscript], &script_char, 1)) != 1) { @@ -2397,7 +2397,7 @@ inchar ( } } - if (read_size < 0) { // Did not get a character from script. + if (read_size <= 0) { // Did not get a character from script. // If we got an interrupt, skip all previously typed characters and // return TRUE if quit reading script file. // Stop reading typeahead when a single CTRL-C was read, diff --git a/src/nvim/main.c b/src/nvim/main.c index da3ec4381e..8114164158 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1060,11 +1060,11 @@ scripterror: if (STRCMP(argv[0], "-") == 0) { const int stdin_dup_fd = os_dup(OS_STDIN_FILENO); FileDescriptor *const stdin_dup = file_open_fd_new( - &error, stdin_dup_fd, false, 0); + &error, stdin_dup_fd, kFileReadOnly|kFileNonBlocking, 0); assert(stdin_dup != NULL); scriptin[0] = stdin_dup; } else if ((scriptin[0] = file_open_new( - &error, argv[0], kFileReadOnly, 0)) == NULL) { + &error, argv[0], kFileReadOnly|kFileNonBlocking, 0)) == NULL) { mch_errmsg(_("Cannot open for reading: \"")); mch_errmsg(argv[0]); mch_errmsg("\": "); diff --git a/src/nvim/os/fileio.c b/src/nvim/os/fileio.c index 3742fd53de..70ed49c3aa 100644 --- a/src/nvim/os/fileio.c +++ b/src/nvim/os/fileio.c @@ -74,7 +74,7 @@ int file_open(FileDescriptor *const ret_fp, const char *const fname, if (fd < 0) { return fd; } - return file_open_fd(ret_fp, fd, (wr == kTrue), mode); + return file_open_fd(ret_fp, fd, flags, mode); } /// Wrap file descriptor with FileDescriptor structure @@ -85,17 +85,24 @@ int file_open(FileDescriptor *const ret_fp, const char *const fname, /// @param[out] ret_fp Address where information needed for reading from or /// writing to a file is saved /// @param[in] fd File descriptor to wrap. -/// @param[in] wr True if fd is opened for writing only, false if it is read -/// only. +/// @param[in] flags Flags, @see FileOpenFlags. Currently reading from and +/// writing to the file at once is not supported, so either +/// FILE_WRITE_ONLY or FILE_READ_ONLY is required. /// @param[in] mode Permissions for the newly created file (ignored if flags /// does not have FILE_CREATE\*). /// /// @return Error code (@see os_strerror()) or 0. Currently always returns 0. int file_open_fd(FileDescriptor *const ret_fp, const int fd, - const bool wr, const int mode) + const int flags, const int mode) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { - ret_fp->wr = wr; + ret_fp->wr = !!(flags & (kFileCreate|kFileCreateOnly + |kFileTruncate + |kFileAppend + |kFileWriteOnly)); + ret_fp->non_blocking = !!(flags & kFileNonBlocking); + // Non-blocking writes not supported currently. + assert(!ret_fp->wr || !ret_fp->non_blocking); ret_fp->fd = fd; ret_fp->eof = false; ret_fp->rv = rbuffer_new(kRWBufferSize); @@ -134,18 +141,17 @@ FileDescriptor *file_open_new(int *const error, const char *const fname, /// @param[out] error Error code, @see os_strerror(). Is set to zero on /// success. /// @param[in] fd File descriptor to wrap. -/// @param[in] wr True if fd is opened for writing only, false if it is read -/// only. +/// @param[in] flags Flags, @see FileOpenFlags. /// @param[in] mode Permissions for the newly created file (ignored if flags /// does not have FILE_CREATE\*). /// /// @return [allocated] Opened file or NULL in case of error. FileDescriptor *file_open_fd_new(int *const error, const int fd, - const bool wr, const int mode) + const int flags, const int mode) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT { FileDescriptor *const fp = xmalloc(sizeof(*fp)); - if ((*error = file_open_fd(fp, fd, wr, mode)) != 0) { + if ((*error = file_open_fd(fp, fd, flags, mode)) != 0) { xfree(fp); return NULL; } @@ -224,7 +230,8 @@ static void file_rb_write_full_cb(RBuffer *const rv, FileDescriptor *const fp) return; } const size_t read_bytes = rbuffer_read(rv, writebuf, kRWBufferSize); - const ptrdiff_t wres = os_write(fp->fd, writebuf, read_bytes); + const ptrdiff_t wres = os_write(fp->fd, writebuf, read_bytes, + fp->non_blocking); if (wres != (ptrdiff_t)read_bytes) { if (wres >= 0) { fp->_error = UV_EIO; @@ -274,7 +281,7 @@ ptrdiff_t file_read(FileDescriptor *const fp, char *const ret_buf, }; assert(write_count == kRWBufferSize); const ptrdiff_t r_ret = os_readv(fp->fd, &fp->eof, iov, - ARRAY_SIZE(iov)); + ARRAY_SIZE(iov), fp->non_blocking); if (r_ret > 0) { if (r_ret > (ptrdiff_t)read_remaining) { rbuffer_produced(rv, (size_t)(r_ret - (ptrdiff_t)read_remaining)); @@ -290,7 +297,8 @@ ptrdiff_t file_read(FileDescriptor *const fp, char *const ret_buf, if (read_remaining >= kRWBufferSize) { // …otherwise leave RBuffer empty and populate only target buffer, // because filtering information through rbuffer will be more syscalls. - const ptrdiff_t r_ret = os_read(fp->fd, &fp->eof, buf, read_remaining); + const ptrdiff_t r_ret = os_read(fp->fd, &fp->eof, buf, read_remaining, + fp->non_blocking); if (r_ret >= 0) { read_remaining -= (size_t)r_ret; return (ptrdiff_t)(size - read_remaining); @@ -301,7 +309,7 @@ ptrdiff_t file_read(FileDescriptor *const fp, char *const ret_buf, size_t write_count; const ptrdiff_t r_ret = os_read(fp->fd, &fp->eof, rbuffer_write_ptr(rv, &write_count), - kRWBufferSize); + kRWBufferSize, fp->non_blocking); assert(write_count == kRWBufferSize); if (r_ret > 0) { rbuffer_produced(rv, (size_t)r_ret); @@ -311,6 +319,10 @@ ptrdiff_t file_read(FileDescriptor *const fp, char *const ret_buf, } #endif } + if (fp->non_blocking) { + // Allow only at most one os_read[v] call. + break; + } } return (ptrdiff_t)(size - read_remaining); } diff --git a/src/nvim/os/fileio.h b/src/nvim/os/fileio.h index 0b55cc695f..7c53cd4f07 100644 --- a/src/nvim/os/fileio.h +++ b/src/nvim/os/fileio.h @@ -14,6 +14,7 @@ typedef struct { RBuffer *rv; ///< Read or write buffer. bool wr; ///< True if file is in write mode. bool eof; ///< True if end of file was encountered. + bool non_blocking; ///< True if EAGAIN should not restart syscalls. } FileDescriptor; /// file_open() flags @@ -32,6 +33,8 @@ typedef enum { ///< kFileCreateOnly. kFileAppend = 64, ///< Append to the file. Implies kFileWriteOnly. Cannot ///< be used with kFileCreateOnly. + kFileNonBlocking = 128, ///< Do not restart read() or write() syscall if + ///< EAGAIN was encountered. } FileOpenFlags; static inline bool file_eof(const FileDescriptor *const fp) diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 9ce50d4afd..383a2ac682 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -462,10 +462,11 @@ os_dup_dup: /// to false. Initial value is ignored. /// @param[out] ret_buf Buffer to write to. May be NULL if size is zero. /// @param[in] size Amount of bytes to read. +/// @param[in] non_blocking Do not restart syscall if EAGAIN was encountered. /// /// @return Number of bytes read or libuv error code (< 0). -ptrdiff_t os_read(const int fd, bool *ret_eof, char *const ret_buf, - const size_t size) +ptrdiff_t os_read(const int fd, bool *const ret_eof, char *const ret_buf, + const size_t size, const bool non_blocking) FUNC_ATTR_WARN_UNUSED_RESULT { *ret_eof = false; @@ -485,7 +486,9 @@ ptrdiff_t os_read(const int fd, bool *ret_eof, char *const ret_buf, if (cur_read_bytes < 0) { const int error = os_translate_sys_error(errno); errno = 0; - if (error == UV_EINTR || error == UV_EAGAIN) { + if (non_blocking && error == UV_EAGAIN) { + break; + } else if (error == UV_EINTR || error == UV_EAGAIN) { continue; } else if (error == UV_ENOMEM && !did_try_to_free) { try_to_free_memory(); @@ -515,7 +518,11 @@ ptrdiff_t os_read(const int fd, bool *ret_eof, char *const ret_buf, /// may change, it is incorrect to use data it points to after /// os_readv(). /// @param[in] iov_size Number of buffers in iov. -ptrdiff_t os_readv(int fd, bool *ret_eof, struct iovec *iov, size_t iov_size) +/// @param[in] non_blocking Do not restart syscall if EAGAIN was encountered. +/// +/// @return Number of bytes read or libuv error code (< 0). +ptrdiff_t os_readv(const int fd, bool *const ret_eof, struct iovec *iov, + size_t iov_size, const bool non_blocking) FUNC_ATTR_NONNULL_ALL { *ret_eof = false; @@ -548,7 +555,9 @@ ptrdiff_t os_readv(int fd, bool *ret_eof, struct iovec *iov, size_t iov_size) } else if (cur_read_bytes < 0) { const int error = os_translate_sys_error(errno); errno = 0; - if (error == UV_EINTR || error == UV_EAGAIN) { + if (non_blocking && error == UV_EAGAIN) { + break; + } else if (error == UV_EINTR || error == UV_EAGAIN) { continue; } else if (error == UV_ENOMEM && !did_try_to_free) { try_to_free_memory(); @@ -568,9 +577,11 @@ ptrdiff_t os_readv(int fd, bool *ret_eof, struct iovec *iov, size_t iov_size) /// @param[in] fd File descriptor to write to. /// @param[in] buf Data to write. May be NULL if size is zero. /// @param[in] size Amount of bytes to write. +/// @param[in] non_blocking Do not restart syscall if EAGAIN was encountered. /// /// @return Number of bytes written or libuv error code (< 0). -ptrdiff_t os_write(const int fd, const char *const buf, const size_t size) +ptrdiff_t os_write(const int fd, const char *const buf, const size_t size, + const bool non_blocking) FUNC_ATTR_WARN_UNUSED_RESULT { if (buf == NULL) { @@ -588,7 +599,9 @@ ptrdiff_t os_write(const int fd, const char *const buf, const size_t size) if (cur_written_bytes < 0) { const int error = os_translate_sys_error(errno); errno = 0; - if (error == UV_EINTR || error == UV_EAGAIN) { + if (non_blocking && error == UV_EAGAIN) { + break; + } else if (error == UV_EINTR || error == UV_EAGAIN) { continue; } else { return error; -- cgit From 88e1a17cde03bf35815136637ac3adf299f10cb2 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 17:36:04 +0300 Subject: *: Fix linter errors --- src/nvim/getchar.c | 4 ++-- src/nvim/globals.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index 4ea8eb0513..8fd7cebbc9 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -2353,8 +2353,8 @@ inchar ( int tb_change_cnt ) { - int len = 0; /* init for GCC */ - int retesc = FALSE; /* return ESC with gotint */ + int len = 0; // Init for GCC. + int retesc = false; // Return ESC with gotint. if (wait_time == -1L || wait_time > 100L) { // flush output before waiting diff --git a/src/nvim/globals.h b/src/nvim/globals.h index 8babb06d56..637e9e0a80 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -913,7 +913,7 @@ EXTERN int do_redraw INIT(= FALSE); /* extra redraw once */ EXTERN int need_highlight_changed INIT(= true); EXTERN char *used_shada_file INIT(= NULL); // name of the ShaDa file to use -EXTERN FILE *scriptout INIT(= NULL); /* stream to write script to */ +EXTERN FILE *scriptout INIT(= NULL); ///< Stream to write script to. // volatile because it is used in a signal handler. EXTERN volatile int got_int INIT(= false); // set to true when interrupt -- cgit From 83b39a4a027ec2b314e60b53c709611c770cc921 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 18:24:20 +0300 Subject: os/fileio: Fix QB failure --- src/nvim/os/fileio.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/nvim/os/fileio.c b/src/nvim/os/fileio.c index 70ed49c3aa..7668a867a0 100644 --- a/src/nvim/os/fileio.c +++ b/src/nvim/os/fileio.c @@ -68,6 +68,10 @@ int file_open(FileDescriptor *const ret_fp, const char *const fname, FLAG(flags, kFileNoSymlink, O_NOFOLLOW, kNone, true); #endif #undef FLAG + // wr is used for kFileReadOnly flag, but on + // QB:neovim-qb-slave-ubuntu-12-04-64bit it still errors out with + // `error: variable ‘wr’ set but not used [-Werror=unused-but-set-variable]` + (void)wr; const int fd = os_open(fname, os_open_flags, mode); -- cgit From ca116df2606a0191f6600acfcd6088d5ce15ce6f Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 19:28:16 +0300 Subject: main: Translate full -s error message, not part of it --- src/nvim/main.c | 18 ++++++++---------- src/nvim/message.c | 10 ++++------ 2 files changed, 12 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/nvim/main.c b/src/nvim/main.c index 8114164158..efe7944fa4 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1049,11 +1049,10 @@ static void command_line_scan(mparm_T *parmp) case 's': /* "-s {scriptin}" read from script file */ if (scriptin[0] != NULL) { scripterror: - mch_errmsg(_("Attempt to open script file again: \"")); - mch_errmsg(argv[-1]); - mch_errmsg(" "); - mch_errmsg(argv[0]); - mch_errmsg("\"\n"); + vim_snprintf((char *)IObuff, IOSIZE, + _("Attempt to open script file again: \"%s %s\"\n"), + argv[-1], argv[0]); + mch_errmsg((const char *)IObuff); mch_exit(2); } int error; @@ -1065,11 +1064,10 @@ scripterror: scriptin[0] = stdin_dup; } else if ((scriptin[0] = file_open_new( &error, argv[0], kFileReadOnly|kFileNonBlocking, 0)) == NULL) { - mch_errmsg(_("Cannot open for reading: \"")); - mch_errmsg(argv[0]); - mch_errmsg("\": "); - mch_errmsg(os_strerror(error)); - mch_errmsg("\n"); + vim_snprintf((char *)IObuff, IOSIZE, + _("Cannot open for reading: \"%s\": %s\n"), + argv[0], os_strerror(error)); + mch_errmsg((const char *)IObuff); mch_exit(2); } save_typebuf(); diff --git a/src/nvim/message.c b/src/nvim/message.c index 4cd0db21e8..c970f92666 100644 --- a/src/nvim/message.c +++ b/src/nvim/message.c @@ -2245,10 +2245,9 @@ static int do_more_prompt(int typed_char) * yet. When stderr can't be used, collect error messages until the GUI has * started and they can be displayed in a message box. */ -void mch_errmsg(char *str) +void mch_errmsg(const char *const str) + FUNC_ATTR_NONNULL_ALL { - int len; - #ifdef UNIX /* On Unix use stderr if it's a tty. * When not going to start the GUI also use stderr. @@ -2262,14 +2261,13 @@ void mch_errmsg(char *str) /* avoid a delay for a message that isn't there */ emsg_on_display = FALSE; - len = (int)STRLEN(str) + 1; + const size_t len = strlen(str) + 1; if (error_ga.ga_data == NULL) { ga_set_growsize(&error_ga, 80); error_ga.ga_itemsize = 1; } ga_grow(&error_ga, len); - memmove((char_u *)error_ga.ga_data + error_ga.ga_len, - (char_u *)str, len); + memmove(error_ga.ga_data + error_ga.ga_len, str, len); #ifdef UNIX /* remove CR characters, they are displayed */ { -- cgit From 78bc52ea5397c092d01cd08296fe1dc85d998329 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 3 Dec 2017 16:50:37 +0300 Subject: getchar: Move REMAP_… values into a enum MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/nvim/getchar.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/nvim/getchar.h b/src/nvim/getchar.h index 5c92067a23..38a2e75663 100644 --- a/src/nvim/getchar.h +++ b/src/nvim/getchar.h @@ -6,15 +6,15 @@ #include "nvim/buffer_defs.h" #include "nvim/ex_cmds_defs.h" -/// Values for "noremap" argument of ins_typebuf(). Also used for -/// map->m_noremap and menu->noremap[]. -/// @addtogroup REMAP_VALUES -/// @{ -#define REMAP_YES 0 ///< allow remapping -#define REMAP_NONE -1 ///< no remapping -#define REMAP_SCRIPT -2 ///< remap script-local mappings only -#define REMAP_SKIP -3 ///< no remapping for first char -/// @} +/// Values for "noremap" argument of ins_typebuf() +/// +/// Also used for map->m_noremap and menu->noremap[]. +enum { + REMAP_YES = 0, ///< Allow remapping. + REMAP_NONE = -1, ///< No remapping. + REMAP_SCRIPT = -2, ///< Remap script-local mappings only. + REMAP_SKIP = -3, ///< No remapping for first char. +} RemapValues; #define KEYLEN_PART_KEY -1 /* keylen value for incomplete key-code */ #define KEYLEN_PART_MAP -2 /* keylen value for incomplete mapping */ -- cgit From 387fbcd95cade4b0c037d18f404944676a59db09 Mon Sep 17 00:00:00 2001 From: b-r-o-c-k Date: Sat, 14 Apr 2018 14:21:36 -0500 Subject: win: Fix reading from stdin * Reading from stdin on Windows is fixed in the same way as it was in #8267. * The file_read function was returning without filling the destination buffer when it was called with a non-blocking file descriptor. --- src/nvim/main.c | 11 +++++++++++ src/nvim/os/fileio.c | 10 +++++----- 2 files changed, 16 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/nvim/main.c b/src/nvim/main.c index 067184c8c7..4f9a5a979f 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1096,6 +1096,17 @@ scripterror: int error; if (STRCMP(argv[0], "-") == 0) { const int stdin_dup_fd = os_dup(STDIN_FILENO); +#ifdef WIN32 + // On Windows, replace the original stdin with the + // console input handle. + close(STDIN_FILENO); + const HANDLE conin_handle = + CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES)NULL, + OPEN_EXISTING, 0, (HANDLE)NULL); + const int conin_fd = _open_osfhandle(conin_handle, _O_RDONLY); + assert(conin_fd == STDIN_FILENO); +#endif FileDescriptor *const stdin_dup = file_open_fd_new( &error, stdin_dup_fd, kFileReadOnly|kFileNonBlocking); assert(stdin_dup != NULL); diff --git a/src/nvim/os/fileio.c b/src/nvim/os/fileio.c index 0ef9307a5a..ccf35fd57c 100644 --- a/src/nvim/os/fileio.c +++ b/src/nvim/os/fileio.c @@ -282,6 +282,7 @@ ptrdiff_t file_read(FileDescriptor *const fp, char *const ret_buf, char *buf = ret_buf; size_t read_remaining = size; RBuffer *const rv = fp->rv; + bool called_read = false; while (read_remaining) { const size_t rv_size = rbuffer_size(rv); if (rv_size > 0) { @@ -289,7 +290,9 @@ ptrdiff_t file_read(FileDescriptor *const fp, char *const ret_buf, buf += rsize; read_remaining -= rsize; } - if (fp->eof) { + if (fp->eof + // Allow only at most one os_read[v] call. + || (called_read && fp->non_blocking)) { break; } if (read_remaining) { @@ -343,10 +346,7 @@ ptrdiff_t file_read(FileDescriptor *const fp, char *const ret_buf, } } #endif - } - if (fp->non_blocking) { - // Allow only at most one os_read[v] call. - break; + called_read = true; } } return (ptrdiff_t)(size - read_remaining); -- cgit