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

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.77    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/parse/glob.c,v 1.76 2009/03/18 05:13:47 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;
                    170:        MKNODE(ONENODE,mkfnode(1,I_FORMULA,ONE),0);
                    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)
                    321:     if (access(cppname, X_OK&R_OK) != 0) {
                    322:         which("cpp", "/lib:/usr/ccs/lib:/usr/bin", cppname, BUFSIZ) ||
                    323:         which("cpp", getenv("PATH"), cppname, BUFSIZ);
                    324:     }
                    325: #endif
1.1       noro      326: }
                    327:
                    328: FILE *in_fp;
                    329:
1.24      noro      330: void process_args(int ac,char **av)
1.1       noro      331: {
1.75      ohara     332:        int nm,dv;
1.1       noro      333:        do_asirrc = 1;
1.33      noro      334: #if !defined(MPI)
1.1       noro      335:        do_message = 1;
                    336: #endif
1.38      ohara     337:        do_quiet = 0;
1.1       noro      338:        while ( ac > 0 ) {
                    339:                if ( !strcmp(*av,"-heap") && (ac >= 2) ) {
                    340:                        void GC_expand_hp(int);
                    341:
                    342:                        GC_expand_hp(atoi(*(av+1))); av += 2; ac -= 2;
                    343:                } else if ( !strcmp(*av,"-adj") && (ac >= 2) ) {
                    344:                        char *slash;
                    345:                        slash = strrchr(*(av+1),'/');
                    346:                        if ( slash ) {
                    347:                                *slash = 0;
1.75      ohara     348:                                nm = atoi(slash+1);
                    349:                        }else {
                    350:                                nm = 1;
1.1       noro      351:                        }
1.75      ohara     352:                        dv = atoi(*(av+1));
                    353:                        Risa_GC_set_adj(nm,dv);
1.1       noro      354:                        av += 2; ac -= 2;
                    355:                } else if ( !strcmp(*av,"-cpp") && (ac >= 2) ) {
                    356:                        strcpy(cppname,*(av+1)); av += 2; ac -= 2;
                    357:                } else if ( !strcmp(*av,"-f") && (ac >= 2) ) {
1.38      ohara     358:                        do_quiet = 1;
1.1       noro      359:                        in_fp = fopen(*(av+1),"r");
                    360:                        if ( !in_fp ) {
1.62      noro      361:                                fprintf(stderr,"%s does not exist!\n",*(av+1));
1.1       noro      362:                                asir_terminate(1);
                    363:                        }
                    364:                        do_file = 1;
1.42      ohara     365:                        do_filename = *(av+1);
1.1       noro      366:                        av += 2; ac -= 2;
1.42      ohara     367:                } else if ( !strcmp(*av,"-E") ) {
                    368:                        asir_setenv = 1; av++; ac--;
1.38      ohara     369:                } else if ( !strcmp(*av,"-quiet") ) {
1.39      noro      370:                        do_quiet = 1; av++; ac--;
1.1       noro      371:                } else if ( !strcmp(*av,"-norc") ) {
                    372:                        do_asirrc = 0; av++; ac--;
                    373:                } else if ( !strcmp(*av,"-nomessage") ) {
                    374:                        do_message = 0; av++; ac--;
1.11      noro      375:                } else if ( !strcmp(*av,"-rootdir") && (ac >= 2) ) {
                    376:                        set_rootdir(*(av+1)); av += 2; ac -= 2;
1.1       noro      377:                } else if ( !strcmp(*av,"-maxheap") && (ac >= 2) ) {
                    378:                        void GC_set_max_heap_size(int);
                    379:
                    380:                        GC_set_max_heap_size(atoi(*(av+1))); av += 2; ac -= 2;
                    381: #if !defined(VISUAL)
                    382:                } else if ( !strcmp(*av,"-display") && (ac >= 2) ) {
                    383:                        strcpy(displayname,*(av+1)); av += 2; ac -= 2;
                    384: #endif
1.32      noro      385: #if FEP
1.13      saito     386:                } else if ( !strcmp(*av,"-fep") ) {
                    387:                        do_fep = 1; av++; ac--;
                    388: #endif
1.31      ohara     389: #if defined(PARI)
1.1       noro      390:                } else if ( !strcmp(*av,"-paristack") ) {
                    391:                        extern int paristack;
                    392:
                    393:                        paristack = atoi(*(av+1)); av += 2; ac -= 2;
                    394: #endif
                    395:                } else {
                    396:                        fprintf(stderr,"%s : unknown option.\n",*av);
                    397:                        asir_terminate(1);
                    398:                }
                    399:        }
1.32      noro      400: #if FEP
1.13      saito     401:        if ( do_fep ) {
                    402:                char *home;
                    403:                home = (char *)getenv("HOME");
                    404:                if (!home)
                    405:                        home = ".";
                    406:                sprintf (asir_history, "%s/.asir_history",home);
                    407:                read_history(asir_history);
                    408:                using_history();
                    409:        }
                    410: #endif
1.1       noro      411: }
                    412:
                    413: #include <signal.h>
                    414:
                    415: void sig_init() {
                    416: #if !defined(VISUAL)
                    417:        signal(SIGINT,int_handler);
                    418: #else
1.24      noro      419:        void register_ctrlc_handler();
                    420:
1.1       noro      421:        register_ctrlc_handler();
                    422: #endif
                    423:        signal(SIGSEGV,segv_handler);
                    424:
1.23      noro      425: #if defined(SIGFPE)
1.1       noro      426:        signal(SIGFPE,fpe_handler);
                    427: #endif
                    428:
1.23      noro      429: #if defined(SIGPIPE)
1.1       noro      430:        signal(SIGPIPE,pipe_handler);
                    431: #endif
                    432:
1.23      noro      433: #if defined(SIGILL)
1.1       noro      434:        signal(SIGILL,ill_handler);
                    435: #endif
                    436:
                    437: #if !defined(VISUAL)
                    438:        signal(SIGBUS,bus_handler);
                    439: #endif
                    440: }
                    441:
                    442: static void (*old_int)(int);
                    443:
                    444: void asir_save_handler() {
                    445:        old_int = signal(SIGINT,SIG_IGN);
                    446:        signal(SIGINT,old_int);
                    447: }
                    448:
                    449: void asir_set_handler() {
                    450:        signal(SIGINT,int_handler);
                    451: }
                    452:
                    453: void asir_reset_handler() {
                    454:        signal(SIGINT,old_int);
                    455: }
                    456:
1.62      noro      457: extern int I_am_server;
                    458:
1.24      noro      459: void resetenv(char *s)
1.1       noro      460: {
                    461:        extern FILE *outfile;
                    462:
1.53      noro      463:        fprintf(stderr,"%s\n",s);
1.1       noro      464:        while ( NEXT(asir_infile) )
                    465:                closecurrentinput();
                    466:        resetpvs();
                    467: #if !defined(VISUAL)
                    468:        if ( do_server_in_X11 )
                    469: #endif
                    470:                show_debug_window(0);
                    471: #if defined(VISUAL_LIB)
                    472:        w_noflush_stderr(0);
                    473: #endif
                    474:        asir_out = stdout;
1.31      ohara     475: #if defined(PARI)
1.1       noro      476:        pari_outfile = stdout;
                    477: #endif
                    478:        /* restore states */
                    479:        reset_engine();
                    480:        reset_io();
1.16      noro      481: #if !defined(VISUAL)
1.15      noro      482:        reset_timer();
1.16      noro      483: #endif
1.62      noro      484:        LONGJMP(main_env,1);
1.1       noro      485: }
                    486:
1.24      noro      487: void fatal(int n)
1.1       noro      488: {
                    489:        resetenv("return to toplevel");
                    490: }
                    491:
                    492: extern int ox_int_received, critical_when_signal;
                    493:
1.24      noro      494: void int_handler(int sig)
1.1       noro      495: {
                    496:        extern NODE PVSS;
1.53      noro      497:        NODE t;
1.48      noro      498:
1.1       noro      499:
                    500:        if ( do_file ) {
                    501:                ExitAsir();
                    502:        }
                    503:        if ( critical_when_signal ) {
                    504:                ox_int_received = 1;
                    505:                return;
                    506:        }
1.12      noro      507: #if defined(VISUAL)
1.19      noro      508:        suspend_timer();
1.1       noro      509: #endif
                    510:        signal(SIGINT,SIG_IGN);
1.3       noro      511: #if !defined(VISUAL)
                    512:     if ( do_server_in_X11 ) {
                    513:                debug(PVSS?((VS)BDY(PVSS))->usrf->f.usrf->body:0);
                    514:                restore_handler();
                    515:                return;
                    516:        }
1.1       noro      517: #endif
                    518: #if defined(linux)
                    519: #if 1
                    520:        while ( stdin->_IO_read_ptr < stdin->_IO_read_end )
                    521: #else
                    522:        while ( stdin->_gptr < stdin->_egptr )
                    523: #endif
                    524:                getchar();
                    525: #endif
                    526:        while ( 1 ) {
                    527:                char buf[BUFSIZ];
                    528:                char c;
                    529:
1.53      noro      530:                fprintf(stderr,"interrupt ?(q/t/c/d/u/w/?) "); fflush(stderr);
1.1       noro      531:                buf[0] = '\n';
1.77    ! noro      532:                while ( !feof(stdin) && buf[0] == '\n' )
1.1       noro      533:                        fgets(buf,BUFSIZ,stdin);
1.77    ! noro      534:                if ( feof(stdin) ) {
        !           535:                        clearerr(stdin);
        !           536:                        continue;
        !           537:                }
1.1       noro      538:                switch ( c = buf[0] ) {
                    539:                        case 'q':
                    540:                                while ( 1 ) {
1.53      noro      541:                                        fprintf(stderr,"Abort this session? (y or n) "); fflush(stderr);
1.1       noro      542:                                        fgets(buf,BUFSIZ,stdin);
                    543:                                        if ( !strncmp(buf,"y",1) ) {
1.53      noro      544:                                                fprintf(stderr,"Bye\n");
1.65      noro      545:                                                /* for terminating myself */
                    546:                                                asir_infile = 0;
1.48      noro      547:                                                asir_terminate(1);
1.1       noro      548:                                        } else if ( !strncmp(buf,"n",1) ) {
                    549:                                                restore_handler();
                    550:                                                return;
                    551:                                        }
                    552:                                }
                    553:                                break;
                    554:                        case 't':
                    555:                        case 'u':
                    556:                                while ( 1 ) {
1.53      noro      557:                                        fprintf(stderr,"Abort this computation? (y or n) "); fflush(stderr);
1.1       noro      558:                                        fgets(buf,BUFSIZ,stdin);
                    559:                                        if ( !strncmp(buf,"y",1) )
                    560:                                                break;
                    561:                                        else if ( !strncmp(buf,"n",1) ) {
                    562:                                                restore_handler();
                    563:                                                return;
                    564:                                        }
                    565:                                }
                    566:                                if ( debug_mode )
                    567:                                        debug_mode = 0;
                    568:                                restore_handler();
                    569:                                if ( c == 'u' ) {
1.53      noro      570:                                        if ( user_int_handler ) {
1.58      noro      571:                                                if ( !do_terse )
                    572:                                                        fprintf(stderr,
                    573:                                                                "Calling the registered exception callbacks...");
1.53      noro      574:                                                for ( t = user_int_handler; t; t = NEXT(t) )
                    575:                                                        bevalf((FUNC)BDY(t),0);
1.58      noro      576:                                                if ( !do_terse )
                    577:                                                        fprintf(stderr, "done.\n");
1.1       noro      578:                                        }
                    579:                                }
1.62      noro      580:                                resetenv("return to toplevel");
1.1       noro      581:                                break;
                    582:                        case 'd':
                    583: #if 0
                    584:                                nextbp = 1; nextbplevel = -1;
                    585: #endif
                    586:                                debug(PVSS?((VS)BDY(PVSS))->usrf->f.usrf->body:0);
                    587:                                restore_handler();
                    588:                                return;
                    589:                        case 'c':
                    590:                                restore_handler();
                    591:                                return; break;
                    592:                        case 'w':
1.48      noro      593:                                showpos();
                    594:                                break;
1.1       noro      595:                        case '?':
1.53      noro      596:                                fprintf(stderr, "q:quit t:toplevel c:continue d:debug u:call registered handler w:where\n");
1.1       noro      597:                                break;
                    598:                        default:
                    599:                                break;
                    600:                }
                    601:        }
                    602: }
                    603:
                    604: void restore_handler() {
1.12      noro      605: #if defined(VISUAL)
1.19      noro      606:        resume_timer();
1.1       noro      607: #endif
1.19      noro      608: #if defined(SIGINT)
1.1       noro      609:        signal(SIGINT,int_handler);
                    610: #endif
                    611: }
                    612:
1.24      noro      613: void segv_handler(int sig)
1.1       noro      614: {
1.19      noro      615: #if defined(SIGSEGV)
1.1       noro      616:        signal(SIGSEGV,segv_handler);
1.19      noro      617:        error("internal error (SEGV)");
1.1       noro      618: #endif
                    619: }
                    620:
1.24      noro      621: void ill_handler(int sig)
1.1       noro      622: {
1.19      noro      623: #if defined(SIGILL)
1.1       noro      624:        signal(SIGILL,ill_handler);
1.19      noro      625:        error("illegal instruction (ILL)");
1.1       noro      626: #endif
                    627: }
                    628:
1.69      noro      629: #if defined(__DARWIN__)
                    630: #define SIGNAL_FOR_TIMER SIGALRM
                    631: #define ITIMER_TYPE ITIMER_REAL
                    632: #else
                    633: #define SIGNAL_FOR_TIMER SIGVTALRM
                    634: #define ITIMER_TYPE ITIMER_VIRTUAL
                    635: #endif
                    636:
1.24      noro      637: void alrm_handler(int sig)
1.1       noro      638: {
                    639:        fprintf(stderr,"interval timer expired (VTALRM)\n");
1.26      noro      640:        LONGJMP(timer_env,1);
1.1       noro      641: }
                    642:
1.24      noro      643: void bus_handler(int sig)
1.1       noro      644: {
                    645: #if defined(SIGBUS)
                    646:        signal(SIGBUS,bus_handler);
                    647:        error("internal error (BUS ERROR)");
                    648: #endif
                    649: }
                    650:
1.24      noro      651: void fpe_handler(int sig)
1.1       noro      652: {
1.19      noro      653: #if defined(SIGFPE)
1.1       noro      654:        signal(SIGFPE,fpe_handler);
1.19      noro      655:        error("internal error (FPE)");
1.1       noro      656: #endif
                    657: }
                    658:
1.24      noro      659: void pipe_handler(int sig)
1.1       noro      660: {
                    661: #if defined(SIGPIPE)
                    662:        signal(SIGPIPE,pipe_handler);
1.20      noro      663:        end_critical();
1.1       noro      664:        error("internal error (BROKEN PIPE)");
                    665: #endif
                    666: }
                    667:
                    668: void resize_buffer()
                    669: {
                    670: }
                    671:
                    672: void tty_init() {
                    673: }
                    674:
                    675: void tty_reset() {
                    676: }
                    677:
                    678: extern int evalstatline;
                    679:
1.24      noro      680: void set_lasterror(char *s)
1.1       noro      681: {
                    682:        strncpy(LastError,s,BUFSIZ);
                    683:        LastError[BUFSIZ-1] = 0;
                    684: }
                    685:
                    686: SNODE error_snode;
1.51      noro      687: int error_in_timer;
1.1       noro      688:
1.24      noro      689: void error(char *s)
1.1       noro      690: {
1.37      noro      691:        SNODE *snp=0;
1.1       noro      692:
1.16      noro      693: #if !defined(VISUAL)
1.51      noro      694:        if ( !error_in_timer && timer_is_set )
1.69      noro      695:                alrm_handler(SIGNAL_FOR_TIMER);
1.16      noro      696: #endif
1.1       noro      697:        fprintf(stderr,"%s\n",s);
                    698:        set_lasterror(s);
                    699:        if ( CPVS != GPVS ) {
1.37      noro      700:                if ( CPVS && CPVS->usrf && CPVS->usrf->f.usrf )
1.7       noro      701:                        searchsn(&BDY(CPVS->usrf->f.usrf),evalstatline,&snp);
1.6       noro      702:                if ( snp )
                    703:                        error_snode = *snp;
                    704:                else
                    705:                        error_snode = 0;
1.1       noro      706:        } else
                    707:                error_snode = 0;
                    708:        if ( do_file ) {
                    709:                char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
                    710:
                    711:                sprintf(errbuf,"%s\n",s);
                    712:                showpos_to_string(errbuf+strlen(errbuf));
                    713:                set_lasterror(errbuf);
                    714:                ExitAsir();
                    715:        }
                    716:        if ( debug_mode )
1.26      noro      717:                LONGJMP(debug_env,1);
1.1       noro      718:        if ( CPVS != GPVS )
1.52      noro      719:                if ( !no_debug_on_error && (do_server_in_X11 || isatty(0)) )
1.1       noro      720:                        bp(error_snode);
1.62      noro      721:        if ( I_am_server )
                    722:                showpos_to_list(&LastStackTrace);
1.40      noro      723:        resetenv("return to toplevel");
                    724: }
                    725:
                    726: void toplevel(char *s)
                    727: {
                    728:        SNODE *snp=0;
                    729:
                    730: #if !defined(VISUAL)
                    731:        if ( timer_is_set )
1.69      noro      732:                alrm_handler(SIGNAL_FOR_TIMER);
1.40      noro      733: #endif
                    734:        fprintf(stderr,"%s\n",s);
                    735:        if ( do_file ) {
                    736:                char errbuf[BUFSIZ*5]; /* sufficient to store stack information ? */
                    737:
                    738:                sprintf(errbuf,"%s\n",s);
                    739:                showpos_to_string(errbuf+strlen(errbuf));
                    740:                ExitAsir();
                    741:        }
1.1       noro      742:        resetenv("return to toplevel");
                    743: }
                    744:
                    745: #if !defined(VISUAL)
                    746: #include <sys/time.h>
                    747:
