Annotation of OpenXM_contrib2/asir2018/parse/glob.c, Revision 1.6
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.6 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2018/parse/glob.c,v 1.5 2020/03/29 17:01:55 fujimoto 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);
260: #if FEP
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:
335: if ( oxhome = getenv("OpenXM_HOME") ) {
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
363: do_quiet = 0;
364: while ( ac > 0 ) {
365: if ( !strcmp(*av,"-heap") && (ac >= 2) ) {
366: void GC_expand_hp(int);
367:
368: GC_expand_hp(atoi(*(av+1))); av += 2; ac -= 2;
369: } else if ( !strcmp(*av,"-adj") && (ac >= 2) ) {
370: char *slash;
371: slash = strrchr(*(av+1),'/');
372: if ( slash ) {
373: *slash = 0;
374: nm = atoi(slash+1);
375: }else {
376: nm = 1;
377: }
378: dv = atoi(*(av+1));
379: Risa_GC_set_adj(nm,dv);
380: av += 2; ac -= 2;
381: } else if ( !strcmp(*av,"-cpp") && (ac >= 2) ) {
382: strcpy(cppname,*(av+1)); av += 2; ac -= 2;
383: } else if ( !strcmp(*av,"-d") && (ac >= 2) ) {
384: #if defined(VISUAL) && defined(VISUAL_CONSOLE)
385: disable_debugger=0;
386: #endif
387: av += 2; ac -= 2;
388: } else if ( !strcmp(*av,"-f") && (ac >= 2) ) {
389: do_quiet = 1;
390: in_fp = fopen(*(av+1),"r");
391: if ( !in_fp ) {
392: fprintf(stderr,"%s does not exist!\n",*(av+1));
393: #if defined(__MINGW32__)
394: fflush(stderr);
395: #endif
396: asir_terminate(1);
397: }
398: do_file = 1;
399: do_filename = *(av+1);
400: av += 2; ac -= 2;
401: } else if ( !strcmp(*av,"-E") ) {
402: asir_setenv = 1; av++; ac--;
403: } else if ( !strcmp(*av,"-quiet") ) {
404: do_quiet = 1; av++; ac--;
405: } else if ( !strcmp(*av,"-norc") ) {
406: do_asirrc = 0; av++; ac--;
407: } else if ( !strcmp(*av,"-nomessage") ) {
408: do_message = 0; av++; ac--;
409: } else if ( !strcmp(*av,"-rootdir") && (ac >= 2) ) {
410: set_rootdir(*(av+1)); av += 2; ac -= 2;
411: } else if ( !strcmp(*av,"-maxheap") && (ac >= 2) ) {
412: void GC_set_max_heap_size(int);
413:
414: GC_set_max_heap_size(atoi(*(av+1))); av += 2; ac -= 2;
415: #if !defined(VISUAL) && !defined(__MINGW32__)
416: } else if ( !strcmp(*av,"-display") && (ac >= 2) ) {
417: strcpy(displayname,*(av+1)); av += 2; ac -= 2;
418: #endif
419: #if FEP
420: } else if ( !strcmp(*av,"-fep") ) {
421: do_fep = 1; av++; ac--;
422: #endif
423: } else {
424: fprintf(stderr,"%s : unknown option.\n",*av);
425: #if defined(__MINGW32__)
426: fflush(stderr);
427: #endif
428: asir_terminate(1);
429: }
430: }
431: #if FEP
432: if ( do_fep ) {
433: char *home;
434: home = (char *)getenv("HOME");
435: if (!home)
436: home = ".";
437: sprintf (asir_history, "%s/.asir_history",home);
438: read_history(asir_history);
439: using_history();
440: }
441: #endif
442: }
443:
444: #if defined(HAVE_SIGACTION)
445: void (*set_signal(int sig, void (*handler)(int)))(int)
446: {
447: struct sigaction act;
448: struct sigaction oldact;
449: if (handler == SIG_IGN || handler == SIG_DFL) {
450: return signal(sig,handler);
451: }
452: act.sa_handler=handler;
453: act.sa_flags=0;
454: act.sa_flags |= SA_RESTART;
455: sigemptyset(&act.sa_mask);
456: sigaction(sig,&act,&oldact);
457: return oldact.sa_handler;
458: }
459: #endif
460:
461: void sig_init() {
462: #if !defined(VISUAL) && !defined(__MINGW32__)
463: set_signal(SIGINT,int_handler);
464: #else
465: void register_ctrlc_handler();
466:
467: register_ctrlc_handler();
468: #endif
469: set_signal(SIGSEGV,segv_handler);
470:
471: #if defined(SIGFPE)
472: set_signal(SIGFPE,fpe_handler);
473: #endif
474:
475: #if defined(SIGPIPE)
476: set_signal(SIGPIPE,pipe_handler);
477: #endif
478:
479: #if defined(SIGILL)
480: set_signal(SIGILL,ill_handler);
481: #endif
482:
483: #if !defined(VISUAL) && !defined(__MINGW32__)
484: set_signal(SIGBUS,bus_handler);
485: #endif
486: }
487:
488: static void (*old_int)(int);
489:
490: void asir_save_handler() {
491: old_int = set_signal(SIGINT,SIG_IGN);
492: set_signal(SIGINT,old_int);
493: }
494:
495: void asir_set_handler() {
496: set_signal(SIGINT,int_handler);
497: }
498:
499: void asir_reset_handler() {
500: set_signal(SIGINT,old_int);
501: }
502:
503: extern int I_am_server;
504:
505: void resetenv(char *s)
506: {
507: extern FILE *outfile;
508:
509: fprintf(stderr,"%s\n",s);
510: while ( NEXT(asir_infile) )
511: closecurrentinput();
512: resetpvs();
513: #if !defined(VISUAL) && !defined(__MINGW32__)
514: if ( do_server_in_X11 )
515: #endif
516: show_debug_window(0);
517: #if defined(VISUAL_LIB)
518: w_noflush_stderr(0);
519: #endif
520: #if defined(__MINGW32__)
521: fflush(stderr);
522: #endif
523: asir_out = stdout;
524: /* restore states */
525: reset_engine();
526: reset_io();
527: #if !defined(VISUAL) && !defined(__MINGW32__)
528: reset_timer();
529: #endif
530: LONGJMP(main_env,1);
531: }
532:
533: void fatal(int n)
534: {
535: resetenv("return to toplevel");
536: }
537:
538: extern int ox_int_received, critical_when_signal;
539: extern int in_gc, caught_intr;
540: extern int ox_get_pari_result;
541:
542: void int_handler(int sig)
543: {
544: extern NODE PVSS;
545: NODE t;
546:
547: if ( do_file || disable_debugger ) {
548: LEAVE_SIGNAL_CS_ALL;
549: ExitAsir();
550: }
551: if ( !ox_get_pari_result && critical_when_signal ) {
552: ox_int_received = 1;
553: return;
554: }
555: if ( in_gc ) {
556: caught_intr = 1;
557: return;
558: }
559: #if defined(VISUAL) || defined(__MINGW32__)
560: suspend_timer();
561: #endif
562: set_signal(SIGINT,SIG_IGN);
563: #if !defined(VISUAL) && !defined(__MINGW32__)
564: if ( do_server_in_X11 ) {
565: debug(PVSS?((VS)BDY(PVSS))->usrf->f.usrf->body:0);
566: restore_handler();
567: return;
568: }
569: #endif
1.4 fujimoto 570: #if !defined(ANDROID) && defined(linux)
1.1 noro 571: #if 1
1.6 ! noro 572: while ( stdin->_IO_read_ptr < stdin->_IO_read_end )
! 573: // while ( __freadable(stdin) )
1.1 noro 574: #else
575: while ( stdin->_gptr < stdin->_egptr )
576: #endif
577: getchar();
578: #endif
579: while ( 1 ) {
580: char buf[BUFSIZ];
581: char c;
582:
583: fprintf(stderr,"interrupt ?(q/t/c/d/u/w/?) "); fflush(stderr);
584: buf[0] = '\n';
585: while ( !feof(stdin) && buf[0] == '\n' )
586: fgets(buf,BUFSIZ,stdin);
587: if ( feof(stdin) ) {
588: clearerr(stdin);
589: continue;
590: }
591: switch ( c = buf[0] ) {
592: case 'q':
593: while ( 1 ) {
594: fprintf(stderr,"Abort this session? (y or n) "); fflush(stderr);
595: fgets(buf,BUFSIZ,stdin);
596: if ( !strncmp(buf,"y",1) ) {
597: fprintf(stderr,"Bye\n");
598: #if defined(__MINGW32__)
599: fflush(stderr);
600: #endif
601: /* for terminating myself */
602: asir_infile = 0;
603: asir_terminate(1);
604: } else if ( !strncmp(buf,"n",1) ) {
605: restore_handler();
606: return;
607: }
608: }
609: break;
610: case 't':
611: case 'u':
612: while ( 1 ) {
613: fprintf(stderr,"Abort this computation? (y or n) "); fflush(stderr);
614: fgets(buf,BUFSIZ,stdin);
615: if ( !strncmp(buf,"y",1) )
616: break;
617: else if ( !strncmp(buf,"n",1) ) {
618: restore_handler();
619: return;
620: }
621: }
622: if ( debug_mode )
623: debug_mode = 0;
624: restore_handler();
625: if ( c == 'u' ) {
626: if ( user_int_handler ) {
627: if ( !do_terse ) {
628: fprintf(stderr,
629: "Calling the registered exception callbacks...");
630: #if defined(__MINGW32__)
631: fflush(stderr);
632: #endif
633: }
634: for ( t = user_int_handler; t; t = NEXT(t) )
635: bevalf((FUNC)BDY(t),0);
636: if ( !do_terse ) {
637: fprintf(stderr, "done.\n");
638: #if defined(__MINGW32__)
639: fflush(stderr);
640: #endif
641: }
642: }
643: }
644: LEAVE_SIGNAL_CS_ALL;
645: resetenv("return to toplevel");
646: break;
647: case 'd':
648: #if 0
649: nextbp = 1; nextbplevel = -1;
650: #endif
651: debug(PVSS?((VS)BDY(PVSS))->usrf->f.usrf->body:0);
652: restore_handler();
653: return;
654: case 'c':
655: restore_handler();
656: return; break;
657: case 'w':
658: showpos();
659: break;
660: case '?':
661: fprintf(stderr, "q:quit t:toplevel c:continue d:debug u:call registered handler w:where\n");
662: #if defined(__MINGW32__)
663: fflush(stderr);
664: #endif
665: break;
666: default:
667: break;
668: }
669: }
670: }
671:
672: void restore_handler() {
673: #if defined(VISUAL) || defined(__MINGW32__)
674: resume_timer();
675: #endif
676: #if defined(SIGINT)
677: set_signal(SIGINT,int_handler);
678: #endif
679: }
680:
681: void segv_handler(int sig)
682: {
683: #if defined(SIGSEGV)
684: set_signal_for_restart(SIGSEGV,segv_handler);
685: error("internal error (SEGV)");
686: #endif
687: }
688:
689: void ill_handler(int sig)
690: {
691: #if defined(SIGILL)
692: set_signal_for_restart(SIGILL,ill_handler);
693: error("illegal instruction (ILL)");
694: #endif
695: }
696:
697: #if defined(__DARWIN__)
698: #define SIGNAL_FOR_TIMER SIGALRM
699: #define ITIMER_TYPE ITIMER_REAL
700: #else
701: #define SIGNAL_FOR_TIMER SIGVTALRM
702: #define ITIMER_TYPE ITIMER_VIRTUAL
703: #endif
704:
705: void alrm_handler(int sig)
706: {
707: fprintf(stderr,"interval timer expired (VTALRM)\n");
708: #if defined(__MINGW32__)
709: fflush(stderr);
710: #endif
711: LONGJMP(timer_env,1);
712: }
713:
714: void bus_handler(int sig)
715: {
716: #if defined(SIGBUS)
717: set_signal_for_restart(SIGBUS,bus_handler);
718: error("internal error (BUS ERROR)");
719: #endif
720: }
721:
722: void fpe_handler(int sig)
723: {
724: #if defined(SIGFPE)
725: set_signal_for_restart(SIGFPE,fpe_handler);
726: error("internal error (FPE)");
727: #endif
728: }
729:
730: void pipe_handler(int sig)
731: {
732: #if defined(SIGPIPE)
733: set_signal_for_restart(SIGPIPE,pipe_handler);
734: end_critical();
735: error("internal error (BROKEN PIPE)");
736: #endif
737: }
738:
739: void resize_buffer()
740: {
741: }
742:
743: void tty_init() {
744: }
745:
746: void tty_reset() {
747: }
748:
749: extern int evalstatline;
750:
751: void set_lasterror(char *s)
752: {
753: strncpy(LastError,s,BUFSIZ);
754: LastError[BUFSIZ-1] = 0;
755: }
756:
757: SNODE error_snode;
758: int error_in_timer;
759:
760: void error(char *s)
761: {
762: SNODE *snp=0;
763:
764: #if !defined(VISUAL) && !defined(__MINGW32__)
765: if ( !error_in_timer && timer_is_set )
766: alrm_handler(SIGNAL_FOR_TIMER);
767: #endif
768: fprintf(stderr,"%s\n",s);
769: #if defined(__MINGW32__)
770: fflush(stderr);
771: #endif
772: set_lasterror(s);
773: if ( CPVS != GPVS ) {
774: if ( CPVS && CPVS->usrf && CPVS->usrf->f.usrf )
775: searchsn(&BDY(CPVS->usrf->f.usrf),evalstatline,&snp);
776: if ( snp )
777: error_snode = *snp;
778: else
779: error_snode = 0;
780: } else
781: error_snode = 0;
782: if ( do_file ) {
783: char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
784:
785: sprintf(errbuf,"%s\n",s);
786: showpos_to_string(errbuf+strlen(errbuf));
787: set_lasterror(errbuf);
788: ExitAsir();
789: }
790: if ( debug_mode )
791: LONGJMP(debug_env,1);
792: if ( CPVS != GPVS )
793: if ( !no_debug_on_error && (do_server_in_X11 || isatty(0)) )
794: bp(error_snode);
795: if ( I_am_server )
796: showpos_to_list(&LastStackTrace);
797: resetenv("return to toplevel");
798: }
799:
800: void goto_toplevel(char *s)
801: {
802: SNODE *snp=0;
803:
804: #if !defined(VISUAL) && !defined(__MINGW32__)
805: if ( timer_is_set )
806: alrm_handler(SIGNAL_FOR_TIMER);
807: #endif
808: fprintf(stderr,"%s\n",s);
809: #if defined(__MINGW32__)
810: fflush(stderr);
811: #endif
812: if ( do_file ) {
813: char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
814:
815: sprintf(errbuf,"%s\n",s);
816: showpos_to_string(errbuf+strlen(errbuf));
817: ExitAsir();
818: }
819: resetenv("return to toplevel");
820: }
821:
822: #if !defined(VISUAL) && !defined(__MINGW32__)
823: #include <sys/time.h>
824:
825: void set_timer(int interval)
826: {
827: struct itimerval it;
828:
829: it.it_interval.tv_sec = interval;
830: it.it_interval.tv_usec = 0;
831: it.it_value.tv_sec = interval;
832: it.it_value.tv_usec = 0;
833: setitimer(ITIMER_TYPE,&it,0);
834: set_signal(SIGNAL_FOR_TIMER,alrm_handler);
835: timer_is_set = 1;
836: }
837:
838: void reset_timer()
839: {
840: struct itimerval it;
841:
842: it.it_interval.tv_sec = 0;
843: it.it_interval.tv_usec = 0;
844: it.it_value.tv_sec = 0;
845: it.it_value.tv_usec = 0;
846: setitimer(ITIMER_TYPE,&it,0);
847: set_signal(SIGNAL_FOR_TIMER,SIG_IGN);
848: timer_is_set = 0;
849: }
850: #endif
851:
852: unsigned int get_asir_version();
853: char *get_asir_distribution();
854:
855: char *get_gcversion()
856: {
1.3 ohara 857: return "GC 7.4.2 copyright 1988-2015, H-J. Boehm, A. J. Demers, Xerox, SGI, HP.\n";
1.1 noro 858: }
859:
860: char *get_pariversion()
861: {
862: return "";
863: }
864:
865: char *get_intervalversion()
866: {
867: #if defined(INTERVAL)
868: return " + Interval Arithmetic";
869: #else
870: return "";
871: #endif
872: }
873:
874: void copyright()
875: {
876: char *scopyright();
877: fputs(scopyright(), stdout);
878: }
879:
880: char *scopyright()
881: {
882: static char *notice;
883: char *s1, *s2, *s3, *s4;
884: int d, len;
1.3 ohara 885: 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-2019, Risa/Asir committers, http://www.openxm.org/.\n%s%s";
1.1 noro 886: if (!notice) {
887: s1 = get_intervalversion();
888: s2 = get_asir_distribution();
889: s3 = get_gcversion();
890: s4 = get_pariversion();
891: d = get_asir_version();
892: len = (strlen(format)-8)+strlen(s1)+strlen(s2)+strlen(s3)+strlen(s4)+sizeof(int)*3;
893: notice = MALLOC(len+1);
894: sprintf(notice, format, s1, d, s2, s3, s4);
895: }
896: return notice;
897: }
898:
899: #if defined(VISUAL) || defined(__MINGW32__)
900: int recv_intr;
901:
902: static CRITICAL_SECTION signal_cs;
903: static int initialized_signal_cs;
904: static int signal_cs_count;
905:
906: static void init_signal_cs()
907: {
908: if (!initialized_signal_cs) {
909: InitializeCriticalSection(&signal_cs);
910: initialized_signal_cs=1;
911: signal_cs_count=0;
912: }
913: }
914:
915: void try_enter_signal_cs()
916: {
917: init_signal_cs();
918: if(TryEnterCriticalSection(&signal_cs)) {
919: signal_cs_count++;
920: }
921: }
922:
923: void enter_signal_cs()
924: {
925: init_signal_cs();
926: EnterCriticalSection(&signal_cs);
927: signal_cs_count++;
928: }
929:
930: void leave_signal_cs()
931: {
932: init_signal_cs();
933: if(signal_cs_count>0) {
934: LeaveCriticalSection(&signal_cs);
935: signal_cs_count--;
936: }
937: }
938:
939: void leave_signal_cs_all()
940: {
941: if (!initialized_signal_cs) {
942: init_signal_cs();
943: }
944: while(signal_cs_count>0) {
945: LeaveCriticalSection(&signal_cs);
946: signal_cs_count--;
947: }
948: }
949:
950: void check_intr()
951: {
952: extern int recv_intr;
953: enter_signal_cs();
954: if ( recv_intr ) {
955: if ( recv_intr == 1 ) {
956: recv_intr = 0;
957: int_handler(SIGINT);
958: } else {
959: recv_intr = 0;
960: ox_usr1_handler(0);
961: }
962: }
963: leave_signal_cs_all();
964: }
965: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>