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

Go to the source code of this file.

Classes

struct  mac_entry
 

Macros

#define MAC_MAGIC   0xbadcafe /* ...sells sub-standard coffee? */
 
#define FLAG_SUPPRESS_WARNINGS   0x1
 
#define FLAG_USE_ENVIRONMENT   0x80
 

Typedefs

typedef struct mac_entry MAC_ENTRY
 

Functions

long epicsStdCall macCreateHandle (MAC_HANDLE **pHandle, const char *pairs[])
 Creates a new macro substitution context. More...
 
void epicsStdCall macSuppressWarning (MAC_HANDLE *handle, int suppress)
 Disable or enable warning messages. More...
 
long epicsStdCall macExpandString (MAC_HANDLE *handle, const char *src, char *dest, long capacity)
 Expand a string which may contain macro references. More...
 
long epicsStdCall macPutValue (MAC_HANDLE *handle, const char *name, const char *value)
 Sets the value of a specific macro. More...
 
long epicsStdCall macGetValue (MAC_HANDLE *handle, const char *name, char *value, long capacity)
 Returns the value of a macro. More...
 
long epicsStdCall macDeleteHandle (MAC_HANDLE *handle)
 Marks a handle invalid, and frees all storage associated with it. More...
 
long epicsStdCall macPushScope (MAC_HANDLE *handle)
 Marks the start of a new scoping level. More...
 
long epicsStdCall macPopScope (MAC_HANDLE *handle)
 Retrieve the last pushed scope (like stack operations) More...
 
long epicsStdCall macReportMacros (MAC_HANDLE *handle)
 Reports details of current definitions. More...
 

Macro Definition Documentation

#define FLAG_SUPPRESS_WARNINGS   0x1

Definition at line 88 of file macCore.c.

#define FLAG_USE_ENVIRONMENT   0x80

Definition at line 89 of file macCore.c.

#define MAC_MAGIC   0xbadcafe /* ...sells sub-standard coffee? */

Definition at line 83 of file macCore.c.

Typedef Documentation

typedef struct mac_entry MAC_ENTRY

Function Documentation

long epicsStdCall macCreateHandle ( MAC_HANDLE **  handle,
const char *  pairs[] 
)

Creates a new macro substitution context.

Returns
0 = OK; <0 = ERROR
Parameters
pHandlepointer to variable to receive pointer to new macro substitution context
pairspointer to NULL-terminated array of {name,value} pair strings. A NULL value implies undefined; a NULL pairs argument implies no macros.

Definition at line 100 of file macCore.c.

108 {
109  MAC_HANDLE *handle; /* pointer to macro substitution context */
110 
111  /* ensure NULL handle pointer is returned on error */
112  *pHandle = NULL;
113 
114  /* allocate macro substitution context */
115  handle = ( MAC_HANDLE * ) dbmfMalloc( sizeof( MAC_HANDLE ) );
116  if ( handle == NULL ) {
117  errlogPrintf( "macCreateHandle: failed to allocate context\n" );
118  return -1;
119  }
120 
121  /* initialize context */
122  handle->magic = MAC_MAGIC;
123  handle->dirty = FALSE;
124  handle->level = 0;
125  handle->debug = 0;
126  handle->flags = 0;
127  ellInit( &handle->list );
128 
129  /* use environment variables if so specified */
130  if (pairs && pairs[0] && !strcmp(pairs[0],"") && pairs[1] && !strcmp(pairs[1],"environ") && !pairs[3]) {
131  handle->flags |= FLAG_USE_ENVIRONMENT;
132  }
133  else {
134  /* if supplied, load macro definitions */
135  for ( ; pairs && pairs[0]; pairs += 2 ) {
136  if ( macPutValue( handle, pairs[0], pairs[1] ) < 0 ) {
137  dbmfFree( handle );
138  return -1;
139  }
140  }
141  }
142 
143  /* set returned handle pointer */
144  *pHandle = handle;
145 
146  return 0;
147 }
long magic
magic number (used for authentication)
Definition: macLib.h:43
#define FALSE
Definition: dbDefs.h:32
#define NULL
Definition: catime.c:38
int flags
operating mode flags
Definition: macLib.h:48
Macro substitution context, for use by macLib routines only.
Definition: macLib.h:42
void dbmfFree(void *mem)
Free the memory allocated by dbmfMalloc.
Definition: dbmf.c:175
int dirty
values need expanding from raw values?
Definition: macLib.h:44
int errlogPrintf(const char *pFormat,...)
Definition: errlog.c:105
#define MAC_MAGIC
Definition: macCore.c:83
ELLLIST list
macro name / value list
Definition: macLib.h:47
int debug
debugging level
Definition: macLib.h:46
#define ellInit(PLIST)
Initialize a list type.
Definition: ellLib.h:76
int level
scoping level
Definition: macLib.h:45
long epicsStdCall macPutValue(MAC_HANDLE *handle, const char *name, const char *value)
Sets the value of a specific macro.
Definition: macCore.c:233
void * dbmfMalloc(size_t size)
Allocate memory.
Definition: dbmf.c:97
#define FLAG_USE_ENVIRONMENT
Definition: macCore.c:89
long epicsStdCall macDeleteHandle ( MAC_HANDLE handle)

