[BACK]Return to glob.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / parse

Annotation of OpenXM_contrib2/asir2000/parse/glob.c, Revision 1.100

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.100   ! ohara      48:  * $OpenXM: OpenXM_contrib2/asir2000/parse/glob.c,v 1.99 2018/03/29 01:32:54 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[] = {
1.99      noro      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}
1.1       noro      120: };
                    121:
                    122: struct oVL oVLIST[52];
                    123:
                    124: VL CO = oVLIST;
                    125: VL ALG;
1.98      noro      126: VL LASTCO;
1.1       noro      127:
1.60      noro      128: struct oVS oGPVS,oAPVS,oEPVS,oPPVS;
1.1       noro      129: VS GPVS = &oGPVS;
                    130: VS APVS = &oAPVS;
                    131: VS EPVS = &oEPVS;
1.60      noro      132: VS PPVS = &oPPVS;
1.35      noro      133: VS CPVS,MPVS;
                    134:
                    135: NODE MODULE_LIST;
                    136: MODULE CUR_MODULE;
1.36      noro      137: char *CUR_FUNC;
1.1       noro      138:
1.46      noro      139: struct oSYMBOL oGrlex, oGlex, oLex;
                    140: SYMBOL Symbol_grlex = &oGrlex;
                    141: SYMBOL Symbol_glex = &oGlex;
                    142: SYMBOL Symbol_lex = &oLex;;
1.44      noro      143:
1.1       noro      144: struct oF oF_TRUE,oF_FALSE;
                    145: F F_TRUE = &oF_TRUE;
                    146: F F_FALSE = &oF_FALSE;
                    147:
1.90      fujimoto  148: #if defined(VISUAL) || defined(__MINGW32__)
1.4       noro      149: char cppname[BUFSIZ] = "c:\\asir\\stdlib\\cpp ";
1.1       noro      150: #else
1.4       noro      151: char cppname[BUFSIZ] = "/lib/cpp ";
1.1       noro      152: #endif
1.4       noro      153: char asirname[BUFSIZ];
1.1       noro      154: char displayname[BUFSIZ];
                    155:
                    156: int Verbose;
1.67      noro      157: int do_quiet;
1.1       noro      158:
                    159: void glob_init() {
1.99      noro      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);
1.1       noro      181: }
                    182:
1.24      noro      183: void input_init(FILE *fp,char *name)
1.1       noro      184: {
1.99      noro      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;
1.1       noro      188: }
                    189:
1.24      noro      190: void notdef(VL vl,Obj a,Obj b,Obj *c)
1.1       noro      191: {
1.99      noro      192:   error("undefined arithmetic operation.");
1.1       noro      193: }
                    194:
1.96      ohara     195: int disable_debugger;
1.1       noro      196: int do_asirrc;
                    197: int do_file;
1.42      ohara     198: char *do_filename;
1.1       noro      199: int do_message;
1.58      noro      200: int do_terse;
1.1       noro      201: int do_fep;
1.42      ohara     202: int asir_setenv;
1.1       noro      203: static int buserr_sav;
                    204: static char asir_history[BUFSIZ];
                    205:
                    206: extern int mpi_myid;
                    207:
1.5       noro      208: #if !defined(VISUAL_LIB)
                    209: void ExitAsir() {
1.99      noro      210:   exit(0);
1.5       noro      211: }
                    212: #endif
                    213:
1.1       noro      214: /*
                    215:  * status = 1 abnormal termination (error() etc.)
                    216:  * status = 2 normal termination (end(), quit() etc.)
1.63      noro      217:  * status = 3 absolute termination
1.1       noro      218:  */
                    219:
