2014-02-02 17:25:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/system/cle/cle.c
|
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2014-02-02 17:25:53 +01:00
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-02-02 17:25:53 +01:00
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2014-02-02 17:25:53 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2020-11-23 23:03:51 +01:00
|
|
|
#include <inttypes.h>
|
2014-10-08 19:34:22 +02:00
|
|
|
#include <stdarg.h>
|
2014-02-02 17:25:53 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/ascii.h>
|
|
|
|
#include <nuttx/vt100.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "system/cle.h"
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Control characters */
|
|
|
|
|
|
|
|
#undef CTRL
|
|
|
|
#define CTRL(a) ((a) & 0x1f)
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
#define CLE_BEL(priv) cle_putch(priv,CTRL('G'))
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/* Sizes of things */
|
|
|
|
|
|
|
|
#define TABSIZE 8 /* A TAB is eight characters */
|
|
|
|
#define TABMASK 7 /* Mask for TAB alignment */
|
|
|
|
#define NEXT_TAB(p) (((p) + TABSIZE) & ~TABMASK)
|
|
|
|
|
|
|
|
/* Debug */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
#ifndef CONFIG_SYSTEM_CLE_DEBUGLEVEL
|
|
|
|
# define CONFIG_SYSTEM_CLE_DEBUGLEVEL 0
|
2014-02-02 17:25:53 +01:00
|
|
|
#endif
|
|
|
|
|
2020-05-04 12:52:59 +02:00
|
|
|
#if CONFIG_SYSTEM_CLE_DEBUGLEVEL > 0
|
|
|
|
# define cledbg cle_debug
|
2014-02-02 17:25:53 +01:00
|
|
|
#else
|
2020-05-04 12:52:59 +02:00
|
|
|
# define cledbg _none
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_SYSTEM_CLE_DEBUGLEVEL > 1
|
|
|
|
# define cleinfo cle_debug
|
|
|
|
#else
|
|
|
|
# define cleinfo _none
|
2014-02-02 17:25:53 +01:00
|
|
|
#endif
|
|
|
|
|
2019-10-17 19:20:20 +02:00
|
|
|
#ifdef CONFIG_SYSTEM_COLOR_CLE
|
|
|
|
# define COLOR_PROMPT VT100_YELLOW
|
|
|
|
# define COLOR_COMMAND VT100_CYAN
|
|
|
|
# define COLOR_OUTPUT VT100_GREEN
|
|
|
|
#endif
|
2018-08-06 00:05:59 +02:00
|
|
|
|
2014-02-02 17:25:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
2019-10-11 18:47:29 +02:00
|
|
|
|
2014-02-02 17:25:53 +01:00
|
|
|
/* VI Key Bindings */
|
|
|
|
|
|
|
|
enum cle_key_e
|
|
|
|
{
|
|
|
|
KEY_BEGINLINE = CTRL('A'), /* Move cursor to start of current line */
|
|
|
|
KEY_LEFT = CTRL('B'), /* Move left one character */
|
2014-02-03 17:56:52 +01:00
|
|
|
KEY_DEL = CTRL('D'), /* Delete a single character at the cursor position */
|
2014-02-02 17:25:53 +01:00
|
|
|
KEY_ENDLINE = CTRL('E'), /* Move cursor to end of current line */
|
|
|
|
KEY_RIGHT = CTRL('F'), /* Move right one character */
|
|
|
|
KEY_DELLEFT = CTRL('H'), /* Delete character, left (backspace) */
|
|
|
|
KEY_DELEOL = CTRL('K'), /* Delete to the end of the line */
|
2019-10-11 18:47:29 +02:00
|
|
|
KEY_CLRSCR = CTRL('L'), /* Clear the screen */
|
2018-08-06 00:05:59 +02:00
|
|
|
KEY_DN = CTRL('N'), /* Cursor down */
|
|
|
|
KEY_UP = CTRL('P'), /* Cursor up */
|
2014-02-02 17:25:53 +01:00
|
|
|
KEY_DELLINE = CTRL('U'), /* Delete the entire line */
|
2018-08-06 00:05:59 +02:00
|
|
|
KEY_QUOTE = '\\' /* The next character is quote (use literal value) */
|
2014-02-02 17:25:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This structure describes the overall state of the editor */
|
|
|
|
|
|
|
|
struct cle_s
|
|
|
|
{
|
|
|
|
uint16_t curpos; /* Current cursor position */
|
|
|
|
uint16_t cursave; /* Saved cursor position */
|
2014-02-02 19:24:45 +01:00
|
|
|
uint16_t row; /* This is the row that we are editing in */
|
2014-02-02 17:25:53 +01:00
|
|
|
uint16_t coloffs; /* Left cursor offset */
|
|
|
|
uint16_t linelen; /* Size of the line buffer */
|
|
|
|
uint16_t nchars; /* Size of data in the line buffer */
|
2021-11-14 19:39:35 +01:00
|
|
|
int infd; /* Input file handle */
|
|
|
|
int outfd; /* Output file handle */
|
2014-02-02 17:25:53 +01:00
|
|
|
FAR char *line; /* Line buffer */
|
2019-10-11 18:47:29 +02:00
|
|
|
FAR const char *prompt; /* Prompt, in case we have to re-print it */
|
2014-02-02 17:25:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-04 12:52:59 +02:00
|
|
|
#if CONFIG_SYSTEM_CLE_DEBUGLEVEL > 0
|
2022-12-12 20:06:32 +01:00
|
|
|
static void cle_debug(FAR const char *fmt, ...) printf_like(1, 2);
|
2014-10-08 19:34:22 +02:00
|
|
|
#endif
|
|
|
|
|
2014-02-02 17:25:53 +01:00
|
|
|
/* Low-level display and data entry functions */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
static void cle_write(FAR struct cle_s *priv, FAR const char *buffer,
|
2014-02-02 17:25:53 +01:00
|
|
|
uint16_t buflen);
|
2014-02-02 19:24:45 +01:00
|
|
|
static void cle_putch(FAR struct cle_s *priv, char ch);
|
|
|
|
static int cle_getch(FAR struct cle_s *priv);
|
|
|
|
static void cle_cursoron(FAR struct cle_s *priv);
|
|
|
|
static void cle_cursoroff(FAR struct cle_s *priv);
|
|
|
|
static void cle_setcursor(FAR struct cle_s *priv, uint16_t column);
|
|
|
|
static int cle_getcursor(FAR struct cle_s *priv, uint16_t *prow,
|
2014-02-02 17:25:53 +01:00
|
|
|
uint16_t *pcolumn);
|
2014-02-02 19:24:45 +01:00
|
|
|
static void cle_clrtoeol(FAR struct cle_s *priv);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/* Editor function */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
static bool cle_opentext(FAR struct cle_s *priv, uint16_t pos,
|
2014-02-02 17:25:53 +01:00
|
|
|
uint16_t increment);
|
2019-10-11 18:47:29 +02:00
|
|
|
static void cle_closetext(FAR struct cle_s *priv, uint16_t pos,
|
|
|
|
uint16_t size);
|
2014-02-02 19:24:45 +01:00
|
|
|
static void cle_showtext(FAR struct cle_s *priv);
|
|
|
|
static void cle_insertch(FAR struct cle_s *priv, char ch);
|
|
|
|
static int cle_editloop(FAR struct cle_s *priv);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-17 19:20:20 +02:00
|
|
|
#ifdef CONFIG_SYSTEM_CLE_CMD_HISTORY
|
|
|
|
/* Command history
|
|
|
|
*
|
|
|
|
* g_cmd_history[][] Circular buffer
|
|
|
|
* g_cmd_history_head Head of the circular buffer, most recent
|
|
|
|
* command
|
|
|
|
* g_cmd_history_steps_from_head Offset from head
|
|
|
|
* g_cmd_history_len Number of elements in the circular buffer
|
|
|
|
*
|
|
|
|
* REVISIT: These globals will *not* work in an environment where there
|
|
|
|
* are multiple copies if the NSH shell! Use of global variables is not
|
|
|
|
* thread safe! These settings should, at least, be semaphore protected so
|
|
|
|
* that the integrity of the data is assured, even though commands from
|
|
|
|
* different sessions may be intermixed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static char g_cmd_history[CONFIG_SYSTEM_CLE_CMD_HISTORY_LEN]
|
|
|
|
[CONFIG_SYSTEM_CLE_CMD_HISTORY_LINELEN];
|
|
|
|
static int g_cmd_history_head = -1;
|
|
|
|
static int g_cmd_history_steps_from_head = 1;
|
|
|
|
static int g_cmd_history_len = 0;
|
|
|
|
|
|
|
|
#endif /* CONFIG_SYSTEM_CLE_CMD_HISTORY */
|
|
|
|
|
2014-02-02 17:25:53 +01:00
|
|
|
/* VT100 escape sequences */
|
|
|
|
|
2014-02-02 20:22:11 +01:00
|
|
|
static const char g_cursoron[] = VT100_CURSORON;
|
|
|
|
static const char g_cursoroff[] = VT100_CURSOROFF;
|
|
|
|
static const char g_getcursor[] = VT100_GETCURSOR;
|
|
|
|
static const char g_erasetoeol[] = VT100_CLEAREOL;
|
|
|
|
static const char g_fmtcursorpos[] = VT100_FMT_CURSORPOS;
|
2019-10-11 18:47:29 +02:00
|
|
|
static const char g_clrscr[] = VT100_CLEARSCREEN;
|
|
|
|
static const char g_home[] = VT100_CURSORHOME;
|
|
|
|
#ifdef CONFIG_SYSTEM_COLOR_CLE
|
2019-10-17 19:20:20 +02:00
|
|
|
static const char g_setcolor[] = VT100_FMT_FORE_COLOR;
|
2019-10-11 18:47:29 +02:00
|
|
|
#endif
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-08 19:34:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_debug
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Print a debug message to the syslog
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-04 12:52:59 +02:00
|
|
|
#if CONFIG_SYSTEM_CLE_DEBUGLEVEL > 0
|
2021-02-18 23:50:32 +01:00
|
|
|
static void cle_debug(FAR const char *fmt, ...)
|
2014-10-08 19:34:22 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
/* Let vsyslog do the real work */
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2021-02-18 23:50:32 +01:00
|
|
|
vsyslog(LOG_DEBUG, fmt, ap);
|
2014-10-08 19:34:22 +02:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-02 17:25:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_write
|
|
|
|
*
|
|
|
|
* Description:
|
2014-02-02 23:13:40 +01:00
|
|
|
* Write a sequence of bytes to the console output device.
|
2014-02-02 17:25:53 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
static void cle_write(FAR struct cle_s *priv, FAR const char *buffer,
|
2014-02-02 20:22:11 +01:00
|
|
|
uint16_t buflen)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
ssize_t nwritten;
|
|
|
|
|
2014-02-02 23:13:40 +01:00
|
|
|
/* Loop until all bytes have been successfully written (or until a
|
|
|
|
* unrecoverable error is encountered)
|
2014-02-02 17:25:53 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2019-11-04 18:35:18 +01:00
|
|
|
/* Put the next gulp */
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2021-11-14 19:39:35 +01:00
|
|
|
nwritten = write(priv->outfd, buffer, buflen);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/* Handle write errors. write() should neve return 0. */
|
|
|
|
|
|
|
|
if (nwritten <= 0)
|
|
|
|
{
|
|
|
|
/* EINTR is not really an error; it simply means that a signal was
|
|
|
|
* received while waiting for write.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int errcode = errno;
|
|
|
|
if (nwritten == 0 || errcode != EINTR)
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
cledbg("ERROR: write to stdout failed: %d\n", errcode);
|
2014-02-02 17:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrement the count of bytes remaining to be sent (to handle the
|
|
|
|
* case of a partial write)
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2021-11-14 19:39:35 +01:00
|
|
|
buffer += nwritten;
|
|
|
|
buflen -= nwritten;
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-14 19:39:35 +01:00
|
|
|
while (buflen > 0);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_putch
|
|
|
|
*
|
|
|
|
* Description:
|
2014-02-02 23:13:40 +01:00
|
|
|
* Write a single character to the console output device.
|
2014-02-02 17:25:53 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
static void cle_putch(FAR struct cle_s *priv, char ch)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_write(priv, &ch, 1);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_getch
|
|
|
|
*
|
|
|
|
* Description:
|
2014-02-02 23:13:40 +01:00
|
|
|
* Get a single character from the console input device.
|
2014-02-02 17:25:53 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
static int cle_getch(FAR struct cle_s *priv)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
char buffer;
|
|
|
|
ssize_t nread;
|
|
|
|
|
|
|
|
/* Loop until we successfully read a character (or until an unexpected
|
|
|
|
* error occurs).
|
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Read one character from the incoming stream */
|
|
|
|
|
2021-11-14 19:39:35 +01:00
|
|
|
nread = read(priv->infd, &buffer, 1);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/* Check for error or end-of-file. */
|
|
|
|
|
|
|
|
if (nread <= 0)
|
|
|
|
{
|
|
|
|
/* EINTR is not really an error; it simply means that a signal we
|
|
|
|
* received while waiting for input.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int errcode = errno;
|
|
|
|
if (nread == 0 || errcode != EINTR)
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
cledbg("ERROR: read from stdin failed: %d\n", errcode);
|
2014-02-02 17:25:53 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nread < 1);
|
|
|
|
|
|
|
|
/* On success, return the character that was read */
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
cleinfo("Returning: %c[%02x]\n", isprint(buffer) ? buffer : '.', buffer);
|
2014-02-02 17:25:53 +01:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_cursoron
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Turn on the cursor
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
static void cle_cursoron(FAR struct cle_s *priv)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
/* Send the VT100 CURSORON command */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_write(priv, g_cursoron, sizeof(g_cursoron));
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_cursoroff
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Turn off the cursor
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
static void cle_cursoroff(FAR struct cle_s *priv)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
/* Send the VT100 CURSOROFF command */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_write(priv, g_cursoroff, sizeof(g_cursoroff));
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_setcursor
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Move the current cursor position to position (row,col)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
static void cle_setcursor(FAR struct cle_s *priv, uint16_t column)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
char buffer[16];
|
|
|
|
int len;
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
cleinfo("row=%d column=%d offset=%d\n", priv->row, column, priv->coloffs);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/* Format the cursor position command. The origin is (1,1). */
|
|
|
|
|
2019-10-11 18:47:29 +02:00
|
|
|
len = snprintf(buffer, 16, g_fmtcursorpos,
|
|
|
|
priv->row, column + priv->coloffs);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/* Send the VT100 CURSORPOS command */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_write(priv, buffer, len);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
|
2019-10-11 18:47:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_setcolor
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set foreground color
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSTEM_COLOR_CLE
|
|
|
|
static void cle_setcolor(FAR struct cle_s *priv, uint8_t color)
|
|
|
|
{
|
|
|
|
char buffer[16];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = snprintf(buffer, 16, g_setcolor, color);
|
|
|
|
cle_write(priv, buffer, len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_outputprompt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send the prompt to the screen
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void cle_outputprompt(FAR struct cle_s *priv)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_SYSTEM_COLOR_CLE
|
|
|
|
cle_setcolor(priv, COLOR_PROMPT);
|
|
|
|
#endif
|
|
|
|
cle_write(priv, priv->prompt, strlen(priv->prompt));
|
|
|
|
#ifdef CONFIG_SYSTEM_COLOR_CLE
|
|
|
|
cle_setcolor(priv, COLOR_OUTPUT);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_clrscr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Clear the screen, and re-establish the prompt
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void cle_clrscr(FAR struct cle_s *priv)
|
|
|
|
{
|
|
|
|
/* Send the VT100 CLEARSCREEN command */
|
|
|
|
|
|
|
|
cle_write(priv, g_clrscr, sizeof(g_clrscr));
|
|
|
|
cle_write(priv, g_home, sizeof(g_home));
|
|
|
|
priv->row = 1;
|
|
|
|
cle_outputprompt(priv);
|
|
|
|
}
|
|
|
|
|
2014-02-02 17:25:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_getcursor
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get the current cursor position.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
static int cle_getcursor(FAR struct cle_s *priv, FAR uint16_t *prow,
|
2014-02-02 20:22:11 +01:00
|
|
|
FAR uint16_t *pcolumn)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
uint32_t row;
|
|
|
|
uint32_t column;
|
|
|
|
int nbad;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
/* Send the VT100 GETCURSOR command */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_write(priv, g_getcursor, sizeof(g_getcursor));
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2019-11-04 18:35:18 +01:00
|
|
|
/* We expect to get back ESC[v;hR where v is the row and h is the column.
|
|
|
|
* once the sequence has started we don't expect any characters
|
|
|
|
* interspersed.
|
|
|
|
*/
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2019-11-04 18:35:18 +01:00
|
|
|
for (nbad = 0; nbad < 10; nbad++)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2019-11-04 18:35:18 +01:00
|
|
|
/* Look for initial ESC */
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
ch = cle_getch(priv);
|
2019-11-04 18:35:18 +01:00
|
|
|
if (ch != ASCII_ESC)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2019-11-04 18:35:18 +01:00
|
|
|
continue;
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
|
2019-11-04 18:35:18 +01:00
|
|
|
/* Have ESC, now we expect '[' */
|
2014-02-02 19:24:45 +01:00
|
|
|
|
|
|
|
ch = cle_getch(priv);
|
2019-11-04 18:35:18 +01:00
|
|
|
if (ch != '[')
|
2014-02-02 19:24:45 +01:00
|
|
|
{
|
2019-11-04 18:35:18 +01:00
|
|
|
continue;
|
2014-02-02 19:24:45 +01:00
|
|
|
}
|
|
|
|
|
2019-11-04 18:35:18 +01:00
|
|
|
/* ...now we expect to see a numeric value terminated with ';' */
|
2014-02-02 19:24:45 +01:00
|
|
|
|
2019-11-04 18:35:18 +01:00
|
|
|
row = 0;
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2019-11-04 18:35:18 +01:00
|
|
|
while (isdigit(ch = cle_getch(priv)))
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2019-11-04 18:35:18 +01:00
|
|
|
row = row * 10 + (ch - '0');
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
2019-11-04 18:35:18 +01:00
|
|
|
|
|
|
|
if (ch != ';')
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2019-11-04 18:35:18 +01:00
|
|
|
continue;
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
2019-11-04 18:35:18 +01:00
|
|
|
|
|
|
|
/* ...now we expect to see another numeric value terminated with 'R' */
|
|
|
|
|
|
|
|
column = 0;
|
|
|
|
while (isdigit(ch = cle_getch(priv)))
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2019-11-04 18:35:18 +01:00
|
|
|
column = 10 * column + (ch - '0');
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
|
2019-11-04 18:35:18 +01:00
|
|
|
/* ...we are done */
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2020-11-23 23:03:51 +01:00
|
|
|
cleinfo("row=%" PRId32 " column=%" PRId32 "\n", row, column);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2019-11-04 18:35:18 +01:00
|
|
|
/* Make sure that the values are within range */
|
|
|
|
|
|
|
|
if (row <= UINT16_MAX && column <= UINT16_MAX)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2019-11-04 18:35:18 +01:00
|
|
|
*prow = row;
|
|
|
|
*pcolumn = column;
|
|
|
|
return OK;
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-04 18:35:18 +01:00
|
|
|
return -ERANGE;
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 18:35:18 +01:00
|
|
|
return -EINVAL;
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_clrtoeol
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Clear the display from the current cursor position to the end of the
|
|
|
|
* current line.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
static void cle_clrtoeol(FAR struct cle_s *priv)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
/* Send the VT100 ERASETOEOL command */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_write(priv, g_erasetoeol, sizeof(g_erasetoeol));
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_opentext
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Make space for new text of size 'increment' at the specified cursor
|
2014-02-02 23:13:40 +01:00
|
|
|
* position. This function will not allow text grow beyond ('linelen' - 1)
|
2014-02-02 19:53:58 +01:00
|
|
|
* in size.
|
2014-02-02 17:25:53 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-02 20:22:11 +01:00
|
|
|
static bool cle_opentext(FAR struct cle_s *priv, uint16_t pos,
|
|
|
|
uint16_t increment)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
cleinfo("pos=%ld increment=%ld\n", (long)pos, (long)increment);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/* Check if there is space in the line buffer to open up a region the size
|
|
|
|
* of 'increment'
|
|
|
|
*/
|
|
|
|
|
2014-02-02 19:53:58 +01:00
|
|
|
if (priv->nchars + increment >= priv->linelen)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2014-02-02 19:53:58 +01:00
|
|
|
CLE_BEL(priv);
|
2014-02-02 17:25:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move text to make space for new text of size 'increment' at the current
|
|
|
|
* cursor position
|
|
|
|
*/
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
for (i = priv->nchars - 1; i >= pos; i--)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
priv->line[i + increment] = priv->line[i];
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust end of file position */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
priv->nchars += increment;
|
2014-02-02 17:25:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_closetext
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Delete a region in the line buffer by copying the end of the line buffer
|
|
|
|
* over the deleted region and adjusting the size of the region.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-13 18:35:03 +01:00
|
|
|
static void cle_closetext(FAR struct cle_s *priv, uint16_t pos,
|
|
|
|
uint16_t size)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
cleinfo("pos=%ld size=%ld\n", (long)pos, (long)size);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/* Close up the gap to remove 'size' characters at 'pos' */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
for (i = pos + size; i < priv->nchars; i++)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
priv->line[i - size] = priv->line[i];
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust sizes and positions */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
priv->nchars -= size;
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2020-04-27 14:24:43 +02:00
|
|
|
if (priv->curpos > pos)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2020-04-27 14:24:43 +02:00
|
|
|
/* Check if the cursor position is beyond the deleted region */
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2020-04-27 14:24:43 +02:00
|
|
|
if (priv->curpos - pos > size)
|
|
|
|
{
|
|
|
|
/* Yes... just subtract the size of the deleted region */
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2020-04-27 14:24:43 +02:00
|
|
|
priv->curpos -= size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* What if the position is within the deleted region? Set it to
|
|
|
|
* the beginning of the deleted region.
|
|
|
|
*/
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2020-04-27 14:24:43 +02:00
|
|
|
priv->curpos = pos;
|
|
|
|
}
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_showtext
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Update the display based on the last operation. This function is
|
2014-02-02 23:13:40 +01:00
|
|
|
* called at the beginning of the editor loop.
|
2014-02-02 17:25:53 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
static void cle_showtext(FAR struct cle_s *priv)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
uint16_t column;
|
|
|
|
uint16_t tabcol;
|
|
|
|
|
|
|
|
/* Turn off the cursor during the update. */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_cursoroff(priv);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/* Set the cursor position to the beginning of this row */
|
|
|
|
|
2019-10-11 18:47:29 +02:00
|
|
|
#ifdef CONFIG_SYSTEM_COLOR_CLE
|
|
|
|
cle_setcolor(priv, COLOR_COMMAND);
|
|
|
|
#endif
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_setcursor(priv, 0);
|
|
|
|
cle_clrtoeol(priv);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/* Loop for each column */
|
|
|
|
|
2014-02-02 19:53:58 +01:00
|
|
|
for (column = 0; column < priv->nchars; )
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
/* Perform TAB expansion */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
if (priv->line[column] == '\t')
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
tabcol = NEXT_TAB(column);
|
2014-02-02 19:24:45 +01:00
|
|
|
if (tabcol < priv->linelen)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
for (; column < tabcol; column++)
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_putch(priv, ' ');
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Break out of the loop... there is nothing left on the
|
|
|
|
* line but whitespace.
|
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the normal character to the display */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_putch(priv, priv->line[column]);
|
2014-02-02 17:25:53 +01:00
|
|
|
column++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 18:47:29 +02:00
|
|
|
#ifdef CONFIG_SYSTEM_COLOR_CLE
|
|
|
|
cle_setcolor(priv, COLOR_OUTPUT);
|
|
|
|
#endif
|
|
|
|
|
2014-02-02 17:25:53 +01:00
|
|
|
/* Turn the cursor back on */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_cursoron(priv);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_insertch
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Insert one character into the line buffer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
static void cle_insertch(FAR struct cle_s *priv, char ch)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2020-11-23 23:03:51 +01:00
|
|
|
cleinfo("curpos=%" PRId16 " ch=%c[%02x]\n", priv->curpos,
|
2019-10-11 18:47:29 +02:00
|
|
|
isprint(ch) ? ch : '.', ch);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/* Make space in the buffer for the new character */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
if (cle_opentext(priv, priv->curpos, 1))
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
/* Add the new character to the buffer */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
priv->line[priv->curpos++] = ch;
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cle_editloop
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Command line editor loop
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
static int cle_editloop(FAR struct cle_s *priv)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
|
|
|
/* Loop while we are in command mode */
|
|
|
|
|
2019-10-11 18:47:29 +02:00
|
|
|
for (; ; )
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2020-03-13 18:35:03 +01:00
|
|
|
#if 1 /* Perhaps here should be a config switch */
|
|
|
|
char state = 0;
|
|
|
|
#endif
|
2014-02-02 17:25:53 +01:00
|
|
|
int ch;
|
|
|
|
|
|
|
|
/* Make sure that the display reflects the current state */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_showtext(priv);
|
|
|
|
cle_setcursor(priv, priv->curpos);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/* Get the next character from the input */
|
|
|
|
|
2018-08-06 00:05:59 +02:00
|
|
|
#if 1 /* Perhaps here should be a config switch */
|
|
|
|
/* Simple decode of some VT100/xterm codes: left/right, up/dn,
|
|
|
|
* home/end, del
|
|
|
|
*/
|
|
|
|
|
2019-10-11 18:47:29 +02:00
|
|
|
/* loop till we have a ch */
|
2018-08-06 00:05:59 +02:00
|
|
|
|
2019-10-11 18:47:29 +02:00
|
|
|
for (; ; )
|
|
|
|
{
|
|
|
|
ch = cle_getch(priv);
|
|
|
|
if (ch < 0)
|
|
|
|
{
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
else if (state != 0)
|
|
|
|
{
|
|
|
|
if (state == (char)1) /* Got ESC */
|
|
|
|
{
|
|
|
|
if (ch == '[' || ch == 'O')
|
|
|
|
{
|
|
|
|
state = ch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break; /* break the for loop */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (state == '[')
|
|
|
|
{
|
|
|
|
/* Got ESC[ */
|
2018-08-06 00:05:59 +02:00
|
|
|
|
2019-10-11 18:47:29 +02:00
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case '3': /* ESC[3~ = DEL */
|
|
|
|
{
|
|
|
|
state = ch;
|
2019-11-04 18:35:18 +01:00
|
|
|
continue;
|
2019-10-11 18:47:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'A':
|
|
|
|
{
|
|
|
|
ch = KEY_UP;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
|
|
|
{
|
|
|
|
ch = KEY_DN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
{
|
|
|
|
ch = KEY_RIGHT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
{
|
|
|
|
ch = KEY_LEFT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'F':
|
|
|
|
{
|
|
|
|
ch = KEY_ENDLINE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'H':
|
|
|
|
{
|
|
|
|
ch = KEY_BEGINLINE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break; /* Break the 'for' loop */
|
|
|
|
}
|
|
|
|
else if (state == 'O')
|
|
|
|
{
|
|
|
|
/* got ESCO */
|
|
|
|
|
|
|
|
if (ch == 'F')
|
|
|
|
{
|
|
|
|
ch = KEY_ENDLINE;
|
|
|
|
}
|
2019-11-04 18:35:18 +01:00
|
|
|
|
2019-10-11 18:47:29 +02:00
|
|
|
break; /* Break the 'for' loop */
|
|
|
|
}
|
|
|
|
else if (state == '3')
|
|
|
|
{
|
|
|
|
if (ch == '~')
|
|
|
|
{
|
|
|
|
ch = KEY_DEL;
|
|
|
|
}
|
2019-11-04 18:35:18 +01:00
|
|
|
|
2019-10-11 18:47:29 +02:00
|
|
|
break; /* Break the 'for' loop */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break; /* Break the 'for' loop */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ch == ASCII_ESC)
|
|
|
|
{
|
|
|
|
++state;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break; /* Break the 'for' loop, use the char */
|
|
|
|
}
|
|
|
|
}
|
2018-08-06 00:05:59 +02:00
|
|
|
|
|
|
|
#else
|
2014-02-02 19:24:45 +01:00
|
|
|
ch = cle_getch(priv);
|
2014-02-02 17:25:53 +01:00
|
|
|
if (ch < 0)
|
|
|
|
{
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2018-08-06 00:05:59 +02:00
|
|
|
#endif
|
|
|
|
|
2014-02-02 17:25:53 +01:00
|
|
|
/* Then handle the character. */
|
|
|
|
|
2018-08-06 00:05:59 +02:00
|
|
|
#ifdef CONFIG_SYSTEM_CLE_CMD_HISTORY
|
|
|
|
if (g_cmd_history_len > 0)
|
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case KEY_UP:
|
2019-10-11 18:47:29 +02:00
|
|
|
|
2018-08-06 00:05:59 +02:00
|
|
|
/* Go to the past command in history */
|
|
|
|
|
|
|
|
g_cmd_history_steps_from_head--;
|
|
|
|
|
|
|
|
if (-g_cmd_history_steps_from_head >= g_cmd_history_len)
|
|
|
|
{
|
|
|
|
g_cmd_history_steps_from_head = -(g_cmd_history_len - 1);
|
|
|
|
}
|
2019-11-04 18:35:18 +01:00
|
|
|
|
2018-08-06 00:05:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_DN:
|
2019-10-11 18:47:29 +02:00
|
|
|
|
2018-08-06 00:05:59 +02:00
|
|
|
/* Go to the recent command in history */
|
|
|
|
|
|
|
|
g_cmd_history_steps_from_head++;
|
|
|
|
|
|
|
|
if (g_cmd_history_steps_from_head > 1)
|
|
|
|
{
|
|
|
|
g_cmd_history_steps_from_head = 1;
|
|
|
|
}
|
2019-11-04 18:35:18 +01:00
|
|
|
|
2018-08-06 00:05:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
i = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
priv->nchars = 0;
|
|
|
|
priv->curpos = 0;
|
|
|
|
|
|
|
|
if (g_cmd_history_steps_from_head != 1)
|
|
|
|
{
|
|
|
|
int idx = g_cmd_history_head +
|
|
|
|
g_cmd_history_steps_from_head;
|
|
|
|
|
|
|
|
/* Circular buffer wrap around */
|
|
|
|
|
|
|
|
if (idx < 0)
|
|
|
|
{
|
|
|
|
idx = idx + CONFIG_SYSTEM_CLE_CMD_HISTORY_LEN;
|
|
|
|
}
|
|
|
|
else if (idx >= CONFIG_SYSTEM_CLE_CMD_HISTORY_LEN)
|
|
|
|
{
|
|
|
|
idx = idx - CONFIG_SYSTEM_CLE_CMD_HISTORY_LEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; g_cmd_history[idx][i] != '\0'; i++)
|
|
|
|
{
|
|
|
|
cle_insertch(priv, g_cmd_history[idx][i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->curpos = priv->nchars;
|
|
|
|
}
|
2019-11-04 18:35:18 +01:00
|
|
|
|
2018-08-06 00:05:59 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_SYSTEM_CLE_CMD_HISTORY */
|
|
|
|
|
2014-02-02 17:25:53 +01:00
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case KEY_BEGINLINE: /* Move cursor to start of current line */
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
priv->curpos = 0;
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_LEFT: /* Move the cursor left 1 character */
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
if (priv->curpos > 0)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
priv->curpos--;
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
CLE_BEL(priv);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_DEL: /* Delete 1 character at the cursor */
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
if (priv->curpos < priv->nchars)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_closetext(priv, priv->curpos, 1);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
CLE_BEL(priv);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_ENDLINE: /* Move cursor to end of current line */
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
priv->curpos = priv->nchars;
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_RIGHT: /* Move the cursor right one character */
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
if (priv->curpos < priv->nchars)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
priv->curpos++;
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
CLE_BEL(priv);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-08-01 23:26:49 +02:00
|
|
|
case ASCII_DEL:
|
2019-10-11 18:47:29 +02:00
|
|
|
case KEY_DELLEFT: /* Delete 1 character before the cursor */
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
if (priv->curpos > 0)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_closetext(priv, --priv->curpos, 1);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
CLE_BEL(priv);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_DELEOL: /* Delete to the end of the line */
|
|
|
|
{
|
2020-01-08 22:28:34 +01:00
|
|
|
priv->nchars = (priv->nchars > 0 ? priv->curpos : 0);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_DELLINE: /* Delete to the end of the line */
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
priv->nchars = 0;
|
|
|
|
priv->curpos = 0;
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-10-11 18:47:29 +02:00
|
|
|
case KEY_CLRSCR: /* Clear the screen & return cursor to top */
|
|
|
|
cle_clrscr(priv);
|
|
|
|
break;
|
|
|
|
|
2014-02-02 17:25:53 +01:00
|
|
|
case KEY_QUOTE: /* Quoted character follows */
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
ch = cle_getch(priv);
|
2014-02-02 17:25:53 +01:00
|
|
|
if (ch < 0)
|
|
|
|
{
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert the next character unconditionally */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_insertch(priv, ch);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-05-23 15:59:17 +02:00
|
|
|
/* Newline terminates editing. But what is a newline? */
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2023-02-13 15:02:25 +01:00
|
|
|
case '\n': /* LF terminates line */
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2020-04-27 14:24:43 +02:00
|
|
|
/* Add the newline to the buffer at the end of the line */
|
2014-05-23 15:19:21 +02:00
|
|
|
|
2014-05-23 15:59:17 +02:00
|
|
|
priv->curpos = priv->nchars;
|
2014-05-23 15:19:21 +02:00
|
|
|
cle_insertch(priv, '\n');
|
2014-02-02 17:25:53 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
break;
|
2014-05-23 15:19:21 +02:00
|
|
|
|
2014-02-02 17:25:53 +01:00
|
|
|
/* Text to insert or unimplemented/invalid keypresses */
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
/* Ignore all control characters except for tab and newline */
|
|
|
|
|
|
|
|
if (!iscntrl(ch) || ch == '\t')
|
|
|
|
{
|
|
|
|
/* Insert the filtered character into the buffer */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
cle_insertch(priv, ch);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
CLE_BEL(priv);
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Command line processing
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2021-11-14 19:39:35 +01:00
|
|
|
* Name: cle/cle_fd
|
2014-02-02 17:25:53 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* EMACS-like command line editor. This is actually more like readline
|
|
|
|
* than is the NuttX readline!
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-11-14 19:39:35 +01:00
|
|
|
int cle_fd(FAR char *line, FAR const char *prompt, uint16_t linelen,
|
|
|
|
int infd, int outfd)
|
2014-02-02 17:25:53 +01:00
|
|
|
{
|
2014-02-02 19:24:45 +01:00
|
|
|
FAR struct cle_s priv;
|
2014-02-02 17:25:53 +01:00
|
|
|
uint16_t column;
|
|
|
|
int ret;
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
/* Initialize the CLE state structure */
|
|
|
|
|
|
|
|
memset(&priv, 0, sizeof(struct cle_s));
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
priv.linelen = linelen;
|
|
|
|
priv.line = line;
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2021-11-14 19:39:35 +01:00
|
|
|
priv.infd = infd;
|
|
|
|
priv.outfd = outfd;
|
2014-02-02 17:25:53 +01:00
|
|
|
|
2019-10-11 18:47:29 +02:00
|
|
|
/* Store the prompt in case we need to re-print it */
|
|
|
|
|
|
|
|
priv.prompt = prompt;
|
|
|
|
cle_outputprompt(&priv);
|
|
|
|
|
2014-02-02 17:25:53 +01:00
|
|
|
/* Get the current cursor position */
|
|
|
|
|
2014-02-02 19:24:45 +01:00
|
|
|
ret = cle_getcursor(&priv, &priv.row, &column);
|
2019-11-04 18:35:18 +01:00
|
|
|
|
2014-02-02 17:25:53 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Turn the column number into an offset */
|
|
|
|
|
|
|
|
if (column < 1)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-02-19 13:17:58 +01:00
|
|
|
priv.coloffs = column;
|
2014-02-02 19:24:45 +01:00
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
cleinfo("row=%d column=%d\n", priv.row, column);
|
2014-02-02 17:25:53 +01:00
|
|
|
|
|
|
|
/* The editor loop */
|
|
|
|
|
2014-02-02 19:53:58 +01:00
|
|
|
ret = cle_editloop(&priv);
|
|
|
|
|
|
|
|
/* Make sure that the line is NUL terminated */
|
|
|
|
|
|
|
|
line[priv.nchars] = '\0';
|
2018-08-06 00:05:59 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_SYSTEM_CLE_CMD_HISTORY
|
|
|
|
/* Save history of command, only if there was something typed besides
|
|
|
|
* return character.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv.nchars > 1)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
g_cmd_history_head =
|
|
|
|
(g_cmd_history_head + 1) % CONFIG_SYSTEM_CLE_CMD_HISTORY_LEN;
|
|
|
|
|
|
|
|
for (i = 0;
|
2019-10-11 18:47:29 +02:00
|
|
|
(i < priv.nchars - 1) &&
|
|
|
|
i < (CONFIG_SYSTEM_CLE_CMD_HISTORY_LINELEN - 1);
|
2018-08-06 00:05:59 +02:00
|
|
|
i++)
|
|
|
|
{
|
|
|
|
g_cmd_history[g_cmd_history_head][i] = line[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
g_cmd_history[g_cmd_history_head][i] = '\0';
|
|
|
|
g_cmd_history_steps_from_head = 1;
|
|
|
|
|
|
|
|
if (g_cmd_history_len < CONFIG_SYSTEM_CLE_CMD_HISTORY_LEN)
|
|
|
|
{
|
|
|
|
g_cmd_history_len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_SYSTEM_CLE_CMD_HISTORY */
|
|
|
|
|
2014-02-02 19:53:58 +01:00
|
|
|
return ret;
|
2014-02-02 17:25:53 +01:00
|
|
|
}
|
2021-11-14 19:39:35 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_FILE_STREAM
|
|
|
|
int cle(FAR char *line, FAR const char *prompt, uint16_t linelen,
|
|
|
|
FAR FILE *instream, FAR FILE *outstream)
|
|
|
|
{
|
|
|
|
return cle_fd(line, prompt, linelen, instream->fs_fd, outstream->fs_fd);
|
|
|
|
}
|
|
|
|
#endif
|