1 /* 2 * macsup.c - macro processing support functions for cawf(1) 3 */ 4 5 /* 6 * Copyright (c) 1991 Purdue University Research Foundation, 7 * West Lafayette, Indiana 47907. All rights reserved. 8 * 9 * Written by Victor A. Abell <abe@mace.cc.purdue.edu>, Purdue 10 * University Computing Center. Not derived from licensed software; 11 * derived from awf(1) by Henry Spencer of the University of Toronto. 12 * 13 * Permission is granted to anyone to use this software for any 14 * purpose on any computer system, and to alter it and redistribute 15 * it freely, subject to the following restrictions: 16 * 17 * 1. The author is not responsible for any consequences of use of 18 * this software, even if they arise from flaws in it. 19 * 20 * 2. The origin of this software must not be misrepresented, either 21 * by explicit claim or by omission. Credits must appear in the 22 * documentation. 23 * 24 * 3. Altered versions must be plainly marked as such, and must not 25 * be misrepresented as being the original software. Credits must 26 * appear in the documentation. 27 * 28 * 4. This notice may not be removed or altered. 29 */ 30 31 #include "cawf.h" 32 33 34 /* 35 * Delmacro(mx) - delete macro 36 */ 37 38 void Delmacro(int mx) { 39 /* macro index mx */ 40 unsigned char buf[MAXLINE]; /* error message buffer */ 41 int i, j; /* temporary indexes */ 42 43 if (mx >= Nmac) { 44 (void) sprintf((char *)buf, " bad Delmacro(%d) index", mx); 45 Error(FATAL, LINE, (char *)buf, NULL); 46 } 47 for (i = Macrotab[mx].bx, j = i + Macrotab[mx].ct; i < j; i++) { 48 Free(&Macrotxt[i]); 49 } 50 for (i = mx; i < (Nmac - 1); i++) { 51 Macrotab[i] = Macrotab[i+1]; 52 } 53 Nmac--; 54 } 55 56 57 /* 58 * Field(n, p, c) - skip to field n in p and optionally return a copy 59 */ 60 61 unsigned char *Field(int n, unsigned char *p, int c) { 62 /* field number n 63 * pointer to line containing fields p 64 * c = 1: make a copy of the field 65 */ 66 unsigned char *fs, *fe, *s; 67 68 if (c) 69 Free(&F); 70 fe = p; 71 while (n) { 72 while (*fe == ' ' || *fe == '\t') 73 fe++; 74 fs = fe; 75 while (*fe && *fe != ' ' && *fe != '\t') 76 fe++; 77 if (fs == fe) 78 return(NULL); 79 if (n == 1) { 80 if ( ! c) 81 return(fs); 82 if ((F = (unsigned char *)malloc((size_t)(fe - fs + 1))) 83 == NULL) 84 Error(FATAL, LINE, " Field out of string space", 85 NULL); 86 (void) strncpy((char *)F, (char *)fs, (fe - fs)); 87 F[fe -fs] = '\0'; 88 return(F); 89 } 90 n--; 91 } 92 return(NULL); 93 } 94 95 /* 96 * Findmacro(p, e) - find macro and optionally enter it 97 * 98 * return = Macrotab[] index or -1 if not found 99 */ 100 101 102 int Findmacro(unsigned char *p, int e) { 103 /* pointer to 2 character macro name p 104 * e = 0 = find, don't enter 105 * e = 1 = enter, don't find 106 */ 107 unsigned char c[3]; 108 int cmp, hi, low, mid; 109 110 c[0] = p[0]; 111 c[1] = (p[1] == ' ' || p[1] == '\t') ? '\0' : p[1]; 112 c[2] = '\0'; 113 low = mid = 0; 114 hi = Nmac - 1; 115 while (low <= hi) { 116 mid = (low + hi) / 2; 117 if ((cmp = strncmp((char *)c, (char *)Macrotab[mid].name, 2)) 118 < 0) 119 hi = mid - 1; 120 else if (cmp > 0) 121 low = mid + 1; 122 else { 123 if ( ! e) 124 return(mid); 125 Error(WARN, LINE, " duplicate macro ", (char *)c); 126 hi = Macrotab[mid].bx + Macrotab[mid].ct; 127 for (low = Macrotab[mid].bx; low < hi; low++) { 128 Free(&Macrotxt[low]); 129 } 130 goto new_macro; 131 } 132 } 133 if ( ! e) 134 return(-1); 135 if (Nmac >= MAXMACRO) 136 Error(FATAL, LINE, " macro table full at ", (char *)c); 137 if (Nmac) { 138 if (cmp > 0) 139 mid++; 140 for (hi = Nmac - 1; hi >= mid; hi--) 141 Macrotab[hi+1] = Macrotab[hi]; 142 } 143 Nmac++; 144 Macrotab[mid].name[0] = c[0]; 145 Macrotab[mid].name[1] = c[1]; 146 147 new_macro: 148 149 Macrotab[mid].bx = -1; 150 Macrotab[mid].ct = 0; 151 return(mid); 152 } 153 154 void Free(unsigned char **p) { 155 if (*p != NULL) { 156 (void) free(*p); 157 *p = NULL; 158 } 159 } 160 161 /* 162 * Newstr(s) - allocate space for string 163 */ 164 165 unsigned char *Newstr(unsigned char *s) { 166 unsigned char *ns; 167 168 if ((ns = (unsigned char *)malloc((size_t)(strlen((char *)s) + 1))) 169 == NULL) 170 Error(FATAL, LINE, " Newstr out of malloc space at ", (char *)s); 171 (void) strcpy((char *)ns, (char *)s); 172 return(ns); 173 } 174