diff --git a/src/libCom/iocsh/iocsh.cpp b/src/libCom/iocsh/iocsh.cpp index a3cbd22a1..c140e56a9 100644 --- a/src/libCom/iocsh/iocsh.cpp +++ b/src/libCom/iocsh/iocsh.cpp @@ -545,10 +545,19 @@ iocshBody (const char *pathname, const char *commandLine) lineno++; /* - * Ignore comment lines other than to echo - * them if they came from a script. + * Skip leading white-space */ - if (*raw == '#') { + icin = 0; + while ((c = raw[icin]) && isspace(c)) { + icin++; + } + + /* + * Ignore comment lines other than to echo + * them if they came from a script. This + * avoids macLib errors from comments. + */ + if (c == '#') { if ((prompt == NULL) && (commandLine == NULL)) puts(raw); continue; @@ -562,15 +571,28 @@ iocshBody (const char *pathname, const char *commandLine) continue; /* - * Echo commands read from scripts + * Skip leading white-space coming from a macro + */ + while ((c = line[icin]) && isspace(c)) { + icin++; + } + + /* + * Echo non-empty lines read from a script */ if ((prompt == NULL) && *line && (commandLine == NULL)) puts(line); + /* + * Ignore lines that became a comment or empty after macro expansion + */ + if (!c || c == '#') + continue; + /* * Break line into words */ - icout = icin = 0; + icout = 0; inword = 0; argc = 0; quote = EOF; diff --git a/src/libCom/macLib/macUtil.c b/src/libCom/macLib/macUtil.c index 904d0f889..6076a6258 100644 --- a/src/libCom/macLib/macUtil.c +++ b/src/libCom/macLib/macUtil.c @@ -3,11 +3,10 @@ * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. -* EPICS BASE Versions 3.13.7 -* and higher are distributed subject to a Software License Agreement found +* EPICS BASE is distributed subject to a Software License Agreement found * in file LICENSE that is included with this distribution. \*************************************************************************/ -/* Revision-Id: anj@aps.anl.gov-20101005192737-disfz3vs0f3fiixd +/* Revision-Id: anj@aps.anl.gov-20131119212622-758nq4pce6q9ahih * * Implementation of utility macro substitution library (macLib) * @@ -51,11 +50,11 @@ epicsShareAPI macParseDefns( int quote; int escape; size_t nbytes; - const unsigned char **ptr; - const unsigned char **end; + const char **ptr; + const char **end; int *del; char *memCp, **memCpp; - const unsigned char *c; + const char *c; char *s, *d, **p; enum { preName, inName, preValue, inValue } state; @@ -68,8 +67,8 @@ epicsShareAPI macParseDefns( numMax = strlen( defns ); if ( numMax < altNumMax ) numMax = altNumMax; - ptr = (const unsigned char **) calloc( numMax, sizeof( char * ) ); - end = (const unsigned char **) calloc( numMax, sizeof( char * ) ); + ptr = (const char **) calloc( numMax, sizeof( char * ) ); + end = (const char **) calloc( numMax, sizeof( char * ) ); del = (int *) calloc( numMax, sizeof( int ) ); if ( ptr == NULL || end == NULL || del == NULL ) goto error; @@ -80,7 +79,7 @@ epicsShareAPI macParseDefns( del[0] = FALSE; quote = 0; state = preName; - for ( c = (const unsigned char *) defns; *c != '\0'; c++ ) { + for ( c = (const char *) defns; *c != '\0'; c++ ) { /* handle quotes */ if ( quote ) @@ -226,8 +225,8 @@ epicsShareAPI macParseDefns( } /* free workspace */ - free( ptr ); - free( end ); + free( ( void * ) ptr ); + free( ( void * ) end ); free( ( char * ) del ); /* debug output */ @@ -240,8 +239,8 @@ epicsShareAPI macParseDefns( /* error exit */ error: errlogPrintf( "macParseDefns: failed to allocate memory\n" ); - if ( ptr != NULL ) free( ptr ); - if ( end != NULL ) free( end ); + if ( ptr != NULL ) free( ( void * ) ptr ); + if ( end != NULL ) free( ( void * ) end ); if ( del != NULL ) free( ( char * ) del ); *pairs = NULL; return -1; diff --git a/src/libCom/misc/aToIPAddr.c b/src/libCom/misc/aToIPAddr.c index c5d53bc30..04809bffe 100644 --- a/src/libCom/misc/aToIPAddr.c +++ b/src/libCom/misc/aToIPAddr.c @@ -1,11 +1,10 @@ /*************************************************************************\ -* Copyright (c) 2002 The University of Chicago, as Operator of Argonne +* Copyright (c) 2012 UChicago Argonne LLC, as Operator of Argonne * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. -* EPICS BASE Versions 3.13.7 -* and higher are distributed subject to a Software License Agreement found -* in file LICENSE that is included with this distribution. +* EPICS BASE is distributed subject to a Software License Agreement found +* in file LICENSE that is included with this distribution. \*************************************************************************/ /* * rational replacement for inet_addr() @@ -13,49 +12,25 @@ * author: Jeff Hill */ #include -#include -#include #include #define epicsExportSharedSymbols #include "osiSock.h" - -#ifndef NELEMENTS -#define NELEMENTS(A) (sizeof(A)/sizeof(A[0])) -#endif /*NELEMENTS*/ - -/* - * addrArrayToUL () - */ -static int addrArrayToUL (const unsigned short *pAddr, unsigned nElements, struct in_addr *pIpAddr) -{ - unsigned i; - unsigned long addr = 0ul; - - for ( i=0u; i < nElements; i++ ) { - if ( pAddr[i] > 0xff ) { - return -1; - } - addr <<= 8; - addr |= pAddr[i]; - } - pIpAddr->s_addr = htonl ( addr ); - - return 0; -} +#include "epicsStdlib.h" /* * initIPAddr() * !! ipAddr should be passed in in network byte order !! * !! port is passed in in host byte order !! */ -static int initIPAddr (struct in_addr ipAddr, unsigned short port, struct sockaddr_in *pIP) +static int initIPAddr (struct in_addr ipAddr, unsigned short port, + struct sockaddr_in *pIP) { - memset (pIP, '\0', sizeof(*pIP)); - pIP->sin_family = AF_INET; - pIP->sin_port = htons(port); - pIP->sin_addr = ipAddr; - return 0; + memset(pIP, '\0', sizeof(*pIP)); + pIP->sin_family = AF_INET; + pIP->sin_port = htons(port); + pIP->sin_addr = ipAddr; + return 0; } /* @@ -69,68 +44,46 @@ static int initIPAddr (struct in_addr ipAddr, unsigned short port, struct sockad * "pAddrString" does not contain an address of the form * "n.n.n.n:p" */ -epicsShareFunc int epicsShareAPI - aToIPAddr(const char *pAddrString, unsigned short defaultPort, struct sockaddr_in *pIP) +epicsShareFunc int epicsShareAPI +aToIPAddr(const char *pAddrString, unsigned short defaultPort, + struct sockaddr_in *pIP) { - int status; - unsigned short addr[4]; - unsigned long rawAddr; - char hostName[512]; /* !! change n elements here requires change in format below !! */ - unsigned short port; - struct in_addr ina; + int status; + char hostName[512]; /* !! change n elements here requires change in format below !! */ + char *endp; + unsigned int port; + unsigned long numaddr; + struct in_addr ina; - /* - * dotted ip addresses - */ - status = sscanf (pAddrString, " %hu.%hu.%hu.%hu:%hu", - addr, addr+1u, addr+2u, addr+3u, &port); - if (status>0) { - if (status>=4) { - if ( addrArrayToUL ( addr, NELEMENTS ( addr ), &ina ) < 0 ) { - return -1; - } - if (status==4) { - port = defaultPort; - } - return initIPAddr (ina, port, pIP); - } - else { - return -1; - } - } - - /* - * IP address as a raw number - */ - status = sscanf ( pAddrString, " %lu:%hu", &rawAddr, &port ); - if (status>=1) { - if ( rawAddr > 0xffffffff ) { - return -1; - } - if ( status == 1 ) { - port = defaultPort; - } - ina.s_addr = htonl ( rawAddr ); - return initIPAddr ( ina, port, pIP ); - } - - /* - * check for a valid host name before giving up - */ - status = sscanf ( pAddrString, " %511[^:]:%hu", hostName, &port ); - if ( status >= 1 ) { - if ( status == 1 ) { - port = defaultPort; - } - status = hostToIPAddr ( hostName, &ina ); - if ( status == 0 ) { - return initIPAddr ( ina, port, pIP ); - } - else { - return -1; - } - } - else { + /* + * Scan for a port number + */ + status = sscanf( pAddrString, " %511[^:]:%u", hostName, &port ); + if ( status == 0 ) { return -1; } + if ( status == 1 ) { + port = defaultPort; + } + else if (status == 2 && port > 65535) { + return -1; + } + + /* + * Look for a valid host name or dotted quad + */ + status = hostToIPAddr( hostName, &ina ); + if ( status == 0 ) { + return initIPAddr( ina, port, pIP ); + } + + /* + * Try the IP address as a decimal integer + */ + numaddr = strtoul( hostName, &endp, 10 ); + if (*endp) + return -1; + + ina.s_addr = htonl( numaddr ); + return initIPAddr( ina, port, pIP ); } diff --git a/src/libCom/misc/epicsConvert.h b/src/libCom/misc/epicsConvert.h index 9674db0ef..1c38bc30a 100644 --- a/src/libCom/misc/epicsConvert.h +++ b/src/libCom/misc/epicsConvert.h @@ -3,12 +3,14 @@ * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. -* EPICS BASE Versions 3.13.7 -* and higher are distributed subject to a Software License Agreement found -* in file LICENSE that is included with this distribution. +* EPICS BASE is distributed subject to a Software License Agreement found +* in file LICENSE that is included with this distribution. \*************************************************************************/ /*epicsConvert.h*/ +#ifndef INC_epicsConvert_H +#define INC_epicsConvert_H + #include #ifdef __cplusplus @@ -21,3 +23,4 @@ epicsShareFunc float epicsConvertDoubleToFloat(double value); } #endif +#endif /* INC_epicsConvert_H */ diff --git a/src/libCom/misc/epicsStdlib.h b/src/libCom/misc/epicsStdlib.h index 84d4af062..78f822e4a 100644 --- a/src/libCom/misc/epicsStdlib.h +++ b/src/libCom/misc/epicsStdlib.h @@ -3,13 +3,15 @@ * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. -* EPICS BASE Versions 3.13.7 -* and higher are distributed subject to a Software License Agreement found -* in file LICENSE that is included with this distribution. +* EPICS BASE is distributed subject to a Software License Agreement found +* in file LICENSE that is included with this distribution. \*************************************************************************/ /*epicsStdlib.h*/ /*Author: Eric Norum */ +#ifndef INC_epicsStdlib_H +#define INC_epicsStdlib_H + #include #ifdef __cplusplus @@ -26,3 +28,4 @@ epicsShareFunc int epicsScanFloat(const char *str, float *dest); } #endif +#endif /* INC_epicsStdlib_H */