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

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

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