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

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.47    ! fujimoto   48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/ctrl.c,v 1.46 2015/08/04 06:20:44 noro Exp $
1.8       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
1.42      ohara      52: #include <string.h>
1.47    ! fujimoto   53: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
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.1       noro      175:
                    176:        if ( !arg ) {
                    177:                *rp = 0;
                    178:                return;
                    179:        }
                    180:        key = BDY((STRING)ARG0(arg));
                    181:        if ( !strcmp(key,"adj") ) {
                    182:                /* special treatment is necessary for "adj" */
                    183:                if ( argc(arg) == 1 ) {
1.39      ohara     184:                        Risa_GC_get_adj(&nm,&dv);
                    185:                        UTON(dv,num);
                    186:                        UTON(nm,den);
1.1       noro      187:                } else {
                    188:                        c = (Q)ARG1(arg);
                    189:                        if ( !c )
                    190:                                error("ctrl : adj : invalid argument");
                    191:                        num = NM(c);
                    192:                        den = !DN(c)?ONEN:DN(c);
1.39      ohara     193:                        dv = BD(num)[0];
                    194:                        nm = BD(den)[0];
                    195:                        Risa_GC_set_adj(nm,dv);
1.1       noro      196:                }
                    197:                NDTOQ(num,den,1,*rp);
1.24      noro      198:                return;
                    199:        } else if ( !strcmp(key,"prompt") ) {
                    200:                /* special treatment is necessary for "prompt" */
                    201:                if ( argc(arg) == 1 ) {
1.26      takayama  202:                  if ((!do_quiet) && (user_defined_prompt == (Obj)0)) *rp=ONE;
                    203:           else *rp = (Q)user_defined_prompt;
1.24      noro      204:                } else {
                    205:                        c = (Q)ARG1(arg);
1.25      noro      206:                        if ( !c ) {
                    207:                                do_quiet = 1;
                    208:                                user_defined_prompt = 0;
                    209:                                *rp = 0;
                    210:                        } else if ( OID(c) == O_STR ) {
                    211:                                str = BDY((STRING)c);
                    212:                                for ( i = 0, n = 0; str[i]; i++ )
                    213:                                        if ( str[i] == '%' )
                    214:                                                n++;
                    215:                                if ( n >= 2 )
                    216:                                        error("ctrl : prompt : invalid prompt specification");
1.24      noro      217:                                do_quiet = 1;
                    218:                                user_defined_prompt = (Obj)c;
                    219:                                *rp = c;
                    220:                        } else if ( NUM(c) && RATN(c) && UNIQ(c) ) {
                    221:                                user_defined_prompt = 0;
                    222:                                do_quiet = 0;
                    223:                        } else {
                    224:                                error("ctrl : prompt : invalid argument");
                    225:                        }
                    226:                }
1.1       noro      227:                return;
1.41      ohara     228:        } else if ( !strcmp(key,"loadpath") ) {
                    229:                *rp = 0;
                    230:                if ( argc(arg) == 1 ) {
                    231:                        if( ASIRLOADPATH[0] ) {
                    232:                                for(i=0; ASIRLOADPATH[i]; i++) {
                    233:                                }
                    234:                                for(i--,p=NULL; i>=0; i--,p=node) {
                    235:                                        MKSTR(s,ASIRLOADPATH[i]);
                    236:                                        MKNODE(node,s,p);
                    237:                                }
                    238:                                MKLIST(list,node);
                    239:                                *rp = list;
                    240:                        }
                    241:                } else {
                    242:                        list = (LIST)ARG1(arg);
                    243:                        if ( OID(list) == O_LIST ) {
                    244:                                for(i=0,p=BDY(list); p; i++,p=NEXT(p)) {
                    245:                                        s=(STRING)BDY(p);
                    246:                                        if(!s || OID(s)!=O_STR) {
                    247:                                                error("ctrl : loadpath : invalid argument");
                    248:                                        }
                    249:                                }
                    250:                                if(i >= ASIRLOADPATH_LEN) {
                    251:                                        ASIRLOADPATH_LEN = i+1;
                    252:                                        ASIRLOADPATH = (char **)MALLOC(sizeof(char *)*ASIRLOADPATH_LEN);
                    253:                                }
                    254:                                for(i=0,p=BDY(list); p; i++,p=NEXT(p)) {
                    255:                                        ASIRLOADPATH[i] = (char *)BDY((STRING)BDY(p));
                    256:                                }
                    257:                                ASIRLOADPATH[i] = NULL;
                    258:                        }else {
                    259:                                error("ctrl : loadpath : invalid argument");
                    260:                        }
                    261:                }
                    262:                return;
1.1       noro      263:        }
                    264:        for ( i = 0; ctrls[i].key; i++ )
                    265:                if ( !strcmp(key,ctrls[i].key) )
                    266:                        break;
                    267:        if ( ctrls[i].key ) {
                    268:                if ( argc(arg) == 1 )
                    269:                        t = *ctrls[i].val;
                    270:                else
                    271:                        *ctrls[i].val = t = QTOS((Q)ARG1(arg));
                    272:                STOQ(t,*rp);
                    273:        } else {
                    274:                sprintf(buf,"ctrl : %s : no such key",key);
                    275:                error(buf);
                    276:        }
                    277: }
