File: main.c

package info (click to toggle)
mathomatic 15.1.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,368 kB
  • ctags: 783
  • sloc: ansic: 17,612; makefile: 230; sh: 133; python: 96
file content (504 lines) | stat: -rw-r--r-- 13,781 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 * This file contains main() and the startup code for Mathomatic,
 * which is a small computer algebra system written entirely in C.
 *
 * Copyright (C) 1987-2010 George Gesslein II.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public 
    License as published by the Free Software Foundation; either 
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public 
    License along with this library; if not, write to the Free Software 
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 */

/*
 * Output to stderr is only done in this file.  The rest of the Mathomatic code
 * should not output to stderr; error messages should use error() or go to stdout.
 *
 * This program only supports binary and unary operators.
 * Unary operators are implemented as a binary operation with a dummy operand.
 *
 * In the storage format, each level of parentheses is indicated
 * by a level number (origin 1).  The deeper the level, the
 * higher the level number.
 *
 * The storage format for expressions is a fixed size array of elements
 * "token_type", which may be a CONSTANT, VARIABLE, or OPERATOR.
 * The array always alternates between operand (CONSTANT or VARIABLE)
 * and OPERATOR.  There is a separate integer for each array which
 * contains the current length of the expression stored in the array.
 * This length is always odd and must never exceed "n_tokens".
 *
 * In the storage format,
 * any number of TIMES and DIVIDE operators may be on the same level of parentheses,
 * because they are similar and the most basic multiplicative class operators.
 * The same for the PLUS and MINUS operators, because they are similar (additive class).
 * All other operators are only allowed one single operator per level of parentheses,
 * and no same nor different operators may be with it on that level within the current grouping.
 *
 * Most of the expression manipulation and comparison routines are recursive,
 * calling themselves for each level of parentheses.
 *
 * Note that equation space numbers internally are 1 less than the equation
 * space numbers displayed.  That is because internal equation space numbers
 * are origin 0 array indexes, while displayed equation numbers are origin 1.
 *
 * See the file "am.h" to start understanding the Mathomatic code and
 * to adjust memory usage.
 */

#if	!LIBRARY	/* This comments out this whole file if using as symbolic math library. */

#include "includes.h"
#if	TRAP_FPERRORS	/* Do not use this define!  Currently does not work. */
#include <fenv.h>
#endif

/*
 * Display invocation usage info.
 */
void
usage()
{
	printf(_("\nMathomatic computer algebra system version %s\n\n"), VERSION);
	printf(_("Usage: %s [ options ] [ input_files ]\n\n"), prog_name);
	printf(_("Options:\n"));
	printf(_("  -b            Enable bold color mode.\n"));
	printf(_("  -c            Toggle color mode.\n"));
	printf(_("  -h            Display this help and exit.\n"));
	printf(_("  -m number     Specify a memory size multiplier.\n"));
	printf(_("  -q            Set quiet mode (don't display prompts).\n"));
	printf(_("  -r            Disable readline.\n"));
	printf(_("  -s level      Set enforced security level for session.\n"));
	printf(_("  -t            Set test mode.\n"));
	printf(_("  -u            Set unbuffered output.\n"));
	printf(_("  -v            Display version information, then exit.\n"));
	printf(_("  -w            Wide output mode, sets unlimited width.\n"));
	printf(_("  -x            Enable HTML/XHTML output mode.\n"));
	printf(_("\nPlease refer to the Mathomatic man page for more information.\n"));
	printf(_("The man page is viewed by typing \"man mathomatic\" in shell.\n"));
}