Marks a handle invalid, and frees all storage associated with it.

Returns
0 = OK; <0 = ERROR
Note
Note that this does not free any strings into which macro values have been returned. Macro values are always returned into strings which were pre-allocated by the caller.
Parameters
handleopaque handle

Definition at line 360 of file macCore.c.

362 {
363  MAC_ENTRY *entry, *nextEntry;
364 
365  /* check handle */
366  if ( handle == NULL || handle->magic != MAC_MAGIC ) {
367  errlogPrintf( "macDeleteHandle: NULL or invalid handle\n" );
368  return -1;
369  }
370 
371  /* debug output */
372  if ( handle->debug & 1 )
373  printf( "macDeleteHandle()\n" );
374 
375  /* delete all entries */
376  for ( entry = first( handle ); entry != NULL; entry = nextEntry ) {
377  nextEntry = next( entry );
378  delete( handle, entry );
379  }
380 
381  /* clear magic field and free context structure */
382  handle->magic = 0;
383  dbmfFree( handle );
384 
385  return 0;
386 }
long magic
magic number (used for authentication)
Definition: macLib.h:43
#define printf
Definition: epicsStdio.h:41
#define NULL
Definition: catime.c:38
void dbmfFree(void *mem)
Free the memory allocated by dbmfMalloc.
Definition: dbmf.c:175
Definition: macCore.c:39
int errlogPrintf(const char *pFormat,...)
Definition: errlog.c:105
#define MAC_MAGIC
Definition: macCore.c:83
int debug
debugging level
Definition: macLib.h:46
long epicsStdCall macExpandString ( MAC_HANDLE handle,
const char *  src,
char *  dest,
long  capacity 
)

Expand a string which may contain macro references.

Returns
Returns the length of the expanded string, <0 if any macro are undefined

This routine parses the src string looking for macro references and passes any it finds to macGetValue() for translation.

Note
The return value is similar to that of macGetValue(). Its absolute value is the number of characters copied to dest. If the return value is negative, at least one undefined macro was left unexpanded.
Parameters
handleopaque handle
srcsource string
destdestination string
capacitycapacity of destination buffer (dest)

Definition at line 174 of file macCore.c.

182 {
184  const char *s;
185  char *d;
186  long length;
187 
188  /* check handle */
189  if ( handle == NULL || handle->magic != MAC_MAGIC ) {
190  errlogPrintf( "macExpandString: NULL or invalid handle\n" );
191  return -1;
192  }
193 
194  /* debug output */
195  if ( handle->debug & 1 )
196  printf( "macExpandString( %s, capacity = %ld )\n", src, capacity );
197 
198  /* Check size */
199  if (capacity <= 1)
200  return -1;
201 
202  /* expand raw values if necessary */
203  if ( expand( handle ) < 0 )
204  errlogPrintf( "macExpandString: failed to expand raw values\n" );
205 
206  /* fill in necessary fields in fake macro entry structure */
207  entry.name = (char *) src;
208  entry.type = "string";
209  entry.error = FALSE;
210 
211  /* expand the string */
212  s = src;
213  d = dest;
214  *d = '\0';
215  trans( handle, &entry, 0, "", &s, &d, d + capacity - 1 );
216 
217  /* return +/- #chars copied depending on successful expansion */
218  length = d - dest;
219  length = ( entry.error ) ? -length : length;
220 
221  /* debug output */
222  if ( handle->debug & 1 )
223  printf( "macExpandString() -> %ld\n", length );
224 
225  return length;
226 }
long magic
magic number (used for authentication)
Definition: macLib.h:43
#define FALSE
Definition: dbDefs.h:32
char * type
Definition: macCore.c:42
#define printf
Definition: epicsStdio.h:41
#define NULL
Definition: catime.c:38
char * name
Definition: macCore.c:41
Definition: macCore.c:39
int errlogPrintf(const char *pFormat,...)
Definition: errlog.c:105
int error
Definition: macCore.c:46
#define MAC_MAGIC
Definition: macCore.c:83
int debug
debugging level
Definition: macLib.h:46
long epicsStdCall macGetValue ( MAC_HANDLE handle,
const char *  name,
char *  value,
long  capacity 
)

