[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.8

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.8     ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2018/parse/glob.c,v 1.7 2020/06/20 05:56:08 fujimoto Exp $
1.1       noro       49: */
                     50: #include "ca.h"
                     51: #include "al.h"
                     52: #include "parse.h"
                     53: #include "ox.h"
                     54: #include <signal.h>
                     55: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(_PA_RISC1_1) && !defined(linux) && !defined(SYSV) && !defined(__CYGWIN__) && !defined(__INTERIX) && !defined(__FreeBSD__)
                     56: #include <sgtty.h>
                     57: #endif
                     58:
                     59: #if defined(VISUAL) || defined(__MINGW32__)
                     60: #include <io.h>
                     61: #include <direct.h>
                     62: #else
                     63: #include <unistd.h>
                     64: #include <string.h>
                     65: #include <stdio.h>
                     66: #endif
                     67:
                     68: #if defined(SYSV) && !defined(_IBMR2)
                     69: #include <sys/ttold.h>
                     70: #endif
                     71:
                     72: #if defined(VISUAL) || defined(__MINGW32__)
                     73: #define HISTORY asir_history
                     74: #endif
                     75:
                     76: #define MAXHIST 100
                     77:
                     78: extern FILE *asir_out;
                     79:
                     80: INFILE asir_infile;
                     81: JMP_BUF main_env,debug_env,timer_env,exec_env;
                     82: int little_endian,debug_mode,no_debug_on_error;
                     83: char *asir_libdir;
                     84: char *asir_contrib_dir;
                     85: char *asir_private_dir;
                     86: char *asir_pager;
                     87:
                     88: NODE usrf,sysf,noargsysf,ubinf,parif;
                     89: NODE ONENODE;
                     90: int main_parser, allow_create_var, ox_do_copy, ox_do_count, ox_count_length;
                     91: int ox_file_io, ox_need_conv;
                     92: char *ox_copy_bptr;
                     93: char *parse_strp;
                     94: SNODE parse_snode;
                     95: FUNC parse_targetf;
                     96: FILE *ox_istream,*ox_ostream;
                     97: int do_server_in_X11;
                     98: Obj LastVal;
                     99: LIST LastStackTrace;
                    100: char LastError[BUFSIZ];
                    101: int timer_is_set;
                    102: NODE current_option;
                    103: NODE user_int_handler, user_quit_handler;
                    104:
                    105: struct oV oVAR[] = {
                    106:   {"x",0,0}, {"y",0,0}, {"z",0,0}, {"u",0,0},
                    107:   {"v",0,0}, {"w",0,0}, {"p",0,0}, {"q",0,0},
                    108:   {"r",0,0}, {"s",0,0}, {"t",0,0}, {"a",0,0},
                    109:   {"b",0,0}, {"c",0,0}, {"d",0,0}, {"e",0,0},
                    110:   {"f",0,0}, {"g",0,0}, {"h",0,0}, {"i",0,0},
                    111:   {"j",0,0}, {"k",0,0}, {"l",0,0}, {"m",0,0},
                    112:   {"n",0,0}, {"o",0,0},
                    113:   {"_x",0,0}, {"_y",0,0}, {"_z",0,0}, {"_u",0,0},
                    114:   {"_v",0,0}, {"_w",0,0}, {"_p",0,0}, {"_q",0,0},
                    115:   {"_r",0,0}, {"_s",0,0}, {"_t",0,0}, {"_a",0,0},
                    116:   {"_b",0,0}, {"_c",0,0}, {"_d",0,0}, {"_e",0,0},
                    117:   {"_f",0,0}, {"_g",0,0}, {"_h",0,0}, {"_i",0,0},
                    118:   {"_j",0,0}, {"_k",0,0}, {"_l",0,0}, {"_m",0,0},
                    119:   {"_n",0,0}, {"_o",0,0}
                    120: };
                    121:
                    122: struct oVL oVLIST[52];
                    123:
                    124: VL CO = oVLIST;
                    125: VL ALG;
                    126: VL LASTCO;
                    127:
                    128: struct oVS oGPVS,oAPVS,oEPVS,oPPVS;
                    129: VS GPVS = &oGPVS;
                    130: VS APVS = &oAPVS;
                    131: VS EPVS = &oEPVS;
                    132: VS PPVS = &oPPVS;
                    133: VS CPVS,MPVS;
                    134:
                    135: NODE MODULE_LIST;
                    136: MODULE CUR_MODULE;
                    137: char *CUR_FUNC;
                    138:
                    139: struct oSYMBOL oGrlex, oGlex, oLex;
                    140: SYMBOL Symbol_grlex = &oGrlex;
                    141: SYMBOL Symbol_glex = &oGlex;
                    142: SYMBOL Symbol_lex = &oLex;;
                    143:
                    144: struct oF oF_TRUE,oF_FALSE;
                    145: F F_TRUE = &oF_TRUE;
                    146: F F_FALSE = &oF_FALSE;
                    147:
                    148: #if defined(VISUAL) || defined(__MINGW32__)
                    149: char cppname[BUFSIZ] = "c:\\asir\\stdlib\\cpp ";
                    150: #else
                    151: char cppname[BUFSIZ] = "/lib/cpp ";
                    152: #endif
                    153: char asirname[BUFSIZ];
                    154: char displayname[BUFSIZ];
                    155:
                    156: int Verbose;
                    157: int do_quiet;
                    158:
                    159: void glob_init() {
                    160:   int i;
                    161:
                    162:   for ( i = 0; i < 51; i++ ) {
                    163:     VR(&oVLIST[i]) = &oVAR[i]; NEXT(&oVLIST[i]) = &oVLIST[i+1];
                    164:   }
                    165:   VR(&oVLIST[i]) = &oVAR[i]; NEXT(&oVLIST[i]) = 0;
                    166:   LASTCO = &oVLIST[i];
                    167:   asir_reallocarray((char **)&GPVS->va,(int *)&GPVS->asize,(int *)&GPVS->n,(int)sizeof(struct oPV));
                    168:   asir_reallocarray((char **)&APVS->va,(int *)&APVS->asize,(int *)&APVS->n,(int)sizeof(struct oPV));
                    169:   asir_reallocarray((char **)&PPVS->va,(int *)&PPVS->asize,(int *)&PPVS->n,(int)sizeof(struct oPV));
                    170:   CPVS = GPVS;
                    171:   MKNODE(ONENODE,mkfnode(1,I_FORMULA,ONE),NULLP);
                    172:   OID(F_TRUE)=O_F; FOP(F_TRUE)=AL_TRUE; F_TRUE->arg.dummy = 0;
                    173:   OID(F_FALSE)=O_F; FOP(F_FALSE)=AL_FALSE; F_FALSE->arg.dummy = 0;
                    174:   OID(Symbol_grlex) = O_SYMBOL; Symbol_grlex->name = "@grlex";
                    175:   Symbol_grlex->value = 0;
                    176:   OID(Symbol_glex) = O_SYMBOL; Symbol_glex->name = "@glex";
                    177:   Symbol_glex->value = 1;
                    178:   OID(Symbol_lex) = O_SYMBOL; Symbol_lex->name = "@lex";
                    179:   Symbol_lex->value = 2;
                    180:   sprintf(asirname,"%s/asir_symtab",asir_libdir);
                    181: }
                    182:
                    183: void input_init(FILE *fp,char *name)
                    184: {
                    185:   asir_infile = (INFILE)CALLOC(sizeof(struct oINFILE),1);
                    186:   asir_infile->name = name; asir_infile->fp = fp;
                    187:   asir_infile->ln = 1; NEXT(asir_infile) = 0;
                    188: }
                    189:
                    190: void notdef(VL vl,Obj a,Obj b,Obj *c)
                    191: {
                    192:   error("undefined arithmetic operation.");
                    193: }
                    194:
                    195: int disable_debugger;
                    196: int do_asirrc;
                    197: int do_file;
                    198: char *do_filename;
                    199: int do_message;
                    200: int do_terse;
                    201: int do_fep;
                    202: int asir_setenv;
                    203: static int buserr_sav;
                    204: static char asir_history[BUFSIZ];
                    205:
                    206: extern int mpi_myid;
                    207:
                    208: #if !defined(VISUAL_LIB)
                    209: void ExitAsir() {
                    210:   exit(0);
                    211: }
                    212: #endif
                    213:
                    214: /*
                    215:  * status = 1 abnormal termination (error() etc.)
                    216:  * status = 2 normal termination (end(), quit() etc.)
                    217:  * status = 3 absolute termination
                    218:  */
                    219:
                    220: void asir_terminate(int status)
                    221: {
                    222:   int t;
                    223:   NODE n;
                    224:
                    225:   /* called from engine in Windows */
                    226:   if ( status == 3 ) asir_infile = 0;
                    227:
                    228:   if ( asir_infile && asir_infile->ready_for_longjmp )
                    229:     LONGJMP(asir_infile->jmpbuf,status);
                    230:   else {
                    231:     if ( user_quit_handler ) {
                    232:       if ( !do_terse ) {
                    233:         fprintf(stderr,"Calling the registered quit callbacks...");
                    234: #if defined(__MINGW32__)
                    235:         fflush(stderr);
                    236: #endif
                    237:       }
                    238:       for ( n = user_quit_handler; n; n = NEXT(n) )
                    239:         bevalf((FUNC)BDY(n),0);
                    240:       if ( !do_terse ) {
                    241:         fprintf(stderr, "done.\n");
                    242: #if defined(__MINGW32__)
                    243:         fflush(stderr);
                    244: #endif
                    245:       }
                    246:     }
                    247:     tty_reset();
                    248: #if defined(MPI)
                    249:     if ( !mpi_myid )
                    250:       close_allconnections();
                    251:     mpi_finalize();
                    252: #else
                    253: #if defined(SIGPIPE)
                    254:     set_signal(SIGPIPE,SIG_IGN);
                    255: #endif
                    256:     close_allconnections();
                    257: #endif
                    258:     if ( asir_out )
                    259:       fflush(asir_out);
                    260: #if FEP
                    261:     if ( do_fep ) {
                    262:       stifle_history(MAXHIST);
                    263:       write_history(asir_history);
                    264:     }
                    265: #endif
                    266:     ExitAsir();
                    267:   }
                    268: }
                    269:
                    270: void param_init() {
                    271:   unsigned int et = 0xff;
                    272:   extern int paristack;
                    273:   if ( *((char *)&et) )
                    274:     little_endian = 1;
                    275:   else
                    276:     little_endian = 0;
                    277: }
                    278:
                    279: Obj user_defined_prompt;
                    280:
                    281: void prompt() {
                    282:   if ( !do_quiet && !do_fep && asir_infile->fp == stdin ) {
                    283:     fprintf(asir_out,"[%d] ",APVS->n);
                    284:     fflush(asir_out);
                    285:   } else if ( do_quiet && user_defined_prompt
                    286:     && OID(user_defined_prompt)==O_STR) {
                    287:     fprintf(asir_out,BDY((STRING)user_defined_prompt),APVS->n);
                    288:     fflush(asir_out);
                    289:   }
                    290: }
                    291:
                    292: void sprompt(char *ptr)
                    293: {
                    294:   sprintf(ptr,"[%d] ",APVS->n);
                    295: }
                    296:
                    297: #if !defined(VISUAL) && !defined(__MINGW32__)
                    298: static int which(char *prog, char *path, char *buf, size_t size)
                    299: {
                    300:     char *tok;
                    301:     char delim[] = ":";
                    302:     char *path2  = malloc(strlen(path)+1);
                    303:     char *name   = malloc(size);
                    304:     int  proglen = strlen(prog)+3; /* "/" + prog + " \0" */
                    305:
                    306:     if (!name || !path2) {
                    307:         return 0;
                    308:     }
                    309:     strcpy(path2, path);
                    310:     tok = strtok(path2, delim);
                    311:     while (tok != NULL) {
                    312:         if (size >= strlen(tok)) {
                    313:             sprintf(name, "%s/%s", tok, prog);
                    314:             if (access(name, X_OK&R_OK) == 0) {
                    315:                 strcpy(buf, name);
                    316:                 strcat(buf, " ");
                    317:                 free(path2); free(name);
                    318:                 return 1;
                    319:             }
                    320:             tok = strtok(NULL, delim);
                    321:         }
                    322:     }
                    323:     free(path2); free(name);
                    324:     return 0;
                    325: }
                    326: #endif
                    327:
                    328: void cppname_init()
                    329: {
                    330: #if !defined(VISUAL) && !defined(__MINGW32__)
                    331:   char *oxhome;
                    332:   char oxcpp[BUFSIZ];
                    333: #define OXCPP "/bin/ox_cpp"
                    334:
1.8     ! noro      335:   if ( ( oxhome = getenv("OpenXM_HOME") ) != 0 ) {
1.1       noro      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.7       fujimoto  571: /*
1.1       noro      572: #if 1
1.6       noro      573:   while ( stdin->_IO_read_ptr < stdin->_IO_read_end )
1.1       noro      574: #else
                    575:   while ( stdin->_gptr < stdin->_egptr )
                    576: #endif
                    577:     getchar();
1.7       fujimoto  578: */
                    579:   if( __freadable(stdin) ) __fpurge(stdin);
1.1       noro      580: #endif
                    581:   while ( 1 ) {
                    582:     char buf[BUFSIZ];
                    583:     char c;
                    584:
                    585:     fprintf(stderr,"interrupt ?(q/t/c/d/u/w/?) "); fflush(stderr);
                    586:     buf[0] = '\n';
                    587:     while ( !feof(stdin) && buf[0] == '\n' )
                    588:       fgets(buf,BUFSIZ,stdin);
                    589:     if ( feof(stdin) ) {
                    590:       clearerr(stdin);
                    591:       continue;
                    592:     }
                    593:     switch ( c = buf[0] ) {
                    594:       case 'q':
                    595:         while ( 1 ) {
                    596:           fprintf(stderr,"Abort this session? (y or n) "); fflush(stderr);
                    597:           fgets(buf,BUFSIZ,stdin);
                    598:           if ( !strncmp(buf,"y",1) ) {
                    599:             fprintf(stderr,"Bye\n");
                    600: #if defined(__MINGW32__)
                    601:             fflush(stderr);
                    602: #endif
                    603:             /* for terminating myself */
                    604:             asir_infile = 0;
                    605:             asir_terminate(1);
                    606:           } else if ( !strncmp(buf,"n",1) ) {
                    607:             restore_handler();
                    608:             return;
                    609:           }
                    610:         }
                    611:         break;
                    612:       case 't':
                    613:       case 'u':
                    614:         while ( 1 ) {
                    615:           fprintf(stderr,"Abort this computation? (y or n) "); fflush(stderr);
                    616:           fgets(buf,BUFSIZ,stdin);
                    617:           if ( !strncmp(buf,"y",1) )
                    618:             break;
                    619:           else if ( !strncmp(buf,"n",1) ) {
                    620:             restore_handler();
                    621:             return;
                    622:           }
                    623:         }
                    624:         if ( debug_mode )
                    625:           debug_mode = 0;
                    626:         restore_handler();
                    627:         if ( c == 'u' ) {
                    628:           if ( user_int_handler ) {
                    629:             if ( !do_terse ) {
                    630:               fprintf(stderr,
                    631:                 "Calling the registered exception callbacks...");
                    632: #if defined(__MINGW32__)
                    633:               fflush(stderr);
                    634: #endif
                    635:             }
                    636:             for ( t = user_int_handler; t; t = NEXT(t) )
                    637:               bevalf((FUNC)BDY(t),0);
                    638:             if ( !do_terse ) {
                    639:               fprintf(stderr, "done.\n");
                    640: #if defined(__MINGW32__)
                    641:               fflush(stderr);
                    642: #endif
                    643:             }
                    644:           }
                    645:         }
                    646:         LEAVE_SIGNAL_CS_ALL;
                    647:         resetenv("return to toplevel");
                    648:         break;
                    649:       case 'd':
                    650: #if 0
                    651:         nextbp = 1; nextbplevel = -1;
                    652: #endif
                    653:         debug(PVSS?((VS)BDY(PVSS))->usrf->f.usrf->body:0);
                    654:          restore_handler();
                    655:          return;
                    656:       case 'c':
                    657:         restore_handler();
                    658:         return; break;
                    659:       case 'w':
                    660:         showpos();
                    661:         break;
                    662:       case '?':
                    663:         fprintf(stderr, "q:quit t:toplevel c:continue d:debug u:call registered handler w:where\n");
                    664: #if defined(__MINGW32__)
                    665:         fflush(stderr);
                    666: #endif
                    667:         break;
                    668:       default:
                    669:         break;
                    670:     }
                    671:   }
                    672: }
                    673:
                    674: void restore_handler() {
                    675: #if defined(VISUAL) || defined(__MINGW32__)
                    676:   resume_timer();
                    677: #endif
                    678: #if defined(SIGINT)
                    679:   set_signal(SIGINT,int_handler);
                    680: #endif
                    681: }
                    682:
                    683: void segv_handler(int sig)
                    684: {
                    685: #if defined(SIGSEGV)
                    686:   set_signal_for_restart(SIGSEGV,segv_handler);
                    687:   error("internal error (SEGV)");
                    688: #endif
                    689: }
                    690:
                    691: void ill_handler(int sig)
                    692: {
                    693: #if defined(SIGILL)
                    694:   set_signal_for_restart(SIGILL,ill_handler);
                    695:   error("illegal instruction (ILL)");
                    696: #endif
                    697: }
                    698:
                    699: #if defined(__DARWIN__)
                    700: #define SIGNAL_FOR_TIMER SIGALRM
                    701: #define ITIMER_TYPE ITIMER_REAL
                    702: #else
                    703: #define SIGNAL_FOR_TIMER SIGVTALRM
                    704: #define ITIMER_TYPE ITIMER_VIRTUAL
                    705: #endif
                    706:
                    707: void alrm_handler(int sig)
                    708: {
                    709:   fprintf(stderr,"interval timer expired (VTALRM)\n");
                    710: #if defined(__MINGW32__)
                    711:   fflush(stderr);
                    712: #endif
                    713:   LONGJMP(timer_env,1);
                    714: }
                    715:
                    716: void bus_handler(int sig)
                    717: {
                    718: #if defined(SIGBUS)
                    719:   set_signal_for_restart(SIGBUS,bus_handler);
                    720:   error("internal error (BUS ERROR)");
                    721: #endif
                    722: }
                    723:
                    724: void fpe_handler(int sig)
                    725: {
                    726: #if defined(SIGFPE)
                    727:   set_signal_for_restart(SIGFPE,fpe_handler);
                    728:   error("internal error (FPE)");
                    729: #endif
                    730: }
                    731:
                    732: void pipe_handler(int sig)
                    733: {
                    734: #if defined(SIGPIPE)
                    735:   set_signal_for_restart(SIGPIPE,pipe_handler);
                    736:   end_critical();
                    737:   error("internal error (BROKEN PIPE)");
                    738: #endif
                    739: }
                    740:
                    741: void resize_buffer()
                    742: {
                    743: }
                    744:
                    745: void tty_init() {
                    746: }
                    747:
                    748: void tty_reset() {
                    749: }
                    750:
                    751: extern int evalstatline;
                    752:
                    753: void set_lasterror(char *s)
                    754: {
                    755:   strncpy(LastError,s,BUFSIZ);
                    756:   LastError[BUFSIZ-1] = 0;
                    757: }
                    758:
                    759: SNODE error_snode;
                    760: int error_in_timer;
                    761:
                    762: void error(char *s)
                    763: {
                    764:   SNODE *snp=0;
                    765:
                    766: #if !defined(VISUAL) && !defined(__MINGW32__)
                    767:   if ( !error_in_timer && timer_is_set )
                    768:     alrm_handler(SIGNAL_FOR_TIMER);
                    769: #endif
                    770:   fprintf(stderr,"%s\n",s);
                    771: #if defined(__MINGW32__)
                    772:   fflush(stderr);
                    773: #endif
                    774:   set_lasterror(s);
                    775:   if ( CPVS != GPVS ) {
                    776:     if ( CPVS && CPVS->usrf && CPVS->usrf->f.usrf )
                    777:       searchsn(&BDY(CPVS->usrf->f.usrf),evalstatline,&snp);
                    778:     if ( snp )
                    779:       error_snode = *snp;
                    780:     else
                    781:       error_snode = 0;
                    782:   } else
                    783:     error_snode = 0;
                    784:   if ( do_file ) {
                    785:     char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
                    786:
                    787:     sprintf(errbuf,"%s\n",s);
                    788:     showpos_to_string(errbuf+strlen(errbuf));
                    789:     set_lasterror(errbuf);
                    790:     ExitAsir();
                    791:   }
                    792:   if ( debug_mode )
                    793:     LONGJMP(debug_env,1);
                    794:   if ( CPVS != GPVS )
                    795:     if ( !no_debug_on_error && (do_server_in_X11 || isatty(0)) )
                    796:       bp(error_snode);
                    797:   if ( I_am_server )
                    798:     showpos_to_list(&LastStackTrace);
                    799:   resetenv("return to toplevel");
                    800: }
                    801:
                    802: void goto_toplevel(char *s)
                    803: {
                    804:   SNODE *snp=0;
                    805:
                    806: #if !defined(VISUAL) && !defined(__MINGW32__)
                    807:   if ( timer_is_set )
                    808:     alrm_handler(SIGNAL_FOR_TIMER);
                    809: #endif
                    810:   fprintf(stderr,"%s\n",s);
                    811: #if defined(__MINGW32__)
                    812:   fflush(stderr);
                    813: #endif
                    814:   if ( do_file ) {
                    815:     char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
                    816:
                    817:     sprintf(errbuf,"%s\n",s);
                    818:     showpos_to_string(errbuf+strlen(errbuf));
                    819:     ExitAsir();
                    820:   }
                    821:   resetenv("return to toplevel");
                    822: }
                    823:
                    824: #if !defined(VISUAL) && !defined(__MINGW32__)
                    825: #include <sys/time.h>
                    826:
                    827: void set_timer(int interval)
                    828: {
                    829:   struct itimerval it;
                    830:
                    831:   it.it_interval.tv_sec = interval;
                    832:   it.it_interval.tv_usec = 0;
                    833:   it.it_value.tv_sec = interval;
                    834:   it.it_value.tv_usec = 0;
                    835:   setitimer(ITIMER_TYPE,&it,0);
                    836:   set_signal(SIGNAL_FOR_TIMER,alrm_handler);
                    837:   timer_is_set = 1;
                    838: }
                    839:
                    840: void reset_timer()
                    841: {
                    842:   struct itimerval it;
                    843:
                    844:   it.it_interval.tv_sec = 0;
                    845:   it.it_interval.tv_usec = 0;
                    846:   it.it_value.tv_sec = 0;
                    847:   it.it_value.tv_usec = 0;
                    848:   setitimer(ITIMER_TYPE,&it,0);
                    849:   set_signal(SIGNAL_FOR_TIMER,SIG_IGN);
                    850:   timer_is_set = 0;
                    851: }
                    852: #endif
                    853:
                    854: unsigned int get_asir_version();
                    855: char *get_asir_distribution();
                    856:
                    857: char *get_gcversion()
                    858: {
1.3       ohara     859:   return "GC 7.4.2 copyright 1988-2015, H-J. Boehm, A. J. Demers, Xerox, SGI, HP.\n";
1.1       noro      860: }
                    861:
                    862: char *get_pariversion()
                    863: {
                    864:   return "";
                    865: }
                    866:
                    867: char *get_intervalversion()
                    868: {
                    869: #if defined(INTERVAL)
                    870:   return " + Interval Arithmetic";
                    871: #else
                    872:   return "";
                    873: #endif
                    874: }
                    875:
                    876: void copyright()
                    877: {
                    878:   char *scopyright();
                    879:   fputs(scopyright(), stdout);
                    880: }
                    881:
                    882: char *scopyright()
                    883: {
                    884:   static char *notice;
                    885:   char *s1, *s2, *s3, *s4;
                    886:   int d, len;
1.3       ohara     887:   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      888:   if (!notice) {
                    889:     s1 = get_intervalversion();
                    890:     s2 = get_asir_distribution();
                    891:     s3 = get_gcversion();
                    892:     s4 = get_pariversion();
                    893:     d  = get_asir_version();
                    894:     len = (strlen(format)-8)+strlen(s1)+strlen(s2)+strlen(s3)+strlen(s4)+sizeof(int)*3;
                    895:     notice = MALLOC(len+1);
                    896:     sprintf(notice, format, s1, d, s2, s3, s4);
                    897:   }
                    898:   return notice;
                    899: }
                    900:
                    901: #if defined(VISUAL) || defined(__MINGW32__)
                    902: int recv_intr;
                    903:
                    904: static CRITICAL_SECTION signal_cs;
                    905: static int initialized_signal_cs;
                    906: static int signal_cs_count;
                    907:
                    908: static void init_signal_cs()
                    909: {
                    910:     if (!initialized_signal_cs) {
                    911:         InitializeCriticalSection(&signal_cs);
                    912:     initialized_signal_cs=1;
                    913:         signal_cs_count=0;
                    914:     }
                    915: }
                    916:
                    917: void try_enter_signal_cs()
                    918: {
                    919:   init_signal_cs();
                    920:     if(TryEnterCriticalSection(&signal_cs)) {
                    921:         signal_cs_count++;
                    922:     }
                    923: }
                    924:
                    925: void enter_signal_cs()
                    926: {
                    927:     init_signal_cs();
                    928:     EnterCriticalSection(&signal_cs);
                    929:     signal_cs_count++;
                    930: }
                    931:
                    932: void leave_signal_cs()
                    933: {
                    934:   init_signal_cs();
                    935:     if(signal_cs_count>0) {
                    936:         LeaveCriticalSection(&signal_cs);
                    937:         signal_cs_count--;
                    938:     }
                    939: }
                    940:
                    941: void leave_signal_cs_all()
                    942: {
                    943:     if (!initialized_signal_cs) {
                    944:         init_signal_cs();
                    945:     }
                    946:     while(signal_cs_count>0) {
                    947:         LeaveCriticalSection(&signal_cs);
                    948:         signal_cs_count--;
                    949:     }
                    950: }
                    951:
                    952: void check_intr()
                    953: {
                    954:   extern int recv_intr;
                    955:   enter_signal_cs();
                    956:   if ( recv_intr ) {
                    957:     if ( recv_intr == 1 ) {
                    958:       recv_intr = 0;
                    959:       int_handler(SIGINT);
                    960:     } else {
                    961:       recv_intr = 0;
                    962:       ox_usr1_handler(0);
                    963:     }
                    964:   }
                    965:   leave_signal_cs_all();
                    966: }
                    967: #endif

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