From 96f30d12e05b66d0c4b14d12471ccb79963ccead Mon Sep 17 00:00:00 2001 From: "W. Eric Norum" Date: Mon, 8 May 2000 18:59:59 +0000 Subject: [PATCH] Convert iocccrf to take a single pathname argument. If the pathname is NULL, emit prompts and read from stdin. Use the last component of the pathname as the `filename' argument to showError. Get the prompt from the IOCSH_PS1 environment variable or use default "iocsh> ". Return 0 on success. Print message and return -1 on failure (e.g. can't open file). If using readline, get history size from IOCSH_HISTSIZE environment variable. --- src/iocsh/iocUtil.c | 13 +------ src/iocsh/ioccrf.c | 54 ++++++++++++++++------------ src/iocsh/ioccrf.h | 2 +- src/libCom/osi/os/RTEMS/rtems_init.c | 17 +++------ 4 files changed, 37 insertions(+), 49 deletions(-) diff --git a/src/iocsh/iocUtil.c b/src/iocsh/iocUtil.c index 38fbdfb04..7241bc7b5 100644 --- a/src/iocsh/iocUtil.c +++ b/src/iocsh/iocUtil.c @@ -23,18 +23,7 @@ static void runScriptCallFunc(ioccrfArg **args) #ifdef __rtems__ runScriptRTEMS ((char *)args[0]->value); #else - char *cp; - FILE *fp; - - cp = (char *)args[0]->value; - fp = fopen (cp, "r"); - if (fp == NULL) { - printf ("Can't open script (%s)\n", cp); - } - else { - ioccrf (fp, cp); - fclose (fp); - } + ioccrf ((char *)args[0]->value); #endif } diff --git a/src/iocsh/ioccrf.c b/src/iocsh/ioccrf.c index cccef07ff..b1c1b5bde 100644 --- a/src/iocsh/ioccrf.c +++ b/src/iocsh/ioccrf.c @@ -12,6 +12,7 @@ of this distribution. #include #include #include +#include #include "cantProceed.h" #include "registry.h" @@ -114,15 +115,15 @@ my_readline (FILE *fp, const char *prompt) * Report an error */ static void -showError (const char *name, int lineno, const char *msg, ...) +showError (const char *filename, int lineno, const char *msg, ...) { va_list ap; va_start (ap, msg); - if (name) - printf ("%s -- Line %d -- ", name, lineno); - vprintf (msg, ap); - printf ("\n"); + if (filename) + fprintf (stderr, "%s -- Line %d -- ", filename, lineno); + vfprintf (stderr, msg, ap); + fprintf (stderr, "\n"); va_end (ap); } @@ -183,9 +184,11 @@ cvtArg (const char *filename, int lineno, char *arg, argvalue *pargvalue, ioccrf /* * The body of the command interpreter */ -void epicsShareAPI -ioccrf (FILE *fp, const char *filename) +int epicsShareAPI +ioccrf (const char *pathname) { + FILE *fp; + const char *filename = NULL; int icin, icout; int c, quote, inword, backslash; char *line = NULL; @@ -195,31 +198,24 @@ ioccrf (FILE *fp, const char *filename) int argvsize = 0; int sep; const char *prompt; - const char *ifs; - const char *historySize; + const char *ifs = " \t(),"; ioccrfFunc *pioccrfFunc; ioccrfFuncDef *pioccrfFuncDef; int arg; argvalue *pargvalue, *prevargvalue, *argvalueHead = NULL; ioccrfArg *pioccrfArg; - /* - * Pick up items from environment - */ - if ((prompt = getenv ("PS1")) == NULL) - prompt = "-> "; - if ((ifs = getenv ("IFS")) == NULL) - ifs = " \t(),"; - /* * See if command interpreter is interactive */ - if (fp == NULL) + if (pathname == NULL) { + const char *historySize; fp = stdin; - if (fp == stdin) { - filename = NULL; - if ((historySize = getenv ("HISTSIZE")) == NULL) - historySize="10"; + if ((prompt = getenv ("IOCSH_PS1")) == NULL) + prompt = "iocsh> "; + if (((historySize = getenv ("IOCSH_HISTSIZE")) == NULL) + && ((historySize = getenv ("HISTSIZE")) == NULL)) + historySize = "10"; stifle_history (atoi (historySize)); /* * FIXME: Could enable tab-completion of commands here @@ -227,11 +223,20 @@ ioccrf (FILE *fp, const char *filename) rl_bind_key ('\t', rl_insert); } else { + fp = fopen (pathname, "r"); + if (fp == NULL) { + fprintf (stderr, "Can't open %s: %s\n", pathname, strerror (errno)); + return -1; + } + if ((filename = strrchr (pathname, '/')) == NULL) + filename = pathname; + else + filename++; prompt = NULL; } /* - * Read commands till EOF + * Read commands till EOF or exit */ for (;;) { /* @@ -380,6 +385,8 @@ ioccrf (FILE *fp, const char *filename) } } } + if (fp != stdin) + fclose (fp); free (line); free (argv); while (argvalueHead) { @@ -387,6 +394,7 @@ ioccrf (FILE *fp, const char *filename) free (argvalueHead); argvalueHead = pargvalue; } + return 0; } /* Readline automatic completion code could go here! */ diff --git a/src/iocsh/ioccrf.h b/src/iocsh/ioccrf.h index c81a47bd4..e48b2557e 100644 --- a/src/iocsh/ioccrf.h +++ b/src/iocsh/ioccrf.h @@ -42,7 +42,7 @@ epicsShareFunc void epicsShareAPI ioccrfRegister( ioccrfFuncDef *pioccrfFuncDef,ioccrfCallFunc func); -epicsShareFunc void epicsShareAPI ioccrf(FILE *, const char *filename); +epicsShareFunc int epicsShareAPI ioccrf(const char *pathname); #ifdef __cplusplus } diff --git a/src/libCom/osi/os/RTEMS/rtems_init.c b/src/libCom/osi/os/RTEMS/rtems_init.c index f6e846cb2..1f36565c6 100644 --- a/src/libCom/osi/os/RTEMS/rtems_init.c +++ b/src/libCom/osi/os/RTEMS/rtems_init.c @@ -282,17 +282,9 @@ void runScriptRTEMS (const char *name) { char *cp; - FILE *fp; cp = rtems_tftp_path (name); - fp = fopen (cp, "r"); - if (fp == NULL) { - printf ("Can't open script (%s)\n", name); - } - else { - ioccrf (fp, name); - fclose (fp); - } + ioccrf (cp); free (cp); } @@ -319,9 +311,8 @@ Init (rtems_task_argument ignored) * Create a reasonable environment */ putenv ("TERM=xterm"); - putenv ("PS1=rtems> "); - putenv ("HISTSIZE=10"); - putenv ("IFS= \t,()"); + putenv ("IOCSH_PS1=rtems> "); + putenv ("IOCSH_HISTSIZE=20"); /* * Start network @@ -354,6 +345,6 @@ Init (rtems_task_argument ignored) * Everything's running! */ threadSleep (2.0); - ioccrf (NULL, NULL); + ioccrf (NULL); LogFatal ("Console command interpreter terminated"); }