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

Annotation of OpenXM_contrib2/asir2000/builtin/ctrl.c, Revision 1.49

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.49    ! ohara      48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/ctrl.c,v 1.48 2015/08/14 13:51:54 fujimoto Exp $
1.8       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
1.42      ohara      52: #include <string.h>
1.48      fujimoto   53: #if defined(VISUAL) || defined(__MINGW32__)
1.42      ohara      54: #include <windows.h>
1.44      ohara      55: #include <winnls.h>
1.42      ohara      56: #else
1.44      ohara      57: #include <locale.h>
1.42      ohara      58: #include <unistd.h>
                     59: #include <sys/types.h>
                     60: #include <sys/socket.h>
                     61: #include <sys/wait.h>
1.43      ohara      62: #include <sys/utsname.h>
1.42      ohara      63: #endif
                     64:
                     65: static struct {
                     66:        char *type;
                     67:        char *kernel;
                     68:        char *name;
                     69:        char *arch;
                     70:        char *release;
                     71:        char *full;
1.44      ohara      72:        char *lang;
1.42      ohara      73: } sysinfo;
1.1       noro       74:
                     75: void Pctrl();
1.42      ohara      76: void Psysinfo(LIST *rp);
                     77: static void get_sysinfo();
1.1       noro       78:
                     79: struct ftab ctrl_tab[] = {
                     80:        {"ctrl",Pctrl,-2},
1.30      noro       81:        {"asir_env",Pctrl,-2},
1.42      ohara      82:        {"sysinfo", Psysinfo, 0},
1.1       noro       83:        {0,0,0},
                     84: };
                     85:
1.33      noro       86: extern int error_in_timer;
1.45      noro       87: extern int prtime,nez,echoback,bigfloat,evalef;
1.39      ohara      88: extern int debug_up;
1.1       noro       89: extern int GC_max_heap_size,Verbose,hideargs,hex_output,do_server_in_X11;
1.36      noro       90: extern int do_message,do_terse;
1.4       noro       91: extern int ox_batch,ox_check,ox_exchange_mathcap;
1.1       noro       92: extern int f4_nocheck;
1.16      saito      93: extern int StrassenSize;
1.22      saito      94: extern int outputstyle;
1.3       noro       95: extern int fortran_output;
1.13      noro       96: extern int real_digit;
1.27      noro       97: extern int real_binary;
1.31      noro       98: extern int allow_laurent;
1.12      saito      99: #if defined(INTERVAL)
                    100: extern int zerorewrite;
1.38      saito     101: extern int Itvplot;
1.12      saito     102: #endif
1.11      noro      103: extern int double_output;
1.14      noro      104: extern int use_new_hensel;
1.15      noro      105: extern int print_quote;
1.20      noro      106: extern int show_crossref;
1.24      noro      107: extern Obj user_defined_prompt;
1.28      ohara     108: extern int asir_setenv;
1.32      noro      109: extern int show_orderspec;
1.34      noro      110: extern int no_debug_on_error;
1.37      noro      111: extern int diag_period;
1.40      noro      112: extern int weight_check;
1.41      ohara     113: extern char **ASIRLOADPATH;
                    114: extern int ASIRLOADPATH_LEN;