1.24      noro      220: void asir_terminate(int status)
1.1       noro      221: {
1.99      noro      222:   int t;
                    223:   NODE n;
1.63      noro      224:
1.99      noro      225:   /* called from engine in Windows */
                    226:   if ( status == 3 ) asir_infile = 0;
1.21      noro      227:
1.99      noro      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();
1.33      noro      248: #if defined(MPI)
1.99      noro      249:     if ( !mpi_myid )
                    250:       close_allconnections();
                    251:     mpi_finalize();
1.1       noro      252: #else
1.20      noro      253: #if defined(SIGPIPE)
1.99      noro      254:     set_signal(SIGPIPE,SIG_IGN);
1.20      noro      255: #endif
1.99      noro      256:     close_allconnections();
1.1       noro      257: #endif
1.99      noro      258:     if ( asir_out )
                    259:       fflush(asir_out);
1.32      noro      260: #if FEP
1.99      noro      261:     if ( do_fep ) {
                    262:       stifle_history(MAXHIST);
                    263:       write_history(asir_history);
                    264:     }
1.13      saito     265: #endif
1.99      noro      266:     ExitAsir();
                    267:   }
1.1       noro      268: }
                    269:
                    270: void param_init() {
1.99      noro      271:   unsigned int et = 0xff;
                    272:   extern int paristack;
                    273:   if ( *((char *)&et) )
                    274:     little_endian = 1;
                    275:   else
                    276:     little_endian = 0;
1.1       noro      277: }
1.39      noro      278:
                    279: Obj user_defined_prompt;
                    280:
1.1       noro      281: void prompt() {
1.99      noro      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:   }
1.1       noro      290: }
                    291:
1.24      noro      292: void sprompt(char *ptr)
1.1       noro      293: {
1.99      noro      294:   sprintf(ptr,"[%d] ",APVS->n);
1.34      ohara     295: }
                    296:
1.90      fujimoto  297: #if !defined(VISUAL) && !defined(__MINGW32__)
1.34      ohara     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: {
1.90      fujimoto  330: #if !defined(VISUAL) && !defined(__MINGW32__)
1.99      noro      331:   char *oxhome;
                    332:   char oxcpp[BUFSIZ];
1.79      noro      333: #define OXCPP "/bin/ox_cpp"
                    334:
1.99      noro      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:   }
1.79      noro      344:     if (access(cppname, X_OK&R_OK) != 0) {
1.34      ohara     345:         which("cpp", "/lib:/usr/ccs/lib:/usr/bin", cppname, BUFSIZ) ||
                    346:         which("cpp", getenv("PATH"), cppname, BUFSIZ);
                    347:     }
                    348: #endif
1.1       noro      349: }
                    350:
                    351: FILE *in_fp;
                    352:
1.24      noro      353: void process_args(int ac,char **av)
1.1       noro      354: {
1.99      noro      355:   int nm,dv;
                    356:   do_asirrc = 1;
1.33      noro      357: #if !defined(MPI)
1.99      noro      358:   do_message = 1;
1.1       noro      359: #endif
1.96      ohara     360: #if defined(VISUAL) && defined(VISUAL_CONSOLE)
1.99      noro      361:   disable_debugger=1;
1.96      ohara     362: #endif
1.99      noro      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) ) {
1.96      ohara     384: #if defined(VISUAL) && defined(VISUAL_CONSOLE)
1.99      noro      385:       disable_debugger=0;
1.96      ohara     386: #endif
1.99      noro      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);
1.1       noro      413:
1.99      noro      414:       GC_set_max_heap_size(atoi(*(av+1))); av += 2; ac -= 2;
1.90      fujimoto  415: #if !defined(VISUAL) && !defined(__MINGW32__)
1.99      noro      416:     } else if ( !strcmp(*av,"-display") && (ac >= 2) ) {
                    417:       strcpy(displayname,*(av+1)); av += 2; ac -= 2;
1.1       noro      418: #endif
1.32      noro      419: #if FEP
1.99      noro      420:     } else if ( !strcmp(*av,"-fep") ) {
                    421:       do_fep = 1; av++; ac--;
1.13      saito     422: #endif
1.99      noro      423:     } else {
                    424:       fprintf(stderr,"%s : unknown option.\n",*av);
1.90      fujimoto  425: #if defined(__MINGW32__)
1.99      noro      426:       fflush(stderr);
1.89      fujimoto  427: #endif
1.99      noro      428:       asir_terminate(1);
                    429:     }
                    430:   }
1.32      noro      431: #if FEP
1.99      noro      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:   }
1.13      saito     441: #endif
1.1       noro      442: }
                    443:
