2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $XConsortium: calctool.c /main/9 1996/09/25 11:28:16 rswiston $ */
26 * Contains the none user interface portion of the Desktop *
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. *
40 #include <sys/types.h>
42 #include <sys/param.h>
45 #include "patchlevel.h"
47 #include "ds_common.h"
51 /* Copied from math.h */
65 6.871947674e+10, 3.245185537e+32,
66 1.000000000e+36, 2.230074520e+43
68 double min_fix0[4] = {
69 2.500000000e-1, 3.750000000e-1,
70 1.000000000e-1, 4.375000000e-1
72 double min_fix1[4] = {
73 1.250000000e-1, 4.687500000e-2,
74 1.000000000e-2, 2.734375000e-2
76 double min_fix2[4] = {
77 6.250000000e-2, 5.859375000e-3,
78 1.000000000e-3, 1.708984375e-3
80 double min_fix3[4] = {
81 3.125000000e-2, 7.324218750e-4,
82 1.000000000e-4, 1.068115234e-4
84 double min_fix4[4] = {
85 1.562500000e-2, 9.155273437e-5,
86 1.000000000e-5, 6.675720215e-6
88 double min_fix5[4] = {
89 7.812500000e-3, 1.144409180e-5,
90 1.000000000e-6, 4.172325134e-7
92 double min_fix6[4] = {
93 6.906250000e-3, 1.430511475e-6,
94 1.000000000e-7, 2.607703209e-8
96 double min_fix7[4] = {
97 1.953125000e-3, 1.788139343e-7,
98 1.000000000e-8, 1.629814506e-9
100 double min_fix8[4] = {
101 9.765625000e-4, 2.235174179e-8,
102 1.000000000e-9, 1.018634066e-10
104 double min_fix9[4] = {
105 4.882812500e-4, 2.793967724e-9,
106 1.000000000e-10, 6.366462912e-12
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. */
120 char digits[] = "0123456789ABCDEF" ;
121 int basevals[4] = { 2, 8, 10, 16 } ;
123 int left_pos[BCOLS] = { 3, 2, 1, 0 } ; /* Left positions. */
124 int right_pos[BCOLS] = { 0, 1, 2, 3 } ; /* "Right" positions. */
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' } ;
130 Vars v ; /* Calctool variables and options. */
132 struct menu_entry menu_entries[MAXENTRIES] ;
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 */
148 /* This table shows the keyboard values that are currently being used:
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 *----------------------------------------------------------------
160 /* Calculator button values. */
162 struct button buttons[TITEMS] = {
163 /* str str2 value opdisp menutype resname func */
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
255 struct button mode_buttons[MAXMODES * MODEKEYS] = {
256 /* str str2 value opdisp menutype resname func */
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 },
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 },
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 },
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) ;
334 mpcim(&i, v->MPdisp_val) ;
335 STRCPY(v->display, make_number(v->MPdisp_val, FALSE)) ;
336 set_item(DISPLAYITEM, v->display) ;
341 v->opsptr = 0 ; /* Clear parentheses stacks. */
343 set_item(HYPITEM, " ") ;
344 set_item(INVITEM, " ") ;
349 convert(line) /* Convert .dtcalcrc line to ascii values. */
350 char *line ; /* Input line to be converted. */
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. */
356 int n = 0 ; /* Position within output line. */
359 for (i = 0; i < len; i++)
361 if (line[i] == ' ') continue ;
362 else if (line[i] == '\\') ctrl = 1 ;
365 output[n++] = CTL(line[i]) ;
368 else output[n++] = line[i] ;
376 do_dtcalc(argc, argv)
383 v->progname = argv[0] ; /* Save programs name. */
385 init_cmdline_opts() ; /* Initialise command line option strings. */
387 if ((ptr = strrchr(argv[0], '/')) != NULL)
388 read_str(&v->appname, ptr+1) ;
389 else read_str(&v->appname, argv[0]) ;
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)
401 read_rcfiles() ; /* Read .dtcalcrc's files. */
403 make_frames() ; /* Create dtcalc window frames. */
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. */
415 srand48((long) time((time_t *) 0)) ; /* Seed random number generator. */
417 make_items() ; /* Create server images and fir frames. */
418 if(v->display[0] == 0)
419 do_clear() ; /* Initialise and clear display. */
421 if (v->rstate == TRUE) /* Show the memory register window? */
423 make_registers(MEM) ;
424 if (!v->iconic) win_display(FCP_REG, TRUE) ;
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. */
433 /* Dtcalc's customised math library error-handling routine. */
439 if (!v->started) return ;
440 STRCPY(v->display, errmes) ;
441 set_item(DISPLAYITEM, v->display) ;
444 set_item(OPITEM, vstrs[(int) V_CLR]) ;
449 get_bool_resource(rtype, boolval) /* Get boolean resource from database. */
450 enum res_type rtype ;
453 char *val, tempstr[MAXLINE] ;
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 ;
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.
472 * XXX: This routine can be improved by using a hash lookup table.
481 for (n = 0; n < TITEMS; n++) {
482 if (ch == buttons[n].value)
486 v->curwin = FCP_KEY ;
491 v->row = n / MAXCOLS ;
492 v->column = n - (v->row * MAXCOLS) ;
498 get_int_resource(rtype, intval) /* Get integer resource from database. */
499 enum res_type rtype ;
504 if ((val = get_resource(rtype)) == NULL) return(0) ;
505 *intval = atoi(val) ;
510 /* Get keyboard equivalent from first character of localised string. */
513 get_key_val(val, str)
527 val = buttons[n].value ;
529 temp = buttons[n].str2;
531 temp = buttons[n].str;
534 STRCPY(v->pstr, temp) ;
541 get_options(argc, argv) /* Extract command line options. */
545 char next[MAXLINE] ; /* The next command line parameter. */
546 char strval[MAXLINE] ;
553 if (argv[0][0] == '-' || argv[0][0] == '+')
557 case 'D' : v->MPdebug = TRUE ; /* MP debug info. to stderr. */
560 getparam(next, argv, opts[(int) O_ACCVAL]) ;
561 v->accuracy = atoi(next) ;
562 if (v->accuracy < 0 || v->accuracy > 9)
564 msg = (char *) XtMalloc(strlen(
565 opts[(int) O_ACCRANGE]) + 3);
566 sprintf(msg, "%s", opts[(int) O_ACCRANGE]);
567 _DtSimpleError (v->appname, DtWarning, NULL, msg);
573 msg = (char *) XtMalloc(strlen(opts[(int) O_MODE])+
575 sprintf(msg, opts[(int) O_MODE], next);
576 getparam(next, argv, msg) ;
578 STRCPY(strval, next) ;
579 len = strlen(strval) ;
580 for (i = 0; i < len; i++)
582 if (islower(strval[i]))
583 strval[i] = toupper(strval[i]) ;
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 ;
593 msg = (char *) XtMalloc(strlen(opts[(int) O_MODE])+
595 sprintf(msg, opts[(int) O_MODE], next);
596 _DtSimpleError (v->appname, DtWarning, NULL, msg);
598 v->modetype = SCIENTIFIC ;
602 getparam(next, argv, opts[(int) O_BASE]) ;
603 STRCPY(strval, next) ;
604 len = strlen(strval) ;
605 for (i = 0; i < len; i++)
607 if (islower(strval[i]))
608 strval[i] = toupper(strval[i]) ;
610 if(strncmp(strval, "BIN", 3) == 0)
612 else if(strncmp(strval, "OCT", 3) == 0)
614 else if(strncmp(strval, "DEC", 3) == 0)
616 else if(strncmp(strval, "HEX", 3) == 0)
620 msg = (char *) XtMalloc(strlen(
621 opts[(int) O_BASE]) + 3);
622 sprintf(msg, "%s", opts[(int) O_BASE]);
623 _DtSimpleError (v->appname, DtWarning, NULL, msg);
628 case 'n' : if(strcmp(&argv[0][1], "notation") == 0)
631 msg = (char *) XtMalloc(strlen(
632 opts[(int) O_DISPLAY]) +
634 sprintf(msg, opts[(int) O_DISPLAY], next);
635 getparam(next, argv, msg) ;
637 STRCPY(strval, next) ;
638 len = strlen(strval) ;
639 for (i = 0; i < len; i++)
641 if (islower(strval[i]))
642 strval[i] = toupper(strval[i]) ;
644 if(strncmp(strval, "FIX", 3) == 0)
646 else if(strncmp(strval, "ENG", 3) == 0)
648 else if(strncmp(strval, "SCI", 3) == 0)
652 msg = (char *) XtMalloc(strlen(
653 opts[(int) O_DISPLAY]) +
655 sprintf(msg, opts[(int) O_DISPLAY], next);
656 _DtSimpleError (v->appname, DtWarning, NULL, msg);
662 else if(strcmp(&argv[0][1], "no_menu_bar") == 0)
665 application_args.menuBar = False;
668 case 't' : if(strcmp(&argv[0][1], "trig") == 0)
671 msg = (char *) XtMalloc(strlen(
672 opts[(int) O_TRIG]) +
674 sprintf(msg, opts[(int) O_TRIG], next);
675 getparam(next, argv, msg) ;
677 STRCPY(strval, next) ;
678 len = strlen(strval) ;
679 for (i = 0; i < len; i++)
681 if (islower(strval[i]))
682 strval[i] = toupper(strval[i]) ;
684 if(strncmp(strval, "DEG", 3) == 0)
686 else if(strncmp(strval, "RAD", 3) == 0)
688 else if(strncmp(strval, "GRAD", 4) == 0)
692 msg = (char *) XtMalloc(strlen(
693 opts[(int) O_TRIG]) +
695 sprintf(msg, opts[(int) O_TRIG], next);
696 _DtSimpleError (v->appname, DtWarning, NULL, msg);
702 case 's' : if(strcmp(&argv[0][1], "session") == 0)
705 getparam(next, argv, opts[(int) O_SESSION]) ;
706 application_args.session = XtNewString(next);
710 case 'v' : usage(v->progname) ;
713 toolarg : /* Pick up generic tool arguments. */
724 getparam(s, argv, errmes)
725 char *s, *argv[], *errmes ;
729 if (*argv != NULL && argv[0][0] != '-') STRCPY(s, *argv) ;
732 msg = (char *) XtMalloc(strlen(mess[(int) MESS_PARAM]) +
734 sprintf(msg, mess[(int) MESS_PARAM], errmes);
735 _DtSimpleError (v->appname, DtError, NULL, msg);
736 FPRINTF(stderr, mess[(int) MESS_PARAM], errmes) ;
743 get_rcfile(name) /* Read .dtcalcrc file. */
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. */
752 FILE *rcfd ; /* File descriptor for dtcalc rc file. */
754 if ((rcfd = fopen(name, "r")) == NULL) return ;
756 /* Process the .dtcalcrc file. There are currently four types of
757 * records to look for:
759 * 1) Those starting with a hash in the first column are comments.
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.
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.
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.
783 * All other lines are ignored.
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.
791 while (fgets(line, MAXLINE, rcfd) != NULL)
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' ;
798 if (line[1] >= '0' && line[1] <= '9' && line[2] == ' ')
800 i = char_val(line[1]) ;
803 n = sscanf(&line[3], "%lf", &cval) ;
808 MPstr_to_num(&line[4], DEC, v->MPcon_vals[i]) ;
809 mpneg(v->MPcon_vals[i], v->MPcon_vals[i]) ;
812 MPstr_to_num(&line[3], DEC, v->MPcon_vals[i]) ;
815 else if (isval == 'f')
817 SSCANF(&line[3], "%s", tmp) ;
818 STRCPY(v->fun_vals[i], convert(tmp)) ;
820 else if (isval == 'r')
822 n = sscanf(&line[3], "%lf", &cval) ;
823 if (n == 1) MPstr_to_num(&line[3], DEC, v->MPmvals[i]) ;
827 for (n = 3; n < len; n++)
828 if (line[n] == ' ' || line[n] == '\n')
830 while (line[n] == ' ') n++ ;
831 line[strlen(line)-1] = '\0' ;
834 STRCPY(tmp, make_number(v->MPcon_vals[i], TRUE)) ;
835 SPRINTF(v->con_names[i], "%1d: %s [%s]",
839 SPRINTF(v->fun_names[i], "%1d: %s [%s]",
850 get_str_resource(rtype, strval) /* Get a string resource from database. */
851 enum res_type rtype ;
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)
862 for (i = 0; i < len; i++)
863 if (islower(strval[i])) strval[i] = toupper(strval[i]) ;
870 grey_buttons(base) /* Grey out numeric buttons depending upon base. */
871 enum base_type base ;
874 int column, dim, i, n, row ;
876 for (i = 0; i < 16; i++)
879 if (isupper(val)) val = tolower(val) ;
880 for (n = 0; n < TITEMS; n++)
881 if (val == buttons[n].value) break ;
883 column = n - (row * MAXCOLS) ;
885 if (i < basevals[(int) base]) dim = FALSE ;
887 grey_button(row, column, dim) ;
893 handle_menu_selection(n, item) /* Process right button menu selection. */
898 if (IS_KEY(v->pending, KEY_LPAR)) /* Are we inside parentheses? */
900 v->current = buttons[n].value ;
907 save_pending_values(buttons[n].value) ;
909 v->ismenu = 1 ; /* To prevent grey buttons being redrawn. */
918 init_vars() /* Setup default values for various variables. */
920 int acc, i, n, size ;
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
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. */
935 mpset(&acc, &size, &size) ;
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. */
950 v->workspaces = NULL;
952 read_str(&v->iconlabel, lstrs[(int) L_LCALC]) ; /* Default icon label. */
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. */
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;
978 v->error = 0 ; /* Currently no display error. */
979 v->cur_op = '?' ; /* No arithmetic operator defined yet. */
980 v->old_cal_value = '?' ;
982 mpcim(&i, v->MPresult) ; /* No previous result yet. */
983 mpcim(&i, v->MPlast_input) ;
988 make_eng_sci(MPnumber) /* Convert engineering or scientific number. */
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 */
1000 if (v->dtype == ENG) eng = 1 ;
1002 mpabs(MPnumber, MPval) ;
1005 if (mplt(MPnumber, MP1)) *optr++ = '-' ;
1006 mpstr(MPval, MPmant) ;
1008 mpcim(&basevals[(int) v->base], MP1base) ;
1010 mppwr(MP1base, &n, MP3base) ;
1013 mppwr(MP1base, &n, MP10base) ;
1017 mpdiv(MP1, MP10base, MPatmp) ;
1021 if (!mpeq(MPmant, MP1))
1023 while (mpge(MPmant, MP10base))
1026 mpmul(MPmant, MPatmp, MPmant) ;
1029 while ((!eng && mpge(MPmant, MP1base)) ||
1030 (eng && (mpge(MPmant, MP3base) || exp % 3 != 0)))
1033 mpdiv(MPmant, MP1base, MPmant) ;
1036 while (mplt(MPmant, MPatmp))
1039 mpmul(MPmant, MP10base, MPmant) ;
1044 while (mplt(MPmant, MP1) || (eng && exp % 3 != 0))
1047 mpmul(MPmant, MP1base, MPmant) ;
1051 STRCPY(fixed, make_fixed(MPmant, MAX_DIGITS-6)) ;
1052 len = strlen(fixed) ;
1053 for (i = 0; i < len; i++) *optr++ = fixed[i] ;
1062 else *optr++ = '+' ;
1064 MPstr_to_num("0.5", DEC, MP1) ;
1065 mpaddi(MP1, &exp, MPval) ;
1068 for (ddig = 0; mpge(MPval, MP1); ddig++)
1069 mpdiv(MPval, MP1base, MPval) ;
1071 if (ddig == 0) *optr++ = '0' ;
1075 mpmul(MPval, MP1base, MPval) ;
1076 mpcmi(MPval, &dval) ;
1077 *optr++ = digits[dval] ;
1079 mpaddi(MPval, &dval, MPval) ;
1089 make_fixed(MPnumber, cmax) /* Convert MP number to fixed number string. */
1091 int cmax ; /* Maximum characters to generate. */
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 . */
1100 mpabs(MPnumber, MPval) ;
1103 if (mplt(MPnumber, MP1)) *optr++ = '-' ;
1105 mpcim(&basevals[(int) v->base], MP1base) ;
1107 mppwr(MP1base, &v->accuracy, MP1) ;
1108 MPstr_to_num("0.5", DEC, MP2) ;
1109 mpdiv(MP2, MP1, MP1) ;
1110 mpadd(MPval, MP1, MPval) ;
1114 if (mplt(MPval, MP2))
1121 for (ddig = 0; mpge(MPval, MP2); ddig++)
1122 mpdiv(MPval, MP1base, MPval) ;
1124 ndig = MIN(ddig + v->accuracy, --cmax) ;
1128 if (ddig-- == 0) *optr++ = '.' ;
1129 mpmul(MPval, MP1base, MPval) ;
1130 mpcmi(MPval, &dval) ;
1131 *optr++ = digits[dval] ;
1133 mpaddi(MPval, &dval, MPval) ;
1145 set_item(DISPLAYITEM, v->display) ;
1146 set_item(OPITEM, " ") ;
1147 set_item(HYPITEM, " ") ;
1148 set_item(INVITEM, " ") ;
1153 make_number(MPnumber, mkFix) /* Convert MP number to character string. */
1157 double number, val ;
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.
1163 * XXX: Needs to be improved. Shouldn't need to convert to a double in
1164 * order to do these tests.
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)
1175 if(v->accuracy == 0)
1177 if(val <= min_fix0[(int) v->base])
1178 return(make_eng_sci(MPnumber)) ;
1180 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1182 else if(v->accuracy == 1)
1184 if(val <= min_fix1[(int) v->base])
1185 return(make_eng_sci(MPnumber)) ;
1187 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1189 else if(v->accuracy == 2)
1191 if(val <= min_fix2[(int) v->base])
1192 return(make_eng_sci(MPnumber)) ;
1194 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1196 else if(v->accuracy == 3)
1198 if(val <= min_fix3[(int) v->base])
1199 return(make_eng_sci(MPnumber)) ;
1201 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1203 else if(v->accuracy == 4)
1205 if(val <= min_fix4[(int) v->base])
1206 return(make_eng_sci(MPnumber)) ;
1208 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1210 else if(v->accuracy == 5)
1212 if(val <= min_fix5[(int) v->base])
1213 return(make_eng_sci(MPnumber)) ;
1215 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1217 else if(v->accuracy == 6)
1219 if(val <= min_fix6[(int) v->base])
1220 return(make_eng_sci(MPnumber)) ;
1222 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1224 else if(v->accuracy == 7)
1226 if(val <= min_fix7[(int) v->base])
1227 return(make_eng_sci(MPnumber)) ;
1229 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1231 else if(v->accuracy == 8)
1233 if(val <= min_fix8[(int) v->base])
1234 return(make_eng_sci(MPnumber)) ;
1236 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1238 else if(v->accuracy == 9)
1240 if(val <= min_fix9[(int) v->base])
1241 return(make_eng_sci(MPnumber)) ;
1243 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1247 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1253 matherr(exc) /* Default math library exception handling routine. */
1254 struct exception *exc ;
1260 strcpy(msg, exc->name);
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 ");
1275 strcat(msg, vstrs[(int) V_ERROR]);
1277 _DtSimpleError (v->appname, DtWarning, NULL, msg);
1281 doerr(vstrs[(int) V_ERROR]) ;
1282 return(1) ; /* Value ignored. */
1287 MPstr_to_num(str, base, MPval) /* Convert string into an MP number. */
1289 enum base_type base ;
1293 int MP1[MP_SIZE], MP2[MP_SIZE], MPbase[MP_SIZE] ;
1301 mpcim(&basevals[(int) base], MPbase) ;
1303 while (*optr == ' ') optr++ ;
1306 /* negative number */
1310 while ((inum = char_val(*optr)) >= 0)
1312 mpmul(MPval, MPbase, MPval) ;
1313 mpaddi(MPval, &inum, MPval) ;
1318 for (i = 1; (inum = char_val(*++optr)) >= 0; i++)
1320 mppwr(MPbase, &i, MP1) ;
1322 mpdiv(MP2, MP1, MP1) ;
1323 mpadd(MPval, MP1, MPval) ;
1326 while (*optr == ' ') optr++ ;
1330 if (*optr == '-') exp_sign = -1 ;
1332 while ((inum = char_val(*++optr)) >= 0)
1333 exp = exp * basevals[(int) base] + inum ;
1337 if (v->key_exp || exp_sign == -1)
1339 mppwr(MPbase, &exp, MP1) ;
1340 mpmul(MPval, MP1, MPval) ;
1346 mpmul(MPval, MP1, MPval) ;
1352 paren_disp(c) /* Append the latest parenthesis char to the display item. */
1357 /* If the character is a Delete, clear the whole line, and exit parenthesis
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.
1364 * If the character is a control character (not Ctrl-h), then append ^(char).
1366 * Otherwise just append the character.
1369 n = strlen(v->display) ;
1370 if (IS_KEY(c, KEY_CLR)) /* Is it a Delete character? */
1372 v->noparens = v->pending = v->opsptr = v->numsptr = 0 ;
1374 set_item(OPITEM, "") ;
1376 mpcim(&i, v->MPdisp_val) ;
1377 show_display(v->MPdisp_val) ;
1379 set_numtype(v->dtype);
1380 set_option_menu((int) TTYPEITEM, (int)v->ttype);
1382 v->curwin = FCP_KEY ;
1386 else if (IS_KEY(c, KEY_BSP)) /* Is is a Back Space character? */
1389 if (v->display[n-1] == '(')
1394 v->pending = v->opsptr = v->numsptr = 0 ;
1396 set_item(OPITEM, "") ;
1397 show_display(v->MPdisp_val) ;
1401 v->display[n-1] = '\0' ;
1403 else if (c <= CTL('z')) /* Is it a control character? */
1407 v->display[n] = '^' ;
1408 v->display[n+1] = c + 96 ;
1409 v->display[n+2] = '\0' ;
1412 else /* It must be an ordinary character. */
1417 v->display[n+1] = '\0' ;
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]) ;
1429 process_event(type) /* Process this event. */
1436 case KEYBOARD_DOWN : if (v->pending)
1438 v->current = v->cur_ch ;
1443 ival = get_index(v->cur_ch) ;
1444 if (ival < TITEMS - EXTRA) {
1445 if ((v->modetype == FINANCIAL &&
1447 (v->modetype == LOGICAL && (
1448 ival == 21 || ival == 22 ||
1452 /* go arm the button */
1453 draw_button(ival, v->curwin, v->row,
1456 process_item(ival) ;
1460 case KEYBOARD_UP : ival = get_index(v->cur_ch) ;
1461 if (ival < TITEMS - EXTRA) {
1462 if ((v->modetype == FINANCIAL &&
1464 (v->modetype == LOGICAL && (
1465 ival == 21 || ival == 22 ||
1469 /* go disarm the button */
1470 draw_button(ival, v->curwin, v->row,
1473 /* go disarm the button */
1474 draw_button(ival, v->curwin, v->row,
1488 if (n < 0 || n >= TITEMS)
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' ;
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 ;
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)() ;
1516 switch (buttons[n].opdisp)
1519 if (v->current == 'T')
1521 if (v->modetype == SCIENTIFIC)
1522 set_item(OPITEM, buttons[n].str) ;
1527 set_item(OPITEM, buttons[n].str) ;
1531 set_item(OPITEM, vstrs[(int) V_CLR]) ;
1533 set_item(OPITEM, "") ;
1535 (*buttons[n].func)() ;
1537 if(strcmp(buttons[n].resname, "clr") != 0)
1542 /* Process a portion of the parentheses stack. */
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. */
1550 char sdisp[MAXLINE] ; /* Used to save display contents. */
1552 int nptr ; /* Pointer to next number from numeric stack. */
1554 STRCPY(sdisp, v->display) ; /* Save current display. */
1557 v->cur_op = '?' ; /* Current operation is initially undefined. */
1558 for (i = 0; i < n; i++)
1560 if (v->opstack[startop + i] == -1)
1562 mpstr(v->MPnumstack[nptr++], v->MPdisp_val) ;
1566 v->cur_ch = v->opstack[startop + i] ;
1567 if (v->cur_ch == '^') /* Control character? */
1570 v->cur_ch = CTL(v->opstack[startop + i]) ;
1574 v->current = v->cur_ch ;
1577 else process_item(get_index(v->cur_ch)) ;
1580 v->numsptr = startnum ;
1581 push_num(v->MPdisp_val) ;
1582 v->opsptr = startop - 1 ;
1584 save_pending_values(KEY_LPAR) ;
1585 STRCPY(v->display, sdisp) ; /* Restore current display. */
1590 process_str(str, mtype)
1592 enum menu_type mtype ;
1598 STRCPY(save, v->display) ;
1599 STRCPY(v->display, " ") ;
1600 set_item(DISPLAYITEM, v->display);
1601 for (i = 0 ; i < len; i++)
1609 STRCPY(v->display, save);
1610 set_item(DISPLAYITEM, v->display);
1617 v->current = str[i] ;
1636 process_item(get_index(str[i])) ;
1643 read_rcfiles() /* Read .dtcalcrc's from home and current directories. */
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. */
1650 struct passwd *entry ;
1652 for (n = 0; n < MAXREGS; n++)
1654 STRCPY(tmp, make_number(v->MPcon_vals[n], FALSE)) ;
1655 SPRINTF(name, "%1d: %s [%s]", n, tmp, v->con_names[n]) ;
1657 STRCPY(v->con_names[n], name) ;
1658 STRCPY(v->fun_vals[n], "") ; /* Initially empty function strings. */
1661 if ((home = getenv("HOME")) == NULL)
1663 if ((entry = getpwuid(getuid())) == NULL) return ;
1664 home = entry->pw_dir ;
1666 SPRINTF(name, "%s/%s", home, RCNAME) ;
1667 get_rcfile(name) ; /* Read .dtcalcrc from users home directory. */
1669 SPRINTF(name, "%s/%s", getcwd(pathname, MAXPATHLEN+1), RCNAME) ;
1670 get_rcfile(name) ; /* Read .dtcalcrc file from current directory. */
1680 STRCPY(v->display, make_number(MPval, TRUE)) ;
1681 set_item(DISPLAYITEM, v->display) ;
1690 FPRINTF(stderr, ustrs[(int) USAGE1], PATCHLEVEL) ;
1691 FPRINTF(stderr, "%s", ustrs[(int) USAGE2]) ;
1692 FPRINTF(stderr, "%s", ustrs[(int) USAGE3]) ;
1697 write_rcfile(mtype, exists, cfno, val, comment)
1698 enum menu_type mtype ;
1700 char *val, *comment ;
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. */
1714 SPRINTF(rcname, "%s/%s", getcwd(pathname, MAXPATHLEN), RCNAME) ;
1715 if (access(rcname, F_OK) == 0) rcexists = 1 ;
1718 if ((home = getenv("HOME")) == NULL)
1720 if ((entry = getpwuid(getuid())) == NULL) return ;
1721 home = entry->pw_dir ;
1723 SPRINTF(rcname, "%s/%s", home, RCNAME) ;
1724 if (access(rcname, F_OK) == 0) rcexists = 1 ;
1726 STRCPY(tmp_filename, "/tmp/.dtcalcrcXXXXXX") ;
1727 MKTEMP(tmp_filename) ;
1728 if ((tmpfd = fopen(tmp_filename, "w+")) == NULL) return ;
1732 rcfd = fopen(rcname, "r") ;
1733 SPRINTF(sval, " %1d", cfno) ;
1734 while (fgets(str, MAXLINE, rcfd))
1740 case M_CON : sval[0] = 'c' ;
1741 if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
1743 if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
1745 case M_FUN : sval[0] = 'f' ;
1746 if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
1748 if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
1751 FPRINTF(tmpfd, "%s", str) ;
1758 case M_CON : FPRINTF(tmpfd, "\nC%1d %s %s\n", cfno, val, comment) ;
1761 if(strcmp(val, "") != 0)
1762 FPRINTF(tmpfd, "\nF%1d %s %s\n", cfno, val, comment) ;
1765 rcfd = fopen(rcname, "w") ;
1767 while (fgets(str, MAXLINE, tmpfd)) FPRINTF(rcfd, "%s", str) ;
1770 UNLINK(tmp_filename) ;
1775 write_resources(filename)
1781 int MPtemp[MP_SIZE];
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)) ;
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)) ;
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)) ;
1816 save_resources(filename) ;