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. *
41 #include <sys/types.h>
43 #include <sys/param.h>
46 #include "patchlevel.h"
48 #include "ds_common.h"
52 /* Copied from math.h */
66 6.871947674e+10, 3.245185537e+32,
67 1.000000000e+36, 2.230074520e+43
69 double min_fix0[4] = {
70 2.500000000e-1, 3.750000000e-1,
71 1.000000000e-1, 4.375000000e-1
73 double min_fix1[4] = {
74 1.250000000e-1, 4.687500000e-2,
75 1.000000000e-2, 2.734375000e-2
77 double min_fix2[4] = {
78 6.250000000e-2, 5.859375000e-3,
79 1.000000000e-3, 1.708984375e-3
81 double min_fix3[4] = {
82 3.125000000e-2, 7.324218750e-4,
83 1.000000000e-4, 1.068115234e-4
85 double min_fix4[4] = {
86 1.562500000e-2, 9.155273437e-5,
87 1.000000000e-5, 6.675720215e-6
89 double min_fix5[4] = {
90 7.812500000e-3, 1.144409180e-5,
91 1.000000000e-6, 4.172325134e-7
93 double min_fix6[4] = {
94 6.906250000e-3, 1.430511475e-6,
95 1.000000000e-7, 2.607703209e-8
97 double min_fix7[4] = {
98 1.953125000e-3, 1.788139343e-7,
99 1.000000000e-8, 1.629814506e-9
101 double min_fix8[4] = {
102 9.765625000e-4, 2.235174179e-8,
103 1.000000000e-9, 1.018634066e-10
105 double min_fix9[4] = {
106 4.882812500e-4, 2.793967724e-9,
107 1.000000000e-10, 6.366462912e-12
110 extern char *base_str[] ; /* Strings for each base value. */
111 extern char *cmdstr[] ; /* Strings for each command line option. */
112 extern char *dtype_str[] ; /* Strings for each display mode value. */
113 extern char *lstrs[] ; /* Labels for various Motif items. */
114 extern char *mess[] ; /* Message strings. */
115 extern char *mode_str[] ; /* Strings for each mode value. */
116 extern char *opts[] ; /* Command line option strings. */
117 extern char *ttype_str[] ; /* Strings for each trig type value. */
118 extern char *ustrs[] ; /* Usage message strings. */
119 extern char *vstrs[] ; /* Various strings. */
121 char digits[] = "0123456789ABCDEF" ;
122 int basevals[4] = { 2, 8, 10, 16 } ;
124 int left_pos[BCOLS] = { 3, 2, 1, 0 } ; /* Left positions. */
125 int right_pos[BCOLS] = { 0, 1, 2, 3 } ; /* "Right" positions. */
127 /* Valid keys when an error condition has occured. */
128 /* MEM KEYS clr clr QUIT REDRAW */
129 char validkeys[MAXVKEYS] = { 'm', 'k', '\177', '\013', 'q', '\f' } ;
131 Vars v ; /* Calctool variables and options. */
133 struct menu_entry menu_entries[MAXENTRIES] ;
135 struct menu cmenus[MAXMENUS] = { /* Calculator menus. */
136 /* title total index defval */
137 { (char *) NULL, 10, 0, 2 /* 2 places */ }, /* ACC */
138 { (char *) NULL, 4, 20, 2 /* Decimal */ }, /* BASE TYPE */
139 { (char *) NULL, 10, 0, 0 /* Con. 0 */ }, /* CON */
140 { (char *) NULL, 10, 10, 0 /* Reg. 0 */ }, /* EXCH */
141 { (char *) NULL, 10, 0, 0 /* Fun. 0 */ }, /* FUN */
142 { (char *) NULL, 4, 30, 0 /* Basic */ }, /* MODE */
143 { (char *) NULL, 3, 24, 1 /* Fixed */ }, /* NUM TYPE */
144 { (char *) NULL, 10, 10, 0 /* Reg. 0 */ }, /* RCL */
145 { (char *) NULL, 10, 10, 0 /* Reg. 0 */ }, /* STO */
146 { (char *) NULL, 3, 27, 0 /* Degrees */ }, /* TRIG TYPE */
149 /* This table shows the keyboard values that are currently being used:
151 * | 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
152 *-------------+--------------------------------------------------
153 * Control: | a c d f h i l m r s t u y
154 * Lower: | a b c d e f h i k m n p q r s v x y
155 * Upper: | A B C D E F G M N P Q R S T X
156 * Numeric: | 0 1 2 3 4 5 6 7 8 9
157 * Other: | @ . + - * / = % ( ) # < > [ ] { } | & ~ ^ ? ! \177
158 *----------------------------------------------------------------
161 /* Calculator button values. */
163 struct button buttons[TITEMS] = {
164 /* str str2 value opdisp menutype resname func */
167 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
168 { (char *)NULL, (char *)NULL, 0, OP_SET, M_FUN, "fun", do_pending },
169 { (char *)NULL, (char *)NULL, 0, OP_SET, M_CON, "con", do_pending },
170 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "keys", do_keys },
173 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "int", do_portion },
174 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "frac", do_portion },
175 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "abs", do_portion },
176 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "exp", do_expno },
179 { (char *)NULL, (char *)NULL, 0, OP_SET, M_ACC, "acc", do_pending },
180 { (char *)NULL, (char *)NULL, 0, OP_SET, M_STO, "sto", do_pending },
181 { (char *)NULL, (char *)NULL, 0, OP_SET, M_RCL, "rcl", do_pending },
182 { (char *)NULL, (char *)NULL, 0, OP_SET, M_EXCH, "exch", do_pending },
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 { (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 { (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 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
203 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "recip", do_immed },
204 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "square", do_immed },
205 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "sqrt", do_immed },
206 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "percent", do_calc },
209 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "lparen", do_paren },
210 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "rparen", do_paren },
211 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "bsp", do_delete },
212 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "clr", do_clear },
215 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "numd", do_number },
216 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "nume", do_number },
217 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "numf", do_number },
218 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "chs", do_immed },
221 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "numa", do_number },
222 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "numb", do_number },
223 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "numc", do_number },
224 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "mul", do_calc },
227 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num7", do_number },
228 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num8", do_number },
229 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num9", do_number },
230 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "div", do_calc },
233 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num4", do_number },
234 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num5", do_number },
235 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num6", do_number },
236 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "sub", do_calc },
239 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num1", do_number },
240 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num2", do_number },
241 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num3", do_number },
242 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "add", do_calc },
245 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num0", do_number },
246 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "point", do_point },
247 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "equals", do_calc },
248 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "quit", do_frame },
250 { (char *)NULL, (char *)NULL, 0, OP_SET, M_BASE, "base", do_pending },
251 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NUM, "disp", do_pending },
252 { (char *)NULL, (char *)NULL, 0, OP_SET, M_MODE, "mode", do_pending },
253 { (char *)NULL, (char *)NULL, 0, OP_SET, M_TRIG, "trig", do_pending },
256 struct button mode_buttons[MAXMODES * MODEKEYS] = {
257 /* str str2 value opdisp menutype resname func */
260 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "term", do_business},
261 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "rate", do_business},
262 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "pv", do_business},
263 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "pmt", do_business},
264 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "fv", do_business},
265 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "ppy", do_business},
266 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "clrreg", do_business},
267 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "ctrm", do_business},
268 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "ddb", do_business},
269 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "sln", do_business},
270 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "syd", do_business},
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 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
278 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "lshift", do_pending },
279 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "rshift", do_pending },
280 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "and16", do_immed },
281 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "and32", do_immed },
282 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "or", do_calc },
283 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "and", do_calc },
284 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "not", do_immed },
285 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "xor", do_calc },
286 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "xnor", do_calc },
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 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
296 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "inv", do_immed },
297 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "hyp", do_immed },
298 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "etox", do_immed },
299 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "tentox", do_immed },
300 { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "ytox", do_calc },
301 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "xfact", do_immed },
302 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "cos", do_trig },
303 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "sin", do_trig },
304 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "tan", do_trig },
305 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "ln", do_immed },
306 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "log", do_immed },
307 { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "rand", do_immed },
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 { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
319 if (chr >= '0' && chr <= '9') return(chr - '0') ;
320 else if (chr >= 'a' && chr <= 'f') return(chr - 'a' + 10) ;
321 else if (chr >= 'A' && chr <= 'F') return(chr - 'A' + 10) ;
335 mpcim(&i, v->MPdisp_val) ;
336 STRCPY(v->display, make_number(v->MPdisp_val, FALSE)) ;
337 set_item(DISPLAYITEM, v->display) ;
342 v->opsptr = 0 ; /* Clear parentheses stacks. */
344 set_item(HYPITEM, " ") ;
345 set_item(INVITEM, " ") ;
350 convert(line) /* Convert .dtcalcrc line to ascii values. */
351 char *line ; /* Input line to be converted. */
353 static char output[MAXLINE] ; /* Converted output record. */
354 int ctrl = 0 ; /* Set if we are processing a control character. */
355 int i ; /* Position within input line. */
357 int n = 0 ; /* Position within output line. */
360 for (i = 0; i < len; i++)
362 if (line[i] == ' ') continue ;
363 else if (line[i] == '\\') ctrl = 1 ;
366 output[n++] = CTL(line[i]) ;
369 else output[n++] = line[i] ;
377 do_dtcalc(argc, argv)
384 v->progname = argv[0] ; /* Save programs name. */
386 init_cmdline_opts() ; /* Initialise command line option strings. */
388 if ((ptr = strrchr(argv[0], '/')) != NULL)
389 read_str(&v->appname, ptr+1) ;
390 else read_str(&v->appname, argv[0]) ;
392 init_text() ; /* Setup text strings depending upon language. */
393 init_vars() ; /* Setup default values for variables. */
394 key_init() ; /* Determine numeric function keys. */
395 load_resources() ; /* Get resources from various places. */
396 read_resources() ; /* Read resources from merged database. */
397 get_options(argc, argv) ; /* Get command line arguments. */
398 if(application_args.session != NULL)
402 read_rcfiles() ; /* Read .dtcalcrc's files. */
404 make_frames() ; /* Create dtcalc window frames. */
406 v->shelf = NULL ; /* No selection for shelf initially. */
407 v->noparens = 0 ; /* No unmatched brackets initially. */
408 v->opsptr = 0 ; /* Nothing on the parentheses op stack. */
409 v->numsptr = 0 ; /* Nothing on the parenthese numeric stack. */
410 v->pending = 0 ; /* No initial pending command. */
411 if(application_args.session == NULL)
412 v->tstate = 0 ; /* Button values displayed first. */
413 v->hyperbolic = 0 ; /* Normal trig functions initially. */
414 v->inverse = 0 ; /* No inverse functions initially. */
416 srand48((long) time((time_t *) 0)) ; /* Seed random number generator. */
418 make_items() ; /* Create server images and fir frames. */
419 if(v->display[0] == 0)
420 do_clear() ; /* Initialise and clear display. */
422 if (v->rstate == TRUE) /* Show the memory register window? */
424 make_registers(MEM) ;
425 if (!v->iconic) win_display(FCP_REG, TRUE) ;
427 if (!v->iconic) win_display(FCP_MODE, TRUE) ;
428 show_display(v->MPdisp_val) ; /* Output in correct display mode. */
429 save_cmdline(argc, argv) ; /* Setup dtcalc command line. */
430 start_tool() ; /* Display the calculator. */
434 /* Dtcalc's customised math library error-handling routine. */
440 if (!v->started) return ;
441 STRCPY(v->display, errmes) ;
442 set_item(DISPLAYITEM, v->display) ;
445 set_item(OPITEM, vstrs[(int) V_CLR]) ;
450 get_bool_resource(rtype, boolval) /* Get boolean resource from database. */
451 enum res_type rtype ;
454 char *val, tempstr[MAXLINE] ;
457 if ((val = get_resource(rtype)) == NULL) return(0) ;
458 STRCPY(tempstr, val) ;
459 len = strlen(tempstr) ;
460 for (n = 0; n < len; n++)
461 if (isupper(tempstr[n])) tempstr[n] = tolower(tempstr[n]) ;
462 if (EQUAL(tempstr, vstrs[(int) V_TRUE])) *boolval = TRUE ;
463 else *boolval = FALSE ;
468 /* Get button index for given character value, setting curwin,
469 * row and column appropriately. Note that if the value isn't found,
470 * then a value of TITEMS is returned. This is "chucked out" by
471 * process_item as being invalid.
473 * XXX: This routine can be improved by using a hash lookup table.
482 for (n = 0; n < TITEMS; n++) {
483 if (ch == buttons[n].value)
487 v->curwin = FCP_KEY ;
492 v->row = n / MAXCOLS ;
493 v->column = n - (v->row * MAXCOLS) ;
499 get_int_resource(rtype, intval) /* Get integer resource from database. */
500 enum res_type rtype ;
505 if ((val = get_resource(rtype)) == NULL) return(0) ;
506 *intval = atoi(val) ;
511 /* Get keyboard equivalent from first character of localised string. */
514 get_key_val(val, str)
528 val = buttons[n].value ;
530 temp = buttons[n].str2;
532 temp = buttons[n].str;
535 STRCPY(v->pstr, temp) ;
542 get_options(argc, argv) /* Extract command line options. */
546 char next[MAXLINE] ; /* The next command line parameter. */
547 char strval[MAXLINE] ;
554 if (argv[0][0] == '-' || argv[0][0] == '+')
558 case 'D' : v->MPdebug = TRUE ; /* MP debug info. to stderr. */
561 getparam(next, argv, opts[(int) O_ACCVAL]) ;
562 v->accuracy = atoi(next) ;
563 if (v->accuracy < 0 || v->accuracy > 9)
565 msg = (char *) XtMalloc(strlen(
566 opts[(int) O_ACCRANGE]) + 3);
567 sprintf(msg, opts[(int) O_ACCRANGE]);
568 _DtSimpleError (v->appname, DtWarning, NULL, msg);
574 msg = (char *) XtMalloc(strlen(opts[(int) O_MODE])+
576 sprintf(msg, opts[(int) O_MODE], next);
577 getparam(next, argv, msg) ;
579 STRCPY(strval, next) ;
580 len = strlen(strval) ;
581 for (i = 0; i < len; i++)
583 if (islower(strval[i]))
584 strval[i] = toupper(strval[i]) ;
586 if(strcmp(strval, "FINANCIAL") == 0)
587 v->modetype = FINANCIAL ;
588 else if(strcmp(strval, "LOGICAL") == 0)
589 v->modetype = LOGICAL ;
590 else if(strcmp(strval, "SCIENTIFIC") == 0)
591 v->modetype = SCIENTIFIC ;
594 msg = (char *) XtMalloc(strlen(opts[(int) O_MODE])+
596 sprintf(msg, opts[(int) O_MODE], next);
597 _DtSimpleError (v->appname, DtWarning, NULL, msg);
599 v->modetype = SCIENTIFIC ;
603 getparam(next, argv, opts[(int) O_BASE]) ;
604 STRCPY(strval, next) ;
605 len = strlen(strval) ;
606 for (i = 0; i < len; i++)
608 if (islower(strval[i]))
609 strval[i] = toupper(strval[i]) ;
611 if(strncmp(strval, "BIN", 3) == 0)
613 else if(strncmp(strval, "OCT", 3) == 0)
615 else if(strncmp(strval, "DEC", 3) == 0)
617 else if(strncmp(strval, "HEX", 3) == 0)
621 msg = (char *) XtMalloc(strlen(
622 opts[(int) O_BASE]) + 3);
623 sprintf(msg, opts[(int) O_BASE]);
624 _DtSimpleError (v->appname, DtWarning, NULL, msg);
629 case 'n' : if(strcmp(&argv[0][1], "notation") == 0)
632 msg = (char *) XtMalloc(strlen(
633 opts[(int) O_DISPLAY]) +
635 sprintf(msg, opts[(int) O_DISPLAY], next);
636 getparam(next, argv, msg) ;
638 STRCPY(strval, next) ;
639 len = strlen(strval) ;
640 for (i = 0; i < len; i++)
642 if (islower(strval[i]))
643 strval[i] = toupper(strval[i]) ;
645 if(strncmp(strval, "FIX", 3) == 0)
647 else if(strncmp(strval, "ENG", 3) == 0)
649 else if(strncmp(strval, "SCI", 3) == 0)
653 msg = (char *) XtMalloc(strlen(
654 opts[(int) O_DISPLAY]) +
656 sprintf(msg, opts[(int) O_DISPLAY], next);
657 _DtSimpleError (v->appname, DtWarning, NULL, msg);
663 else if(strcmp(&argv[0][1], "no_menu_bar") == 0)
666 application_args.menuBar = False;
669 case 't' : if(strcmp(&argv[0][1], "trig") == 0)
672 msg = (char *) XtMalloc(strlen(
673 opts[(int) O_TRIG]) +
675 sprintf(msg, opts[(int) O_TRIG], next);
676 getparam(next, argv, msg) ;
678 STRCPY(strval, next) ;
679 len = strlen(strval) ;
680 for (i = 0; i < len; i++)
682 if (islower(strval[i]))
683 strval[i] = toupper(strval[i]) ;
685 if(strncmp(strval, "DEG", 3) == 0)
687 else if(strncmp(strval, "RAD", 3) == 0)
689 else if(strncmp(strval, "GRAD", 4) == 0)
693 msg = (char *) XtMalloc(strlen(
694 opts[(int) O_TRIG]) +
696 sprintf(msg, opts[(int) O_TRIG], next);
697 _DtSimpleError (v->appname, DtWarning, NULL, msg);
703 case 's' : if(strcmp(&argv[0][1], "session") == 0)
706 getparam(next, argv, opts[(int) O_SESSION]) ;
707 application_args.session = XtNewString(next);
711 case 'v' : usage(v->progname) ;
714 toolarg : /* Pick up generic tool arguments. */
725 getparam(s, argv, errmes)
726 char *s, *argv[], *errmes ;
730 if (*argv != NULL && argv[0][0] != '-') STRCPY(s, *argv) ;
733 msg = (char *) XtMalloc(strlen(mess[(int) MESS_PARAM]) +
735 sprintf(msg, mess[(int) MESS_PARAM], errmes);
736 _DtSimpleError (v->appname, DtError, NULL, msg);
737 FPRINTF(stderr, mess[(int) MESS_PARAM], errmes) ;
744 get_rcfile(name) /* Read .dtcalcrc file. */
747 char line[MAXLINE] ; /* Current line from the .dtcalcrc file. */
748 char tmp[MAXLINE] ; /* Used to extract definitions. */
749 double cval ; /* Current constant value being converted. */
750 int i ; /* Index to constant or function array. */
751 int isval ; /* Set to 'c' or 'f' for convertable line. */
753 FILE *rcfd ; /* File descriptor for dtcalc rc file. */
755 if ((rcfd = fopen(name, "r")) == NULL) return ;
757 /* Process the .dtcalcrc file. There are currently four types of
758 * records to look for:
760 * 1) Those starting with a hash in the first column are comments.
762 * 2) Lines starting with 'c' or 'C' in the first column are
763 * definitions for constants. The cC is followed by a digit in
764 * the range 0-9, then a space. This is followed by a number
765 * in fixed or scientific notation. Following this is an optional
766 * comment, which if found, will be used in the popup menu for
767 * the constants. If the comment is present, there must be at
768 * least one space between this and the preceding number.
770 * 3) Those starting with a 'f' or a 'F' in the first column are
771 * definitions for functions. The fF is followed by a digit in
772 * the range 0-9, then a space. This is followed by a function
773 * definition. Following this is an optional comment, which if
774 * found, will be used in the popup menu for the functions.
775 * If the comment is present, there must be at least one space
776 * between this and the preceding function definition.
778 * 4) Lines starting with a 'r' or a 'R' in the first column are
779 * definitions for the initial contents of the calculators
780 * memory registers. The rR is followed by a digit in the
781 * range 0-9, then a space. This is followed by a number in
782 * fixed or scientific notation. The rest of the line is ignored.
784 * All other lines are ignored.
786 * Two other things to note. There should be no embedded spaces in
787 * the function definitions, and whenever a backslash is found, that
788 * and the following character signify a control character, for
789 * example \g would be ascii 7.
792 while (fgets(line, MAXLINE, rcfd) != NULL)
795 if (line[0] == 'c' || line[0] == 'C') isval = 'c' ;
796 else if (line[0] == 'f' || line[0] == 'F') isval = 'f' ;
797 else if (line[0] == 'r' || line[0] == 'R') isval = 'r' ;
799 if (line[1] >= '0' && line[1] <= '9' && line[2] == ' ')
801 i = char_val(line[1]) ;
804 n = sscanf(&line[3], "%lf", &cval) ;
809 MPstr_to_num(&line[4], DEC, v->MPcon_vals[i]) ;
810 mpneg(v->MPcon_vals[i], v->MPcon_vals[i]) ;
813 MPstr_to_num(&line[3], DEC, v->MPcon_vals[i]) ;
816 else if (isval == 'f')
818 SSCANF(&line[3], "%s", tmp) ;
819 STRCPY(v->fun_vals[i], convert(tmp)) ;
821 else if (isval == 'r')
823 n = sscanf(&line[3], "%lf", &cval) ;
824 if (n == 1) MPstr_to_num(&line[3], DEC, v->MPmvals[i]) ;
828 for (n = 3; n < len; n++)
829 if (line[n] == ' ' || line[n] == '\n')
831 while (line[n] == ' ') n++ ;
832 line[strlen(line)-1] = '\0' ;
835 STRCPY(tmp, make_number(v->MPcon_vals[i], TRUE)) ;
836 SPRINTF(v->con_names[i], "%1d: %s [%s]",
840 SPRINTF(v->fun_names[i], "%1d: %s [%s]",
851 get_str_resource(rtype, strval) /* Get a string resource from database. */
852 enum res_type rtype ;
858 if ((val = get_resource(rtype)) == NULL) return(0) ;
859 STRCPY(strval, val) ;
860 len = strlen(strval) ;
861 if(rtype != R_TRIG && rtype != R_DISPLAY)
863 for (i = 0; i < len; i++)
864 if (islower(strval[i])) strval[i] = toupper(strval[i]) ;
871 grey_buttons(base) /* Grey out numeric buttons depending upon base. */
872 enum base_type base ;
875 int column, dim, i, n, row ;
877 for (i = 0; i < 16; i++)
880 if (isupper(val)) val = tolower(val) ;
881 for (n = 0; n < TITEMS; n++)
882 if (val == buttons[n].value) break ;
884 column = n - (row * MAXCOLS) ;
886 if (i < basevals[(int) base]) dim = FALSE ;
888 grey_button(row, column, dim) ;
894 handle_menu_selection(n, item) /* Process right button menu selection. */
899 if (IS_KEY(v->pending, KEY_LPAR)) /* Are we inside parentheses? */
901 v->current = buttons[n].value ;
908 save_pending_values(buttons[n].value) ;
910 v->ismenu = 1 ; /* To prevent grey buttons being redrawn. */
919 init_vars() /* Setup default values for various variables. */
921 int acc, i, n, size ;
923 v->accuracy = 2 ; /* Initial accuracy. */
924 v->base = DEC ; /* Initial base. */
925 v->dtype = FIX ; /* Initial number display mode. */
926 v->ttype = DEG ; /* Initial trigonometric type. */
927 v->modetype = SCIENTIFIC; /* Initial calculator mode. */
928 v->rstate = 0 ; /* No memory register frame display initially. */
929 v->frstate = 0 ; /* No fin. memory register frame display
931 v->iconic = FALSE ; /* Calctool not iconic by default. */
932 v->MPdebug = FALSE ; /* No debug info by default. */
933 v->MPerrors = TRUE ; /* No error information. */
934 acc = MAX_DIGITS + 12 ; /* MP internal accuracy. */
936 mpset(&acc, &size, &size) ;
938 v->hasicon = FALSE ; /* Use standard dtcalc icon by default. */
939 v->beep = TRUE ; /* Beep on error by default. */
940 v->error = 0 ; /* No calculator error initially. */
941 v->key_exp = 0 ; /* Not entering an exponent number. */
942 v->pending_op = 0 ; /* No pending arithmetic operation. */
943 v->titleline = NULL ; /* No User supplied title line. */
951 v->workspaces = NULL;
953 read_str(&v->iconlabel, lstrs[(int) L_LCALC]) ; /* Default icon label. */
955 MPstr_to_num("0.621", DEC, v->MPcon_vals[0]) ; /* kms/hr <=> miles/hr. */
956 MPstr_to_num("1.4142135623", DEC, v->MPcon_vals[1]) ; /* square root of 2 */
957 MPstr_to_num("2.7182818284", DEC, v->MPcon_vals[2]) ; /* e */
958 MPstr_to_num("3.1415926535", DEC, v->MPcon_vals[3]) ; /* pi */
959 MPstr_to_num("2.54", DEC, v->MPcon_vals[4]) ; /* cms <=> inch. */
960 MPstr_to_num("57.295779513", DEC, v->MPcon_vals[5]) ; /* degrees/radian. */
961 MPstr_to_num("1048576.0", DEC, v->MPcon_vals[6]) ; /* 2 ^ 20. */
962 MPstr_to_num("0.0353", DEC, v->MPcon_vals[7]) ; /* grams <=> ounce. */
963 MPstr_to_num("0.948", DEC, v->MPcon_vals[8]) ; /* Kjoules <=> BTU's. */
964 MPstr_to_num("0.0610", DEC, v->MPcon_vals[9]) ; /* cms3 <=> inches3. */
967 for (i = 0; i < MAXREGS; i++) mpcim(&n, v->MPmvals[i]) ;
968 for (i = 0; i < FINREGS; i++)
969 v->MPfvals[i] = (double)n;
970 v->MPfvals[FINREGS - 1] = (double)12;
979 v->error = 0 ; /* Currently no display error. */
980 v->cur_op = '?' ; /* No arithmetic operator defined yet. */
981 v->old_cal_value = '?' ;
983 mpcim(&i, v->MPresult) ; /* No previous result yet. */
984 mpcim(&i, v->MPlast_input) ;
989 make_eng_sci(MPnumber) /* Convert engineering or scientific number. */
992 char fixed[MAX_DIGITS+1], *optr ;
993 int MP1[MP_SIZE], MPatmp[MP_SIZE], MPval[MP_SIZE] ;
994 int MP1base[MP_SIZE], MP3base[MP_SIZE], MP10base[MP_SIZE] ;
995 int i, dval, len, n ;
996 int MPmant[MP_SIZE] ; /* Mantissa. */
997 int ddig ; /* Number of digits in exponent. */
998 int eng = 0 ; /* Set if this is an engineering number. */
999 int exp = 0 ; /* Exponent */
1001 if (v->dtype == ENG) eng = 1 ;
1003 mpabs(MPnumber, MPval) ;
1006 if (mplt(MPnumber, MP1)) *optr++ = '-' ;
1007 mpstr(MPval, MPmant) ;
1009 mpcim(&basevals[(int) v->base], MP1base) ;
1011 mppwr(MP1base, &n, MP3base) ;
1014 mppwr(MP1base, &n, MP10base) ;
1018 mpdiv(MP1, MP10base, MPatmp) ;
1022 if (!mpeq(MPmant, MP1))
1024 while (mpge(MPmant, MP10base))
1027 mpmul(MPmant, MPatmp, MPmant) ;
1030 while ((!eng && mpge(MPmant, MP1base)) ||
1031 (eng && (mpge(MPmant, MP3base) || exp % 3 != 0)))
1034 mpdiv(MPmant, MP1base, MPmant) ;
1037 while (mplt(MPmant, MPatmp))
1040 mpmul(MPmant, MP10base, MPmant) ;
1045 while (mplt(MPmant, MP1) || (eng && exp % 3 != 0))
1048 mpmul(MPmant, MP1base, MPmant) ;
1052 STRCPY(fixed, make_fixed(MPmant, MAX_DIGITS-6)) ;
1053 len = strlen(fixed) ;
1054 for (i = 0; i < len; i++) *optr++ = fixed[i] ;
1063 else *optr++ = '+' ;
1065 MPstr_to_num("0.5", DEC, MP1) ;
1066 mpaddi(MP1, &exp, MPval) ;
1069 for (ddig = 0; mpge(MPval, MP1); ddig++)
1070 mpdiv(MPval, MP1base, MPval) ;
1072 if (ddig == 0) *optr++ = '0' ;
1076 mpmul(MPval, MP1base, MPval) ;
1077 mpcmi(MPval, &dval) ;
1078 *optr++ = digits[dval] ;
1080 mpaddi(MPval, &dval, MPval) ;
1090 make_fixed(MPnumber, cmax) /* Convert MP number to fixed number string. */
1092 int cmax ; /* Maximum characters to generate. */
1095 int MP1base[MP_SIZE], MP1[MP_SIZE], MP2[MP_SIZE], MPval[MP_SIZE] ;
1096 int ndig ; /* Total number of digits to generate. */
1097 int ddig ; /* Number of digits to left of . */
1101 mpabs(MPnumber, MPval) ;
1104 if (mplt(MPnumber, MP1)) *optr++ = '-' ;
1106 mpcim(&basevals[(int) v->base], MP1base) ;
1108 mppwr(MP1base, &v->accuracy, MP1) ;
1109 MPstr_to_num("0.5", DEC, MP2) ;
1110 mpdiv(MP2, MP1, MP1) ;
1111 mpadd(MPval, MP1, MPval) ;
1115 if (mplt(MPval, MP2))
1122 for (ddig = 0; mpge(MPval, MP2); ddig++)
1123 mpdiv(MPval, MP1base, MPval) ;
1125 ndig = MIN(ddig + v->accuracy, --cmax) ;
1129 if (ddig-- == 0) *optr++ = '.' ;
1130 mpmul(MPval, MP1base, MPval) ;
1131 mpcmi(MPval, &dval) ;
1132 *optr++ = digits[dval] ;
1134 mpaddi(MPval, &dval, MPval) ;
1146 set_item(DISPLAYITEM, v->display) ;
1147 set_item(OPITEM, " ") ;
1148 set_item(HYPITEM, " ") ;
1149 set_item(INVITEM, " ") ;
1154 make_number(MPnumber, mkFix) /* Convert MP number to character string. */
1158 double number, val ;
1160 /* NOTE: make_number can currently set v->error when converting to a double.
1161 * This is to provide the same look&feel as V3 even though dtcalc
1162 * now does internal arithmetic to "infinite" precision.
1164 * XXX: Needs to be improved. Shouldn't need to convert to a double in
1165 * order to do these tests.
1168 mpcmd(MPnumber, &number) ;
1169 val = fabs(number) ;
1170 if (v->error) return(vstrs[(int) V_ERROR]) ;
1171 if (v->dtype == ENG || v->dtype == SCI ||
1172 v->dtype == FIX && val != 0.0 && (val > max_fix[(int) v->base]))
1173 return(make_eng_sci(MPnumber)) ;
1174 else if (v->dtype == FIX && val != 0.0 && mkFix)
1176 if(v->accuracy == 0)
1178 if(val <= min_fix0[(int) v->base])
1179 return(make_eng_sci(MPnumber)) ;
1181 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1183 else if(v->accuracy == 1)
1185 if(val <= min_fix1[(int) v->base])
1186 return(make_eng_sci(MPnumber)) ;
1188 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1190 else if(v->accuracy == 2)
1192 if(val <= min_fix2[(int) v->base])
1193 return(make_eng_sci(MPnumber)) ;
1195 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1197 else if(v->accuracy == 3)
1199 if(val <= min_fix3[(int) v->base])
1200 return(make_eng_sci(MPnumber)) ;
1202 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1204 else if(v->accuracy == 4)
1206 if(val <= min_fix4[(int) v->base])
1207 return(make_eng_sci(MPnumber)) ;
1209 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1211 else if(v->accuracy == 5)
1213 if(val <= min_fix5[(int) v->base])
1214 return(make_eng_sci(MPnumber)) ;
1216 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1218 else if(v->accuracy == 6)
1220 if(val <= min_fix6[(int) v->base])
1221 return(make_eng_sci(MPnumber)) ;
1223 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1225 else if(v->accuracy == 7)
1227 if(val <= min_fix7[(int) v->base])
1228 return(make_eng_sci(MPnumber)) ;
1230 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1232 else if(v->accuracy == 8)
1234 if(val <= min_fix8[(int) v->base])
1235 return(make_eng_sci(MPnumber)) ;
1237 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1239 else if(v->accuracy == 9)
1241 if(val <= min_fix9[(int) v->base])
1242 return(make_eng_sci(MPnumber)) ;
1244 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1248 return(make_fixed(MPnumber, MAX_DIGITS)) ;
1254 matherr(exc) /* Default math library exception handling routine. */
1255 struct exception *exc ;
1261 strcpy(msg, exc->name);
1263 if(exc->type == DOMAIN)
1264 strcat(msg, "DOMAIN ");
1265 else if(exc->type == SING)
1266 strcat(msg, "SING ");
1267 else if(exc->type == OVERFLOW)
1268 strcat(msg, "OVERFLOW ");
1269 else if(exc->type == UNDERFLOW)
1270 strcat(msg, "UNDERFLOW ");
1271 else if(exc->type == TLOSS)
1272 strcat(msg, "TLOSS ");
1273 else if(exc->type == PLOSS)
1274 strcat(msg, "PLOSS ");
1276 strcat(msg, vstrs[(int) V_ERROR]);
1278 _DtSimpleError (v->appname, DtWarning, NULL, msg);
1282 doerr(vstrs[(int) V_ERROR]) ;
1283 return(1) ; /* Value ignored. */
1288 MPstr_to_num(str, base, MPval) /* Convert string into an MP number. */
1290 enum base_type base ;
1294 int MP1[MP_SIZE], MP2[MP_SIZE], MPbase[MP_SIZE] ;
1302 mpcim(&basevals[(int) base], MPbase) ;
1304 while (*optr == ' ') optr++ ;
1307 /* negative number */
1311 while ((inum = char_val(*optr)) >= 0)
1313 mpmul(MPval, MPbase, MPval) ;
1314 mpaddi(MPval, &inum, MPval) ;
1319 for (i = 1; (inum = char_val(*++optr)) >= 0; i++)
1321 mppwr(MPbase, &i, MP1) ;
1323 mpdiv(MP2, MP1, MP1) ;
1324 mpadd(MPval, MP1, MPval) ;
1327 while (*optr == ' ') optr++ ;
1331 if (*optr == '-') exp_sign = -1 ;
1333 while ((inum = char_val(*++optr)) >= 0)
1334 exp = exp * basevals[(int) base] + inum ;
1338 if (v->key_exp || exp_sign == -1)
1340 mppwr(MPbase, &exp, MP1) ;
1341 mpmul(MPval, MP1, MPval) ;
1347 mpmul(MPval, MP1, MPval) ;
1353 paren_disp(c) /* Append the latest parenthesis char to the display item. */
1358 /* If the character is a Delete, clear the whole line, and exit parenthesis
1361 * If the character is a Back Space, remove the last character. If the last
1362 * character was a left parenthesis, decrement the parentheses count. If
1363 * the parentheses count is zero, exit parenthesis processing.
1365 * If the character is a control character (not Ctrl-h), then append ^(char).
1367 * Otherwise just append the character.
1370 n = strlen(v->display) ;
1371 if (IS_KEY(c, KEY_CLR)) /* Is it a Delete character? */
1373 v->noparens = v->pending = v->opsptr = v->numsptr = 0 ;
1375 set_item(OPITEM, "") ;
1377 mpcim(&i, v->MPdisp_val) ;
1378 show_display(v->MPdisp_val) ;
1380 set_numtype(v->dtype);
1381 set_option_menu((int) TTYPEITEM, (int)v->ttype);
1383 v->curwin = FCP_KEY ;
1387 else if (IS_KEY(c, KEY_BSP)) /* Is is a Back Space character? */
1390 if (v->display[n-1] == '(')
1395 v->pending = v->opsptr = v->numsptr = 0 ;
1397 set_item(OPITEM, "") ;
1398 show_display(v->MPdisp_val) ;
1402 v->display[n-1] = '\0' ;
1404 else if (c <= CTL('z')) /* Is it a control character? */
1408 v->display[n] = '^' ;
1409 v->display[n+1] = c + 96 ;
1410 v->display[n+2] = '\0' ;
1413 else /* It must be an ordinary character. */
1418 v->display[n+1] = '\0' ;
1422 n = (n < MAX_DIGITS) ? 0 : n - MAX_DIGITS ;
1423 v->show_paren = 1 ; /* Hack to get set_item to really display it. */
1424 set_item(DISPLAYITEM, &v->display[n]) ;
1430 process_event(type) /* Process this event. */
1437 case KEYBOARD_DOWN : if (v->pending)
1439 v->current = v->cur_ch ;
1444 ival = get_index(v->cur_ch) ;
1445 if (ival < TITEMS - EXTRA) {
1446 if ((v->modetype == FINANCIAL &&
1448 (v->modetype == LOGICAL && (
1449 ival == 21 || ival == 22 ||
1453 /* go arm the button */
1454 draw_button(ival, v->curwin, v->row,
1457 process_item(ival) ;
1461 case KEYBOARD_UP : ival = get_index(v->cur_ch) ;
1462 if (ival < TITEMS - EXTRA) {
1463 if ((v->modetype == FINANCIAL &&
1465 (v->modetype == LOGICAL && (
1466 ival == 21 || ival == 22 ||
1470 /* go disarm the button */
1471 draw_button(ival, v->curwin, v->row,
1474 /* go disarm the button */
1475 draw_button(ival, v->curwin, v->row,
1489 if (n < 0 || n >= TITEMS)
1495 v->current = buttons[n].value ;
1496 if (v->current == '*') v->current = 'x' ; /* Reassign "extra" values. */
1497 if (v->current == '\015') v->current = '=' ;
1498 if (v->current == 'Q') v->current = 'q' ;
1502 isvalid = 0 ; /* Must press a valid key first. */
1503 for (i = 0; i < MAXVKEYS; i++)
1504 if (v->current == validkeys[i]) isvalid = 1 ;
1505 if (v->pending == '?') isvalid = 1 ;
1506 if (!isvalid) return ;
1512 if (v->pending_win == FCP_KEY) (*buttons[v->pending_n].func)() ;
1513 else (*mode_buttons[MODEKEYS * ((int) v->pending_mode - 1) +
1514 v->pending_n].func)() ;
1517 switch (buttons[n].opdisp)
1520 if (v->current == 'T')
1522 if (v->modetype == SCIENTIFIC)
1523 set_item(OPITEM, buttons[n].str) ;
1528 set_item(OPITEM, buttons[n].str) ;
1532 set_item(OPITEM, vstrs[(int) V_CLR]) ;
1534 set_item(OPITEM, "") ;
1536 (*buttons[n].func)() ;
1538 if(strcmp(buttons[n].resname, "clr") != 0)
1543 /* Process a portion of the parentheses stack. */
1546 process_stack(startop, startnum, n)
1547 int startop ; /* Initial position in the operand stack. */
1548 int startnum ; /* Initial position in the numeric stack. */
1549 int n ; /* Number of items to process. */
1551 char sdisp[MAXLINE] ; /* Used to save display contents. */
1553 int nptr ; /* Pointer to next number from numeric stack. */
1555 STRCPY(sdisp, v->display) ; /* Save current display. */
1558 v->cur_op = '?' ; /* Current operation is initially undefined. */
1559 for (i = 0; i < n; i++)
1561 if (v->opstack[startop + i] == -1)
1563 mpstr(v->MPnumstack[nptr++], v->MPdisp_val) ;
1567 v->cur_ch = v->opstack[startop + i] ;
1568 if (v->cur_ch == '^') /* Control character? */
1571 v->cur_ch = CTL(v->opstack[startop + i]) ;
1575 v->current = v->cur_ch ;
1578 else process_item(get_index(v->cur_ch)) ;
1581 v->numsptr = startnum ;
1582 push_num(v->MPdisp_val) ;
1583 v->opsptr = startop - 1 ;
1585 save_pending_values(KEY_LPAR) ;
1586 STRCPY(v->display, sdisp) ; /* Restore current display. */
1591 process_str(str, mtype)
1593 enum menu_type mtype ;
1599 STRCPY(save, v->display) ;
1600 STRCPY(v->display, " ") ;
1601 set_item(DISPLAYITEM, v->display);
1602 for (i = 0 ; i < len; i++)
1610 STRCPY(v->display, save);
1611 set_item(DISPLAYITEM, v->display);
1618 v->current = str[i] ;
1637 process_item(get_index(str[i])) ;
1644 read_rcfiles() /* Read .dtcalcrc's from home and current directories. */
1646 char *home ; /* Pathname for users home directory. */
1647 char name[MAXPATHLEN + 50] ; /* Full name of users .dtcalcrc file. */
1648 char pathname[MAXPATHLEN + 5] ; /* Current working directory. */
1649 char tmp[MAXLINE] ; /* For temporary constant string creation. */
1651 struct passwd *entry ;
1653 for (n = 0; n < MAXREGS; n++)
1655 STRCPY(tmp, make_number(v->MPcon_vals[n], FALSE)) ;
1656 SPRINTF(name, "%1d: %s [%s]", n, tmp, v->con_names[n]) ;
1658 STRCPY(v->con_names[n], name) ;
1659 STRCPY(v->fun_vals[n], "") ; /* Initially empty function strings. */
1662 if ((home = getenv("HOME")) == NULL)
1664 if ((entry = getpwuid(getuid())) == NULL) return ;
1665 home = entry->pw_dir ;
1667 SPRINTF(name, "%s/%s", home, RCNAME) ;
1668 get_rcfile(name) ; /* Read .dtcalcrc from users home directory. */
1670 SPRINTF(name, "%s/%s", getcwd(pathname, MAXPATHLEN+1), RCNAME) ;
1671 get_rcfile(name) ; /* Read .dtcalcrc file from current directory. */
1681 STRCPY(v->display, make_number(MPval, TRUE)) ;
1682 set_item(DISPLAYITEM, v->display) ;
1691 FPRINTF(stderr, ustrs[(int) USAGE1], PATCHLEVEL) ;
1692 FPRINTF(stderr, ustrs[(int) USAGE2]) ;
1693 FPRINTF(stderr, ustrs[(int) USAGE3]) ;
1698 write_rcfile(mtype, exists, cfno, val, comment)
1699 enum menu_type mtype ;
1701 char *val, *comment ;
1703 char *home ; /* Pathname for users home directory. */
1704 char pathname[MAXPATHLEN] ; /* Current working directory. */
1705 char rcname[MAXPATHLEN] ; /* Full name of users .dtcalcrc file. */
1706 char str[MAXLINE] ; /* Temporary buffer. */
1707 char sval[3] ; /* Used for string comparisons. */
1708 char tmp_filename[MAXLINE] ; /* Used to construct temp filename. */
1709 int rcexists ; /* Set to 1, if .dtcalcrc file exists. */
1710 FILE *rcfd ; /* File descriptor for .dtcalcrc file. */
1711 FILE *tmpfd ; /* File descriptor for new temp .dtcalcrc. */
1712 struct passwd *entry ; /* The user's /etc/passwd entry. */
1715 SPRINTF(rcname, "%s/%s", getcwd(pathname, MAXPATHLEN), RCNAME) ;
1716 if (access(rcname, F_OK) == 0) rcexists = 1 ;
1719 if ((home = getenv("HOME")) == NULL)
1721 if ((entry = getpwuid(getuid())) == NULL) return ;
1722 home = entry->pw_dir ;
1724 SPRINTF(rcname, "%s/%s", home, RCNAME) ;
1725 if (access(rcname, F_OK) == 0) rcexists = 1 ;
1727 STRCPY(tmp_filename, "/tmp/.dtcalcrcXXXXXX") ;
1728 MKTEMP(tmp_filename) ;
1729 if ((tmpfd = fopen(tmp_filename, "w+")) == NULL) return ;
1733 rcfd = fopen(rcname, "r") ;
1734 SPRINTF(sval, " %1d", cfno) ;
1735 while (fgets(str, MAXLINE, rcfd))
1741 case M_CON : sval[0] = 'c' ;
1742 if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
1744 if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
1746 case M_FUN : sval[0] = 'f' ;
1747 if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
1749 if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
1752 FPRINTF(tmpfd, "%s", str) ;
1759 case M_CON : FPRINTF(tmpfd, "\nC%1d %s %s\n", cfno, val, comment) ;
1762 if(strcmp(val, "") != 0)
1763 FPRINTF(tmpfd, "\nF%1d %s %s\n", cfno, val, comment) ;
1766 rcfd = fopen(rcname, "w") ;
1768 while (fgets(str, MAXLINE, tmpfd)) FPRINTF(rcfd, "%s", str) ;
1771 UNLINK(tmp_filename) ;
1776 write_resources(filename)
1782 int MPtemp[MP_SIZE];
1784 SPRINTF(intval, "%d", v->accuracy) ;
1785 put_resource(R_ACCURACY, intval) ;
1786 put_resource(R_DISPLAYED, v->display) ;
1787 put_resource(R_BASE, base_str[(int) v->base]) ;
1788 put_resource(R_DISPLAY, dtype_str[(int) v->dtype]) ;
1789 put_resource(R_MODE, mode_str[(int) v->modetype]) ;
1790 put_resource(R_TRIG, ttype_str[(int) v->ttype]) ;
1791 put_resource(R_REGS, set_bool(v->rstate == TRUE)) ;
1793 put_resource(R_REG0, make_number(v->MPmvals[0], FALSE)) ;
1794 put_resource(R_REG1, make_number(v->MPmvals[1], FALSE)) ;
1795 put_resource(R_REG2, make_number(v->MPmvals[2], FALSE)) ;
1796 put_resource(R_REG3, make_number(v->MPmvals[3], FALSE)) ;
1797 put_resource(R_REG4, make_number(v->MPmvals[4], FALSE)) ;
1798 put_resource(R_REG5, make_number(v->MPmvals[5], FALSE)) ;
1799 put_resource(R_REG6, make_number(v->MPmvals[6], FALSE)) ;
1800 put_resource(R_REG7, make_number(v->MPmvals[7], FALSE)) ;
1801 put_resource(R_REG8, make_number(v->MPmvals[8], FALSE)) ;
1802 put_resource(R_REG9, make_number(v->MPmvals[9], FALSE)) ;
1804 mpcdm(&(v->MPfvals[0]), MPtemp);
1805 put_resource(R_FREG0, make_number(MPtemp, FALSE)) ;
1806 mpcdm(&(v->MPfvals[1]), MPtemp);
1807 put_resource(R_FREG1, make_number(MPtemp, FALSE)) ;
1808 mpcdm(&(v->MPfvals[2]), MPtemp);
1809 put_resource(R_FREG2, make_number(MPtemp, FALSE)) ;
1810 mpcdm(&(v->MPfvals[3]), MPtemp);
1811 put_resource(R_FREG3, make_number(MPtemp, FALSE)) ;
1812 mpcdm(&(v->MPfvals[4]), MPtemp);
1813 put_resource(R_FREG4, make_number(MPtemp, FALSE)) ;
1814 mpcdm(&(v->MPfvals[5]), MPtemp);
1815 put_resource(R_FREG5, make_number(MPtemp, FALSE)) ;
1817 save_resources(filename) ;