Annotation of OpenXM_contrib2/asir2018/parse/glob.c, Revision 1.11
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.11 ! ohara 48: * $OpenXM: OpenXM_contrib2/asir2018/parse/glob.c,v 1.10 2021/03/25 23:25:03 noro Exp $
1.1 noro 49: */
50: #include "ca.h"
51: #include "al.h"
52: #include "parse.h"
53: #include "ox.h"
54: #include <signal.h>
55: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(_PA_RISC1_1) && !defined(linux) && !defined(SYSV) && !defined(__CYGWIN__) && !defined(__INTERIX) && !defined(__FreeBSD__)
56: #include <sgtty.h>
57: #endif
58:
59: #if defined(VISUAL) || defined(__MINGW32__)
60: #include <io.h>
61: #include <direct.h>
62: #else
63: #include <unistd.h>
64: #include <string.h>
65: #include <stdio.h>
66: #endif
67:
68: #if defined(SYSV) && !defined(_IBMR2)
69: #include <sys/ttold.h>
70: #endif
71:
72: #if defined(VISUAL) || defined(__MINGW32__)
73: #define HISTORY asir_history
74: #endif
75:
76: #define MAXHIST 100
77:
78: extern FILE *asir_out;
79:
80: INFILE asir_infile;
81: JMP_BUF main_env,debug_env,timer_env,exec_env;
82: int little_endian,debug_mode,no_debug_on_error;
83: char *asir_libdir;
84: char *asir_contrib_dir;
85: char *asir_private_dir;
86: char *asir_pager;
87:
88: NODE usrf,sysf,noargsysf,ubinf,parif;
89: NODE ONENODE;
90: int main_parser, allow_create_var, ox_do_copy, ox_do_count, ox_count_length;
91: int ox_file_io, ox_need_conv;
92: char *ox_copy_bptr;
93: char *parse_strp;
94: SNODE parse_snode;
95: FUNC parse_targetf;
96: FILE *ox_istream,*ox_ostream;
97: int do_server_in_X11;
98: Obj LastVal;
99: LIST LastStackTrace;
100: char LastError[BUFSIZ];
101: int timer_is_set;
102: NODE current_option;
103: NODE user_int_handler, user_quit_handler;
104:
105: struct oV oVAR[] = {
106: {"x",0,0}, {"y",0,0}, {"z",0,0}, {"u",0,0},
107: {"v",0,0}, {"w",0,0}, {"p",0,0}, {"q",0,0},
108: {"r",0,0}, {"s",0,0}, {"t",0,0}, {"a",0,0},
109: {"b",0,0}, {"c",0,0}, {"d",0,0}, {"e",0,0},
110: {"f",0,0}, {"g",0,0}, {"h",0,0}, {"i",0,0},
111: {"j",0,0}, {"k",0,0}, {"l",0,0}, {"m",0,0},
112: {"n",0,0}, {"o",0,0},
113: {"_x",0,0}, {"_y",0,0}, {"_z",0,0}, {"_u",0,0},
114: {"_v",0,0}, {"_w",0,0}, {"_p",0,0}, {"_q",0,0},
115: {"_r",0,0}, {"_s",0,0}, {"_t",0,0}, {"_a",0,0},
116: {"_b",0,0}, {"_c",0,0}, {"_d",0,0}, {"_e",0,0},
117: {"_f",0,0}, {"_g",0,0}, {"_h",0,0}, {"_i",0,0},
118: {"_j",0,0}, {"_k",0,0}, {"_l",0,0}, {"_m",0,0},
119: {"_n",0,0}, {"_o",0,0}
120: };
121:
122: struct oVL oVLIST[52];
123:
124: VL CO = oVLIST;
125: VL ALG;
126: VL LASTCO;
127:
128: struct oVS oGPVS,oAPVS,oEPVS,oPPVS;
129: VS GPVS = &oGPVS;
130: VS APVS = &oAPVS;
131: VS EPVS = &oEPVS;
132: VS PPVS = &oPPVS;
133: VS CPVS,MPVS;
134:
135: NODE MODULE_LIST;
136: MODULE CUR_MODULE;
137: char *CUR_FUNC;
138:
139: struct oSYMBOL oGrlex, oGlex, oLex;
140: SYMBOL Symbol_grlex = &oGrlex;
141: SYMBOL Symbol_glex = &oGlex;
142: SYMBOL Symbol_lex = &oLex;;
143:
144: struct oF oF_TRUE,oF_FALSE;
145: F F_TRUE = &oF_TRUE;
146: F F_FALSE = &oF_FALSE;
147:
148: #if defined(VISUAL) || defined(__MINGW32__)
149: char cppname[BUFSIZ] = "c:\\asir\\stdlib\\cpp ";
150: #else
151: char cppname[BUFSIZ] = "/lib/cpp ";
152: #endif
153: char asirname[BUFSIZ];
154: char displayname[BUFSIZ];
155:
156: int Verbose;
157: int do_quiet;
158:
159: void glob_init() {
160: int i;
161:
162: for ( i = 0; i < 51; i++ ) {
163: VR(&oVLIST[i]) = &oVAR[i]; NEXT(&oVLIST[i]) = &oVLIST[i+1];
164: }
165: VR(&oVLIST[i]) = &oVAR[i]; NEXT(&oVLIST[i]) = 0;
166: LASTCO = &oVLIST[i];
167: asir_reallocarray((char **)&GPVS->va,(int *)&GPVS->asize,(int *)&GPVS->n,(int)sizeof(struct oPV));
168: asir_reallocarray((char **)&APVS->va,(int *)&APVS->asize,(int *)&APVS->n,(int)sizeof(struct oPV));
169: asir_reallocarray((char **)&PPVS->va,(int *)&PPVS->asize,(int *)&PPVS->n,(int)sizeof(struct oPV));
170: CPVS = GPVS;
171: MKNODE(ONENODE,mkfnode(1,I_FORMULA,ONE),NULLP);
172: OID(F_TRUE)=O_F; FOP(F_TRUE)=AL_TRUE; F_TRUE->arg.dummy = 0;
173: OID(F_FALSE)=O_F; FOP(F_FALSE)=AL_FALSE; F_FALSE->arg.dummy = 0;
174: OID(Symbol_grlex) = O_SYMBOL; Symbol_grlex->name = "@grlex";
175: Symbol_grlex->value = 0;
176: OID(Symbol_glex) = O_SYMBOL; Symbol_glex->name = "@glex";
177: Symbol_glex->value = 1;
178: OID(Symbol_lex) = O_SYMBOL; Symbol_lex->name = "@lex";
179: Symbol_lex->value = 2;
180: sprintf(asirname,"%s/asir_symtab",asir_libdir);
181: }
182:
183: void input_init(FILE *fp,char *name)
184: {
185: asir_infile = (INFILE)CALLOC(sizeof(struct oINFILE),1);
186: asir_infile->name = name; asir_infile->fp = fp;
187: asir_infile->ln = 1; NEXT(asir_infile) = 0;
188: }
189:
190: void notdef(VL vl,Obj a,Obj b,Obj *c)
191: {
192: error("undefined arithmetic operation.");
193: }
194:
195: int disable_debugger;
196: int do_asirrc;
197: int do_file;
198: char *do_filename;
199: int do_message;
200: int do_terse;
201: int do_fep;
202: int asir_setenv;
203: static int buserr_sav;
204: static char asir_history[BUFSIZ];
205:
206: extern int mpi_myid;
207:
208: #if !defined(VISUAL_LIB)
209: void ExitAsir() {
210: exit(0);
211: }
212: #endif
213:
214: /*
215: * status = 1 abnormal termination (error() etc.)
216: * status = 2 normal termination (end(), quit() etc.)
217: * status = 3 absolute termination
218: */
219:
220: void asir_terminate(int status)
221: {
222: int t;
223: NODE n;
224:
225: /* called from engine in Windows */
226: if ( status == 3 ) asir_infile = 0;
227:
228: if ( asir_infile && asir_infile->ready_for_longjmp )
229: LONGJMP(asir_infile->jmpbuf,status);
230: else {
231: if ( user_quit_handler ) {
232: if ( !do_terse ) {
233: fprintf(stderr,"Calling the registered quit callbacks...");
234: #if defined(__MINGW32__)
235: fflush(stderr);
236: #endif
237: }
238: for ( n = user_quit_handler; n; n = NEXT(n) )
239: bevalf((FUNC)BDY(n),0);
240: if ( !do_terse ) {
241: fprintf(stderr, "done.\n");
242: #if defined(__MINGW32__)
243: fflush(stderr);
244: #endif
245: }
246: }
247: tty_reset();
248: #if defined(MPI)
249: if ( !mpi_myid )
250: close_allconnections();
251: mpi_finalize();
252: #else
253: #if defined(SIGPIPE)
254: set_signal(SIGPIPE,SIG_IGN);
255: #endif
256: close_allconnections();
257: #endif
258: if ( asir_out )
259: fflush(asir_out);
1.11 ! ohara 260: #if defined(FEP)
1.1 noro 261: if ( do_fep ) {
262: stifle_history(MAXHIST);
263: write_history(asir_history);
264: }
265: #endif
266: ExitAsir();
267: }
268: }
269:
270: void param_init() {
271: unsigned int et = 0xff;
272: extern int paristack;
273: if ( *((char *)&et) )
274: little_endian = 1;
275: else
276: little_endian = 0;
277: }
278:
279: Obj user_defined_prompt;
280:
281: void prompt() {
282: if ( !do_quiet && !do_fep && asir_infile->fp == stdin ) {
283: fprintf(asir_out,"[%d] ",APVS->n);
284: fflush(asir_out);
285: } else if ( do_quiet && user_defined_prompt
286: && OID(user_defined_prompt)==O_STR) {
287: fprintf(asir_out,BDY((STRING)user_defined_prompt),APVS->n);
288: fflush(asir_out);
289: }
290: }
291:
292: void sprompt(char *ptr)
293: {
294: sprintf(ptr,"[%d] ",APVS->n);
295: }
296:
297: #if !defined(VISUAL) && !defined(__MINGW32__)
298: static int which(char *prog, char *path, char *buf, size_t size)
299: {
300: char *tok;
301: char delim[] = ":";
302: char *path2 = malloc(strlen(path)+1);
303: char *name = malloc(size);
304: int proglen = strlen(prog)+3; /* "/" + prog + " \0" */
305:
306: if (!name || !path2) {
307: return 0;
308: }
309: strcpy(path2, path);
310: tok = strtok(path2, delim);
311: while (tok != NULL) {
312: if (size >= strlen(tok)) {
313: sprintf(name, "%s/%s", tok, prog);
314: if (access(name, X_OK&R_OK) == 0) {
315: strcpy(buf, name);
316: strcat(buf, " ");
317: free(path2); free(name);
318: return 1;
319: }
320: tok = strtok(NULL, delim);
321: }
322: }
323: free(path2); free(name);
324: return 0;
325: }
326: #endif
327:
328: void cppname_init()
329: {
330: #if !defined(VISUAL) && !defined(__MINGW32__)
331: char *oxhome;
332: char oxcpp[BUFSIZ];
333: #define OXCPP "/bin/ox_cpp"
334:
1.8 noro 335: if ( ( oxhome = getenv("OpenXM_HOME") ) != 0 ) {
1.1 noro 336: if ( strlen(oxhome)+strlen(OXCPP)<BUFSIZ ) {
337: sprintf(oxcpp,"%s%s",oxhome,OXCPP);
338: if ( access(oxcpp,X_OK&R_OK) == 0 ) {
339: strcpy(cppname,oxcpp);
340: return;
341: }
342: }
343: }
344: if (access(cppname, X_OK&R_OK) != 0) {
345: which("cpp", "/lib:/usr/ccs/lib:/usr/bin", cppname, BUFSIZ) ||
346: which("cpp", getenv("PATH"), cppname, BUFSIZ);
347: }
348: #endif
349: }
350:
351: FILE *in_fp;
352:
353: void process_args(int ac,char **av)
354: {
355: int nm,dv;
356: do_asirrc = 1;
357: #if !defined(MPI)
358: do_message = 1;
359: #endif
360: #if defined(VISUAL) && defined(VISUAL_CONSOLE)
361: disable_debugger=1;
362: #endif
1.11 ! ohara 363: #if defined(FEP)
! 364: do_fep = 1;
! 365: #endif
1.1 noro 366: do_quiet = 0;
367: while ( ac > 0 ) {
368: if ( !strcmp(*av,"-heap") && (ac >= 2) ) {
369: void GC_expand_hp(int);
370:
371: GC_expand_hp(atoi(*(av+1))); av += 2; ac -= 2;
372: } else if ( !strcmp(*av,"-adj") && (ac >= 2) ) {
373: char *slash;
374: slash = strrchr(*(av+1),'/');
375: if ( slash ) {
376: *slash = 0;
377: nm = atoi(slash+1);
378: }else {
379: nm = 1;
380: }
381: dv = atoi(*(av+1));
382: Risa_GC_set_adj(nm,dv);
383: av += 2; ac -= 2;
384: } else if ( !strcmp(*av,"-cpp") && (ac >= 2) ) {
385: strcpy(cppname,*(av+1)); av += 2; ac -= 2;
386: } else if ( !strcmp(*av,"-d") && (ac >= 2) ) {
387: #if defined(VISUAL) && defined(VISUAL_CONSOLE)
388: disable_debugger=0;
389: #endif
390: av += 2; ac -= 2;
391: } else if ( !strcmp(*av,"-f") && (ac >= 2) ) {
392: do_quiet = 1;
393: in_fp = fopen(*(av+1),"r");
394: if ( !in_fp ) {
395: fprintf(stderr,"%s does not exist!\n",*(av+1));
396: #if defined(__MINGW32__)
397: fflush(stderr);
398: #endif
399: asir_terminate(1);
400: }
401: do_file = 1;
402: do_filename = *(av+1);
403: av += 2; ac -= 2;
404: } else if ( !strcmp(*av,"-E") ) {
405: asir_setenv = 1; av++; ac--;
406: } else if ( !strcmp(*av,"-quiet") ) {
407: do_quiet = 1; av++; ac--;
408: } else if ( !strcmp(*av,"-norc") ) {
409: do_asirrc = 0; av++; ac--;
410: } else if ( !strcmp(*av,"-nomessage") ) {
411: do_message = 0; av++; ac--;
412: } else if ( !strcmp(*av,"-rootdir") && (ac >= 2) ) {
413: set_rootdir(*(av+1)); av += 2; ac -= 2;
414: } else if ( !strcmp(*av,"-maxheap") && (ac >= 2) ) {
415: void GC_set_max_heap_size(int);
416:
417: GC_set_max_heap_size(atoi(*(av+1))); av += 2; ac -= 2;
418: #if !defined(VISUAL) && !defined(__MINGW32__)
419: } else if ( !strcmp(*av,"-display") && (ac >= 2) ) {
420: strcpy(displayname,*(av+1)); av += 2; ac -= 2;
421: #endif
1.11 ! ohara 422: #if defined(FEP)
1.1 noro 423: } else if ( !strcmp(*av,"-fep") ) {
424: do_fep = 1; av++; ac--;
1.11 ! ohara 425: } else if ( !strcmp(*av,"-nofep") ) {
! 426: do_fep = 0; av++; ac--;
1.1 noro 427: #endif
428: } else {
429: fprintf(stderr,"%s : unknown option.\n",*av);
430: #if defined(__MINGW32__)
431: fflush(stderr);
432: #endif
433: asir_terminate(1);
434: }
435: }
1.11 ! ohara 436: #if defined(FEP)
1.1 noro 437: if ( do_fep ) {
438: char *home;
439: home = (char *)getenv("HOME");
440: if (!home)
441: home = ".";
442: sprintf (asir_history, "%s/.asir_history",home);
443: read_history(asir_history);
444: using_history();
445: }
446: #endif
447: }
448:
449: #if defined(HAVE_SIGACTION)
450: void (*set_signal(int sig, void (*handler)(int)))(int)
451: {
452: struct sigaction act;
453: struct sigaction oldact;
454: if (handler == SIG_IGN || handler == SIG_DFL) {
455: return signal(sig,handler);
456: }
457: act.sa_handler=handler;
458: act.sa_flags=0;
459: act.sa_flags |= SA_RESTART;
460: sigemptyset(&act.sa_mask);
461: sigaction(sig,&act,&oldact);
462: return oldact.sa_handler;
463: }
464: #endif
465:
466: void sig_init() {
467: #if !defined(VISUAL) && !defined(__MINGW32__)
468: set_signal(SIGINT,int_handler);
469: #else
470: void register_ctrlc_handler();
471:
472: register_ctrlc_handler();
473: #endif
474: set_signal(SIGSEGV,segv_handler);
475:
476: #if defined(SIGFPE)
477: set_signal(SIGFPE,fpe_handler);
478: #endif
479:
480: #if defined(SIGPIPE)
481: set_signal(SIGPIPE,pipe_handler);
482: #endif
483:
484: #if defined(SIGILL)
485: set_signal(SIGILL,ill_handler);
486: #endif
487:
488: #if !defined(VISUAL) && !defined(__MINGW32__)
489: set_signal(SIGBUS,bus_handler);
490: #endif
491: }
492:
493: static void (*old_int)(int);
494:
495: void asir_save_handler() {
496: old_int = set_signal(SIGINT,SIG_IGN);
497: set_signal(SIGINT,old_int);
498: }
499:
500: void asir_set_handler() {
501: set_signal(SIGINT,int_handler);
502: }
503:
504: void asir_reset_handler() {
505: set_signal(SIGINT,old_int);
506: }
507:
508: extern int I_am_server;
509:
510: void resetenv(char *s)
511: {
512: extern FILE *outfile;
513:
514: fprintf(stderr,"%s\n",s);
515: while ( NEXT(asir_infile) )
516: closecurrentinput();
517: resetpvs();
518: #if !defined(VISUAL) && !defined(__MINGW32__)
519: if ( do_server_in_X11 )
520: #endif
521: show_debug_window(0);
522: #if defined(VISUAL_LIB)
523: w_noflush_stderr(0);
524: #endif
525: #if defined(__MINGW32__)
526: fflush(stderr);
527: #endif
528: asir_out = stdout;
529: /* restore states */
530: reset_engine();
531: reset_io();
532: #if !defined(VISUAL) && !defined(__MINGW32__)
533: reset_timer();
534: #endif
535: LONGJMP(main_env,1);
536: }
537:
538: void fatal(int n)
539: {
540: resetenv("return to toplevel");
541: }
542:
543: extern int ox_int_received, critical_when_signal;
544: extern int in_gc, caught_intr;
545: extern int ox_get_pari_result;
546:
547: void int_handler(int sig)
548: {
549: extern NODE PVSS;
550: NODE t;
551:
552: if ( do_file || disable_debugger ) {
553: LEAVE_SIGNAL_CS_ALL;
554: ExitAsir();
555: }
556: if ( !ox_get_pari_result && critical_when_signal ) {
557: ox_int_received = 1;
558: return;
559: }
560: if ( in_gc ) {
561: caught_intr = 1;
562: return;
563: }
564: #if defined(VISUAL) || defined(__MINGW32__)
565: suspend_timer();
566: #endif
567: set_signal(SIGINT,SIG_IGN);
568: #if !defined(VISUAL) && !defined(__MINGW32__)
569: if ( do_server_in_X11 ) {
570: debug(PVSS?((VS)BDY(PVSS))->usrf->f.usrf->body:0);
571: restore_handler();
572: return;
573: }
574: #endif
1.9 fujimoto 575: #if defined(linux)
576: #if !defined(__GLIBC__) && !defined(ANDROID)
577: if( __freadahead(stdin) > 0 ) __fpurge(stdin);
1.1 noro 578: #else
1.9 fujimoto 579: while ( FP_DATA_IS_AVAILABLE(stdin) )
580: getchar();
1.1 noro 581: #endif
582: #endif
583: while ( 1 ) {
584: char buf[BUFSIZ];
585: char c;
586:
587: fprintf(stderr,"interrupt ?(q/t/c/d/u/w/?) "); fflush(stderr);
588: buf[0] = '\n';
589: while ( !feof(stdin) && buf[0] == '\n' )
590: fgets(buf,BUFSIZ,stdin);
591: if ( feof(stdin) ) {
592: clearerr(stdin);
593: continue;
594: }
595: switch ( c = buf[0] ) {
596: case 'q':
597: while ( 1 ) {
598: fprintf(stderr,"Abort this session? (y or n) "); fflush(stderr);
599: fgets(buf,BUFSIZ,stdin);
600: if ( !strncmp(buf,"y",1) ) {
601: fprintf(stderr,"Bye\n");
602: #if defined(__MINGW32__)
603: fflush(stderr);
604: #endif
605: /* for terminating myself */
606: asir_infile = 0;
607: asir_terminate(1);
608: } else if ( !strncmp(buf,"n",1) ) {
609: restore_handler();
610: return;
611: }
612: }
613: break;
614: case 't':
615: case 'u':
616: while ( 1 ) {
617: fprintf(stderr,"Abort this computation? (y or n) "); fflush(stderr);
618: fgets(buf,BUFSIZ,stdin);
619: if ( !strncmp(buf,"y",1) )
620: break;
621: else if ( !strncmp(buf,"n",1) ) {
622: restore_handler();
623: return;
624: }
625: }
626: if ( debug_mode )
627: debug_mode = 0;
628: restore_handler();
629: if ( c == 'u' ) {
630: if ( user_int_handler ) {
631: if ( !do_terse ) {
632: fprintf(stderr,
633: "Calling the registered exception callbacks...");
634: #if defined(__MINGW32__)
635: fflush(stderr);
636: #endif
637: }
638: for ( t = user_int_handler; t; t = NEXT(t) )
639: bevalf((FUNC)BDY(t),0);
640: if ( !do_terse ) {
641: fprintf(stderr, "done.\n");
642: #if defined(__MINGW32__)
643: fflush(stderr);
644: #endif
645: }
646: }
647: }
648: LEAVE_SIGNAL_CS_ALL;
649: resetenv("return to toplevel");
650: break;
651: case 'd':
652: #if 0
653: nextbp = 1; nextbplevel = -1;
654: #endif
655: debug(PVSS?((VS)BDY(PVSS))->usrf->f.usrf->body:0);
656: restore_handler();
657: return;
658: case 'c':
659: restore_handler();
660: return; break;
661: case 'w':
662: showpos();
663: break;
664: case '?':
665: fprintf(stderr, "q:quit t:toplevel c:continue d:debug u:call registered handler w:where\n");
666: #if defined(__MINGW32__)
667: fflush(stderr);
668: #endif
669: break;
670: default:
671: break;
672: }
673: }
674: }
675:
676: void restore_handler() {
677: #if defined(VISUAL) || defined(__MINGW32__)
678: resume_timer();
679: #endif
680: #if defined(SIGINT)
681: set_signal(SIGINT,int_handler);
682: #endif
683: }
684:
685: void segv_handler(int sig)
686: {
687: #if defined(SIGSEGV)
688: set_signal_for_restart(SIGSEGV,segv_handler);
689: error("internal error (SEGV)");
690: #endif
691: }
692:
693: void ill_handler(int sig)
694: {
695: #if defined(SIGILL)
696: set_signal_for_restart(SIGILL,ill_handler);
697: error("illegal instruction (ILL)");
698: #endif
699: }
700:
701: #if defined(__DARWIN__)
702: #define SIGNAL_FOR_TIMER SIGALRM
703: #define ITIMER_TYPE ITIMER_REAL
704: #else
705: #define SIGNAL_FOR_TIMER SIGVTALRM
706: #define ITIMER_TYPE ITIMER_VIRTUAL
707: #endif
708:
709: void alrm_handler(int sig)
710: {
711: fprintf(stderr,"interval timer expired (VTALRM)\n");
712: #if defined(__MINGW32__)
713: fflush(stderr);
714: #endif
715: LONGJMP(timer_env,1);
716: }
717:
718: void bus_handler(int sig)
719: {
720: #if defined(SIGBUS)
721: set_signal_for_restart(SIGBUS,bus_handler);
722: error("internal error (BUS ERROR)");
723: #endif
724: }
725:
726: void fpe_handler(int sig)
727: {
728: #if defined(SIGFPE)
729: set_signal_for_restart(SIGFPE,fpe_handler);
730: error("internal error (FPE)");
731: #endif
732: }
733:
734: void pipe_handler(int sig)
735: {
736: #if defined(SIGPIPE)
737: set_signal_for_restart(SIGPIPE,pipe_handler);
738: end_critical();
739: error("internal error (BROKEN PIPE)");
740: #endif
741: }
742:
743: void resize_buffer()
744: {
745: }
746:
747: void tty_init() {
748: }
749:
750: void tty_reset() {
751: }
752:
753: extern int evalstatline;
754:
755: void set_lasterror(char *s)
756: {
757: strncpy(LastError,s,BUFSIZ);
758: LastError[BUFSIZ-1] = 0;
759: }
760:
761: SNODE error_snode;
762: int error_in_timer;
763:
764: void error(char *s)
765: {
766: SNODE *snp=0;
767:
768: #if !defined(VISUAL) && !defined(__MINGW32__)
769: if ( !error_in_timer && timer_is_set )
770: alrm_handler(SIGNAL_FOR_TIMER);
771: #endif
772: fprintf(stderr,"%s\n",s);
773: #if defined(__MINGW32__)
774: fflush(stderr);
775: #endif
776: set_lasterror(s);
777: if ( CPVS != GPVS ) {
778: if ( CPVS && CPVS->usrf && CPVS->usrf->f.usrf )
779: searchsn(&BDY(CPVS->usrf->f.usrf),evalstatline,&snp);
780: if ( snp )
781: error_snode = *snp;
782: else
783: error_snode = 0;
784: } else
785: error_snode = 0;
786: if ( do_file ) {
787: char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
788:
789: sprintf(errbuf,"%s\n",s);
790: showpos_to_string(errbuf+strlen(errbuf));
791: set_lasterror(errbuf);
792: ExitAsir();
793: }
794: if ( debug_mode )
795: LONGJMP(debug_env,1);
796: if ( CPVS != GPVS )
797: if ( !no_debug_on_error && (do_server_in_X11 || isatty(0)) )
798: bp(error_snode);
799: if ( I_am_server )
800: showpos_to_list(&LastStackTrace);
801: resetenv("return to toplevel");
802: }
803:
804: void goto_toplevel(char *s)
805: {
806: SNODE *snp=0;
807:
808: #if !defined(VISUAL) && !defined(__MINGW32__)
809: if ( timer_is_set )
810: alrm_handler(SIGNAL_FOR_TIMER);
811: #endif
812: fprintf(stderr,"%s\n",s);
813: #if defined(__MINGW32__)
814: fflush(stderr);
815: #endif
816: if ( do_file ) {
817: char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
818:
819: sprintf(errbuf,"%s\n",s);
820: showpos_to_string(errbuf+strlen(errbuf));
821: ExitAsir();
822: }
823: resetenv("return to toplevel");
824: }
825:
826: #if !defined(VISUAL) && !defined(__MINGW32__)
827: #include <sys/time.h>
828:
829: void set_timer(int interval)
830: {
831: struct itimerval it;
832:
833: it.it_interval.tv_sec = interval;
834: it.it_interval.tv_usec = 0;
835: it.it_value.tv_sec = interval;
836: it.it_value.tv_usec = 0;
837: setitimer(ITIMER_TYPE,&it,0);
838: set_signal(SIGNAL_FOR_TIMER,alrm_handler);
839: timer_is_set = 1;
840: }
841:
842: void reset_timer()
843: {
844: struct itimerval it;
845:
846: it.it_interval.tv_sec = 0;
847: it.it_interval.tv_usec = 0;
848: it.it_value.tv_sec = 0;
849: it.it_value.tv_usec = 0;
850: setitimer(ITIMER_TYPE,&it,0);
851: set_signal(SIGNAL_FOR_TIMER,SIG_IGN);
852: timer_is_set = 0;
853: }
854: #endif
855:
856: unsigned int get_asir_version();
857: char *get_asir_distribution();
858:
859: char *get_gcversion()
860: {
1.10 noro 861: return "GC 7.6.12 copyright 1988-2018, H-J. Boehm, A. J. Demers, Xerox, SGI, HP, I. Maidanski.\n";
1.1 noro 862: }
863:
864: char *get_pariversion()
865: {
866: return "";
867: }
868:
869: char *get_intervalversion()
870: {
871: #if defined(INTERVAL)
872: return " + Interval Arithmetic";
873: #else
874: return "";
875: #endif
876: }
877:
878: void copyright()
879: {
880: char *scopyright();
881: fputs(scopyright(), stdout);
882: }
883:
884: char *scopyright()
885: {
886: static char *notice;
887: char *s1, *s2, *s3, *s4;
888: int d, len;
1.10 noro 889: char *format = "This is Risa/Asir%s, full GMP Version %d (%s Distribution).\nCopyright (C) 1994-2000, all rights reserved, FUJITSU LABORATORIES LIMITED.\nCopyright 2000-2021, Risa/Asir committers, http://www.openxm.org/.\n%s%s";
1.1 noro 890: if (!notice) {
891: s1 = get_intervalversion();
892: s2 = get_asir_distribution();
893: s3 = get_gcversion();
894: s4 = get_pariversion();
895: d = get_asir_version();
896: len = (strlen(format)-8)+strlen(s1)+strlen(s2)+strlen(s3)+strlen(s4)+sizeof(int)*3;
897: notice = MALLOC(len+1);
898: sprintf(notice, format, s1, d, s2, s3, s4);
899: }
900: return notice;
901: }
902:
903: #if defined(VISUAL) || defined(__MINGW32__)
904: int recv_intr;
905:
906: static CRITICAL_SECTION signal_cs;
907: static int initialized_signal_cs;
908: static int signal_cs_count;
909:
910: static void init_signal_cs()
911: {
912: if (!initialized_signal_cs) {
913: InitializeCriticalSection(&signal_cs);
914: initialized_signal_cs=1;
915: signal_cs_count=0;
916: }
917: }
918:
919: void try_enter_signal_cs()
920: {
921: init_signal_cs();
922: if(TryEnterCriticalSection(&signal_cs)) {
923: signal_cs_count++;
924: }
925: }
926:
927: void enter_signal_cs()
928: {
929: init_signal_cs();
930: EnterCriticalSection(&signal_cs);
931: signal_cs_count++;
932: }
933:
934: void leave_signal_cs()
935: {
936: init_signal_cs();
937: if(signal_cs_count>0) {
938: LeaveCriticalSection(&signal_cs);
939: signal_cs_count--;
940: }
941: }
942:
943: void leave_signal_cs_all()
944: {
945: if (!initialized_signal_cs) {
946: init_signal_cs();
947: }
948: while(signal_cs_count>0) {
949: LeaveCriticalSection(&signal_cs);
950: signal_cs_count--;
951: }
952: }
953:
954: void check_intr()
955: {
956: extern int recv_intr;
957: enter_signal_cs();
958: if ( recv_intr ) {
959: if ( recv_intr == 1 ) {
960: recv_intr = 0;
961: int_handler(SIGINT);
962: } else {
963: recv_intr = 0;
964: ox_usr1_handler(0);
965: }
966: }
967: leave_signal_cs_all();
968: }
969: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>