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 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
|
/*
* evaluate the dc language, from a FILE* or a string
*
* Copyright (C) 1994, 1997, 1998, 2000, 2003, 2005, 2006, 2008, 2010, 2012-2017
* Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* This is the only module which knows about the dc input language */
#include "config.h"
#include <stdio.h>
#ifdef HAVE_STRING_H
# include <string.h> /* memchr */
#else
# ifdef HAVE_MEMORY_H
# include <memory.h> /* memchr, maybe */
# else
# ifdef HAVE_STRINGS_H
# include <strings.h> /* memchr, maybe */
# endif
#endif
#endif
#include <signal.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include "dc.h"
#include "dc-proto.h"
#include "number.h"
typedef enum {DC_FALSE, DC_TRUE} dc_boolean;
typedef enum {
DC_OKAY = DC_SUCCESS, /* no further intervention needed for this command */
DC_EATONE, /* caller needs to eat the lookahead char */
DC_EVALREG, /* caller needs to eval the string named by `peekc' */
DC_EVALTOS, /* caller needs to eval the string on top of the stack */
DC_QUIT, /* quit out of unwind_depth levels of evaluation */
DC_INT, /* caller needs to parse a dc_num from input stream */
DC_STR, /* caller needs to parse a dc_str from input stream */
DC_SYSTEM, /* caller needs to run a system() on next input line */
DC_COMMENT, /* caller needs to skip to the next input line */
DC_NEGCMP, /* caller needs to re-call dc_func() with `negcmp' set */
DC_EOF_ERROR /* unexpected end of input; abort current eval */
} dc_status;
static int dc_ibase=10; /* input base, 2 <= dc_ibase <= DC_IBASE_MAX */
static int dc_obase=10; /* output base, 2 <= dc_obase */
static int dc_scale=0; /* scale (see user documentaton) */
/* for Quitting evaluations */
static int unwind_depth=0;
/* for handling SIGINT properly */
static volatile sig_atomic_t interrupt_seen=0;
/* if true, active Quit will not exit program */
static dc_boolean unwind_noexit=DC_FALSE;
/*
* Used to synchronize lookahead on stdin for '?' command.
* If set to EOF then lookahead is used up.
*/
static int stdin_lookahead=EOF;
/* forward reference */
static int evalstr(dc_data *string);
/* input_fil and input_str are passed as arguments to dc_getnum */
/* used by the input_* functions: */
static FILE *input_fil_fp;
static const char *input_str_string;
/* Since we have a need for two characters of pushback, and
* ungetc() only guarantees one, we place the second pushback here
*/
static int input_pushback;
/* passed as an argument to dc_getnum */
static int
input_fil DC_DECLVOID()
{
int c;
if (input_pushback != EOF){
c = input_pushback;
input_pushback = EOF;
return c;
}
c = getc(input_fil_fp);
checkferror_input(input_fil_fp);
return c;
}
/* passed as an argument to dc_getnum */
static int
input_str DC_DECLVOID()
{
if (*input_str_string == '\0')
return EOF;
return *(const unsigned char *)input_str_string++;
}
/* takes a string and evals it; frees the string when done */
/* Wrapper around evalstr to avoid duplicating the free call
* at all possible return points.
*/
static int
dc_eval_and_free_str DC_DECLARG((string))
dc_data *string DC_DECLEND
{
dc_status status;
status = evalstr(string);
if (string->dc_type == DC_STRING)
dc_free_str(&string->v.string);
return status;
}
/* notice when an interrupt event happens */
static void
dc_trap_interrupt DC_DECLARG((signo))
int signo DC_DECLEND
{
signal(signo, dc_trap_interrupt);
interrupt_seen = 1;
}
/* step pointer past next end-of-line (or to end-of-string) */
static const char *
skip_past_eol DC_DECLARG((strptr, strend))
const char *strptr DC_DECLSEP
const char *strend DC_DECLEND
{
const char *p = memchr(strptr, '\n', (size_t)(strend-strptr));
if (p != NULL)
return p+1;
return strend;
}
/* dc_func does the grunt work of figuring out what each input
* character means; used by both dc_evalstr and dc_evalfile
*
* c -> the "current" input character under consideration
* peekc -> the lookahead input character
* negcmp -> negate comparison test (for <,=,> commands)
*/
static dc_status
dc_func DC_DECLARG((c, peekc, negcmp))
int c DC_DECLSEP
int peekc DC_DECLSEP
int negcmp DC_DECLEND
{
dc_data datum;
int tmpint;
switch (c){
case '_': case '.':
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
case '8': case '9': case 'A': case 'B':
case 'C': case 'D': case 'E': case 'F':
return DC_INT;
case ' ':
case '\t':
case '\n':
/* standard command separators */
break;
case '+': /* add top two stack elements */
dc_binop(dc_add, dc_scale);
break;
case '-': /* subtract top two stack elements */
dc_binop(dc_sub, dc_scale);
break;
case '*': /* multiply top two stack elements */
dc_binop(dc_mul, dc_scale);
break;
case '/': /* divide top two stack elements */
dc_binop(dc_div, dc_scale);
break;
case '%':
/* take the remainder from division of the top two stack elements */
dc_binop(dc_rem, dc_scale);
break;
case '~':
/* Do division on the top two stack elements. Return the
* quotient as next-to-top of stack and the remainder as
* top-of-stack.
*/
dc_binop2(dc_divrem, dc_scale);
break;
case '|':
/* Consider the top three elements of the stack as (base, exp, mod),
* where mod is top-of-stack, exp is next-to-top, and base is
* second-from-top. Mod must be non-zero, exp must be non-negative,
* and all three must be integers. Push the result of raising
* base to the exp power, reduced modulo mod. If we had base in
* register b, exp in register e, and mod in register m then this
* is conceptually equivalent to "lble^lm%", but it is implemented
* in a more efficient manner, and can handle arbritrarily large
* values for exp.
*/
dc_triop(dc_modexp, dc_scale);
break;
case '^': /* exponientiation of the top two stack elements */
dc_binop(dc_exp, dc_scale);
break;
case '<':
/* eval register named by peekc if
* less-than holds for top two stack elements
*/
if (peekc == EOF)
return DC_EOF_ERROR;
if ( (dc_cmpop() < 0) == (negcmp==0) )
return DC_EVALREG;
return DC_EATONE;
case '=':
/* eval register named by peekc if
* equal-to holds for top two stack elements
*/
if (peekc == EOF)
return DC_EOF_ERROR;
if ( (dc_cmpop() == 0) == (negcmp==0) )
return DC_EVALREG;
return DC_EATONE;
case '>':
/* eval register named by peekc if
* greater-than holds for top two stack elements
*/
if (peekc == EOF)
return DC_EOF_ERROR;
if ( (dc_cmpop() > 0) == (negcmp==0) )
return DC_EVALREG;
return DC_EATONE;
case '?': /* read a line from standard-input and eval it */
if (stdin_lookahead != EOF){
ungetc(stdin_lookahead, stdin);
stdin_lookahead = EOF;
}
datum = dc_readstring(stdin, '\n', '\n');
if (ferror(stdin))
return DC_EOF_ERROR;
dc_push(datum);
return DC_EVALTOS;
case '[': /* read to balancing ']' into a dc_str */
return DC_STR;
case '!': /* read to newline and call system() on resulting string */
if (peekc == '<' || peekc == '=' || peekc == '>')
return DC_NEGCMP;
return DC_SYSTEM;
case '#': /* comment; skip remainder of current line */
return DC_COMMENT;
case 'a': /* Convert top of stack to an ascii character. */
if (dc_pop(&datum) == DC_SUCCESS){
char tmps;
if (datum.dc_type == DC_NUMBER){
tmps = (char) dc_num2int(datum.v.number, DC_TOSS);
}else if (datum.dc_type == DC_STRING){
tmps = *dc_str2charp(datum.v.string);
dc_free_str(&datum.v.string);
}else{
dc_garbage("at top of stack", -1);
}
dc_push(dc_makestring(&tmps, 1));
}
break;
case 'c': /* clear whole stack */
dc_clear_stack();
break;
case 'd': /* duplicate the datum on the top of stack */
if (dc_top_of_stack(&datum) == DC_SUCCESS)
dc_push(dc_dup(datum));
break;
case 'f': /* print list of all stack items */
dc_printall(dc_obase);
break;
case 'i': /* set input base to value on top of stack */
if (dc_pop(&datum) == DC_SUCCESS){
tmpint = 0;
if (datum.dc_type == DC_NUMBER)
tmpint = dc_num2int(datum.v.number, DC_TOSS);
if (2 <= tmpint && tmpint <= DC_IBASE_MAX)
dc_ibase = tmpint;
else {
fprintf(stderr,
"%s: input base must be a number \
between 2 and %d (inclusive)\n",
progname, DC_IBASE_MAX);
checkferror_output(stderr);
}
}
break;
case 'k': /* set scale to value on top of stack */
if (dc_pop(&datum) == DC_SUCCESS){
tmpint = -1;
if (datum.dc_type == DC_NUMBER)
tmpint = dc_num2int(datum.v.number, DC_TOSS);
if ( ! (tmpint >= 0) ) {
fprintf(stderr,
"%s: scale must be a nonnegative number\n",
progname);
checkferror_output(stderr);
} else
dc_scale = tmpint;
}
break;
case 'l': /* "load" -- push value on top of register stack named
* by peekc onto top of evaluation stack; does not
* modify the register stack
*/
if (peekc == EOF)
return DC_EOF_ERROR;
if (dc_register_get(peekc, &datum) == DC_SUCCESS)
dc_push(datum);
return DC_EATONE;
case 'n': /* print the value popped off of top-of-stack;
* do not add a trailing newline
*/
if (dc_pop(&datum) == DC_SUCCESS)
dc_print(datum, dc_obase, DC_NONL, DC_TOSS);
break;
case 'o': /* set output base to value on top of stack */
if (dc_pop(&datum) == DC_SUCCESS){
tmpint = 0;
if (datum.dc_type == DC_NUMBER)
tmpint = dc_num2int(datum.v.number, DC_TOSS);
if ( ! (tmpint > 1) ) {
fprintf(stderr,
"%s: output base must be a number greater than 1\n",
progname);
checkferror_output(stderr);
} else
dc_obase = tmpint;
}
break;
case 'p': /* print the datum on the top of stack,
* with a trailing newline
*/
if (dc_top_of_stack(&datum) == DC_SUCCESS)
dc_print(datum, dc_obase, DC_WITHNL, DC_KEEP);
break;
case 'q': /* quit two levels of evaluation, posibly exiting program */
unwind_depth = 1; /* the return below is the first level of returns */
unwind_noexit = DC_FALSE;
return DC_QUIT;
case 'r': /* rotate (swap) the top two elements on the stack */
dc_stack_rotate(2);
break;
case 's': /* "store" -- replace top of register stack named
* by peekc with the value popped from the top
* of the evaluation stack
*/
if (peekc == EOF)
return DC_EOF_ERROR;
if (dc_pop(&datum) == DC_SUCCESS)
dc_register_set(peekc, datum);
return DC_EATONE;
case 'v': /* replace top of stack with its square root */
if (dc_pop(&datum) == DC_SUCCESS){
dc_num tmpnum;
if (datum.dc_type != DC_NUMBER){
fprintf(stderr,
"%s: square root of nonnumeric attempted\n",
progname);
checkferror_output(stderr);
}else if (dc_sqrt(datum.v.number, dc_scale, &tmpnum) == DC_SUCCESS){
dc_free_num(&datum.v.number);
datum.v.number = tmpnum;
dc_push(datum);
}
}
break;
case 'x': /* eval the datum popped from top of stack */
return DC_EVALTOS;
case 'z': /* push the current stack depth onto the top of stack */
dc_push(dc_int2data(dc_tell_stackdepth()));
break;
case 'I': /* push the current input base onto the stack */
dc_push(dc_int2data(dc_ibase));
break;
case 'K': /* push the current scale onto the stack */
dc_push(dc_int2data(dc_scale));
break;
case 'L': /* pop a value off of register stack named by peekc
* and push it onto the evaluation stack
*/
if (peekc == EOF)
return DC_EOF_ERROR;
if (dc_register_pop(peekc, &datum) == DC_SUCCESS)
dc_push(datum);
return DC_EATONE;
case 'O': /* push the current output base onto the stack */
dc_push(dc_int2data(dc_obase));
break;
case 'P':
/* Pop the value off the top of a stack. If it is
* a number, dump out the integer portion of its
* absolute value as a "base UCHAR_MAX+1" byte stream;
* if it is a string, just print it.
* In either case, do not append a trailing newline.
*/
if (dc_pop(&datum) == DC_SUCCESS){
if (datum.dc_type == DC_NUMBER)
dc_dump_num(datum.v.number, DC_TOSS);
else if (datum.dc_type == DC_STRING)
dc_out_str(datum.v.string, DC_TOSS);
else
dc_garbage("at top of stack", -1);
}
fflush(stdout);
checkferror_output(stdout);
break;
case 'Q': /* quit out of top-of-stack nested evals;
* pops value from stack;
* does not exit program (stops short if necessary)
*/
if (dc_pop(&datum) == DC_SUCCESS){
unwind_depth = 0;
unwind_noexit = DC_TRUE;
if (datum.dc_type == DC_NUMBER)
unwind_depth = dc_num2int(datum.v.number, DC_TOSS);
if (unwind_depth-- > 0)
return DC_QUIT;
unwind_depth = 0; /* paranoia */
fprintf(stderr,
"%s: Q command requires a number >= 1\n",
progname);
checkferror_output(stderr);
}
break;
case 'R': /* pop a value off of the evaluation stack,;
* rotate the top remaining stack elements that many
* places forward (negative numbers mean rotate
* backward).
*/
if (dc_pop(&datum) == DC_SUCCESS){
tmpint = 0;
if (datum.dc_type == DC_NUMBER)
tmpint = dc_num2int(datum.v.number, DC_TOSS);
dc_stack_rotate(tmpint);
}
break;
case 'S': /* pop a value off of the evaluation stack
* and push it onto the register stack named by peekc
*/
if (peekc == EOF)
return DC_EOF_ERROR;
if (dc_pop(&datum) == DC_SUCCESS)
dc_register_push(peekc, datum);
return DC_EATONE;
case 'X': /* replace the number on top-of-stack with its scale factor */
if (dc_pop(&datum) == DC_SUCCESS){
tmpint = 0;
if (datum.dc_type == DC_NUMBER)
tmpint = dc_tell_scale(datum.v.number, DC_TOSS);
dc_push(dc_int2data(tmpint));
}
break;
case 'Z': /* replace the datum on the top-of-stack with its length */
if (dc_pop(&datum) == DC_SUCCESS)
dc_push(dc_int2data(dc_tell_length(datum, DC_TOSS)));
break;
case ':': /* store into array */
if (peekc == EOF)
return DC_EOF_ERROR;
if (dc_pop(&datum) == DC_SUCCESS){
tmpint = -1;
if (datum.dc_type == DC_NUMBER)
tmpint = dc_num2int(datum.v.number, DC_TOSS);
if (dc_pop(&datum) == DC_SUCCESS){
if (tmpint < 0) {
fprintf(stderr,
"%s: array index must be a nonnegative integer\n",
progname);
checkferror_output(stderr);
} else
dc_array_set(peekc, tmpint, datum);
}
}
return DC_EATONE;
case ';': /* retreive from array */
if (peekc == EOF)
return DC_EOF_ERROR;
if (dc_pop(&datum) == DC_SUCCESS){
tmpint = -1;
if (datum.dc_type == DC_NUMBER)
tmpint = dc_num2int(datum.v.number, DC_TOSS);
if (tmpint < 0) {
fprintf(stderr,
"%s: array index must be a nonnegative integer\n",
progname);
checkferror_output(stderr);
} else
dc_push(dc_array_get(peekc, tmpint));
}
return DC_EATONE;
default: /* What did that user mean? */
fprintf(stderr, "%s: ", progname);
checkferror_output(stderr);
dc_show_id(stdout, c, " unimplemented\n");
checkferror_output(stdout);
break;
}
return DC_OKAY;
}
/* takes a string and evals it */
static int
evalstr DC_DECLARG((string))
dc_data *string DC_DECLEND
{
const char *s;
const char *end;
const char *p;
size_t len;
int c;
int peekc;
int count;
int negcmp;
int next_negcmp = 0;
int tail_depth = 1; /* how much tail recursion is active */
dc_data evalstr;
if (string->dc_type != DC_STRING){
fprintf(stderr,
"%s: eval called with non-string argument\n",
progname);
checkferror_output(stderr);
return DC_OKAY;
}
interrupt_seen = 0;
s = dc_str2charp(string->v.string);
end = s + dc_strlen(string->v.string);
while (s < end && interrupt_seen==0){
c = *(const unsigned char *)s++;
peekc = EOF;
if (s < end)
peekc = *(const unsigned char *)s;
negcmp = next_negcmp;
next_negcmp = 0;
switch (dc_func(c, peekc, negcmp)){
case DC_OKAY:
break;
case DC_EATONE:
if (peekc != EOF)
++s;
break;
case DC_EVALREG:
/*commands which return this guarantee that peekc!=EOF*/
++s;
if (dc_register_get(peekc, &evalstr) != DC_SUCCESS)
break;
dc_push(evalstr);
/*@fallthrough@*/
case DC_EVALTOS:
/*skip trailing whitespace to assist tail-recursion detection*/
while (s<end && (*s==' '||*s=='\t'||*s=='\n'||*s=='#')){
if (*s++ == '#')
s = skip_past_eol(s, end);
}
if (dc_pop(&evalstr) == DC_SUCCESS){
if (evalstr.dc_type == DC_NUMBER){
dc_push(evalstr);
}else if (evalstr.dc_type != DC_STRING){
dc_garbage("at top of stack", -1);
}else if (s == end){
/*handle tail recursion*/
dc_free_str(&string->v.string);
*string = evalstr;
s = dc_str2charp(string->v.string);
end = s + dc_strlen(string->v.string);
++tail_depth;
}else if (dc_eval_and_free_str(&evalstr) == DC_QUIT){
if (unwind_depth > 0){
--unwind_depth;
return DC_QUIT;
}
return DC_OKAY;
}
}
break;
case DC_QUIT:
if (unwind_depth >= tail_depth){
unwind_depth -= tail_depth;
return DC_QUIT;
}
/*adjust tail recursion accounting and continue*/
tail_depth -= unwind_depth;
break;
case DC_INT:
input_str_string = s - 1;
dc_push(dc_getnum(input_str, dc_ibase, &peekc));
s = input_str_string;
if (peekc != EOF)
--s;
break;
case DC_STR:
count = 1;
for (p=s; p<end && count>0; ++p)
if (*p == ']')
--count;
else if (*p == '[')
++count;
len = (size_t) (p - s);
dc_push(dc_makestring(s, (count==0 ? len-1 : len)));
s = p;
break;
case DC_SYSTEM:
s = dc_system(s);
break;
case DC_COMMENT:
s = skip_past_eol(s, end);
break;
case DC_NEGCMP:
next_negcmp = 1;
break;
case DC_EOF_ERROR:
if (ferror(stdin)) {
fprintf(stderr, "%s: ", progname);
perror("error reading stdin");
return DC_FAIL;
}
fprintf(stderr, "%s: unexpected EOS\n", progname);
checkferror_output(stderr);
return DC_OKAY;
}
}
return DC_OKAY;
}
/* wrapper around evalstr, to handle top-level QUIT requests correctly*/
int
dc_evalstr(dc_data *string)
{
switch (evalstr(string)) {
case DC_OKAY:
return DC_SUCCESS;
case DC_QUIT:
if (unwind_noexit != DC_TRUE)
return DC_FAIL;
return DC_SUCCESS;
default:
return DC_FAIL;
}
}
/* This is the main function of the whole DC program.
* Reads the file described by fp, calls dc_func to do
* the dirty work, and takes care of dc_func's shortcomings.
*/
int
dc_evalfile DC_DECLARG((fp))
FILE *fp DC_DECLEND
{
int c;
int peekc;
int negcmp;
int next_negcmp = 0;
typedef void (*handler_t)(int);
handler_t sigint_handler = dc_trap_interrupt;
handler_t sigint_default = signal(SIGINT, SIG_IGN);
dc_data datum;
/* Signals are awkward: we want to allow interactive users
* to break out of long running macros, but otherwise we
* prefer that SIGINT not be given any special treatment.
* Sometimes "no special treatment" means to continue to
* *ignore* the signal, but usually it means to kill the program.
*/
signal(SIGINT, sigint_default);
#ifdef HAVE_UNISTD_H
/* don't trap SIGINT if we can tell that we are not reading from a tty */
if ( ! isatty(fileno(fp)) )
sigint_handler = sigint_default;
#endif
stdin_lookahead = EOF;
for (c=getc(fp); c!=EOF; c=peekc){
peekc = getc(fp);
checkferror_input(stdin);
/*
* The following if() is the only place where ``stdin_lookahead''
* might be set to other than EOF:
*/
if (fp == stdin)
stdin_lookahead = peekc;
/*
* In the switch(), cases which naturally update peekc
* (unconditionally) do not have to update or reference
* stdin_lookahead; other functions use the predicate:
* stdin_lookahead != peekc && fp == stdin
* to recognize the case where:
* a) stdin_lookahead == EOF (stdin and peekc are not in sync)
* b) peekc != EOF (resync is possible)
* c) fp == stdin (resync is relevant)
* The whole stdin_lookahead complication arises because the
* '?' command may be invoked from an arbritrarily deeply
* nested dc_evalstr(), '?' reads exclusively from stdin,
* and this winds up making peekc invalid when fp==stdin.
*/
negcmp = next_negcmp;
next_negcmp = 0;
signal(SIGINT, sigint_handler);
switch (dc_func(c, peekc, negcmp)){
case DC_OKAY:
if (stdin_lookahead != peekc && fp == stdin) {
peekc = getc(fp);
checkferror_input(stdin);
}
break;
case DC_EATONE:
peekc = getc(fp);
checkferror_input(fp);
break;
case DC_EVALREG:
/*commands which send us here shall guarantee that peekc!=EOF*/
c = peekc;
peekc = getc(fp);
checkferror_input(fp);
stdin_lookahead = peekc;
if (dc_register_get(c, &datum) != DC_SUCCESS)
break;
dc_push(datum);
/*@fallthrough@*/
case DC_EVALTOS:
if (stdin_lookahead != peekc && fp == stdin) {
peekc = getc(fp);
checkferror_input(stdin);
}
if (dc_pop(&datum) == DC_SUCCESS){
if (datum.dc_type == DC_NUMBER){
dc_push(datum);
}else if (datum.dc_type == DC_STRING){
if (dc_eval_and_free_str(&datum) == DC_QUIT){
if (unwind_noexit != DC_TRUE)
goto reset_and_exit_quit;
fprintf(stderr, "%s: Q command argument exceeded \
string execution depth\n", progname);
checkferror_output(stderr);
}
}else{
dc_garbage("at top of stack", -1);
}
}
break;
case DC_QUIT:
if (unwind_noexit != DC_TRUE)
goto reset_and_exit_quit;
fprintf(stderr,
"%s: Q command argument exceeded string execution depth\n",
progname);
checkferror_output(stderr);
if (stdin_lookahead != peekc && fp == stdin) {
peekc = getc(fp);
checkferror_input(stdin);
}
break;
case DC_INT:
input_fil_fp = fp;
input_pushback = c;
ungetc(peekc, fp);
dc_push(dc_getnum(input_fil, dc_ibase, &peekc));
if (ferror(fp))
goto error_fail;
break;
case DC_STR:
ungetc(peekc, fp);
datum = dc_readstring(fp, '[', ']');
if (ferror(fp))
goto error_fail;
dc_push(datum);
peekc = getc(fp);
break;
case DC_SYSTEM:
ungetc(peekc, fp);
datum = dc_readstring(fp, '\n', '\n');
if (ferror(fp))
goto error_fail;
(void)dc_system(dc_str2charp(datum.v.string));
dc_free_str(&datum.v.string);
peekc = getc(fp);
break;
case DC_COMMENT:
while (peekc!=EOF && peekc!='\n')
peekc = getc(fp);
if (peekc != EOF)
peekc = getc(fp);
break;
case DC_NEGCMP:
next_negcmp = 1;
break;
case DC_EOF_ERROR:
if (ferror(fp))
goto error_fail;
fprintf(stderr, "%s: unexpected EOF\n", progname);
goto reset_and_exit_fail;
}
if (interrupt_seen)
fprintf(stderr, "\nInterrupt!\n");
interrupt_seen = 0;
signal(SIGINT, sigint_default);
}
if (!ferror(fp))
goto reset_and_exit_success;
error_fail:
fprintf(stderr, "%s: ", progname);
perror("error reading input");
return DC_FAIL;
reset_and_exit_quit:
reset_and_exit_fail:
signal(SIGINT, sigint_default);
return DC_FAIL;
reset_and_exit_success:
signal(SIGINT, sigint_default);
return DC_SUCCESS;
}
/*
* Local Variables:
* mode: C
* tab-width: 4
* End:
* vi: set ts=4 :
*/
|