1.92      ohara     444: #if defined(HAVE_SIGACTION)
                    445: void (*set_signal(int sig, void (*handler)(int)))(int)
                    446: {
1.99      noro      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;
1.92      ohara     458: }
                    459: #endif
1.1       noro      460:
                    461: void sig_init() {
1.90      fujimoto  462: #if !defined(VISUAL) && !defined(__MINGW32__)
1.99      noro      463:   set_signal(SIGINT,int_handler);
1.1       noro      464: #else
1.99      noro      465:   void register_ctrlc_handler();
1.24      noro      466:
1.99      noro      467:   register_ctrlc_handler();
1.1       noro      468: #endif
1.99      noro      469:   set_signal(SIGSEGV,segv_handler);
1.1       noro      470:
1.23      noro      471: #if defined(SIGFPE)
1.99      noro      472:   set_signal(SIGFPE,fpe_handler);
1.1       noro      473: #endif
                    474:
1.23      noro      475: #if defined(SIGPIPE)
1.99      noro      476:   set_signal(SIGPIPE,pipe_handler);
1.1       noro      477: #endif
                    478:
1.23      noro      479: #if defined(SIGILL)
1.99      noro      480:   set_signal(SIGILL,ill_handler);
1.1       noro      481: #endif
                    482:
1.90      fujimoto  483: #if !defined(VISUAL) && !defined(__MINGW32__)
1.99      noro      484:   set_signal(SIGBUS,bus_handler);
1.1       noro      485: #endif
                    486: }
                    487:
                    488: static void (*old_int)(int);
                    489:
                    490: void asir_save_handler() {
1.99      noro      491:   old_int = set_signal(SIGINT,SIG_IGN);
                    492:   set_signal(SIGINT,old_int);
1.1       noro      493: }
                    494:
                    495: void asir_set_handler() {
1.99      noro      496:   set_signal(SIGINT,int_handler);
1.1       noro      497: }
                    498:
                    499: void asir_reset_handler() {
1.99      noro      500:   set_signal(SIGINT,old_int);
1.1       noro      501: }
                    502:
1.62      noro      503: extern int I_am_server;
                    504:
1.24      noro      505: void resetenv(char *s)
1.1       noro      506: {
1.99      noro      507:   extern FILE *outfile;
1.1       noro      508:
1.99      noro      509:   fprintf(stderr,"%s\n",s);
                    510:   while ( NEXT(asir_infile) )
                    511:     closecurrentinput();
                    512:   resetpvs();
1.90      fujimoto  513: #if !defined(VISUAL) && !defined(__MINGW32__)
1.99      noro      514:   if ( do_server_in_X11 )
1.1       noro      515: #endif
1.99      noro      516:     show_debug_window(0);
1.1       noro      517: #if defined(VISUAL_LIB)
1.99      noro      518:   w_noflush_stderr(0);
1.1       noro      519: #endif
1.90      fujimoto  520: #if defined(__MINGW32__)
1.99      noro      521:   fflush(stderr);
1.90      fujimoto  522: #endif
1.99      noro      523:   asir_out = stdout;
                    524:   /* restore states */
                    525:   reset_engine();
                    526:   reset_io();
1.90      fujimoto  527: #if !defined(VISUAL) && !defined(__MINGW32__)
1.99      noro      528:   reset_timer();
1.16      noro      529: #endif
1.99      noro      530:   LONGJMP(main_env,1);
1.1       noro      531: }
                    532:
1.24      noro      533: void fatal(int n)
1.1       noro      534: {
1.99      noro      535:   resetenv("return to toplevel");
1.1       noro      536: }
                    537:
                    538: extern int ox_int_received, critical_when_signal;