1.42      ohara     278:
                    279: void Psysinfo(LIST *rp)
                    280: {
                    281:     int i;
                    282:     NODE n,p;
1.44      ohara     283:     STRING s[7];
1.42      ohara     284:
                    285:     get_sysinfo();
                    286:     MKSTR(s[0],sysinfo.type);  MKSTR(s[1],sysinfo.kernel);   MKSTR(s[2],sysinfo.name);
                    287:     MKSTR(s[3],sysinfo.arch);  MKSTR(s[4],sysinfo.release);  MKSTR(s[5],sysinfo.full);
1.44      ohara     288:     MKSTR(s[6],sysinfo.lang);
                    289:     for(i=6,p=NULL; i>=0; i--,p=n) {
1.42      ohara     290:         MKNODE(n,s[i],p);
                    291:     }
                    292:     MKLIST(*rp,n);
                    293: }
                    294:
1.47    ! fujimoto  295: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__MINGW64__)
1.44      ohara     296:
                    297: static char *get_lang()
                    298: {
                    299:     char *c, *p, *q;
                    300:     c = setlocale(LC_ALL, NULL); /* saving current locale */
                    301:     p = setlocale(LC_ALL, "");
                    302:     q = (char *)MALLOC(strlen(p)+1);
                    303:     strcpy(q,p);
                    304:     setlocale(LC_ALL, c);        /* restoring current locale */
                    305:     return q;
                    306: }
                    307:
1.43      ohara     308: static char *myuname(char *option)
1.42      ohara     309: {
                    310:     char buf[BUFSIZ];
                    311:     char *s;
1.43      ohara     312:     int fd[2], status, pid;
1.42      ohara     313:     *buf = 0;
                    314:     if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
1.43      ohara     315:         *buf = 0; return NULL;
1.42      ohara     316:     }
1.43      ohara     317:     pid = fork();
                    318:     if (pid < 0) {
                    319:         return NULL;
                    320:     }else if (pid == 0) {
1.42      ohara     321:         dup2(fd[1], 1);
1.43      ohara     322:         close(2);
1.42      ohara     323:         execlp("uname", "uname", option, NULL);
                    324:     }
1.43      ohara     325:     waitpid(pid, &status, 0);
                    326:     if (status) { /* error */
                    327:         return NULL;
                    328:     }