int
main(argc, argv)
int	argc;
char	**argv;
{
	extern char	*optarg;	/* set by getopt(3) */
	extern int	optind;

	int		i;
	char		*cp = NULL;
	double		numerator, denominator;
	double		new_size;
	int		coption = false, boption = false, wide_flag = false;

#if	I18N
	/* Initialize internationalization so that messages are in the right language. */
	setlocale(LC_ALL, "");
	textdomain(prog_name);
	bindtextdomain(prog_name, LOCALEDIR);
#endif

#if	CYGWIN
	dir_path = strdup(dirname_win(argv[0]));	/* set dir_path to this executable's directory */
#endif
	/* initialize the global variables */
	init_gvars();
	default_out = stdout;	/* set default_out to any file you want to redirect output to */
	gfp = default_out;

	/* process command line options */
	while ((i = getopt(argc, argv, "s:bqrtchuvwxm:")) >= 0) {
		switch (i) {
		case 's':
#if	SECURE
			fprintf(stderr, _("%s: Compiled for maximum security, therefore no need for setting security level.\n"), prog_name);
#else
			security_level = atoi(optarg);
#endif
			break;
		case 'w':
			wide_flag = true;
			break;
		case 'b':
			boption = true;
			break;
		case 'c':
			coption++;
			break;
		case 'x':
			html_flag = true;
			wide_flag = true;
			break;
		case 'm':
			new_size = strtod(optarg, &cp) * DEFAULT_N_TOKENS;
			if (cp == NULL || *cp || new_size <= 0 || new_size >= (INT_MAX / 3)) {
				fprintf(stderr, _("%s: Invalid memory size multiplier specified.\n"), prog_name);
				exit(2);
			}
			n_tokens = (int) new_size;
			break;
		case 'q':
			quiet_mode = true;
			break;
		case 'r':
			readline_enabled = false;
			break;
		case 't':
			readline_enabled = false;
			wide_flag = true;
			test_mode = true;
			break;
		case 'u':
			setbuf(stdout, NULL);	/* make output unbuffered */
			setbuf(stderr, NULL);
			break;
		case 'h':
			usage();
			exit(0);
		case 'v':
			version_report();
			exit(0);
		default:
			usage();
			exit(2);
		}
	}
	if (n_tokens < 100) {
		fprintf(stderr, _("%s: Expression array size too small.\n"), prog_name);
		exit(2);
	}
	if (!init_mem()) {
		fprintf(stderr, _("%s: Not enough memory.\n"), prog_name);
		exit(2);
	}
#if	READLINE
	if (readline_enabled) {	/* readline_enabled flag must not change after this */
		if ((cp = getenv("HOME")) && prog_name) {
			snprintf(history_filename_storage, sizeof(history_filename_storage), "%s/.%s_history", cp, prog_name);
			history_filename = history_filename_storage;
		}
		rl_initialize();		/* initialize readline */
		using_history();		/* initialize readline history */
		stifle_history(500);		/* maximum of 500 entries */
		rl_inhibit_completion = true;	/* turn off readline file name completion */
#if	!NO_COLOR && !LIBRARY	/* not 100% tested and this might confuse the user with the -c toggle */
		if (!html_flag) {		/* If doing ANSI color: */
			color_flag = (tigetnum("colors") >= 8);	/* autoset color output mode. */
		}
#endif
#if	!SECURE
		read_history(history_filename);	/* restore readline history of previous session */
#endif
	}
#endif
#if	UNIX || CYGWIN
	get_screen_size();
#endif
	if (html_flag) {
		printf("<pre>\n");
	}
	if (!test_mode && !quiet_mode) {
#if	SECURE
		printf(_("Secure "));
#else
		if (security_level >= 2)
			printf(_("Secure "));
#endif
		printf("Mathomatic version %s (www.mathomatic.org)\n", VERSION);
		printf("Copyright (C) 1987-2010 George Gesslein II.\n");
		printf(_("%d equation spaces available, %ld kilobytes per equation space.\n"),
		    N_EQUATIONS, (long) n_tokens * sizeof(token_type) * 2L / 1000L);
	}
#if	!SECURE
	/* read the user options initialization file */
	if (!test_mode && !load_rc()) {
		fprintf(stderr, _("%s: Error loading set options from \"%s\".\n"), prog_name, rc_file);
	}
#endif
	if (wide_flag) {
		screen_columns = 0;
		screen_rows = 0;
	}
	if (coption & 1) {
		color_flag = !color_flag;
	}
	if (boption) {
		color_flag = true;
		bold_colors = true;
	}
	if (test_mode) {
		color_flag = false;
	}
	if (!quiet_mode && color_flag) {
		printf(_("%s%s color mode enabled"), html_flag ? "HTML" : "ANSI", bold_colors ? " bold" : "");
		if (!boption) {
			printf(_("; disable with the -c option or \"set no color\".\n"));
		} else {
			printf(".\n");
		}
	}
	if ((i = setjmp(jmp_save)) != 0) {
		/* for error handling */
		clean_up();
		switch (i) {
		case 14:
			error(_("Expression too large."));
		default:
			printf(_("Operation aborted.\n"));
			break;
		}
	} else {
		if (!set_signals()) {
			fprintf(stderr, _("signal(2) setting failed.\n"));
		}
		if (!f_to_fraction(0.5, &numerator, &denominator)
		    || numerator != 1.0 || denominator != 2.0
		    || !f_to_fraction(1.0/3.0, &numerator, &denominator)
		    || numerator != 1.0 || denominator != 3.0) {
			fprintf(stderr, _("%s: Cannot convert any floating point values to fractions.\n"), prog_name);
			fprintf(stderr, _("Roots will not work properly.\n"));
		}
#if	!SECURE
		/* read in files specified on the command line, exit if error */
		for (i = optind; i < argc && argv[i]; i++) {
			if (strcmp(argv[i], "-") == 0) {
				main_io_loop();
			} else if (!read_file(argv[i])) {
				fprintf(stderr, _("Read of file \"%s\" failed.\n"), argv[i]);
				exit_program(1);
			}
		}
#endif
	}
	main_io_loop();		/* main input/output loop */
	exit_program(0);	/* exit Mathomatic, doesn't return */
	return(0);		/* so the compiler doesn't complain */
}

/*
 * Repeatedly read a line of text from standard input and process the expression or command.
 */
void
main_io_loop()
{
	char	*cp = NULL;

	for (;;) {
		default_color();
		snprintf(prompt_str, sizeof(prompt_str), "%d%s", cur_equation + 1, html_flag ? HTML_PROMPT_STR : PROMPT_STR);
		if ((cp = get_string((char *) tlhs, n_tokens * sizeof(token_type))) == NULL)
			break;
		process(cp);
	}
}

