2012-02-02 20:42:55 +01:00
|
|
|
/****************************************************************************
|
2013-09-30 19:34:04 +02:00
|
|
|
* apps/nshlib/nsh_console.c
|
2012-02-02 20:42:55 +01:00
|
|
|
*
|
2021-06-10 16:34:12 +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
|
2012-02-02 20:42:55 +01:00
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-02-02 20:42:55 +01:00
|
|
|
*
|
2021-06-10 16:34:12 +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.
|
2012-02-02 20:42:55 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-02-21 23:08:13 +01:00
|
|
|
#include <sys/ioctl.h>
|
2012-02-02 20:42:55 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-01-24 16:49:17 +01:00
|
|
|
#include <stdarg.h>
|
2012-02-02 20:42:55 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include "nsh.h"
|
|
|
|
#include "nsh_console.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct serialsave_s
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
int cn_errfd; /* Re-directed error output file descriptor */
|
|
|
|
int cn_outfd; /* Re-directed output file descriptor */
|
2012-02-02 20:42:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
|
|
|
static FAR struct nsh_vtbl_s *nsh_consoleclone(FAR struct nsh_vtbl_s *vtbl);
|
|
|
|
#endif
|
|
|
|
static void nsh_consolerelease(FAR struct nsh_vtbl_s *vtbl);
|
2012-10-12 18:59:17 +02:00
|
|
|
static ssize_t nsh_consolewrite(FAR struct nsh_vtbl_s *vtbl,
|
2023-02-22 06:13:45 +01:00
|
|
|
FAR const void *buffer, size_t nbytes);
|
|
|
|
static int nsh_consoleioctl(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
int cmd, unsigned long arg);
|
2012-10-12 18:59:17 +02:00
|
|
|
static int nsh_consoleoutput(FAR struct nsh_vtbl_s *vtbl,
|
2023-02-22 06:13:45 +01:00
|
|
|
FAR const char *fmt, ...) printf_like(2, 3);
|
2023-06-08 13:37:31 +02:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_ERROR_PRINT
|
2018-12-09 01:53:54 +01:00
|
|
|
static int nsh_erroroutput(FAR struct nsh_vtbl_s *vtbl,
|
2023-02-22 06:13:45 +01:00
|
|
|
FAR const char *fmt, ...) printf_like(2, 3);
|
2023-06-08 13:37:31 +02:00
|
|
|
#endif
|
2012-02-02 20:42:55 +01:00
|
|
|
static FAR char *nsh_consolelinebuffer(FAR struct nsh_vtbl_s *vtbl);
|
2012-10-12 18:59:17 +02:00
|
|
|
static void nsh_consoleredirect(FAR struct nsh_vtbl_s *vtbl, int fd,
|
2023-02-22 06:13:45 +01:00
|
|
|
FAR uint8_t *save);
|
2012-10-12 18:59:17 +02:00
|
|
|
static void nsh_consoleundirect(FAR struct nsh_vtbl_s *vtbl,
|
2023-02-22 06:13:45 +01:00
|
|
|
FAR uint8_t *save);
|
|
|
|
static void nsh_consoleexit(FAR struct nsh_vtbl_s *vtbl,
|
2023-02-21 23:08:13 +01:00
|
|
|
int exitstatus) noreturn_function;
|
2012-02-02 20:42:55 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_closeifnotclosed
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Close the output stream if it is not the standard output stream.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void nsh_closeifnotclosed(struct console_stdio_s *pstate)
|
|
|
|
{
|
2023-02-10 10:50:46 +01:00
|
|
|
if (OUTFD(pstate) >= 0 && OUTFD(pstate) != STDOUT_FILENO)
|
2012-02-02 20:42:55 +01:00
|
|
|
{
|
2023-02-10 10:50:46 +01:00
|
|
|
close(OUTFD(pstate));
|
2012-02-02 20:42:55 +01:00
|
|
|
}
|
2023-02-10 10:50:46 +01:00
|
|
|
|
|
|
|
if (ERRFD(pstate) >= 0 && ERRFD(pstate) != STDERR_FILENO
|
|
|
|
&& ERRFD(pstate) != OUTFD(pstate))
|
2012-02-02 20:42:55 +01:00
|
|
|
{
|
2023-02-10 10:50:46 +01:00
|
|
|
close(ERRFD(pstate));
|
2012-02-02 20:42:55 +01:00
|
|
|
}
|
2023-02-10 10:50:46 +01:00
|
|
|
|
|
|
|
ERRFD(pstate) = -1;
|
|
|
|
OUTFD(pstate) = -1;
|
2012-02-02 20:42:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_consolewrite
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* write a buffer to the remote shell window.
|
|
|
|
*
|
|
|
|
* Currently only used by cat.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-17 14:41:51 +01:00
|
|
|
static ssize_t nsh_consolewrite(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR const void *buffer, size_t nbytes)
|
2012-02-02 20:42:55 +01:00
|
|
|
{
|
|
|
|
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* Write the data to the output stream */
|
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
ret = write(OUTFD(pstate), buffer, nbytes);
|
2012-02-02 20:42:55 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-16 20:12:34 +02:00
|
|
|
_err("ERROR: [%d] Failed to send buffer: %d\n",
|
2023-02-10 10:50:46 +01:00
|
|
|
OUTFD(pstate), errno);
|
2012-02-02 20:42:55 +01:00
|
|
|
}
|
2018-11-07 18:18:03 +01:00
|
|
|
|
2012-02-02 20:42:55 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-22 06:13:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_consolewrite
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Issue ioctl to the currently selected stream.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nsh_consoleioctl(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
|
|
|
|
|
|
|
|
return ioctl(OUTFD(pstate), cmd, arg);
|
|
|
|
}
|
|
|
|
|
2012-02-02 20:42:55 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_consoleoutput
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Print a string to the currently selected stream.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-24 16:49:17 +01:00
|
|
|
static int nsh_consoleoutput(FAR struct nsh_vtbl_s *vtbl,
|
2016-01-17 14:53:52 +01:00
|
|
|
FAR const char *fmt, ...)
|
2012-02-02 20:42:55 +01:00
|
|
|
{
|
|
|
|
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
|
|
|
|
va_list ap;
|
2013-09-30 19:34:04 +02:00
|
|
|
int ret;
|
2012-02-02 20:42:55 +01:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2023-02-10 10:50:46 +01:00
|
|
|
ret = vdprintf(OUTFD(pstate), fmt, ap);
|
2012-02-02 20:42:55 +01:00
|
|
|
va_end(ap);
|
2013-09-30 19:34:04 +02:00
|
|
|
|
2012-02-02 20:42:55 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-12-09 01:53:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_erroroutput
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Print a string to the currently selected error stream.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-08 13:37:31 +02:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_ERROR_PRINT
|
2018-12-09 01:53:54 +01:00
|
|
|
static int nsh_erroroutput(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR const char *fmt, ...)
|
|
|
|
{
|
|
|
|
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2023-02-10 10:50:46 +01:00
|
|
|
ret = vdprintf(ERRFD(pstate), fmt, ap);
|
2018-12-09 01:53:54 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2023-06-08 13:37:31 +02:00
|
|
|
#endif
|
2018-12-09 01:53:54 +01:00
|
|
|
|
2012-02-02 20:42:55 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_consolelinebuffer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return a reference to the current line buffer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR char *nsh_consolelinebuffer(FAR struct nsh_vtbl_s *vtbl)
|
|
|
|
{
|
|
|
|
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
|
|
|
|
return pstate->cn_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_consoleclone
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Make an independent copy of the vtbl
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
|
|
|
static FAR struct nsh_vtbl_s *nsh_consoleclone(FAR struct nsh_vtbl_s *vtbl)
|
|
|
|
{
|
2021-06-17 16:59:42 +02:00
|
|
|
FAR struct console_stdio_s *pclone = nsh_newconsole(vtbl->isctty);
|
2012-02-02 20:42:55 +01:00
|
|
|
return &pclone->cn_vtbl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_consolerelease
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release the cloned instance
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void nsh_consolerelease(FAR struct nsh_vtbl_s *vtbl)
|
|
|
|
{
|
|
|
|
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
|
|
|
|
|
|
|
|
/* Close the output stream */
|
|
|
|
|
|
|
|
nsh_closeifnotclosed(pstate);
|
|
|
|
|
|
|
|
/* Close the console stream */
|
|
|
|
|
2014-05-05 16:52:02 +02:00
|
|
|
#ifdef CONFIG_NSH_ALTCONDEV
|
2023-02-10 10:50:46 +01:00
|
|
|
close(pstate->cn_confd);
|
2013-09-30 19:34:04 +02:00
|
|
|
#endif
|
2012-02-02 20:42:55 +01:00
|
|
|
|
2018-10-01 21:30:25 +02:00
|
|
|
#ifdef CONFIG_NSH_VARS
|
|
|
|
/* Free any NSH variables */
|
|
|
|
|
|
|
|
if (pstate->varp != NULL)
|
|
|
|
{
|
|
|
|
free(pstate->varp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-02 20:42:55 +01:00
|
|
|
/* Then release the vtable container */
|
|
|
|
|
|
|
|
free(pstate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_consoleredirect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set up for redirected output. This function is called from nsh_parse()
|
|
|
|
* in two different contexts:
|
|
|
|
*
|
|
|
|
* 1) Redirected background commands of the form: command > xyz.text &
|
|
|
|
*
|
|
|
|
* In this case:
|
|
|
|
* - vtbl: A newly allocated and initialized instance created by
|
|
|
|
* nsh_consoleclone,
|
|
|
|
* - fd:- The file descriptor of the redirected output
|
|
|
|
* - save: NULL
|
|
|
|
*
|
|
|
|
* nsh_consolerelease() will perform the clean-up when the clone is
|
|
|
|
* destroyed.
|
2013-09-30 19:34:04 +02:00
|
|
|
*
|
2012-02-02 20:42:55 +01:00
|
|
|
* 2) Redirected foreground commands of the form: command > xyz.txt
|
|
|
|
*
|
|
|
|
* In this case:
|
|
|
|
* - vtbl: The current state structure,
|
|
|
|
* - fd: The file descriptor of the redirected output
|
|
|
|
* - save: Where to save the re-directed registers.
|
|
|
|
*
|
|
|
|
* nsh_consoleundirect() will perform the clean-up after the redirected
|
|
|
|
* command completes.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-10-01 21:30:25 +02:00
|
|
|
static void nsh_consoleredirect(FAR struct nsh_vtbl_s *vtbl, int fd,
|
|
|
|
FAR uint8_t *save)
|
2012-02-02 20:42:55 +01:00
|
|
|
{
|
2013-09-30 19:34:04 +02:00
|
|
|
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
|
2012-02-02 20:42:55 +01:00
|
|
|
FAR struct serialsave_s *ssave = (FAR struct serialsave_s *)save;
|
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
/* Redirected foreground commands */
|
2012-02-02 20:42:55 +01:00
|
|
|
|
|
|
|
if (ssave)
|
|
|
|
{
|
|
|
|
/* Save the current fd and stream values. These will be restored
|
|
|
|
* when nsh_consoleundirect() is called.
|
|
|
|
*/
|
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
ERRFD(ssave) = ERRFD(pstate);
|
|
|
|
OUTFD(ssave) = OUTFD(pstate);
|
2012-02-02 20:42:55 +01:00
|
|
|
}
|
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
/* Set the fd of the new. */
|
2012-02-02 20:42:55 +01:00
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
OUTFD(pstate) = fd;
|
2012-02-02 20:42:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_consoleundirect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set up for redirected output
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
static void nsh_consoleundirect(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR uint8_t *save)
|
2012-02-02 20:42:55 +01:00
|
|
|
{
|
|
|
|
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
|
2023-02-10 10:50:46 +01:00
|
|
|
FAR struct serialsave_s *ssave = (FAR struct serialsave_s *)save;
|
2012-02-02 20:42:55 +01:00
|
|
|
|
|
|
|
nsh_closeifnotclosed(pstate);
|
2023-02-10 10:50:46 +01:00
|
|
|
ERRFD(pstate) = ERRFD(ssave);
|
|
|
|
OUTFD(pstate) = OUTFD(ssave);
|
2012-02-02 20:42:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_consoleexit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Exit the shell task
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void nsh_consoleexit(FAR struct nsh_vtbl_s *vtbl, int exitstatus)
|
|
|
|
{
|
|
|
|
/* Destroy ourself then exit with the provided status */
|
|
|
|
|
|
|
|
nsh_consolerelease(vtbl);
|
|
|
|
exit(exitstatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_newconsole
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-17 16:59:42 +02:00
|
|
|
FAR struct console_stdio_s *nsh_newconsole(bool isctty)
|
2012-02-02 20:42:55 +01:00
|
|
|
{
|
2016-01-17 14:41:51 +01:00
|
|
|
FAR struct console_stdio_s *pstate =
|
|
|
|
(FAR struct console_stdio_s *)zalloc(sizeof(struct console_stdio_s));
|
|
|
|
|
2012-02-02 20:42:55 +01:00
|
|
|
if (pstate)
|
|
|
|
{
|
|
|
|
/* Initialize the call table */
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
2018-08-29 14:18:30 +02:00
|
|
|
pstate->cn_vtbl.clone = nsh_consoleclone;
|
|
|
|
#endif
|
2021-05-06 16:28:05 +02:00
|
|
|
pstate->cn_vtbl.release = nsh_consolerelease;
|
2018-08-29 14:18:30 +02:00
|
|
|
pstate->cn_vtbl.write = nsh_consolewrite;
|
2023-02-22 06:13:45 +01:00
|
|
|
pstate->cn_vtbl.ioctl = nsh_consoleioctl;
|
2018-08-29 14:18:30 +02:00
|
|
|
pstate->cn_vtbl.output = nsh_consoleoutput;
|
2023-06-08 13:37:31 +02:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_ERROR_PRINT
|
2018-12-09 01:53:54 +01:00
|
|
|
pstate->cn_vtbl.error = nsh_erroroutput;
|
2023-06-08 13:37:31 +02:00
|
|
|
#endif
|
2018-08-29 14:18:30 +02:00
|
|
|
pstate->cn_vtbl.linebuffer = nsh_consolelinebuffer;
|
|
|
|
pstate->cn_vtbl.exit = nsh_consoleexit;
|
2021-06-17 16:59:42 +02:00
|
|
|
pstate->cn_vtbl.isctty = isctty;
|
2018-08-29 14:18:30 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLESCRIPT
|
|
|
|
/* Set the initial option flags */
|
|
|
|
|
|
|
|
pstate->cn_vtbl.np.np_flags = NSH_NP_SET_OPTIONS_INIT;
|
2012-02-02 20:42:55 +01:00
|
|
|
#endif
|
2013-09-30 19:34:04 +02:00
|
|
|
|
2018-08-29 14:18:30 +02:00
|
|
|
pstate->cn_vtbl.redirect = nsh_consoleredirect;
|
|
|
|
pstate->cn_vtbl.undirect = nsh_consoleundirect;
|
2012-02-02 20:42:55 +01:00
|
|
|
|
2018-12-09 01:53:54 +01:00
|
|
|
/* Initialize the error stream */
|
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
ERRFD(pstate) = STDERR_FILENO;
|
2012-02-02 20:42:55 +01:00
|
|
|
|
|
|
|
/* Initialize the output stream */
|
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
OUTFD(pstate) = STDOUT_FILENO;
|
2012-02-02 20:42:55 +01:00
|
|
|
}
|
2014-09-13 01:09:18 +02:00
|
|
|
|
2012-02-02 20:42:55 +01:00
|
|
|
return pstate;
|
|
|
|
}
|