Returns the value of a macro.

Returns
Returns the length of the value string, <0 if undefined

value will be zero-terminated if the length of the value is less than capacity. The return value is the number of characters copied to value (see below for behavior if the macro is undefined). If capacity is zero, no characters will be copied to value (which may be NULL) and the call can be used to check whether the macro is defined.

Note
Truncation of the value is not reported, applications should assume that truncation has occurred if the return value is equal to capacity.

If the macro is undefined, the macro reference will be returned in the value string (if permitted by maxlen) and the function value will be minus the number of characters copied. Note that treatment of capacity is intended to be consistent with the strncpy() routine.

If the value contains macro references, these references will be expanded recursively. This expansion will detect a direct or indirect self reference.

Macro references begin with a "$" immediately followed by either a "(" or a "{" character. The macro name comes next, and may optionally be succeeded by an "=" and a default value, which will be returned if the named macro is undefined at the moment of expansion. A reference is terminated by the matching ")" or "}" character.

Parameters
handleopaque handle
namemacro name or reference
valuestring to receive macro value or name argument if macro is undefined
capacitycapacity of destination buffer (value)

Definition at line 301 of file macCore.c.

310 {
311  MAC_ENTRY *entry; /* pointer to this macro's entry structure */
312  long length; /* number of characters returned */
313 
314  /* check handle */
315  if ( handle == NULL || handle->magic != MAC_MAGIC ) {
316  errlogPrintf( "macGetValue: NULL or invalid handle\n" );
317  return -1;
318  }
319 
320  /* debug output */
321  if ( handle->debug & 1 )
322  printf( "macGetValue( %s )\n", name );
323 
324  /* look up macro name */
325  entry = lookup( handle, name, FALSE );
326 
327  /* if capacity <= 1 or VALUE == NULL just return -1 / 0 for undefined /
328  defined macro */
329  if ( capacity <= 1 || value == NULL ) {
330  return ( entry == NULL ) ? -1 : 0;
331  }
332 
333  /* if not found, copy name to value and return minus #chars copied */
334  if ( entry == NULL ) {
335  strncpy( value, name, capacity );
336  return ( value[capacity-1] == '\0' ) ? - (long) strlen( name ) : -capacity;
337  }
338 
339  /* expand raw values if necessary; if fail (can only fail because of
340  memory allocation failure), return same as if not found */
341  if ( expand( handle ) < 0 ) {
342  errlogPrintf( "macGetValue: failed to expand raw values\n" );
343  strncpy( value, name, capacity );
344  return ( value[capacity-1] == '\0' ) ? - (long) strlen( name ) : -capacity;
345  }
346 
347  /* copy value and return +/- #chars copied depending on successful
348  expansion */
349  strncpy( value, entry->value, capacity );
350  length = ( value[capacity-1] == '\0' ) ? entry->length : capacity;
351 
352  return ( entry->error ) ? -length : length;
353 }
long magic
magic number (used for authentication)
Definition: macLib.h:43
Definition: link.h:174
#define FALSE
Definition: dbDefs.h:32
#define printf
Definition: epicsStdio.h:41
char * value
Definition: macCore.c:44
#define NULL
Definition: catime.c:38
Definition: macCore.c:39
int errlogPrintf(const char *pFormat,...)
Definition: errlog.c:105
int error
Definition: macCore.c:46
#define MAC_MAGIC
Definition: macCore.c:83
int debug
debugging level
Definition: macLib.h:46
size_t length
Definition: macCore.c:45
long epicsStdCall macPopScope ( MAC_HANDLE handle)

Retrieve the last pushed scope (like stack operations)

Returns
0 = OK; <0 = ERROR

See macPushScope()

Parameters
handleopaque handle

Definition at line 427 of file macCore.c.

