minor fixes for safecopy & safemap tests
[minix.git] / commands / cawf / expr.c
blob1b19e16e766633099cbfe36f5c23bf43b4acf7eb
1 /*
2 * expr.c - expression support functions for cawf(1)
3 */
5 /*
6 * Copyright (c) 1991 Purdue University Research Foundation,
7 * West Lafayette, Indiana 47907. All rights reserved.
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.
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:
17 * 1. The author is not responsible for any consequences of use of
18 * this software, even if they arise from flaws in it.
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.
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.
28 * 4. This notice may not be removed or altered.
31 #include "cawf.h"
35 * Asmcode(s, c) - assemble number/name code following backslash-character
36 * definition - e. .g, "\\nPO"
39 unsigned char *
40 Asmcode(s, c)
41 unsigned char **s; /* pointer to character after '\\' */
42 unsigned char *c; /* code destination (c[3]) */
44 unsigned char *s1;
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;
55 return(s1);
60 * Delnum(nx) - delete number
63 void
64 Delnum(nx)
65 int nx; /* number index */
67 unsigned char buf[MAXLINE]; /* message buffer */
69 if (nx >= Nnr) {
70 (void) sprintf((char *)buf, " bad Delnum(%d) index", nx);
71 Error(FATAL, LINE, (char *)buf, NULL);
73 while (nx < (Nnr - 1)) {
74 Numb[nx] = Numb[nx + 1];
75 nx++;
77 Nnr--;
82 * Findnum(n, v, e) - find or optionally enter number value
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 */
91 int cmp, low, hi, mid; /* binary search controls */
92 unsigned char c[3]; /* name buffer */
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);
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];
121 Nnr++;
122 Numb[mid].nm[0] = c[0];
123 Numb[mid].nm[1] = c[1];
124 Numb[mid].val = v;
125 return(mid);
130 * Findparms(n) - find parameter registers
133 Findparms(n)
134 unsigned char *n; /* parameter name */
136 unsigned char c[3]; /* character buffer */
137 int i; /* temporary index */
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);
146 return(-1);
151 * Findscale(n, v, e) - find and optionally enter scaling factor value
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 */
160 int i;
161 double *pval;
163 for (i = 0; Scale[i].nm; i++) {
164 if ((unsigned char )n == Scale[i].nm)
165 break;
167 if (Scale[i].nm) {
168 if (e) {
169 pval = &Scale[i].val;
170 *pval = v;
172 return(i);
174 return(-1);