Merge branch 'master' of https://git.code.sf.net/p/cdesktopenv/code
[oweals/cde.git] / cde / programs / dtcalc / calctool.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: calctool.c /main/9 1996/09/25 11:28:16 rswiston $ */
24 /*                                                                      *
25  *  calctool.c                                                          *
26  *   Contains the none user interface portion of the Desktop            *
27  *   Calculator.                                                        *
28  *                                                                      *
29  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
30  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
31  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
32  * (c) Copyright 1993, 1994 Novell, Inc.                                *
33  */
34
35 #include <stdio.h>
36 #include <ctype.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/file.h>
42 #include <sys/param.h>
43 #include <pwd.h>
44 #include <math.h>
45 #include "patchlevel.h"
46 #include "calctool.h"
47 #include "ds_common.h"
48
49 #ifdef sun
50
51 /* Copied from math.h */
52 struct exception {
53         int type;
54         char *name;
55         double arg1;
56         double arg2;
57         double retval;
58 };
59
60 #endif
61
62 time_t time() ;
63
64 double max_fix[4] = {
65           6.871947674e+10, 3.245185537e+32,
66           1.000000000e+36, 2.230074520e+43
67 } ;
68 double min_fix0[4] = {
69           2.500000000e-1, 3.750000000e-1,
70           1.000000000e-1, 4.375000000e-1
71 } ;
72 double min_fix1[4] = {
73           1.250000000e-1, 4.687500000e-2,
74           1.000000000e-2, 2.734375000e-2
75 } ;
76 double min_fix2[4] = {
77           6.250000000e-2, 5.859375000e-3,
78           1.000000000e-3, 1.708984375e-3
79 } ;
80 double min_fix3[4] = {
81           3.125000000e-2, 7.324218750e-4,
82           1.000000000e-4, 1.068115234e-4
83 } ;
84 double min_fix4[4] = {
85           1.562500000e-2, 9.155273437e-5,
86           1.000000000e-5, 6.675720215e-6
87 } ;
88 double min_fix5[4] = {
89           7.812500000e-3, 1.144409180e-5,
90           1.000000000e-6, 4.172325134e-7
91 } ;
92 double min_fix6[4] = {
93           6.906250000e-3, 1.430511475e-6,
94           1.000000000e-7, 2.607703209e-8
95 } ;
96 double min_fix7[4] = {
97           1.953125000e-3, 1.788139343e-7,
98           1.000000000e-8, 1.629814506e-9
99 } ;
100 double min_fix8[4] = {
101           9.765625000e-4, 2.235174179e-8,
102           1.000000000e-9, 1.018634066e-10
103 } ;
104 double min_fix9[4] = {
105           4.882812500e-4, 2.793967724e-9,
106           1.000000000e-10, 6.366462912e-12
107 } ;
108
109 extern char *base_str[] ;       /* Strings for each base value. */
110 extern char *cmdstr[] ;         /* Strings for each command line option. */
111 extern char *dtype_str[] ;      /* Strings for each display mode value. */
112 extern char *lstrs[] ;          /* Labels for various Motif items. */
113 extern char *mess[] ;           /* Message strings. */
114 extern char *mode_str[] ;       /* Strings for each mode value. */
115 extern char *opts[] ;           /* Command line option strings. */
116 extern char *ttype_str[] ;      /* Strings for each trig type value. */
117 extern char *ustrs[] ;          /* Usage message strings. */
118 extern char *vstrs[] ;          /* Various strings. */
119
120 char digits[] = "0123456789ABCDEF" ;
121 int basevals[4] = { 2, 8, 10, 16 } ;
122
123 int left_pos[BCOLS]  = { 3, 2, 1, 0 } ;  /* Left positions. */
124 int right_pos[BCOLS] = { 0, 1, 2, 3 } ;  /* "Right" positions. */
125
126 /* Valid keys when an error condition has occured. */
127 /*                            MEM  KEYS clr     clr     QUIT REDRAW */
128 char validkeys[MAXVKEYS]  = { 'm', 'k', '\177', '\013', 'q', '\f' } ;
129
130 Vars v ;            /* Calctool variables and options. */
131
132 struct menu_entry menu_entries[MAXENTRIES] ;
133
134 struct menu cmenus[MAXMENUS] = {                 /* Calculator menus. */
135 /*      title     total index defval                           */
136   { (char *) NULL, 10,    0,    2  /* 2 places */    },    /* ACC */
137   { (char *) NULL,  4,   20,    2  /* Decimal  */    },    /* BASE TYPE */
138   { (char *) NULL, 10,    0,    0  /* Con. 0   */    },    /* CON */
139   { (char *) NULL, 10,   10,    0  /* Reg. 0   */    },    /* EXCH */
140   { (char *) NULL, 10,    0,    0  /* Fun. 0   */    },    /* FUN */
141   { (char *) NULL,  4,   30,    0  /* Basic    */    },    /* MODE */
142   { (char *) NULL,  3,   24,    1  /* Fixed    */    },    /* NUM TYPE */
143   { (char *) NULL, 10,   10,    0  /* Reg. 0   */    },    /* RCL */
144   { (char *) NULL, 10,   10,    0  /* Reg. 0   */    },    /* STO */
145   { (char *) NULL,  3,   27,    0  /* Degrees  */    },    /* TRIG TYPE */
146 } ;
147
148 /*  This table shows the keyboard values that are currently being used:
149  *
150  *           | a b c d e f g h i j k l m n o p q r s t u v w x y z
151  *-------------+--------------------------------------------------
152  *  Control: | a   c d   f   h i     l m         r s t u       y
153  *  Lower:   | a b c d e f   h i   k   m n   p q r s     v   x y
154  *  Upper:   | A B C D E F G           M N   P Q R S T       X
155  *  Numeric: | 0 1 2 3 4 5 6 7 8 9
156  *  Other:   | @ . + - * / = % ( ) # < > [ ] { } | & ~ ^ ? ! \177
157  *----------------------------------------------------------------
158  */
159
160 /* Calculator button values. */
161
162 struct button buttons[TITEMS] = {
163 /*     str       str2      value opdisp   menutype   resname  func */
164
165 /* Row 1. */
166 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "blank",  do_none },
167 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_FUN,    "fun",    do_pending },
168 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_CON,    "con",    do_pending },
169 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "keys",   do_keys    },
170
171 /* Row 2. */
172 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "int",    do_portion },
173 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "frac",   do_portion },
174 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "abs",    do_portion },
175 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "exp",    do_expno   },
176
177 /* Row 3. */
178 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_ACC,    "acc",    do_pending },
179 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_STO,    "sto",    do_pending },
180 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_RCL,    "rcl",    do_pending },
181 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_EXCH,   "exch",   do_pending },
182
183 /* Row 4. */
184 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "blank",  do_none },
185 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "blank",  do_none },
186 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "blank",  do_none },
187 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "blank",  do_none },
188
189 /* Row 5. */ 
190 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "blank",  do_none },
191 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "blank",  do_none },
192 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "blank",  do_none },
193 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "blank",  do_none },
194
195 /* Row 6. */ 
196 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "blank",  do_none },
197 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "blank",  do_none },
198 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "blank",  do_none },
199 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "blank",  do_none },
200
201 /* Row 7. */
202 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "recip",   do_immed   },
203 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "square",  do_immed   },
204 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "sqrt",    do_immed   },
205 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "percent", do_calc   },
206
207 /* Row 8. */
208 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "lparen", do_paren   },
209 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "rparen", do_paren   },
210 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "bsp",    do_delete  },
211 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "clr",    do_clear   },
212
213 /* Row 9. */
214 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "numd",   do_number  },
215 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "nume",   do_number  },
216 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "numf",   do_number  },
217 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "chs",    do_immed   },
218
219 /* Row 10. */
220 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "numa",   do_number  },
221 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "numb",   do_number  },
222 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "numc",   do_number  },
223 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "mul",    do_calc    },
224
225 /* Row 11. */
226 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "num7",   do_number  },
227 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "num8",   do_number  },
228 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "num9",   do_number  },
229 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "div",    do_calc    },
230
231 /* Row 12. */
232 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "num4",   do_number  },
233 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "num5",   do_number  },
234 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "num6",   do_number  },
235 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "sub",    do_calc    },
236
237 /* Row 13. */
238 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "num1",   do_number  },
239 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "num2",   do_number  },
240 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "num3",   do_number  },
241 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "add",    do_calc    },
242
243 /* Row 14. */
244 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "num0",   do_number  },
245 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "point",  do_point   },
246 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "equals", do_calc    },
247 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "quit",   do_frame   },
248
249 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_BASE,   "base",   do_pending },
250 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NUM,    "disp",   do_pending },
251 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_MODE,   "mode",   do_pending },
252 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_TRIG,   "trig",   do_pending },
253 } ; 
254
255 struct button mode_buttons[MAXMODES * MODEKEYS] = {
256 /*     str       str2      value opdisp   menutype   resname  func */
257
258 /* Financial. */
259 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "term",   do_business},
260 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "rate",   do_business},
261 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "pv",     do_business},
262 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "pmt",    do_business},
263 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "fv",     do_business},
264 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "ppy",    do_business},
265 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "clrreg", do_business},
266 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "ctrm",   do_business},
267 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "ddb",    do_business},
268 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "sln",    do_business},
269 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "syd",    do_business},
270 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
271 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
272 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
273 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
274 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
275
276 /* Logical. */
277 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "lshift", do_pending },
278 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "rshift", do_pending },
279 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "and16",  do_immed   },
280 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "and32",  do_immed   },
281 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "or",     do_calc    },
282 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "and",    do_calc    },
283 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "not",    do_immed   },
284 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "xor",    do_calc    },
285 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "xnor",   do_calc    },
286 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
287 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
288 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
289 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
290 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
291 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
292 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
293
294 /* Scientific. */
295 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "inv",    do_immed   },
296 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "hyp",    do_immed   },
297 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "etox",   do_immed   },
298 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "tentox", do_immed   },
299 { (char *)NULL, (char *)NULL, 0, OP_SET,   M_NONE,   "ytox",   do_calc    },
300 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "xfact",  do_immed   },
301 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "cos",    do_trig    },
302 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "sin",    do_trig    },
303 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "tan",    do_trig    },
304 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "ln",     do_immed   },
305 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "log",    do_immed   },
306 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE,   "rand",   do_immed   },
307 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
308 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
309 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
310 { (char *)NULL, (char *)NULL, 0, OP_NOP,   M_NONE,   "",       do_none    },
311 } ;
312
313
314 int
315 char_val(chr)
316 char chr ;
317 {
318        if (chr >= '0' && chr <= '9') return(chr - '0') ;
319   else if (chr >= 'a' && chr <= 'f') return(chr - 'a' + 10) ;
320   else if (chr >= 'A' && chr <= 'F') return(chr - 'A' + 10) ;
321   else return(-1) ;
322 }
323
324
325 void
326 clear_display()
327 {
328   int i ;
329  
330   v->pointed = 0 ;
331   v->toclear = 1 ;
332   v->defState = 1 ;
333   i = 0 ;
334   mpcim(&i, v->MPdisp_val) ;
335   STRCPY(v->display, make_number(v->MPdisp_val, FALSE)) ;
336   set_item(DISPLAYITEM, v->display) ;
337   
338   v->hyperbolic = 0 ;
339   v->inverse    = 0 ;
340   v->show_paren = 0 ;
341   v->opsptr     = 0 ;            /* Clear parentheses stacks. */
342   v->numsptr    = 0 ;
343   set_item(HYPITEM, "    ") ;
344   set_item(INVITEM, "    ") ;
345 }
346
347
348 char *
349 convert(line)              /* Convert .dtcalcrc line to ascii values. */
350 char *line ;               /* Input line to be converted. */
351 {
352   static char output[MAXLINE] ;   /* Converted output record. */
353   int ctrl = 0       ;     /* Set if we are processing a control character. */
354   int i ;                  /* Position within input line. */
355   int len ;
356   int n = 0 ;              /* Position within output line. */
357  
358   len = strlen(line) ;
359   for (i = 0; i < len; i++)
360     {
361            if (line[i] == ' ') continue ;
362       else if (line[i] == '\\') ctrl = 1 ;
363       else if (ctrl)
364         {
365           output[n++] = CTL(line[i]) ;
366           ctrl = 0 ;
367         }
368       else output[n++] = line[i] ;
369     }
370   output[n] = '\0' ;
371   return(output) ;
372 }
373
374
375 void
376 do_dtcalc(argc, argv)
377 int argc ;
378 char **argv ;
379 {
380   char *ptr ;
381   int i ;
382
383   v->progname = argv[0] ;     /* Save programs name. */
384   v->appname  = NULL ;
385   init_cmdline_opts() ;       /* Initialise command line option strings. */
386
387   if ((ptr = strrchr(argv[0], '/')) != NULL)
388     read_str(&v->appname, ptr+1) ;
389   else read_str(&v->appname, argv[0]) ;
390
391   init_text() ;               /* Setup text strings depending upon language. */
392   init_vars() ;               /* Setup default values for variables. */
393   key_init() ;                /* Determine numeric function keys. */
394   load_resources() ;          /* Get resources from various places. */
395   read_resources() ;          /* Read resources from merged database. */
396   get_options(argc, argv) ;   /* Get command line arguments. */
397   if(application_args.session != NULL)
398   {
399       RestoreSession(); 
400   }
401   read_rcfiles() ;            /* Read .dtcalcrc's files. */
402   init_graphics() ;
403   make_frames() ;             /* Create dtcalc window frames. */
404
405   v->shelf      = NULL ;      /* No selection for shelf initially. */
406   v->noparens   = 0 ;         /* No unmatched brackets initially. */
407   v->opsptr     = 0 ;         /* Nothing on the parentheses op stack. */
408   v->numsptr    = 0 ;         /* Nothing on the parenthese numeric stack. */
409   v->pending    = 0 ;         /* No initial pending command. */
410   if(application_args.session == NULL)
411      v->tstate     = 0 ;      /* Button values displayed first. */
412   v->hyperbolic = 0 ;         /* Normal trig functions initially. */
413   v->inverse    = 0 ;         /* No inverse functions initially. */
414
415   srand48((long) time((time_t *) 0)) ;   /* Seed random number generator. */
416
417   make_items() ;              /* Create server images and fir frames. */
418   if(v->display[0] == 0)
419      do_clear() ;                /* Initialise and clear display. */
420
421   if (v->rstate == TRUE)      /* Show the memory register window? */
422     {
423       make_registers(MEM) ;
424       if (!v->iconic) win_display(FCP_REG, TRUE) ;
425     }
426   if (!v->iconic) win_display(FCP_MODE, TRUE) ;
427   show_display(v->MPdisp_val) ;     /* Output in correct display mode. */
428   save_cmdline(argc, argv) ;        /* Setup dtcalc command line. */
429   start_tool() ;                    /* Display the calculator. */
430 }
431
432
433 /* Dtcalc's customised math library error-handling routine. */
434
435 void
436 doerr(errmes)
437 char *errmes ;
438 {
439   if (!v->started) return ;
440   STRCPY(v->display, errmes) ;
441   set_item(DISPLAYITEM, v->display) ;
442   v->error = 1 ;
443   beep() ;
444   set_item(OPITEM, vstrs[(int) V_CLR]) ;
445 }
446
447
448 int
449 get_bool_resource(rtype, boolval)   /* Get boolean resource from database. */
450 enum res_type rtype ;
451 int *boolval ;
452 {
453   char *val, tempstr[MAXLINE] ;
454   int len, n ;
455  
456   if ((val = get_resource(rtype)) == NULL) return(0) ;
457   STRCPY(tempstr, val) ;
458   len = strlen(tempstr) ;
459   for (n = 0; n < len; n++)
460     if (isupper(tempstr[n])) tempstr[n] = tolower(tempstr[n]) ;
461   if (EQUAL(tempstr, vstrs[(int) V_TRUE])) *boolval = TRUE ;
462   else                                     *boolval = FALSE ;
463   return(1) ;
464 }
465
466
467 /*  Get button index for given character value, setting curwin,
468  *  row and column appropriately. Note that if the value isn't found,
469  *  then a value of TITEMS is returned. This is "chucked out" by
470  *  process_item as being invalid.
471  *
472  *  XXX: This routine can be improved by using a hash lookup table.
473  */
474  
475 int
476 get_index(ch)
477 char ch ;
478 {
479   int n, val ;
480  
481   for (n = 0; n < TITEMS; n++) {
482     if (ch == buttons[n].value)  
483        break ;
484   }
485   if (n < TITEMS) 
486      v->curwin = FCP_KEY ;
487   else
488   {
489      return(TITEMS) ;
490   }
491   v->row = n / MAXCOLS ;
492   v->column = n - (v->row * MAXCOLS) ;
493   return(n) ;
494 }
495
496
497 int
498 get_int_resource(rtype, intval)   /* Get integer resource from database. */
499 enum res_type rtype ;
500 int *intval ;
501 {
502   char *val ;
503
504   if ((val = get_resource(rtype)) == NULL) return(0) ;
505   *intval = atoi(val) ;
506   return(1) ;
507 }
508
509
510 /* Get keyboard equivalent from first character of localised string. */
511
512 void
513 get_key_val(val, str)
514 char *val, *str ;
515 {
516   *val = str[0] ;
517 }
518
519
520 void
521 get_label(n)
522 int n ;
523 {
524   int val ;
525   char *temp;
526
527   val = buttons[n].value ;
528   if (v->tstate)
529      temp = buttons[n].str2;
530   else 
531      temp = buttons[n].str;
532
533   if(temp != NULL)
534      STRCPY(v->pstr, temp) ;
535   else
536      STRCPY(v->pstr, "");
537 }
538
539
540 void
541 get_options(argc, argv)      /* Extract command line options. */
542 int argc ;
543 char *argv[] ;
544 {
545   char next[MAXLINE] ;       /* The next command line parameter. */
546   char strval[MAXLINE] ;
547   char *msg;
548   int i, len;
549
550   INC ;
551   while (argc > 0)
552     {
553       if (argv[0][0] == '-' || argv[0][0] == '+')
554         {
555           switch (argv[0][1])
556             {
557               case 'D' : v->MPdebug = TRUE ;   /* MP debug info. to stderr. */
558                          break ;
559               case 'a' : INC ;
560                          getparam(next, argv, opts[(int) O_ACCVAL]) ;
561                          v->accuracy = atoi(next) ;
562                          if (v->accuracy < 0 || v->accuracy > 9)
563                            {
564                              msg = (char *) XtMalloc(strlen(
565                                                    opts[(int) O_ACCRANGE]) + 3);
566                              sprintf(msg, opts[(int) O_ACCRANGE]);
567                              _DtSimpleError (v->appname, DtWarning, NULL, msg);
568                              XtFree(msg);
569                              v->accuracy = 2 ;
570                            }
571                          break ;
572               case 'm' : INC ;
573                          msg = (char *) XtMalloc(strlen(opts[(int) O_MODE])+
574                                                              strlen(next) + 3);
575                          sprintf(msg, opts[(int) O_MODE], next);
576                          getparam(next, argv, msg) ;
577                          XtFree(msg);
578                          STRCPY(strval, next) ;
579                          len = strlen(strval) ;
580                          for (i = 0; i < len; i++)
581                          {
582                            if (islower(strval[i])) 
583                               strval[i] = toupper(strval[i]) ;
584                          }
585                          if(strcmp(strval, "FINANCIAL") == 0)
586                              v->modetype = FINANCIAL ;
587                          else if(strcmp(strval, "LOGICAL") == 0)
588                              v->modetype = LOGICAL ;
589                          else if(strcmp(strval, "SCIENTIFIC") == 0)
590                              v->modetype = SCIENTIFIC ;
591                          else
592                            {
593                              msg = (char *) XtMalloc(strlen(opts[(int) O_MODE])+
594                                                     strlen(next) + 3);
595                              sprintf(msg, opts[(int) O_MODE], next);
596                              _DtSimpleError (v->appname, DtWarning, NULL, msg);
597                              XtFree(msg);
598                              v->modetype = SCIENTIFIC ;
599                            }
600                          break ;
601               case 'b' : INC ;
602                          getparam(next, argv, opts[(int) O_BASE]) ;
603                          STRCPY(strval, next) ;
604                          len = strlen(strval) ;
605                          for (i = 0; i < len; i++)
606                          {
607                            if (islower(strval[i]))
608                               strval[i] = toupper(strval[i]) ;
609                          }
610                          if(strncmp(strval, "BIN", 3) == 0)
611                              v->base = BIN ;
612                          else if(strncmp(strval, "OCT", 3) == 0)
613                              v->base = OCT ;
614                          else if(strncmp(strval, "DEC", 3) == 0)
615                              v->base = DEC ;
616                          else if(strncmp(strval, "HEX", 3) == 0)
617                              v->base = HEX ;
618                          else
619                            {
620                              msg = (char *) XtMalloc(strlen(
621                                                     opts[(int) O_BASE]) + 3);
622                              sprintf(msg, opts[(int) O_BASE]);
623                              _DtSimpleError (v->appname, DtWarning, NULL, msg);
624                              XtFree(msg);
625                              v->base = DEC ;
626                            }
627                          break ;
628               case 'n' : if(strcmp(&argv[0][1], "notation") == 0) 
629                          {
630                             INC ;
631                             msg = (char *) XtMalloc(strlen(
632                                                  opts[(int) O_DISPLAY]) + 
633                                                  strlen(next) + 3);
634                             sprintf(msg, opts[(int) O_DISPLAY], next);
635                             getparam(next, argv, msg) ;
636                             XtFree(msg);
637                             STRCPY(strval, next) ;
638                             len = strlen(strval) ;
639                             for (i = 0; i < len; i++)
640                             {
641                               if (islower(strval[i]))
642                                  strval[i] = toupper(strval[i]) ;
643                             }
644                             if(strncmp(strval, "FIX", 3) == 0)
645                                 v->dtype = FIX ;
646                             else if(strncmp(strval, "ENG", 3) == 0)
647                                 v->dtype = ENG ;
648                             else if(strncmp(strval, "SCI", 3) == 0)
649                                 v->dtype = SCI ;
650                             else
651                               {
652                                 msg = (char *) XtMalloc(strlen(
653                                                     opts[(int) O_DISPLAY]) + 
654                                                     strlen(next) + 3);
655                                 sprintf(msg, opts[(int) O_DISPLAY], next);
656                                 _DtSimpleError (v->appname, DtWarning, NULL, msg);
657                                 XtFree(msg);
658                                 v->dtype = FIX ;
659                               }
660                             break ;
661                          }
662                          else if(strcmp(&argv[0][1], "no_menu_bar") == 0)
663                          {
664                             INC ;
665                             application_args.menuBar = False;
666                             break ;
667                          }
668               case 't' : if(strcmp(&argv[0][1], "trig") == 0)
669                          {
670                             INC ;
671                             msg = (char *) XtMalloc(strlen(
672                                                     opts[(int) O_TRIG]) + 
673                                                     strlen(next) + 3);
674                             sprintf(msg, opts[(int) O_TRIG], next);
675                             getparam(next, argv, msg) ;
676                             XtFree(msg);
677                             STRCPY(strval, next) ;
678                             len = strlen(strval) ;
679                             for (i = 0; i < len; i++)
680                             {
681                               if (islower(strval[i]))
682                                  strval[i] = toupper(strval[i]) ;
683                             }
684                             if(strncmp(strval, "DEG", 3) == 0)
685                                 v->ttype = DEG ;
686                             else if(strncmp(strval, "RAD", 3) == 0)
687                                 v->ttype = RAD ;
688                             else if(strncmp(strval, "GRAD", 4) == 0)
689                                 v->ttype = GRAD ;
690                             else
691                               {
692                                 msg = (char *) XtMalloc(strlen(
693                                                     opts[(int) O_TRIG]) + 
694                                                     strlen(next) + 3);
695                                 sprintf(msg, opts[(int) O_TRIG], next);
696                                 _DtSimpleError (v->appname, DtWarning, NULL, msg);
697                                 XtFree(msg);
698                                 v->ttype = DEG ;
699                               }
700                             break ;
701                          }
702               case 's' : if(strcmp(&argv[0][1], "session") == 0)
703                          {
704                             INC ;
705                             getparam(next, argv, opts[(int) O_SESSION]) ;
706                             application_args.session = XtNewString(next);
707                             break ;
708                          }
709               case '?' :
710               case 'v' : usage(v->progname) ;
711                          break ;
712               default  :
713               toolarg  :             /* Pick up generic tool arguments. */
714                          usage(v->progname) ;
715             }
716           INC ;
717         }
718       else INC ;
719     }
720 }
721
722
723 void
724 getparam(s, argv, errmes)
725 char *s, *argv[], *errmes ;
726 {
727   char *msg;
728
729   if (*argv != NULL && argv[0][0] != '-') STRCPY(s, *argv) ;
730   else
731     { 
732       msg = (char *) XtMalloc(strlen(mess[(int) MESS_PARAM]) + 
733                               strlen(errmes) + 3);
734       sprintf(msg, mess[(int) MESS_PARAM], errmes);
735       _DtSimpleError (v->appname, DtError, NULL, msg);
736       FPRINTF(stderr, mess[(int) MESS_PARAM], errmes) ;
737       exit(1) ;
738     }
739 }
740
741
742 void
743 get_rcfile(name)          /* Read .dtcalcrc file. */
744 char *name ;
745 {
746   char line[MAXLINE] ;    /* Current line from the .dtcalcrc file. */
747   char tmp[MAXLINE] ;     /* Used to extract definitions. */
748   double cval ;           /* Current constant value being converted. */
749   int i ;                 /* Index to constant or function array. */
750   int isval ;             /* Set to 'c' or 'f' for convertable line. */
751   int len, n ;            
752   FILE *rcfd ;            /* File descriptor for dtcalc rc file. */
753  
754   if ((rcfd = fopen(name, "r")) == NULL) return ;
755  
756 /*  Process the .dtcalcrc file. There are currently four types of
757  *  records to look for:
758  *
759  *  1) Those starting with a hash in the first column are comments.
760  *
761  *  2) Lines starting with 'c' or 'C' in the first column are
762  *     definitions for constants. The cC is followed by a digit in
763  *     the range 0-9, then a space. This is followed by a number
764  *     in fixed or scientific notation. Following this is an optional
765  *     comment, which if found, will be used in the popup menu for
766  *     the constants. If the comment is present, there must be at
767  *     least one space between this and the preceding number.
768  *
769  *  3) Those starting with a 'f' or a 'F' in the first column are
770  *     definitions for functions. The fF is followed by a digit in
771  *     the range 0-9, then a space. This is followed by a function
772  *     definition. Following this is an optional comment, which if
773  *     found, will be used in the popup menu for the functions.
774  *     If the comment is present, there must be at least one space
775  *     between this and the preceding function definition.
776  *
777  *  4) Lines starting with a 'r' or a 'R' in the first column are
778  *     definitions for the initial contents of the calculators
779  *     memory registers. The rR is followed by a digit in the
780  *     range 0-9, then a space. This is followed by a number in
781  *     fixed or scientific notation. The rest of the line is ignored.
782  *
783  *  All other lines are ignored.
784  *
785  *  Two other things to note. There should be no embedded spaces in
786  *  the function definitions, and whenever a backslash is found, that
787  *  and the following character signify a control character, for
788  *  example \g would be ascii 7.
789  */
790
791   while (fgets(line, MAXLINE, rcfd) != NULL)
792     {
793       isval = 0 ;
794            if (line[0] == 'c' || line[0] == 'C') isval = 'c' ;
795       else if (line[0] == 'f' || line[0] == 'F') isval = 'f' ;
796       else if (line[0] == 'r' || line[0] == 'R') isval = 'r' ;
797       if (isval)
798         if (line[1] >= '0' && line[1] <= '9' && line[2] == ' ')
799           {
800             i = char_val(line[1]) ;
801             if (isval == 'c')
802               {  
803                 n = sscanf(&line[3], "%lf", &cval) ;
804                 if (n == 1) 
805                 {
806                    if(line[3] == '-')
807                    {
808                       MPstr_to_num(&line[4], DEC, v->MPcon_vals[i]) ;
809                       mpneg(v->MPcon_vals[i], v->MPcon_vals[i]) ;
810                    }
811                    else
812                       MPstr_to_num(&line[3], DEC, v->MPcon_vals[i]) ;
813                 }
814               }  
815             else if (isval == 'f')
816               {
817                 SSCANF(&line[3], "%s", tmp) ;
818                 STRCPY(v->fun_vals[i], convert(tmp)) ;
819               }  
820             else if (isval == 'r')
821               {  
822                 n = sscanf(&line[3], "%lf", &cval) ;
823                 if (n == 1) MPstr_to_num(&line[3], DEC, v->MPmvals[i]) ;
824                 continue ;
825               }  
826             len = strlen(line) ;
827             for (n = 3; n < len; n++)
828               if (line[n] == ' ' || line[n] == '\n')
829                 {
830                   while (line[n] == ' ') n++ ;
831                   line[strlen(line)-1] = '\0' ;
832                   if (isval == 'c')
833                     {
834                       STRCPY(tmp, make_number(v->MPcon_vals[i], TRUE)) ;
835                       SPRINTF(v->con_names[i], "%1d: %s [%s]",
836                               i, tmp, &line[n]) ;
837                     }
838                   else
839                     SPRINTF(v->fun_names[i], "%1d: %s [%s]",
840                             i, tmp, &line[n]) ;
841                   break ;
842                 }
843           }
844     }
845   FCLOSE(rcfd) ;
846 }
847
848
849 int
850 get_str_resource(rtype, strval)   /* Get a string resource from database. */
851 enum res_type rtype ;
852 char *strval ;
853 {
854   char *val ;
855   int i, len ;
856
857   if ((val = get_resource(rtype)) == NULL) return(0) ;
858   STRCPY(strval, val) ;
859   len = strlen(strval) ;
860   if(rtype != R_TRIG && rtype != R_DISPLAY)
861   {
862      for (i = 0; i < len; i++)
863        if (islower(strval[i])) strval[i] = toupper(strval[i]) ;
864   }
865   return(1) ;
866 }
867
868
869 void
870 grey_buttons(base)     /* Grey out numeric buttons depending upon base. */
871 enum base_type base ;
872 {
873   char val ;
874   int column, dim, i, n, row ;
875
876   for (i = 0; i < 16; i++)
877     {
878       val = digits[i] ;
879       if (isupper(val)) val = tolower(val) ;
880       for (n = 0; n < TITEMS; n++)
881         if (val == buttons[n].value) break ;
882       row = n / MAXCOLS ;
883       column = n - (row * MAXCOLS) ;
884
885       if (i < basevals[(int) base]) dim = FALSE ;
886       else                          dim = TRUE ;
887       grey_button(row, column, dim) ;
888     }
889 }
890
891
892 void
893 handle_menu_selection(n, item)   /* Process right button menu selection. */
894 int n, item ;
895 {
896   if (item != -1)
897     {
898       if (IS_KEY(v->pending, KEY_LPAR))     /* Are we inside parentheses? */
899         {
900           v->current = buttons[n].value ;
901           do_paren() ;
902           v->current = item ;
903           do_paren() ;
904         }
905       else
906         { 
907           save_pending_values(buttons[n].value) ;
908           v->current = item ;
909           v->ismenu = 1 ;       /* To prevent grey buttons being redrawn. */
910           do_pending() ;
911           v->ismenu = 0 ;
912         }
913     }
914 }
915
916
917 void
918 init_vars()    /* Setup default values for various variables. */
919 {
920   int acc, i, n, size ;
921
922   v->accuracy   = 2 ;        /* Initial accuracy. */
923   v->base       = DEC ;      /* Initial base. */
924   v->dtype      = FIX ;      /* Initial number display mode. */
925   v->ttype      = DEG ;      /* Initial trigonometric type. */
926   v->modetype   = SCIENTIFIC;    /* Initial calculator mode. */
927   v->rstate     = 0 ;        /* No memory register frame display initially. */
928   v->frstate    = 0 ;        /* No fin. memory register frame display 
929                                 initially. */
930   v->iconic     = FALSE ;    /* Calctool not iconic by default. */
931   v->MPdebug    = FALSE ;    /* No debug info by default. */
932   v->MPerrors   = TRUE ;               /* No error information. */
933   acc           = MAX_DIGITS + 12 ;     /* MP internal accuracy. */
934   size          = MP_SIZE ;
935   mpset(&acc, &size, &size) ;
936
937   v->hasicon     = FALSE ;        /* Use standard dtcalc icon by default. */
938   v->beep        = TRUE ;         /* Beep on error by default. */
939   v->error       = 0 ;            /* No calculator error initially. */
940   v->key_exp     = 0 ;            /* Not entering an exponent number. */
941   v->pending_op  = 0 ;            /* No pending arithmetic operation. */
942   v->titleline   = NULL ;         /* No User supplied title line. */
943   v->display[0]  = 0 ;         
944
945   v->x  = 0;
946   v->x  = 0;
947   v->width  = 0;
948   v->height = 0; 
949
950   v->workspaces = NULL; 
951
952   read_str(&v->iconlabel, lstrs[(int) L_LCALC]) ;  /* Default icon label. */
953
954   MPstr_to_num("0.621", DEC, v->MPcon_vals[0]) ;  /* kms/hr <=> miles/hr. */
955   MPstr_to_num("1.4142135623", DEC, v->MPcon_vals[1]) ;  /* square root of 2 */
956   MPstr_to_num("2.7182818284", DEC, v->MPcon_vals[2]) ;  /* e */
957   MPstr_to_num("3.1415926535", DEC, v->MPcon_vals[3]) ;  /* pi */
958   MPstr_to_num("2.54",         DEC, v->MPcon_vals[4]) ;  /* cms <=> inch. */
959   MPstr_to_num("57.295779513", DEC, v->MPcon_vals[5]) ;  /* degrees/radian. */
960   MPstr_to_num("1048576.0",    DEC, v->MPcon_vals[6]) ;  /* 2 ^ 20. */
961   MPstr_to_num("0.0353", DEC, v->MPcon_vals[7]) ;  /* grams <=> ounce. */
962   MPstr_to_num("0.948",  DEC, v->MPcon_vals[8]) ;  /* Kjoules <=> BTU's. */
963   MPstr_to_num("0.0610", DEC, v->MPcon_vals[9]) ;  /* cms3 <=> inches3. */
964
965   n = 0 ;
966   for (i = 0; i < MAXREGS; i++) mpcim(&n, v->MPmvals[i]) ;
967   for (i = 0; i < FINREGS; i++)  
968      v->MPfvals[i] = (double)n;
969   v->MPfvals[FINREGS - 1] = (double)12;
970 }
971
972
973 void
974 initialise()
975 {
976   int i ;
977
978   v->error         = 0 ;           /* Currently no display error. */
979   v->cur_op        = '?' ;         /* No arithmetic operator defined yet. */
980   v->old_cal_value = '?' ;
981   i = 0 ;
982   mpcim(&i, v->MPresult) ;         /* No previous result yet. */
983   mpcim(&i, v->MPlast_input) ;
984 }
985
986
987 char *
988 make_eng_sci(MPnumber)      /* Convert engineering or scientific number. */
989 int *MPnumber ;
990 {
991   char fixed[MAX_DIGITS+1], *optr ;
992   int MP1[MP_SIZE], MPatmp[MP_SIZE], MPval[MP_SIZE] ;
993   int MP1base[MP_SIZE], MP3base[MP_SIZE], MP10base[MP_SIZE] ;
994   int i, dval, len, n ;
995   int MPmant[MP_SIZE] ;        /* Mantissa. */
996   int ddig ;                   /* Number of digits in exponent. */
997   int eng = 0 ;                /* Set if this is an engineering number. */
998   int exp = 0 ;                /* Exponent */
999  
1000   if (v->dtype == ENG) eng = 1 ;
1001   optr = v->snum ;
1002   mpabs(MPnumber, MPval) ;
1003   n = 0 ;
1004   mpcim(&n, MP1) ;
1005   if (mplt(MPnumber, MP1)) *optr++ = '-' ;
1006   mpstr(MPval, MPmant) ;
1007  
1008   mpcim(&basevals[(int) v->base], MP1base) ;
1009   n = 3 ;
1010   mppwr(MP1base, &n, MP3base) ;
1011  
1012   n = 10 ;
1013   mppwr(MP1base, &n, MP10base) ;
1014  
1015   n = 1 ;
1016   mpcim(&n, MP1) ;
1017   mpdiv(MP1, MP10base, MPatmp) ;
1018  
1019   n = 0 ;
1020   mpcim(&n, MP1) ;
1021   if (!mpeq(MPmant, MP1))
1022     {
1023       while (mpge(MPmant, MP10base))
1024         {
1025           exp += 10 ;
1026           mpmul(MPmant, MPatmp, MPmant) ;
1027         }
1028
1029       while ((!eng &&  mpge(MPmant, MP1base)) ||
1030               (eng && (mpge(MPmant, MP3base) || exp % 3 != 0)))
1031         {
1032           exp += 1 ;
1033           mpdiv(MPmant, MP1base, MPmant) ;
1034         }
1035
1036       while (mplt(MPmant, MPatmp))
1037         {
1038           exp -= 10 ;
1039           mpmul(MPmant, MP10base, MPmant) ;
1040         }
1041
1042       n = 1 ;
1043       mpcim(&n, MP1) ;
1044       while (mplt(MPmant, MP1) || (eng && exp % 3 != 0))
1045         {
1046           exp -= 1 ;
1047           mpmul(MPmant, MP1base, MPmant) ;
1048         }
1049     }    
1050
1051   STRCPY(fixed, make_fixed(MPmant, MAX_DIGITS-6)) ;
1052   len = strlen(fixed) ;
1053   for (i = 0; i < len; i++) *optr++ = fixed[i] ;
1054
1055   *optr++ = 'e' ;
1056
1057   if (exp < 0)
1058     {
1059       exp = -exp ;
1060       *optr++ = '-' ;
1061     }
1062   else *optr++ = '+' ;
1063
1064   MPstr_to_num("0.5", DEC, MP1) ;
1065   mpaddi(MP1, &exp, MPval) ;
1066   n = 1 ;
1067   mpcim(&n, MP1) ;
1068   for (ddig = 0; mpge(MPval, MP1); ddig++)
1069     mpdiv(MPval, MP1base, MPval) ;
1070
1071   if (ddig == 0) *optr++ = '0' ;
1072
1073   while (ddig-- > 0)
1074     {
1075       mpmul(MPval, MP1base, MPval) ;
1076       mpcmi(MPval, &dval) ;
1077       *optr++ = digits[dval] ;
1078       dval = -dval ;
1079       mpaddi(MPval, &dval, MPval) ;
1080     }
1081   *optr++    = '\0' ;
1082   v->toclear = 1 ;
1083   v->pointed = 0 ;
1084   return(v->snum) ;
1085 }
1086
1087
1088 char *
1089 make_fixed(MPnumber, cmax)     /* Convert MP number to fixed number string. */
1090 int *MPnumber ;
1091 int cmax ;                     /* Maximum characters to generate. */
1092 {
1093   char *optr ;
1094   int MP1base[MP_SIZE], MP1[MP_SIZE], MP2[MP_SIZE], MPval[MP_SIZE] ;
1095   int ndig ;                   /* Total number of digits to generate. */
1096   int ddig ;                   /* Number of digits to left of . */
1097   int dval, n ;
1098  
1099   optr = v->fnum ;
1100   mpabs(MPnumber, MPval) ;
1101   n = 0 ;
1102   mpcim(&n, MP1) ;
1103   if (mplt(MPnumber, MP1)) *optr++ = '-' ;
1104  
1105   mpcim(&basevals[(int) v->base], MP1base) ;
1106  
1107   mppwr(MP1base, &v->accuracy, MP1) ;
1108   MPstr_to_num("0.5", DEC, MP2) ;
1109   mpdiv(MP2, MP1, MP1) ;
1110   mpadd(MPval, MP1, MPval) ;
1111  
1112   n = 1 ;
1113   mpcim(&n, MP2) ;
1114   if (mplt(MPval, MP2))
1115     {
1116       ddig = 0 ;
1117       *optr++ = '0' ;
1118       cmax-- ;
1119     }
1120   else
1121     for (ddig = 0; mpge(MPval, MP2); ddig++)
1122       mpdiv(MPval, MP1base, MPval) ;
1123
1124   ndig = MIN(ddig + v->accuracy, --cmax) ;
1125
1126   while (ndig-- > 0)
1127     {
1128       if (ddig-- == 0) *optr++ = '.' ;
1129       mpmul(MPval, MP1base, MPval) ;
1130       mpcmi(MPval, &dval) ;
1131       *optr++ = digits[dval] ;
1132       dval = -dval ;
1133       mpaddi(MPval, &dval, MPval) ;
1134     }
1135   *optr++    = '\0' ;
1136   v->toclear = 1 ;
1137   v->pointed = 0 ;
1138   return(v->fnum) ;
1139 }
1140
1141
1142 void
1143 make_items()
1144 {
1145   set_item(DISPLAYITEM, v->display) ;
1146   set_item(OPITEM,      "    ") ;
1147   set_item(HYPITEM,     "    ") ;
1148   set_item(INVITEM,     "    ") ;
1149 }
1150
1151
1152 char *
1153 make_number(MPnumber, mkFix)     /* Convert MP number to character string. */
1154 int *MPnumber ;
1155 BOOLEAN mkFix ;
1156 {
1157   double number, val ;
1158
1159 /*  NOTE: make_number can currently set v->error when converting to a double.
1160  *        This is to provide the same look&feel as V3 even though dtcalc
1161  *        now does internal arithmetic to "infinite" precision.
1162  *
1163  *  XXX:  Needs to be improved. Shouldn't need to convert to a double in
1164  *        order to do these tests.
1165  */  
1166      
1167   mpcmd(MPnumber, &number) ;
1168   val = fabs(number) ;
1169   if (v->error) return(vstrs[(int) V_ERROR]) ;
1170   if (v->dtype == ENG || v->dtype == SCI ||
1171        v->dtype == FIX && val != 0.0 && (val > max_fix[(int) v->base]))
1172      return(make_eng_sci(MPnumber)) ;
1173   else if (v->dtype == FIX && val != 0.0 && mkFix)
1174   {
1175      if(v->accuracy == 0)
1176      {
1177         if(val <= min_fix0[(int) v->base])
1178            return(make_eng_sci(MPnumber)) ;
1179         else
1180            return(make_fixed(MPnumber, MAX_DIGITS)) ;
1181      }
1182      else if(v->accuracy == 1)
1183      {
1184         if(val <= min_fix1[(int) v->base])
1185            return(make_eng_sci(MPnumber)) ;
1186         else
1187            return(make_fixed(MPnumber, MAX_DIGITS)) ;
1188      }
1189      else if(v->accuracy == 2)
1190      {
1191         if(val <= min_fix2[(int) v->base])
1192            return(make_eng_sci(MPnumber)) ;
1193         else
1194            return(make_fixed(MPnumber, MAX_DIGITS)) ;
1195      }
1196      else if(v->accuracy == 3)
1197      {
1198         if(val <= min_fix3[(int) v->base])
1199            return(make_eng_sci(MPnumber)) ;
1200         else
1201            return(make_fixed(MPnumber, MAX_DIGITS)) ;
1202      }
1203      else if(v->accuracy == 4)
1204      {
1205         if(val <= min_fix4[(int) v->base])
1206            return(make_eng_sci(MPnumber)) ;
1207         else
1208            return(make_fixed(MPnumber, MAX_DIGITS)) ;
1209      }
1210      else if(v->accuracy == 5)
1211      {
1212         if(val <= min_fix5[(int) v->base])
1213            return(make_eng_sci(MPnumber)) ;
1214         else
1215            return(make_fixed(MPnumber, MAX_DIGITS)) ;
1216      }
1217      else if(v->accuracy == 6)
1218      {
1219         if(val <= min_fix6[(int) v->base])
1220            return(make_eng_sci(MPnumber)) ;
1221         else
1222            return(make_fixed(MPnumber, MAX_DIGITS)) ;
1223      }
1224      else if(v->accuracy == 7)
1225      {
1226         if(val <= min_fix7[(int) v->base])
1227            return(make_eng_sci(MPnumber)) ;
1228         else
1229            return(make_fixed(MPnumber, MAX_DIGITS)) ;
1230      }
1231      else if(v->accuracy == 8)
1232      {
1233         if(val <= min_fix8[(int) v->base])
1234            return(make_eng_sci(MPnumber)) ;
1235         else
1236            return(make_fixed(MPnumber, MAX_DIGITS)) ;
1237      }
1238      else if(v->accuracy == 9)
1239      {
1240         if(val <= min_fix9[(int) v->base])
1241            return(make_eng_sci(MPnumber)) ;
1242         else
1243            return(make_fixed(MPnumber, MAX_DIGITS)) ;
1244      }
1245   }
1246   else 
1247      return(make_fixed(MPnumber, MAX_DIGITS)) ;
1248 }
1249
1250
1251 /*ARGSUSED*/
1252 int
1253 matherr(exc)        /* Default math library exception handling routine. */
1254 struct exception *exc ;
1255 {
1256   char msg[100];
1257   
1258 #if 0
1259   if (exc) {
1260           strcpy(msg, exc->name);
1261           strcat(msg, ": ");
1262           if(exc->type == DOMAIN)
1263              strcat(msg, "DOMAIN ");
1264           else if(exc->type == SING)
1265              strcat(msg, "SING ");
1266           else if(exc->type == OVERFLOW)
1267              strcat(msg, "OVERFLOW ");
1268           else if(exc->type == UNDERFLOW)
1269              strcat(msg, "UNDERFLOW ");
1270           else if(exc->type == TLOSS)
1271              strcat(msg, "TLOSS ");
1272           else if(exc->type == PLOSS)
1273              strcat(msg, "PLOSS ");
1274           
1275           strcat(msg, vstrs[(int) V_ERROR]);
1276
1277           _DtSimpleError (v->appname, DtWarning, NULL, msg);
1278   }
1279 #endif
1280
1281   doerr(vstrs[(int) V_ERROR]) ;
1282   return(1) ;                     /* Value ignored. */
1283 }
1284
1285
1286 void
1287 MPstr_to_num(str, base, MPval)    /* Convert string into an MP number. */
1288 char *str ;
1289 enum base_type base ;
1290 int *MPval ;
1291 {
1292   char   *optr ;
1293   int MP1[MP_SIZE], MP2[MP_SIZE], MPbase[MP_SIZE] ;
1294   int    i, inum ;
1295   int    neg      = 0 ;
1296   int    exp      = 0 ;
1297   int    exp_sign = 1 ;
1298
1299   i = 0 ;
1300   mpcim(&i, MPval) ;
1301   mpcim(&basevals[(int) base], MPbase) ;
1302   optr = str ;
1303   while (*optr == ' ') optr++ ;
1304   if(*optr == '-')
1305   {
1306      /* negative number */
1307      optr++ ;
1308      neg = 1;
1309   }
1310   while ((inum = char_val(*optr)) >= 0)
1311     {
1312       mpmul(MPval, MPbase, MPval) ;
1313       mpaddi(MPval, &inum, MPval) ;
1314       optr++ ;
1315     }
1316
1317   if (*optr == '.')
1318     for (i = 1; (inum = char_val(*++optr)) >= 0; i++)
1319       {  
1320         mppwr(MPbase, &i, MP1) ;
1321         mpcim(&inum, MP2) ;
1322         mpdiv(MP2, MP1, MP1) ;
1323         mpadd(MPval, MP1, MPval) ;
1324       }
1325  
1326   while (*optr == ' ') optr++ ;
1327
1328   if (*optr != '\0')
1329     {
1330       if (*optr == '-') exp_sign = -1 ;
1331
1332       while ((inum = char_val(*++optr)) >= 0)
1333         exp = exp * basevals[(int) base] + inum ;
1334     }
1335   exp *= exp_sign ;
1336
1337   if (v->key_exp || exp_sign == -1) 
1338     {
1339       mppwr(MPbase, &exp, MP1) ;
1340       mpmul(MPval, MP1, MPval) ;
1341     }
1342   if( neg == 1)
1343   {
1344       i = -1 ;
1345       mpcim(&i, MP1) ;
1346       mpmul(MPval, MP1, MPval) ;
1347   }
1348 }
1349
1350
1351 void
1352 paren_disp(c)   /* Append the latest parenthesis char to the display item. */
1353 char c ;
1354 {
1355   int i, n ;
1356
1357 /*  If the character is a Delete, clear the whole line, and exit parenthesis
1358  *  processing.
1359  *
1360  *  If the character is a Back Space, remove the last character. If the last
1361  *  character was a left parenthesis, decrement the parentheses count. If
1362  *  the parentheses count is zero, exit parenthesis processing.
1363  *
1364  *  If the character is a control character (not Ctrl-h), then append ^(char).
1365  *
1366  *  Otherwise just append the character.
1367  */
1368  
1369   n = strlen(v->display) ;
1370   if (IS_KEY(c, KEY_CLR))             /* Is it a Delete character? */
1371     {
1372       v->noparens = v->pending = v->opsptr = v->numsptr = 0 ;
1373       v->cur_op = '?' ;
1374       set_item(OPITEM, "") ;
1375       i = 0 ;
1376       mpcim(&i, v->MPdisp_val) ;
1377       show_display(v->MPdisp_val) ;
1378       set_base(v->base);
1379       set_numtype(v->dtype);
1380       set_option_menu((int) TTYPEITEM, (int)v->ttype);
1381       make_modewin() ;
1382       v->curwin = FCP_KEY ;
1383       v->defState = 1;
1384       return ;
1385     }
1386   else if (IS_KEY(c, KEY_BSP))        /* Is is a Back Space character? */
1387     {
1388       if (!n) return ;
1389       if (v->display[n-1] == '(')
1390         {
1391           v->noparens-- ;
1392           if (!v->noparens)
1393             {
1394               v->pending = v->opsptr = v->numsptr = 0 ;
1395               v->cur_op = '?' ;
1396               set_item(OPITEM, "") ;
1397               show_display(v->MPdisp_val) ;
1398               return ;
1399             }
1400         }
1401       v->display[n-1] = '\0' ;
1402     }
1403   else if (c <= CTL('z'))             /* Is it a control character? */
1404     {
1405       if (n < MAXLINE-2)
1406         {
1407           v->display[n]   = '^' ;
1408           v->display[n+1] = c + 96 ;
1409           v->display[n+2] = '\0' ;
1410         }
1411     }    
1412   else                                /* It must be an ordinary character. */
1413     {
1414       if (n < MAXLINE-1)
1415         {
1416           v->display[n]   = c ;
1417           v->display[n+1] = '\0' ;
1418         }
1419     }    
1420
1421   n = (n < MAX_DIGITS) ? 0 : n - MAX_DIGITS ;
1422   v->show_paren = 1 ;       /* Hack to get set_item to really display it. */
1423   set_item(DISPLAYITEM, &v->display[n]) ;
1424   v->show_paren = 0 ;
1425 }
1426
1427
1428 void
1429 process_event(type)       /* Process this event. */
1430 int type ;
1431 {
1432   int ival ;
1433
1434   switch (type)
1435     {
1436       case KEYBOARD_DOWN   : if (v->pending)
1437                                {
1438                                  v->current = v->cur_ch ;
1439                                  do_pending() ;
1440                                }
1441                              else
1442                                { 
1443                                  ival = get_index(v->cur_ch) ;
1444                                  if (ival < TITEMS - EXTRA) {
1445                                     if ((v->modetype == FINANCIAL && 
1446                                                                  ival == 23) ||
1447                                         (v->modetype == LOGICAL && (
1448                                                     ival == 21 || ival == 22 || 
1449                                                                  ival == 23)))
1450                                        break;
1451                                     else
1452                                        /* go arm the button */
1453                                        draw_button(ival, v->curwin, v->row, 
1454                                                    v->column, TRUE) ;
1455                                  }
1456                                  process_item(ival) ;
1457                                }
1458                              break ;
1459
1460       case KEYBOARD_UP     : ival = get_index(v->cur_ch) ;
1461                              if (ival < TITEMS - EXTRA) {
1462                                  if ((v->modetype == FINANCIAL && 
1463                                                               ival == 23) ||
1464                                      (v->modetype == LOGICAL && (
1465                                                  ival == 21 || ival == 22 ||
1466                                                               ival == 23)))
1467                                     break;
1468                                  else
1469                                     /* go disarm the button */
1470                                     draw_button(ival, v->curwin, v->row, 
1471                                                 v->column, FALSE) ;
1472                              }
1473                              /* go disarm the button */
1474                              draw_button(ival, v->curwin, v->row, 
1475                                                 v->column, FALSE) ;
1476                              break ;
1477  
1478     }
1479 }
1480
1481
1482 void
1483 process_item(n)
1484 int n ;
1485 {
1486   int i,isvalid ;
1487
1488   if (n < 0 || n >= TITEMS)
1489     {
1490       beep() ;
1491       v->error = TRUE;
1492     }
1493
1494   v->current = buttons[n].value ;
1495   if (v->current == '*') v->current = 'x' ;      /* Reassign "extra" values. */
1496   if (v->current == '\015') v->current = '=' ;
1497   if (v->current == 'Q') v->current = 'q' ;
1498
1499   if (v->error)
1500     {
1501       isvalid = 0 ;                    /* Must press a valid key first. */
1502       for (i = 0; i < MAXVKEYS; i++)
1503         if (v->current == validkeys[i]) isvalid = 1 ;
1504       if (v->pending == '?') isvalid = 1 ;
1505       if (!isvalid) return ;
1506       v->error = 0 ;
1507     }
1508
1509   if (v->pending)
1510     {
1511       if (v->pending_win == FCP_KEY) (*buttons[v->pending_n].func)() ;
1512       else (*mode_buttons[MODEKEYS * ((int) v->pending_mode - 1) +
1513             v->pending_n].func)() ;
1514       return ;
1515     }
1516   switch (buttons[n].opdisp)
1517     {
1518       case OP_SET   : 
1519          if (v->current == 'T') 
1520          {
1521              if (v->modetype == SCIENTIFIC)
1522                 set_item(OPITEM, buttons[n].str) ;
1523              else
1524                 v->pending = 0;
1525          }
1526          else
1527             set_item(OPITEM, buttons[n].str) ;
1528          break ;
1529       case OP_CLEAR : 
1530          if (v->error) 
1531             set_item(OPITEM, vstrs[(int) V_CLR]) ;
1532          else 
1533             set_item(OPITEM, "") ;
1534     }
1535   (*buttons[n].func)() ;
1536
1537   if(strcmp(buttons[n].resname, "clr") != 0)
1538       v->defState = 0;
1539 }
1540
1541
1542 /* Process a portion of the parentheses stack. */
1543
1544 void
1545 process_stack(startop, startnum, n)
1546 int startop ;         /* Initial position in the operand stack. */
1547 int startnum ;        /* Initial position in the numeric stack. */
1548 int n ;               /* Number of items to process. */
1549 {
1550   char sdisp[MAXLINE] ;     /* Used to save display contents. */
1551   int i ;
1552   int nptr ;                /* Pointer to next number from numeric stack. */
1553
1554   STRCPY(sdisp, v->display) ;  /* Save current display. */
1555   nptr = startnum ;
1556   v->pending = 0 ;
1557   v->cur_op = '?' ;            /* Current operation is initially undefined. */
1558   for (i = 0; i < n; i++)
1559     {
1560       if (v->opstack[startop + i] == -1)
1561         {
1562           mpstr(v->MPnumstack[nptr++], v->MPdisp_val) ;
1563         }
1564       else
1565         { 
1566           v->cur_ch = v->opstack[startop + i] ;
1567           if (v->cur_ch == '^')                    /* Control character? */
1568             {
1569               i++ ;
1570               v->cur_ch = CTL(v->opstack[startop + i]) ;
1571             }
1572           if (v->pending)
1573             {
1574               v->current = v->cur_ch ;
1575               do_pending() ;
1576             }
1577           else process_item(get_index(v->cur_ch)) ;
1578         }
1579     }    
1580   v->numsptr = startnum ;
1581   push_num(v->MPdisp_val) ;
1582   v->opsptr = startop - 1 ;
1583   push_op(-1) ;
1584   save_pending_values(KEY_LPAR) ;
1585   STRCPY(v->display, sdisp) ;  /* Restore current display. */
1586 }
1587
1588
1589 void
1590 process_str(str, mtype)
1591 char *str ;
1592 enum menu_type mtype ;
1593 {
1594   int i, len ;
1595   char save[80];
1596
1597   len = strlen(str) ;
1598   STRCPY(save, v->display) ;
1599   STRCPY(v->display, " ") ;
1600   set_item(DISPLAYITEM, v->display);
1601   for (i = 0 ; i < len; i++)
1602     {    
1603       if(str[i] == '*')
1604          str[i] = 'x';
1605       if (v->error) 
1606       {
1607         if(mtype == M_FUN)
1608         {
1609            STRCPY(v->display, save);
1610            set_item(DISPLAYITEM, v->display);
1611            v->error = 0;
1612         }
1613         return ;
1614       }
1615       if (v->pending)
1616         {
1617           v->current = str[i] ;
1618           do_pending() ;
1619         }
1620       else 
1621         switch(v->base)
1622         {
1623            case DEC:
1624               if(str[i] == 'a' ||
1625                  str[i] == 'b' ||
1626                  str[i] == 'c' ||
1627                  str[i] == 'd' ||
1628                  str[i] == 'e' ||
1629                  str[i] == 'f')
1630               {
1631                  v->error = True;
1632                  beep();
1633                  break;
1634               }
1635            default:
1636               process_item(get_index(str[i])) ;
1637         }
1638     }
1639 }
1640
1641
1642 void
1643 read_rcfiles()   /* Read .dtcalcrc's from home and current directories. */
1644 {
1645   char *home ;                  /* Pathname for users home directory. */
1646   char name[MAXPATHLEN + 50] ;          /* Full name of users .dtcalcrc file. */
1647   char pathname[MAXPATHLEN + 5] ;   /* Current working directory. */
1648   char tmp[MAXLINE] ;           /* For temporary constant string creation. */
1649   int n ;
1650   struct passwd *entry ;
1651
1652   for (n = 0; n < MAXREGS; n++)
1653     {
1654       STRCPY(tmp, make_number(v->MPcon_vals[n], FALSE)) ;
1655       SPRINTF(name, "%1d: %s [%s]", n, tmp, v->con_names[n]) ;
1656
1657       STRCPY(v->con_names[n], name) ;
1658       STRCPY(v->fun_vals[n], "") ;    /* Initially empty function strings. */
1659     }
1660
1661   if ((home = getenv("HOME")) == NULL)
1662     {
1663       if ((entry = getpwuid(getuid())) == NULL) return ;
1664       home = entry->pw_dir ;
1665     }
1666   SPRINTF(name, "%s/%s", home, RCNAME) ;
1667   get_rcfile(name) ;      /* Read .dtcalcrc from users home directory. */
1668  
1669   SPRINTF(name, "%s/%s", getcwd(pathname, MAXPATHLEN+1), RCNAME) ;
1670   get_rcfile(name) ;      /* Read .dtcalcrc file from current directory. */
1671 }
1672
1673
1674 void
1675 show_display(MPval)
1676 int *MPval ;
1677 {
1678   if (!v->error)
1679     {
1680       STRCPY(v->display, make_number(MPval, TRUE)) ;
1681       set_item(DISPLAYITEM, v->display) ;
1682     }
1683 }
1684
1685
1686 void
1687 usage(progname)
1688 char *progname ;
1689 {
1690   FPRINTF(stderr, ustrs[(int) USAGE1], PATCHLEVEL) ;
1691   FPRINTF(stderr, ustrs[(int) USAGE2]) ;
1692   FPRINTF(stderr, ustrs[(int) USAGE3]) ;
1693   exit(1) ;
1694 }
1695
1696 void
1697 write_rcfile(mtype, exists, cfno, val, comment)
1698 enum menu_type mtype ;
1699 int exists, cfno ;
1700 char *val, *comment ;
1701 {
1702   char *home ;                  /* Pathname for users home directory. */
1703   char pathname[MAXPATHLEN] ;   /* Current working directory. */
1704   char rcname[MAXPATHLEN] ;     /* Full name of users .dtcalcrc file. */
1705   char str[MAXLINE] ;           /* Temporary buffer. */
1706   char sval[3] ;                /* Used for string comparisons. */
1707   char tmp_filename[MAXLINE] ;  /* Used to construct temp filename. */
1708   int rcexists ;                /* Set to 1, if .dtcalcrc file exists. */
1709   FILE *rcfd ;                  /* File descriptor for .dtcalcrc file. */
1710   FILE *tmpfd ;                 /* File descriptor for new temp .dtcalcrc. */
1711   struct passwd *entry ;        /* The user's /etc/passwd entry. */
1712
1713   rcexists = 0 ;
1714   SPRINTF(rcname, "%s/%s", getcwd(pathname, MAXPATHLEN), RCNAME) ;
1715   if (access(rcname, F_OK) == 0) rcexists = 1 ;
1716   else
1717     { 
1718       if ((home = getenv("HOME")) == NULL)
1719         {
1720           if ((entry = getpwuid(getuid())) == NULL) return ;
1721           home = entry->pw_dir ;
1722         }
1723       SPRINTF(rcname, "%s/%s", home, RCNAME) ;
1724       if (access(rcname, F_OK) == 0) rcexists = 1 ;
1725     }
1726   STRCPY(tmp_filename, "/tmp/.dtcalcrcXXXXXX") ;
1727   MKTEMP(tmp_filename) ;
1728   if ((tmpfd = fopen(tmp_filename, "w+")) == NULL) return ;
1729
1730   if (rcexists)
1731     {
1732       rcfd = fopen(rcname, "r") ;
1733       SPRINTF(sval, " %1d", cfno) ;
1734       while (fgets(str, MAXLINE, rcfd))
1735         {
1736           if (exists)
1737             {
1738               switch (mtype)
1739                 {
1740                   case M_CON : sval[0] = 'c' ;
1741                                if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
1742                                sval[0] = 'C' ;
1743                                if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
1744                                break ;
1745                   case M_FUN : sval[0] = 'f' ;
1746                                if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
1747                                sval[0] = 'F' ;
1748                                if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
1749                 }
1750             }    
1751           FPRINTF(tmpfd, "%s", str) ;
1752         }
1753       FCLOSE(rcfd) ;
1754     }
1755
1756   switch (mtype)
1757     {
1758       case M_CON : FPRINTF(tmpfd, "\nC%1d %s %s\n", cfno, val, comment) ;
1759                    break ;
1760       case M_FUN : 
1761                if(strcmp(val, "") != 0)
1762                   FPRINTF(tmpfd, "\nF%1d %s %s\n", cfno, val, comment) ;
1763     }
1764   UNLINK(rcname) ;
1765   rcfd = fopen(rcname, "w") ;
1766   REWIND(tmpfd) ;
1767   while (fgets(str, MAXLINE, tmpfd)) FPRINTF(rcfd, "%s", str) ;
1768   FCLOSE(rcfd) ;
1769   FCLOSE(tmpfd);
1770   UNLINK(tmp_filename) ;
1771 }
1772
1773
1774 void
1775 write_resources(filename)
1776 char *filename;
1777 {
1778   char regval[5] ;
1779   char intval[5] ;
1780   int i;
1781   int MPtemp[MP_SIZE];
1782
1783   SPRINTF(intval, "%d", v->accuracy) ;
1784   put_resource(R_ACCURACY, intval) ;
1785   put_resource(R_DISPLAYED, v->display) ;
1786   put_resource(R_BASE,     base_str[(int) v->base]) ;
1787   put_resource(R_DISPLAY,  dtype_str[(int) v->dtype]) ;
1788   put_resource(R_MODE,     mode_str[(int) v->modetype]) ;
1789   put_resource(R_TRIG,     ttype_str[(int) v->ttype]) ;
1790   put_resource(R_REGS,     set_bool(v->rstate     == TRUE)) ;
1791
1792   put_resource(R_REG0,     make_number(v->MPmvals[0], FALSE)) ;
1793   put_resource(R_REG1,     make_number(v->MPmvals[1], FALSE)) ;
1794   put_resource(R_REG2,     make_number(v->MPmvals[2], FALSE)) ;
1795   put_resource(R_REG3,     make_number(v->MPmvals[3], FALSE)) ;
1796   put_resource(R_REG4,     make_number(v->MPmvals[4], FALSE)) ;
1797   put_resource(R_REG5,     make_number(v->MPmvals[5], FALSE)) ;
1798   put_resource(R_REG6,     make_number(v->MPmvals[6], FALSE)) ;
1799   put_resource(R_REG7,     make_number(v->MPmvals[7], FALSE)) ;
1800   put_resource(R_REG8,     make_number(v->MPmvals[8], FALSE)) ;
1801   put_resource(R_REG9,     make_number(v->MPmvals[9], FALSE)) ;
1802
1803   mpcdm(&(v->MPfvals[0]), MPtemp);
1804   put_resource(R_FREG0,     make_number(MPtemp, FALSE)) ;
1805   mpcdm(&(v->MPfvals[1]), MPtemp);
1806   put_resource(R_FREG1,     make_number(MPtemp, FALSE)) ;
1807   mpcdm(&(v->MPfvals[2]), MPtemp);
1808   put_resource(R_FREG2,     make_number(MPtemp, FALSE)) ;
1809   mpcdm(&(v->MPfvals[3]), MPtemp);
1810   put_resource(R_FREG3,     make_number(MPtemp, FALSE)) ;
1811   mpcdm(&(v->MPfvals[4]), MPtemp);
1812   put_resource(R_FREG4,     make_number(MPtemp, FALSE)) ;
1813   mpcdm(&(v->MPfvals[5]), MPtemp);
1814   put_resource(R_FREG5,     make_number(MPtemp, FALSE)) ;
1815
1816   save_resources(filename) ;
1817 }