Annotation of OpenXM_contrib2/asir2000/parse/glob.c, Revision 1.2
1.2 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/parse/glob.c,v 1.1.1.1 1999/12/03 07:39:12 noro Exp $ */
1.1 noro 2: #include "ca.h"
3: #include "al.h"
4: #include "parse.h"
5: #if PARI
6: #include "genpari.h"
7: #endif
8: #if !defined(THINK_C) && !defined(VISUAL) && !defined(_PA_RISC1_1) && !defined(linux) && !defined(SYSV)
9: #include <sgtty.h>
10: #else
11: #if defined(THINK_C) && !defined(__MWERKS__)
12: #include <console.h>
13: #endif
14: #endif
15:
16: #if defined(VISUAL)
17: #include <io.h>
18: #include <direct.h>
19: #endif
20:
21: #if defined(SYSV)
22: #include <sys/ttold.h>
23: #endif
24:
25: #if defined(THINK_C) || defined(VISUAL)
26: #if defined(VISUAL)
27: #define HISTORY asir_history
28: #else
29: #define HISTORY "history"
30: #endif
31: #endif
32: #define MAXHIST 100
33:
34: extern int GC_free_space_divisor;
35: extern int GC_free_space_numerator;
36: extern FILE *asir_out;
37:
38: IN asir_infile;
39: jmp_buf env,debug_env,timer_env;
40: int little_endian,debug_mode;
41: char *asir_libdir;
42: char *asir_pager;
43:
44: NODE usrf,sysf,noargsysf,ubinf,parif;
45: NODE ONENODE;
46: int main_parser, ox_do_copy, ox_do_count, ox_count_length;
47: int ox_file_io, ox_need_conv;
48: char *ox_copy_bptr;
49: char *parse_strp;
50: SNODE parse_snode;
51: FUNC parse_targetf;
52: FILE *ox_istream,*ox_ostream;
53: int do_server_in_X11;
54: Obj LastVal;
55: char LastError[BUFSIZ];
56:
57: struct oV oVAR[] = {
58: {"x",0,0}, {"y",0,0}, {"z",0,0}, {"u",0,0},
59: {"v",0,0}, {"w",0,0}, {"p",0,0}, {"q",0,0},
60: {"r",0,0}, {"s",0,0}, {"t",0,0}, {"a",0,0},
61: {"b",0,0}, {"c",0,0}, {"d",0,0}, {"e",0,0},
62: {"f",0,0}, {"g",0,0}, {"h",0,0}, {"i",0,0},
63: {"j",0,0}, {"k",0,0}, {"l",0,0}, {"m",0,0},
64: {"n",0,0}, {"o",0,0},
65: {"_x",0,0}, {"_y",0,0}, {"_z",0,0}, {"_u",0,0},
66: {"_v",0,0}, {"_w",0,0}, {"_p",0,0}, {"_q",0,0},
67: {"_r",0,0}, {"_s",0,0}, {"_t",0,0}, {"_a",0,0},
68: {"_b",0,0}, {"_c",0,0}, {"_d",0,0}, {"_e",0,0},
69: {"_f",0,0}, {"_g",0,0}, {"_h",0,0}, {"_i",0,0},
70: {"_j",0,0}, {"_k",0,0}, {"_l",0,0}, {"_m",0,0},
71: {"_n",0,0}, {"_o",0,0}
72: };
73:
74: struct oVL oVLIST[52];
75:
76: VL CO = oVLIST;
77: VL ALG;
78:
79: struct oVS oGPVS,oAPVS,oEPVS;
80: VS GPVS = &oGPVS;
81: VS APVS = &oAPVS;
82: VS EPVS = &oEPVS;
83: VS CPVS;
84:
85: struct oF oF_TRUE,oF_FALSE;
86: F F_TRUE = &oF_TRUE;
87: F F_FALSE = &oF_FALSE;
88:
89: #if defined(__SVR4) && defined(sun)
90: char cppname[64] = "/usr/ccs/lib/cpp ";
91: #else
92: #if defined(__FreeBSD__) || defined(__NetBSD__)
93: char cppname[64] = "/usr/bin/cpp ";
94: #else
95: #if defined(VISUAL)
96: char cppname[64] = "c:\\asir\\stdlib\\cpp ";
97: #else
98: char cppname[64] = "/lib/cpp ";
99: #endif
100: #endif
101: #endif
102: char asirname[64];
103: char displayname[BUFSIZ];
104:
105: int Verbose;
106:
107: void glob_init() {
108: int i;
109:
110: for ( i = 0; i < 51; i++ ) {
111: VR(&oVLIST[i]) = &oVAR[i]; NEXT(&oVLIST[i]) = &oVLIST[i+1];
112: }
113: VR(&oVLIST[i]) = &oVAR[i]; NEXT(&oVLIST[i]) = 0;
114: reallocarray((char **)&GPVS->va,(int *)&GPVS->asize,(int *)&GPVS->n,(int)sizeof(struct oPV));
115: reallocarray((char **)&APVS->va,(int *)&APVS->asize,(int *)&APVS->n,(int)sizeof(struct oPV));
116: CPVS = GPVS;
117: MKNODE(ONENODE,mkfnode(1,I_FORMULA,ONE),0);
118: OID(F_TRUE)=O_F; FOP(F_TRUE)=AL_TRUE; F_TRUE->arg.dummy = 0;
119: OID(F_FALSE)=O_F; FOP(F_FALSE)=AL_FALSE; F_FALSE->arg.dummy = 0;
120: sprintf(asirname,"%s/asir_symtab",asir_libdir);
121: #if defined(THINK_C)
122: initVol();
123: #endif
124: }
125:
126: void input_init(fp,name)
127: FILE *fp;
128: char *name;
129: {
130: asir_infile = (IN)CALLOC(sizeof(struct oIN),1);
131: asir_infile->name = name; asir_infile->fp = fp;
132: asir_infile->ln = 1; NEXT(asir_infile) = 0;
133: }
134:
135: void notdef(vl,a,b,c)
136: VL vl;
137: Obj a,b,*c;
138: {
139: *c = 0;
140: error("undefined arithmetic operation.");
141: }
142:
143: int kernelmode;
144: int do_asirrc;
145: int do_file;
146: int do_message;
147: int do_fep;
148: int no_prompt;
149: int read_exec_file;
150: static int buserr_sav;
151: static char asir_history[BUFSIZ];
152:
153: extern int mpi_myid;
154:
155: /*
156: * status = 1 abnormal termination (error() etc.)
157: * status = 2 normal termination (end(), quit() etc.)
158: */
159:
160: void asir_terminate(status)
161: int status;
162: {
163: if ( read_exec_file ) {
164: read_exec_file = 0; longjmp(env,status);
165: } else {
166: tty_reset();
167: #if INET && !defined(THINK_C)
168: #if MPI
169: if ( !mpi_myid )
170: close_allconnections();
171: mpi_finalize();
172: #else
173: close_allconnections();
174: #endif
175: #endif
176: if ( kernelmode )
177: fputc(0xff,asir_out);
178: if ( asir_out )
179: fflush(asir_out);
180: #if defined(THINK_C) && !defined(__MWERKS__)
181: *((int *)0x8) = buserr_sav;
182: while ( NEXT(asir_infile) )
183: closecurrentinput();
184: console_options.pause_atexit = 0;
185: resetDir();
186: write_hist(HISTORY);
187: #endif
188: ExitAsir();
189: }
190: }
191:
192: void param_init() {
193: unsigned int et = 0xff;
194: extern int paristack;
195: #if defined(THINK_C)
196: char name[BUFSIZ];
197: int c;
198: int val;
199: FILE *fp;
200: int stacksize;
201:
202: #include <Memory.h>
203: GC_free_space_divisor = 4; stacksize = 0x40000;
204: if ( fp = fopen("params","r") ) {
205: while ( 1 ) {
206: c = fgetc(fp);
207: if ( c == EOF )
208: break;
209: else
210: ungetc(c,fp);
211: fscanf(fp,"%s %d",name,&val);
212: if ( !strcmp(name,"stacksize") )
213: stacksize = val;
214: else if ( !strcmp(name,"adj") )
215: GC_free_space_divisor = val;
216: #if PARI
217: else if ( !strcmp(name,"paristack") )
218: paristack = val;
219: #endif
220: }
221: fclose(fp);
222: }
223: #if defined(__MWERKS__)
224: {
225: Ptr al;
226: al = LMGetApplLimit();
227: LMSetApplLimit(al-stacksize);
228: }
229: #else
230: ApplLimit = (Ptr)((char *)ApplLimit-stacksize);
231: console_options.title = "\pAsir";
232: #endif
233: #endif
234: if ( *((char *)&et) )
235: little_endian = 1;
236: else
237: little_endian = 0;
238: }
239:
240: void prompt() {
241: if ( !no_prompt && !do_fep && asir_infile->fp == stdin )
242: fprintf(asir_out,"[%d]%c",APVS->n,kernelmode?0xfe:' ');
243: fflush(asir_out);
244: }
245:
246: void sprompt(ptr)
247: char *ptr;
248: {
249: sprintf(ptr,"[%d]%c",APVS->n,kernelmode?0xfe:' ');
250: }
251:
252: #if 0 && !defined(THINK_C) && !defined(VISUAL) && !defined(_PA_RISC1_1)
253: static struct tchars tc;
254: static char oldeof;
255: struct winsize wsize;
256: int ttywidth;
257: char *upperbuf,*lowerbuf;
258: #endif
259:
260: FILE *in_fp;
261:
262: void process_args(ac,av)
263: int ac;
264: char **av;
265: {
266: #if !defined(THINK_C)
267: do_asirrc = 1;
268: #if !MPI
269: do_message = 1;
270: #endif
271: while ( ac > 0 ) {
272: if ( !strcmp(*av,"-heap") && (ac >= 2) ) {
273: void GC_expand_hp(int);
274:
275: GC_expand_hp(atoi(*(av+1))); av += 2; ac -= 2;
276: } else if ( !strcmp(*av,"-adj") && (ac >= 2) ) {
277: char *slash;
278:
279: slash = strrchr(*(av+1),'/');
280: if ( slash ) {
281: *slash = 0;
282: GC_free_space_numerator = atoi(slash+1);
283: }
284: GC_free_space_divisor = atoi(*(av+1));
285: av += 2; ac -= 2;
286: } else if ( !strcmp(*av,"-cpp") && (ac >= 2) ) {
287: strcpy(cppname,*(av+1)); av += 2; ac -= 2;
288: } else if ( !strcmp(*av,"-f") && (ac >= 2) ) {
289: in_fp = fopen(*(av+1),"r");
290: if ( !in_fp ) {
291: fprintf(stderr,"%s does not exist!",*(av+1));
292: asir_terminate(1);
293: }
294: do_file = 1;
295: av += 2; ac -= 2;
296: } else if ( !strcmp(*av,"-kernel") ) {
297: kernelmode = 1; av++; ac--;
298: } else if ( !strcmp(*av,"-norc") ) {
299: do_asirrc = 0; av++; ac--;
300: } else if ( !strcmp(*av,"-nomessage") ) {
301: do_message = 0; av++; ac--;
302: } else if ( !strcmp(*av,"-terse") ) {
303: no_prompt = 0; av++; ac--;
304: } else if ( !strcmp(*av,"-maxheap") && (ac >= 2) ) {
305: void GC_set_max_heap_size(int);
306:
307: GC_set_max_heap_size(atoi(*(av+1))); av += 2; ac -= 2;
308: #if !defined(VISUAL)
309: } else if ( !strcmp(*av,"-display") && (ac >= 2) ) {
310: strcpy(displayname,*(av+1)); av += 2; ac -= 2;
311: #endif
312: #if PARI
313: } else if ( !strcmp(*av,"-paristack") ) {
314: extern int paristack;
315:
316: paristack = atoi(*(av+1)); av += 2; ac -= 2;
317: #endif
318: } else {
319: fprintf(stderr,"%s : unknown option.\n",*av);
320: asir_terminate(1);
321: }
322: }
323: #endif
324: #if (defined(THINK_C) && !defined(__MWERKS__))
325: init_hist(MAXHIST);
326: read_hist(HISTORY);
327: #endif
328: }
329:
330: #include <signal.h>
331:
332: void sig_init() {
333: #if !defined(VISUAL)
334: signal(SIGINT,int_handler);
335: #else
336: register_ctrlc_handler();
337: #endif
338: signal(SIGSEGV,segv_handler);
339:
340: #if defined SIGFPE
341: signal(SIGFPE,fpe_handler);
342: #endif
343:
344: #if defined SIGPIPE
345: signal(SIGPIPE,pipe_handler);
346: #endif
347:
348: #if defined SIGILL
349: signal(SIGILL,ill_handler);
350: #endif
351:
352: #if defined(THINK_C)
353: buserr_sav = *((int *)0x8);
354: *((int *)0x8) = (int)bus_handler;
355: #else /* THINK_C */
356:
357: #if !defined(VISUAL)
358: signal(SIGBUS,bus_handler);
359: #endif
360:
361: #if 0
362: #if !defined(VISUAL) && !defined(_PA_RISC1_1)
363: signal(SIGWINCH,winch_handler);
364: #endif
365: #endif
366:
367: #endif /* THINK_C */
368: }
369:
370: static void (*old_int)(int);
371:
372: void asir_save_handler() {
373: old_int = signal(SIGINT,SIG_IGN);
374: signal(SIGINT,old_int);
375: }
376:
377: void asir_set_handler() {
378: signal(SIGINT,int_handler);
379: }
380:
381: void asir_reset_handler() {
382: signal(SIGINT,old_int);
383: }
384:
385: void resetenv(s)
386: char *s;
387: {
388: extern FILE *outfile;
389:
390: fprintf(stderr,"%s\n",s);
391: while ( NEXT(asir_infile) )
392: closecurrentinput();
393: resetpvs();
394: #if !defined(VISUAL)
395: if ( do_server_in_X11 )
396: #endif
397: show_debug_window(0);
398: #if defined(VISUAL_LIB)
399: w_noflush_stderr(0);
400: #endif
401: asir_out = stdout;
402: #if PARI
403: pari_outfile = stdout;
404: #endif
405: /* restore states */
406: reset_engine();
407: reset_io();
408: longjmp(env,1);
409: }
410:
411: void fatal(n)
412: int n;
413: {
414: resetenv("return to toplevel");
415: }
416:
417: FUNC registered_handler;
418: extern int ox_int_received, critical_when_signal;
419:
420: void int_handler(sig)
421: int sig;
422: {
423: extern NODE PVSS;
424:
425: if ( do_file ) {
426: ExitAsir();
427: }
428: if ( critical_when_signal ) {
429: ox_int_received = 1;
430: return;
431: }
432: #if defined(THINK_C) || defined(VISUAL)
433: suspend_timer(); signal(SIGINT,SIG_IGN);
434: #endif
435: #if defined(_PA_RISC1_1) || defined(linux) || defined(VISUAL) || defined(__svr4__)
436: signal(SIGINT,SIG_IGN);
437: #endif
438: #if defined(linux)
439: #if 1
440: while ( stdin->_IO_read_ptr < stdin->_IO_read_end )
441: #else
442: while ( stdin->_gptr < stdin->_egptr )
443: #endif
444: getchar();
445: #endif
446: while ( 1 ) {
447: char buf[BUFSIZ];
448: char c;
449:
450: fprintf(stderr,"interrupt ?(q/t/c/d/u/w/?) "); fflush(stderr);
451: if ( kernelmode )
452: fputc('\0',stderr);
453: buf[0] = '\n';
454: while ( buf[0] == '\n' )
455: fgets(buf,BUFSIZ,stdin);
456: switch ( c = buf[0] ) {
457: case 'q':
458: while ( 1 ) {
459: fprintf(stderr,"Abort this session? (y or n) "); fflush(stderr);
460: fgets(buf,BUFSIZ,stdin);
461: if ( !strncmp(buf,"y",1) ) {
462: read_exec_file = 0;
463: fprintf(stderr,"Bye\n"); asir_terminate(1);
464: } else if ( !strncmp(buf,"n",1) ) {
465: restore_handler();
466: return;
467: }
468: }
469: break;
470: case 't':
471: case 'u':
472: while ( 1 ) {
473: fprintf(stderr,"Abort this computation? (y or n) "); fflush(stderr);
474: fgets(buf,BUFSIZ,stdin);
475: if ( !strncmp(buf,"y",1) )
476: break;
477: else if ( !strncmp(buf,"n",1) ) {
478: restore_handler();
479: return;
480: }
481: }
482: if ( debug_mode )
483: debug_mode = 0;
484: if ( kernelmode )
485: fputc('\0',stderr);
486: restore_handler();
487: if ( c == 'u' ) {
488: if ( registered_handler ) {
489: fprintf(stderr,
490: "Calling the registered exception handler...");
491: bevalf(registered_handler,0);
492: fprintf(stderr, "done.\n");
493: }
494: }
495: if ( read_exec_file ) {
496: read_exec_file = 0;
497: resetenv("initialization aborted; return to toplevel");
498: } else
499: resetenv("return to toplevel");
500: break;
501: case 'd':
502: #if 0
503: nextbp = 1; nextbplevel = -1;
504: #endif
505: debug(PVSS?((VS)BDY(PVSS))->usrf->f.usrf->body:0);
506: restore_handler();
507: return;
508: case 'c':
509: if ( kernelmode )
510: fputc('\0',stderr);
511: restore_handler();
512: return; break;
513: case 'w':
514: showpos(); break;
515: case '?':
516: fprintf(stderr, "q:quit t:toplevel c:continue d:debug u:call registered handler w:where\n");
517: break;
518: default:
519: break;
520: }
521: }
522: }
523:
524: void restore_handler() {
525: #if defined(THINK_C) || defined(VISUAL)
526: resume_timer(); signal(SIGINT,int_handler);
527: #endif
528: #if defined(_PA_RISC1_1) || defined(linux) || defined(__svr4__)
529: signal(SIGINT,int_handler);
530: #endif
531: }
532:
533: void segv_handler(sig)
534: int sig;
535: {
536: #if defined(THINK_C) || defined(_PA_RISC1_1) || defined(linux) || defined(VISUAL) || defined(__svr4__)
537: signal(SIGSEGV,segv_handler);
538: #endif
539: error("internal error (SEGV)");
540: }
541:
542: void ill_handler(sig)
543: int sig;
544: {
545: #if defined(THINK_C) || defined(_PA_RISC1_1) || defined(linux) || defined(VISUAL) || defined(__svr4__)
546: signal(SIGILL,ill_handler);
547: #endif
548: error("illegal instruction (ILL)");
549: }
550:
551: void alrm_handler(sig)
552: int sig;
553: {
554: fprintf(stderr,"interval timer expired (VTALRM)\n");
555: longjmp(timer_env,1);
556: }
557:
558: void bus_handler(sig)
559: int sig;
560: {
561: #if defined(SIGBUS)
562: signal(SIGBUS,bus_handler);
563: error("internal error (BUS ERROR)");
564: #endif
565: }
566:
567: void fpe_handler(sig)
568: int sig;
569: {
570: #if defined(THINK_C) || defined(_PA_RISC1_1) || defined(linux) || defined(VISUAL) || defined(__svr4__)
571: signal(SIGFPE,fpe_handler);
572: #endif
573: error("internal error (FPE)");
574: }
575:
576: void winch_handler(sig)
577: int sig;
578: {
579: #if 0
580: #if !defined(THINK_C) && !defined(VISUAL) && !defined(_PA_RISC1_1) && !defined(__svr4__)
581: if ( isatty(1) ) {
582: struct winsize t;
583:
584: if ( ioctl(1,TIOCGWINSZ,&t) == -1 )
585: perror("TIOCGWINSZ");
586: if ( t.ws_col != wsize.ws_col || t.ws_row != wsize.ws_row ) {
587: resize_buffer();
588: if ( killpg(getpgrp(0),SIGWINCH) == -1 )
589: perror("killpg");
590: }
591: }
592: #endif
593: #endif
594: }
595:
596: void pipe_handler(sig)
597: int sig;
598: {
599: #if defined(SIGPIPE)
600: signal(SIGPIPE,pipe_handler);
601: error("internal error (BROKEN PIPE)");
602: #endif
603: }
604:
605: void resize_buffer()
606: {
607: #if 0 && !defined(THINK_C) && !defined(VISUAL) && !defined(_PA_RISC1_1)
608: if ( isatty(1) ) {
609: if ( ioctl(1,TIOCGWINSZ,&wsize) == -1 )
610: perror("TIOCGWINSZ");
611: if ( wsize.ws_col > 2 ) {
612: ttywidth = wsize.ws_col - 2;
613: upperbuf = (char *)MALLOC(wsize.ws_col);
614: lowerbuf = (char *)MALLOC(wsize.ws_col);
615: }
616: }
617: #endif
618: }
619:
620: void tty_init() {
621: #if 0 && !defined(THINK_C) && !defined(VISUAL) && !defined(_PA_RISC1_1)
622: if ( isatty(0) ) {
623: if ( ioctl(0,TIOCGETC,&tc) == -1 )
624: perror("TIOCGETC");
625: oldeof = tc.t_eofc; tc.t_eofc = 0xff;
626: if ( ioctl(0,TIOCSETC,&tc) == -1 )
627: perror("TIOCSETC");
628: setpgrp(0, getpid());
629: }
630: resize_buffer();
631: #endif
632: }
633:
634: void tty_reset() {
635: #if 0 && !defined(THINK_C) && !defined(VISUAL) && !defined(_PA_RISC1_1)
636: if ( oldeof ) {
637: tc.t_eofc = oldeof;
638: if ( ioctl(0,TIOCSETC,&tc) == -1 )
639: perror("TIOCSETC");
640: }
641: #endif
642: }
643:
644: extern int evalstatline;
645:
646: void set_lasterror(s)
647: char *s;
648: {
649: strncpy(LastError,s,BUFSIZ);
650: LastError[BUFSIZ-1] = 0;
651: }
652:
653: SNODE error_snode;
654:
655: void error(s)
656: char *s;
657: {
658: SNODE *snp;
659:
660: fprintf(stderr,"%s\n",s);
661: set_lasterror(s);
662: if ( CPVS != GPVS ) {
663: searchsn(&BDY(CPVS->usrf->f.usrf),evalstatline,&snp);
664: error_snode = *snp;
665: } else
666: error_snode = 0;
667: if ( do_file ) {
668: char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
669:
670: sprintf(errbuf,"%s\n",s);
671: showpos_to_string(errbuf+strlen(errbuf));
672: set_lasterror(errbuf);
673: ExitAsir();
674: }
675: if ( debug_mode )
676: longjmp(debug_env,1);
677: if ( CPVS != GPVS )
678: if ( do_server_in_X11 || isatty(0) )
679: bp(error_snode);
680: if ( read_exec_file )
681: read_exec_file = 0;
682: resetenv("return to toplevel");
683: }
684:
685: #if !defined(VISUAL)
686: #include <sys/time.h>
687:
688: void set_timer(interval)
689: int interval;
690: {
691: struct itimerval it;
692:
693: it.it_interval.tv_sec = interval;
694: it.it_interval.tv_usec = 0;
695: it.it_value.tv_sec = interval;
696: it.it_value.tv_usec = 0;
697: setitimer(ITIMER_VIRTUAL,&it,0);
698: signal(SIGVTALRM,alrm_handler);
699: }
700:
701: void reset_timer()
702: {
703: struct itimerval it;
704:
705: it.it_interval.tv_sec = 0;
706: it.it_interval.tv_usec = 0;
707: it.it_value.tv_sec = 0;
708: it.it_value.tv_usec = 0;
709: setitimer(ITIMER_VIRTUAL,&it,0);
710: signal(SIGVTALRM,SIG_IGN);
711: }
712: #endif
713:
714: unsigned int get_asir_version();
715:
716: void copyright() {
1.2 ! noro 717: printf("This is Risa/Asir, Version %d.\n",get_asir_version());
1.1 noro 718: printf("Copyright (C) FUJITSU LABORATORIES LIMITED.\n");
1.2 ! noro 719: printf("1994-1999. All rights reserved.\n");
1.1 noro 720: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>