/* * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory */ /* * * macro.c -- macro expansion routines * */ #include /* SAOMOD_CTYPE -- work around Slackware/RedHat incompatibility */ #ifdef linux #ifdef isalnum #undef isalnum #define isalnum(c) (isalpha(c)||isdigit(c)) #endif #endif #define BUFINC 5000 /* * * Private Routines * * */ /* * * AddString -- add a string to a buffer * */ #ifdef ANSI_FUNC static void AddString(char **buf, int *blen, int *maxlen, char *str) #else static void AddString(buf, blen, maxlen, str) char **buf; int *blen; int *maxlen; char *str; #endif { int slen; if( !str || !*str ) return; slen = strlen(str) + 1; while( (*blen + slen) >= *maxlen ){ *maxlen += BUFINC; *buf = (char *)realloc(*buf, *maxlen); } strcat(*buf, str); *blen += slen; } /* * * AddChar -- add a single char to a buffer * */ #ifdef ANSI_FUNC static void AddChar(char **buf, int *blen, int *maxlen, int c) #else static void AddChar(buf, blen, maxlen, c) char **buf; int *blen; int *maxlen; int c; #endif { char tbuf[2]; tbuf[0] = (char)c; tbuf[1] = '\0'; AddString(buf, blen, maxlen, tbuf); } /* * * LookupKeywords -- lookup a name in a list fo keywords and * return the associated value. * (Should use quarks ...) * */ #ifdef ANSI_FUNC static char * LookupKeywords(char *name, char **keyword, char **value, int nkey) #else static char *LookupKeywords(name, keyword, value, nkey) char *name; char **keyword; char **value; int nkey; #endif { int i; for(i=0; i 0) && (s=LookupKeywords(tbuf, keyword, value, nkey)) != NULL ){ AddString(&result, &i, &maxlen, s); } /* execute the client routine to expand macros */ else if( (client_callback != NULL) && ((s=(*client_callback)(tbuf, client_data)) != NULL) ){ AddString(&result, &i, &maxlen, s); } /* look for an environment variable */ else if( (s = (char *)getenv(tbuf)) != NULL ){ AddString(&result, &i, &maxlen, s); } /* if we don't recognize this macro, put it back onto the string */ else{ int len; len = ip - mip + 1; strncpy(tbuf1, mip, len); tbuf1[len] = '\0'; AddString(&result, &i, &maxlen, tbuf1); } } } /* null terminate and save the string */ result[i] = '\0'; result = (char *)realloc(result, i+1); return(result); }