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 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
|
/* xlread - xlisp expression input routine */
/* Copyright (c) 1985, by David Michael Betz
All Rights Reserved
Permission is granted for unrestricted non-commercial use */
/* CHANGE LOG
* --------------------------------------------------------------------
* 28Apr03 dm eliminate some compiler warnings
* replaced system-specific code with generic calls (see path.c)
*/
#include "stdlib.h"
#include "string.h"
#include "switches.h"
#include "xlisp.h"
#ifdef WINDOWS
#include "winfun.h"
#endif
#ifdef MACINTOSH
#include "macstuff.h"
#endif
#ifdef DEBUG_INPUT
extern FILE *debug_input_fp;
#endif
/* symbol parser modes */
#define DONE 0
#define NORMAL 1
#define ESCAPE 2
/* external variables */
extern LVAL s_stdout,s_true,s_dot;
extern LVAL s_quote,s_function,s_bquote,s_comma,s_comat;
extern LVAL s_rtable,k_wspace,k_const,k_nmacro,k_tmacro;
extern LVAL k_sescape,k_mescape;
extern char buf[];
/* external routines */
extern FILE *osaopen();
/* on the NeXT, atof is a macro in stdlib.h */
#if !defined(atof) && !defined(_WIN32)
extern double atof();
#endif
#ifndef __MWERKS__
#if !defined(ITYPE) && !defined(_WIN32)
extern ITYPE;
#endif
#endif
#define WSPACE "\t \f\r\n"
#define CONST1 "!$%&*+-./0123456789:<=>?@[]^_{}~"
#define CONST2 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
/* forward declarations */
FORWARD LVAL callmacro(LVAL fptr, int ch);
FORWARD LOCAL LVAL psymbol(LVAL fptr);
FORWARD LOCAL LVAL punintern(LVAL fptr);
FORWARD LOCAL LVAL pnumber(LVAL fptr, int radix);
FORWARD LOCAL LVAL pquote(LVAL fptr, LVAL sym);
FORWARD LOCAL LVAL plist(LVAL fptr);
FORWARD LOCAL LVAL pvector(LVAL fptr);
FORWARD LOCAL void upcase(char *str);
FORWARD LOCAL int pname(LVAL fptr,int *pescflag);
FORWARD LOCAL void pcomment(LVAL fptr);
FORWARD LOCAL int checkeof(LVAL fptr);
FORWARD LOCAL int nextch(LVAL fptr);
FORWARD LOCAL void badeof(LVAL fptr);
FORWARD LOCAL int storech(char *buf, int i, int ch);
#ifdef WINDOWS
static char save_file_name[STRMAX+1]; /* keeps files opened by prompt */
static int sfn_valid = FALSE;
#endif
#ifdef DEBUG_INPUT
extern FILE *read_by_xlisp;
#endif
/* xlload - load a file of xlisp expressions */
int xlload(const char *fname, int vflag, int pflag)
{
char fullname[STRMAX+1];
#ifdef WINDOWS
char *ptr;
#endif
LVAL fptr,expr;
XLCONTEXT cntxt;
FILE *fp;
int sts;
/* protect some pointers */
xlstkcheck(2);
xlsave(fptr);
xlsave(expr);
/* space for copy + extension? */
if (strlen(fname) > STRMAX - 4) {
expr = cvstring(fname);
goto toolong;
}
strcpy(fullname,fname);
#ifdef WINDOWS
#ifdef WINGUI
if (strcmp(fullname, "*") == 0) {
if (sfn_valid) {
strcpy(fullname, save_file_name);
} else {
strcpy(fullname, "*.*");
}
}
if (strcmp(fullname, "*.*") == 0) {
const char *name = getfilename(NULL, "lsp", "r", "Load file");
if (name) {
strcpy(fullname, name);
strcpy(save_file_name, name);
sfn_valid = TRUE;
} else {
xlpopn(2);
return FALSE;
}
}
#endif
/* replace "/" with "\" so that (current-path) will work */
for (ptr = fullname; *ptr; ptr++) {
if (*ptr == '/') *ptr = '\\';
}
#endif
/* allocate a file node */
fptr = cvfile(NULL);
/* open the file */
fp = osaopen(fullname, "r");
if (fp == NULL) {
/* default the extension if there is room */
if (needsextension(fullname)) {
char fullname_plus[STRMAX+1];
strcpy(fullname_plus, fullname);
strcat(fullname_plus, ".lsp");
fp = osaopen(fullname_plus, "r");
if (fp) strcpy(fullname, fullname_plus);
}
}
if (fp == NULL) {
/* new cross-platform code by dmazzoni - new xlisp_path
implementation is in path.c */
const char *newname = find_in_xlisp_path(fullname);
if (newname && newname[0]) {
if (strlen(newname) > STRMAX) {
expr = cvstring(newname);
goto toolong;
}
strcpy(fullname, newname);
fp = osaopen(fullname, "r");
}
}
if (fp == NULL) {
/* the file STILL wasn't found */
#ifdef DEBUG_INPUT
if (read_by_xlisp) {
fprintf(read_by_xlisp, ";;;;xlload: failed to open %s\n", fullname);
}
#endif
xlpopn(2);
return (FALSE);
}
setfile(fptr,fp);
setvalue(s_loadingfiles, cons(fptr, getvalue(s_loadingfiles)));
setvalue(s_loadingfiles, cons(cvstring(fullname), getvalue(s_loadingfiles)));
/* print the information line */
if (vflag)
{ sprintf(buf,"; loading \"%s\"\n",fullname); stdputstr(buf); }
#ifdef DEBUG_INPUT
if (read_by_xlisp) {
fprintf(read_by_xlisp, ";;;;xlload: begin loading %s\n", fullname);
}
#endif
/* read, evaluate and possibly print each expression in the file */
xlbegin(&cntxt,CF_ERROR,s_true);
if (_setjmp(cntxt.c_jmpbuf))
sts = FALSE;
#ifdef DEBUG_INPUT
if (read_by_xlisp) {
fprintf(read_by_xlisp, ";;;;xlload: catch longjump, back to %s\n", fullname);
}
#endif
else {
#ifdef DEBUG_INPUT
if (read_by_xlisp) {
fprintf(read_by_xlisp, ";;;;xlload: about to read from %s (%x)\n", fullname, fptr);
}
#endif
/* a nested load that fails will cause all loading files to be closed,
* so check to make sure fptr is still valid each time through the loop */
while (getfile(fptr) && xlread(fptr,&expr,FALSE)) {
#ifdef DEBUG_INPUT
if (debug_input_fp) {
int c = getc(debug_input_fp);
ungetc(c, debug_input_fp);
}
#endif
expr = xleval(expr);
#ifdef DEBUG_INPUT
if (debug_input_fp) {
int c = getc(debug_input_fp);
ungetc(c, debug_input_fp);
}
#endif
if (pflag)
stdprint(expr);
#ifdef DEBUG_INPUT
if (debug_input_fp) {
int c = getc(debug_input_fp);
ungetc(c, debug_input_fp);
}
#endif
#ifdef DEBUG_INPUT
if (read_by_xlisp) {
fprintf(read_by_xlisp, ";;;;xlload: about to read from %s (%x)\n", fullname, fptr);
}
#endif
}
#ifdef DEBUG_INPUT
if (read_by_xlisp) {
fprintf(read_by_xlisp, ";;;;xlload: xlread returned false for %s (%x)\n", fullname, fptr);
}
#endif
/* return success only if file did not disappear out from under us */
sts = (getfile(fptr) != NULL);
}
xlend(&cntxt);
/* close the file */
if (getfile(fptr)) { /* test added by RBD, see close_loadingfiles() */
osclose(getfile(fptr));
setfile(fptr,NULL);
}
if (consp(getvalue(s_loadingfiles)) &&
consp(cdr(getvalue(s_loadingfiles))) &&
car(cdr(getvalue(s_loadingfiles))) == fptr) {
setvalue(s_loadingfiles, cdr(cdr(getvalue(s_loadingfiles))));
}
/* restore the stack */
xlpopn(2);
#ifdef DEBUG_INPUT
if (read_by_xlisp) {
fprintf(read_by_xlisp, ";;;;xlload: finished loading %s\n", fullname);
}
#endif
/* return status */
return (sts);
toolong:
xlcerror("ignore file", "file name too long", expr);
xlpopn(2);
return FALSE;
}
/* xlread - read an xlisp expression */
int xlread(LVAL fptr, LVAL *pval, int rflag)
{
int sts;
/* read an expression */
while ((sts = readone(fptr,pval)) == FALSE)
#ifdef DEBUG_INPUT
if (debug_input_fp) {
int c = getc(debug_input_fp);
ungetc(c, debug_input_fp);
}
#endif
;
/* return status */
return (sts == EOF ? FALSE : TRUE);
}
/* readone - attempt to read a single expression */
int readone(LVAL fptr, LVAL *pval)
{
LVAL val,type;
int ch;
#ifdef DEBUG_INPUT
if (debug_input_fp) {
int c = getc(debug_input_fp);
ungetc(c, debug_input_fp);
}
#endif
/* get a character and check for EOF */
if ((ch = xlgetc(fptr)) == EOF)
return (EOF);
/* handle white space */
if ((type = tentry(ch)) == k_wspace)
return (FALSE);
/* handle symbol constituents */
else if (type == k_const) {
xlungetc(fptr,ch);
*pval = psymbol(fptr);
return (TRUE);
}
/* handle single and multiple escapes */
else if (type == k_sescape || type == k_mescape) {
xlungetc(fptr,ch);
*pval = psymbol(fptr);
return (TRUE);
}
/* handle read macros */
else if (consp(type)) {
if ((val = callmacro(fptr,ch)) && consp(val)) {
*pval = car(val);
return (TRUE);
}
else
return (FALSE);
}
/* handle illegal characters */
else {
xlerror("illegal character",cvfixnum((FIXTYPE)ch));
/* this point will never be reached because xlerror() does a
_longjmp(). The return is added to avoid false positive
error messages from static analyzers and compilers */
return (FALSE);
}
}
/* rmhash - read macro for '#' */
LVAL rmhash(void)
{
LVAL fptr,mch,val;
int escflag,ch;
/* protect some pointers */
xlsave1(val);
/* get the file and macro character */
fptr = xlgetfile();
mch = xlgachar();
xllastarg();
/* make the return value */
val = consa(NIL);
/* check the next character */
switch (ch = xlgetc(fptr)) {
case '\'':
rplaca(val,pquote(fptr,s_function));
break;
case '(':
rplaca(val,pvector(fptr));
break;
case 'b':
case 'B':
rplaca(val,pnumber(fptr,2));
break;
case 'o':
case 'O':
rplaca(val,pnumber(fptr,8));
break;
case 'x':
case 'X':
rplaca(val,pnumber(fptr,16));
break;
case '\\':
xlungetc(fptr,ch);
pname(fptr,&escflag);
ch = buf[0];
if (strlen(buf) > 1) {
upcase((char *) buf);
if (strcmp(buf,"NEWLINE") == 0)
ch = '\n';
else if (strcmp(buf,"SPACE") == 0)
ch = ' ';
else if (strcmp(buf,"TAB") == 0)
ch = '\t';
else
xlerror("unknown character name",cvstring(buf));
}
rplaca(val,cvchar(ch));
break;
case ':':
rplaca(val,punintern(fptr));
break;
case '|':
pcomment(fptr);
val = NIL;
break;
default:
xlerror("illegal character after #",cvfixnum((FIXTYPE)ch));
}
/* restore the stack */
xlpop();
/* return the value */
return (val);
}
/* rmquote - read macro for '\'' */
LVAL rmquote(void)
{
LVAL fptr,mch;
/* get the file and macro character */
fptr = xlgetfile();
mch = xlgachar();
xllastarg();
/* parse the quoted expression */
return (consa(pquote(fptr,s_quote)));
}
/* rmdquote - read macro for '"' */
LVAL rmdquote(void)
{
unsigned char buf[STRMAX+1],*p,*sptr;
LVAL fptr,str,newstr,mch;
int len,blen,ch,d2,d3;
/* protect some pointers */
xlsave1(str);
/* get the file and macro character */
fptr = xlgetfile();
mch = xlgachar();
xllastarg();
/* loop looking for a closing quote */
len = blen = 0; p = buf;
while ((ch = checkeof(fptr)) != '"') {
/* handle escaped characters */
switch (ch) {
case '\\':
switch (ch = checkeof(fptr)) {
case 't':
ch = '\011';
break;
case 'n':
ch = '\012';
break;
case 'f':
ch = '\014';
break;
case 'r':
ch = '\015';
break;
default:
if (ch >= '0' && ch <= '7') {
d2 = checkeof(fptr);
d3 = checkeof(fptr);
if (d2 < '0' || d2 > '7'
|| d3 < '0' || d3 > '7')
xlfail("invalid octal digit");
ch -= '0'; d2 -= '0'; d3 -= '0';
ch = (ch << 6) | (d2 << 3) | d3;
}
break;
}
}
/* check for buffer overflow */
if (blen >= STRMAX) {
newstr = new_string(len + STRMAX + 1);
sptr = getstring(newstr); *sptr = '\0';
if (str) strcat((char *) sptr, (char *) getstring(str));
*p = '\0'; strcat((char *) sptr, (char *) buf);
p = buf; blen = 0;
len += STRMAX;
str = newstr;
}
/* store the character */
*p++ = ch; ++blen;
}
/* append the last substring */
if (str == NIL || blen) {
newstr = new_string(len + blen + 1);
sptr = getstring(newstr); *sptr = '\0';
if (str) strcat((char *) sptr, (char *) getstring(str));
*p = '\0'; strcat((char *) sptr, (char *) buf);
str = newstr;
}
/* restore the stack */
xlpop();
/* return the new string */
return (consa(str));
}
/* rmbquote - read macro for '`' */
LVAL rmbquote(void)
{
LVAL fptr,mch;
/* get the file and macro character */
fptr = xlgetfile();
mch = xlgachar();
xllastarg();
/* parse the quoted expression */
return (consa(pquote(fptr,s_bquote)));
}
/* rmcomma - read macro for ',' */
LVAL rmcomma(void)
{
LVAL fptr,mch,sym;
int ch;
/* get the file and macro character */
fptr = xlgetfile();
mch = xlgachar();
xllastarg();
/* check the next character */
if ((ch = xlgetc(fptr)) == '@')
sym = s_comat;
else {
xlungetc(fptr,ch);
sym = s_comma;
}
/* make the return value */
return (consa(pquote(fptr,sym)));
}
/* rmlpar - read macro for '(' */
LVAL rmlpar(void)
{
LVAL fptr,mch;
/* get the file and macro character */
fptr = xlgetfile();
mch = xlgachar();
xllastarg();
/* make the return value */
return (consa(plist(fptr)));
}
/* 4035 is the "no return value" warning message */
/* rmrpar, pcomment, badeof, and upcase don't return anything */
/* #pragma warning(disable: 4035) */
/* rmrpar - read macro for ')' */
LVAL rmrpar(void)
{
xlfail("misplaced right paren");
return NULL; /* never used */
}
/* rmsemi - read macro for ';' */
LVAL rmsemi(void)
{
LVAL fptr,mch;
int ch;
/* get the file and macro character */
fptr = xlgetfile();
mch = xlgachar();
xllastarg();
/* skip to end of line */
while ((ch = xlgetc(fptr)) != EOF && ch != '\n')
;
/* return nil (nothing read) */
return (NIL);
}
/* pcomment - parse a comment delimited by #| and |# */
LOCAL void pcomment(LVAL fptr)
{
int lastch,ch,n;
/* look for the matching delimiter (and handle nesting) */
for (n = 1, lastch = -1; n > 0 && (ch = xlgetc(fptr)) != EOF; ) {
if (lastch == '|' && ch == '#')
{ --n; ch = -1; }
else if (lastch == '#' && ch == '|')
{ ++n; ch = -1; }
lastch = ch;
}
}
/* pnumber - parse a number */
LOCAL LVAL pnumber(LVAL fptr, int radix)
{
int digit,ch;
long num;
for (num = 0L; (ch = xlgetc(fptr)) != EOF; ) {
if (islower(ch)) ch = toupper(ch);
if (!('0' <= ch && ch <= '9') && !('A' <= ch && ch <= 'F'))
break;
if ((digit = (ch <= '9' ? ch - '0' : ch - 'A' + 10)) >= radix)
break;
num = num * (long)radix + (long)digit;
}
xlungetc(fptr,ch);
return (cvfixnum((FIXTYPE)num));
}
/* plist - parse a list */
LOCAL LVAL plist(LVAL fptr)
{
LVAL val,expr,lastnptr,nptr;
/* protect some pointers */
xlstkcheck(2);
xlsave(val);
xlsave(expr);
/* keep appending nodes until a closing paren is found */
for (lastnptr = NIL; nextch(fptr) != ')'; )
/* get the next expression */
switch (readone(fptr,&expr)) {
case EOF:
badeof(fptr);
case TRUE:
/* check for a dotted tail */
if (expr == s_dot) {
/* make sure there's a node */
if (lastnptr == NIL)
xlfail("invalid dotted pair");
/* parse the expression after the dot */
if (!xlread(fptr,&expr,TRUE))
badeof(fptr);
rplacd(lastnptr,expr);
/* make sure its followed by a close paren */
if (nextch(fptr) != ')')
xlfail("invalid dotted pair");
}
/* otherwise, handle a normal list element */
else {
nptr = consa(expr);
if (lastnptr == NIL)
val = nptr;
else
rplacd(lastnptr,nptr);
lastnptr = nptr;
}
break;
}
/* skip the closing paren */
xlgetc(fptr);
/* restore the stack */
xlpopn(2);
/* return successfully */
return (val);
}
/* pvector - parse a vector */
LOCAL LVAL pvector(LVAL fptr)
{
LVAL list,expr,val,lastnptr,nptr;
int len,ch,i;
/* protect some pointers */
xlstkcheck(2);
xlsave(list);
xlsave(expr);
/* keep appending nodes until a closing paren is found */
for (lastnptr = NIL, len = 0; (ch = nextch(fptr)) != ')'; ) {
/* check for end of file */
if (ch == EOF)
badeof(fptr);
/* get the next expression */
switch (readone(fptr,&expr)) {
case EOF:
badeof(fptr);
case TRUE:
nptr = consa(expr);
if (lastnptr == NIL)
list = nptr;
else
rplacd(lastnptr,nptr);
lastnptr = nptr;
len++;
break;
}
}
/* skip the closing paren */
xlgetc(fptr);
/* make a vector of the appropriate length */
val = newvector(len);
/* copy the list into the vector */
for (i = 0; i < len; ++i, list = cdr(list))
setelement(val,i,car(list));
/* restore the stack */
xlpopn(2);
/* return successfully */
return (val);
}
/* pquote - parse a quoted expression */
LOCAL LVAL pquote(LVAL fptr, LVAL sym)
{
LVAL val,p;
/* protect some pointers */
xlsave1(val);
/* allocate two nodes */
val = consa(sym);
rplacd(val,consa(NIL));
/* initialize the second to point to the quoted expression */
if (!xlread(fptr,&p,TRUE))
badeof(fptr);
rplaca(cdr(val),p);
/* restore the stack */
xlpop();
/* return the quoted expression */
return (val);
}
/* psymbol - parse a symbol name */
LOCAL LVAL psymbol(LVAL fptr)
{
int escflag;
LVAL val;
pname(fptr,&escflag);
return (escflag || !xlisnumber(buf,&val) ? xlenter(buf) : val);
}
/* punintern - parse an uninterned symbol */
LOCAL LVAL punintern(LVAL fptr)
{
int escflag;
pname(fptr,&escflag);
return (xlmakesym(buf));
}
/* pname - parse a symbol/package name */
LOCAL int pname(LVAL fptr,int *pescflag)
{
int mode,ch=0,i;
LVAL type;
/* initialize */
*pescflag = FALSE;
mode = NORMAL;
i = 0;
/* accumulate the symbol name */
while (mode != DONE) {
/* handle normal mode */
while (mode == NORMAL)
if ((ch = xlgetc(fptr)) == EOF)
mode = DONE;
else if ((type = tentry(ch)) == k_sescape) {
i = storech(buf,i,checkeof(fptr));
*pescflag = TRUE;
}
else if (type == k_mescape) {
*pescflag = TRUE;
mode = ESCAPE;
}
else if (type == k_const
|| (consp(type) && car(type) == k_nmacro))
i = storech(buf,i,islower(ch) ? toupper(ch) : ch);
else
mode = DONE;
/* handle multiple escape mode */
while (mode == ESCAPE)
if ((ch = xlgetc(fptr)) == EOF)
badeof(fptr);
else if ((type = tentry(ch)) == k_sescape)
i = storech(buf,i,checkeof(fptr));
else if (type == k_mescape)
mode = NORMAL;
else
i = storech(buf,i,ch);
}
buf[i] = 0;
/* check for a zero length name */
if (i == 0)
xlerror("zero length name", s_unbound);
/* unget the last character and return it */
xlungetc(fptr,ch);
return (ch);
}
/* storech - store a character in the print name buffer */
LOCAL int storech(char *buf, int i, int ch)
{
if (i < STRMAX)
buf[i++] = ch;
return (i);
}
/* tentry - get a readtable entry */
LVAL tentry(int ch)
{
LVAL rtable;
rtable = getvalue(s_rtable);
if (!vectorp(rtable) || ch < 0 || ch >= getsize(rtable))
return (NIL);
return (getelement(rtable,ch));
}
/* nextch - look at the next non-blank character */
LOCAL int nextch(LVAL fptr)
{
int ch;
/* return and save the next non-blank character */
while ((ch = xlgetc(fptr)) != EOF && isspace(ch))
;
xlungetc(fptr,ch);
return (ch);
}
/* checkeof - get a character and check for end of file */
LOCAL int checkeof(LVAL fptr)
{
int ch;
if ((ch = xlgetc(fptr)) == EOF)
badeof(fptr);
return (ch);
}
/* badeof - unexpected eof */
LOCAL void badeof(LVAL fptr)
{
xlgetc(fptr);
xlfail("unexpected EOF");
}
/* xlisnumber - check if this string is a number */
int xlisnumber(char *str, LVAL *pval)
{
int dl,dr;
char *p;
/* initialize */
p = str; dl = dr = 0;
/* check for a sign */
if (*p == '+' || *p == '-')
p++;
/* check for a string of digits */
while (isdigit(*p))
p++, dl++;
/* check for a decimal point */
if (*p == '.') {
p++;
while (isdigit(*p))
p++, dr++;
}
/* check for an exponent */
if ((dl || dr) && *p == 'E') {
p++;
/* check for a sign */
if (*p == '+' || *p == '-')
p++;
/* check for a string of digits */
while (isdigit(*p))
p++, dr++;
}
/* make sure there was at least one digit and this is the end */
if ((dl == 0 && dr == 0) || *p)
return (FALSE);
/* convert the string to an integer and return successfully */
if (pval) {
if (*str == '+') ++str;
if (str[strlen(str)-1] == '.') str[strlen(str)-1] = 0;
*pval = (dr ? cvflonum(atof(str)) : cvfixnum(ICNV(str)));
}
return (TRUE);
}
/* defmacro - define a read macro */
void defmacro(int ch, LVAL type, int offset)
{
extern FUNDEF funtab[];
LVAL subr;
subr = cvsubr(funtab[offset].fd_subr,funtab[offset].fd_type,offset);
setelement(getvalue(s_rtable),ch,cons(type,subr));
}
/* callmacro - call a read macro */
LVAL callmacro(LVAL fptr, int ch)
{
LVAL *newfp;
/* create the new call frame */
newfp = xlsp;
pusharg(cvfixnum((FIXTYPE)(newfp - xlfp)));
pusharg(cdr(getelement(getvalue(s_rtable),ch)));
pusharg(cvfixnum((FIXTYPE)2));
pusharg(fptr);
pusharg(cvchar(ch));
xlfp = newfp;
return (xlapply(2));
}
/* upcase - translate a string to upper case */
LOCAL void upcase(char *str)
{
for (; *str != '\0'; ++str)
if (islower(*str))
*str = toupper(*str);
}
/* xlrinit - initialize the reader */
void xlrinit(void)
{
LVAL rtable;
char *p;
int ch;
/* create the read table */
rtable = newvector(256);
setvalue(s_rtable,rtable);
/* initialize the readtable */
for (p = WSPACE; (ch = *p++); )
setelement(rtable,ch,k_wspace);
for (p = CONST1; (ch = *p++); )
setelement(rtable,ch,k_const);
for (p = CONST2; (ch = *p++); )
setelement(rtable,ch,k_const);
/* setup the escape characters */
setelement(rtable,'\\',k_sescape);
setelement(rtable,'|', k_mescape);
/* install the read macros */
defmacro('#', k_nmacro,FT_RMHASH);
defmacro('\'',k_tmacro,FT_RMQUOTE);
defmacro('"', k_tmacro,FT_RMDQUOTE);
defmacro('`', k_tmacro,FT_RMBQUOTE);
defmacro(',', k_tmacro,FT_RMCOMMA);
defmacro('(', k_tmacro,FT_RMLPAR);
defmacro(')', k_tmacro,FT_RMRPAR);
defmacro(';', k_tmacro,FT_RMSEMI);
}
|