system/cu: Remove the dependence on stdio FILE * function

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2023-03-26 11:56:12 +08:00 committed by Alin Jerpelea
parent 5f9d9bb1c2
commit a5ef6d57c1

View File

@ -59,7 +59,7 @@
#ifdef CONFIG_SYSTEM_CUTERM_DISABLE_ERROR_PRINT #ifdef CONFIG_SYSTEM_CUTERM_DISABLE_ERROR_PRINT
# define cu_error(...) # define cu_error(...)
#else #else
# define cu_error(...) fprintf(stderr, __VA_ARGS__) # define cu_error(...) dprintf(STDERR_FILENO, __VA_ARGS__)
#endif #endif
/**************************************************************************** /****************************************************************************
@ -110,8 +110,7 @@ static FAR void *cu_listener(FAR void *parameter)
break; break;
} }
fputc(ch, stdout); write(STDOUT_FILENO, &ch, 1);
fflush(stdout);
} }
/* Won't get here */ /* Won't get here */
@ -288,7 +287,6 @@ int main(int argc, FAR char *argv[])
int nocrlf = 0; int nocrlf = 0;
int option; int option;
int ret; int ret;
int bcmd;
int start_of_line = 1; int start_of_line = 1;
int exitval = EXIT_FAILURE; int exitval = EXIT_FAILURE;
bool badarg = false; bool badarg = false;
@ -383,17 +381,17 @@ int main(int argc, FAR char *argv[])
* right descriptor that is used to refer to tty * right descriptor that is used to refer to tty
*/ */
if (isatty(fileno(stderr))) if (isatty(STDERR_FILENO))
{ {
cu->stdfd = fileno(stderr); cu->stdfd = STDERR_FILENO;
} }
else if (isatty(fileno(stdout))) else if (isatty(STDOUT_FILENO))
{ {
cu->stdfd = fileno(stdout); cu->stdfd = STDOUT_FILENO;
} }
else if (isatty(fileno(stdin))) else if (isatty(STDIN_FILENO))
{ {
cu->stdfd = fileno(stdin); cu->stdfd = STDIN_FILENO;
} }
else else
{ {
@ -440,24 +438,26 @@ int main(int argc, FAR char *argv[])
while (!cu->force_exit) while (!cu->force_exit)
{ {
char ch; char ch;
int c = getc(stdin);
if (c < 0) if (read(STDIN_FILENO, &ch, 1) <= 0)
{ {
continue; continue;
} }
ch = c;
if (start_of_line == 1 && ch == cu->escape) if (start_of_line == 1 && ch == cu->escape)
{ {
/* We've seen and escape (~) character, echo it to local /* We've seen and escape (~) character, echo it to local
* terminal and read the next char from serial * terminal and read the next char from serial
*/ */
fputc(ch, stdout); write(STDOUT_FILENO, &ch, 1);
bcmd = getc(stdin);
if (bcmd == ch) if (read(STDIN_FILENO, &ch, 1) <= 0)
{
continue;
}
if (ch == cu->escape)
{ {
/* Escaping a tilde: handle like normal char */ /* Escaping a tilde: handle like normal char */