From 1d056c6fe42bc645f50c823d8b5861faa2f1b554 Mon Sep 17 00:00:00 2001 From: Doug Murray Date: Tue, 6 Dec 2022 12:47:50 -0800 Subject: [PATCH] Add support for CA tools timeout from environment variable EPICS_CLI_TIMEOUT --- modules/ca/src/tools/caget.c | 6 ++++-- modules/ca/src/tools/cainfo.c | 11 +++++++++-- modules/ca/src/tools/camonitor.c | 11 +++++++++-- modules/ca/src/tools/caput.c | 13 ++++++++++--- modules/ca/src/tools/tool_lib.c | 22 +++++++++++++++++++++- modules/ca/src/tools/tool_lib.h | 1 + modules/libcom/src/misc/epicsStdlib.h | 4 ++-- 7 files changed, 56 insertions(+), 12 deletions(-) diff --git a/modules/ca/src/tools/caget.c b/modules/ca/src/tools/caget.c index e60f61922..d24fcd066 100644 --- a/modules/ca/src/tools/caget.c +++ b/modules/ca/src/tools/caget.c @@ -392,6 +392,8 @@ int main (int argc, char *argv[]) LINE_BUFFER(stdout); /* Configure stdout buffering */ + use_ca_timeout_env ( &caTimeout); + while ((opt = getopt(argc, argv, ":taicnhsSVe:f:g:l:#:d:0:w:p:F:")) != -1) { switch (opt) { case 'h': /* Print usage */ @@ -437,8 +439,8 @@ int main (int argc, char *argv[]) if(epicsScanDouble(optarg, &caTimeout) != 1) { fprintf(stderr, "'%s' is not a valid timeout value " - "- ignored. ('caget -h' for help.)\n", optarg); - caTimeout = DEFAULT_TIMEOUT; + "- ignored, using '%.1f'. ('caget -h' for help.)\n", + optarg, caTimeout); } break; case '#': /* Array count */ diff --git a/modules/ca/src/tools/cainfo.c b/modules/ca/src/tools/cainfo.c index 7fa4dfb45..1bedcf52e 100644 --- a/modules/ca/src/tools/cainfo.c +++ b/modules/ca/src/tools/cainfo.c @@ -141,6 +141,8 @@ int main (int argc, char *argv[]) LINE_BUFFER(stdout); /* Configure stdout buffering */ + use_ca_timeout_env ( &caTimeout); + while ((opt = getopt(argc, argv, ":nhVw:s:p:")) != -1) { switch (opt) { case 'h': /* Print usage */ @@ -150,11 +152,16 @@ int main (int argc, char *argv[]) printf( "\nEPICS Version %s, CA Protocol version %s\n", EPICS_VERSION_STRING, ca_version() ); return 0; case 'w': /* Set CA timeout value */ + /* + * epicsScanDouble is a macro defined as epicsParseDouble, + * (found in modules/libcom/src/misc) which will only + * change caTimeout here if it finds an acceptable value. + */ if(epicsScanDouble(optarg, &caTimeout) != 1) { fprintf(stderr, "'%s' is not a valid timeout value " - "- ignored. ('cainfo -h' for help.)\n", optarg); - caTimeout = DEFAULT_TIMEOUT; + "- ignored, using '%.1f'. ('cainfo -h' for help.)\n", + optarg, caTimeout); } break; case 's': /* ca_client_status interest level */ diff --git a/modules/ca/src/tools/camonitor.c b/modules/ca/src/tools/camonitor.c index b272bc17f..c2bd10a74 100644 --- a/modules/ca/src/tools/camonitor.c +++ b/modules/ca/src/tools/camonitor.c @@ -219,6 +219,8 @@ int main (int argc, char *argv[]) LINE_BUFFER(stdout); /* Configure stdout buffering */ + use_ca_timeout_env ( &caTimeout); + while ((opt = getopt(argc, argv, ":nhVm:sSe:f:g:l:#:0:w:t:p:F:")) != -1) { switch (opt) { case 'h': /* Print usage */ @@ -251,11 +253,16 @@ int main (int argc, char *argv[]) } break; case 'w': /* Set CA timeout value */ + /* + * epicsScanDouble is a macro defined as epicsParseDouble, + * (found in modules/libcom/src/misc) which will only + * change caTimeout here if it finds an acceptable value. + */ if(epicsScanDouble(optarg, &caTimeout) != 1) { fprintf(stderr, "'%s' is not a valid timeout value " - "- ignored. ('camonitor -h' for help.)\n", optarg); - caTimeout = DEFAULT_TIMEOUT; + "- ignored, using '%.1f'. ('camonitor -h' for help.)\n", + optarg, caTimeout); } break; case '#': /* Array count */ diff --git a/modules/ca/src/tools/caput.c b/modules/ca/src/tools/caput.c index 05247643a..3f368469c 100644 --- a/modules/ca/src/tools/caput.c +++ b/modules/ca/src/tools/caput.c @@ -284,6 +284,8 @@ int main (int argc, char *argv[]) LINE_BUFFER(stdout); /* Configure stdout buffering */ putenv("POSIXLY_CORRECT="); /* Behave correct on GNU getopt systems */ + use_ca_timeout_env ( &caTimeout); + while ((opt = getopt(argc, argv, ":cnlhatsVS#:w:p:F:")) != -1) { switch (opt) { case 'h': /* Print usage */ @@ -318,11 +320,16 @@ int main (int argc, char *argv[]) request = callback; break; case 'w': /* Set CA timeout value */ + /* + * epicsScanDouble is a macro defined as epicsParseDouble, + * (found in modules/libcom/src/misc) which will only + * change caTimeout here if it finds an acceptable value. + */ if(epicsScanDouble(optarg, &caTimeout) != 1) { fprintf(stderr, "'%s' is not a valid timeout value " - "- ignored. ('caput -h' for help.)\n", optarg); - caTimeout = DEFAULT_TIMEOUT; + "- ignored, using '%.1f'. ('caput -h' for help.)\n", + optarg, caTimeout); } break; case '#': /* Array count */ @@ -337,7 +344,7 @@ int main (int argc, char *argv[]) if (sscanf(optarg,"%u", &caPriority) != 1) { fprintf(stderr, "'%s' is not a valid CA priority " - "- ignored. ('caget -h' for help.)\n", optarg); + "- ignored. ('caput -h' for help.)\n", optarg); caPriority = DEFAULT_CA_PRIORITY; } if (caPriority > CA_PRIORITY_MAX) caPriority = CA_PRIORITY_MAX; diff --git a/modules/ca/src/tools/tool_lib.c b/modules/ca/src/tools/tool_lib.c index 6d4d971b6..58bd3d047 100644 --- a/modules/ca/src/tools/tool_lib.c +++ b/modules/ca/src/tools/tool_lib.c @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -53,7 +54,7 @@ char fieldSeparator = ' '; /* OFS default is whitespace */ int enumAsNr = 0; /* used for -n option - get DBF_ENUM as number */ int charArrAsStr = 0; /* used for -S option - treat char array as (long) string */ -double caTimeout = 1.0; /* wait time default (see -w option) */ +double caTimeout = DEFAULT_TIMEOUT; /* wait time default (see -w option) */ capri caPriority = DEFAULT_CA_PRIORITY; /* CA Priority */ #define TIMETEXTLEN 28 /* Length of timestamp text buffer */ @@ -639,3 +640,22 @@ int connect_pvs (pv* pvs, int nPvs) } return returncode; } + + +/* Set the timeout to EPICS_CLI_TIMEOUT */ +void use_ca_timeout_env ( double* timeout) +{ + const char* tmoStr; /* contents of environment var */ + + if ((tmoStr = getenv("EPICS_CLI_TIMEOUT")) != NULL && timeout != NULL) + { + if(epicsScanDouble(tmoStr, timeout) != 1) + { + fprintf(stderr, "'%s' is not a valid timeout value " + "(from 'EPICS_CLI_TIMEOUT' in the environment) - " + "ignored. (use '-h' for help.)\n", tmoStr); + *timeout = DEFAULT_TIMEOUT; + } + + } +} diff --git a/modules/ca/src/tools/tool_lib.h b/modules/ca/src/tools/tool_lib.h index 0933fc8c2..ed3d8b0cd 100644 --- a/modules/ca/src/tools/tool_lib.h +++ b/modules/ca/src/tools/tool_lib.h @@ -100,6 +100,7 @@ extern char *dbr2str (const void *value, unsigned type); extern void print_time_val_sts (pv *pv, unsigned long reqElems); extern int create_pvs (pv *pvs, int nPvs, caCh *pCB ); extern int connect_pvs (pv *pvs, int nPvs ); +extern void use_ca_timeout_env (double* timeout); /* * no additions below this endif diff --git a/modules/libcom/src/misc/epicsStdlib.h b/modules/libcom/src/misc/epicsStdlib.h index 660ad9abc..dcb9fb584 100644 --- a/modules/libcom/src/misc/epicsStdlib.h +++ b/modules/libcom/src/misc/epicsStdlib.h @@ -87,8 +87,8 @@ LIBCOM_API int * \brief Convert a string to a double type * * \param str Pointer to a constant character array - * \param to Pointer to the specified type (this will be set during the conversion) - * \param units Pointer to a char * (this will be set with the units string) + * \param to Pointer to the specified type (this will be set only upon successful conversion) + * \param units Pointer to a char * (this will be set with the units string only upon successful conversion) * \return Status code (0=OK, see macro definitions for possible errors) */ LIBCOM_API int