1.83      noro      539: extern int in_gc, caught_intr;
1.91      noro      540: extern int ox_get_pari_result;
1.1       noro      541:
1.24      noro      542: void int_handler(int sig)
1.1       noro      543: {
1.99      noro      544:   extern NODE PVSS;
                    545:   NODE t;
1.48      noro      546:
1.99      noro      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:   }
1.90      fujimoto  559: #if defined(VISUAL) || defined(__MINGW32__)
1.99      noro      560:   suspend_timer();
1.1       noro      561: #endif
1.99      noro      562:   set_signal(SIGINT,SIG_IGN);
1.90      fujimoto  563: #if !defined(VISUAL) && !defined(__MINGW32__)
1.3       noro      564:     if ( do_server_in_X11 ) {
1.99      noro      565:     debug(PVSS?((VS)BDY(PVSS))->usrf->f.usrf->body:0);
                    566:      restore_handler();
                    567:     return;
                    568:   }
1.1       noro      569: #endif
                    570: #if defined(linux)
                    571: #if 1
1.99      noro      572:   while ( stdin->_IO_read_ptr < stdin->_IO_read_end )
1.1       noro      573: #else
1.99      noro      574:   while ( stdin->_gptr < stdin->_egptr )
                    575: #endif
                    576:     getchar();
                    577: #endif
                    578:   while ( 1 ) {
                    579:     char buf[BUFSIZ];
                    580:     char c;
                    581:
                    582:     fprintf(stderr,"interrupt ?(q/t/c/d/u/w/?) "); fflush(stderr);
                    583:     buf[0] = '\n';
                    584:     while ( !feof(stdin) && buf[0] == '\n' )
                    585:       fgets(buf,BUFSIZ,stdin);
                    586:     if ( feof(stdin) ) {
                    587:       clearerr(stdin);
                    588:       continue;
                    589:     }
                    590:     switch ( c = buf[0] ) {
                    591:       case 'q':
                    592:         while ( 1 ) {
                    593:           fprintf(stderr,"Abort this session? (y or n) "); fflush(stderr);
                    594:           fgets(buf,BUFSIZ,stdin);
                    595:           if ( !strncmp(buf,"y",1) ) {
                    596:             fprintf(stderr,"Bye\n");
                    597: #if defined(__MINGW32__)
                    598:             fflush(stderr);
                    599: #endif
                    600:             /* for terminating myself */
                    601:             asir_infile = 0;
                    602:             asir_terminate(1);
                    603:           } else if ( !strncmp(buf,"n",1) ) {
                    604:             restore_handler();
                    605:             return;
                    606:           }
                    607:         }
                    608:         break;
                    609:       case 't':
                    610:       case 'u':
                    611:         while ( 1 ) {
                    612:           fprintf(stderr,"Abort this computation? (y or n) "); fflush(stderr);
                    613:           fgets(buf,BUFSIZ,stdin);
                    614:           if ( !strncmp(buf,"y",1) )
                    615:             break;
                    616:           else if ( !strncmp(buf,"n",1) ) {
                    617:             restore_handler();
                    618:             return;
                    619:           }
                    620:         }
                    621:         if ( debug_mode )
                    622:           debug_mode = 0;
                    623:         restore_handler();
                    624:         if ( c == 'u' ) {
                    625:           if ( user_int_handler ) {
                    626:             if ( !do_terse ) {
                    627:               fprintf(stderr,
                    628:                 "Calling the registered exception callbacks...");
                    629: #if defined(__MINGW32__)
                    630:               fflush(stderr);
1.1       noro      631: #endif
1.99      noro      632:             }
                    633:             for ( t = user_int_handler; t; t = NEXT(t) )
                    634:               bevalf((FUNC)BDY(t),0);
                    635:             if ( !do_terse ) {
                    636:               fprintf(stderr, "done.\n");
                    637: #if defined(__MINGW32__)
                    638:               fflush(stderr);
1.1       noro      639: #endif
1.99      noro      640:             }
                    641:           }
                    642:         }
                    643:         LEAVE_SIGNAL_CS_ALL;
                    644:         resetenv("return to toplevel");
                    645:         break;
                    646:       case 'd':
1.1       noro      647: #if 0
1.99      noro      648:         nextbp = 1; nextbplevel = -1;
1.1       noro      649: #endif
1.99      noro      650:         debug(PVSS?((VS)BDY(PVSS))->usrf->f.usrf->body:0);
                    651:          restore_handler();
                    652:          return;
                    653:       case 'c':
                    654:         restore_handler();
                    655:         return; break;
                    656:       case 'w':
                    657:         showpos();
                    658:         break;
                    659:       case '?':
                    660:         fprintf(stderr, "q:quit t:toplevel c:continue d:debug u:call registered handler w:where\n");
                    661: #if defined(__MINGW32__)
                    662:         fflush(stderr);
                    663: #endif
                    664:         break;
                    665:       default:
                    666:         break;
                    667:     }
                    668:   }
