1 /* 2 * expr.c - expression 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 * Asmcode(s, c) - assemble number/name code following backslash-character 36 * definition - e. .g, "\\nPO" 37 */ 38 39 unsigned char * 40 Asmcode(s, c) 41 unsigned char **s; /* pointer to character after '\\' */ 42 unsigned char *c; /* code destination (c[3]) */ 43 { 44 unsigned char *s1; 45 46 s1 = *s + 1; 47 c[0] = c[1] = c[2] = '\0'; 48 if ((c[0] = *s1) == '(') { 49 s1++; 50 if ((c[0] = *s1) != '\0') { 51 s1++; 52 c[1] = *s1; 53 } 54 } 55 return(s1); 56 } 57 58 59 /* 60 * Delnum(nx) - delete number 61 */ 62 63 void 64 Delnum(nx) 65 int nx; /* number index */ 66 { 67 unsigned char buf[MAXLINE]; /* message buffer */ 68 69 if (nx >= Nnr) { 70 (void) sprintf((char *)buf, " bad Delnum(%d) index", nx); 71 Error(FATAL, LINE, (char *)buf, NULL); 72 } 73 while (nx < (Nnr - 1)) { 74 Numb[nx] = Numb[nx + 1]; 75 nx++; 76 } 77 Nnr--; 78 } 79 80 81 /* 82 * Findnum(n, v, e) - find or optionally enter number value 83 */ 84 85 Findnum(n, v, e) 86 unsigned char *n; /* register name */ 87 int v; /* value */ 88 int e; /* 0 = find, don't enter 89 * 1 = enter, don't find */ 90 { 91 int cmp, low, hi, mid; /* binary search controls */ 92 unsigned char c[3]; /* name buffer */ 93 94 c[0] = n[0]; 95 c[1] = (n[1] == ' ' || n[1] == '\t') ? '\0' : n[1]; 96 c[2] = '\0'; 97 low = mid = 0; 98 hi = Nnr - 1; 99 while (low <= hi) { 100 mid = (low + hi) / 2; 101 if ((cmp = strncmp((char *)c, (char *)Numb[mid].nm, 2)) < 0) 102 hi = mid - 1; 103 else if (cmp > 0) 104 low = mid + 1; 105 else { 106 if (e) 107 Numb[mid].val = v; 108 return(mid); 109 } 110 } 111 if ( ! e) 112 return(-1); 113 if (Nnr >= MAXNR) 114 Error(FATAL, LINE, " out of number registers at ", (char *)c); 115 if (Nnr) { 116 if (cmp > 0) 117 mid++; 118 for (hi = Nnr - 1; hi >= mid; hi--) 119 Numb[hi+1] = Numb[hi]; 120 } 121 Nnr++; 122 Numb[mid].nm[0] = c[0]; 123 Numb[mid].nm[1] = c[1]; 124 Numb[mid].val = v; 125 return(mid); 126 } 127 128 129 /* 130 * Findparms(n) - find parameter registers 131 */ 132 133 Findparms(n) 134 unsigned char *n; /* parameter name */ 135 { 136 unsigned char c[3]; /* character buffer */ 137 int i; /* temporary index */ 138 139 c[0] = n[0]; 140 c[1] = (n[1] == ' ' || n[1] == '\t') ? '\0' : n[1]; 141 c[2] = '\0'; 142 for (i = 0; Parms[i].nm[0]; i++) { 143 if (c[0] == Parms[i].nm[0] && c[1] == Parms[i].nm[1]) 144 return(i); 145 } 146 return(-1); 147 } 148 149 150 /* 151 * Findscale(n, v, e) - find and optionally enter scaling factor value 152 */ 153 154 Findscale(n, v, e) 155 int n; /* scaling factor name */ 156 double v; /* value */ 157 int e; /* 0 = find, don't enter 158 * 1 = enter, don't find */ 159 { 160 int i; 161 double *pval; 162 163 for (i = 0; Scale[i].nm; i++) { 164 if ((unsigned char )n == Scale[i].nm) 165 break; 166 } 167 if (Scale[i].nm) { 168 if (e) { 169 pval = &Scale[i].val; 170 *pval = v; 171 } 172 return(i); 173 } 174 return(-1); 175 } 176