int main (int argc, char **argv) { struct expr *e; int i; mpz_t r; int errcode = 0; char *str; int base = 10; setup_error_handler (); gmp_randinit (rstate, GMP_RAND_ALG_LC, 128); { #if HAVE_GETTIMEOFDAY struct timeval tv; gettimeofday (&tv, NULL); gmp_randseed_ui (rstate, tv.tv_sec + tv.tv_usec); #else time_t t; time (&t); gmp_randseed_ui (rstate, t); #endif } mpz_init (r); while (argc > 1 && argv[1][0] == '-') { char *arg = argv[1]; if (arg[1] >= '0' && arg[1] <= '9') break; if (arg[1] == 't') print_timing = 1; else if (arg[1] == 'b' && arg[2] >= '0' && arg[2] <= '9') { base = atoi (arg + 2); if (base < 2 || base > 62) { fprintf (stderr, "error: invalid output base\n"); exit (-1); } } else if (arg[1] == 'b' && arg[2] == 0) base = 2; else if (arg[1] == 'x' && arg[2] == 0) base = 16; else if (arg[1] == 'X' && arg[2] == 0) base = -16; else if (arg[1] == 'o' && arg[2] == 0) base = 8; else if (arg[1] == 'd' && arg[2] == 0) base = 10; else if (arg[1] == 'v' && arg[2] == 0) { printf ("pexpr linked to gmp %s\n", __gmp_version); } else if (strcmp (arg, "-html") == 0) { flag_html = 1; newline = "<br>"; } else if (strcmp (arg, "-wml") == 0) { flag_wml = 1; newline = "<br/>"; } else if (strcmp (arg, "-split") == 0) { flag_splitup_output = 1; } else if (strcmp (arg, "-noprint") == 0) { flag_print = 0; } else { fprintf (stderr, "error: unknown option `%s'\n", arg); exit (-1); } argv++; argc--; } for (i = 1; i < argc; i++) { int s; int jmpval; /* Set up error handler for parsing expression. */ jmpval = setjmp (errjmpbuf); if (jmpval != 0) { fprintf (stderr, "error: %s%s\n", error, newline); fprintf (stderr, " %s%s\n", argv[i], newline); if (! flag_html) { /* ??? Dunno how to align expression position with arrow in HTML ??? */ fprintf (stderr, " "); for (s = jmpval - (long) argv[i]; --s >= 0; ) putc (' ', stderr); fprintf (stderr, "^\n"); } errcode |= 1; continue; } str = expr (argv[i], &e); if (str[0] != 0) { fprintf (stderr, "error: garbage where end of expression expected%s\n", newline); fprintf (stderr, " %s%s\n", argv[i], newline); if (! flag_html) { /* ??? Dunno how to align expression position with arrow in HTML ??? */ fprintf (stderr, " "); for (s = str - argv[i]; --s; ) putc (' ', stderr); fprintf (stderr, "^\n"); } errcode |= 1; free_expr (e); continue; } /* Set up error handler for evaluating expression. */ if (setjmp (errjmpbuf)) { fprintf (stderr, "error: %s%s\n", error, newline); fprintf (stderr, " %s%s\n", argv[i], newline); if (! flag_html) { /* ??? Dunno how to align expression position with arrow in HTML ??? */ fprintf (stderr, " "); for (s = str - argv[i]; --s >= 0; ) putc (' ', stderr); fprintf (stderr, "^\n"); } errcode |= 2; continue; } if (print_timing) { int t; TIME (t, mpz_eval_expr (r, e)); printf ("computation took %d ms%s\n", t, newline); } else mpz_eval_expr (r, e); if (flag_print) { size_t out_len; char *tmp, *s; out_len = mpz_sizeinbase (r, base >= 0 ? base : -base) + 2; #ifdef LIMIT_RESOURCE_USAGE if (out_len > 100000) { printf ("result is about %ld digits, not printing it%s\n", (long) out_len - 3, newline); exit (-2); } #endif tmp = malloc (out_len); if (print_timing) { int t; printf ("output conversion "); TIME (t, mpz_get_str (tmp, base, r)); printf ("took %d ms%s\n", t, newline); } else mpz_get_str (tmp, base, r); out_len = strlen (tmp); if (flag_splitup_output) { for (s = tmp; out_len > 80; s += 80) { fwrite (s, 1, 80, stdout); printf ("%s\n", newline); out_len -= 80; } fwrite (s, 1, out_len, stdout); } else { fwrite (tmp, 1, out_len, stdout); } free (tmp); printf ("%s\n", newline); } else { printf ("result is approximately %ld digits%s\n", (long) mpz_sizeinbase (r, base >= 0 ? base : -base), newline); } free_expr (e); } exit (errcode); }
main (int argc, char **argv) { struct expr *e; int i; mpz_t r; int errcode = 0; char *str; int base = 10; #if !defined(_WIN32) && !defined(__DJGPP__) setup_error_handler (); #endif mpz_init (r); while (argc > 1 && argv[1][0] == '-') { char *arg = argv[1]; if (arg[1] >= '0' && arg[1] <= '9') break; if (arg[1] == 't') print_timing = 1; else if (arg[1] == 'b' && arg[2] >= '0' && arg[2] <= '9') { base = atoi (arg + 2); if (base < 2 || base > 36) { fprintf (stderr, "error: invalid output base\n"); exit (-1); } } else if (arg[1] == 'b' && arg[2] == 0) base = 2; else if (arg[1] == 'x' && arg[2] == 0) base = 16; else if (arg[1] == 'o' && arg[2] == 0) base = 8; else if (arg[1] == 'd' && arg[2] == 0) base = 10; else if (strcmp (arg, "-html") == 0) { flag_html = 1; newline = "<BR>"; } else if (strcmp (arg, "-split") == 0) { flag_splitup_output = 1; } else if (strcmp (arg, "-noprint") == 0) { flag_print = 0; } else { fprintf (stderr, "error: unknown option `%s'\n", arg); exit (-1); } argv++; argc--; } for (i = 1; i < argc; i++) { int s; int jmpval; /* Set up error handler for parsing expression. */ jmpval = setjmp (errjmpbuf); if (jmpval != 0) { fprintf (stderr, "error: %s%s\n", error, newline); fprintf (stderr, " %s%s\n", argv[i], newline); if (! flag_html) { /* ??? Dunno how to align expression position with arrow in HTML ??? */ fprintf (stderr, " "); for (s = jmpval - (long) argv[i]; --s >= 0; ) putc (' ', stderr); fprintf (stderr, "^\n"); } errcode |= 1; continue; } str = expr (argv[i], &e); if (str[0] != 0) { fprintf (stderr, "error: garbage where end of expression expected%s\n", newline); fprintf (stderr, " %s%s\n", argv[i], newline); if (! flag_html) { /* ??? Dunno how to align expression position with arrow in HTML ??? */ fprintf (stderr, " "); for (s = str - argv[i]; --s; ) putc (' ', stderr); fprintf (stderr, "^\n"); } errcode |= 1; free_expr (e); continue; } /* Set up error handler for evaluating expression. */ if (setjmp (errjmpbuf)) { fprintf (stderr, "error: %s%s\n", error, newline); fprintf (stderr, " %s%s\n", argv[i], newline); if (! flag_html) { /* ??? Dunno how to align expression position with arrow in HTML ??? */ fprintf (stderr, " "); for (s = str - argv[i]; --s >= 0; ) putc (' ', stderr); fprintf (stderr, "^\n"); } errcode |= 2; continue; } { int t0; if (print_timing) t0 = cputime (); mpz_eval_expr (r, e); if (print_timing) printf ("computation took %d ms%s\n", cputime () - t0, newline); } if (flag_print) { size_t out_len; char *tmp, *s; int t0; out_len = mpz_sizeinbase (r, base) + 1; tmp = malloc (out_len); if (print_timing) t0 = cputime (); if (print_timing) /* Print first half of message... */ printf ("output conversion "); mpz_get_str (tmp, -base, r); if (print_timing) /* ...print 2nd half of message unless we caught a time limit and therefore longjmp'ed */ printf ("took %d ms%s\n", cputime () - t0, newline); out_len = strlen (tmp); if (flag_splitup_output) { for (s = tmp; out_len > 60; s += 60) { fwrite (s, 1, 60, stdout); printf ("%s\n", newline); out_len -= 60; } fwrite (s, 1, out_len, stdout); } else { fwrite (tmp, 1, out_len, stdout); } free (tmp); printf ("%s\n", newline); } else { printf ("result is approximately %ld digits%s\n", (long) mpz_sizeinbase (r, 10), newline); } free_expr (e); } exit (errcode); }