429 {
430  MAC_ENTRY *entry, *nextEntry;
431 
432  /* check handle */
433  if ( handle == NULL || handle->magic != MAC_MAGIC ) {
434  errlogPrintf( "macPopScope: NULL or invalid handle\n" );
435  return -1;
436  }
437 
438  /* debug output */
439  if ( handle->debug & 1 )
440  printf( "macPopScope()\n" );
441 
442  /* check scoping level isn't already zero */
443  if ( handle->level == 0 ) {
444  errlogPrintf( "macPopScope: no scope to pop\n" );
445  return -1;
446  }
447 
448  /* look up most recent scope entry */
449  entry = lookup( handle, "<scope>", TRUE );
450  if ( entry == NULL ) {
451  errlogPrintf( "macPopScope: no scope to pop\n" );
452  return -1;
453  }
454 
455  /* delete scope entry and all macros defined since it */
456  for ( ; entry != NULL; entry = nextEntry ) {
457  nextEntry = next( entry );
458  delete( handle, entry );
459  }
460 
461  /* decrement scoping level */
462  handle->level--;
463 
464  return 0;
465 }
long magic
magic number (used for authentication)
Definition: macLib.h:43
#define printf
Definition: epicsStdio.h:41
#define NULL
Definition: catime.c:38
Definition: macCore.c:39
int errlogPrintf(const char *pFormat,...)
Definition: errlog.c:105
#define MAC_MAGIC
Definition: macCore.c:83
int debug
debugging level
Definition: macLib.h:46
#define TRUE
Definition: dbDefs.h:27
int level
scoping level
Definition: macLib.h:45
long epicsStdCall macPushScope ( MAC_HANDLE handle)

Marks the start of a new scoping level.

Returns
0 = OK; <0 = ERROR

Marks all macro definitions added after this call as belonging to another scope. These macros will be lost on a macPopScope() call and those at the current scope will be re-instated.

Parameters
handleopaque handle

Definition at line 392 of file macCore.c.

394 {
395  MAC_ENTRY *entry;
396 
397  /* check handle */
398  if ( handle == NULL || handle->magic != MAC_MAGIC ) {
399  errlogPrintf( "macPushScope: NULL or invalid handle\n" );
400  return -1;
401  }
402 
403  /* debug output */
404  if ( handle->debug & 1 )
405  printf( "macPushScope()\n" );
406 
407  /* increment scoping level */
408  handle->level++;
409 
410  /* create new "special" entry of name "<scope>" */
411  entry = create( handle, "<scope>", TRUE );
412  if ( entry == NULL ) {
413  handle->level--;
414  errlogPrintf( "macPushScope: failed to push scope\n" );
415  return -1;
416  } else {
417  entry->type = "scope marker";
418  }
419 
420  return 0;
421 }
long magic
magic number (used for authentication)
Definition: macLib.h:43
char * type
Definition: macCore.c:42
#define printf
Definition: epicsStdio.h:41
#define NULL
Definition: catime.c:38
Definition: macCore.c:39
int errlogPrintf(const char *pFormat,...)
Definition: errlog.c:105
#define MAC_MAGIC
Definition: macCore.c:83
int debug
debugging level
Definition: macLib.h:46
#define TRUE
Definition: dbDefs.h:27
int level
scoping level
Definition: macLib.h:45
long epicsStdCall macPutValue ( MAC_HANDLE handle,
const char *  name,
const char *  value 
)

Sets the value of a specific macro.

