diff options
Diffstat (limited to 'src/nvim/getchar.c')
| -rw-r--r-- | src/nvim/getchar.c | 94 | 
1 files changed, 46 insertions, 48 deletions
diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index 03929a58b6..98164b2653 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -48,8 +48,14 @@  #include "nvim/event/loop.h"  #include "nvim/os/input.h"  #include "nvim/os/os.h" +#include "nvim/os/fileio.h"  #include "nvim/api/private/handle.h" + +/// Index in scriptin +static int curscript = 0; +FileDescriptor *scriptin[NSCRIPT] = { NULL }; +  /*   * These buffers are used for storing:   * - stuffed characters: A command that is translated into another command. @@ -1243,10 +1249,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(); @@ -1296,7 +1305,7 @@ static void closescript(void)    free_typebuf();    typebuf = saved_typebuf[curscript]; -  fclose(scriptin[curscript]); +  file_free(scriptin[curscript], false);    scriptin[curscript] = NULL;    if (curscript > 0)      --curscript; @@ -2336,9 +2345,8 @@ inchar (      int tb_change_cnt  )  { -  int len = 0;                      /* init for GCC */ -  int retesc = FALSE;               /* return ESC with gotint */ -  int script_char; +  int len = 0;  // Init for GCC. +  int retesc = false;  // Return ESC with gotint.    if (wait_time == -1L || wait_time > 100L) {      // flush output before waiting @@ -2356,45 +2364,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]; @@ -2407,21 +2408,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);  }  | 
