4 * Copyright (c) 1987-2008 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>
28 #include <glib-object.h>
34 #include "functions.h"
38 #include "mp-equation.h"
42 int basevals
[4] = { 2, 8, 10, 16 };
44 /* Calctool variables and options. */
45 static CalculatorVariables calc_state
;
46 CalculatorVariables
*v
;
48 /* Calctools' customised math library error-handling routine. */
52 v
->math_error
= -MPMATH_ERR
;
53 free(v
->math_error_text
);
54 v
->math_error_text
= strdup(errmes
);
57 /* Default math library exception handling routine. */
61 matherr(struct exception
*exc
)
65 /* Translators: Error displayed to user when the math library reports an
67 snprintf(text
, MAXLINE
, _("Error in math library function %s"), exc
->name
);
76 /* NOTE: Is not translated so can be easily parsed */
77 fprintf(stderr
, "%1$s %2$s\n", v
->progname
, VERSION
);
81 solve(const char *equation
)
85 char result_str
[MAXLINE
];
88 v
->ttype
= MP_DEGREES
;
92 error
= mp_equation_parse(equation
, &result
);
94 fprintf(stderr
, "Error %d\n", error
);
98 mp_cast_to_string(&result
, basevals
[v
->base
], 9, 1, result_str
, MAXLINE
);
99 printf("%s\n", result_str
);
107 /* Translators: Description on how to use gcalctool displayed on command-line */
110 " %s - Perform mathematical calculations"), v
->progname
);
115 /* Translators: Description on gcalctool command-line help options displayed on command-line */
118 " -v, --version Show release version\n"
119 " -h, -?, --help Show help options\n"
120 " --help-all Show all help options\n"
121 " --help-gtk Show GTK+ options"));
126 /* Translators: Description on gcalctool command-line GTK+ options displayed on command-line */
129 " --class=CLASS Program class as used by the window manager\n"
130 " --name=NAME Program name as used by the window manager\n"
131 " --screen=SCREEN X screen to use\n"
132 " --sync Make X calls synchronous\n"
133 " --gtk-module=MODULES Load additional GTK+ modules\n"
134 " --g-fatal-warnings Make all warnings fatal"));
139 /* Translators: Description on gcalctool application options displayed on command-line */
141 _("Application Options:\n"
142 " -u, --unittest Perform unittests\n"
143 " -s, --solve <equation> Solve the given equation"));
149 get_options(int argc
, char *argv
[])
154 for (i
= 1; i
< argc
; i
++) {
157 if (strcmp(arg
, "-v") == 0 ||
158 strcmp(arg
, "--version") == 0 ||
159 strcmp(arg
, "-?") == 0) {
163 else if (strcmp(arg
, "-h") == 0 ||
164 strcmp(arg
, "--help") == 0) {
168 else if (strcmp(arg
, "--help-all") == 0) {
172 else if (strcmp(arg
, "-s") == 0 ||
173 strcmp(arg
, "--solve") == 0) {
176 /* Translators: Error printed to stderr when user uses --solve argument without an equation */
177 fprintf(stderr
, _("Argument --solve requires an equation to solve"));
178 fprintf(stderr
, "\n");
184 else if (strcmp(arg
, "-u") == 0 ||
185 strcmp(arg
, "--unittest") == 0) {
189 /* Translators: Error printed to stderr when user provides an unknown command-line argument */
190 fprintf(stderr
, _("Unknown argument '%s'"), arg
);
191 fprintf(stderr
, "\n");
204 acc
= MAX_DIGITS
+ 12; /* MP internal accuracy. */
207 v
->error
= FALSE
; /* No calculator error initially. */
208 v
->radix
= get_radix(); /* Locale specific radix string. */
209 v
->tsep
= get_tsep(); /* Locale specific thousands separator. */
210 v
->tsep_count
= get_tsep_count();
213 v
->math_error_text
= strdup("");
215 if (get_int_resource(R_ACCURACY
, &i
))
218 v
->accuracy
= DEFAULT_ACCURACY
;
219 if (v
->accuracy
< 0 || v
->accuracy
> MAXACC
) {
220 /* Translators: A log message displayed when an invalid accuracy
221 is read from the configuration */
222 fprintf(stderr
, _("%s: accuracy should be in the range 0-%d\n"),
223 v
->progname
, MAXACC
);
224 v
->accuracy
= DEFAULT_ACCURACY
;
227 if (get_enumerated_resource(R_BASE
, Rbstr
, &i
))
228 v
->base
= (BaseType
) i
;
232 if (get_enumerated_resource(R_TRIG
, Rtstr
, &i
))
233 v
->ttype
= (MPAngleUnit
) i
;
235 v
->ttype
= MP_DEGREES
;
237 if (get_int_resource(R_WORDLEN
, &i
))
245 main(int argc
, char **argv
)
247 memset(&calc_state
, 0, sizeof(calc_state
));
252 bindtextdomain(GETTEXT_PACKAGE
, PACKAGE_LOCALE_DIR
);
253 bind_textdomain_codeset(GETTEXT_PACKAGE
, "UTF-8");
254 textdomain(GETTEXT_PACKAGE
);
256 v
->progname
= g_path_get_basename(argv
[0]);
258 /* Seed random number generator. */
259 srand48((long) time((time_t *) 0));
265 display_init(&v
->display
);
266 ui_init(&argc
, &argv
);
268 get_options(argc
, argv
);