summaryrefslogtreecommitdiffstats
path: root/funtools/util/macro.c
blob: a96b3ccc34c6ce5e0cc589c53d9b1f9b9752498c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 *	Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
 */

/*
 *
 * macro.c -- macro expansion routines
 *
 */

#include <macro.h>

/* 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<nkey; i++){
    if( keyword[i] && !strcmp(name, keyword[i]) )
      return(value[i]);
  }
  return(NULL);
}

/*
 *
 * Public Routines
 *
 *
 */

/*
 *
 * ExpandMacro -- expand a macro using a client's callback
 * returns: expanded macro as an allocated string
 *
 */
#ifdef ANSI_FUNC
char *
ExpandMacro(char *icmd, char **keyword, char **value, int nkey,
	    MacroCall client_callback, void *client_data)
#else
char *ExpandMacro(icmd, keyword, value, nkey, client_callback, client_data)
     char *icmd;
     char **keyword;
     char **value;
     int nkey;
     MacroCall client_callback;
     void *client_data;
#endif
{
  int  i, j;
  int  maxlen;
  char brace;
  char *result;
  char tbuf[1000];
  char tbuf1[1000];
  char *s;
  char *ip;
  char *mip;

  /* make a new string using the command as a base, but substituting
     for "$" values as needed */
  result = (char *)malloc(BUFINC+1);
  maxlen = BUFINC;
  *result = '\0';
  for(i=0, ip=icmd; *ip; ip++){
    if( *ip != '$' ){
      AddChar(&result, &i, &maxlen, (int)*ip);
    }
    else{
      /* save beginning of macro */
      mip = ip;
      /* skip past '$' */
      ip++;
      /* check for brace mode */
      if( *ip == '{' ){
	brace = '{';
	ip++;
      }
      else if( *ip == '(' ){
	brace = '(';
	ip++;
      }
      else
	brace = '\0';
      /* get variable up to next non-alpha character or close brace */
      for(*tbuf='\0', j=0; *ip; ip++ ){
	/* if we are in brace mode, look for trailing brace */
	if( brace && *ip == (brace == '(' ? ')' : '}') ){
	  ip++;
	  break;
	}
	/* else look for a non-alpha character */
	else if( !isalnum((int)*ip) && *ip != '_'){
	  break;
	}
	else{
	  tbuf[j++] = *ip;
	  tbuf[j] = '\0';
	}
      }
      /* back up so the outer loop adds this delimiting char to the output */
      ip--;
      /* search for keyword from the list */
      if( (nkey > 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);
}