Annotation of OpenXM_contrib2/asir2000/parse/lex.c, Revision 1.54
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.54 ! ohara 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/lex.c,v 1.53 2018/03/29 01:32:54 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.54 ! ohara 476: #elif defined(VISUAL) && _MSC_VER >= 1900
! 477: rewind(stdin);
1.50 fujimoto 478: #elif defined(sparc) || defined(__alpha) || defined(__SVR4) || defined(mips) || defined(VISUAL) || defined(__MINGW32__) || defined(_IBMR2)
1.53 noro 479: stdin->_ptr = stdin->_base; stdin->_cnt = 0;
1.28 ohara 480: #elif (defined(__MACH__) && defined(__ppc__)) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__INTERIX)
1.53 noro 481: stdin->_r = 0; stdin->_p = stdin->_bf._base;
1.6 noro 482: #else
483: --->FIXIT
484: #endif
1.1 noro 485: }
1.32 noro 486:
1.1 noro 487: static int skipspace() {
1.53 noro 488: int c,c1;
1.1 noro 489:
1.53 noro 490: for ( c = Getc(); ; )
491: switch ( c ) {
492: case ' ': case '\t': case '\r':
493: c = Getc(); break;
494: case '\n':
495: c = afternl(); break;
496: case '/':
497: if ( (c1 = Getc()) == '*' )
498: c = aftercomment();
499: else {
500: Ungetc(c1); return c;
501: }
502: break;
503: default:
504: return c; break;
505: }
1.1 noro 506: }
507:
508: int afternl() {
1.53 noro 509: int c,ac,i,quote;
510: char *ptr,*buf0;
511: char *av[BUFSIZ];
512: static int ilevel = 0;
513: char buf[BUFSIZ];
514:
515: if ( !ilevel )
516: asir_infile->ln++;
517: while ( (c = Getc()) == '#' ) {
518: Gets(buf);
1.46 noro 519: #define LINE "line"
1.53 noro 520: if ( !strncmp(buf,LINE,strlen(LINE)) ) buf0 = buf+strlen(LINE);
521: else buf0 = buf;
522: for ( quote = 0, ptr = buf0; *ptr; ptr++ )
523: if ( *ptr == '"' )
524: quote = quote ? 0 : 1;
525: else if ( quote && (*ptr == ' ') )
526: *ptr = '_';
527: stoarg(buf0,&ac,av);
528: if ( ac == 3 )
529: if ( (i = atoi(av[2])) == 1 )
530: ilevel++;
531: else if ( i == 2 )
532: ilevel--;
533: if ( !ilevel )
534: asir_infile->ln = atoi(av[0]);
535: }
536: return c;
1.1 noro 537: }
538:
539: int aftercomment() {
1.53 noro 540: int c,c1;
1.1 noro 541:
1.53 noro 542: for ( c = Getc(); ; ) {
543: if ( c == '\n' ) asir_infile->ln++;
544: c1 = Getc();
545: if ( (c == '*') && (c1 == '/') )
546: return Getc();
547: else
548: c = c1;
549: }
1.1 noro 550: }
1.53 noro 551:
1.18 noro 552: int myatoi(char *s)
1.1 noro 553: {
1.53 noro 554: int i,r;
555: for ( i = 0, r = 0; i < DLENGTH; i++ ) r = r * 10 + ( s[i] - '0' );
556: return ( r );
1.1 noro 557: }
558:
559: extern int ox_do_copy;
1.30 noro 560: extern int I_am_server;
561: extern JMP_BUF main_env;
1.40 noro 562: extern int at_root;
563: extern LIST LastStackTrace;
1.41 noro 564: extern char *CUR_FUNC;
1.1 noro 565:
1.18 noro 566: void yyerror(char *s)
1.1 noro 567: {
1.53 noro 568: STRING fname,name,kwd;
569: USINT u;
570: NODE t;
571: LIST l,l2;
572:
573: if ( main_parser ) {
574: if ( ox_do_copy ) {
575: /* push errors to DebugStack */
576: } else {
577: if ( asir_infile->fp == stdin )
578: fprintf(stderr,"%s\n",s);
579: else
580: fprintf(stderr,"\"%s\", near line %d: %s\n",asir_infile->name,asir_infile->ln,s);
581: }
582: if ( I_am_server ) {
583: if ( NEXT(asir_infile) ) {
584: /* error in a file; record the position */
585: MKSTR(fname,asir_infile->name);
586: if ( CPVS == GPVS )
587: MKSTR(name,"");
588: else
589: MKSTR(name,CUR_FUNC);
590: MKUSINT(u,asir_infile->ln);
591: t = mknode(3,fname,name,u); MKLIST(l,t);
592: /* line number at the toplevel */
593: MKSTR(fname,"toplevel"); MKUSINT(u,at_root);
594: t = mknode(2,fname,u); MKLIST(l2,t);
595: t = mknode(2,l2,l);
596: } else {
597: MKSTR(fname,"toplevel"); MKUSINT(u,asir_infile->ln);
598: t = mknode(2,fname,u); MKLIST(l,t);
599: t = mknode(1,l);
600: }
601: MKLIST(l,t);
602: MKSTR(kwd,"asir_where"); t = mknode(2,kwd,l);
603: MKLIST(LastStackTrace,t);
604: set_lasterror(s);
605: LONGJMP(main_env,1);
606: }
607: } else
608: fprintf(stderr,"exprparse : %s\n",s);
1.1 noro 609: }
610:
611: int echoback;
612:
1.38 noro 613: extern int do_fep, do_file;
1.1 noro 614:
615: unsigned char encrypt_char(unsigned char);
616: unsigned char decrypt_char(unsigned char);
617:
1.18 noro 618: int Egetc(FILE *fp)
1.1 noro 619: {
1.53 noro 620: int c;
1.1 noro 621:
1.53 noro 622: if ( fp ) {
1.23 noro 623: #if FEP
1.53 noro 624: if ( do_fep && isatty(fileno(fp)) )
625: c = readline_getc();
626: else
1.13 noro 627: #endif
1.53 noro 628: c = getc(fp);
1.50 fujimoto 629: #if defined(VISUAL) || defined(__MINGW32__)
1.53 noro 630: check_intr();
1.11 noro 631: #endif
1.53 noro 632: if ( c == EOF )
633: return c;
634: if ( asir_infile->encoded )
635: c = decrypt_char((unsigned char)c);
636: return c;
637: } else {
638: c = *parse_strp++;
639: if ( !c )
640: return EOF;
641: else
642: return c;
643: }
1.1 noro 644: }
645:
1.18 noro 646: void Eungetc(int c,FILE *fp)
1.1 noro 647: {
1.53 noro 648: if ( fp ) {
649: if ( asir_infile->encoded )
650: c = (int)encrypt_char((unsigned char)c);
1.23 noro 651: #if FEP
1.53 noro 652: if ( do_fep && isatty(fileno(fp)) )
653: readline_ungetc();
654: else
1.13 noro 655: #endif
1.53 noro 656: ungetc(c,fp);
657: } else
658: *--parse_strp = c;
1.1 noro 659: }
660:
661: static int Getc() {
1.53 noro 662: int c;
1.1 noro 663:
1.53 noro 664: if ( main_parser ) {
665: while ( 1 ) {
666: if ((c = Egetc(asir_infile->fp)) == EOF)
667: if ( NEXT(asir_infile) ) {
668: closecurrentinput();
669: /* if the input is the top level, generate error */
670: if ( !NEXT(asir_infile) )
671: error("end-of-file detected during parsing");
672: else
673: c = Getc();
674: break;
675: } else if ( asir_infile->fp || do_file ) {
676: if ( asir_infile->fp )
677: clearerr(asir_infile->fp);
678: asir_terminate(2);
679: } else {
680: error("end-of-line detected during parsing");
681: }
682: else
683: break;
684: }
685: if ( echoback )
686: fputc(c,asir_out);
687: } else
688: c = *parse_strp++;
689: return ( c );
1.1 noro 690: }
691:
1.18 noro 692: static void Ungetc(int c) {
1.53 noro 693: if ( main_parser ) {
694: Eungetc(c,asir_infile->fp);
695: if ( echoback )
696: fputc('',asir_out);
697: } else
698: *--parse_strp = c;
1.1 noro 699: }
700:
1.18 noro 701: static void Gets(char *s)
1.1 noro 702: {
1.53 noro 703: int c;
1.1 noro 704:
1.53 noro 705: while ( (c = Getc()) != '\n' )
706: *s++ = c;
707: *s = 0;
1.1 noro 708: }
1.12 saito 709:
1.23 noro 710: #if FEP
1.12 saito 711:
712: static char *readline_line;
713: static int readline_nc,readline_index;
714: char *readline_console();
715:
716: int readline_getc()
717: {
718: char buf[BUFSIZ];
719:
720: if ( !readline_nc ) {
721: if ( readline_line )
722: free(readline_line);
723: sprompt(buf);
724: readline_line = readline_console(buf);
725: readline_nc = strlen(readline_line);
726: readline_index = 0;
727: }
728: readline_nc--;
729: return readline_line[readline_index++];
730: }
731:
732: void readline_ungetc()
733: {
734: readline_nc++; readline_index--;
735: }
736:
1.18 noro 737: char *readline_console(char *prompt)
1.12 saito 738: {
739: char *line;
740: int exp_result;
741: char *expansion;
742:
743: while ( 1 ) {
744: line = (char *)readline(prompt);
745: if ( line && *line ) {
746: using_history();
747: exp_result = history_expand(line,&expansion);
748: if ( !exp_result ) {
749: free(expansion);
1.35 noro 750: for ( ; isspace((unsigned char)*line); line++ );
1.12 saito 751: add_history(line);
752: break;
753: } else if ( exp_result > 0 ) {
754: free(line);
755: line = expansion;
756: break;
757: }
758: }
759: }
760: return line;
761: }
762: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>