Annotation of OpenXM_contrib2/asir2018/parse/lex.c, Revision 1.3
1.1 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
26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
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.3 ! fujimoto 48: * $OpenXM: OpenXM_contrib2/asir2018/parse/lex.c,v 1.2 2019/12/13 14:40:50 fujimoto Exp $
1.1 noro 49: */
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>
57: #if defined(VISUAL) || defined(__MINGW32__)
58: #include "ytab.h"
59: #else
60: #include "y.tab.h"
61: #endif
62: #if FEP
63: #include <readline/readline.h>
64: #endif
65:
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;
72: extern struct oTKWD kwd[];
73: extern Obj VOIDobj;
74:
75: extern int main_parser;
76: extern char *parse_strp;
77:
78: #define NBUFSIZ (BUFSIZ*10)
79: #define TBUFSIZ (BUFSIZ)
80:
81: #define REALLOC_NBUF \
82: if ( i >= nbufsize ) {\
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);\
89: }
90:
91: #define REALLOC_TBUF \
92: if ( i >= tbufsize ) {\
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);\
99: }
100:
101: #define READ_ALNUM_NBUF \
102: while ( 1 ) {\
103: c = Getc();\
104: if ( isalnum(c) ) {\
105: REALLOC_NBUF nbuf[i++] = c;\
106: } else\
107: break;\
108: }
109:
110: #define READ_DIGIT_NBUF \
111: while ( 1 ) {\
112: c = Getc();\
113: if ( isdigit(c) ) {\
114: REALLOC_NBUF nbuf[i++] = c;\
115: } else\
116: break;\
117: }
118:
119: int yylex()
120: {
121: #define yylvalp (&yylval)
122: int c,c1;
123: 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: Z 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: hextoz(nbuf,&n1); r = (Obj)n1;
156: yylvalp->p = (pointer)r;
157: return ( FORMULA );
158: } else if ( c == 'b' || c == 'B' ) {
159: for ( i = 0; i < 32; i++ )
160: nbuf[i] = '0';
161: READ_ALNUM_NBUF
162: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
163: binarytoz(nbuf,&n1); r = (Obj)n1;
164: yylvalp->p = (pointer)r;
165: return ( FORMULA );
166: } else if ( !isdigit(c) ) {
167: yylvalp->p = 0; Ungetc(c);
168: return ( FORMULA );
169: }
170: break;
171: case '\'' :
172: for ( i = 0; ; i++ ) {
173: c = Getc();
174: if ( c == '\'' )
175: break;
176: if ( c == '\\' )
177: c = Getc();
178: REALLOC_TBUF tbuf[i] = c;
179: }
180: REALLOC_TBUF tbuf[i] = 0;
181: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
182: yylvalp->p = (pointer)cptr;
183: return LCASE; break;
184: case '"' :
185: i = 0;
186: do {
187: c = Getc();
188: if ( c == '\n' ) asir_infile->ln++;
189: if ( c == '\\' ) {
190: c1 = Getc();
191: if ( c1 == 'n' ) {
192: c1 = '\n';
193: }else if ( c1 == 'r' ) {
194: c1 = '\r';
195: }else if ( c1 == 't' ) {
196: c1 = '\t';
197: }else if ( isdigit(c1) ){
198: d = c1 - '0';
199: c1 = Getc();
200: if ( isdigit(c1) ) {
201: d = 8*d + (c1 - '0');
202: c1 = Getc();
203: if ( isdigit(c1) ) {
204: d = 8*d + (c1 - '0');
205: }else {
206: Ungetc(c1);
207: }
208: }else {
209: Ungetc(c1);
210: }
211: c1 = d;
212: }
213: REALLOC_NBUF nbuf[i++] = c1;
214: } else {
215: REALLOC_NBUF nbuf[i++] = c;
216: }
217: } while ( c != '"' );
218: nbuf[i-1] = 0; /* REALLOC_NBUF is not necessary */
219: cptr = (char *)MALLOC(strlen(nbuf)+1);
220: strcpy(cptr,nbuf); yylvalp->p = (pointer) cptr;
221: return ( STR ); break;
222: case '>': case '<': case '=': case '!':
223: if ( (c1 = Getc()) == '=' )
224: switch ( c ) {
225: case '>': yylvalp->i = (int)C_GE; break;
226: case '<': yylvalp->i = (int)C_LE; break;
227: case '=': yylvalp->i = (int)C_EQ; break;
228: case '!': yylvalp->i = (int)C_NE; break;
229: default: break;
230: }
231: else if ( (c == '<' && c1 == '<') || (c == '>' && c1 == '>') )
232: return c;
233: else {
234: Ungetc(c1);
235: switch ( c ) {
236: case '>': yylvalp->i = (int)C_GT; break;
237: case '<': yylvalp->i = (int)C_LT; break;
238: default: return c; break;
239: }
240: }
241: return CMP; break;
242: case '+': case '-': case '*': case '/': case '%': case '^':
243: case '|': case '&':
244: switch ( c ) {
245: case '+': yylvalp->p = (pointer)addfs; break;
246: case '-': yylvalp->p = (pointer)subfs; break;
247: case '*': yylvalp->p = (pointer)mulfs; break;
248: case '/': yylvalp->p = (pointer)divfs; break;
249: case '%': yylvalp->p = (pointer)remfs; break;
250: case '^': yylvalp->p = (pointer)pwrfs; break;
251: default: break;
252: }
253: if ( (c1 = Getc()) == c )
254: switch ( c ) {
255: case '+': case '-': return SELF; break;
256: case '|': return OR; break;
257: case '&': return AND; break;
258: default: Ungetc(c1); return c; break;
259: }
260: else if ( c1 == '=' )
261: return BOPASS;
262: else if ( (c == '-') && (c1 == '>') )
263: return POINT;
264: else {
265: Ungetc(c1); return c;
266: }
267: break;
268: default :
269: break;
270: }
271: if ( isdigit(c) ) {
272: for ( i = 0; i < DLENGTH; i++ )
273: nbuf[i] = '0';
274: REALLOC_NBUF nbuf[i++] = c;
275: READ_DIGIT_NBUF
276: if ( c == '.' ) {
277: floatingpoint = 1;
278:
279: REALLOC_NBUF nbuf[i++] = c;
280: READ_DIGIT_NBUF
281: if ( c == 'e' || c == 'E' ) {
282: REALLOC_NBUF nbuf[i++] = c;
283: c = Getc();
284: if ( (c == '+') || (c == '-') ) {
285: REALLOC_NBUF nbuf[i++] = c;
286: } else
287: Ungetc(c);
288: READ_DIGIT_NBUF
289: }
290: } else if ( c == 'e' || c == 'E' ) {
291: floatingpoint = 1;
292: REALLOC_NBUF nbuf[i++] = c;
293: c = Getc();
294: if ( (c == '+') || (c == '-') ) {
295: REALLOC_NBUF nbuf[i++] = c;
296: } else
297: Ungetc(c);
298: READ_DIGIT_NBUF
299: }
300: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
301: if ( floatingpoint ) {
302: if ( !bigfloat ) {
303: dbl = (double)atof(nbuf+DLENGTH);
304: MKReal(dbl,real); r = (Obj)real;
305: } else
306: strtobf(nbuf,(BF *)&r);
307: } else {
308: mpz_t z;
309:
310: mpz_init(z);
311: mpz_set_str(z,nbuf,10);
312: MPZTOZ(z,n1); r = (Obj)n1;
313: }
314: yylvalp->p = (pointer)r;
315: return ( FORMULA );
316: } else if ( isalpha(c) || c == ':' || c == '_' ) {
317: if ( c == ':' ) {
318: c1 = Getc();
319: if ( c1 != ':' ) {
320: Ungetc(c1);
321: return c;
322: }
323: c1 = Getc();
324: if ( !isalpha(c1) ) {
325: Ungetc(c1);
326: return COLONCOLON;
327: }
328: i = 0;
329: tbuf[i++] = ':';
330: tbuf[i++] = ':';
331: tbuf[i++] = c1;
332: } else {
333: i = 0;
334: tbuf[i++] = c;
335: }
336: while ( 1 ) {
337: c = Getc();
338: if ( isalpha(c)||isdigit(c)||(c=='_')||(c=='.') ) {
339: REALLOC_TBUF tbuf[i++] = c;
340: } else
341: break;
342: }
343: REALLOC_TBUF tbuf[i] = 0; Ungetc(c);
344: if ( isupper(tbuf[0]) || (tbuf[0] == '_' && isupper(tbuf[1])) ) {
345: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
346: yylvalp->p = (pointer)cptr;
347: return UCASE;
348: } else {
349: for ( i = 0; kwd[i].name && strcmp(tbuf,kwd[i].name); i++ );
350: if ( kwd[i].name ) {
351: yylvalp->i = asir_infile->ln;
352: return kwd[i].token;
353: } else {
354: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
355: yylvalp->p = (pointer)cptr;
356: return LCASE;
357: }
358: }
359: } else if ( c == '@' ) {
360: if ( isdigit(c = Getc()) ) {
361: i = 0;
362: nbuf[i++] = c;
363: READ_DIGIT_NBUF
364: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
365: yylvalp->i = atoi(nbuf);
366: return ANS;
367: } else if ( c == '@' ) {
368: yylvalp->i = MAX(0,APVS->n-1);
369: return ANS;
370: } else if ( c == '>' || c == '<' || c == '=' || c == '!' ) {
371: if ( (c1 = Getc()) == '=' )
372: switch ( c ) {
373: case '>': yylvalp->i = (int)L_GE; break;
374: case '<': yylvalp->i = (int)L_LE; break;
375: case '=': yylvalp->i = (int)L_EQ; break;
376: case '!': yylvalp->i = (int)L_NE; break;
377: default: break;
378: }
379: else {
380: Ungetc(c1);
381: switch ( c ) {
382: case '>': yylvalp->i = (int)L_GT; break;
383: case '<': yylvalp->i = (int)L_LT; break;
384: case '=': yylvalp->i = (int)L_EQ; break;
385: case '!': yylvalp->i = (int)L_NOT; return FOP_NOT; break;
386: default: break;
387: }
388: }
389: return LOP;
390: } else if ( c == '|' || c == '&' ) {
391: if ( (c1 = Getc()) != c )
392: Ungetc(c1);
393: switch ( c ) {
394: case '|': yylvalp->i = (int)L_OR;
395: return FOP_OR; break;
396: case '&': yylvalp->i = (int)L_AND;
397: return FOP_AND; break;
398: }
399: } else if ( isalpha(c) ) {
400: i = 0;
401: tbuf[i++] = '@';
402: tbuf[i++] = c;
403: while ( 1 ) {
404: c = Getc();
405: if ( isalpha(c) ) {
406: REALLOC_TBUF tbuf[i++] = c;
407: } else
408: break;
409: }
410: Ungetc(c); REALLOC_TBUF tbuf[i] = 0;
411: if ( !strcmp(tbuf,"@p") )
412: return GFPNGEN;
413: else if ( !strcmp(tbuf,"@s") )
414: return GFSNGEN;
415: else if ( !strcmp(tbuf,"@void") ) {
416: yylvalp->p = VOIDobj;
417: return FORMULA;
418: } else if ( !strcmp(tbuf,"@i") ) {
419: extern pointer IU;
420:
421: yylvalp->p = IU;
422: return FORMULA;
423: } else if ( !strcmp(tbuf,"@true") ) {
424: yylvalp->p = F_TRUE;
425: return FORMULA;
426: } else if ( !strcmp(tbuf,"@false") ) {
427: yylvalp->p = F_FALSE;
428: return FORMULA;
429: } else if ( !strcmp(tbuf,"@impl") ) {
430: yylvalp->i = (int)L_IMPL;
431: return FOP_IMPL;
432: } else if ( !strcmp(tbuf,"@repl") ) {
433: yylvalp->i = (int)L_REPL;
434: return FOP_REPL;
435: } else if ( !strcmp(tbuf,"@equiv") ) {
436: yylvalp->i = (int)L_EQUIV;
437: return FOP_EQUIV;
438: } else if ( !strcmp(tbuf,"@grlex") ) {
439: yylvalp->p = Symbol_grlex;
440: return FORMULA;
441: } else if ( !strcmp(tbuf,"@glex") ) {
442: yylvalp->p = Symbol_glex;
443: return FORMULA;
444: } else if ( !strcmp(tbuf,"@lex") ) {
445: yylvalp->p = Symbol_lex;
446: return FORMULA;
447: } else {
448: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
449: yylvalp->p = (pointer)cptr;
450: return LCASE;
451: }
452: } else {
453: Ungetc(c);
454: return GF2NGEN;
455: }
456: } else
457: return ( c );
458: }
459:
460: void purge_stdin()
461: {
1.2 fujimoto 462: #if defined(__FreeBSD__) || defined(__DARWIN__) || defined(ANDROID)
1.1 noro 463: fpurge(stdin);
464: #elif defined(linux)
1.3 ! fujimoto 465: // stdin->_IO_read_end = stdin->_IO_read_base;
! 466: // stdin->_IO_read_ptr = stdin->_IO_read_base;
! 467: __fpurge(stdin);
1.1 noro 468: #elif defined(VISUAL_LIB)
469: void w_purge_stdin();
470:
471: w_purge_stdin();
472: #elif defined(VISUAL) && _MSC_VER >= 1900
473: rewind(stdin);
474: #elif defined(sparc) || defined(__alpha) || defined(__SVR4) || defined(mips) || defined(VISUAL) || defined(__MINGW32__) || defined(_IBMR2)
475: stdin->_ptr = stdin->_base; stdin->_cnt = 0;
476: #elif (defined(__MACH__) && defined(__ppc__)) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__INTERIX)
477: stdin->_r = 0; stdin->_p = stdin->_bf._base;
478: #else
479: --->FIXIT
480: #endif
481: }
482:
483: static int skipspace() {
484: int c,c1;
485:
486: for ( c = Getc(); ; )
487: switch ( c ) {
488: case ' ': case '\t': case '\r':
489: c = Getc(); break;
490: case '\n':
491: c = afternl(); break;
492: case '/':
493: if ( (c1 = Getc()) == '*' )
494: c = aftercomment();
495: else {
496: Ungetc(c1); return c;
497: }
498: break;
499: default:
500: return c; break;
501: }
502: }
503:
504: int afternl() {
505: int c,ac,i,quote;
506: char *ptr,*buf0;
507: char *av[BUFSIZ];
508: static int ilevel = 0;
509: char buf[BUFSIZ];
510:
511: if ( !ilevel )
512: asir_infile->ln++;
513: while ( (c = Getc()) == '#' ) {
514: Gets(buf);
515: #define LINE "line"
516: if ( !strncmp(buf,LINE,strlen(LINE)) ) buf0 = buf+strlen(LINE);
517: else buf0 = buf;
518: for ( quote = 0, ptr = buf0; *ptr; ptr++ )
519: if ( *ptr == '"' )
520: quote = quote ? 0 : 1;
521: else if ( quote && (*ptr == ' ') )
522: *ptr = '_';
523: stoarg(buf0,&ac,av);
524: if ( ac == 3 )
525: if ( (i = atoi(av[2])) == 1 )
526: ilevel++;
527: else if ( i == 2 )
528: ilevel--;
529: if ( !ilevel )
530: asir_infile->ln = atoi(av[0]);
531: }
532: return c;
533: }
534:
535: int aftercomment() {
536: int c,c1;
537:
538: for ( c = Getc(); ; ) {
539: if ( c == '\n' ) asir_infile->ln++;
540: c1 = Getc();
541: if ( (c == '*') && (c1 == '/') )
542: return Getc();
543: else
544: c = c1;
545: }
546: }
547:
548: int myatoi(char *s)
549: {
550: int i,r;
551: for ( i = 0, r = 0; i < DLENGTH; i++ ) r = r * 10 + ( s[i] - '0' );
552: return ( r );
553: }
554:
555: extern int ox_do_copy;
556: extern int I_am_server;
557: extern JMP_BUF main_env;
558: extern int at_root;
559: extern LIST LastStackTrace;
560: extern char *CUR_FUNC;
561:
562: void yyerror(char *s)
563: {
564: STRING fname,name,kwd;
565: USINT u;
566: NODE t;
567: LIST l,l2;
568:
569: if ( main_parser ) {
570: if ( ox_do_copy ) {
571: /* push errors to DebugStack */
572: } else {
573: if ( asir_infile->fp == stdin )
574: fprintf(stderr,"%s\n",s);
575: else
576: fprintf(stderr,"\"%s\", near line %d: %s\n",asir_infile->name,asir_infile->ln,s);
577: }
578: if ( I_am_server ) {
579: if ( NEXT(asir_infile) ) {
580: /* error in a file; record the position */
581: MKSTR(fname,asir_infile->name);
582: if ( CPVS == GPVS )
583: MKSTR(name,"");
584: else
585: MKSTR(name,CUR_FUNC);
586: MKUSINT(u,asir_infile->ln);
587: t = mknode(3,fname,name,u); MKLIST(l,t);
588: /* line number at the toplevel */
589: MKSTR(fname,"toplevel"); MKUSINT(u,at_root);
590: t = mknode(2,fname,u); MKLIST(l2,t);
591: t = mknode(2,l2,l);
592: } else {
593: MKSTR(fname,"toplevel"); MKUSINT(u,asir_infile->ln);
594: t = mknode(2,fname,u); MKLIST(l,t);
595: t = mknode(1,l);
596: }
597: MKLIST(l,t);
598: MKSTR(kwd,"asir_where"); t = mknode(2,kwd,l);
599: MKLIST(LastStackTrace,t);
600: set_lasterror(s);
601: LONGJMP(main_env,1);
602: }
603: } else
604: fprintf(stderr,"exprparse : %s\n",s);
605: }
606:
607: int echoback;
608:
609: extern int do_fep, do_file;
610:
611: unsigned char encrypt_char(unsigned char);
612: unsigned char decrypt_char(unsigned char);
613:
614: int Egetc(FILE *fp)
615: {
616: int c;
617:
618: if ( fp ) {
619: #if FEP
620: if ( do_fep && isatty(fileno(fp)) )
621: c = readline_getc();
622: else
623: #endif
624: c = getc(fp);
625: #if defined(VISUAL) || defined(__MINGW32__)
626: check_intr();
627: #endif
628: if ( c == EOF )
629: return c;
630: if ( asir_infile->encoded )
631: c = decrypt_char((unsigned char)c);
632: return c;
633: } else {
634: c = *parse_strp++;
635: if ( !c )
636: return EOF;
637: else
638: return c;
639: }
640: }
641:
642: void Eungetc(int c,FILE *fp)
643: {
644: if ( fp ) {
645: if ( asir_infile->encoded )
646: c = (int)encrypt_char((unsigned char)c);
647: #if FEP
648: if ( do_fep && isatty(fileno(fp)) )
649: readline_ungetc();
650: else
651: #endif
652: ungetc(c,fp);
653: } else
654: *--parse_strp = c;
655: }
656:
657: static int Getc() {
658: int c;
659:
660: if ( main_parser ) {
661: while ( 1 ) {
662: if ((c = Egetc(asir_infile->fp)) == EOF)
663: if ( NEXT(asir_infile) ) {
664: closecurrentinput();
665: /* if the input is the top level, generate error */
666: if ( !NEXT(asir_infile) )
667: error("end-of-file detected during parsing");
668: else
669: c = Getc();
670: break;
671: } else if ( asir_infile->fp || do_file ) {
672: if ( asir_infile->fp )
673: clearerr(asir_infile->fp);
674: asir_terminate(2);
675: } else {
676: error("end-of-line detected during parsing");
677: }
678: else
679: break;
680: }
681: if ( echoback )
682: fputc(c,asir_out);
683: } else
684: c = *parse_strp++;
685: return ( c );
686: }
687:
688: static void Ungetc(int c) {
689: if ( main_parser ) {
690: Eungetc(c,asir_infile->fp);
691: if ( echoback )
692: fputc('',asir_out);
693: } else
694: *--parse_strp = c;
695: }
696:
697: static void Gets(char *s)
698: {
699: int c;
700:
701: while ( (c = Getc()) != '\n' )
702: *s++ = c;
703: *s = 0;
704: }
705:
706: #if FEP
707:
708: static char *readline_line;
709: static int readline_nc,readline_index;
710: char *readline_console();
711:
712: int readline_getc()
713: {
714: char buf[BUFSIZ];
715:
716: if ( !readline_nc ) {
717: if ( readline_line )
718: free(readline_line);
719: sprompt(buf);
720: readline_line = readline_console(buf);
721: readline_nc = strlen(readline_line);
722: readline_index = 0;
723: }
724: readline_nc--;
725: return readline_line[readline_index++];
726: }
727:
728: void readline_ungetc()
729: {
730: readline_nc++; readline_index--;
731: }
732:
733: char *readline_console(char *prompt)
734: {
735: char *line;
736: int exp_result;
737: char *expansion;
738:
739: while ( 1 ) {
740: line = (char *)readline(prompt);
741: if ( line && *line ) {
742: using_history();
743: exp_result = history_expand(line,&expansion);
744: if ( !exp_result ) {
745: free(expansion);
746: for ( ; isspace((unsigned char)*line); line++ );
747: add_history(line);
748: break;
749: } else if ( exp_result > 0 ) {
750: free(line);
751: line = expansion;
752: break;
753: }
754: }
755: }
756: return line;
757: }
758: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>