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

Annotation of OpenXM_contrib2/asir2018/parse/glob.c, Revision 1.4

1.1       noro        1: /*
                      2:  * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
                      3:  * All rights reserved.
                      4:  *
                      5:  * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
                      6:  * non-exclusive and royalty-free license to use, copy, modify and
                      7:  * redistribute, solely for non-commercial and non-profit purposes, the
                      8:  * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
                      9:  * conditions of this Agreement. For the avoidance of doubt, you acquire
                     10:  * only a limited right to use the SOFTWARE hereunder, and FLL or any
                     11:  * third party developer retains all rights, including but not limited to
                     12:  * copyrights, in and to the SOFTWARE.
                     13:  *
                     14:  * (1) FLL does not grant you a license in any way for commercial
                     15:  * purposes. You may use the SOFTWARE only for non-commercial and
                     16:  * non-profit purposes only, such as academic, research and internal
                     17:  * business use.
                     18:  * (2) The SOFTWARE is protected by the Copyright Law of Japan and
                     19:  * international copyright treaties. If you make copies of the SOFTWARE,
                     20:  * with or without modification, as permitted hereunder, you shall affix
                     21:  * to all such copies of the SOFTWARE the above copyright notice.
                     22:  * (3) An explicit reference to this SOFTWARE and its copyright owner
                     23:  * shall be made on your publication or presentation in any form of the
                     24:  * results obtained by use of the SOFTWARE.
                     25:  * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
                     26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
                     27:  * for such modification or the source code of the modified part of the
                     28:  * SOFTWARE.
                     29:  *
                     30:  * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
                     31:  * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
                     32:  * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
                     33:  * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
                     34:  * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
                     35:  * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
                     36:  * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
                     37:  * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
                     38:  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
                     39:  * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
                     40:  * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
                     41:  * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
                     42:  * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
                     43:  * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
                     44:  * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
                     45:  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
                     46:  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
                     47:  *
1.4     ! fujimoto   48:  * $OpenXM: OpenXM_contrib2/asir2018/parse/glob.c,v 1.3 2019/03/29 04:43:18 ohara Exp $
1.1       noro       49: */
                     50: #include "ca.h"
                     51: #include "al.h"
                     52: #include "parse.h"
                     53: #include "ox.h"
                     54: #include <signal.h>
                     55: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(_PA_RISC1_1) && !defined(linux) && !defined(SYSV) && !defined(__CYGWIN__) && !defined(__INTERIX) && !defined(__FreeBSD__)
                     56: #include <sgtty.h>
                     57: #endif
                     58:
                     59: #if defined(VISUAL) || defined(__MINGW32__)
                     60: #include <io.h>
                     61: #include <direct.h>
                     62: #else
                     63: #include <unistd.h>
                     64: #include <string.h>
                     65: #include <stdio.h>
                     66: #endif
                     67:
                     68: #if defined(SYSV) && !defined(_IBMR2)
                     69: #include <sys/ttold.h>
                     70: #endif
                     71:
                     72: #if defined(VISUAL) || defined(__MINGW32__)
                     73: #define HISTORY asir_history
                     74: #endif
                     75:
                     76: #define MAXHIST 100
                     77:
                     78: extern FILE *asir_out;
                     79:
                     80: INFILE asir_infile;
                     81: JMP_BUF main_env,debug_env,timer_env,exec_env;
                     82: int little_endian,debug_mode,no_debug_on_error;
                     83: char *asir_libdir;
                     84: char *asir_contrib_dir;
                     85: char *asir_private_dir;
                     86: char *asir_pager;
                     87:
                     88: NODE usrf,sysf,noargsysf,ubinf,parif;
                     89: NODE ONENODE;
                     90: int main_parser, allow_create_var, ox_do_copy, ox_do_count, ox_count_length;
                     91: int ox_file_io, ox_need_conv;
                     92: char *ox_copy_bptr;
                     93: char *parse_strp;
                     94: SNODE parse_snode;
                     95: FUNC parse_targetf;
                     96: FILE *ox_istream,*ox_ostream;
                     97: int do_server_in_X11;
                     98: Obj LastVal;
                     99: LIST LastStackTrace;
                    100: char LastError[BUFSIZ];
                    101: int timer_is_set;
                    102: NODE current_option;
                    103: NODE user_int_handler, user_quit_handler;
                    104:
                    105: struct oV oVAR[] = {
                    106:   {"x",0,0}, {"y",0,0}, {"z",0,0}, {"u",0,0},
                    107:   {"v",0,0}, {"w",0,0}, {"p",0,0}, {"q",0,0},
                    108:   {"r",0,0}, {"s",0,0}, {"t",0,0}, {"a",0,0},
                    109:   {"b",0,0}, {"c",0,0}, {"d",0,0}, {"e",0,0},
                    110:   {"f",0,0}, {"g",0,0}, {"h",0,0}, {"i",0,0},
                    111:   {"j",0,0}, {"k",0,0}, {"l",0,0}, {"m",0,0},
                    112:   {"n",0,0}, {"o",0,0},
                    113:   {"_x",0,0}, {"_y",0,0}, {"_z",0,0}, {"_u",0,0},
                    114:   {"_v",0,0}, {"_w",0,0}, {"_p",0,0}, {"_q",0,0},
                    115:   {"_r",0,0}, {"_s",0,0}, {"_t",0,0}, {"_a",0,0},
                    116:   {"_b",0,0}, {"_c",0,0}, {"_d",0,0}, {"_e",0,0},
                    117:   {"_f",0,0}, {"_g",0,0}, {"_h",0,0}, {"_i",0,0},
                    118:   {"_j",0,0}, {"_k",0,0}, {"_l",0,0}, {"_m",0,0},
                    119:   {"_n",0,0}, {"_o",0,0}
                    120: };
                    121:
                    122: struct oVL oVLIST[52];
                    123:
                    124: VL CO = oVLIST;
                    125: VL ALG;
                    126: VL LASTCO;
                    127:
                    128: struct oVS oGPVS,oAPVS,oEPVS,oPPVS;
                    129: VS GPVS = &oGPVS;
                    130: VS APVS = &oAPVS;
                    131: VS EPVS = &oEPVS;
                    132: VS PPVS = &oPPVS;
                    133: VS CPVS,MPVS;
                    134:
                    135: NODE MODULE_LIST;
                    136: MODULE CUR_MODULE;
                    137: char *CUR_FUNC;
                    138:
                    139: struct oSYMBOL oGrlex, oGlex, oLex;
                    140: SYMBOL Symbol_grlex = &oGrlex;
                    141: SYMBOL Symbol_glex = &oGlex;
                    142: SYMBOL Symbol_lex = &oLex;;
                    143:
                    144: struct oF oF_TRUE,oF_FALSE;
                    145: F F_TRUE = &oF_TRUE;
                    146: F F_FALSE = &oF_FALSE;
                    147:
                    148: #if defined(VISUAL) || defined(__MINGW32__)
                    149: char cppname[BUFSIZ] = "c:\\asir\\stdlib\\cpp ";
                    150: #else
                    151: char cppname[BUFSIZ] = "/lib/cpp ";
                    152: #endif
                    153: char asirname[BUFSIZ];
                    154: char displayname[BUFSIZ];
                    155:
                    156: int Verbose;
                    157: int do_quiet;
                    158:
                    159: void glob_init() {
                    160:   int i;
                    161:
                    162:   for ( i = 0; i < 51; i++ ) {
                    163:     VR(&oVLIST[i]) = &oVAR[i]; NEXT(&oVLIST[i]) = &oVLIST[i+1];
                    164:   }
                    165:   VR(&oVLIST[i]) = &oVAR[i]; NEXT(&oVLIST[i]) = 0;
                    166:   LASTCO = &oVLIST[i];
                    167:   asir_reallocarray((char **)&GPVS->va,(int *)&GPVS->asize,(int *)&GPVS->n,(int)sizeof(struct oPV));
                    168:   asir_reallocarray((char **)&APVS->va,(int *)&APVS->asize,(int *)&APVS->n,(int)sizeof(struct oPV));
                    169:   asir_reallocarray((char **)&PPVS->va,(int *)&PPVS->asize,(int *)&PPVS->n,(int)sizeof(struct oPV));
                    170:   CPVS = GPVS;
                    171:   MKNODE(ONENODE,mkfnode(1,I_FORMULA,ONE),NULLP);
                    172:   OID(F_TRUE)=O_F; FOP(F_TRUE)=AL_TRUE; F_TRUE->arg.dummy = 0;
                    173:   OID(F_FALSE)=O_F; FOP(F_FALSE)=AL_FALSE; F_FALSE->arg.dummy = 0;
                    174:   OID(Symbol_grlex) = O_SYMBOL; Symbol_grlex->name = "@grlex";
                    175:   Symbol_grlex->value = 0;
                    176:   OID(Symbol_glex) = O_SYMBOL; Symbol_glex->name = "@glex";
                    177:   Symbol_glex->value = 1;
                    178:   OID(Symbol_lex) = O_SYMBOL; Symbol_lex->name = "@lex";
                    179:   Symbol_lex->value = 2;
                    180:   sprintf(asirname,"%s/asir_symtab",asir_libdir);
                    181: }
                    182:
                    183: void input_init(FILE *fp,char *name)
                    184: {
                    185:   asir_infile = (INFILE)CALLOC(sizeof(struct oINFILE),1);
                    186:   asir_infile->name = name; asir_infile->fp = fp;
                    187:   asir_infile->ln = 1; NEXT(asir_infile) = 0;
                    188: }
                    189:
                    190: void notdef(VL vl,Obj a,Obj b,Obj *c)
                    191: {
                    192:   error("undefined arithmetic operation.");
                    193: }
                    194:
                    195: int disable_debugger;
                    196: int do_asirrc;
                    197: int do_file;
                    198: char *do_filename;
                    199: int do_message;
                    200: int do_terse;
                    201: int do_fep;
                    202: int asir_setenv;
                    203: static int buserr_sav;
                    204: static char asir_history[BUFSIZ];
                    205:
                    206: extern int mpi_myid;
                    207:
                    208: #if !defined(VISUAL_LIB)
                    209: void ExitAsir() {
                    210:   exit(0);
                    211: }
                    212: #endif
                    213:
                    214: /*
                    215:  * status = 1 abnormal termination (error() etc.)
                    216:  * status = 2 normal termination (end(), quit() etc.)
                    217:  * status = 3 absolute termination
                    218:  */
                    219:
                    220: void asir_terminate(int status)
                    221: {
                    222:   int t;
                    223:   NODE n;
                    224:
                    225:   /* called from engine in Windows */
                    226:   if ( status == 3 ) asir_infile = 0;
                    227:
                    228:   if ( asir_infile && asir_infile->ready_for_longjmp )
                    229:     LONGJMP(asir_infile->jmpbuf,status);
                    230:   else {
                    231:     if ( user_quit_handler ) {
                    232:       if ( !do_terse ) {
                    233:         fprintf(stderr,"Calling the registered quit callbacks...");
                    234: #if defined(__MINGW32__)
                    235:         fflush(stderr);
                    236: #endif
                    237:       }
                    238:       for ( n = user_quit_handler; n; n = NEXT(n) )
                    239:         bevalf((FUNC)BDY(n),0);
                    240:       if ( !do_terse ) {
                    241:         fprintf(stderr, "done.\n");
                    242: #if defined(__MINGW32__)
                    243:         fflush(stderr);
                    244: #endif
                    245:       }
                    246:     }
                    247:     tty_reset();
                    248: #if defined(MPI)
                    249:     if ( !mpi_myid )
                    250:       close_allconnections();
                    251:     mpi_finalize();
                    252: #else
                    253: #if defined(SIGPIPE)
                    254:     set_signal(SIGPIPE,SIG_IGN);
                    255: #endif
                    256:     close_allconnections();
                    257: #endif
                    258:     if ( asir_out )
                    259:       fflush(asir_out);
                    260: #if FEP
                    261:     if ( do_fep ) {
                    262:       stifle_history(MAXHIST);
                    263:       write_history(asir_history);
                    264:     }
                    265: #endif
                    266:     ExitAsir();
                    267:   }
                    268: }
                    269:
                    270: void param_init() {
                    271:   unsigned int et = 0xff;
                    272:   extern int paristack;
                    273:   if ( *((char *)&et) )
                    274:     little_endian = 1;
                    275:   else
                    276:     little_endian = 0;
                    277: }
                    278:
                    279: Obj user_defined_prompt;
                    280:
                    281: void prompt() {
                    282:   if ( !do_quiet && !do_fep && asir_infile->fp == stdin ) {
                    283:     fprintf(asir_out,"[%d] ",APVS->n);
                    284:     fflush(asir_out);
                    285:   } else if ( do_quiet && user_defined_prompt
                    286:     && OID(user_defined_prompt)==O_STR) {
                    287:     fprintf(asir_out,BDY((STRING)user_defined_prompt),APVS->n);
                    288:     fflush(asir_out);
                    289:   }
                    290: }
                    291:
                    292: void sprompt(char *ptr)
                    293: {
                    294:   sprintf(ptr,"[%d] ",APVS->n);
                    295: }
                    296:
                    297: #if !defined(VISUAL) && !defined(__MINGW32__)
                    298: static int which(char *prog, char *path, char *buf, size_t size)
                    299: {
                    300:     char *tok;
                    301:     char delim[] = ":";
                    302:     char *path2  = malloc(strlen(path)+1);
                    303:     char *name   = malloc(size);
                    304:     int  proglen = strlen(prog)+3; /* "/" + prog + " \0" */
                    305:
                    306:     if (!name || !path2) {
                    307:         return 0;
                    308:     }
                    309:     strcpy(path2, path);
                    310:     tok = strtok(path2, delim);
                    311:     while (tok != NULL) {
                    312:         if (size >= strlen(tok)) {
                    313:             sprintf(name, "%s/%s", tok, prog);
                    314:             if (access(name, X_OK&R_OK) == 0) {
                    315:                 strcpy(buf, name);
                    316:                 strcat(buf, " ");
                    317:                 free(path2); free(name);
                    318:                 return 1;
                    319:             }
                    320:             tok = strtok(NULL, delim);
                    321:         }
                    322:     }
                    323:     free(path2); free(name);
                    324:     return 0;
                    325: }
                    326: #endif
                    327:
                    328: void cppname_init()
                    329: {
                    330: #if !defined(VISUAL) && !defined(__MINGW32__)
                    331:   char *oxhome;
                    332:   char oxcpp[BUFSIZ];
                    333: #define OXCPP "/bin/ox_cpp"
                    334:
                    335:   if ( oxhome = getenv("OpenXM_HOME") ) {
                    336:     if ( strlen(oxhome)+strlen(OXCPP)<BUFSIZ ) {
                    337:       sprintf(oxcpp,"%s%s",oxhome,OXCPP);
                    338:       if ( access(oxcpp,X_OK&R_OK) == 0 ) {
                    339:             strcpy(cppname,oxcpp);
                    340:         return;
                    341:       }
                    342:     }
                    343:   }
                    344:     if (access(cppname, X_OK&R_OK) != 0) {
                    345:         which("cpp", "/lib:/usr/ccs/lib:/usr/bin", cppname, BUFSIZ) ||
                    346:         which("cpp", getenv("PATH"), cppname, BUFSIZ);
                    347:     }
                    348: #endif
                    349: }
                    350:
                    351: FILE *in_fp;
                    352:
                    353: void process_args(int ac,char **av)
                    354: {
                    355:   int nm,dv;
                    356:   do_asirrc = 1;
                    357: #if !defined(MPI)
                    358:   do_message = 1;
                    359: #endif
                    360: #if defined(VISUAL) && defined(VISUAL_CONSOLE)
                    361:   disable_debugger=1;
                    362: #endif
                    363:   do_quiet = 0;
                    364:   while ( ac > 0 ) {
                    365:     if ( !strcmp(*av,"-heap") && (ac >= 2) ) {
                    366:       void GC_expand_hp(int);
                    367:
                    368:       GC_expand_hp(atoi(*(av+1))); av += 2; ac -= 2;
                    369:     } else if ( !strcmp(*av,"-adj") && (ac >= 2) ) {
                    370:       char *slash;
                    371:       slash = strrchr(*(av+1),'/');
                    372:       if ( slash ) {
                    373:         *slash = 0;
                    374:         nm = atoi(slash+1);
                    375:       }else {
                    376:         nm = 1;
                    377:       }
                    378:       dv = atoi(*(av+1));
                    379:       Risa_GC_set_adj(nm,dv);
                    380:       av += 2; ac -= 2;
                    381:     } else if ( !strcmp(*av,"-cpp") && (ac >= 2) ) {
                    382:       strcpy(cppname,*(av+1)); av += 2; ac -= 2;
                    383:     } else if ( !strcmp(*av,"-d") && (ac >= 2) ) {
                    384: #if defined(VISUAL) && defined(VISUAL_CONSOLE)
                    385:       disable_debugger=0;
                    386: #endif
                    387:       av += 2; ac -= 2;
                    388:     } else if ( !strcmp(*av,"-f") && (ac >= 2) ) {
                    389:       do_quiet = 1;
                    390:       in_fp = fopen(*(av+1),"r");
                    391:       if ( !in_fp ) {
                    392:         fprintf(stderr,"%s does not exist!\n",*(av+1));
                    393: #if defined(__MINGW32__)
                    394:         fflush(stderr);
                    395: #endif
                    396:         asir_terminate(1);
                    397:       }
                    398:       do_file = 1;
                    399:       do_filename = *(av+1);
                    400:       av += 2; ac -= 2;
                    401:     } else if ( !strcmp(*av,"-E") ) {
                    402:       asir_setenv = 1; av++; ac--;
                    403:     } else if ( !strcmp(*av,"-quiet") ) {
                    404:       do_quiet = 1; av++; ac--;
                    405:     } else if ( !strcmp(*av,"-norc") ) {
                    406:       do_asirrc = 0; av++; ac--;
                    407:     } else if ( !strcmp(*av,"-nomessage") ) {
                    408:       do_message = 0; av++; ac--;
                    409:     } else if ( !strcmp(*av,"-rootdir") && (ac >= 2) ) {
                    410:       set_rootdir(*(av+1)); av += 2; ac -= 2;
                    411:     } else if ( !strcmp(*av,"-maxheap") && (ac >= 2) ) {
                    412:       void GC_set_max_heap_size(int);
                    413:
                    414:       GC_set_max_heap_size(atoi(*(av+1))); av += 2; ac -= 2;
                    415: #if !defined(VISUAL) && !defined(__MINGW32__)
                    416:     } else if ( !strcmp(*av,"-display") && (ac >= 2) ) {
                    417:       strcpy(displayname,*(av+1)); av += 2; ac -= 2;
                    418: #endif
                    419: #if FEP
                    420:     } else if ( !strcmp(*av,"-fep") ) {
                    421:       do_fep = 1; av++; ac--;
                    422: #endif
                    423:     } else {
                    424:       fprintf(stderr,"%s : unknown option.\n",*av);
                    425: #if defined(__MINGW32__)
                    426:       fflush(stderr);
                    427: #endif
                    428:       asir_terminate(1);
                    429:     }
                    430:   }
                    431: #if FEP
                    432:   if ( do_fep ) {
                    433:     char *home;
                    434:     home = (char *)getenv("HOME");
                    435:     if (!home)
                    436:       home = ".";
                    437:     sprintf (asir_history, "%s/.asir_history",home);
                    438:     read_history(asir_history);
                    439:     using_history();
                    440:   }
                    441: #endif
                    442: }
                    443:
                    444: #if defined(HAVE_SIGACTION)
                    445: void (*set_signal(int sig, void (*handler)(int)))(int)
                    446: {
                    447:   struct sigaction act;
                    448:   struct sigaction oldact;
                    449:   if (handler == SIG_IGN || handler == SIG_DFL) {
                    450:     return signal(sig,handler);
                    451:   }
                    452:   act.sa_handler=handler;
                    453:   act.sa_flags=0;
                    454:   act.sa_flags |= SA_RESTART;
                    455:   sigemptyset(&act.sa_mask);
                    456:   sigaction(sig,&act,&oldact);
                    457:   return oldact.sa_handler;
                    458: }
                    459: #endif
                    460:
                    461: void sig_init() {
                    462: #if !defined(VISUAL) && !defined(__MINGW32__)
                    463:   set_signal(SIGINT,int_handler);
                    464: #else
                    465:   void register_ctrlc_handler();
                    466:
                    467:   register_ctrlc_handler();
                    468: #endif
                    469:   set_signal(SIGSEGV,segv_handler);
                    470:
                    471: #if defined(SIGFPE)
                    472:   set_signal(SIGFPE,fpe_handler);
                    473: #endif
                    474:
                    475: #if defined(SIGPIPE)
                    476:   set_signal(SIGPIPE,pipe_handler);
                    477: #endif
                    478:
                    479: #if defined(SIGILL)
                    480:   set_signal(SIGILL,ill_handler);
                    481: #endif
                    482:
                    483: #if !defined(VISUAL) && !defined(__MINGW32__)
                    484:   set_signal(SIGBUS,bus_handler);
                    485: #endif
                    486: }
                    487:
                    488: static void (*old_int)(int);
                    489:
                    490: void asir_save_handler() {
                    491:   old_int = set_signal(SIGINT,SIG_IGN);
                    492:   set_signal(SIGINT,old_int);
                    493: }
                    494:
                    495: void asir_set_handler() {
                    496:   set_signal(SIGINT,int_handler);
                    497: }
                    498:
                    499: void asir_reset_handler() {
                    500:   set_signal(SIGINT,old_int);
                    501: }
                    502:
                    503: extern int I_am_server;
                    504:
                    505: void resetenv(char *s)
                    506: {
                    507:   extern FILE *outfile;
                    508:
                    509:   fprintf(stderr,"%s\n",s);
                    510:   while ( NEXT(asir_infile) )
                    511:     closecurrentinput();
                    512:   resetpvs();
                    513: #if !defined(VISUAL) && !defined(__MINGW32__)
                    514:   if ( do_server_in_X11 )
                    515: #endif
                    516:     show_debug_window(0);
                    517: #if defined(VISUAL_LIB)
                    518:   w_noflush_stderr(0);
                    519: #endif
                    520: #if defined(__MINGW32__)
                    521:   fflush(stderr);
                    522: #endif
                    523:   asir_out = stdout;
                    524:   /* restore states */
                    525:   reset_engine();
                    526:   reset_io();
                    527: #if !defined(VISUAL) && !defined(__MINGW32__)
                    528:   reset_timer();
                    529: #endif
                    530:   LONGJMP(main_env,1);
                    531: }
                    532:
                    533: void fatal(int n)
                    534: {
                    535:   resetenv("return to toplevel");
                    536: }
                    537:
                    538: extern int ox_int_received, critical_when_signal;
                    539: extern int in_gc, caught_intr;
                    540: extern int ox_get_pari_result;
                    541:
                    542: void int_handler(int sig)
                    543: {
                    544:   extern NODE PVSS;
                    545:   NODE t;
                    546:
                    547:   if ( do_file || disable_debugger ) {
                    548:     LEAVE_SIGNAL_CS_ALL;
                    549:     ExitAsir();
                    550:   }
                    551:   if ( !ox_get_pari_result && critical_when_signal ) {
                    552:     ox_int_received = 1;
                    553:     return;
                    554:   }
                    555:   if ( in_gc ) {
                    556:     caught_intr = 1;
                    557:     return;
                    558:   }
                    559: #if defined(VISUAL) || defined(__MINGW32__)
                    560:   suspend_timer();
                    561: #endif
                    562:   set_signal(SIGINT,SIG_IGN);
                    563: #if !defined(VISUAL) && !defined(__MINGW32__)
                    564:     if ( do_server_in_X11 ) {
                    565:     debug(PVSS?((VS)BDY(PVSS))->usrf->f.usrf->body:0);
                    566:      restore_handler();
                    567:     return;
                    568:   }
                    569: #endif
1.4     ! fujimoto  570: #if !defined(ANDROID) && defined(linux)
1.1       noro      571: #if 1
                    572:   while ( stdin->_IO_read_ptr < stdin->_IO_read_end )
                    573: #else
                    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);
                    631: #endif
                    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);
                    639: #endif
                    640:             }
                    641:           }
                    642:         }
                    643:         LEAVE_SIGNAL_CS_ALL;
                    644:         resetenv("return to toplevel");
                    645:         break;
                    646:       case 'd':
                    647: #if 0
                    648:         nextbp = 1; nextbplevel = -1;
                    649: #endif
                    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:   }
                    669: }
                    670:
                    671: void restore_handler() {
                    672: #if defined(VISUAL) || defined(__MINGW32__)
                    673:   resume_timer();
                    674: #endif
                    675: #if defined(SIGINT)
                    676:   set_signal(SIGINT,int_handler);
                    677: #endif
                    678: }
                    679:
                    680: void segv_handler(int sig)
                    681: {
                    682: #if defined(SIGSEGV)
                    683:   set_signal_for_restart(SIGSEGV,segv_handler);
                    684:   error("internal error (SEGV)");
                    685: #endif
                    686: }
                    687:
                    688: void ill_handler(int sig)
                    689: {
                    690: #if defined(SIGILL)
                    691:   set_signal_for_restart(SIGILL,ill_handler);
                    692:   error("illegal instruction (ILL)");
                    693: #endif
                    694: }
                    695:
                    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:
                    704: void alrm_handler(int sig)
                    705: {
                    706:   fprintf(stderr,"interval timer expired (VTALRM)\n");
                    707: #if defined(__MINGW32__)
                    708:   fflush(stderr);
                    709: #endif
                    710:   LONGJMP(timer_env,1);
                    711: }
                    712:
                    713: void bus_handler(int sig)
                    714: {
                    715: #if defined(SIGBUS)
                    716:   set_signal_for_restart(SIGBUS,bus_handler);
                    717:   error("internal error (BUS ERROR)");
                    718: #endif
                    719: }
                    720:
                    721: void fpe_handler(int sig)
                    722: {
                    723: #if defined(SIGFPE)
                    724:   set_signal_for_restart(SIGFPE,fpe_handler);
                    725:   error("internal error (FPE)");
                    726: #endif
                    727: }
                    728:
                    729: void pipe_handler(int sig)
                    730: {
                    731: #if defined(SIGPIPE)
                    732:   set_signal_for_restart(SIGPIPE,pipe_handler);
                    733:   end_critical();
                    734:   error("internal error (BROKEN PIPE)");
                    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:
                    750: void set_lasterror(char *s)
                    751: {
                    752:   strncpy(LastError,s,BUFSIZ);
                    753:   LastError[BUFSIZ-1] = 0;
                    754: }
                    755:
                    756: SNODE error_snode;
                    757: int error_in_timer;
                    758:
                    759: void error(char *s)
                    760: {
                    761:   SNODE *snp=0;
                    762:
                    763: #if !defined(VISUAL) && !defined(__MINGW32__)
                    764:   if ( !error_in_timer && timer_is_set )
                    765:     alrm_handler(SIGNAL_FOR_TIMER);
                    766: #endif
                    767:   fprintf(stderr,"%s\n",s);
                    768: #if defined(__MINGW32__)
                    769:   fflush(stderr);
                    770: #endif
                    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");
                    797: }
                    798:
                    799: void goto_toplevel(char *s)
                    800: {
                    801:   SNODE *snp=0;
                    802:
                    803: #if !defined(VISUAL) && !defined(__MINGW32__)
                    804:   if ( timer_is_set )
                    805:     alrm_handler(SIGNAL_FOR_TIMER);
                    806: #endif
                    807:   fprintf(stderr,"%s\n",s);
                    808: #if defined(__MINGW32__)
                    809:   fflush(stderr);
                    810: #endif
                    811:   if ( do_file ) {
                    812:     char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
                    813:
                    814:     sprintf(errbuf,"%s\n",s);
                    815:     showpos_to_string(errbuf+strlen(errbuf));
                    816:     ExitAsir();
                    817:   }
                    818:   resetenv("return to toplevel");
                    819: }
                    820:
                    821: #if !defined(VISUAL) && !defined(__MINGW32__)
                    822: #include <sys/time.h>
                    823:
                    824: void set_timer(int interval)
                    825: {
                    826:   struct itimerval it;
                    827:
                    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;
                    835: }
                    836:
                    837: void reset_timer()
                    838: {
                    839:   struct itimerval it;
                    840:
                    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;
                    848: }
                    849: #endif
                    850:
                    851: unsigned int get_asir_version();
                    852: char *get_asir_distribution();
                    853:
                    854: char *get_gcversion()
                    855: {
1.3       ohara     856:   return "GC 7.4.2 copyright 1988-2015, H-J. Boehm, A. J. Demers, Xerox, SGI, HP.\n";
1.1       noro      857: }
                    858:
                    859: char *get_pariversion()
                    860: {
                    861:   return "";
                    862: }
                    863:
                    864: char *get_intervalversion()
                    865: {
                    866: #if defined(INTERVAL)
                    867:   return " + Interval Arithmetic";
                    868: #else
                    869:   return "";
                    870: #endif
                    871: }
                    872:
                    873: void copyright()
                    874: {
                    875:   char *scopyright();
                    876:   fputs(scopyright(), stdout);
                    877: }
                    878:
                    879: char *scopyright()
                    880: {
                    881:   static char *notice;
                    882:   char *s1, *s2, *s3, *s4;
                    883:   int d, len;
1.3       ohara     884:   char *format = "This is Risa/Asir%s, full GMP Version %d (%s Distribution).\nCopyright (C) 1994-2000, all rights reserved, FUJITSU LABORATORIES LIMITED.\nCopyright 2000-2019, Risa/Asir committers, http://www.openxm.org/.\n%s%s";
1.1       noro      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;
                    896: }
                    897:
                    898: #if defined(VISUAL) || defined(__MINGW32__)
                    899: int recv_intr;
                    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);
                    909:     initialized_signal_cs=1;
                    910:         signal_cs_count=0;
                    911:     }
                    912: }
                    913:
                    914: void try_enter_signal_cs()
                    915: {
                    916:   init_signal_cs();
                    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: {
                    931:   init_signal_cs();
                    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:
                    949: void check_intr()
                    950: {
                    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();
                    963: }
                    964: #endif

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