1*433d6423SLionel Sambuc /* 2*433d6423SLionel Sambuc * expr.c - expression support functions for cawf(1) 3*433d6423SLionel Sambuc */ 4*433d6423SLionel Sambuc 5*433d6423SLionel Sambuc /* 6*433d6423SLionel Sambuc * Copyright (c) 1991 Purdue University Research Foundation, 7*433d6423SLionel Sambuc * West Lafayette, Indiana 47907. All rights reserved. 8*433d6423SLionel Sambuc * 9*433d6423SLionel Sambuc * Written by Victor A. Abell <abe@mace.cc.purdue.edu>, Purdue 10*433d6423SLionel Sambuc * University Computing Center. Not derived from licensed software; 11*433d6423SLionel Sambuc * derived from awf(1) by Henry Spencer of the University of Toronto. 12*433d6423SLionel Sambuc * 13*433d6423SLionel Sambuc * Permission is granted to anyone to use this software for any 14*433d6423SLionel Sambuc * purpose on any computer system, and to alter it and redistribute 15*433d6423SLionel Sambuc * it freely, subject to the following restrictions: 16*433d6423SLionel Sambuc * 17*433d6423SLionel Sambuc * 1. The author is not responsible for any consequences of use of 18*433d6423SLionel Sambuc * this software, even if they arise from flaws in it. 19*433d6423SLionel Sambuc * 20*433d6423SLionel Sambuc * 2. The origin of this software must not be misrepresented, either 21*433d6423SLionel Sambuc * by explicit claim or by omission. Credits must appear in the 22*433d6423SLionel Sambuc * documentation. 23*433d6423SLionel Sambuc * 24*433d6423SLionel Sambuc * 3. Altered versions must be plainly marked as such, and must not 25*433d6423SLionel Sambuc * be misrepresented as being the original software. Credits must 26*433d6423SLionel Sambuc * appear in the documentation. 27*433d6423SLionel Sambuc * 28*433d6423SLionel Sambuc * 4. This notice may not be removed or altered. 29*433d6423SLionel Sambuc */ 30*433d6423SLionel Sambuc 31*433d6423SLionel Sambuc #include "cawf.h" 32*433d6423SLionel Sambuc 33*433d6423SLionel Sambuc 34*433d6423SLionel Sambuc /* 35*433d6423SLionel Sambuc * Asmcode(s, c) - assemble number/name code following backslash-character 36*433d6423SLionel Sambuc * definition - e. .g, "\\nPO" 37*433d6423SLionel Sambuc */ 38*433d6423SLionel Sambuc 39*433d6423SLionel Sambuc unsigned char * 40*433d6423SLionel Sambuc Asmcode(s, c) 41*433d6423SLionel Sambuc unsigned char **s; /* pointer to character after '\\' */ 42*433d6423SLionel Sambuc unsigned char *c; /* code destination (c[3]) */ 43*433d6423SLionel Sambuc { 44*433d6423SLionel Sambuc unsigned char *s1; 45*433d6423SLionel Sambuc 46*433d6423SLionel Sambuc s1 = *s + 1; 47*433d6423SLionel Sambuc c[0] = c[1] = c[2] = '\0'; 48*433d6423SLionel Sambuc if ((c[0] = *s1) == '(') { 49*433d6423SLionel Sambuc s1++; 50*433d6423SLionel Sambuc if ((c[0] = *s1) != '\0') { 51*433d6423SLionel Sambuc s1++; 52*433d6423SLionel Sambuc c[1] = *s1; 53*433d6423SLionel Sambuc } 54*433d6423SLionel Sambuc } 55*433d6423SLionel Sambuc return(s1); 56*433d6423SLionel Sambuc } 57*433d6423SLionel Sambuc 58*433d6423SLionel Sambuc 59*433d6423SLionel Sambuc /* 60*433d6423SLionel Sambuc * Delnum(nx) - delete number 61*433d6423SLionel Sambuc */ 62*433d6423SLionel Sambuc 63*433d6423SLionel Sambuc void 64*433d6423SLionel Sambuc Delnum(nx) 65*433d6423SLionel Sambuc int nx; /* number index */ 66*433d6423SLionel Sambuc { 67*433d6423SLionel Sambuc unsigned char buf[MAXLINE]; /* message buffer */ 68*433d6423SLionel Sambuc 69*433d6423SLionel Sambuc if (nx >= Nnr) { 70*433d6423SLionel Sambuc (void) sprintf((char *)buf, " bad Delnum(%d) index", nx); 71*433d6423SLionel Sambuc Error(FATAL, LINE, (char *)buf, NULL); 72*433d6423SLionel Sambuc } 73*433d6423SLionel Sambuc while (nx < (Nnr - 1)) { 74*433d6423SLionel Sambuc Numb[nx] = Numb[nx + 1]; 75*433d6423SLionel Sambuc nx++; 76*433d6423SLionel Sambuc } 77*433d6423SLionel Sambuc Nnr--; 78*433d6423SLionel Sambuc } 79*433d6423SLionel Sambuc 80*433d6423SLionel Sambuc 81*433d6423SLionel Sambuc /* 82*433d6423SLionel Sambuc * Findnum(n, v, e) - find or optionally enter number value 83*433d6423SLionel Sambuc */ 84*433d6423SLionel Sambuc 85*433d6423SLionel Sambuc Findnum(n, v, e) 86*433d6423SLionel Sambuc unsigned char *n; /* register name */ 87*433d6423SLionel Sambuc int v; /* value */ 88*433d6423SLionel Sambuc int e; /* 0 = find, don't enter 89*433d6423SLionel Sambuc * 1 = enter, don't find */ 90*433d6423SLionel Sambuc { 91*433d6423SLionel Sambuc int cmp, low, hi, mid; /* binary search controls */ 92*433d6423SLionel Sambuc unsigned char c[3]; /* name buffer */ 93*433d6423SLionel Sambuc 94*433d6423SLionel Sambuc c[0] = n[0]; 95*433d6423SLionel Sambuc c[1] = (n[1] == ' ' || n[1] == '\t') ? '\0' : n[1]; 96*433d6423SLionel Sambuc c[2] = '\0'; 97*433d6423SLionel Sambuc low = mid = 0; 98*433d6423SLionel Sambuc hi = Nnr - 1; 99*433d6423SLionel Sambuc while (low <= hi) { 100*433d6423SLionel Sambuc mid = (low + hi) / 2; 101*433d6423SLionel Sambuc if ((cmp = strncmp((char *)c, (char *)Numb[mid].nm, 2)) < 0) 102*433d6423SLionel Sambuc hi = mid - 1; 103*433d6423SLionel Sambuc else if (cmp > 0) 104*433d6423SLionel Sambuc low = mid + 1; 105*433d6423SLionel Sambuc else { 106*433d6423SLionel Sambuc if (e) 107*433d6423SLionel Sambuc Numb[mid].val = v; 108*433d6423SLionel Sambuc return(mid); 109*433d6423SLionel Sambuc } 110*433d6423SLionel Sambuc } 111*433d6423SLionel Sambuc if ( ! e) 112*433d6423SLionel Sambuc return(-1); 113*433d6423SLionel Sambuc if (Nnr >= MAXNR) 114*433d6423SLionel Sambuc Error(FATAL, LINE, " out of number registers at ", (char *)c); 115*433d6423SLionel Sambuc if (Nnr) { 116*433d6423SLionel Sambuc if (cmp > 0) 117*433d6423SLionel Sambuc mid++; 118*433d6423SLionel Sambuc for (hi = Nnr - 1; hi >= mid; hi--) 119*433d6423SLionel Sambuc Numb[hi+1] = Numb[hi]; 120*433d6423SLionel Sambuc } 121*433d6423SLionel Sambuc Nnr++; 122*433d6423SLionel Sambuc Numb[mid].nm[0] = c[0]; 123*433d6423SLionel Sambuc Numb[mid].nm[1] = c[1]; 124*433d6423SLionel Sambuc Numb[mid].val = v; 125*433d6423SLionel Sambuc return(mid); 126*433d6423SLionel Sambuc } 127*433d6423SLionel Sambuc 128*433d6423SLionel Sambuc 129*433d6423SLionel Sambuc /* 130*433d6423SLionel Sambuc * Findparms(n) - find parameter registers 131*433d6423SLionel Sambuc */ 132*433d6423SLionel Sambuc 133*433d6423SLionel Sambuc Findparms(n) 134*433d6423SLionel Sambuc unsigned char *n; /* parameter name */ 135*433d6423SLionel Sambuc { 136*433d6423SLionel Sambuc unsigned char c[3]; /* character buffer */ 137*433d6423SLionel Sambuc int i; /* temporary index */ 138*433d6423SLionel Sambuc 139*433d6423SLionel Sambuc c[0] = n[0]; 140*433d6423SLionel Sambuc c[1] = (n[1] == ' ' || n[1] == '\t') ? '\0' : n[1]; 141*433d6423SLionel Sambuc c[2] = '\0'; 142*433d6423SLionel Sambuc for (i = 0; Parms[i].nm[0]; i++) { 143*433d6423SLionel Sambuc if (c[0] == Parms[i].nm[0] && c[1] == Parms[i].nm[1]) 144*433d6423SLionel Sambuc return(i); 145*433d6423SLionel Sambuc } 146*433d6423SLionel Sambuc return(-1); 147*433d6423SLionel Sambuc } 148*433d6423SLionel Sambuc 149*433d6423SLionel Sambuc 150*433d6423SLionel Sambuc /* 151*433d6423SLionel Sambuc * Findscale(n, v, e) - find and optionally enter scaling factor value 152*433d6423SLionel Sambuc */ 153*433d6423SLionel Sambuc 154*433d6423SLionel Sambuc Findscale(n, v, e) 155*433d6423SLionel Sambuc int n; /* scaling factor name */ 156*433d6423SLionel Sambuc double v; /* value */ 157*433d6423SLionel Sambuc int e; /* 0 = find, don't enter 158*433d6423SLionel Sambuc * 1 = enter, don't find */ 159*433d6423SLionel Sambuc { 160*433d6423SLionel Sambuc int i; 161*433d6423SLionel Sambuc double *pval; 162*433d6423SLionel Sambuc 163*433d6423SLionel Sambuc for (i = 0; Scale[i].nm; i++) { 164*433d6423SLionel Sambuc if ((unsigned char )n == Scale[i].nm) 165*433d6423SLionel Sambuc break; 166*433d6423SLionel Sambuc } 167*433d6423SLionel Sambuc if (Scale[i].nm) { 168*433d6423SLionel Sambuc if (e) { 169*433d6423SLionel Sambuc pval = &Scale[i].val; 170*433d6423SLionel Sambuc *pval = v; 171*433d6423SLionel Sambuc } 172*433d6423SLionel Sambuc return(i); 173*433d6423SLionel Sambuc } 174*433d6423SLionel Sambuc return(-1); 175*433d6423SLionel Sambuc } 176