4 * Copyright (c) 1987-2007 Sun Microsystems, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include <sys/types.h>
31 #include "functions.h"
36 int basevals
[4] = { 2, 8, 10, 16 };
38 /* Calctool variables and options. */
41 // FIXME: Sort this list
42 /* Note that none of these strings can be translated as the parser expects them to be correct */
43 struct button buttons
[NKEYS
] = {
358 KEY_NATURAL_LOGARITHM
,
528 /* Calctools' customised math library error-handling routine. */
539 strncpy(v
->display
, errmes
, MAXLINE
- 1);
540 v
->display
[MAXLINE
- 1] = '\0';
541 ui_set_error_state(TRUE
);
542 ui_set_display(v
->display
, FALSE
);
547 v
->math_error
= -MPMATH_ERR
;
556 init_text() /* Setup constant strings. */
558 STRNCPY(v
->con_names
[0], _("Kilometer-to-mile conversion factor"),
560 STRNCPY(v
->con_names
[1], _("square root of 2"), MAXLINE
- 1);
561 STRNCPY(v
->con_names
[2], _("e"), MAXLINE
- 1);
562 STRNCPY(v
->con_names
[3], _("pi"), MAXLINE
- 1);
563 STRNCPY(v
->con_names
[4], _("Centimeter-to-inch conversion factor"),
565 STRNCPY(v
->con_names
[5], _("degrees in a radian"), MAXLINE
- 1);
566 STRNCPY(v
->con_names
[6], _("2 ^ 20"), MAXLINE
- 1);
567 STRNCPY(v
->con_names
[7], _("Gram-to-ounce conversion factor"), MAXLINE
- 1);
568 STRNCPY(v
->con_names
[8],
569 _("Kilojoule-to-British-thermal-unit conversion factor"),
571 STRNCPY(v
->con_names
[9],
572 _("Cubic-centimeter-to-cubic-inch conversion factor"), MAXLINE
- 1);
576 /* Default math library exception handling routine. */
581 struct exception
*exc
;
590 getparam(char *s
, char *argv
[], char *errmes
)
592 if (*argv
!= NULL
&& argv
[0][0] != '-') {
593 STRNCPY(s
, *argv
, MAXLINE
- 1);
595 FPRINTF(stderr
, _("%s: %s as next argument.\n"), v
->progname
, errmes
);
602 usage(char *progname
)
604 FPRINTF(stderr
, _("%s version %s\n\n"), progname
, VERSION
);
605 FPRINTF(stderr
, _("Usage: %s: [-D] [-E] [-a accuracy] "), progname
);
606 FPRINTF(stderr
, _("\t\t [-?] [-v] [-h]\n"));
610 #define INC { argc--; argv++; }
613 get_options(int argc
, char *argv
[]) /* Extract command line options. */
615 char next
[MAXLINE
]; /* The next command line parameter. */
619 if (argv
[0][0] == '-') {
620 switch (argv
[0][1]) {
621 case 'D' : /* MP debug info. to stderr. */
625 case 'E' : /* MP errors to stderr. */
631 getparam(next
, argv
, _("-a needs accuracy value"));
632 v
->accuracy
= atoi(next
);
633 if (v
->accuracy
< 0 || v
->accuracy
> MAXACC
) {
635 _("%s: accuracy should be in the range 0-%d\n"),
636 v
->progname
, MAXACC
);
642 if (strcmp(argv
[0], "-name") == 0)
645 read_str(&v
->appname
, argv
[0]);
664 init_constant(int n
, gchar
*value
)
666 gchar
*str
= g_strdup(value
);
668 MPstr_to_num(str
, DEC
, v
->MPcon_vals
[n
]);
678 v
->ghost_zero
= 1; /* Display initially has empty content. */
679 v
->accuracy
= 9; /* Initial accuracy. */
680 v
->show_zeroes
= FALSE
; /* Don't show trailing zeroes. */
681 v
->base
= DEC
; /* Initial base. */
682 v
->dtype
= FIX
; /* Initial number display mode. */
683 v
->ttype
= DEG
; /* Initial trigonometric type. */
684 v
->modetype
= BASIC
; /* Initial calculator mode. */
685 v
->MPdebug
= FALSE
; /* No debug info by default. */
686 v
->MPerrors
= FALSE
; /* No error information. */
687 acc
= MAX_DIGITS
+ 12; /* MP internal accuracy. */
689 mpset(&acc
, &size
, &size
);
691 v
->error
= 0; /* No calculator error initially. */
692 v
->key_exp
= 0; /* Not entering an exponent number. */
694 v
->current
= KEY_CALCULATE
;
695 v
->shelf
= NULL
; /* No selection for shelf initially. */
696 v
->noparens
= 0; /* No unmatched brackets initially. */
697 v
->numsptr
= 0; /* Nothing on the parenthese numeric stack. */
698 v
->warn_change_mode
= 1; /* Warn user when changing modes. */
700 init_constant(0, "0.621"); /* kms/hr <=> miles/hr. */
701 init_constant(1, "1.4142135623"); /* square root of 2 */
702 init_constant(2, "2.7182818284"); /* e */
703 init_constant(3, "3.1415926536"); /* pi */
704 init_constant(4, "0.3937007"); /* cms <=> inch. */
705 init_constant(5, "57.295779513"); /* degrees/radian. */
706 init_constant(6, "1048576.0"); /* 2 ^ 20. */
707 init_constant(7, "0.0353"); /* grams <=> ounce. */
708 init_constant(8, "0.948"); /* Kjoules <=> BTU's. */
709 init_constant(9, "0.0610"); /* cms3 <=> inches3. */
712 for (i
= 0; i
< MAXREGS
; i
++) {
713 mpcim(&n
, v
->MPmvals
[i
]);
719 main(int argc
, char **argv
)
723 v
= (Vars
) LINT_CAST(calloc(1, sizeof(struct calcVars
)));
725 bindtextdomain(GETTEXT_PACKAGE
, PACKAGE_LOCALE_DIR
);
726 bind_textdomain_codeset(GETTEXT_PACKAGE
, "UTF-8");
727 textdomain(GETTEXT_PACKAGE
);
729 v
->progname
= argv
[0]; /* Save programs name. */
731 if ((ptr
= strrchr(argv
[0], '/')) != NULL
) {
732 read_str(&v
->appname
, ptr
+1);
734 read_str(&v
->appname
, argv
[0]);
739 get_options(argc
, argv
); /* Get command line arguments. */
740 ui_init(&argc
, &argv
); /* Initialise UI */
741 resources_init(); /* Initialise configuration */
743 v
->radix
= get_radix(); /* Locale specific radix string. */
744 v
->tsep
= get_tsep(); /* Locale specific thousands seperator. */
746 init_text(); /* Setup text strings depending upon language. */
747 read_resources(); /* Read resources from merged database. */
750 srand48((long) time((time_t *) 0)); /* Seed random number generator. */
752 do_clear(); /* Initialise and clear display. */
754 show_display(v
->MPdisp_val
); /* Output in correct display mode. */
756 memset(&(v
->h
), 0, sizeof(struct exprm_state_history
)); /* clear expression mode state history*/
758 ui_start(); /* Display the calculator. */