/*
 * All signal(2) initialization goes here.
 *
 * Return true on success.
 */
int
set_signals()
{
	int	rv = true;

	if (signal(SIGFPE, fphandler) == SIG_ERR)
		rv = false;
#if	TRAP_FPERRORS
	if (feenableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW) < 0)
		rv = false;
#endif
	if (signal(SIGINT, inthandler) == SIG_ERR)
		rv = false;
	if (signal(SIGQUIT, inthandler) == SIG_ERR)
		rv = false;
	if (signal(SIGTERM, exithandler) == SIG_ERR)
		rv = false;
	if (signal(SIGHUP, exithandler) == SIG_ERR)
		rv = false;
#if	UNIX || CYGWIN
	if (signal(SIGWINCH, resizehandler) == SIG_ERR)
		rv = false;
#endif
#if	TIMEOUT_SECONDS
	if (signal(SIGALRM, alarmhandler) == SIG_ERR)
		rv = false;
	alarm(TIMEOUT_SECONDS);
#endif
	return rv;
}

#if	CYGWIN
/*
 * dirname(3) function for Microsoft Windows.
 * dirname(3) strips the non-directory suffix from a filename.
 */
char *
dirname_win(cp)
char	*cp;	/* string containing filename to modify */
{
	int	i;

	if (cp == NULL)
		return(".");
	i = strlen(cp);
	while (i >= 0 && cp[i] != '\\' && cp[i] != '/')
		i--;
	if (i < 0)
		return(".");
	cp[i] = '\0';
	return(cp);
}
#endif

#if	!SECURE
/*
 * Load set options from startup file "~/.mathomaticrc".
 *
 * Return false if there was an error reading the startup file,
 * otherwise return true.
 */
int
load_rc()
{
	FILE	*fp = NULL;
	char	buf[MAX_CMD_LEN];
	char	*cp;
	int	rv = true;

	cp = getenv("HOME");
	if (cp) {
		snprintf(rc_file, sizeof(rc_file), "%s/%s", cp, ".mathomaticrc");
		fp = fopen(rc_file, "r");
	}
#if	CYGWIN
	if (fp == NULL) {
		snprintf(rc_file, sizeof(rc_file), "%s/%s", dir_path, "mathomatic.rc");
		fp = fopen(rc_file, "r");
	}
#endif
	if (fp == NULL) {
		return true;
	}
	while ((cp = fgets(buf, sizeof(buf), fp)) != NULL) {
		set_error_level(cp);
		if (!set_options(cp))
			rv = false;
	}
	fclose(fp);
	return rv;
}
#endif

/*
 * Floating point exception handler.
 * Floating point exceptions are currently ignored.
 *
 * I do not know the proper function calls that should be here to make the TRAP_FPERRORS define work.
 * Currently it goes in an endless loop when an FP exception occurs and TRAP_FPERRORS is true,
 * because when this function returns, the exception immediately happens again.
 * Therefore, do not define TRAP_FPERRORS!
 */
void
fphandler(sig)
int	sig;
{
#if	TRAP_FPERRORS
	warning(_("Floating point exception."));
#endif
}

/*
 * Fancy Control-C (interrupt) signal handler.
 * Interrupts processing and returns to main prompt through a polling mechanism.
 * If it can't, repeated calls terminate this program.
 */
void
inthandler(sig)
int	sig;
{
	abort_flag++;
	switch (abort_flag) {
	case 0:
	case 1:
		/* wait for graceful abort */
		printf(_("\nUser interrupt signal received; three times quits Mathomatic.\n"));
		return;
	case 2:
		printf(_("\nPress Control-C once more to quit program.\n"));
		return;
	default:
		/* abruptly quit this program */
		printf(_("\nRepeatedly interrupted; returning to operating system...\n"));
		exit_program(1);
	}
}

/*
 * Alarm signal handler.
 */
void
alarmhandler(sig)
int	sig;
{
	printf(_("\nTimeout, quitting...\n"));
	exit_program(1);
}

/*
 * Signal handler for proper exiting to the operating system.
 */
void
exithandler(sig)
int	sig;
{
	exit_program(1);
}

#if	UNIX || CYGWIN
/*
 * Window resize signal handler.
 */
void
resizehandler(sig)
int	sig;
{
	if (screen_columns)
		get_screen_size();
}
#endif

/*
 * Properly exit this program and return to the operating system.
 */
void
exit_program(exit_value)
int	exit_value;	/* zero if OK, non-zero indicates error return */
{
	reset_attr();
	if (html_flag) {
		printf("</pre>\n");
	}
#if	READLINE && !SECURE
	if (readline_enabled) {
		if (write_history(history_filename)) {	/* save readline history */
			fprintf(stderr, _("%s: Unable to save readline history into file \"%s\".\n"), prog_name, history_filename);
		}
	}
#endif
	if (exit_value == 0 && !quiet_mode && !html_flag)
		printf(_("Thank you for using Mathomatic!\n"));
	exit(exit_value);
}
#endif