This is Unofficial EPICS BASE Doxygen Site
envSubr.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE Versions 3.13.7
7 * and higher are distributed subject to a Software License Agreement found
8 * in file LICENSE that is included with this distribution.
9 \*************************************************************************/
10 /*
11  * Author: Roger A. Cole
12  * Date: 07-20-91
13  */
14 /*+/mod***********************************************************************
15 * TITLE envSubr.c - routines to get and set EPICS environment parameters
16 *
17 * DESCRIPTION
18 * These routines are oriented for use with EPICS environment
19 * parameters under UNIX and VxWorks. They may be used for other
20 * purposes, as well.
21 *
22 * Many EPICS environment parameters are predefined in envDefs.h.
23 *
24 * QUICK REFERENCE
25 * #include "envDefs.h"
26 * ENV_PARAM param;
27 * char *envGetConfigParamPtr( pParam )
28 * char *envGetConfigParam( pParam, bufDim, pBuf )
29 * long envGetLongConfigParam( pParam, pLong )
30 * long envGetDoubleConfigParam( pParam, pDouble )
31 * long envGetInetAddrConfigParam( pParam, pAddr )
32 * long envPrtConfigParam( pParam )
33 *
34 * SEE ALSO
35 * $epics/share/bin/envSetupParams, envDefs.h
36 *
37 *-***************************************************************************/
38 
39 #include <stdio.h>
40 #include <string.h>
41 #include <limits.h>
42 
43 #include "epicsStdlib.h"
44 #include "epicsStdio.h"
45 #include "epicsString.h"
46 #include "errMdef.h"
47 #include "errlog.h"
48 #include "envDefs.h"
49 #include "epicsAssert.h"
50 #include "osiSock.h"
51 
52 
53 /*+/subr**********************************************************************
54 * NAME envGetConfigParamPtr - returns a pointer to the configuration
55 * parameter value string
56 *
57 * DESCRIPTION
58 * Returns a pointer to a configuration parameter value.
59 * If the configuration parameter isn't found in the environment,
60 * then a pointer to the default value for the parameter is copied.
61 * If no parameter is found and there is no default, then
62 * NULL is returned.
63 *
64 * RETURNS
65 * pointer to the environment variable value string, or
66 * NULL if no parameter value and no default value was found
67 *
68 * EXAMPLES
69 * 1. Get the value for the EPICS-defined environment parameter
70 * EPICS_TS_NTP_INET.
71 *
72 * #include "envDefs.h"
73 * const char *pStr;
74 *
75 * pStr = envGetConfigParamPtr(&EPICS_TS_NTP_INET);
76 * if (pStr) {
77 * printf("NTP time server is: %s\n", pStr);
78 * }
79 *
80 *-*/
81 const char * epicsStdCall envGetConfigParamPtr(
82 const ENV_PARAM *pParam /* I pointer to config param structure */
83 )
84 {
85  const char *pEnv; /* pointer to environment string */
86 
87  pEnv = getenv(pParam->name);
88 
89  if (pEnv == NULL) {
90  pEnv = pParam->pdflt;
91  }
92 
93  if (pEnv) {
94  if (pEnv[0u] == '\0') {
95  pEnv = NULL;
96  }
97  }
98 
99  return pEnv;
100 }
101 
102 
103 /*+/subr**********************************************************************
104 * NAME envGetConfigParam - get value of a configuration parameter
105 *
106 * DESCRIPTION
107 * Gets the value of a configuration parameter and copies it
108 * into the caller's buffer. If the configuration parameter
109 * isn't found in the environment, then the default value for
110 * the parameter is copied. If no parameter is found and there
111 * is no default, then '\0' is copied and NULL is returned.
112 *
113 * RETURNS
114 * pointer to callers buffer, or
115 * NULL if no parameter value or default value was found
116 *
117 * EXAMPLES
118 * 1. Get the value for the EPICS-defined environment parameter
119 * EPICS_TS_NTP_INET.
120 *
121 * #include "envDefs.h"
122 * char temp[80];
123 *
124 * printf("NTP time server is: %s\n",
125 * envGetConfigParam(&EPICS_TS_NTP_INET, sizeof(temp), temp));
126 *
127 * 2. Get the value for the DISPLAY environment parameter under UNIX.
128 *
129 * #include "envDefs.h"
130 * char temp[80];
131 * ENV_PARAM display={"DISPLAY",""}
132 *
133 * if (envGetConfigParam(&display, sizeof(temp), temp) == NULL)
134 * printf("DISPLAY isn't defined\n");
135 * else
136 * printf("DISPLAY is %s\n", temp);
137 *
138 *-*/
139 char * epicsStdCall envGetConfigParam(
140 const ENV_PARAM *pParam,/* I pointer to config param structure */
141 int bufDim, /* I dimension of parameter buffer */
142 char *pBuf /* I pointer to parameter buffer */
143 )
144 {
145  const char *pEnv; /* pointer to environment string */
146 
147  pEnv = envGetConfigParamPtr(pParam);
148  if (!pEnv) {
149  return NULL;
150  }
151 
152  strncpy(pBuf, pEnv, bufDim-1);
153  pBuf[bufDim-1] = '\0';
154 
155  return pBuf;
156 }
157 
158 /*+/subr**********************************************************************
159 * NAME envGetDoubleConfigParam - get value of a double configuration parameter
160 *
161 * DESCRIPTION
162 * Gets the value of a configuration parameter and copies it into the
163 * caller's real (double) buffer. If the configuration parameter isn't
164 * found in the environment, then the default value for the parameter
165 * is copied.
166 *
167 * If no parameter is found and there is no default, then -1 is
168 * returned and the caller's buffer is unmodified.
169 *
170 * RETURNS
171 * 0, or
172 * -1 if an error is encountered
173 *
174 * EXAMPLE
175 * 1. Get the value for the real environment parameter EPICS_THRESHOLD.
176 *
177 * #include "envDefs.h"
178 * double threshold;
179 * long status;
180 *
181 * status = envGetDoubleConfigParam(&EPICS_THRESHOLD, &threshold);
182 * if (status == 0) {
183 * printf("the threshold is: %lf\n", threshold);
184 * }
185 * else {
186 * printf("%s could not be found or was not a real number\n",
187 * EPICS_THRESHOLD.name);
188 * }
189 *
190 *-*/
191 long epicsStdCall envGetDoubleConfigParam(
192 const ENV_PARAM *pParam,/* I pointer to config param structure */
193 double *pDouble /* O pointer to place to store value */
194 )
195 {
196  char text[128];
197  char *ptext;
198  int count;
199 
200  ptext = envGetConfigParam(pParam, sizeof text, text);
201  if (ptext != NULL) {
202  count = epicsScanDouble(text, pDouble);
203  if (count == 1) {
204  return 0;
205  }
206  (void)fprintf(stderr,"Unable to find a real number in %s=%s\n",
207  pParam->name, text);
208  }
209 
210  return -1;
211 }
212 
213 /*+/subr**********************************************************************
214 * NAME envGetInetAddrConfigParam - get value of an inet addr config parameter
215 *
216 * DESCRIPTION
217 * Gets the value of a configuration parameter and copies it into
218 * the caller's (struct in_addr) buffer. If the configuration parameter
219 * isn't found in the environment, then the default value for
220 * the parameter is copied.
221 *
222 * If no parameter is found and there is no default, then -1 is
223 * returned and the callers buffer is unmodified.
224 *
225 * RETURNS
226 * 0, or
227 * -1 if an error is encountered
228 *
229 * EXAMPLE
230 * 1. Get the value for the inet address environment parameter EPICS_INET.
231 *
232 * #include "envDefs.h"
233 * struct in_addr addr;
234 * long status;
235 *
236 * status = envGetInetAddrConfigParam(&EPICS_INET, &addr);
237 * if (status == 0) {
238 * printf("the s_addr is: %x\n", addr.s_addr);
239 * }
240 * else {
241 * printf("%s could not be found or was not an inet address\n",
242 * EPICS_INET.name);
243 * }
244 *
245 *-*/
246 long epicsStdCall envGetInetAddrConfigParam(
247 const ENV_PARAM *pParam,/* I pointer to config param structure */
248 struct in_addr *pAddr /* O pointer to struct to receive inet addr */
249 )
250 {
251  char text[128];
252  char *ptext;
253  long status;
254  struct sockaddr_in sin;
255 
256  ptext = envGetConfigParam(pParam, sizeof text, text);
257  if (ptext) {
258  status = aToIPAddr (text, 0u, &sin);
259  if (status == 0) {
260  *pAddr = sin.sin_addr;
261  return 0;
262  }
263  (void)fprintf(stderr,"Unable to find an IP address or valid host name in %s=%s\n",
264  pParam->name, text);
265  }
266  return -1;
267 }
268 
269 /*+/subr**********************************************************************
270 * NAME envGetLongConfigParam - get value of an integer config parameter
271 *
272 * DESCRIPTION
273 * Gets the value of a configuration parameter and copies it
274 * into the caller's integer (long) buffer. If the configuration
275 * parameter isn't found in the environment, then the default value for
276 * the parameter is copied.
277 *
278 * If no parameter is found and there is no default, then -1 is
279 * returned and the callers buffer is unmodified.
280 *
281 * RETURNS
282 * 0, or
283 * -1 if an error is encountered
284 *
285 * EXAMPLE
286 * 1. Get the value as a long for the integer environment parameter
287 * EPICS_NUMBER_OF_ITEMS.
288 *
289 * #include "envDefs.h"
290 * long count;
291 * long status;
292 *
293 * status = envGetLongConfigParam(&EPICS_NUMBER_OF_ITEMS, &count);
294 * if (status == 0) {
295 * printf("and the count is: %d\n", count);
296 * }
297 * else {
298 * printf("%s could not be found or was not an integer\n",
299 * EPICS_NUMBER_OF_ITEMS.name);
300 * }
301 *
302 *-*/
303 long epicsStdCall envGetLongConfigParam(
304 const ENV_PARAM *pParam,/* I pointer to config param structure */
305 long *pLong /* O pointer to place to store value */
306 )
307 {
308  char text[128];
309  char *ptext;
310  int count;
311 
312  ptext = envGetConfigParam(pParam, sizeof text, text);
313  if (ptext) {
314  count = sscanf(text, "%ld", pLong);
315  if (count == 1)
316  return 0;
317  (void)fprintf(stderr,"Unable to find an integer in %s=%s\n",
318  pParam->name, text);
319  }
320  return -1;
321 }
322 
323 
324 long epicsStdCall
325 envGetBoolConfigParam(const ENV_PARAM *pParam, int *pBool)
326 {
327  char text[20];
328 
329  if(!envGetConfigParam(pParam, sizeof(text), text))
330  return -1;
331  *pBool = epicsStrCaseCmp(text, "yes")==0;
332  return 0;
333 }
334 
335 /*+/subr**********************************************************************
336 * NAME envPrtConfigParam - print value of a configuration parameter
337 *
338 * DESCRIPTION
339 * Prints the value of a configuration parameter.
340 *
341 * RETURNS
342 * 0
343 *
344 * EXAMPLE
345 * 1. Print the value for the EPICS-defined environment parameter
346 * EPICS_TS_NTP_INET.
347 *
348 * #include "envDefs.h"
349 *
350 * envPrtConfigParam(&EPICS_TS_NTP_INET);
351 *
352 *-*/
353 long epicsStdCall envPrtConfigParam(
354 const ENV_PARAM *pParam) /* pointer to config param structure */
355 {
356  const char *pVal;
357 
358  pVal = envGetConfigParamPtr(pParam);
359  if (pVal == NULL)
360  fprintf(stdout, "%s is undefined\n", pParam->name);
361  else
362  fprintf(stdout,"%s: %s\n", pParam->name, pVal);
363  return 0;
364 }
365 
366 /*+/subr**********************************************************************
367 * NAME epicsPrtEnvParams - print value of all configuration parameters
368 *
369 * DESCRIPTION
370 * Prints all configuration parameters and their current value.
371 *
372 * RETURNS
373 * 0
374 *
375 * EXAMPLE
376 * 1. Print the value for all EPICS-defined environment parameters.
377 *
378 * #include "envDefs.h"
379 *
380 * epicsPrtEnvParams();
381 *
382 *-*/
383 long epicsStdCall
385 {
386  const ENV_PARAM **ppParam = env_param_list;
387 
388  while (*ppParam != NULL)
389  envPrtConfigParam(*(ppParam++));
390 
391  return 0;
392 }
393 
394 /*
395  * envGetInetPortConfigParam ()
396  */
397 LIBCOM_API unsigned short epicsStdCall envGetInetPortConfigParam
398  (const ENV_PARAM *pEnv, unsigned short defaultPort)
399 {
400  long longStatus;
401  long epicsParam;
402 
403  longStatus = envGetLongConfigParam (pEnv, &epicsParam);
404  if (longStatus!=0) {
405  epicsParam = (long) defaultPort;
406  errlogPrintf ("EPICS Environment \"%s\" integer fetch failed\n", pEnv->name);
407  errlogPrintf ("setting \"%s\" = %ld\n", pEnv->name, epicsParam);
408  }
409 
410  if (epicsParam<=IPPORT_USERRESERVED || epicsParam>USHRT_MAX) {
411  errlogPrintf ("EPICS Environment \"%s\" out of range\n", pEnv->name);
412  /*
413  * Quit if the port is wrong due coding error
414  */
415  assert (epicsParam != (long) defaultPort);
416  epicsParam = (long) defaultPort;
417  errlogPrintf ("Setting \"%s\" = %ld\n", pEnv->name, epicsParam);
418  }
419 
420  /*
421  * ok to clip to unsigned short here because we checked the range
422  */
423  return (unsigned short) epicsParam;
424 }
425 
long epicsStdCall envGetBoolConfigParam(const ENV_PARAM *pParam, int *pBool)
Get value of a boolean configuration parameter.
Definition: envSubr.c:325
LIBCOM_API int epicsStdCall aToIPAddr(const char *pAddrString, unsigned short defaultPort, struct sockaddr_in *pIP)
Definition: aToIPAddr.c:78
#define assert(exp)
Declare that a condition should be true.
Definition: epicsAssert.h:70
long epicsStdCall envGetDoubleConfigParam(const ENV_PARAM *pParam, double *pDouble)
Get value of a double configuration parameter.
Definition: envSubr.c:191
pvd::Status status
An EPICS-specific replacement for ANSI C&#39;s assert.
char * name
Name of the parameter.
Definition: envDefs.h:42
Routines to get and set EPICS environment parameters.
#define NULL
Definition: catime.c:38
#define epicsScanDouble(str, to)
Definition: epicsStdlib.h:78
long epicsStdCall envGetLongConfigParam(const ENV_PARAM *pParam, long *pLong)
Get value of a long configuration parameter.
Definition: envSubr.c:303
int epicsStrCaseCmp(const char *s1, const char *s2)
Definition: epicsString.c:191
char * pdflt
Default value.
Definition: envDefs.h:43
A structure to hold a single environment parameter.
Definition: envDefs.h:41
long epicsStdCall envGetInetAddrConfigParam(const ENV_PARAM *pParam, struct in_addr *pAddr)
Get value of an inet addr config parameter.
Definition: envSubr.c:246
long epicsStdCall epicsPrtEnvParams(void)
Prints all configuration parameters and their current value.
Definition: envSubr.c:384
int errlogPrintf(const char *pFormat,...)
Definition: errlog.c:105
#define stdout
Definition: epicsStdio.h:30
char *epicsStdCall envGetConfigParam(const ENV_PARAM *pParam, int bufDim, char *pBuf)
Get value of a configuration parameter.
Definition: envSubr.c:139
#define stderr
Definition: epicsStdio.h:32
const char *epicsStdCall envGetConfigParamPtr(const ENV_PARAM *pParam)
Get a configuration parameter&#39;s value or default string.
Definition: envSubr.c:81
LIBCOM_API unsigned short epicsStdCall envGetInetPortConfigParam(const ENV_PARAM *pEnv, unsigned short defaultPort)
Get value of a port number configuration parameter.
Definition: envSubr.c:398
long epicsStdCall envPrtConfigParam(const ENV_PARAM *pParam)
Print the value of a configuration parameter.
Definition: envSubr.c:353
LIBCOM_API const ENV_PARAM * env_param_list[]