ssize_t readline_common()

in system/readline/readline_common.c [474:738]


ssize_t readline_common(FAR struct rl_common_s *vtbl, FAR char *buf,
                        int buflen)
{
  int escape;
  int nch;
#ifdef CONFIG_READLINE_CMD_HISTORY
  int i;
#endif

  /* Sanity checks */

  DEBUGASSERT(buf && buflen > 0);

  if (buflen < 2)
    {
      *buf = '\0';
      return 0;
    }

  /* <esc>[K is the VT100 command that erases to the end of the line. */

#ifdef CONFIG_READLINE_ECHO
  RL_WRITE(vtbl, g_erasetoeol, sizeof(g_erasetoeol));
#endif

  /* Read characters until we have a full line. On each the loop we must
   * be assured that there are two free bytes in the line buffer:  One for
   * the next character and one for the null terminator.
   */

  escape = 0;
  nch    = 0;

  for (; ; )
    {
      /* Get the next character. readline_rawgetc() returns EOF on any
       * errors or at the end of file.
       */

      int ch = RL_GETC(vtbl);

      /* Check for end-of-file or read error */

      if (ch == EOF)
        {
          /* Did we already received some data? */

          if (nch > 0)
            {
              /* Yes.. Terminate the line (which might be zero length)
               * and return the data that was received.  The end-of-file
               * or error condition will be reported next time.
               */

              buf[nch] = '\0';
              return nch;
            }

          return EOF;
        }

      /* Are we processing a VT100 escape sequence */

      else if (escape)
        {
          /* Yes, is it an <esc>[, 3 byte sequence */

          if (ch != ASCII_LBRACKET || escape == 2)
            {
              /* We are finished with the escape sequence */

#ifdef CONFIG_READLINE_CMD_HISTORY
              /* Intercept up and down arrow keys */

              if (g_cmdhist.len > 0)
                {
                  if (ch == 'A') /* up arrow */
                    {
                      /* Go to the past command in history */

                      g_cmdhist.offset--;

                      if (-g_cmdhist.offset >= g_cmdhist.len)
                        {
                          g_cmdhist.offset = -(g_cmdhist.len - 1);
                        }
                    }
                  else if (ch == 'B') /* down arrow */
                    {
                      /* Go to the recent command in history */

                      g_cmdhist.offset++;

                      if (g_cmdhist.offset > 1)
                        {
                          g_cmdhist.offset = 1;
                        }
                    }

                  /* Clear out current command from the prompt */

                  while (nch > 0)
                    {
                      nch--;

#ifdef CONFIG_READLINE_ECHO
                      RL_PUTC(vtbl, ASCII_BS);
                      RL_WRITE(vtbl, g_erasetoeol, sizeof(g_erasetoeol));
#endif
                    }

                  if (g_cmdhist.offset != 1)
                    {
                      int idx = g_cmdhist.head + g_cmdhist.offset;

                      /* Circular buffer wrap around */

                      if (idx < 0)
                        {
                          idx = idx + RL_CMDHIST_LEN;
                        }
                      else if (idx >= RL_CMDHIST_LEN)
                        {
                          idx = idx - RL_CMDHIST_LEN;
                        }

                      for (i = 0; g_cmdhist.buf[idx][i] != '\0'; i++)
                        {
                          buf[nch++] = g_cmdhist.buf[idx][i];
                          RL_PUTC(vtbl, g_cmdhist.buf[idx][i]);
                        }

                      buf[nch] = '\0';
                    }
                }
#endif /* CONFIG_READLINE_CMD_HISTORY */

              escape = 0;
              ch = 'a';
            }
          else
            {
              /* The next character is the end of a 3-byte sequence.
               * NOTE:  Some of the <esc>[ sequences are longer than
               * 3-bytes, but I have not encountered any in normal use
               * yet and, so, have not provided the decoding logic.
               */

              escape = 2;
            }
        }

      /* Check for backspace
       *
       * There are several notions of backspace, for an elaborate summary see
       * http://www.ibb.net/~anne/keyboard.html. There is no clean solution.
       * Here both DEL and backspace are treated like backspace here.  The
       * Unix/Linux screen terminal by default outputs  DEL (0x7f) when the
       * backspace key is pressed.
       */

      else if (ch == ASCII_BS || ch == ASCII_DEL)
        {
          /* Eliminate that last character in the buffer. */

          if (nch > 0)
            {
              nch--;

#ifdef CONFIG_READLINE_ECHO
              /* Echo the backspace character on the console.  Always output
               * the backspace character because the VT100 terminal doesn't
               * understand DEL properly.
               */

              RL_PUTC(vtbl, ASCII_BS);
              RL_WRITE(vtbl, g_erasetoeol, sizeof(g_erasetoeol));
#endif
            }
        }

      /* Check for the beginning of a VT100 escape sequence */

      else if (ch == ASCII_ESC)
        {
          /* The next character is escaped */

          escape = 1;
        }

      /* Check for end-of-line.  This is tricky only in that some
       * environments may return CR as end-of-line, others LF, and
       * others both.
       */

      else if (ch == '\n')
        {
#ifdef CONFIG_READLINE_CMD_HISTORY
          /* Save history of command, only if there was something
           * typed besides return character.
           */

          if (nch >= 1)
            {
              /* If this command is the one at the top of the circular
               * buffer, don't save it again.
               */

              if (strncmp(buf, g_cmdhist.buf[g_cmdhist.head], nch + 1) != 0)
                {
                  g_cmdhist.head = (g_cmdhist.head + 1) % RL_CMDHIST_LEN;

                  for (i = 0; (i < nch) && i < (RL_CMDHIST_LINELEN - 1); i++)
                    {
                      g_cmdhist.buf[g_cmdhist.head][i] = buf[i];
                    }

                  g_cmdhist.buf[g_cmdhist.head][i] = '\0';

                  if (g_cmdhist.len < RL_CMDHIST_LEN)
                    {
                      g_cmdhist.len++;
                    }
                }

              g_cmdhist.offset = 1;
            }
#endif /* CONFIG_READLINE_CMD_HISTORY */

          /* The newline is stored in the buffer along with the null
           * terminator.
           */

          buf[nch++] = '\n';
          buf[nch]   = '\0';

          return nch;
        }

      /* Otherwise, put the character in the line buffer if the
       * character is not a control byte
       */

      else if (!iscntrl(ch & 0xff))
        {
          buf[nch++] = ch;

          /* Check if there is room for another character and the line's
           * null terminator.  If not then we have to end the line now.
           */

          if (nch + 1 >= buflen)
            {
              buf[nch] = '\0';
              return nch;
            }
        }
#ifdef CONFIG_READLINE_TABCOMPLETION
      else if (ch == '\t') /* TAB character */
        {
          tab_completion(vtbl, buf, buflen, &nch);
        }
#endif
    }
}