1433d6423SLionel Sambuc /*
2433d6423SLionel Sambuc * expr.c - expression support functions for cawf(1)
3433d6423SLionel Sambuc */
4433d6423SLionel Sambuc
5433d6423SLionel Sambuc /*
6433d6423SLionel Sambuc * Copyright (c) 1991 Purdue University Research Foundation,
7433d6423SLionel Sambuc * West Lafayette, Indiana 47907. All rights reserved.
8433d6423SLionel Sambuc *
9433d6423SLionel Sambuc * Written by Victor A. Abell <abe@mace.cc.purdue.edu>, Purdue
10433d6423SLionel Sambuc * University Computing Center. Not derived from licensed software;
11433d6423SLionel Sambuc * derived from awf(1) by Henry Spencer of the University of Toronto.
12433d6423SLionel Sambuc *
13433d6423SLionel Sambuc * Permission is granted to anyone to use this software for any
14433d6423SLionel Sambuc * purpose on any computer system, and to alter it and redistribute
15433d6423SLionel Sambuc * it freely, subject to the following restrictions:
16433d6423SLionel Sambuc *
17433d6423SLionel Sambuc * 1. The author is not responsible for any consequences of use of
18433d6423SLionel Sambuc * this software, even if they arise from flaws in it.
19433d6423SLionel Sambuc *
20433d6423SLionel Sambuc * 2. The origin of this software must not be misrepresented, either
21433d6423SLionel Sambuc * by explicit claim or by omission. Credits must appear in the
22433d6423SLionel Sambuc * documentation.
23433d6423SLionel Sambuc *
24433d6423SLionel Sambuc * 3. Altered versions must be plainly marked as such, and must not
25433d6423SLionel Sambuc * be misrepresented as being the original software. Credits must
26433d6423SLionel Sambuc * appear in the documentation.
27433d6423SLionel Sambuc *
28433d6423SLionel Sambuc * 4. This notice may not be removed or altered.
29433d6423SLionel Sambuc */
30433d6423SLionel Sambuc
31433d6423SLionel Sambuc #include "cawf.h"
32433d6423SLionel Sambuc
33433d6423SLionel Sambuc
34433d6423SLionel Sambuc /*
35433d6423SLionel Sambuc * Asmcode(s, c) - assemble number/name code following backslash-character
36433d6423SLionel Sambuc * definition - e. .g, "\\nPO"
37433d6423SLionel Sambuc */
38433d6423SLionel Sambuc
Asmcode(unsigned char ** s,unsigned char * c)39*d9494baaSJacob Adams unsigned char *Asmcode(unsigned char **s, unsigned char *c) {
40*d9494baaSJacob Adams /* pointer to character after '\\' s
41*d9494baaSJacob Adams * code destination (c[3]) c
42*d9494baaSJacob Adams */
43433d6423SLionel Sambuc unsigned char *s1;
44433d6423SLionel Sambuc
45433d6423SLionel Sambuc s1 = *s + 1;
46433d6423SLionel Sambuc c[0] = c[1] = c[2] = '\0';
47433d6423SLionel Sambuc if ((c[0] = *s1) == '(') {
48433d6423SLionel Sambuc s1++;
49433d6423SLionel Sambuc if ((c[0] = *s1) != '\0') {
50433d6423SLionel Sambuc s1++;
51433d6423SLionel Sambuc c[1] = *s1;
52433d6423SLionel Sambuc }
53433d6423SLionel Sambuc }
54433d6423SLionel Sambuc return(s1);
55433d6423SLionel Sambuc }
56433d6423SLionel Sambuc
57433d6423SLionel Sambuc
58433d6423SLionel Sambuc /*
59433d6423SLionel Sambuc * Delnum(nx) - delete number
60433d6423SLionel Sambuc */
61433d6423SLionel Sambuc
Delnum(int nx)62*d9494baaSJacob Adams void Delnum(int nx) {
63*d9494baaSJacob Adams /* number index nx */
64*d9494baaSJacob Adams
65433d6423SLionel Sambuc unsigned char buf[MAXLINE]; /* message buffer */
66433d6423SLionel Sambuc
67433d6423SLionel Sambuc if (nx >= Nnr) {
68433d6423SLionel Sambuc (void) sprintf((char *)buf, " bad Delnum(%d) index", nx);
69433d6423SLionel Sambuc Error(FATAL, LINE, (char *)buf, NULL);
70433d6423SLionel Sambuc }
71433d6423SLionel Sambuc while (nx < (Nnr - 1)) {
72433d6423SLionel Sambuc Numb[nx] = Numb[nx + 1];
73433d6423SLionel Sambuc nx++;
74433d6423SLionel Sambuc }
75433d6423SLionel Sambuc Nnr--;
76433d6423SLionel Sambuc }
77433d6423SLionel Sambuc
78433d6423SLionel Sambuc
79433d6423SLionel Sambuc /*
80433d6423SLionel Sambuc * Findnum(n, v, e) - find or optionally enter number value
81433d6423SLionel Sambuc */
82433d6423SLionel Sambuc
Findnum(unsigned char * n,int v,int e)83*d9494baaSJacob Adams int Findnum(unsigned char *n, int v, int e) {
84*d9494baaSJacob Adams /* register name n
85*d9494baaSJacob Adams * value v
86*d9494baaSJacob Adams * e = 0 = find, don't enter
87*d9494baaSJacob Adams * e = 1 = enter, don't find
88*d9494baaSJacob Adams */
89433d6423SLionel Sambuc int cmp, low, hi, mid; /* binary search controls */
90433d6423SLionel Sambuc unsigned char c[3]; /* name buffer */
91433d6423SLionel Sambuc
92433d6423SLionel Sambuc c[0] = n[0];
93433d6423SLionel Sambuc c[1] = (n[1] == ' ' || n[1] == '\t') ? '\0' : n[1];
94433d6423SLionel Sambuc c[2] = '\0';
95433d6423SLionel Sambuc low = mid = 0;
96433d6423SLionel Sambuc hi = Nnr - 1;
97433d6423SLionel Sambuc while (low <= hi) {
98433d6423SLionel Sambuc mid = (low + hi) / 2;
99433d6423SLionel Sambuc if ((cmp = strncmp((char *)c, (char *)Numb[mid].nm, 2)) < 0)
100433d6423SLionel Sambuc hi = mid - 1;
101433d6423SLionel Sambuc else if (cmp > 0)
102433d6423SLionel Sambuc low = mid + 1;
103433d6423SLionel Sambuc else {
104433d6423SLionel Sambuc if (e)
105433d6423SLionel Sambuc Numb[mid].val = v;
106433d6423SLionel Sambuc return(mid);
107433d6423SLionel Sambuc }
108433d6423SLionel Sambuc }
109433d6423SLionel Sambuc if ( ! e)
110433d6423SLionel Sambuc return(-1);
111433d6423SLionel Sambuc if (Nnr >= MAXNR)
112433d6423SLionel Sambuc Error(FATAL, LINE, " out of number registers at ", (char *)c);
113433d6423SLionel Sambuc if (Nnr) {
114433d6423SLionel Sambuc if (cmp > 0)
115433d6423SLionel Sambuc mid++;
116433d6423SLionel Sambuc for (hi = Nnr - 1; hi >= mid; hi--)
117433d6423SLionel Sambuc Numb[hi+1] = Numb[hi];
118433d6423SLionel Sambuc }
119433d6423SLionel Sambuc Nnr++;
120433d6423SLionel Sambuc Numb[mid].nm[0] = c[0];
121433d6423SLionel Sambuc Numb[mid].nm[1] = c[1];
122433d6423SLionel Sambuc Numb[mid].val = v;
123433d6423SLionel Sambuc return(mid);
124433d6423SLionel Sambuc }
125433d6423SLionel Sambuc
126433d6423SLionel Sambuc
127433d6423SLionel Sambuc /*
128433d6423SLionel Sambuc * Findparms(n) - find parameter registers
129433d6423SLionel Sambuc */
130433d6423SLionel Sambuc
Findparms(unsigned char * n)131*d9494baaSJacob Adams int Findparms(unsigned char *n) {
132*d9494baaSJacob Adams /* parameter name n */
133433d6423SLionel Sambuc unsigned char c[3]; /* character buffer */
134433d6423SLionel Sambuc int i; /* temporary index */
135433d6423SLionel Sambuc
136433d6423SLionel Sambuc c[0] = n[0];
137433d6423SLionel Sambuc c[1] = (n[1] == ' ' || n[1] == '\t') ? '\0' : n[1];
138433d6423SLionel Sambuc c[2] = '\0';
139433d6423SLionel Sambuc for (i = 0; Parms[i].nm[0]; i++) {
140433d6423SLionel Sambuc if (c[0] == Parms[i].nm[0] && c[1] == Parms[i].nm[1])
141433d6423SLionel Sambuc return(i);
142433d6423SLionel Sambuc }
143433d6423SLionel Sambuc return(-1);
144433d6423SLionel Sambuc }
145433d6423SLionel Sambuc
146433d6423SLionel Sambuc
147433d6423SLionel Sambuc /*
148433d6423SLionel Sambuc * Findscale(n, v, e) - find and optionally enter scaling factor value
149433d6423SLionel Sambuc */
150433d6423SLionel Sambuc
Findscale(int n,double v,int e)151*d9494baaSJacob Adams int Findscale(int n, double v, int e) {
152*d9494baaSJacob Adams /* scaling factor name n
153*d9494baaSJacob Adams * value v
154*d9494baaSJacob Adams * e = 0 = find, don't enter
155*d9494baaSJacob Adams * e = 1 = enter, don't find
156*d9494baaSJacob Adams */
157433d6423SLionel Sambuc int i;
158433d6423SLionel Sambuc double *pval;
159433d6423SLionel Sambuc
160433d6423SLionel Sambuc for (i = 0; Scale[i].nm; i++) {
161433d6423SLionel Sambuc if ((unsigned char )n == Scale[i].nm)
162433d6423SLionel Sambuc break;
163433d6423SLionel Sambuc }
164433d6423SLionel Sambuc if (Scale[i].nm) {
165433d6423SLionel Sambuc if (e) {
166433d6423SLionel Sambuc pval = &Scale[i].val;
167433d6423SLionel Sambuc *pval = v;
168433d6423SLionel Sambuc }
169433d6423SLionel Sambuc return(i);
170433d6423SLionel Sambuc }
171433d6423SLionel Sambuc return(-1);
172433d6423SLionel Sambuc }
173