1.1       noro      669: }
                    670:
                    671: void restore_handler() {
1.90      fujimoto  672: #if defined(VISUAL) || defined(__MINGW32__)
1.99      noro      673:   resume_timer();
1.1       noro      674: #endif
1.19      noro      675: #if defined(SIGINT)
1.99      noro      676:   set_signal(SIGINT,int_handler);
1.1       noro      677: #endif
                    678: }
                    679:
1.24      noro      680: void segv_handler(int sig)
1.1       noro      681: {
1.19      noro      682: #if defined(SIGSEGV)
1.99      noro      683:   set_signal_for_restart(SIGSEGV,segv_handler);
                    684:   error("internal error (SEGV)");
1.1       noro      685: #endif
                    686: }
                    687:
1.24      noro      688: void ill_handler(int sig)
1.1       noro      689: {
1.19      noro      690: #if defined(SIGILL)
1.99      noro      691:   set_signal_for_restart(SIGILL,ill_handler);
                    692:   error("illegal instruction (ILL)");
1.1       noro      693: #endif
                    694: }
                    695:
1.69      noro      696: #if defined(__DARWIN__)
                    697: #define SIGNAL_FOR_TIMER SIGALRM
                    698: #define ITIMER_TYPE ITIMER_REAL
                    699: #else
                    700: #define SIGNAL_FOR_TIMER SIGVTALRM
                    701: #define ITIMER_TYPE ITIMER_VIRTUAL
                    702: #endif
                    703:
1.24      noro      704: void alrm_handler(int sig)
1.1       noro      705: {
1.99      noro      706:   fprintf(stderr,"interval timer expired (VTALRM)\n");
1.90      fujimoto  707: #if defined(__MINGW32__)
1.99      noro      708:   fflush(stderr);
1.89      fujimoto  709: #endif
1.99      noro      710:   LONGJMP(timer_env,1);
1.1       noro      711: }
                    712:
1.24      noro      713: void bus_handler(int sig)
1.1       noro      714: {
                    715: #if defined(SIGBUS)
1.99      noro      716:   set_signal_for_restart(SIGBUS,bus_handler);
                    717:   error("internal error (BUS ERROR)");
1.1       noro      718: #endif
                    719: }
                    720:
1.24      noro      721: void fpe_handler(int sig)
1.1       noro      722: {
1.19      noro      723: #if defined(SIGFPE)
1.99      noro      724:   set_signal_for_restart(SIGFPE,fpe_handler);
                    725:   error("internal error (FPE)");
1.1       noro      726: #endif
                    727: }
                    728:
1.24      noro      729: void pipe_handler(int sig)
1.1       noro      730: {
                    731: #if defined(SIGPIPE)
1.99      noro      732:   set_signal_for_restart(SIGPIPE,pipe_handler);
                    733:   end_critical();
                    734:   error("internal error (BROKEN PIPE)");
1.1       noro      735: #endif
                    736: }
                    737:
                    738: void resize_buffer()
                    739: {
                    740: }
                    741:
                    742: void tty_init() {
                    743: }
                    744:
                    745: void tty_reset() {
                    746: }
                    747:
                    748: extern int evalstatline;
                    749:
1.24      noro      750: void set_lasterror(char *s)
1.1       noro      751: {
1.99      noro      752:   strncpy(LastError,s,BUFSIZ);
                    753:   LastError[BUFSIZ-1] = 0;
1.1       noro      754: }
                    755:
                    756: SNODE error_snode;
