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
|
/*
* This file contains main() and the startup code for Mathomatic.
* Leave this file out when compiling for the symbolic math library.
*
* Copyright (C) 1987-2006 George Gesslein II.
*
* Output to stderr is only done in this file. The rest of the code
* should not output to stderr.
*
* 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".
*
* Only one POWER operator is allowed per level in the storage format,
* and no other operators may be on that level. Same with the FACTORIAL
* and MODULUS operators.
*
* Any number of TIMES and DIVIDE operators may be on the same
* level, because they are simple multiplicative class operators.
* The same for PLUS and MINUS, because they are additive class operators.
*/
#if !LIBRARY
#include "includes.h"
#include <sys/ioctl.h>
#include <termios.h>
static void usage();
static int set_signals();
#if CYGWIN
/*
* dirname(3) function for Windows.
*/
char *
dirname1(cp)
char *cp;
{
int i;
i = strlen(cp);
while (i >= 0 && cp[i] != '\\' && cp[i] != '/')
i--;
if (i < 0)
return(".");
cp[i] = '\0';
return(cp);
}
#endif
int
main(argc, argv)
int argc;
char **argv;
{
extern char *optarg;
extern int optind;
int i;
char *cp;
double numerator, denominator;
double multiplier;
struct winsize ws;
int coption = false;
#if UNIX
prog_name = strdup(basename(argv[0])); /* set prog_name to this executable's filename */
#endif
#if CYGWIN
dir_path = strdup(dirname1(argv[0])); /* set dir_path to this executable's directory */
#endif
/* initialize the global variables */
init_gvars();
gfp = stdout;
/* process command line options */
while ((i = getopt(argc, argv, "qtchuvm:")) >= 0) {
switch (i) {
case 'c':
coption = true;
break;
case 'h':
readline_enabled = false;
html_flag = true;
break;
case 'm':
multiplier = atof(optarg);
if (multiplier <= 0.0 || (n_tokens = (int) (multiplier * DEFAULT_N_TOKENS)) < 100) {
fprintf(stderr, _("Invalid memory size multiplier specified.\n"));
usage();
}
break;
case 'q':
quiet_mode = true;
break;
case 't':
readline_enabled = false;
test_mode = true;
break;
case 'u':
setbuf(stdout, NULL); /* make standard output unbuffered */
break;
case 'v':
version_report();
exit(0);
default:
usage();
}
}
if (test_mode || html_flag) {
screen_columns = 0;
screen_rows = 0;
} else {
/* get the screen width and height */
ws.ws_col = 0;
ws.ws_row = 0;
if (ioctl(1, TIOCGWINSZ, &ws) >= 0) {
if (ws.ws_col) {
screen_columns = ws.ws_col;
}
if (ws.ws_row) {
screen_rows = ws.ws_row;
}
}
}
if (!init_mem()) {
fprintf(stderr, _("%s: Not enough memory.\n"), prog_name);
exit(2);
}
if (html_flag) {
printf("<pre>\n");
}
if (!test_mode && !quiet_mode) {
#if SECURE
printf(_("Secure "));
#endif
printf(_("Mathomatic version %s (www.mathomatic.org)\n"), VERSION);
printf(_("Copyright (C) 1987-2006 George Gesslein II.\n"));
printf(_("%d equation spaces available, %ldKB per equation space.\n\n"),
N_EQUATIONS, (long) n_tokens * sizeof(token_type) * 2L / 1000L);
}
#if !SECURE
/* read the options initialization file */
if (!test_mode && !load_rc()) {
fprintf(stderr, _("Error loading set options from \"%s\".\n"), RC_FILE);
}
#endif
if (test_mode) {
color_flag = false;
}
if (coption) {
color_flag = !color_flag;
}
if ((i = setjmp(jmp_save)) != 0) {
/* for error handling */
clean_up();
switch (i) {
case 13:
printf(_("Operation abruptly aborted.\n"));
break;
case 14:
error(_("Expression too big."));
default:
printf(_("Operation aborted.\n"));
}
} else {
if (!set_signals()) {
fprintf(stderr, _("signal(2) setting failed.\n"));
}
if (!f_to_fraction(0.5, &numerator, &denominator)
|| !f_to_fraction(1.0/3.0, &numerator, &denominator)) {
fprintf(stderr, _("Cannot convert floating point values to fractions.\n"));
}
#if !SECURE
/* read in files on the command line */
for (i = optind; i < argc; i++) {
if (!read_cmd(argv[i])) {
exit_program(1);
}
}
#endif
}
/* main input/output loop */
for (;;) {
default_color();
snprintf(prompt_str, sizeof(prompt_str), "%d%s", cur_equation + 1, html_flag ? HTML_PROMPT : PROMPT);
if ((cp = get_string((char *) tlhs, n_tokens * sizeof(token_type))) == NULL)
break;
process(cp);
}
exit_program(0);
}
/*
* Display invocation usage info and exit with error.
*/
static void
usage()
{
fprintf(stderr, _("Mathomatic version %s - automatic algebraic manipulator\n\n"), VERSION);
fprintf(stderr, _("Usage: %s [ options ] [ input_files ]\n\n"), prog_name);
fprintf(stderr, _("Options:\n"));
fprintf(stderr, _(" -c Toggle color mode.\n"));
fprintf(stderr, _(" -h Enable HTML mode and disable readline input.\n"));
fprintf(stderr, _(" -m number Specify a memory size multiplier.\n"));
fprintf(stderr, _(" -q Quiet mode (don't display prompts).\n"));
fprintf(stderr, _(" -t Used when testing.\n"));
fprintf(stderr, _(" -u Unbuffered output.\n"));
fprintf(stderr, _(" -v Display version number and compile flags used.\n"));
exit(1);
}
/*
* All signal(2) initialization goes here.
*
* Return true on success.
*/
static int
set_signals()
{
int rv = true;
if (signal(SIGFPE, fphandler) == SIG_ERR)
rv = false;
if (signal(SIGINT, inthandler) == SIG_ERR)
rv = false;
if (signal(SIGWINCH, resizehandler) == SIG_ERR)
rv = false;
#if TIMEOUT_SECONDS
if (signal(SIGALRM, alarmhandler) == SIG_ERR)
rv = false;
alarm(TIMEOUT_SECONDS);
#endif
return rv;
}
#if !SECURE
/*
* Load set options from "~/.mathomaticrc".
*
* Return false if error encountered.
*/
int
load_rc()
{
FILE *fp;
char buf[MAX_CMD_LEN];
char *cp;
int rv = true;
#if CYGWIN
snprintf(buf, sizeof(buf), "%s/%s", dir_path, RC_FILE);
fp = fopen(buf, "r");
if (fp == NULL) {
return true;
}
#else
cp = getenv("HOME");
if (cp == NULL)
return true;
snprintf(buf, sizeof(buf), "%s/%s", cp, RC_FILE);
fp = fopen(buf, "r");
if (fp == NULL)
return true;
#endif
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.
* Seldom works.
*/
void
fphandler(sig)
int sig;
{
error(_("Floating point exception."));
signal(SIGFPE, fphandler);
longjmp(jmp_save, 2);
}
/*
* Control-C handler.
* Abruptly quits this program.
*/
void
inthandler(sig)
int sig;
{
exit_program(1);
}
#if TIMEOUT_SECONDS
/*
* Alarm signal handler.
*/
void
alarmhandler(sig)
int sig;
{
printf(_("\ntimeout\n"));
exit_program(1);
}
#endif
/*
* Window resize handler.
*/
void
resizehandler(sig)
int sig;
{
struct winsize ws;
ws.ws_col = 0;
ws.ws_row = 0;
if (ioctl(1, TIOCGWINSZ, &ws) >= 0) {
if (ws.ws_col && screen_columns) {
screen_columns = ws.ws_col;
}
if (ws.ws_row && screen_rows) {
screen_rows = ws.ws_row;
}
}
}
/*
* Exit this program and return to the Operating System.
*/
void
exit_program(exit_value)
int exit_value;
{
reset_attr();
printf("\n");
if (html_flag) {
printf("</pre>\n");
}
exit(exit_value);
}
#endif
|