expr.c

Go to the documentation of this file.
00001 /*
00002  *      expr.c - expression support functions for cawf(1)
00003  */
00004 
00005 /*
00006  *      Copyright (c) 1991 Purdue University Research Foundation,
00007  *      West Lafayette, Indiana 47907.  All rights reserved.
00008  *
00009  *      Written by Victor A. Abell <abe@mace.cc.purdue.edu>,  Purdue
00010  *      University Computing Center.  Not derived from licensed software;
00011  *      derived from awf(1) by Henry Spencer of the University of Toronto.
00012  *
00013  *      Permission is granted to anyone to use this software for any
00014  *      purpose on any computer system, and to alter it and redistribute
00015  *      it freely, subject to the following restrictions:
00016  *
00017  *      1. The author is not responsible for any consequences of use of
00018  *         this software, even if they arise from flaws in it.
00019  *
00020  *      2. The origin of this software must not be misrepresented, either
00021  *         by explicit claim or by omission.  Credits must appear in the
00022  *         documentation.
00023  *
00024  *      3. Altered versions must be plainly marked as such, and must not
00025  *         be misrepresented as being the original software.  Credits must
00026  *         appear in the documentation.
00027  *
00028  *      4. This notice may not be removed or altered.
00029  */
00030 
00031 #include "cawf.h"
00032 
00033 
00034 /*
00035  * Asmcode(s, c) - assemble number/name code following backslash-character
00036  *                 definition  - e. .g, "\\nPO"
00037  */
00038 
00039 unsigned char *
00040 Asmcode(s, c)
00041         unsigned char **s;              /* pointer to character after '\\' */
00042         unsigned char *c;               /* code destination (c[3]) */
00043 {
00044         unsigned char *s1;
00045 
00046         s1 = *s + 1;
00047         c[0] = c[1] = c[2] = '\0';
00048         if ((c[0] = *s1) == '(') {
00049                 s1++;
00050                 if ((c[0] = *s1) != '\0') {
00051                         s1++;
00052                         c[1] = *s1;
00053                 }
00054         }
00055         return(s1);
00056 }
00057 
00058 
00059 /*
00060  * Delnum(nx) - delete number
00061  */
00062 
00063 void
00064 Delnum(nx)
00065         int nx;                         /* number index */
00066 {
00067         unsigned char buf[MAXLINE];     /* message buffer */
00068 
00069         if (nx >= Nnr) {
00070                 (void) sprintf((char *)buf, " bad Delnum(%d) index", nx);
00071                 Error(FATAL, LINE, (char *)buf, NULL);
00072         }
00073         while (nx < (Nnr - 1)) {
00074                 Numb[nx] = Numb[nx + 1];
00075                 nx++;
00076         }
00077         Nnr--;
00078 }
00079 
00080 
00081 /*
00082  * Findnum(n, v, e) - find or optionally enter number value
00083  */
00084 
00085 Findnum(n, v, e)
00086         unsigned char *n;               /* register name */
00087         int v;                          /* value */
00088         int e;                          /* 0 = find, don't enter
00089                                          * 1 = enter, don't find */
00090 {
00091         int cmp, low, hi, mid;          /* binary search controls */
00092         unsigned char c[3];             /* name buffer */
00093 
00094         c[0] = n[0];
00095         c[1] = (n[1] == ' ' || n[1] == '\t') ? '\0' : n[1];
00096         c[2] = '\0';
00097         low = mid = 0;
00098         hi = Nnr - 1;
00099         while (low <= hi) {
00100                 mid = (low + hi) / 2;
00101                 if ((cmp = strncmp((char *)c, (char *)Numb[mid].nm, 2)) < 0)
00102                         hi = mid - 1;
00103                 else if (cmp > 0)
00104                         low = mid + 1;
00105                 else {
00106                         if (e)
00107                                 Numb[mid].val = v;
00108                         return(mid);
00109                 }
00110         }
00111         if ( ! e)
00112                 return(-1);
00113         if (Nnr >= MAXNR)
00114                 Error(FATAL, LINE, " out of number registers at ", (char *)c);
00115         if (Nnr) {
00116                 if (cmp > 0)
00117                         mid++;
00118                 for (hi = Nnr - 1; hi >= mid; hi--)
00119                         Numb[hi+1] = Numb[hi];
00120         }
00121         Nnr++;
00122         Numb[mid].nm[0] = c[0];
00123         Numb[mid].nm[1] = c[1];
00124         Numb[mid].val = v;
00125         return(mid);
00126 }
00127 
00128 
00129 /*
00130  * Findparms(n) - find parameter registers
00131  */
00132 
00133 Findparms(n)
00134         unsigned char *n;               /* parameter name */
00135 {
00136         unsigned char c[3];             /* character buffer */
00137         int i;                          /* temporary index */
00138 
00139         c[0] = n[0];
00140         c[1] = (n[1] == ' ' || n[1] == '\t') ? '\0' : n[1];
00141         c[2] = '\0';
00142         for (i = 0; Parms[i].nm[0]; i++) {
00143                 if (c[0] == Parms[i].nm[0] && c[1] == Parms[i].nm[1])
00144                         return(i);
00145         }
00146         return(-1);
00147 }
00148 
00149 
00150 /*
00151  * Findscale(n, v, e) - find and optionally enter scaling factor value
00152  */
00153 
00154 Findscale(n, v, e)
00155         int n;                          /* scaling factor name */
00156         double v;                       /* value */
00157         int e;                          /* 0 = find, don't enter
00158                                          * 1 = enter, don't find */
00159 {
00160         int i;
00161         double *pval;
00162 
00163         for (i = 0; Scale[i].nm; i++) {
00164                 if ((unsigned char )n == Scale[i].nm)
00165                         break;
00166         }
00167         if (Scale[i].nm) {
00168                 if (e) {
00169                         pval = &Scale[i].val;
00170                         *pval = v;
00171                 }
00172                 return(i);
00173         }
00174         return(-1);
00175 }

Generated on Fri Apr 14 22:56:39 2006 for minix by  doxygen 1.4.6