This is Unofficial EPICS BASE Doxygen Site
macEnv.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2009 UChicago Argonne LLC, as Operator of Argonne
3 * National Laboratory.
4 * EPICS BASE is distributed subject to a Software License Agreement found
5 * in file LICENSE that is included with this distribution.
6 \*************************************************************************/
7 /*
8  * Macro expansion of environment variables
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "errlog.h"
16 #include "epicsString.h"
17 #include "macLib.h"
18 
19 char * epicsStdCall
20 macEnvExpand(const char *str)
21 {
22  return macDefExpand(str, NULL);
23 }
24 
25 char * epicsStdCall
26 macDefExpand(const char *str, MAC_HANDLE *macros)
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
char *epicsStdCall macEnvExpand(const char *str)
Expand environment variables in a string.
Definition: macEnv.c:20
Macro substitution context, for use by macLib routines only.
Definition: macLib.h:42
char *epicsStdCall macDefExpand(const char *str, MAC_HANDLE *macros)
Expands macros and environment variables in a string.
Definition: macEnv.c:26
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
Text macro substitution routines.