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