nshlib: Shouldn't set TIOCSCTTY with STDOUT_FILENO directly

to handle the shell redirection correctly

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2023-02-22 13:13:45 +08:00 committed by GUIDINGLI
parent 9246502aca
commit 80feb2432d
4 changed files with 32 additions and 11 deletions

View File

@ -136,7 +136,7 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
{
/* Setup up to receive SIGINT if control-C entered. */
tc = ioctl(STDOUT_FILENO, TIOCSCTTY, ret);
tc = nsh_ioctl(vtbl, TIOCSCTTY, ret);
}
/* Wait for the application to exit. We did lock the scheduler
@ -201,7 +201,7 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
if (vtbl->isctty && tc == 0)
{
ioctl(STDOUT_FILENO, TIOCNOTTY);
nsh_ioctl(vtbl, TIOCNOTTY, 0);
}
}
# ifndef CONFIG_NSH_DISABLEBG

View File

@ -57,18 +57,20 @@ 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);
static ssize_t nsh_consolewrite(FAR struct nsh_vtbl_s *vtbl,
FAR const void *buffer, size_t nbytes);
FAR const void *buffer, size_t nbytes);
static int nsh_consoleioctl(FAR struct nsh_vtbl_s *vtbl,
int cmd, unsigned long arg);
static int nsh_consoleoutput(FAR struct nsh_vtbl_s *vtbl,
FAR const char *fmt, ...) printf_like(2, 3);
FAR const char *fmt, ...) printf_like(2, 3);
static int nsh_erroroutput(FAR struct nsh_vtbl_s *vtbl,
FAR const char *fmt, ...) printf_like(2, 3);
FAR const char *fmt, ...) printf_like(2, 3);
static FAR char *nsh_consolelinebuffer(FAR struct nsh_vtbl_s *vtbl);
static void nsh_consoleredirect(FAR struct nsh_vtbl_s *vtbl, int fd,
FAR uint8_t *save);
FAR uint8_t *save);
static void nsh_consoleundirect(FAR struct nsh_vtbl_s *vtbl,
FAR uint8_t *save);
static void nsh_consoleexit(FAR struct nsh_vtbl_s *vtbl, int exitstatus)
noreturn_function;
FAR uint8_t *save);
static void nsh_consoleexit(FAR struct nsh_vtbl_s *vtbl,
int exitstatus) anoreturn_function;
/****************************************************************************
* Private Functions
@ -127,6 +129,22 @@ static ssize_t nsh_consolewrite(FAR struct nsh_vtbl_s *vtbl,
return ret;
}
/****************************************************************************
* 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);
}
/****************************************************************************
* Name: nsh_consoleoutput
*
@ -347,6 +365,7 @@ FAR struct console_stdio_s *nsh_newconsole(bool isctty)
#endif
pstate->cn_vtbl.release = nsh_consolerelease;
pstate->cn_vtbl.write = nsh_consolewrite;
pstate->cn_vtbl.ioctl = nsh_consoleioctl;
pstate->cn_vtbl.output = nsh_consoleoutput;
pstate->cn_vtbl.error = nsh_erroroutput;
pstate->cn_vtbl.linebuffer = nsh_consolelinebuffer;

View File

@ -43,6 +43,7 @@
#define nsh_clone(v) (v)->clone(v)
#define nsh_release(v) (v)->release(v)
#define nsh_write(v,b,n) (v)->write(v,b,n)
#define nsh_ioctl(v,c,a) (v)->ioctl(v,c,a)
#define nsh_linebuffer(v) (v)->linebuffer(v)
#define nsh_redirect(v,f,s) (v)->redirect(v,f,s)
#define nsh_undirect(v,s) (v)->undirect(v,s)
@ -103,6 +104,7 @@ struct nsh_vtbl_s
void (*release)(FAR struct nsh_vtbl_s *vtbl);
ssize_t (*write)(FAR struct nsh_vtbl_s *vtbl, FAR const void *buffer,
size_t nbytes);
int (*ioctl)(FAR struct nsh_vtbl_s *vtbl, int cmd, unsigned long arg);
int (*error)(FAR struct nsh_vtbl_s *vtbl, FAR const char *fmt, ...)
printf_like(2, 3);
int (*output)(FAR struct nsh_vtbl_s *vtbl, FAR const char *fmt, ...)

View File

@ -204,7 +204,7 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
{
/* Setup up to receive SIGINT if control-C entered. */
tc = ioctl(stdout->fs_fd, TIOCSCTTY, pid);
tc = nsh_ioctl(vtbl, TIOCSCTTY, pid);
}
/* Wait for the application to exit. We did lock the scheduler
@ -260,7 +260,7 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
if (vtbl->isctty && tc == 0)
{
ioctl(stdout->fs_fd, TIOCNOTTY);
nsh_ioctl(vtbl, TIOCNOTTY, 0);
}
}
# ifndef CONFIG_NSH_DISABLEBG