1.51      noro      757: int error_in_timer;
1.1       noro      758:
1.24      noro      759: void error(char *s)
1.1       noro      760: {
1.99      noro      761:   SNODE *snp=0;
1.1       noro      762:
1.90      fujimoto  763: #if !defined(VISUAL) && !defined(__MINGW32__)
1.99      noro      764:   if ( !error_in_timer && timer_is_set )
                    765:     alrm_handler(SIGNAL_FOR_TIMER);
1.16      noro      766: #endif
1.99      noro      767:   fprintf(stderr,"%s\n",s);
1.90      fujimoto  768: #if defined(__MINGW32__)
1.99      noro      769:   fflush(stderr);
1.89      fujimoto  770: #endif
1.99      noro      771:   set_lasterror(s);
                    772:   if ( CPVS != GPVS ) {
                    773:     if ( CPVS && CPVS->usrf && CPVS->usrf->f.usrf )
                    774:       searchsn(&BDY(CPVS->usrf->f.usrf),evalstatline,&snp);
                    775:     if ( snp )
                    776:       error_snode = *snp;
                    777:     else
                    778:       error_snode = 0;
                    779:   } else
                    780:     error_snode = 0;
                    781:   if ( do_file ) {
                    782:     char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
                    783:
                    784:     sprintf(errbuf,"%s\n",s);
                    785:     showpos_to_string(errbuf+strlen(errbuf));
                    786:     set_lasterror(errbuf);
                    787:     ExitAsir();
                    788:   }
                    789:   if ( debug_mode )
                    790:     LONGJMP(debug_env,1);
                    791:   if ( CPVS != GPVS )
                    792:     if ( !no_debug_on_error && (do_server_in_X11 || isatty(0)) )
                    793:       bp(error_snode);
                    794:   if ( I_am_server )
                    795:     showpos_to_list(&LastStackTrace);
                    796:   resetenv("return to toplevel");
1.40      noro      797: }
                    798:
1.86      noro      799: void goto_toplevel(char *s)
1.40      noro      800: {
1.99      noro      801:   SNODE *snp=0;
1.40      noro      802:
1.90      fujimoto  803: #if !defined(VISUAL) && !defined(__MINGW32__)
1.99      noro      804:   if ( timer_is_set )
                    805:     alrm_handler(SIGNAL_FOR_TIMER);
1.40      noro      806: #endif
1.99      noro      807:   fprintf(stderr,"%s\n",s);
1.90      fujimoto  808: #if defined(__MINGW32__)
1.99      noro      809:   fflush(stderr);
1.89      fujimoto  810: #endif
1.99      noro      811:   if ( do_file ) {
                    812:     char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
1.40      noro      813:
1.99      noro      814:     sprintf(errbuf,"%s\n",s);
                    815:     showpos_to_string(errbuf+strlen(errbuf));
                    816:     ExitAsir();
                    817:   }
                    818:   resetenv("return to toplevel");
1.1       noro      819: }
                    820:
1.90      fujimoto  821: #if !defined(VISUAL) && !defined(__MINGW32__)
1.1       noro      822: #include <sys/time.h>
                    823:
1.24      noro      824: void set_timer(int interval)
1.1       noro      825: {
1.99      noro      826:   struct itimerval it;
1.1       noro      827:
1.99      noro      828:   it.it_interval.tv_sec = interval;
                    829:   it.it_interval.tv_usec = 0;
                    830:   it.it_value.tv_sec = interval;
                    831:   it.it_value.tv_usec = 0;
                    832:   setitimer(ITIMER_TYPE,&it,0);
                    833:   set_signal(SIGNAL_FOR_TIMER,alrm_handler);
                    834:   timer_is_set = 1;
1.1       noro      835: }
                    836:
                    837: void reset_timer()
                    838: {
1.99      noro      839:   struct itimerval it;
1.1       noro      840:
1.99      noro      841:   it.it_interval.tv_sec = 0;
                    842:   it.it_interval.tv_usec = 0;
                    843:   it.it_value.tv_sec = 0;
                    844:   it.it_value.tv_usec = 0;
                    845:   setitimer(ITIMER_TYPE,&it,0);
                    846:   set_signal(SIGNAL_FOR_TIMER,SIG_IGN);
                    847:   timer_is_set = 0;
1.1       noro      848: }
                    849: #endif
                    850:
                    851: unsigned int get_asir_version();