1.24      noro      748: void set_timer(int interval)
1.1       noro      749: {
                    750:        struct itimerval it;
                    751:
                    752:        it.it_interval.tv_sec = interval;
                    753:        it.it_interval.tv_usec = 0;
                    754:        it.it_value.tv_sec = interval;
                    755:        it.it_value.tv_usec = 0;
1.69      noro      756:        setitimer(ITIMER_TYPE,&it,0);
                    757:        signal(SIGNAL_FOR_TIMER,alrm_handler);
1.15      noro      758:        timer_is_set = 1;
1.1       noro      759: }
                    760:
                    761: void reset_timer()
                    762: {
                    763:        struct itimerval it;
                    764:
                    765:        it.it_interval.tv_sec = 0;
                    766:        it.it_interval.tv_usec = 0;
                    767:        it.it_value.tv_sec = 0;
                    768:        it.it_value.tv_usec = 0;
1.69      noro      769:        setitimer(ITIMER_TYPE,&it,0);
                    770:        signal(SIGNAL_FOR_TIMER,SIG_IGN);
1.15      noro      771:        timer_is_set = 0;
1.1       noro      772: }
                    773: #endif
                    774:
                    775: unsigned int get_asir_version();
1.14      noro      776: char *get_asir_distribution();
1.1       noro      777:
1.72      ohara     778: char *get_gcversion()
                    779: {
                    780: #if defined(GC7)
1.76      ohara     781:        return "GC 7.1 copyright 1988-2008, H-J. Boehm, A. J. Demers, Xerox, SGI, HP.\n";
1.72      ohara     782: #else
1.74      ohara     783:        return "GC 6.8 copyright 1988-2006, H-J. Boehm, A. J. Demers, Xerox, SGI, HP.\n";
1.72      ohara     784: #endif
                    785: }
                    786:
1.47      ohara     787: char *get_pariversion()
                    788: {
1.32      noro      789: #if PARI
                    790: #if 0
1.59      saito     791:        return PARIVERSION", Copyright (C) 2000-2005 The PARI Group.\n";
                    792: #else
                    793:        return "PARI 2.0.17, copyright 1989-1999, C. Batut, K. Belabas, D. Bernardi,\n   H. Cohen and M. Olivier.\n";
1.32      noro      794: #endif
                    795: #endif
1.47      ohara     796:        return "";
1.1       noro      797: }
1.14      noro      798:
1.47      ohara     799: char *get_intervalversion()
                    800: {
                    801: #if defined(INTERVAL)
                    802:        return " + Interval Arithmetic";
                    803: #else
                    804:        return "";
                    805: #endif
                    806: }
                    807:
                    808: void copyright()
                    809: {
1.72      ohara     810:        char *scopyright();
                    811:        fputs(scopyright(), stdout);
1.47      ohara     812: }
1.5       noro      813:
1.47      ohara     814: char *scopyright()
                    815: {
                    816:        static char *notice;
1.72      ohara     817:        char *s1, *s2, *s3, *s4;
1.47      ohara     818:        int d, len;
1.72      ohara     819:        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     820:        if (!notice) {
                    821:                s1 = get_intervalversion();
                    822:                s2 = get_asir_distribution();
1.72      ohara     823:                s3 = get_gcversion();
                    824:                s4 = get_pariversion();
1.47      ohara     825:                d  = get_asir_version();
1.72      ohara     826:                len = (strlen(format)-8)+strlen(s1)+strlen(s2)+strlen(s3)+strlen(s4)+sizeof(int)*3;
1.47      ohara     827:                notice = MALLOC(len+1);
1.72      ohara     828:                sprintf(notice, format, s1, d, s2, s3, s4);
1.47      ohara     829:        }
                    830:        return notice;
                    831: }

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