Returns
Returns the length of the value string.
Note
If value is NULL, all instances of name are undefined at all scoping levels (the named macro doesn't have to exist in this case). Macros referenced in value need not be defined at this point.
Parameters
handleopaque handle
namemacro name
valuemacro value

Definition at line 233 of file macCore.c.

239 {
240  MAC_ENTRY *entry; /* pointer to this macro's entry structure */
241 
242  /* check handle */
243  if ( handle == NULL || handle->magic != MAC_MAGIC ) {
244  errlogPrintf( "macPutValue: NULL or invalid handle\n" );
245  return -1;
246  }
247 
248  if ( handle->debug & 1 )
249  printf( "macPutValue( %s, %s )\n", name, value ? value : "NULL" );
250 
251  /* handle NULL value case: if name was found, delete entry (may be
252  several entries at different scoping levels) */
253  if ( value == NULL ) {
254  /*
255  * FIXME: shouldn't be able to delete entries from lower scopes
256  * NOTE: when this is changed, this functionality of removing
257  * a macro from all scopes will still be needed by iocshEnvClear
258  */
259  while ( ( entry = lookup( handle, name, FALSE ) ) != NULL ) {
260  int done = strcmp(entry->type, "environment variable") == 0;
261  delete( handle, entry );
262 
263  if (done)
264  break;
265  }
266 
267  return 0;
268  }
269 
270  /* look up macro name */
271  entry = lookup( handle, name, FALSE );
272 
273  /* new entry must be created if macro doesn't exist or if it only
274  exists at a lower scoping level */
275  if ( entry == NULL || entry->level < handle->level ) {
276  entry = create( handle, name, FALSE );
277  if ( entry == NULL ) {
278  errlogPrintf( "macPutValue: failed to create macro %s = %s\n",
279  name, value );
280  return -1;
281  } else {
282  entry->type = "macro";
283  }
284  }
285 
286  /* copy raw value */
287  if ( rawval( handle, entry, value ) == NULL ) {
288  errlogPrintf( "macPutValue: failed to copy macro %s = %s\n",
289  name, value ) ;
290  return -1;
291  }
292 
293  /* return length of value */
294  return strlen( value );
295 }
long magic
magic number (used for authentication)
Definition: macLib.h:43
Definition: link.h:174
#define FALSE
Definition: dbDefs.h:32
char * type
Definition: macCore.c:42
#define printf
Definition: epicsStdio.h:41
#define NULL
Definition: catime.c:38
int level
Definition: macCore.c:49
Definition: macCore.c:39
int errlogPrintf(const char *pFormat,...)
Definition: errlog.c:105
#define MAC_MAGIC
Definition: macCore.c:83
int debug
debugging level
Definition: macLib.h:46
void done(int k)
Definition: antelope.c:77
int level
scoping level
Definition: macLib.h:45
long epicsStdCall macReportMacros ( MAC_HANDLE handle)

Reports details of current definitions.

Returns
0 = OK; <0 = ERROR This sends details of current definitions to standard output, and is intended purely for debugging purposes.
Parameters
handleopaque handle

Definition at line 471 of file macCore.c.

473 {
474  const char *format = "%-1s %-16s %-16s %s\n";
475  MAC_ENTRY *entry;
476 
477  /* check handle */
478  if ( handle == NULL || handle->magic != MAC_MAGIC ) {
479  errlogPrintf( "macReportMacros: NULL or invalid handle\n" );
480  return -1;
481  }
482 
483  /* expand raw values if necessary; report but ignore failure */
484  if ( expand( handle ) < 0 )
485  errlogPrintf( "macGetValue: failed to expand raw values\n" );
486 
487  /* loop through macros, reporting names and values */
488  printf( format, "e", "name", "rawval", "value" );
489  printf( format, "-", "----", "------", "-----" );
490  for ( entry = first( handle ); entry != NULL; entry = next( entry ) ) {
491 
492  /* differentiate between "special" (scope marker) and ordinary
493  entries */
494  if ( entry->special )
495  printf( format, "s", "----", "------", "-----" );
496  else
497  printf( format, entry->error ? "*" : " ", entry->name,
498  entry->rawval ? entry->rawval : "",
499  entry->value ? entry->value : "");
500  }
501 
502  return 0;
503 }
long magic
magic number (used for authentication)
Definition: macLib.h:43
#define printf
Definition: epicsStdio.h:41
char * value
Definition: macCore.c:44
#define NULL
Definition: catime.c:38
char * name
Definition: macCore.c:41
int special
Definition: macCore.c:48
Definition: macCore.c:39
int errlogPrintf(const char *pFormat,...)
Definition: errlog.c:105
int error
Definition: macCore.c:46
#define MAC_MAGIC
Definition: macCore.c:83
char * rawval
Definition: macCore.c:43
void epicsStdCall macSuppressWarning ( MAC_HANDLE handle,
int  falseTrue 
)

Disable or enable warning messages.

The macExpandString() routine prints warnings when it cant expand a macro. This routine can be used to silence those warnings. A non zero value will suppress the warning messages from subsequent library routines given the same handle.

Parameters
handleopaque handle
suppress0 means issue, 1 means suppress

Definition at line 154 of file macCore.c.

158 {
159  if ( handle && handle->magic == MAC_MAGIC ) {
160  handle->flags = (handle->flags & ~FLAG_SUPPRESS_WARNINGS) |
161  (suppress ? FLAG_SUPPRESS_WARNINGS : 0);
162  }
163 }
long magic
magic number (used for authentication)
Definition: macLib.h:43
#define FLAG_SUPPRESS_WARNINGS
Definition: macCore.c:88
int flags
operating mode flags
Definition: macLib.h:48
#define MAC_MAGIC
Definition: macCore.c:83