From 3ec821a74acc6cc09aa7367bed7d8e531daa4140 Mon Sep 17 00:00:00 2001 From: "W. Eric Norum" Date: Mon, 7 May 2001 16:11:57 +0000 Subject: [PATCH] Create os-specific versions of routines to set and display environment variables. --- src/iocsh/iocUtil.c | 70 +++++++++++---------------- src/libCom/Makefile | 1 + src/libCom/env/envDefs.h | 7 +-- src/libCom/env/envSubr.c | 76 ------------------------------ src/libCom/osi/os/default/osdEnv.c | 73 ++++++++++++++++++++++++++++ src/libCom/osi/os/vxWorks/osdEnv.c | 69 +++++++++++++++++++++++++++ 6 files changed, 174 insertions(+), 122 deletions(-) create mode 100644 src/libCom/osi/os/default/osdEnv.c create mode 100644 src/libCom/osi/os/vxWorks/osdEnv.c diff --git a/src/iocsh/iocUtil.c b/src/iocsh/iocUtil.c index e82c65c26..139b3a867 100644 --- a/src/iocsh/iocUtil.c +++ b/src/iocsh/iocUtil.c @@ -93,57 +93,41 @@ static void showCallFunc(const ioccrfArgBuf *args) } } -/* putenv */ -static const ioccrfArg putenvArg0 = { "environment_variable=name",ioccrfArgString}; -static const ioccrfArg * const putenvArgs[1] = {&putenvArg0}; -static const ioccrfFuncDef putenvFuncDef = {"putenv",1,putenvArgs}; -static void putenvCallFunc(const ioccrfArgBuf *args) +/* epicsEnvSet */ +static const ioccrfArg epicsEnvSetArg0 = { "name",ioccrfArgString}; +static const ioccrfArg epicsEnvSetArg1 = { "value",ioccrfArgString}; +static const ioccrfArg * const epicsEnvSetArgs[2] = {&epicsEnvSetArg0,&epicsEnvSetArg1}; +static const ioccrfFuncDef epicsEnvSetFuncDef = {"epicsEnvSet",2,epicsEnvSetArgs}; +static void epicsEnvSetCallFunc(const ioccrfArgBuf *args) { - char *arg0 = args[0].sval; - char *cp; + char *name = args[0].sval; + char *value = args[1].sval; - /* - * Some versions of putenv set the environment to - * point to the string that is passed so we have - * to make a copy before stashing it. - * Yes, this will cause memory leaks if the same variable is - * placed in the environment more than once. - */ - if (!arg0) + if (name == NULL) { + printf ("Missing environment variable name argument.\n"); return; - cp = calloc(strlen(arg0)+1,sizeof(char)); - strcpy(cp,arg0); - if ((cp == NULL) || putenv (cp)) { - free (cp); - printf ("putenv(%s) failed.\n", args[0].sval); } + if (value == NULL) { + printf ("Missing environment variable value argument.\n"); + return; + } + epicsEnvSet (name, value); } -/* epicsPrtEnvParams */ -static const ioccrfFuncDef epicsPrtEnvParamsFuncDef = {"epicsPrtEnvParams",0,NULL}; -static void epicsPrtEnvParamsCallFunc(const ioccrfArgBuf *args) +/* epicsParamShow */ +static const ioccrfFuncDef epicsParamShowFuncDef = {"epicsParamShow",0,NULL}; +static void epicsParamShowCallFunc(const ioccrfArgBuf *args) { epicsPrtEnvParams (); } -/* printEnv */ -static const ioccrfArg printEnvArg0 = { "name ...", ioccrfArgArgv}; -static const ioccrfArg * const printEnvArgs[1] = {&printEnvArg0}; -static const ioccrfFuncDef printEnvFuncDef = {"printEnv",1,printEnvArgs}; -static void printEnvCallFunc(const ioccrfArgBuf *args) +/* epicsEnvShow */ +static const ioccrfArg epicsEnvShowArg0 = { "[name ...]", ioccrfArgArgv}; +static const ioccrfArg * const epicsEnvShowArgs[1] = {&epicsEnvShowArg0}; +static const ioccrfFuncDef epicsEnvShowFuncDef = {"epicsEnvShow",1,epicsEnvShowArgs}; +static void epicsEnvShowCallFunc(const ioccrfArgBuf *args) { - int i = 1; - const char *cp; - int argc = args[0].aval.ac; - char **argv = args[0].aval.av; - - for (i = 1 ; i < argc ; i++) { - cp = getenv (argv[i]); - if (cp == NULL) - printf ("%s is not an environment variable.\n", argv[i]); - else - printf ("%s\n", cp); - } + epicsEnvShow (args[0].aval.ac, args[0].aval.av); } /* iocLogInit */ @@ -159,8 +143,8 @@ void epicsShareAPI iocUtilRegister(void) ioccrfRegister(&chdirFuncDef,chdirCallFunc); ioccrfRegister(&pwdFuncDef,pwdCallFunc); ioccrfRegister(&showFuncDef,showCallFunc); - ioccrfRegister(&putenvFuncDef,putenvCallFunc); - ioccrfRegister(&epicsPrtEnvParamsFuncDef,epicsPrtEnvParamsCallFunc); - ioccrfRegister(&printEnvFuncDef,printEnvCallFunc); + ioccrfRegister(&epicsEnvSetFuncDef,epicsEnvSetCallFunc); + ioccrfRegister(&epicsParamShowFuncDef,epicsParamShowCallFunc); + ioccrfRegister(&epicsEnvShowFuncDef,epicsEnvShowCallFunc); ioccrfRegister(&iocLogInitFuncDef,iocLogInitCallFunc); } diff --git a/src/libCom/Makefile b/src/libCom/Makefile index 77188a123..608567cfa 100644 --- a/src/libCom/Makefile +++ b/src/libCom/Makefile @@ -160,6 +160,7 @@ SRCS += osdAssert.c SRCS += osdFindSymbol.c SRCS += osdInterrupt.c SRCS += osdPoolStatus.c +SRCS += osdEnv.c SRCS += osdThread.c SRCS += osdMutex.c diff --git a/src/libCom/env/envDefs.h b/src/libCom/env/envDefs.h index ffd6de5d6..d7aeb047f 100644 --- a/src/libCom/env/envDefs.h +++ b/src/libCom/env/envDefs.h @@ -111,8 +111,6 @@ epicsShareFunc const char * epicsShareAPI envGetConfigParamPtr(const ENV_PARAM *pParam); epicsShareFunc long epicsShareAPI envPrtConfigParam(const ENV_PARAM *pParam); -epicsShareFunc long epicsShareAPI - envSetConfigParam(const ENV_PARAM *pParam, char *value); epicsShareFunc long epicsShareAPI envGetInetAddrConfigParam(const ENV_PARAM *pParam, struct in_addr *pAddr); epicsShareFunc long epicsShareAPI @@ -124,16 +122,19 @@ epicsShareFunc const char * epicsShareAPI epicsShareFunc unsigned short epicsShareAPI envGetInetPortConfigParam (const ENV_PARAM *pEnv, unsigned short defaultPort); epicsShareFunc long epicsShareAPI epicsPrtEnvParams(void); +epicsShareFunc void epicsShareAPI epicsEnvSet (char *name, char *value); +epicsShareFunc void epicsShareAPI epicsEnvShow (int argc, char **argv); #else epicsShareFunc char * epicsShareAPI envGetConfigParam(); epicsShareFunc char * epicsShareAPI envGetConfigParamPtr(); epicsShareFunc long epicsShareAPI envPrtConfigParam(); -epicsShareFunc long epicsShareAPI envSetConfigParam(); epicsShareFunc long epicsShareAPI envGetInetAddrConfigParam(); epicsShareFunc long epicsShareAPI envGetDoubleConfigParam(); epicsShareFunc long epicsShareAPI envGetLongConfigParam(); epicsShareFunc char * epicsShareAPI envGetConfigParamPtr(); epicsShareFunc unsigned short epicsShareAPI envGetInetPortConfigParam(); +epicsShareFunc void epicsShareAPI epicsEnvSet (); +epicsShareFunc void epicsShareAPI epicsEnvShow (); #endif #ifdef __cplusplus diff --git a/src/libCom/env/envSubr.c b/src/libCom/env/envSubr.c index 8a73ea3a0..368ad0149 100644 --- a/src/libCom/env/envSubr.c +++ b/src/libCom/env/envSubr.c @@ -64,7 +64,6 @@ * long envGetDoubleConfigParam( pParam, pDouble ) * long envGetInetAddrConfigParam( pParam, pAddr ) * long envPrtConfigParam( pParam ) -* long envSetConfigParam( pParam, valueString ) * * SEE ALSO * $epics/share/bin/envSetupParams, envDefs.h @@ -385,81 +384,6 @@ const ENV_PARAM *pParam) /* pointer to config param structure */ return 0; } -/*+/subr********************************************************************** -* NAME envSetConfigParam - set value of a configuration parameter -* -* DESCRIPTION -* Sets the value of a configuration parameter. -* -* RETURNS -* 0 -* -* NOTES -* 1. Performs a useful function only under VxWorks. -* -* EXAMPLE -* 1. Set the value for the EPICS-defined environment parameter -* EPICS_TS_MIN_WEST to 360, for USA central time zone. -* -* Under UNIX: -* -* % setenv EPICS_TS_MIN_WEST 360 -* -* In a program running under VxWorks: -* -* #include "envDefs.h" -* -* envSetConfigParam(&EPICS_TS_MIN_WEST, "360"); -* -* Under the VxWorks command shell: -* -* envSetConfigParam &EPICS_TS_MIN_WEST,"360" -* -*-*/ -long epicsShareAPI envSetConfigParam ( -const ENV_PARAM *pParam, /* I pointer to config param structure */ -char *value /* I pointer to value string */ -) -{ - long retCode = 0; - int status; - char *pEnv; - - /* - * space for two strings, an '=' character, - * and a null termination - */ - pEnv = malloc (strlen (pParam->name) + strlen (value) + 2); - if (!pEnv) { - errPrintf( - -1L, - __FILE__, - __LINE__, -"Failed to set environment parameter \"%s\" to \"%s\" because \"%s\"\n", - pParam->name, - value, - strerror (errno)); - return -1L; - } - - strcpy (pEnv, pParam->name); - strcat (pEnv, "="); - strcat (pEnv, value); - status = putenv (pEnv); - if (status<0) { - errPrintf( - -1L, - __FILE__, - __LINE__, -"Failed to set environment parameter \"%s\" to \"%s\" because \"%s\"\n", - pParam->name, - value, - strerror (errno)); - retCode = -1L; - } - return retCode; -} - /*+/subr********************************************************************** * NAME epicsPrtEnvParams - print value of all configuration parameters * diff --git a/src/libCom/osi/os/default/osdEnv.c b/src/libCom/osi/os/default/osdEnv.c new file mode 100644 index 000000000..929d462fe --- /dev/null +++ b/src/libCom/osi/os/default/osdEnv.c @@ -0,0 +1,73 @@ +/* osdEnv.c */ +/* + * $Id$ + * + * Author: Eric Norum + * Date: May 7, 2001 + * + * Routines to modify/display environment variables and EPICS parameters + * + */ + +#include +#include +#include +#include + +#include +#include +#include + +#define epicsExportSharedSymbols +#include "epicsFindSymbol.h" + +/* + * Set the value of an environment variable + * Leaks memory, but the assumption is that this routine won't be + * called often enough for the leak to be a problem. + */ +epicsShareFunc void epicsShareAPI epicsEnvSet (char *name, char *value) +{ + char *cp; + + cp = mallocMustSucceed (strlen (name) + strlen (value) + 2, "epicsEnvSet"); + strcpy (cp, name); + strcat (cp, "="); + strcat (cp, value); + if (putenv (cp) < 0) { + errPrintf( + -1L, + __FILE__, + __LINE__, + "Failed to set environment parameter \"%s\" to \"%s\": %s\n", + name, + value, + strerror (errno)); + free (cp); + } +} + +/* + * Show the value of the specified, or all, environment variables + */ +epicsShareFunc void epicsShareAPI epicsEnvShow (int argc, char **argv) +{ + if (argc <= 1) { + extern char **environ; + char **sp; + + for (sp = environ ; (sp != NULL) && (*sp != NULL) ; sp++) + printf ("%s\n", *sp); + } + else { + int i; + + for (i = 1 ; i < argc ; i++) { + const char *cp = getenv (argv[i]); + if (cp == NULL) + printf ("%s is not an environment variable.\n", argv[i]); + else + printf ("%s=%s\n", argv[i], cp); + } + } +} diff --git a/src/libCom/osi/os/vxWorks/osdEnv.c b/src/libCom/osi/os/vxWorks/osdEnv.c new file mode 100644 index 000000000..21b586871 --- /dev/null +++ b/src/libCom/osi/os/vxWorks/osdEnv.c @@ -0,0 +1,69 @@ +/* osdEnv.c */ +/* + * $Id$ + * + * Author: Eric Norum + * Date: May 7, 2001 + * + * Routines to modify/display environment variables and EPICS parameters + * + */ + +#include +#include +#include +#include + +#include +#include +#include + +#define epicsExportSharedSymbols +#include "epicsFindSymbol.h" + +/* + * Set the value of an environment variable + * Leaks memory, but the assumption is that this routine won't be + * called often enough for the leak to be a problem. + */ +epicsShareFunc void epicsShareAPI epicsEnvSet (char *name, char *value) +{ + char *cp; + + cp = mallocMustSucceed (strlen (name) + strlen (value) + 2, "epicsEnvSet"); + strcpy (cp, name); + strcat (cp, "="); + strcat (cp, value); + if (putenv (cp) < 0) { + errPrintf( + -1L, + __FILE__, + __LINE__, + "Failed to set environment parameter \"%s\" to \"%s\": %s\n", + name, + value, + strerror (errno)); + free (cp); + } +} + +/* + * Show the value of the specified, or all, environment variables + */ +epicsShareFunc void epicsShareAPI epicsEnvShow (int argc, char **argv) +{ + if (argc <= 1) { + envShow (0); + } + else { + int i; + + for (i = 1 ; i < argc ; i++) { + const char *cp = getenv (argv[i]); + if (cp == NULL) + printf ("%s is not an environment variable.\n", argv[i]); + else + printf ("%s=%s\n", argv[i], cp); + } + } +}