1.1       noro      115:
                    116: static struct {
                    117:        char *key;
                    118:        int *val;
                    119: } ctrls[] = {
1.33      noro      120:        {"error_in_timer",&error_in_timer},
1.1       noro      121:        {"cputime",&prtime},
                    122:        {"nez",&nez},
                    123:        {"echo",&echoback},
                    124:        {"bigfloat",&bigfloat},
1.45      noro      125:        {"evalef",&evalef},
1.1       noro      126:        {"verbose",&Verbose},
1.23      ohara     127:        {"quiet_mode",&do_quiet},
1.1       noro      128:        {"hideargs",&hideargs},
                    129:        {"hex",&hex_output},
                    130:        {"debug_window",&do_server_in_X11},
                    131:        {"message",&do_message},
1.36      noro      132:        {"terse",&do_terse},
1.1       noro      133:        {"debug_up",&debug_up},
1.24      noro      134:        {"no_prompt",&do_quiet},
1.28      ohara     135:        {"asir_setenv",&asir_setenv},
1.1       noro      136:        {"ox_batch",&ox_batch},
                    137:        {"ox_check",&ox_check},
1.5       noro      138:        {"ox_exchange_mathcap",&ox_exchange_mathcap},
1.1       noro      139:        {"f4_nocheck",&f4_nocheck},
1.16      saito     140:        {"StrassenSize",&StrassenSize},
1.22      saito     141:        {"outputstyle",&outputstyle},
1.11      noro      142:        {"double_output",&double_output},
1.13      noro      143:        {"real_digit",&real_digit},
1.27      noro      144:        {"real_binary",&real_binary},
1.3       noro      145:        {"fortran_output",&fortran_output},
1.14      noro      146:        {"new_hensel",&use_new_hensel},
1.15      noro      147:        {"print_quote",&print_quote},
1.20      noro      148:        {"show_crossref",&show_crossref},
1.31      noro      149:        {"allow_laurent",&allow_laurent},
1.32      noro      150:        {"show_orderspec",&show_orderspec},
1.34      noro      151:        {"no_debug_on_error",&no_debug_on_error},
1.37      noro      152:        {"diag_period",&diag_period},
1.40      noro      153:        {"weight_check",&weight_check},
1.12      saito     154: #if defined(INTERVAL)
                    155:        {"zerorewrite",&zerorewrite},
1.38      saito     156:        {"itvplotsize",&Itvplot},
1.12      saito     157: #endif
1.1       noro      158:        {0,0},
                    159: };
                    160:
                    161: void Pctrl(arg,rp)
                    162: NODE arg;
                    163: Q *rp;
                    164: {
1.24      noro      165:        int t,i,n;
1.39      ohara     166:        int nm,dv;
1.1       noro      167:        N num,den;
                    168:        Q c;
                    169:        char *key;
                    170:        char buf[BUFSIZ];
1.24      noro      171:        char *str;
1.41      ohara     172:        STRING s;
                    173:        NODE node,p;
                    174:        LIST list;
1.49    ! ohara     175:        extern Q ox_pari_stream;
        !           176:        extern int ox_pari_stream_initialized;
1.1       noro      177:
                    178:        if ( !arg ) {
                    179:                *rp = 0;
                    180:                return;
                    181:        }
                    182:        key = BDY((STRING)ARG0(arg));
                    183:        if ( !strcmp(key,"adj") ) {
                    184:                /* special treatment is necessary for "adj" */
                    185:                if ( argc(arg) == 1 ) {
1.39      ohara     186:                        Risa_GC_get_adj(&nm,&dv);
                    187:                        UTON(dv,num);
                    188:                        UTON(nm,den);
1.1       noro      189:                } else {
                    190:                        c = (Q)ARG1(arg);
                    191:                        if ( !c )
                    192:                                error("ctrl : adj : invalid argument");
                    193:                        num = NM(c);
                    194:                        den = !DN(c)?ONEN:DN(c);
1.39      ohara     195:                        dv = BD(num)[0];
                    196:                        nm = BD(den)[0];
                    197:                        Risa_GC_set_adj(nm,dv);
1.1       noro      198:                }
                    199:                NDTOQ(num,den,1,*rp);
1.24      noro      200:                return;
                    201:        } else if ( !strcmp(key,"prompt") ) {
                    202:                /* special treatment is necessary for "prompt" */
                    203:                if ( argc(arg) == 1 ) {
1.26      takayama  204:                  if ((!do_quiet) && (user_defined_prompt == (Obj)0)) *rp=ONE;
                    205:           else *rp = (Q)user_defined_prompt;
1.24      noro      206:                } else {
                    207:                        c = (Q)ARG1(arg);
1.25      noro      208:                        if ( !c ) {
                    209:                                do_quiet = 1;
                    210:                                user_defined_prompt = 0;
                    211:                                *rp = 0;
                    212:                        } else if ( OID(c) == O_STR ) {
                    213:                                str = BDY((STRING)c);
                    214:                                for ( i = 0, n = 0; str[i]; i++ )
                    215:                                        if ( str[i] == '%' )
                    216:                                                n++;
                    217:                                if ( n >= 2 )
                    218:                                        error("ctrl : prompt : invalid prompt specification");
1.24      noro      219:                                do_quiet = 1;
                    220:                                user_defined_prompt = (Obj)c;
                    221:                                *rp = c;
                    222:                        } else if ( NUM(c) && RATN(c) && UNIQ(c) ) {
                    223:                                user_defined_prompt = 0;
                    224:                                do_quiet = 0;
                    225:                        } else {
                    226:                                error("ctrl : prompt : invalid argument");
                    227:                        }
                    228:                }
1.1       noro      229:                return;
1.41      ohara     230:        } else if ( !strcmp(key,"loadpath") ) {
                    231:                *rp = 0;
                    232:                if ( argc(arg) == 1 ) {
                    233:                        if( ASIRLOADPATH[0] ) {
                    234:                                for(i=0; ASIRLOADPATH[i]; i++) {
                    235:                                }
                    236:                                for(i--,p=NULL; i>=0; i--,p=node) {
                    237:                                        MKSTR(s,ASIRLOADPATH[i]);
                    238:                                        MKNODE(node,s,p);
                    239:                                }
                    240:                                MKLIST(list,node);
                    241:                                *rp = list;
                    242:                        }
                    243:                } else {
                    244:                        list = (LIST)ARG1(arg);
                    245:                        if ( OID(list) == O_LIST ) {
                    246:                                for(i=0,p=BDY(list); p; i++,p=NEXT(p)) {
                    247:                                        s=(STRING)BDY(p);
                    248:                                        if(!s || OID(s)!=O_STR) {
                    249:                                                error("ctrl : loadpath : invalid argument");
                    250:                                        }
                    251:                                }
                    252:                                if(i >= ASIRLOADPATH_LEN) {
                    253:                                        ASIRLOADPATH_LEN = i+1;
                    254:                                        ASIRLOADPATH = (char **)MALLOC(sizeof(char *)*ASIRLOADPATH_LEN);
                    255:                                }
                    256:                                for(i=0,p=BDY(list); p; i++,p=NEXT(p)) {
                    257:                                        ASIRLOADPATH[i] = (char *)BDY((STRING)BDY(p));
                    258:                                }
                    259:                                ASIRLOADPATH[i] = NULL;
                    260:                        }else {
                    261:                                error("ctrl : loadpath : invalid argument");
                    262:                        }
                    263:                }
                    264:                return;
1.49    ! ohara     265:        } else if ( !strcmp(key,"oxpari_id") ) {
        !           266:         *rp = 0;
        !           267:         if ( argc(arg) == 1 ) {
        !           268:             if(!ox_pari_stream_initialized) {
        !           269:                 t=-1;
        !           270:                 STOQ(t,*rp);
        !           271:             }else {
        !           272:                 *rp = ox_pari_stream;
        !           273:             }
        !           274:         }else {
        !           275:                        c = (Q)ARG1(arg);
        !           276:                        if ( !c || ( NUM(c) && INT(c) && SGN(c)>0 ) ) {
        !           277:                                ox_pari_stream_initialized = 1;
        !           278:                                ox_pari_stream = c;
        !           279:                                *rp = c;
        !           280:                        }else {
        !           281:                 t=-1;
        !           282:                 STOQ(t,*rp);
        !           283:                        }
        !           284:                }
        !           285:                return;
1.1       noro      286:        }
                    287:        for ( i = 0; ctrls[i].key; i++ )
                    288:                if ( !strcmp(key,ctrls[i].key) )
                    289:                        break;
                    290:        if ( ctrls[i].key ) {
                    291:                if ( argc(arg) == 1 )
                    292:                        t = *ctrls[i].val;
                    293:                else
                    294:                        *ctrls[i].val = t = QTOS((Q)ARG1(arg));
                    295:                STOQ(t,*rp);
                    296:        } else {
                    297:                sprintf(buf,"ctrl : %s : no such key",key);
                    298:                error(buf);
                    299:        }
                    300: }
