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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/*
* Configurable ps-like program.
* Macro definition routines.
*
* Copyright (c) 2010 David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*/
#include "ips.h"
/*
* Number of different macro lists.
*/
#define MACRO_TYPE_COUNT 3
/*
* Macro definition.
* This is a variable sized structure, with all its string data allocated
* within it. So the structure must be treated as a unit, and its elements
* cannot be individually freed. Only the complete structure can be freed.
*/
typedef struct MACRO MACRO;
struct MACRO
{
MACRO * next; /* next macro in list */
char * name; /* macro name */
char ** values; /* replacement name list */
int count; /* number of elements in list */
char buf[1]; /* string buffer (variable length) */
};
#define NULL_MACRO ((MACRO *) 0)
/*
* Header for macro lists.
*/
typedef struct
{
char * define; /* string used in configuration files */
BOOL words; /* whether macro is expanded by words */
MACRO * list; /* list of macros */
} MACRO_HEADER;
/*
* The headers for all type of macros.
* These definitions cannot be changed in isolation.
*/
static MACRO_HEADER macroLists[MACRO_TYPE_COUNT] =
{
{"option", TRUE, NULL},
{"column", TRUE, NULL},
{"expr", FALSE, NULL}
};
/*
* Local procedures.
*/
static MACRO_HEADER * FindMacroHeader(MACRO_TYPE type);
static MACRO * FindMacro(MACRO_HEADER * head, const char * name);
/*
* Determine whether or not the specified macro name exists for the
* specified type of macro.
*/
BOOL
MacroExists(MACRO_TYPE type, const char * name)
{
MACRO_HEADER * head;
head = FindMacroHeader(type);
if (head == NULL)
return FALSE;
return (FindMacro(head, name) != NULL_MACRO);
}
/*
* Expand a macro name of the specified type into its replacement word list.
* The supplied ARGS structure is filled in with the count and table of words.
* Returns TRUE if successful, or FALSE with an error message on error.
*/
BOOL
ExpandMacro(MACRO_TYPE type, const char * name, ARGS * retargs)
{
MACRO_HEADER * head;
MACRO * macro;
retargs->table = NULL;
retargs->count = 0;
if (!isMacro(*name))
{
fprintf(stderr, "Macro name \"%s\" is not upper case\n", name);
return FALSE;
}
head = FindMacroHeader(type);
if (head == NULL)
return FALSE;
macro = FindMacro(head, name);
if (macro == NULL_MACRO)
{
fprintf(stderr, "Macro name \"%s\" is undefined\n", name);
return FALSE;
}
retargs->table = macro->values;
retargs->count = macro->count;
return TRUE;
}
/*
* Define the specified macro name of the specified type to have the
* following expansion. The expansion is either in terms of space-separated
* words, or just as a complete line. Macro names must begin with an upper
* case letter to distinguish them from non-macros. Redefining of existing
* macros is allowed; the new macros will simply be found first.
* Returns TRUE if successful.
*/
BOOL
DefineMacro(MACRO_TYPE type, const char * name, const char * str)
{
MACRO_HEADER * head; /* header of list */
MACRO * macro; /* allocated macro */
char * bp; /* pointer into buffer for alloc */
const char * word; /* beginning of current word */
int wordCount; /* number of words */
int size; /* total size of allocated macro */
int i;
int wordLengths[MAX_WORDS];
const char * words[MAX_WORDS];
if (*name == '\0')
{
fprintf(stderr, "Missing macro name for define\n");
return FALSE;
}
if (!isMacro(*name))
{
fprintf(stderr, "Macro name \"%s\" is not upper case\n",
name);
return FALSE;
}
if (strlen(name) > MAX_MACRO_LEN)
{
fprintf(stderr, "Macro name \"%s\" is too long\n", name);
return FALSE;
}
head = FindMacroHeader(type);
if (head == NULL)
return FALSE;
size = sizeof(MACRO) + strlen(name) + 1;
wordCount = 0;
/*
* See if the input string is to be broken up into words.
* If so, then do that, otherwise leave the string as it is.
*/
if (head->words)
{
while (TRUE)
{
while (isBlank(*str))
str++;
if (*str == '\0')
break;
if (wordCount >= MAX_WORDS)
{
fprintf(stderr,
"Too many words defined for macro \"%s\"\n",
name);
return FALSE;
}
word = str;
while ((*str != '\0') && !isBlank(*str))
str++;
words[wordCount] = word;
wordLengths[wordCount] = (str - word);
size += (wordLengths[wordCount] + 1);
wordCount++;
}
}
else
{
if (wordCount >= MAX_WORDS)
{
fprintf(stderr,
"Too many words defined for macro \"%s\"\n",
name);
return FALSE;
}
words[wordCount] = str;
wordLengths[wordCount] = strlen(str);
size += (wordLengths[wordCount] + 1);
wordCount++;
}
size += (sizeof(char *) * (wordCount + 1));
macro = (MACRO *) malloc(size);
if (macro == NULL)
{
fprintf(stderr, "Cannot allocate macro structure\n");
return FALSE;
}
bp = macro->buf;
macro->values = (char **) bp;
bp += (sizeof(char *) * (wordCount + 1));
macro->name = bp;
strcpy(bp, name);
bp += (strlen(bp) + 1);
for (i = 0; i < wordCount; i++)
{
macro->values[i] = bp;
memcpy(bp, words[i], wordLengths[i]);
bp += wordLengths[i];
*bp++ = '\0';
}
macro->values[wordCount] = NULL;
macro->count = wordCount;
macro->next = head->list;
head->list = macro;
return TRUE;
}
static MACRO *
FindMacro(MACRO_HEADER * head, const char * name)
{
MACRO * macro;
for (macro = head->list; macro; macro = macro->next)
{
if (strcmp(name, macro->name) == 0)
return macro;
}
return NULL_MACRO;
}
/*
* Find the header for the specified type of macro.
* Returns NULL if the macro type is unknown.
*/
static MACRO_HEADER *
FindMacroHeader(MACRO_TYPE type)
{
if ((type < 0) || (type >= MACRO_TYPE_COUNT))
{
fprintf(stderr, "Illegal macro type %d\n", type);
return NULL;
}
return ¯oLists[type];
}
/*
* Display the definition of all the macros.
* This description is suitable for reading back in as an initialization file.
*/
void
ListMacros(void)
{
MACRO_TYPE type;
const MACRO_HEADER * head;
const MACRO * macro;
int i;
printf("%s\n", FIRST_LINE);
printf("# System initialization file is \"%s\".\n", SYSTEM_INIT_FILE);
for (type = 0; type < MACRO_TYPE_COUNT; type++)
{
head = ¯oLists[type];
for (macro = head->list; macro; macro = macro->next)
{
printf("%s %s", head->define, macro->name);
for (i = 0; i < macro->count; i++)
printf(" %s", macro->values[i]);
printf("\n");
}
}
}
/* END CODE */
|