This is Unofficial EPICS BASE Doxygen Site
macEnv.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "errlog.h"
#include "epicsString.h"
#include "macLib.h"
+ Include dependency graph for macEnv.c:

Go to the source code of this file.

Functions

char *epicsStdCall macEnvExpand (const char *str)
 Expand environment variables in a string. More...
 
char *epicsStdCall macDefExpand (const char *str, MAC_HANDLE *macros)
 Expands macros and environment variables in a string. More...
 

Function Documentation

char* epicsStdCall macDefExpand ( const char *  str,
MAC_HANDLE macros 
)

Expands macros and environment variables in a string.

Returns
Expanded string; NULL if any undefined macros were used.

This routine is similar to macEnvExpand() but allows an optional handle to be passed in that may contain additional macro definitions. These macros are appended to the set of macros from environment variables when expanding the string.

Parameters
strstring to be expanded
macrosopaque handle; may be NULL if only environment variables are to be used

Definition at line 26 of file macEnv.c.

27 {
28  MAC_HANDLE *handle;
29  static const char * pairs[] = { "", "environ", NULL, NULL };
30  long destCapacity = 128;
31  char *dest = NULL;
32  int n;
33 
34  if (macros) {
35  handle = macros;
36  } else {
37  if (macCreateHandle(&handle, pairs)){
38  errlogMessage("macDefExpand: macCreateHandle failed.");
39  return NULL;
40  }
41  }
42 
43  do {
44  destCapacity *= 2;
45  /*
46  * Use free/malloc rather than realloc since there's no need to
47  * keep the original contents.
48  */
49  free(dest);
50  dest = malloc(destCapacity);
51  if(!dest)
52  goto done;
53 
54  n = macExpandString(handle, str, dest, destCapacity);
55  } while (n >= (destCapacity - 1));
56 
57  if (n < 0) {
58  free(dest);
59  dest = NULL;
60  } else {
61  size_t unused = destCapacity - ++n;
62 
63  if (unused >= 20)
64  dest = realloc(dest, n);
65  }
66 
67 done:
68  if (macros == NULL) {
69  if (macDeleteHandle(handle)) {
70  errlogMessage("macDefExpand: macDeleteHandle failed.");
71  }
72  }
73  return dest;
74 }
#define NULL
Definition: catime.c:38
#define str(v)
long epicsStdCall macExpandString(MAC_HANDLE *handle, const char *src, char *dest, long capacity)
Expand a string which may contain macro references.
Definition: macCore.c:174
Macro substitution context, for use by macLib routines only.
Definition: macLib.h:42
int errlogMessage(const char *message)
Definition: errlog.c:180
long epicsStdCall macDeleteHandle(MAC_HANDLE *handle)
Marks a handle invalid, and frees all storage associated with it.
Definition: macCore.c:360
long epicsStdCall macCreateHandle(MAC_HANDLE **pHandle, const char *pairs[])
Creates a new macro substitution context.
Definition: macCore.c:100
void done(int k)
Definition: antelope.c:77
char* epicsStdCall macEnvExpand ( const char *  str)

Expand environment variables in a string.

Returns
Expanded string; NULL if any undefined macros were used.

This routine expands a string which may contain macros that are environment variables. It parses the string looking for such references and passes them to macGetValue() for translation. It uses malloc() to allocate space for the expanded string and returns a pointer to this null-terminated string. It returns NULL if the source string contains any undefined references.

Parameters
strstring to be expanded

Definition at line 20 of file macEnv.c.

21 {
22  return macDefExpand(str, NULL);
23 }
#define NULL
Definition: catime.c:38
#define str(v)
char *epicsStdCall macDefExpand(const char *str, MAC_HANDLE *macros)
Expands macros and environment variables in a string.
Definition: macEnv.c:26