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