1.42      ohara     329:     s = buf;
                    330:     if( !read(fd[0], s, BUFSIZ-1) || (s = strchr(s, '\n')) ) {
                    331:         *s = 0;
                    332:     }
                    333:     close(fd[0]);
                    334:     close(fd[1]);
                    335:     s = (char *)MALLOC(strlen(buf)+1);
                    336:     strcpy(s, buf);
                    337:     return s;
                    338: }
                    339:
                    340: static void get_sysinfo()
                    341: {
1.43      ohara     342:     static struct utsname u;
1.42      ohara     343:     static int initialized = 0;
                    344:     if (initialized) {
                    345:         return;
                    346:     }
                    347:     initialized = 1;
1.43      ohara     348:     uname(&u);
                    349:     sysinfo.kernel = u.sysname;
1.42      ohara     350: #if defined(__DARWIN__)
                    351:     sysinfo.type   = "macosx";
                    352:     sysinfo.name   = sysinfo.kernel;
                    353: #else
                    354:     sysinfo.type   = "unix";
1.43      ohara     355:     sysinfo.name   = myuname("-o"); // not work on Darwin
                    356:     if (!sysinfo.name) {
                    357:         sysinfo.name = sysinfo.kernel;
                    358:     }
1.42      ohara     359: #endif
1.43      ohara     360:     sysinfo.arch   = u.machine;
                    361:     sysinfo.release= u.release;
                    362:     sysinfo.full   = myuname("-a");
1.44      ohara     363:     sysinfo.lang   = get_lang();
1.42      ohara     364: }
                    365:
                    366: #else
                    367:
                    368: /* http://msdn.microsoft.com/ja-jp/library/windows/desktop/ms724834%28v=vs.85%29.aspx */
                    369: static char *osnameNT(int major, int minor)
                    370: {
                    371:     if ((major == 3 && minor == 51) || (major == 4 && minor == 0)) {
                    372:         return "WindowsNT";
                    373:     }else if (major == 5 && minor == 0) {
                    374:         return "Windows2000";
                    375:     }else if (major == 5 && minor == 1) {
                    376:         return "WindowsXP";
                    377:     }else if (major == 5 && minor == 2) {
                    378:         return "Windows2003 Server";
                    379:     }else if (major == 6 && minor == 0) {
                    380:         return "WindowsVista";
                    381:     }else if (major == 6 && minor == 1) {
                    382:         return "Windows7";
                    383:     }else if (major == 6 && minor == 2) {
                    384:         return "Windows8";
                    385:     }else if (major == 6 && minor == 3) {
                    386:         return "Windows8.1";
                    387:     }
                    388:     return "unknown";
                    389: }
                    390:
                    391: static char *osname95(int major, int minor)
                    392: {
                    393:     if (major == 4 && minor == 0) {
                    394:         return "Windows95";
                    395:     }else if (major == 4 && minor == 10) {
                    396:         return "Windows98";
                    397:     }else if (major == 4 && minor == 90) {
                    398:         return "WindowsMe";
                    399:     }
                    400:     return "unknown";
                    401: }
                    402:
1.44      ohara     403: static char *get_lang()
                    404: {
                    405:     char lang[BUFSIZ];
                    406:     char *s;
                    407:     if(GetLocaleInfo(GetUserDefaultLCID(), LOCALE_SISO639LANGNAME, lang, BUFSIZ)) {
                    408:         s = (char *)MALLOC(strlen(lang)+1);
                    409:         strcpy(s,lang);
                    410:         return s;
                    411:     }
                    412:     return "en"; // English
                    413: }
                    414:
1.42      ohara     415: static void get_sysinfo()
                    416: {
                    417:     int arch64 = 0;
                    418:     char buf[BUFSIZ];
                    419:     OSVERSIONINFO v;
                    420:     char *s;
                    421:     static int initialized = 0;
                    422:
                    423:     if (initialized) {
                    424:         return;
                    425:     }
                    426:     initialized = 1;
                    427:
                    428:     v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
                    429:     GetVersionEx(&v);
                    430:
                    431:     sysinfo.type = "windows";
                    432:     sysinfo.arch = "x86";
                    433:     sprintf(buf, "%d.%d.%d", v.dwMajorVersion, v.dwMinorVersion, v.dwBuildNumber);
                    434:     s = (char *)MALLOC(strlen(buf)+1);
                    435:     strcpy(s, buf);
                    436:     sysinfo.release = s;
                    437:
                    438:     if (v.dwPlatformId == VER_PLATFORM_WIN32_NT) {
                    439:         sysinfo.kernel = "WindowsNT";
                    440:         sysinfo.name = osnameNT(v.dwMajorVersion, v.dwMinorVersion);
                    441:
                    442: #if defined(_WIN64)
                    443:         sysinfo.arch = "x86_64";
                    444: #else
                    445:         // IsWow64Process exists on WindowsXP SP2 or later
                    446:         if(GetProcAddress(GetModuleHandle("kernel32.dll"), "IsWow64Process")) {
                    447:             IsWow64Process(GetCurrentProcess(),&arch64);
                    448:             if(arch64) {
                    449:                 sysinfo.arch = "x86_64";
                    450:             }
                    451:         }
                    452: #endif
                    453:     }else { /* v.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS */
                    454:         sysinfo.kernel = "Windows95";
                    455:         sysinfo.name = osname95(v.dwMajorVersion, v.dwMinorVersion);
                    456:     }
                    457:     sprintf(buf, "%s %s %s %s %s", sysinfo.kernel, sysinfo.release, sysinfo.name, v.szCSDVersion, sysinfo.arch);
                    458:     s = (char *)MALLOC(strlen(buf)+1);
                    459:     strcpy(s, buf);
                    460:     sysinfo.full = s;
1.44      ohara     461:     sysinfo.lang = get_lang();
1.42      ohara     462: }
                    463:
                    464: #endif

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