1.14      noro      852: char *get_asir_distribution();
1.1       noro      853:
1.72      ohara     854: char *get_gcversion()
                    855: {
1.100   ! ohara     856:   return "GC 7.4.2 copyright 1988-2015, H-J. Boehm, A. J. Demers, Xerox, SGI, HP.\n";
1.72      ohara     857: }
                    858:
1.47      ohara     859: char *get_pariversion()
                    860: {
1.99      noro      861:   return "";
1.1       noro      862: }
1.14      noro      863:
1.47      ohara     864: char *get_intervalversion()
                    865: {
                    866: #if defined(INTERVAL)
1.99      noro      867:   return " + Interval Arithmetic";
1.47      ohara     868: #else
1.99      noro      869:   return "";
1.47      ohara     870: #endif
                    871: }
                    872:
                    873: void copyright()
                    874: {
1.99      noro      875:   char *scopyright();
                    876:   fputs(scopyright(), stdout);
1.47      ohara     877: }
1.5       noro      878:
1.47      ohara     879: char *scopyright()
                    880: {
1.99      noro      881:   static char *notice;
                    882:   char *s1, *s2, *s3, *s4;
                    883:   int d, len;
                    884:   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";
                    885:   if (!notice) {
                    886:     s1 = get_intervalversion();
                    887:     s2 = get_asir_distribution();
                    888:     s3 = get_gcversion();
                    889:     s4 = get_pariversion();
                    890:     d  = get_asir_version();
                    891:     len = (strlen(format)-8)+strlen(s1)+strlen(s2)+strlen(s3)+strlen(s4)+sizeof(int)*3;
                    892:     notice = MALLOC(len+1);
                    893:     sprintf(notice, format, s1, d, s2, s3, s4);
                    894:   }
                    895:   return notice;
1.47      ohara     896: }
1.84      noro      897:
1.90      fujimoto  898: #if defined(VISUAL) || defined(__MINGW32__)
1.94      ohara     899: int recv_intr;
1.97      ohara     900:
                    901: static CRITICAL_SECTION signal_cs;
                    902: static int initialized_signal_cs;
                    903: static int signal_cs_count;
                    904:
                    905: static void init_signal_cs()
                    906: {
                    907:     if (!initialized_signal_cs) {
                    908:         InitializeCriticalSection(&signal_cs);
1.99      noro      909:     initialized_signal_cs=1;
1.97      ohara     910:         signal_cs_count=0;
                    911:     }
                    912: }
                    913:
                    914: void try_enter_signal_cs()
                    915: {
1.99      noro      916:   init_signal_cs();
1.97      ohara     917:     if(TryEnterCriticalSection(&signal_cs)) {
                    918:         signal_cs_count++;
                    919:     }
                    920: }
                    921:
                    922: void enter_signal_cs()
                    923: {
                    924:     init_signal_cs();
                    925:     EnterCriticalSection(&signal_cs);
                    926:     signal_cs_count++;
                    927: }
                    928:
                    929: void leave_signal_cs()
                    930: {
1.99      noro      931:   init_signal_cs();
1.97      ohara     932:     if(signal_cs_count>0) {
                    933:         LeaveCriticalSection(&signal_cs);
                    934:         signal_cs_count--;
                    935:     }
                    936: }
                    937:
                    938: void leave_signal_cs_all()
                    939: {
                    940:     if (!initialized_signal_cs) {
                    941:         init_signal_cs();
                    942:     }
                    943:     while(signal_cs_count>0) {
                    944:         LeaveCriticalSection(&signal_cs);
                    945:         signal_cs_count--;
                    946:     }
                    947: }
                    948:
1.84      noro      949: void check_intr()
                    950: {
1.99      noro      951:   extern int recv_intr;
                    952:   enter_signal_cs();
                    953:   if ( recv_intr ) {
                    954:     if ( recv_intr == 1 ) {
                    955:       recv_intr = 0;
                    956:       int_handler(SIGINT);
                    957:     } else {
                    958:       recv_intr = 0;
                    959:       ox_usr1_handler(0);
                    960:     }
                    961:   }
                    962:   leave_signal_cs_all();
1.84      noro      963: }
                    964: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>