From 6cc2387e1a6b4762931b587a114d21ee2d22399b Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 24 Jul 2024 11:42:39 +0200 Subject: [PATCH] shell: Only use readline on terminals Should fix xmllint shell tests. --- shell.c | 52 ++++++++++++++++++++++++++++++++-------------------- xmlcatalog.c | 50 +++++++++++++++++++++++++++++--------------------- 2 files changed, 61 insertions(+), 41 deletions(-) diff --git a/shell.c b/shell.c index 6a21898d..a1572b28 100644 --- a/shell.c +++ b/shell.c @@ -12,6 +12,12 @@ #include #include +#ifdef _WIN32 + #include +#else + #include +#endif + #ifdef HAVE_LIBREADLINE #include #ifdef HAVE_LIBHISTORY @@ -31,6 +37,10 @@ #include "private/shell.h" +#ifndef STDIN_FILENO + #define STDIN_FILENO 0 +#endif + /* * TODO: Improvement/cleanups for the XML shell * - allow to shell out an editor on a subpart @@ -1047,37 +1057,39 @@ xmllintShellPwd(xmllintShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer, */ static char * xmllintShellReadline(char *prompt) { -#ifdef HAVE_LIBREADLINE - char *line_read; - - /* Get a line from the user. */ - line_read = readline (prompt); - -#ifdef HAVE_LIBHISTORY - /* If the line has any text in it, save it on the history. */ - if (line_read && *line_read) - add_history (line_read); -#endif - - return (line_read); -#else - char line_read[501]; + char buf[501]; char *ret; int len; +#ifdef HAVE_LIBREADLINE + if (isatty(STDIN_FILENO)) { + char *line_read; + + /* Get a line from the user. */ + line_read = readline (prompt); + +#ifdef HAVE_LIBHISTORY + /* If the line has any text in it, save it on the history. */ + if (line_read && *line_read) + add_history (line_read); +#endif + + return (line_read); + } +#endif + if (prompt != NULL) fprintf(stdout, "%s", prompt); fflush(stdout); - if (!fgets(line_read, 500, stdin)) + if (!fgets(buf, 500, stdin)) return(NULL); - line_read[500] = 0; - len = strlen(line_read); + buf[500] = 0; + len = strlen(buf); ret = (char *) malloc(len + 1); if (ret != NULL) { - memcpy (ret, line_read, len + 1); + memcpy (ret, buf, len + 1); } return(ret); -#endif } /** diff --git a/xmlcatalog.c b/xmlcatalog.c index a1cd9870..ac4b80aa 100644 --- a/xmlcatalog.c +++ b/xmlcatalog.c @@ -16,6 +16,8 @@ #ifdef _WIN32 #include #include +#else + #include #endif #ifdef HAVE_LIBREADLINE @@ -30,6 +32,10 @@ #include #include +#ifndef STDIN_FILENO + #define STDIN_FILENO 0 +#endif + #if defined(LIBXML_CATALOG_ENABLED) && defined(LIBXML_OUTPUT_ENABLED) static int shell = 0; static int sgml = 0; @@ -63,37 +69,39 @@ static char *filename = NULL; */ static char * xmlShellReadline(const char *prompt) { -#ifdef HAVE_LIBREADLINE - char *line_read; - - /* Get a line from the user. */ - line_read = readline (prompt); - -#ifdef HAVE_LIBHISTORY - /* If the line has any text in it, save it on the history. */ - if (line_read && *line_read) - add_history (line_read); -#endif - - return (line_read); -#else - char line_read[501]; + char buf[501]; char *ret; int len; +#ifdef HAVE_LIBREADLINE + if (isatty(STDIN_FILENO)) { + char *line_read; + + /* Get a line from the user. */ + line_read = readline (prompt); + +#ifdef HAVE_LIBHISTORY + /* If the line has any text in it, save it on the history. */ + if (line_read && *line_read) + add_history (line_read); +#endif + + return (line_read); + } +#endif + if (prompt != NULL) - fprintf(stdout, "%s", prompt); + fprintf(stdout, "%s", prompt); fflush(stdout); - if (!fgets(line_read, 500, stdin)) + if (!fgets(buf, 500, stdin)) return(NULL); - line_read[500] = 0; - len = strlen(line_read); + buf[500] = 0; + len = strlen(buf); ret = (char *) malloc(len + 1); if (ret != NULL) { - memcpy (ret, line_read, len + 1); + memcpy (ret, buf, len + 1); } return(ret); -#endif } static void usershell(void) {