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

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

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

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