Fix errors from make distcheck
[gcalctool.git] / src / calctool.c
blob1516758274ac484c437643ac1116b25f48b466c1
2 /* $Header$
4 * Copyright (c) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
5 *
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)
9 * any later version.
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
19 * 02111-1307, USA.
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <assert.h>
26 #include <sys/types.h>
27 #include <math.h>
28 #include <glib-object.h>
30 #include "calctool.h"
31 #include "unittest.h"
32 #include "get.h"
33 #include "display.h"
34 #include "functions.h"
35 #include "ui.h"
36 #include "mp.h"
37 #include "register.h"
38 #include "mp-equation.h"
40 time_t time();
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. */
49 void
50 doerr(char *errmes)
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. */
59 /*ARGSUSED*/
60 int
61 matherr(struct exception *exc)
63 char text[MAXLINE];
65 /* Translators: Error displayed to user when the math library reports an
66 * error */
67 snprintf(text, MAXLINE, _("Error in math library function %s"), exc->name);
68 doerr(text);
70 return 1;
73 static void
74 version()
76 /* NOTE: Is not translated so can be easily parsed */
77 fprintf(stderr, "%1$s %2$s\n", v->progname, VERSION);
80 static void
81 solve(const char *equation)
83 int error;
84 MPNumber result;
85 char result_str[MAXLINE];
87 v->base = DEC;
88 v->ttype = MP_DEGREES;
89 v->wordlen = 32;
90 v->accuracy = 9;
92 error = mp_equation_parse(equation, &result);
93 if(error != 0) {
94 fprintf(stderr, "Error %d\n", error);
95 exit(1);
97 else {
98 mp_cast_to_string(&result, basevals[v->base], 9, 1, result_str, MAXLINE);
99 printf("%s\n", result_str);
100 exit(0);
104 static void
105 usage(int show_gtk)
107 /* Translators: Description on how to use gcalctool displayed on command-line */
108 fprintf(stderr,
109 _("Usage:\n"
110 " %s - Perform mathematical calculations"), v->progname);
112 fprintf(stderr,
113 "\n\n");
115 /* Translators: Description on gcalctool command-line help options displayed on command-line */
116 fprintf(stderr,
117 _("Help Options:\n"
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"));
122 fprintf(stderr,
123 "\n\n");
125 if (show_gtk) {
126 /* Translators: Description on gcalctool command-line GTK+ options displayed on command-line */
127 fprintf(stderr,
128 _("GTK+ Options:\n"
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"));
135 fprintf(stderr,
136 "\n\n");
139 /* Translators: Description on gcalctool application options displayed on command-line */
140 fprintf(stderr,
141 _("Application Options:\n"
142 " -u, --unittest Perform unittests\n"
143 " -s, --solve <equation> Solve the given equation"));
144 fprintf(stderr,
145 "\n\n");
148 void
149 get_options(int argc, char *argv[])
151 int i;
152 char *arg;
154 for (i = 1; i < argc; i++) {
155 arg = argv[i];
157 if (strcmp(arg, "-v") == 0 ||
158 strcmp(arg, "--version") == 0 ||
159 strcmp(arg, "-?") == 0) {
160 version();
161 exit(0);
163 else if (strcmp(arg, "-h") == 0 ||
164 strcmp(arg, "--help") == 0) {
165 usage(FALSE);
166 exit(0);
168 else if (strcmp(arg, "--help-all") == 0) {
169 usage(TRUE);
170 exit(0);
172 else if (strcmp(arg, "-s") == 0 ||
173 strcmp(arg, "--solve") == 0) {
174 i++;
175 if (i >= argc) {
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");
179 exit(1);
181 else
182 solve(argv[i]);
184 else if (strcmp(arg, "-u") == 0 ||
185 strcmp(arg, "--unittest") == 0) {
186 unittest();
188 else {
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");
192 usage(FALSE);
193 exit(1);
199 static void
200 init_state(void)
202 int acc, i;
204 acc = MAX_DIGITS + 12; /* MP internal accuracy. */
205 mp_init(acc);
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();
212 v->math_error = 0;
213 v->math_error_text = strdup("");
215 if (get_int_resource(R_ACCURACY, &i))
216 v->accuracy = i;
217 else
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;
229 else
230 v->base = DEC;
232 if (get_enumerated_resource(R_TRIG, Rtstr, &i))
233 v->ttype = (MPAngleUnit) i;
234 else
235 v->ttype = MP_DEGREES;
237 if (get_int_resource(R_WORDLEN, &i))
238 v->wordlen = i;
239 else
240 v->wordlen = 64;
245 main(int argc, char **argv)
247 memset(&calc_state, 0, sizeof(calc_state));
248 v = &calc_state;
250 g_type_init();
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));
261 resources_init();
263 init_state();
264 register_init();
265 display_init(&v->display);
266 ui_init(&argc, &argv);
268 get_options(argc, argv);
270 ui_load();
271 ui_start();
273 return(0);