Annotation of OpenXM_contrib2/asir2000/parse/lex.c, Revision 1.53
1.4 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.5 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.4 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.53 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/lex.c,v 1.52 2017/03/28 03:26:33 noro Exp $
1.4 noro 49: */
1.1 noro 50: #include <ctype.h>
51: #include "ca.h"
52: #include "al.h"
53: #include "base.h"
54: #include "parse.h"
55: #include <sys/types.h>
56: #include <sys/stat.h>
1.50 fujimoto 57: #if defined(VISUAL) || defined(__MINGW32__)
1.10 noro 58: #include "ytab.h"
59: #else
1.1 noro 60: #include "y.tab.h"
1.10 noro 61: #endif
1.44 saito 62: #if FEP
63: #include <readline/readline.h>
64: #endif
1.1 noro 65:
1.18 noro 66: static int Getc();
67: static void Ungetc(int c);
68: static void Gets(char *s);
69: static int skipspace();
70:
71: extern INFILE asir_infile;
1.1 noro 72: extern struct oTKWD kwd[];
1.37 noro 73: extern Obj VOIDobj;
1.1 noro 74:
75: extern int main_parser;
76: extern char *parse_strp;
77:
1.2 noro 78: #define NBUFSIZ (BUFSIZ*10)
79: #define TBUFSIZ (BUFSIZ)
80:
81: #define REALLOC_NBUF \
82: if ( i >= nbufsize ) {\
1.53 ! noro 83: nbufsize += NBUFSIZ;\
! 84: if ( nbuf == nbuf0 ) {\
! 85: nbuf = (char *)MALLOC_ATOMIC(nbufsize);\
! 86: bcopy(nbuf0,nbuf,nbufsize-NBUFSIZ);\
! 87: } else\
! 88: nbuf = REALLOC(nbuf,nbufsize);\
1.2 noro 89: }
90:
91: #define REALLOC_TBUF \
92: if ( i >= tbufsize ) {\
1.53 ! noro 93: tbufsize += TBUFSIZ;\
! 94: if ( tbuf == tbuf0 ) {\
! 95: tbuf = (char *)MALLOC_ATOMIC(tbufsize);\
! 96: bcopy(tbuf0,tbuf,tbufsize-TBUFSIZ);\
! 97: } else\
! 98: tbuf = REALLOC(tbuf,tbufsize);\
1.2 noro 99: }
100:
101: #define READ_ALNUM_NBUF \
102: while ( 1 ) {\
1.53 ! noro 103: c = Getc();\
! 104: if ( isalnum(c) ) {\
! 105: REALLOC_NBUF nbuf[i++] = c;\
! 106: } else\
! 107: break;\
1.2 noro 108: }
109:
110: #define READ_DIGIT_NBUF \
111: while ( 1 ) {\
1.53 ! noro 112: c = Getc();\
! 113: if ( isdigit(c) ) {\
! 114: REALLOC_NBUF nbuf[i++] = c;\
! 115: } else\
! 116: break;\
1.2 noro 117: }
118:
1.18 noro 119: int yylex()
1.1 noro 120: {
121: #define yylvalp (&yylval)
1.53 ! noro 122: register int c,c1;
! 123: register int *ptr;
! 124: char *cptr;
! 125: int d,i,j;
! 126: char nbuf0[NBUFSIZ],tbuf0[TBUFSIZ];
! 127: char *nbuf, *tbuf;
! 128: int nbufsize, tbufsize;
! 129: N n,n1;
! 130: Q q;
! 131: Obj r;
! 132: int floatingpoint = 0;
! 133: double dbl;
! 134: Real real;
! 135: double atof();
! 136: extern int bigfloat;
! 137:
! 138:
! 139: /* initialize buffer pointers */
! 140: nbuf = nbuf0; tbuf = tbuf0;
! 141: nbufsize = NBUFSIZ; tbufsize = TBUFSIZ;
! 142:
! 143: switch ( c = skipspace() ) {
! 144: case EOF :
! 145: asir_terminate(2); break;
! 146: case '0' :
! 147: while ( ( c = Getc() ) == '0' );
! 148: if ( c == '.' ) {
! 149: Ungetc(c); c = '0';
! 150: } else if ( c == 'x' || c == 'X' ) {
! 151: for ( i = 0; i < 8; i++ )
! 152: nbuf[i] = '0';
! 153: READ_ALNUM_NBUF
! 154: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
! 155: hexton(nbuf,&n1);
! 156: NTOQ(n1,1,q); r = (Obj)q;
! 157: yylvalp->p = (pointer)r;
! 158: return ( FORMULA );
! 159: } else if ( c == 'b' || c == 'B' ) {
! 160: for ( i = 0; i < 32; i++ )
! 161: nbuf[i] = '0';
! 162: READ_ALNUM_NBUF
! 163: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
! 164: binaryton(nbuf,&n1);
! 165: NTOQ(n1,1,q); r = (Obj)q;
! 166: yylvalp->p = (pointer)r;
! 167: return ( FORMULA );
! 168: } else if ( !isdigit(c) ) {
! 169: yylvalp->p = 0; Ungetc(c);
! 170: return ( FORMULA );
! 171: }
! 172: break;
! 173: case '\'' :
! 174: for ( i = 0; ; i++ ) {
! 175: c = Getc();
! 176: if ( c == '\'' )
! 177: break;
! 178: if ( c == '\\' )
! 179: c = Getc();
! 180: REALLOC_TBUF tbuf[i] = c;
! 181: }
! 182: REALLOC_TBUF tbuf[i] = 0;
! 183: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
! 184: yylvalp->p = (pointer)cptr;
! 185: return LCASE; break;
! 186: case '"' :
! 187: i = 0;
! 188: do {
! 189: c = Getc();
1.52 noro 190: if ( c == '\n' ) asir_infile->ln++;
1.53 ! noro 191: if ( c == '\\' ) {
! 192: c1 = Getc();
! 193: if ( c1 == 'n' ) {
! 194: c1 = '\n';
! 195: }else if ( c1 == 'r' ) {
! 196: c1 = '\r';
1.27 ohara 197: }else if ( c1 == 't' ) {
198: c1 = '\t';
199: }else if ( isdigit(c1) ){
200: d = c1 - '0';
201: c1 = Getc();
202: if ( isdigit(c1) ) {
203: d = 8*d + (c1 - '0');
204: c1 = Getc();
205: if ( isdigit(c1) ) {
206: d = 8*d + (c1 - '0');
207: }else {
208: Ungetc(c1);
209: }
210: }else {
211: Ungetc(c1);
212: }
213: c1 = d;
214: }
1.53 ! noro 215: REALLOC_NBUF nbuf[i++] = c1;
! 216: } else {
! 217: REALLOC_NBUF nbuf[i++] = c;
! 218: }
! 219: } while ( c != '"' );
! 220: nbuf[i-1] = 0; /* REALLOC_NBUF is not necessary */
! 221: cptr = (char *)MALLOC(strlen(nbuf)+1);
! 222: strcpy(cptr,nbuf); yylvalp->p = (pointer) cptr;
! 223: return ( STR ); break;
! 224: case '>': case '<': case '=': case '!':
! 225: if ( (c1 = Getc()) == '=' )
! 226: switch ( c ) {
! 227: case '>': yylvalp->i = (int)C_GE; break;
! 228: case '<': yylvalp->i = (int)C_LE; break;
! 229: case '=': yylvalp->i = (int)C_EQ; break;
! 230: case '!': yylvalp->i = (int)C_NE; break;
! 231: default: break;
! 232: }
! 233: else if ( (c == '<' && c1 == '<') || (c == '>' && c1 == '>') )
! 234: return c;
! 235: else {
! 236: Ungetc(c1);
! 237: switch ( c ) {
! 238: case '>': yylvalp->i = (int)C_GT; break;
! 239: case '<': yylvalp->i = (int)C_LT; break;
! 240: default: return c; break;
! 241: }
! 242: }
! 243: return CMP; break;
! 244: case '+': case '-': case '*': case '/': case '%': case '^':
! 245: case '|': case '&':
! 246: switch ( c ) {
! 247: case '+': yylvalp->p = (pointer)addfs; break;
! 248: case '-': yylvalp->p = (pointer)subfs; break;
! 249: case '*': yylvalp->p = (pointer)mulfs; break;
! 250: case '/': yylvalp->p = (pointer)divfs; break;
! 251: case '%': yylvalp->p = (pointer)remfs; break;
! 252: case '^': yylvalp->p = (pointer)pwrfs; break;
! 253: default: break;
! 254: }
! 255: if ( (c1 = Getc()) == c )
! 256: switch ( c ) {
! 257: case '+': case '-': return SELF; break;
! 258: case '|': return OR; break;
! 259: case '&': return AND; break;
! 260: default: Ungetc(c1); return c; break;
! 261: }
! 262: else if ( c1 == '=' )
! 263: return BOPASS;
! 264: else if ( (c == '-') && (c1 == '>') )
! 265: return POINT;
! 266: else {
! 267: Ungetc(c1); return c;
! 268: }
! 269: break;
! 270: default :
! 271: break;
! 272: }
! 273: if ( isdigit(c) ) {
! 274: for ( i = 0; i < DLENGTH; i++ )
! 275: nbuf[i] = '0';
! 276: REALLOC_NBUF nbuf[i++] = c;
! 277: READ_DIGIT_NBUF
! 278: if ( c == '.' ) {
! 279: floatingpoint = 1;
! 280:
! 281: REALLOC_NBUF nbuf[i++] = c;
! 282: READ_DIGIT_NBUF
! 283: if ( c == 'e' || c == 'E' ) {
! 284: REALLOC_NBUF nbuf[i++] = c;
! 285: c = Getc();
! 286: if ( (c == '+') || (c == '-') ) {
! 287: REALLOC_NBUF nbuf[i++] = c;
! 288: } else
! 289: Ungetc(c);
! 290: READ_DIGIT_NBUF
! 291: }
! 292: } else if ( c == 'e' || c == 'E' ) {
! 293: floatingpoint = 1;
! 294: REALLOC_NBUF nbuf[i++] = c;
! 295: c = Getc();
! 296: if ( (c == '+') || (c == '-') ) {
! 297: REALLOC_NBUF nbuf[i++] = c;
! 298: } else
! 299: Ungetc(c);
! 300: READ_DIGIT_NBUF
! 301: }
! 302: if ( floatingpoint ) {
! 303: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
! 304: if ( !bigfloat ) {
! 305: dbl = (double)atof(nbuf+DLENGTH);
! 306: MKReal(dbl,real); r = (Obj)real;
! 307: } else
! 308: strtobf(nbuf,(BF *)&r);
! 309: } else {
! 310: Ungetc(c);
! 311: i -= DLENGTH; d = (i%DLENGTH?i/DLENGTH+1:i/DLENGTH);
! 312: n = NALLOC(d); PL(n) = d;
! 313: for ( j = 0, ptr = BD(n); j < d; j++ ,i -= DLENGTH )
! 314: ptr[j] = myatoi(nbuf+i);
! 315: bnton(DBASE,n,&n1);
! 316: NTOQ(n1,1,q); r = (Obj)q;
! 317: /* optobj(&r); */
! 318: }
! 319: yylvalp->p = (pointer)r;
! 320: return ( FORMULA );
! 321: } else if ( isalpha(c) || c == ':' || c == '_' ) {
! 322: if ( c == ':' ) {
! 323: c1 = Getc();
! 324: if ( c1 != ':' ) {
! 325: Ungetc(c1);
! 326: return c;
! 327: }
! 328: c1 = Getc();
! 329: if ( !isalpha(c1) ) {
! 330: Ungetc(c1);
! 331: return COLONCOLON;
! 332: }
! 333: i = 0;
! 334: tbuf[i++] = ':';
! 335: tbuf[i++] = ':';
! 336: tbuf[i++] = c1;
! 337: } else {
! 338: i = 0;
! 339: tbuf[i++] = c;
! 340: }
! 341: while ( 1 ) {
! 342: c = Getc();
! 343: if ( isalpha(c)||isdigit(c)||(c=='_')||(c=='.') ) {
! 344: REALLOC_TBUF tbuf[i++] = c;
! 345: } else
! 346: break;
! 347: }
! 348: REALLOC_TBUF tbuf[i] = 0; Ungetc(c);
! 349: if ( isupper(tbuf[0]) || (tbuf[0] == '_' && isupper(tbuf[1])) ) {
! 350: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
! 351: yylvalp->p = (pointer)cptr;
! 352: return UCASE;
! 353: } else {
! 354: for ( i = 0; kwd[i].name && strcmp(tbuf,kwd[i].name); i++ );
! 355: if ( kwd[i].name ) {
! 356: yylvalp->i = asir_infile->ln;
! 357: return kwd[i].token;
! 358: } else {
! 359: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
! 360: yylvalp->p = (pointer)cptr;
! 361: return LCASE;
! 362: }
! 363: }
! 364: } else if ( c == '@' ) {
! 365: if ( isdigit(c = Getc()) ) {
! 366: i = 0;
! 367: nbuf[i++] = c;
! 368: READ_DIGIT_NBUF
! 369: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
! 370: yylvalp->i = atoi(nbuf);
! 371: return ANS;
! 372: } else if ( c == '@' ) {
! 373: yylvalp->i = MAX(0,APVS->n-1);
! 374: return ANS;
! 375: } else if ( c == '>' || c == '<' || c == '=' || c == '!' ) {
! 376: if ( (c1 = Getc()) == '=' )
! 377: switch ( c ) {
! 378: case '>': yylvalp->i = (int)L_GE; break;
! 379: case '<': yylvalp->i = (int)L_LE; break;
! 380: case '=': yylvalp->i = (int)L_EQ; break;
! 381: case '!': yylvalp->i = (int)L_NE; break;
! 382: default: break;
! 383: }
! 384: else {
! 385: Ungetc(c1);
! 386: switch ( c ) {
! 387: case '>': yylvalp->i = (int)L_GT; break;
! 388: case '<': yylvalp->i = (int)L_LT; break;
! 389: case '=': yylvalp->i = (int)L_EQ; break;
! 390: case '!': yylvalp->i = (int)L_NOT; return FOP_NOT; break;
! 391: default: break;
! 392: }
! 393: }
! 394: return LOP;
! 395: } else if ( c == '|' || c == '&' ) {
! 396: if ( (c1 = Getc()) != c )
! 397: Ungetc(c1);
! 398: switch ( c ) {
! 399: case '|': yylvalp->i = (int)L_OR;
! 400: return FOP_OR; break;
! 401: case '&': yylvalp->i = (int)L_AND;
! 402: return FOP_AND; break;
! 403: }
! 404: } else if ( isalpha(c) ) {
! 405: i = 0;
! 406: tbuf[i++] = '@';
! 407: tbuf[i++] = c;
! 408: while ( 1 ) {
! 409: c = Getc();
! 410: if ( isalpha(c) ) {
! 411: REALLOC_TBUF tbuf[i++] = c;
! 412: } else
! 413: break;
! 414: }
! 415: Ungetc(c); REALLOC_TBUF tbuf[i] = 0;
! 416: if ( !strcmp(tbuf,"@p") )
! 417: return GFPNGEN;
! 418: else if ( !strcmp(tbuf,"@s") )
! 419: return GFSNGEN;
! 420: else if ( !strcmp(tbuf,"@void") ) {
! 421: yylvalp->p = VOIDobj;
! 422: return FORMULA;
! 423: } else if ( !strcmp(tbuf,"@i") ) {
! 424: extern pointer IU;
! 425:
! 426: yylvalp->p = IU;
! 427: return FORMULA;
! 428: } else if ( !strcmp(tbuf,"@true") ) {
! 429: yylvalp->p = F_TRUE;
! 430: return FORMULA;
! 431: } else if ( !strcmp(tbuf,"@false") ) {
! 432: yylvalp->p = F_FALSE;
! 433: return FORMULA;
! 434: } else if ( !strcmp(tbuf,"@impl") ) {
! 435: yylvalp->i = (int)L_IMPL;
! 436: return FOP_IMPL;
! 437: } else if ( !strcmp(tbuf,"@repl") ) {
! 438: yylvalp->i = (int)L_REPL;
! 439: return FOP_REPL;
! 440: } else if ( !strcmp(tbuf,"@equiv") ) {
! 441: yylvalp->i = (int)L_EQUIV;
! 442: return FOP_EQUIV;
! 443: } else if ( !strcmp(tbuf,"@grlex") ) {
! 444: yylvalp->p = Symbol_grlex;
! 445: return FORMULA;
! 446: } else if ( !strcmp(tbuf,"@glex") ) {
! 447: yylvalp->p = Symbol_glex;
! 448: return FORMULA;
! 449: } else if ( !strcmp(tbuf,"@lex") ) {
! 450: yylvalp->p = Symbol_lex;
! 451: return FORMULA;
! 452: } else {
! 453: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
! 454: yylvalp->p = (pointer)cptr;
! 455: return LCASE;
! 456: }
! 457: } else {
! 458: Ungetc(c);
! 459: return GF2NGEN;
! 460: }
! 461: } else
! 462: return ( c );
1.6 noro 463: }
464:
465: void purge_stdin()
466: {
1.43 ohara 467: #if defined(__FreeBSD__) || defined(__DARWIN__)
1.53 ! noro 468: fpurge(stdin);
1.6 noro 469: #elif defined(linux)
1.53 ! noro 470: stdin->_IO_read_end = stdin->_IO_read_base;
! 471: stdin->_IO_read_ptr = stdin->_IO_read_base;
1.10 noro 472: #elif defined(VISUAL_LIB)
1.53 ! noro 473: void w_purge_stdin();
1.18 noro 474:
1.53 ! noro 475: w_purge_stdin();
1.50 fujimoto 476: #elif defined(sparc) || defined(__alpha) || defined(__SVR4) || defined(mips) || defined(VISUAL) || defined(__MINGW32__) || defined(_IBMR2)
1.53 ! noro 477: stdin->_ptr = stdin->_base; stdin->_cnt = 0;
1.28 ohara 478: #elif (defined(__MACH__) && defined(__ppc__)) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__INTERIX)
1.53 ! noro 479: stdin->_r = 0; stdin->_p = stdin->_bf._base;
1.6 noro 480: #else
481: --->FIXIT
482: #endif
1.1 noro 483: }
1.32 noro 484:
1.1 noro 485: static int skipspace() {
1.53 ! noro 486: int c,c1;
1.1 noro 487:
1.53 ! noro 488: for ( c = Getc(); ; )
! 489: switch ( c ) {
! 490: case ' ': case '\t': case '\r':
! 491: c = Getc(); break;
! 492: case '\n':
! 493: c = afternl(); break;
! 494: case '/':
! 495: if ( (c1 = Getc()) == '*' )
! 496: c = aftercomment();
! 497: else {
! 498: Ungetc(c1); return c;
! 499: }
! 500: break;
! 501: default:
! 502: return c; break;
! 503: }
1.1 noro 504: }
505:
506: int afternl() {
1.53 ! noro 507: int c,ac,i,quote;
! 508: char *ptr,*buf0;
! 509: char *av[BUFSIZ];
! 510: static int ilevel = 0;
! 511: char buf[BUFSIZ];
! 512:
! 513: if ( !ilevel )
! 514: asir_infile->ln++;
! 515: while ( (c = Getc()) == '#' ) {
! 516: Gets(buf);
1.46 noro 517: #define LINE "line"
1.53 ! noro 518: if ( !strncmp(buf,LINE,strlen(LINE)) ) buf0 = buf+strlen(LINE);
! 519: else buf0 = buf;
! 520: for ( quote = 0, ptr = buf0; *ptr; ptr++ )
! 521: if ( *ptr == '"' )
! 522: quote = quote ? 0 : 1;
! 523: else if ( quote && (*ptr == ' ') )
! 524: *ptr = '_';
! 525: stoarg(buf0,&ac,av);
! 526: if ( ac == 3 )
! 527: if ( (i = atoi(av[2])) == 1 )
! 528: ilevel++;
! 529: else if ( i == 2 )
! 530: ilevel--;
! 531: if ( !ilevel )
! 532: asir_infile->ln = atoi(av[0]);
! 533: }
! 534: return c;
1.1 noro 535: }
536:
537: int aftercomment() {
1.53 ! noro 538: int c,c1;
1.1 noro 539:
1.53 ! noro 540: for ( c = Getc(); ; ) {
! 541: if ( c == '\n' ) asir_infile->ln++;
! 542: c1 = Getc();
! 543: if ( (c == '*') && (c1 == '/') )
! 544: return Getc();
! 545: else
! 546: c = c1;
! 547: }
1.1 noro 548: }
1.53 ! noro 549:
1.18 noro 550: int myatoi(char *s)
1.1 noro 551: {
1.53 ! noro 552: int i,r;
! 553: for ( i = 0, r = 0; i < DLENGTH; i++ ) r = r * 10 + ( s[i] - '0' );
! 554: return ( r );
1.1 noro 555: }
556:
557: extern int ox_do_copy;
1.30 noro 558: extern int I_am_server;
559: extern JMP_BUF main_env;
1.40 noro 560: extern int at_root;
561: extern LIST LastStackTrace;
1.41 noro 562: extern char *CUR_FUNC;
1.1 noro 563:
1.18 noro 564: void yyerror(char *s)
1.1 noro 565: {
1.53 ! noro 566: STRING fname,name,kwd;
! 567: USINT u;
! 568: NODE t;
! 569: LIST l,l2;
! 570:
! 571: if ( main_parser ) {
! 572: if ( ox_do_copy ) {
! 573: /* push errors to DebugStack */
! 574: } else {
! 575: if ( asir_infile->fp == stdin )
! 576: fprintf(stderr,"%s\n",s);
! 577: else
! 578: fprintf(stderr,"\"%s\", near line %d: %s\n",asir_infile->name,asir_infile->ln,s);
! 579: }
! 580: if ( I_am_server ) {
! 581: if ( NEXT(asir_infile) ) {
! 582: /* error in a file; record the position */
! 583: MKSTR(fname,asir_infile->name);
! 584: if ( CPVS == GPVS )
! 585: MKSTR(name,"");
! 586: else
! 587: MKSTR(name,CUR_FUNC);
! 588: MKUSINT(u,asir_infile->ln);
! 589: t = mknode(3,fname,name,u); MKLIST(l,t);
! 590: /* line number at the toplevel */
! 591: MKSTR(fname,"toplevel"); MKUSINT(u,at_root);
! 592: t = mknode(2,fname,u); MKLIST(l2,t);
! 593: t = mknode(2,l2,l);
! 594: } else {
! 595: MKSTR(fname,"toplevel"); MKUSINT(u,asir_infile->ln);
! 596: t = mknode(2,fname,u); MKLIST(l,t);
! 597: t = mknode(1,l);
! 598: }
! 599: MKLIST(l,t);
! 600: MKSTR(kwd,"asir_where"); t = mknode(2,kwd,l);
! 601: MKLIST(LastStackTrace,t);
! 602: set_lasterror(s);
! 603: LONGJMP(main_env,1);
! 604: }
! 605: } else
! 606: fprintf(stderr,"exprparse : %s\n",s);
1.1 noro 607: }
608:
609: int echoback;
610:
1.38 noro 611: extern int do_fep, do_file;
1.1 noro 612:
613: unsigned char encrypt_char(unsigned char);
614: unsigned char decrypt_char(unsigned char);
615:
1.18 noro 616: int Egetc(FILE *fp)
1.1 noro 617: {
1.53 ! noro 618: int c;
1.1 noro 619:
1.53 ! noro 620: if ( fp ) {
1.23 noro 621: #if FEP
1.53 ! noro 622: if ( do_fep && isatty(fileno(fp)) )
! 623: c = readline_getc();
! 624: else
1.13 noro 625: #endif
1.53 ! noro 626: c = getc(fp);
1.50 fujimoto 627: #if defined(VISUAL) || defined(__MINGW32__)
1.53 ! noro 628: check_intr();
1.11 noro 629: #endif
1.53 ! noro 630: if ( c == EOF )
! 631: return c;
! 632: if ( asir_infile->encoded )
! 633: c = decrypt_char((unsigned char)c);
! 634: return c;
! 635: } else {
! 636: c = *parse_strp++;
! 637: if ( !c )
! 638: return EOF;
! 639: else
! 640: return c;
! 641: }
1.1 noro 642: }
643:
1.18 noro 644: void Eungetc(int c,FILE *fp)
1.1 noro 645: {
1.53 ! noro 646: if ( fp ) {
! 647: if ( asir_infile->encoded )
! 648: c = (int)encrypt_char((unsigned char)c);
1.23 noro 649: #if FEP
1.53 ! noro 650: if ( do_fep && isatty(fileno(fp)) )
! 651: readline_ungetc();
! 652: else
1.13 noro 653: #endif
1.53 ! noro 654: ungetc(c,fp);
! 655: } else
! 656: *--parse_strp = c;
1.1 noro 657: }
658:
659: static int Getc() {
1.53 ! noro 660: int c;
1.1 noro 661:
1.53 ! noro 662: if ( main_parser ) {
! 663: while ( 1 ) {
! 664: if ((c = Egetc(asir_infile->fp)) == EOF)
! 665: if ( NEXT(asir_infile) ) {
! 666: closecurrentinput();
! 667: /* if the input is the top level, generate error */
! 668: if ( !NEXT(asir_infile) )
! 669: error("end-of-file detected during parsing");
! 670: else
! 671: c = Getc();
! 672: break;
! 673: } else if ( asir_infile->fp || do_file ) {
! 674: if ( asir_infile->fp )
! 675: clearerr(asir_infile->fp);
! 676: asir_terminate(2);
! 677: } else {
! 678: error("end-of-line detected during parsing");
! 679: }
! 680: else
! 681: break;
! 682: }
! 683: if ( echoback )
! 684: fputc(c,asir_out);
! 685: } else
! 686: c = *parse_strp++;
! 687: return ( c );
1.1 noro 688: }
689:
1.18 noro 690: static void Ungetc(int c) {
1.53 ! noro 691: if ( main_parser ) {
! 692: Eungetc(c,asir_infile->fp);
! 693: if ( echoback )
! 694: fputc('',asir_out);
! 695: } else
! 696: *--parse_strp = c;
1.1 noro 697: }
698:
1.18 noro 699: static void Gets(char *s)
1.1 noro 700: {
1.53 ! noro 701: int c;
1.1 noro 702:
1.53 ! noro 703: while ( (c = Getc()) != '\n' )
! 704: *s++ = c;
! 705: *s = 0;
1.1 noro 706: }
1.12 saito 707:
1.23 noro 708: #if FEP
1.12 saito 709:
710: static char *readline_line;
711: static int readline_nc,readline_index;
712: char *readline_console();
713:
714: int readline_getc()
715: {
716: char buf[BUFSIZ];
717:
718: if ( !readline_nc ) {
719: if ( readline_line )
720: free(readline_line);
721: sprompt(buf);
722: readline_line = readline_console(buf);
723: readline_nc = strlen(readline_line);
724: readline_index = 0;
725: }
726: readline_nc--;
727: return readline_line[readline_index++];
728: }
729:
730: void readline_ungetc()
731: {
732: readline_nc++; readline_index--;
733: }
734:
1.18 noro 735: char *readline_console(char *prompt)
1.12 saito 736: {
737: char *line;
738: int exp_result;
739: char *expansion;
740:
741: while ( 1 ) {
742: line = (char *)readline(prompt);
743: if ( line && *line ) {
744: using_history();
745: exp_result = history_expand(line,&expansion);
746: if ( !exp_result ) {
747: free(expansion);
1.35 noro 748: for ( ; isspace((unsigned char)*line); line++ );
1.12 saito 749: add_history(line);
750: break;
751: } else if ( exp_result > 0 ) {
752: free(line);
753: line = expansion;
754: break;
755: }
756: }
757: }
758: return line;
759: }
760: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>