1.42      ohara     301:
                    302: void Psysinfo(LIST *rp)
                    303: {
                    304:     int i;
                    305:     NODE n,p;
1.44      ohara     306:     STRING s[7];
1.42      ohara     307:
                    308:     get_sysinfo();
                    309:     MKSTR(s[0],sysinfo.type);  MKSTR(s[1],sysinfo.kernel);   MKSTR(s[2],sysinfo.name);
                    310:     MKSTR(s[3],sysinfo.arch);  MKSTR(s[4],sysinfo.release);  MKSTR(s[5],sysinfo.full);
1.44      ohara     311:     MKSTR(s[6],sysinfo.lang);
                    312:     for(i=6,p=NULL; i>=0; i--,p=n) {
1.42      ohara     313:         MKNODE(n,s[i],p);
                    314:     }
                    315:     MKLIST(*rp,n);
                    316: }
                    317:
1.48      fujimoto  318: #if !defined(VISUAL) && !defined(__MINGW32__)
1.44      ohara     319:
                    320: static char *get_lang()
                    321: {
                    322:     char *c, *p, *q;
                    323:     c = setlocale(LC_ALL, NULL); /* saving current locale */
                    324:     p = setlocale(LC_ALL, "");
                    325:     q = (char *)MALLOC(strlen(p)+1);
                    326:     strcpy(q,p);
                    327:     setlocale(LC_ALL, c);        /* restoring current locale */
                    328:     return q;
                    329: }
                    330:
1.43      ohara     331: static char *myuname(char *option)
1.42      ohara     332: {
                    333:     char buf[BUFSIZ];
                    334:     char *s;
1.43      ohara     335:     int fd[2], status, pid;
1.42      ohara     336:     *buf = 0;
                    337:     if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
1.43      ohara     338:         *buf = 0; return NULL;
1.42      ohara     339:     }
1.43      ohara     340:     pid = fork();
                    341:     if (pid < 0) {
                    342:         return NULL;
                    343:     }else if (pid == 0) {
1.42      ohara     344:         dup2(fd[1], 1);
1.43      ohara     345:         close(2);
1.42      ohara     346:         execlp("uname", "uname", option, NULL);
                    347:     }
1.43      ohara     348:     waitpid(pid, &status, 0);
                    349:     if (status) { /* error */
                    350:         return NULL;
                    351:     }
1.42      ohara     352:     s = buf;
                    353:     if( !read(fd[0], s, BUFSIZ-1) || (s = strchr(s, '\n')) ) {
                    354:         *s = 0;
                    355:     }
                    356:     close(fd[0]);
                    357:     close(fd[1]);
                    358:     s = (char *)MALLOC(strlen(buf)+1);
                    359:     strcpy(s, buf);
                    360:     return s;
                    361: }
                    362:
                    363: static void get_sysinfo()
                    364: {
1.43      ohara     365:     static struct utsname u;
1.42      ohara     366:     static int initialized = 0;
                    367:     if (initialized) {
                    368:         return;
                    369:     }
                    370:     initialized = 1;
1.43      ohara     371:     uname(&u);
                    372:     sysinfo.kernel = u.sysname;
1.42      ohara     373: #if defined(__DARWIN__)
                    374:     sysinfo.type   = "macosx";
                    375:     sysinfo.name   = sysinfo.kernel;
                    376: #else
                    377:     sysinfo.type   = "unix";
1.43      ohara     378:     sysinfo.name   = myuname("-o"); // not work on Darwin
                    379:     if (!sysinfo.name) {
                    380:         sysinfo.name = sysinfo.kernel;
                    381:     }
1.42      ohara     382: #endif
1.43      ohara     383:     sysinfo.arch   = u.machine;
                    384:     sysinfo.release= u.release;
                    385:     sysinfo.full   = myuname("-a");
1.44      ohara     386:     sysinfo.lang   = get_lang();
1.42      ohara     387: }
                    388:
                    389: #else
                    390:
                    391: /* http://msdn.microsoft.com/ja-jp/library/windows/desktop/ms724834%28v=vs.85%29.aspx */
                    392: static char *osnameNT(int major, int minor)
                    393: {
                    394:     if ((major == 3 && minor == 51) || (major == 4 && minor == 0)) {
                    395:         return "WindowsNT";
                    396:     }else if (major == 5 && minor == 0) {
                    397:         return "Windows2000";
                    398:     }else if (major == 5 && minor == 1) {
                    399:         return "WindowsXP";
                    400:     }else if (major == 5 && minor == 2) {
                    401:         return "Windows2003 Server";
                    402:     }else if (major == 6 && minor == 0) {
                    403:         return "WindowsVista";
                    404:     }else if (major == 6 && minor == 1) {
                    405:         return "Windows7";
                    406:     }else if (major == 6 && minor == 2) {
                    407:         return "Windows8";
                    408:     }else if (major == 6 && minor == 3) {
                    409:         return "Windows8.1";
                    410:     }
                    411:     return "unknown";
                    412: }
                    413:
                    414: static char *osname95(int major, int minor)
                    415: {
                    416:     if (major == 4 && minor == 0) {
                    417:         return "Windows95";
                    418:     }else if (major == 4 && minor == 10) {
                    419:         return "Windows98";
                    420:     }else if (major == 4 && minor == 90) {
                    421:         return "WindowsMe";
                    422:     }
                    423:     return "unknown";
                    424: }
                    425:
1.44      ohara     426: static char *get_lang()
                    427: {
                    428:     char lang[BUFSIZ];
                    429:     char *s;
                    430:     if(GetLocaleInfo(GetUserDefaultLCID(), LOCALE_SISO639LANGNAME, lang, BUFSIZ)) {
                    431:         s = (char *)MALLOC(strlen(lang)+1);
                    432:         strcpy(s,lang);
                    433:         return s;
                    434:     }
                    435:     return "en"; // English
                    436: }
                    437:
1.42      ohara     438: static void get_sysinfo()
                    439: {
                    440:     int arch64 = 0;
                    441:     char buf[BUFSIZ];
                    442:     OSVERSIONINFO v;
                    443:     char *s;
                    444:     static int initialized = 0;
                    445:
                    446:     if (initialized) {
                    447:         return;
                    448:     }
                    449:     initialized = 1;
                    450:
                    451:     v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
                    452:     GetVersionEx(&v);
                    453:
                    454:     sysinfo.type = "windows";
                    455:     sysinfo.arch = "x86";
                    456:     sprintf(buf, "%d.%d.%d", v.dwMajorVersion, v.dwMinorVersion, v.dwBuildNumber);
                    457:     s = (char *)MALLOC(strlen(buf)+1);
                    458:     strcpy(s, buf);
                    459:     sysinfo.release = s;
                    460:
                    461:     if (v.dwPlatformId == VER_PLATFORM_WIN32_NT) {
                    462:         sysinfo.kernel = "WindowsNT";
                    463:         sysinfo.name = osnameNT(v.dwMajorVersion, v.dwMinorVersion);
                    464:
                    465: #if defined(_WIN64)
                    466:         sysinfo.arch = "x86_64";
                    467: #else
                    468:         // IsWow64Process exists on WindowsXP SP2 or later
                    469:         if(GetProcAddress(GetModuleHandle("kernel32.dll"), "IsWow64Process")) {
                    470:             IsWow64Process(GetCurrentProcess(),&arch64);
                    471:             if(arch64) {
                    472:                 sysinfo.arch = "x86_64";
                    473:             }
                    474:         }
                    475: #endif
                    476:     }else { /* v.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS */
                    477:         sysinfo.kernel = "Windows95";
                    478:         sysinfo.name = osname95(v.dwMajorVersion, v.dwMinorVersion);
                    479:     }
                    480:     sprintf(buf, "%s %s %s %s %s", sysinfo.kernel, sysinfo.release, sysinfo.name, v.szCSDVersion, sysinfo.arch);
                    481:     s = (char *)MALLOC(strlen(buf)+1);
                    482:     strcpy(s, buf);
                    483:     sysinfo.full = s;
1.44      ohara     484:     sysinfo.lang = get_lang();
1.42      ohara     485: }
                    486:
                    487: #endif

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