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

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

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