Annotation of OpenXM_contrib2/asir2000/parse/glob.c, Revision 1.92
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.92 ! ohara 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/glob.c,v 1.91 2015/08/19 05:29:23 noro 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;
165: reallocarray((char **)&GPVS->va,(int *)&GPVS->asize,(int *)&GPVS->n,(int)sizeof(struct oPV));
166: reallocarray((char **)&APVS->va,(int *)&APVS->asize,(int *)&APVS->n,(int)sizeof(struct oPV));
1.60 noro 167: 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:
193: int do_asirrc;
194: int do_file;
1.42 ohara 195: char *do_filename;
1.1 noro 196: int do_message;
1.58 noro 197: int do_terse;
1.1 noro 198: int do_fep;
1.42 ohara 199: int asir_setenv;
1.1 noro 200: static int buserr_sav;
201: static char asir_history[BUFSIZ];
202:
203: extern int mpi_myid;
204:
1.5 noro 205: #if !defined(VISUAL_LIB)
206: void ExitAsir() {
207: exit(0);
208: }
209: #endif
210:
1.1 noro 211: /*
212: * status = 1 abnormal termination (error() etc.)
213: * status = 2 normal termination (end(), quit() etc.)
1.63 noro 214: * status = 3 absolute termination
1.1 noro 215: */
216:
1.24 noro 217: void asir_terminate(int status)
1.1 noro 218: {
1.21 noro 219: int t;
1.53 noro 220: NODE n;
1.63 noro 221:
222: /* called from engine in Windows */
223: if ( status == 3 ) asir_infile = 0;
1.21 noro 224:
1.62 noro 225: if ( asir_infile && asir_infile->ready_for_longjmp )
226: LONGJMP(asir_infile->jmpbuf,status);
227: else {
1.54 noro 228: if ( user_quit_handler ) {
1.90 fujimoto 229: if ( !do_terse ) {
1.58 noro 230: fprintf(stderr,"Calling the registered quit callbacks...");
1.90 fujimoto 231: #if defined(__MINGW32__)
232: fflush(stderr);
233: #endif
234: }
1.54 noro 235: for ( n = user_quit_handler; n; n = NEXT(n) )
236: bevalf((FUNC)BDY(n),0);
1.90 fujimoto 237: if ( !do_terse ) {
1.58 noro 238: fprintf(stderr, "done.\n");
1.90 fujimoto 239: #if defined(__MINGW32__)
240: fflush(stderr);
1.89 fujimoto 241: #endif
1.90 fujimoto 242: }
1.54 noro 243: }
1.1 noro 244: tty_reset();
1.33 noro 245: #if defined(MPI)
1.1 noro 246: if ( !mpi_myid )
247: close_allconnections();
248: mpi_finalize();
249: #else
1.20 noro 250: #if defined(SIGPIPE)
1.92 ! ohara 251: set_signal(SIGPIPE,SIG_IGN);
1.20 noro 252: #endif
1.1 noro 253: close_allconnections();
254: #endif
255: if ( asir_out )
256: fflush(asir_out);
1.32 noro 257: #if FEP
1.13 saito 258: if ( do_fep ) {
259: stifle_history(MAXHIST);
260: write_history(asir_history);
261: }
262: #endif
1.1 noro 263: ExitAsir();
264: }
265: }
266:
267: void param_init() {
268: unsigned int et = 0xff;
269: extern int paristack;
270: if ( *((char *)&et) )
271: little_endian = 1;
272: else
273: little_endian = 0;
274: }
1.39 noro 275:
276: Obj user_defined_prompt;
277:
1.1 noro 278: void prompt() {
1.48 noro 279: if ( !do_quiet && !do_fep && asir_infile->fp == stdin ) {
1.21 noro 280: fprintf(asir_out,"[%d] ",APVS->n);
1.48 noro 281: fflush(asir_out);
282: } else if ( do_quiet && user_defined_prompt
1.39 noro 283: && OID(user_defined_prompt)==O_STR) {
284: fprintf(asir_out,BDY((STRING)user_defined_prompt),APVS->n);
1.48 noro 285: fflush(asir_out);
1.39 noro 286: }
1.1 noro 287: }
288:
1.24 noro 289: void sprompt(char *ptr)
1.1 noro 290: {
1.21 noro 291: sprintf(ptr,"[%d] ",APVS->n);
1.34 ohara 292: }
293:
1.90 fujimoto 294: #if !defined(VISUAL) && !defined(__MINGW32__)
1.34 ohara 295: static int which(char *prog, char *path, char *buf, size_t size)
296: {
297: char *tok;
298: char delim[] = ":";
299: char *path2 = malloc(strlen(path)+1);
300: char *name = malloc(size);
301: int proglen = strlen(prog)+3; /* "/" + prog + " \0" */
302:
303: if (!name || !path2) {
304: return 0;
305: }
306: strcpy(path2, path);
307: tok = strtok(path2, delim);
308: while (tok != NULL) {
309: if (size >= strlen(tok)) {
310: sprintf(name, "%s/%s", tok, prog);
311: if (access(name, X_OK&R_OK) == 0) {
312: strcpy(buf, name);
313: strcat(buf, " ");
314: free(path2); free(name);
315: return 1;
316: }
317: tok = strtok(NULL, delim);
318: }
319: }
320: free(path2); free(name);
321: return 0;
322: }
323: #endif
324:
325: void cppname_init()
326: {
1.90 fujimoto 327: #if !defined(VISUAL) && !defined(__MINGW32__)
1.79 noro 328: char *oxhome;
329: char oxcpp[BUFSIZ];
330: #define OXCPP "/bin/ox_cpp"
331:
332: if ( oxhome = getenv("OpenXM_HOME") ) {
333: if ( strlen(oxhome)+strlen(OXCPP)<BUFSIZ ) {
334: sprintf(oxcpp,"%s%s",oxhome,OXCPP);
335: if ( access(oxcpp,X_OK&R_OK) == 0 ) {
336: strcpy(cppname,oxcpp);
337: return;
338: }
339: }
340: }
341: if (access(cppname, X_OK&R_OK) != 0) {
1.34 ohara 342: which("cpp", "/lib:/usr/ccs/lib:/usr/bin", cppname, BUFSIZ) ||
343: which("cpp", getenv("PATH"), cppname, BUFSIZ);
344: }
345: #endif
1.1 noro 346: }
347:
348: FILE *in_fp;
349:
1.24 noro 350: void process_args(int ac,char **av)
1.1 noro 351: {
1.75 ohara 352: int nm,dv;
1.1 noro 353: do_asirrc = 1;
1.33 noro 354: #if !defined(MPI)
1.1 noro 355: do_message = 1;
356: #endif
1.38 ohara 357: do_quiet = 0;
1.1 noro 358: while ( ac > 0 ) {
359: if ( !strcmp(*av,"-heap") && (ac >= 2) ) {
360: void GC_expand_hp(int);
361:
362: GC_expand_hp(atoi(*(av+1))); av += 2; ac -= 2;
363: } else if ( !strcmp(*av,"-adj") && (ac >= 2) ) {
364: char *slash;
365: slash = strrchr(*(av+1),'/');
366: if ( slash ) {
367: *slash = 0;
1.75 ohara 368: nm = atoi(slash+1);
369: }else {
370: nm = 1;
1.1 noro 371: }
1.75 ohara 372: dv = atoi(*(av+1));
373: Risa_GC_set_adj(nm,dv);
1.1 noro 374: av += 2; ac -= 2;
375: } else if ( !strcmp(*av,"-cpp") && (ac >= 2) ) {
376: strcpy(cppname,*(av+1)); av += 2; ac -= 2;
377: } else if ( !strcmp(*av,"-f") && (ac >= 2) ) {
1.38 ohara 378: do_quiet = 1;
1.1 noro 379: in_fp = fopen(*(av+1),"r");
380: if ( !in_fp ) {
1.62 noro 381: fprintf(stderr,"%s does not exist!\n",*(av+1));
1.90 fujimoto 382: #if defined(__MINGW32__)
1.89 fujimoto 383: fflush(stderr);
384: #endif
1.1 noro 385: asir_terminate(1);
386: }
387: do_file = 1;
1.42 ohara 388: do_filename = *(av+1);
1.1 noro 389: av += 2; ac -= 2;
1.42 ohara 390: } else if ( !strcmp(*av,"-E") ) {
391: asir_setenv = 1; av++; ac--;
1.38 ohara 392: } else if ( !strcmp(*av,"-quiet") ) {
1.39 noro 393: do_quiet = 1; av++; ac--;
1.1 noro 394: } else if ( !strcmp(*av,"-norc") ) {
395: do_asirrc = 0; av++; ac--;
396: } else if ( !strcmp(*av,"-nomessage") ) {
397: do_message = 0; av++; ac--;
1.11 noro 398: } else if ( !strcmp(*av,"-rootdir") && (ac >= 2) ) {
399: set_rootdir(*(av+1)); av += 2; ac -= 2;
1.1 noro 400: } else if ( !strcmp(*av,"-maxheap") && (ac >= 2) ) {
401: void GC_set_max_heap_size(int);
402:
403: GC_set_max_heap_size(atoi(*(av+1))); av += 2; ac -= 2;
1.90 fujimoto 404: #if !defined(VISUAL) && !defined(__MINGW32__)
1.1 noro 405: } else if ( !strcmp(*av,"-display") && (ac >= 2) ) {
406: strcpy(displayname,*(av+1)); av += 2; ac -= 2;
407: #endif
1.32 noro 408: #if FEP
1.13 saito 409: } else if ( !strcmp(*av,"-fep") ) {
410: do_fep = 1; av++; ac--;
411: #endif
1.1 noro 412: } else {
413: fprintf(stderr,"%s : unknown option.\n",*av);
1.90 fujimoto 414: #if defined(__MINGW32__)
1.89 fujimoto 415: fflush(stderr);
416: #endif
1.1 noro 417: asir_terminate(1);
418: }
419: }
1.32 noro 420: #if FEP
1.13 saito 421: if ( do_fep ) {
422: char *home;
423: home = (char *)getenv("HOME");
424: if (!home)
425: home = ".";
426: sprintf (asir_history, "%s/.asir_history",home);
427: read_history(asir_history);
428: using_history();
429: }
430: #endif
1.1 noro 431: }
432:
1.92 ! ohara 433: #if defined(HAVE_SIGACTION)
! 434: void (*set_signal(int sig, void (*handler)(int)))(int)
! 435: {
! 436: struct sigaction act;
! 437: struct sigaction oldact;
! 438: if (handler == SIG_IGN || handler == SIG_DFL) {
! 439: return signal(sig,handler);
! 440: }
! 441: act.sa_handler=handler;
! 442: act.sa_flags=0;
! 443: act.sa_flags |= SA_RESTART;
! 444: sigemptyset(&act.sa_mask);
! 445: sigaction(sig,&act,&oldact);
! 446: return oldact.sa_handler;
! 447: }
! 448: #endif
1.1 noro 449:
450: void sig_init() {
1.90 fujimoto 451: #if !defined(VISUAL) && !defined(__MINGW32__)
1.92 ! ohara 452: set_signal(SIGINT,int_handler);
1.1 noro 453: #else
1.24 noro 454: void register_ctrlc_handler();
455:
1.1 noro 456: register_ctrlc_handler();
457: #endif
1.92 ! ohara 458: set_signal(SIGSEGV,segv_handler);
1.1 noro 459:
1.23 noro 460: #if defined(SIGFPE)
1.92 ! ohara 461: set_signal(SIGFPE,fpe_handler);
1.1 noro 462: #endif
463:
1.23 noro 464: #if defined(SIGPIPE)
1.92 ! ohara 465: set_signal(SIGPIPE,pipe_handler);
1.1 noro 466: #endif
467:
1.23 noro 468: #if defined(SIGILL)
1.92 ! ohara 469: set_signal(SIGILL,ill_handler);
1.1 noro 470: #endif
471:
1.90 fujimoto 472: #if !defined(VISUAL) && !defined(__MINGW32__)
1.92 ! ohara 473: set_signal(SIGBUS,bus_handler);
1.1 noro 474: #endif
475: }
476:
477: static void (*old_int)(int);
478:
479: void asir_save_handler() {
1.92 ! ohara 480: old_int = set_signal(SIGINT,SIG_IGN);
! 481: set_signal(SIGINT,old_int);
1.1 noro 482: }
483:
484: void asir_set_handler() {
1.92 ! ohara 485: set_signal(SIGINT,int_handler);
1.1 noro 486: }
487:
488: void asir_reset_handler() {
1.92 ! ohara 489: set_signal(SIGINT,old_int);
1.1 noro 490: }
491:
1.62 noro 492: extern int I_am_server;
493:
1.24 noro 494: void resetenv(char *s)
1.1 noro 495: {
496: extern FILE *outfile;
497:
1.53 noro 498: fprintf(stderr,"%s\n",s);
1.1 noro 499: while ( NEXT(asir_infile) )
500: closecurrentinput();
501: resetpvs();
1.90 fujimoto 502: #if !defined(VISUAL) && !defined(__MINGW32__)
1.1 noro 503: if ( do_server_in_X11 )
504: #endif
505: show_debug_window(0);
506: #if defined(VISUAL_LIB)
507: w_noflush_stderr(0);
508: #endif
1.90 fujimoto 509: #if defined(__MINGW32__)
510: fflush(stderr);
511: #endif
1.1 noro 512: asir_out = stdout;
513: /* restore states */
514: reset_engine();
515: reset_io();
1.90 fujimoto 516: #if !defined(VISUAL) && !defined(__MINGW32__)
1.15 noro 517: reset_timer();
1.16 noro 518: #endif
1.62 noro 519: LONGJMP(main_env,1);
1.1 noro 520: }
521:
1.24 noro 522: void fatal(int n)
1.1 noro 523: {
524: resetenv("return to toplevel");
525: }
526:
527: extern int ox_int_received, critical_when_signal;
1.83 noro 528: extern int in_gc, caught_intr;
1.91 noro 529: extern int ox_get_pari_result;
1.1 noro 530:
1.24 noro 531: void int_handler(int sig)
1.1 noro 532: {
533: extern NODE PVSS;
1.53 noro 534: NODE t;
1.48 noro 535:
1.1 noro 536:
537: if ( do_file ) {
538: ExitAsir();
539: }
1.91 noro 540: if ( !ox_get_pari_result && critical_when_signal ) {
1.1 noro 541: ox_int_received = 1;
542: return;
543: }
1.83 noro 544: if ( in_gc ) {
545: caught_intr = 1;
546: return;
547: }
1.90 fujimoto 548: #if defined(VISUAL) || defined(__MINGW32__)
1.19 noro 549: suspend_timer();
1.1 noro 550: #endif
1.92 ! ohara 551: set_signal(SIGINT,SIG_IGN);
1.90 fujimoto 552: #if !defined(VISUAL) && !defined(__MINGW32__)
1.3 noro 553: if ( do_server_in_X11 ) {
554: debug(PVSS?((VS)BDY(PVSS))->usrf->f.usrf->body:0);
555: restore_handler();
556: return;
557: }
1.1 noro 558: #endif
559: #if defined(linux)
560: #if 1
561: while ( stdin->_IO_read_ptr < stdin->_IO_read_end )
562: #else
563: while ( stdin->_gptr < stdin->_egptr )
564: #endif
565: getchar();
566: #endif
567: while ( 1 ) {
568: char buf[BUFSIZ];
569: char c;
570:
1.53 noro 571: fprintf(stderr,"interrupt ?(q/t/c/d/u/w/?) "); fflush(stderr);
1.1 noro 572: buf[0] = '\n';
1.77 noro 573: while ( !feof(stdin) && buf[0] == '\n' )
1.1 noro 574: fgets(buf,BUFSIZ,stdin);
1.77 noro 575: if ( feof(stdin) ) {
576: clearerr(stdin);
577: continue;
578: }
1.1 noro 579: switch ( c = buf[0] ) {
580: case 'q':
581: while ( 1 ) {
1.53 noro 582: fprintf(stderr,"Abort this session? (y or n) "); fflush(stderr);
1.1 noro 583: fgets(buf,BUFSIZ,stdin);
584: if ( !strncmp(buf,"y",1) ) {
1.53 noro 585: fprintf(stderr,"Bye\n");
1.90 fujimoto 586: #if defined(__MINGW32__)
587: fflush(stderr);
588: #endif
1.65 noro 589: /* for terminating myself */
590: asir_infile = 0;
1.48 noro 591: asir_terminate(1);
1.1 noro 592: } else if ( !strncmp(buf,"n",1) ) {
593: restore_handler();
594: return;
595: }
596: }
597: break;
598: case 't':
599: case 'u':
600: while ( 1 ) {
1.53 noro 601: fprintf(stderr,"Abort this computation? (y or n) "); fflush(stderr);
1.1 noro 602: fgets(buf,BUFSIZ,stdin);
603: if ( !strncmp(buf,"y",1) )
604: break;
605: else if ( !strncmp(buf,"n",1) ) {
606: restore_handler();
607: return;
608: }
609: }
610: if ( debug_mode )
611: debug_mode = 0;
612: restore_handler();
613: if ( c == 'u' ) {
1.53 noro 614: if ( user_int_handler ) {
1.90 fujimoto 615: if ( !do_terse ) {
1.58 noro 616: fprintf(stderr,
617: "Calling the registered exception callbacks...");
1.90 fujimoto 618: #if defined(__MINGW32__)
619: fflush(stderr);
620: #endif
621: }
1.53 noro 622: for ( t = user_int_handler; t; t = NEXT(t) )
623: bevalf((FUNC)BDY(t),0);
1.90 fujimoto 624: if ( !do_terse ) {
1.58 noro 625: fprintf(stderr, "done.\n");
1.90 fujimoto 626: #if defined(__MINGW32__)
627: fflush(stderr);
1.89 fujimoto 628: #endif
1.90 fujimoto 629: }
1.1 noro 630: }
631: }
1.62 noro 632: resetenv("return to toplevel");
1.1 noro 633: break;
634: case 'd':
635: #if 0
636: nextbp = 1; nextbplevel = -1;
637: #endif
638: debug(PVSS?((VS)BDY(PVSS))->usrf->f.usrf->body:0);
639: restore_handler();
640: return;
641: case 'c':
642: restore_handler();
643: return; break;
644: case 'w':
1.48 noro 645: showpos();
646: break;
1.1 noro 647: case '?':
1.53 noro 648: fprintf(stderr, "q:quit t:toplevel c:continue d:debug u:call registered handler w:where\n");
1.90 fujimoto 649: #if defined(__MINGW32__)
650: fflush(stderr);
1.89 fujimoto 651: #endif
1.1 noro 652: break;
653: default:
654: break;
655: }
656: }
657: }
658:
659: void restore_handler() {
1.90 fujimoto 660: #if defined(VISUAL) || defined(__MINGW32__)
1.19 noro 661: resume_timer();
1.1 noro 662: #endif
1.19 noro 663: #if defined(SIGINT)
1.92 ! ohara 664: set_signal(SIGINT,int_handler);
1.1 noro 665: #endif
666: }
667:
1.24 noro 668: void segv_handler(int sig)
1.1 noro 669: {
1.19 noro 670: #if defined(SIGSEGV)
1.92 ! ohara 671: set_signal_for_restart(SIGSEGV,segv_handler);
1.19 noro 672: error("internal error (SEGV)");
1.1 noro 673: #endif
674: }
675:
1.24 noro 676: void ill_handler(int sig)
1.1 noro 677: {
1.19 noro 678: #if defined(SIGILL)
1.92 ! ohara 679: set_signal_for_restart(SIGILL,ill_handler);
1.19 noro 680: error("illegal instruction (ILL)");
1.1 noro 681: #endif
682: }
683:
1.69 noro 684: #if defined(__DARWIN__)
685: #define SIGNAL_FOR_TIMER SIGALRM
686: #define ITIMER_TYPE ITIMER_REAL
687: #else
688: #define SIGNAL_FOR_TIMER SIGVTALRM
689: #define ITIMER_TYPE ITIMER_VIRTUAL
690: #endif
691:
1.24 noro 692: void alrm_handler(int sig)
1.1 noro 693: {
694: fprintf(stderr,"interval timer expired (VTALRM)\n");
1.90 fujimoto 695: #if defined(__MINGW32__)
1.89 fujimoto 696: fflush(stderr);
697: #endif
1.26 noro 698: LONGJMP(timer_env,1);
1.1 noro 699: }
700:
1.24 noro 701: void bus_handler(int sig)
1.1 noro 702: {
703: #if defined(SIGBUS)
1.92 ! ohara 704: set_signal_for_restart(SIGBUS,bus_handler);
1.1 noro 705: error("internal error (BUS ERROR)");
706: #endif
707: }
708:
1.24 noro 709: void fpe_handler(int sig)
1.1 noro 710: {
1.19 noro 711: #if defined(SIGFPE)
1.92 ! ohara 712: set_signal_for_restart(SIGFPE,fpe_handler);
1.19 noro 713: error("internal error (FPE)");
1.1 noro 714: #endif
715: }
716:
1.24 noro 717: void pipe_handler(int sig)
1.1 noro 718: {
719: #if defined(SIGPIPE)
1.92 ! ohara 720: set_signal_for_restart(SIGPIPE,pipe_handler);
1.20 noro 721: end_critical();
1.1 noro 722: error("internal error (BROKEN PIPE)");
723: #endif
724: }
725:
726: void resize_buffer()
727: {
728: }
729:
730: void tty_init() {
731: }
732:
733: void tty_reset() {
734: }
735:
736: extern int evalstatline;
737:
1.24 noro 738: void set_lasterror(char *s)
1.1 noro 739: {
740: strncpy(LastError,s,BUFSIZ);
741: LastError[BUFSIZ-1] = 0;
742: }
743:
744: SNODE error_snode;
1.51 noro 745: int error_in_timer;
1.1 noro 746:
1.24 noro 747: void error(char *s)
1.1 noro 748: {
1.37 noro 749: SNODE *snp=0;
1.1 noro 750:
1.90 fujimoto 751: #if !defined(VISUAL) && !defined(__MINGW32__)
1.51 noro 752: if ( !error_in_timer && timer_is_set )
1.69 noro 753: alrm_handler(SIGNAL_FOR_TIMER);
1.16 noro 754: #endif
1.1 noro 755: fprintf(stderr,"%s\n",s);
1.90 fujimoto 756: #if defined(__MINGW32__)
1.89 fujimoto 757: fflush(stderr);
758: #endif
1.1 noro 759: set_lasterror(s);
760: if ( CPVS != GPVS ) {
1.37 noro 761: if ( CPVS && CPVS->usrf && CPVS->usrf->f.usrf )
1.7 noro 762: searchsn(&BDY(CPVS->usrf->f.usrf),evalstatline,&snp);
1.6 noro 763: if ( snp )
764: error_snode = *snp;
765: else
766: error_snode = 0;
1.1 noro 767: } else
768: error_snode = 0;
769: if ( do_file ) {
770: char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
771:
772: sprintf(errbuf,"%s\n",s);
773: showpos_to_string(errbuf+strlen(errbuf));
774: set_lasterror(errbuf);
775: ExitAsir();
776: }
777: if ( debug_mode )
1.26 noro 778: LONGJMP(debug_env,1);
1.1 noro 779: if ( CPVS != GPVS )
1.52 noro 780: if ( !no_debug_on_error && (do_server_in_X11 || isatty(0)) )
1.1 noro 781: bp(error_snode);
1.62 noro 782: if ( I_am_server )
783: showpos_to_list(&LastStackTrace);
1.40 noro 784: resetenv("return to toplevel");
785: }
786:
1.86 noro 787: void goto_toplevel(char *s)
1.40 noro 788: {
789: SNODE *snp=0;
790:
1.90 fujimoto 791: #if !defined(VISUAL) && !defined(__MINGW32__)
1.40 noro 792: if ( timer_is_set )
1.69 noro 793: alrm_handler(SIGNAL_FOR_TIMER);
1.40 noro 794: #endif
795: fprintf(stderr,"%s\n",s);
1.90 fujimoto 796: #if defined(__MINGW32__)
1.89 fujimoto 797: fflush(stderr);
798: #endif
1.40 noro 799: if ( do_file ) {
800: char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
801:
802: sprintf(errbuf,"%s\n",s);
803: showpos_to_string(errbuf+strlen(errbuf));
804: ExitAsir();
805: }
1.1 noro 806: resetenv("return to toplevel");
807: }
808:
1.90 fujimoto 809: #if !defined(VISUAL) && !defined(__MINGW32__)
1.1 noro 810: #include <sys/time.h>
811:
1.24 noro 812: void set_timer(int interval)
1.1 noro 813: {
814: struct itimerval it;
815:
816: it.it_interval.tv_sec = interval;
817: it.it_interval.tv_usec = 0;
818: it.it_value.tv_sec = interval;
819: it.it_value.tv_usec = 0;
1.69 noro 820: setitimer(ITIMER_TYPE,&it,0);
1.92 ! ohara 821: set_signal(SIGNAL_FOR_TIMER,alrm_handler);
1.15 noro 822: timer_is_set = 1;
1.1 noro 823: }
824:
825: void reset_timer()
826: {
827: struct itimerval it;
828:
829: it.it_interval.tv_sec = 0;
830: it.it_interval.tv_usec = 0;
831: it.it_value.tv_sec = 0;
832: it.it_value.tv_usec = 0;
1.69 noro 833: setitimer(ITIMER_TYPE,&it,0);
1.92 ! ohara 834: set_signal(SIGNAL_FOR_TIMER,SIG_IGN);
1.15 noro 835: timer_is_set = 0;
1.1 noro 836: }
837: #endif
838:
839: unsigned int get_asir_version();
1.14 noro 840: char *get_asir_distribution();
1.1 noro 841:
1.72 ohara 842: char *get_gcversion()
843: {
1.82 ohara 844: return "GC 7.2 copyright 1988-2012, H-J. Boehm, A. J. Demers, Xerox, SGI, HP.\n";
1.72 ohara 845: }
846:
1.47 ohara 847: char *get_pariversion()
848: {
849: return "";
1.1 noro 850: }
1.14 noro 851:
1.47 ohara 852: char *get_intervalversion()
853: {
854: #if defined(INTERVAL)
855: return " + Interval Arithmetic";
856: #else
857: return "";
858: #endif
859: }
860:
861: void copyright()
862: {
1.72 ohara 863: char *scopyright();
864: fputs(scopyright(), stdout);
1.47 ohara 865: }
1.5 noro 866:
1.47 ohara 867: char *scopyright()
868: {
869: static char *notice;
1.72 ohara 870: char *s1, *s2, *s3, *s4;
1.47 ohara 871: int d, len;
1.72 ohara 872: 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 873: if (!notice) {
874: s1 = get_intervalversion();
875: s2 = get_asir_distribution();
1.72 ohara 876: s3 = get_gcversion();
877: s4 = get_pariversion();
1.47 ohara 878: d = get_asir_version();
1.72 ohara 879: len = (strlen(format)-8)+strlen(s1)+strlen(s2)+strlen(s3)+strlen(s4)+sizeof(int)*3;
1.47 ohara 880: notice = MALLOC(len+1);
1.72 ohara 881: sprintf(notice, format, s1, d, s2, s3, s4);
1.47 ohara 882: }
883: return notice;
884: }
1.84 noro 885:
1.90 fujimoto 886: #if defined(VISUAL) || defined(__MINGW32__)
1.84 noro 887: void check_intr()
888: {
889: extern int recv_intr;
890: if ( recv_intr ) {
891: if ( recv_intr == 1 ) {
892: recv_intr = 0;
893: int_handler(0);
894: } else {
895: recv_intr = 0;
896: ox_usr1_handler(0);
897: }
898: }
899: }
900: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>