From 0d6784c31bf69b2413f3f334cef75f05c9667908 Mon Sep 17 00:00:00 2001 From: "W. Eric Norum" Date: Thu, 7 Oct 2004 20:10:38 +0000 Subject: [PATCH] Provide epicsStrGlobMatch(). Add globbing to iocsh help command. --- src/db/dbTest.c | 48 +---------------------------------- src/iocsh/iocsh.cpp | 36 +++++++++++++------------- src/libCom/misc/epicsString.c | 47 ++++++++++++++++++++++++++++++++++ src/libCom/misc/epicsString.h | 2 ++ 4 files changed, 67 insertions(+), 66 deletions(-) diff --git a/src/db/dbTest.c b/src/db/dbTest.c index 7cf1ce2c1..199b29a8e 100644 --- a/src/db/dbTest.c +++ b/src/db/dbTest.c @@ -194,52 +194,6 @@ long epicsShareAPI dbnr(int verbose) return(0); } -static int specified_by(const char *ptest, const char *pspec) -{ - short inx; - short wild_card_start; - - /* check if the specification begins with a wild card */ - if (*pspec == '*') wild_card_start = TRUE; - else wild_card_start = FALSE; - - /* check for specification */ - while (TRUE) { - /* skip any wild cards */ - while (*pspec == '*') pspec++; - - /* find the specification chars to compare */ - inx = 0; - while ( (*(pspec+inx) != '*') && (*(pspec+inx)) ) - inx++; - - /* check for specification ending with wildcard */ - if (inx == 0) return(TRUE); - - /* find the spec chars in the test string */ - while ((strlen(ptest) >= inx) - && (strncmp(ptest,pspec,inx) != 0) ) { - - /* check variable beginning */ - if (!wild_card_start) return(FALSE); - else ptest++; - } - - /* check segment found */ - if (strlen(ptest) < inx) return(FALSE); - - /* adjust pointers and wild card indication */ - wild_card_start = TRUE; - ptest += inx; - pspec += inx; - - /* check for end of specification */ - if (*pspec == 0) { - if (*ptest == 0) return(TRUE); - else return(FALSE); - } - } -} long epicsShareAPI dbgrep(const char *pmask) { @@ -262,7 +216,7 @@ long epicsShareAPI dbgrep(const char *pmask) status = dbFirstRecord(pdbentry); while(!status) { pname = dbGetRecordName(pdbentry); - if (specified_by(pname, pmask)) printf("%s\n", pname); + if (epicsStrGlobMatch(pname, pmask)) printf("%s\n", pname); status = dbNextRecord(pdbentry); } status = dbNextRecordType(pdbentry); diff --git a/src/iocsh/iocsh.cpp b/src/iocsh/iocsh.cpp index 464dedaf4..c69600ff3 100644 --- a/src/iocsh/iocsh.cpp +++ b/src/iocsh/iocsh.cpp @@ -370,15 +370,15 @@ static void helpCallFunc(const iocshArgBuf *args) int argc = args[0].aval.ac; const char * const * argv = args[0].aval.av; struct iocshFuncDef const *piocshFuncDef; - struct iocshCommand *found; + struct iocshCommand *pcmd; if (argc == 1) { int l, col = 0; printf ("Type `help command_name' to get more information about a particular command.\n"); iocshTableLock (); - for (found = iocshCommandHead ; found != NULL ; found = found->next) { - piocshFuncDef = found->pFuncDef; + for (pcmd = iocshCommandHead ; pcmd != NULL ; pcmd = pcmd->next) { + piocshFuncDef = pcmd->pFuncDef; l = strlen (piocshFuncDef->name); if ((l + col) >= 79) { fputc ('\n', stdout); @@ -403,24 +403,22 @@ static void helpCallFunc(const iocshArgBuf *args) } else { for (int iarg = 1 ; iarg < argc ; iarg++) { - found = (iocshCommand *)registryFind (iocshCmdID, argv[iarg]); - if (found == NULL) { - printf ("%s -- no such command.\n", argv[iarg]); - } - else { - piocshFuncDef = found->pFuncDef; - fputs (piocshFuncDef->name, stdout); - for (int a = 0 ; a < piocshFuncDef->nargs ; a++) { - const char *cp = piocshFuncDef->arg[a]->name; - if ((piocshFuncDef->arg[a]->type == iocshArgArgv) - || (strchr (cp, ' ') == NULL)) { - fprintf (stdout, " %s", cp); - } - else { - fprintf (stdout, " '%s'", cp); + for (pcmd = iocshCommandHead ; pcmd != NULL ; pcmd = pcmd->next) { + piocshFuncDef = pcmd->pFuncDef; + if (epicsStrGlobMatch(piocshFuncDef->name, argv[iarg]) != 0) { + fputs (piocshFuncDef->name, stdout); + for (int a = 0 ; a < piocshFuncDef->nargs ; a++) { + const char *cp = piocshFuncDef->arg[a]->name; + if ((piocshFuncDef->arg[a]->type == iocshArgArgv) + || (strchr (cp, ' ') == NULL)) { + fprintf (stdout, " %s", cp); + } + else { + fprintf (stdout, " '%s'", cp); + } } + fprintf (stdout,"\n");; } - fprintf (stdout,"\n");; } } } diff --git a/src/libCom/misc/epicsString.c b/src/libCom/misc/epicsString.c index be0e69f73..d2fd3b6f5 100644 --- a/src/libCom/misc/epicsString.c +++ b/src/libCom/misc/epicsString.c @@ -204,3 +204,50 @@ epicsShareFunc int epicsShareAPI epicsStrSnPrintEscaped( } return nout; } +epicsShareFunc int epicsShareAPI epicsStrGlobMatch( + const char *str, const char *pattern) +{ + int inx; + int wild_card_start; + + /* check if the specification begins with a wild card */ + if (*pattern == '*') wild_card_start = 1; + else wild_card_start = 0; + + /* check for specification */ + while (1) { + /* skip any wild cards */ + while (*pattern == '*') pattern++; + + /* find the specification chars to compare */ + inx = 0; + while ( (*(pattern+inx) != '*') && (*(pattern+inx)) ) + inx++; + + /* check for specification ending with wildcard */ + if (inx == 0) return 1; + + /* find the spec chars in the test string */ + while ((strlen(str) >= inx) + && (strncmp(str,pattern,inx) != 0) ) { + + /* check variable beginning */ + if (!wild_card_start) return 0; + else str++; + } + + /* check segment found */ + if (strlen(str) < inx) return 0; + + /* adjust pointers and wild card indication */ + wild_card_start = 1; + str += inx; + pattern += inx; + + /* check for end of specification */ + if (*pattern == '\0') { + if (*str == 0) return 1; + else return 0; + } + } +} diff --git a/src/libCom/misc/epicsString.h b/src/libCom/misc/epicsString.h index f41c5da90..aa694bacf 100644 --- a/src/libCom/misc/epicsString.h +++ b/src/libCom/misc/epicsString.h @@ -30,6 +30,8 @@ epicsShareFunc int epicsShareAPI epicsStrPrintEscaped( FILE *fp, const char *s, int n); epicsShareFunc int epicsShareAPI epicsStrSnPrintEscaped( char *outbuf, int outsize, const char *inbuf, int inlen); +epicsShareFunc int epicsShareAPI epicsStrGlobMatch( + const char *str, const char *pattern); #ifdef __cplusplus }