1 # include "useful.h" 2 3 static char SccsId[] = "@(#)macro.c 1.2 03/28/81"; 4 5 char *Macro[128]; 6 extern bool Debug; 7 8 /* 9 ** EXPAND -- macro expand a string using $x escapes. 10 ** 11 ** Parameters: 12 ** s -- the string to expand. 13 ** buf -- the place to put the expansion. 14 ** buflim -- the buffer limit, i.e., the address 15 ** of the last usable position in buf. 16 ** 17 ** Returns: 18 ** buf. 19 ** 20 ** Side Effects: 21 ** none. 22 */ 23 24 char * 25 expand(s, buf, buflim) 26 register char *s; 27 register char *buf; 28 char *buflim; 29 { 30 register char *q; 31 register char *bp; 32 bool skipping; 33 34 # ifdef DEBUG 35 if (Debug) 36 printf("expand(%s)\n", s); 37 # endif DEBUG 38 39 skipping = FALSE; 40 for (bp = buf; *s != '\0'; s++) 41 { 42 /* q will be the interpolated quantity */ 43 q = NULL; 44 if (*s == '$') 45 { 46 char c; 47 48 c = *++s; 49 switch (c) 50 { 51 case '?': /* see if var set */ 52 c = *++s; 53 skipping = Macro[c] == NULL; 54 break; 55 56 case ':': /* else */ 57 skipping = !skipping; 58 break; 59 60 case '.': /* end if */ 61 skipping = FALSE; 62 break; 63 64 default: 65 q = Macro[c & 0177]; 66 break; 67 } 68 if (q == NULL) 69 continue; 70 } 71 72 /* 73 ** Interpolate q or output one character 74 */ 75 76 if (skipping) 77 continue; 78 if (q != NULL) 79 bp = expand(q, bp, buflim); 80 else if (bp < buflim - 1) 81 *bp++ = *s; 82 } 83 *bp = '\0'; 84 85 # ifdef DEBUG 86 if (Debug) 87 printf("expand ==> '%s'\n", buf); 88 # endif DEBUG 89 90 return (bp); 91 } 92 /* 93 ** DEFINE -- define a macro. 94 ** 95 ** this would be better done using a #define macro. 96 ** 97 ** Parameters: 98 ** n -- the macro name. 99 ** v -- the macro value. 100 ** 101 ** Returns: 102 ** none. 103 ** 104 ** Side Effects: 105 ** Macro[n] is defined. 106 */ 107 108 define(n, v) 109 char n; 110 char *v; 111 { 112 # ifdef DEBUG 113 if (Debug) 114 printf("define(%c as %s)\n", n, v); 115 # endif DEBUG 116 Macro[n & 0177] = v; 117 } 118