13379Seric # include "useful.h" 23379Seric 3*3387Seric static char SccsId[] = "@(#)macro.c 1.2 03/28/81"; 43379Seric 53379Seric char *Macro[128]; 63379Seric extern bool Debug; 73379Seric 83379Seric /* 93379Seric ** EXPAND -- macro expand a string using $x escapes. 103379Seric ** 113379Seric ** Parameters: 123379Seric ** s -- the string to expand. 133379Seric ** buf -- the place to put the expansion. 143379Seric ** buflim -- the buffer limit, i.e., the address 153379Seric ** of the last usable position in buf. 163379Seric ** 173379Seric ** Returns: 183379Seric ** buf. 193379Seric ** 203379Seric ** Side Effects: 213379Seric ** none. 223379Seric */ 233379Seric 243379Seric char * 253379Seric expand(s, buf, buflim) 263379Seric register char *s; 273379Seric register char *buf; 283379Seric char *buflim; 293379Seric { 303379Seric register char *q; 313379Seric register char *bp; 32*3387Seric bool skipping; 333379Seric 343379Seric # ifdef DEBUG 353379Seric if (Debug) 363379Seric printf("expand(%s)\n", s); 373379Seric # endif DEBUG 383379Seric 39*3387Seric skipping = FALSE; 403379Seric for (bp = buf; *s != '\0'; s++) 413379Seric { 423379Seric /* q will be the interpolated quantity */ 433379Seric q = NULL; 443379Seric if (*s == '$') 45*3387Seric { 46*3387Seric char c; 473379Seric 48*3387Seric c = *++s; 49*3387Seric switch (c) 50*3387Seric { 51*3387Seric case '?': /* see if var set */ 52*3387Seric c = *++s; 53*3387Seric skipping = Macro[c] == NULL; 54*3387Seric break; 55*3387Seric 56*3387Seric case ':': /* else */ 57*3387Seric skipping = !skipping; 58*3387Seric break; 59*3387Seric 60*3387Seric case '.': /* end if */ 61*3387Seric skipping = FALSE; 62*3387Seric break; 63*3387Seric 64*3387Seric default: 65*3387Seric q = Macro[c & 0177]; 66*3387Seric break; 67*3387Seric } 68*3387Seric if (q == NULL) 69*3387Seric continue; 70*3387Seric } 71*3387Seric 723379Seric /* 733379Seric ** Interpolate q or output one character 743379Seric */ 753379Seric 76*3387Seric if (skipping) 77*3387Seric continue; 783379Seric if (q != NULL) 793379Seric bp = expand(q, bp, buflim); 803379Seric else if (bp < buflim - 1) 813379Seric *bp++ = *s; 823379Seric } 833379Seric *bp = '\0'; 843379Seric 853379Seric # ifdef DEBUG 863379Seric if (Debug) 873379Seric printf("expand ==> '%s'\n", buf); 883379Seric # endif DEBUG 893379Seric 903379Seric return (bp); 913379Seric } 923379Seric /* 933379Seric ** DEFINE -- define a macro. 943379Seric ** 953379Seric ** this would be better done using a #define macro. 963379Seric ** 973379Seric ** Parameters: 983379Seric ** n -- the macro name. 993379Seric ** v -- the macro value. 1003379Seric ** 1013379Seric ** Returns: 1023379Seric ** none. 1033379Seric ** 1043379Seric ** Side Effects: 1053379Seric ** Macro[n] is defined. 1063379Seric */ 1073379Seric 1083379Seric define(n, v) 1093379Seric char n; 1103379Seric char *v; 1113379Seric { 1123379Seric # ifdef DEBUG 1133379Seric if (Debug) 1143379Seric printf("define(%c as %s)\n", n, v); 1153379Seric # endif DEBUG 1163379Seric Macro[n & 0177] = v; 1173379Seric } 118