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

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.60    ! ohara      48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/ctrl.c,v 1.59 2018/03/29 01:32:50 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.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 {
1.59      noro       66:   char *type;
                     67:   char *kernel;
                     68:   char *name;
                     69:   char *arch;
                     70:   char *release;
                     71:   char *full;
                     72:   char *lang;
1.60    ! ohara      73: #if defined(VISUAL)
        !            74:   int ver_major;
        !            75:   int ver_minor;
        !            76:   int ver_build;
        !            77: #endif
1.42      ohara      78: } sysinfo;
1.1       noro       79:
                     80: void Pctrl();
1.42      ohara      81: void Psysinfo(LIST *rp);
                     82: static void get_sysinfo();
1.1       noro       83:
                     84: struct ftab ctrl_tab[] = {
1.59      noro       85:   {"ctrl",Pctrl,-2},
                     86:   {"asir_env",Pctrl,-2},
                     87:   {"sysinfo", Psysinfo, 0},
                     88:   {0,0,0},
1.1       noro       89: };
                     90:
1.33      noro       91: extern int error_in_timer;
1.45      noro       92: extern int prtime,nez,echoback,bigfloat,evalef;
1.39      ohara      93: extern int debug_up;
1.1       noro       94: extern int GC_max_heap_size,Verbose,hideargs,hex_output,do_server_in_X11;
1.36      noro       95: extern int do_message,do_terse;
1.4       noro       96: extern int ox_batch,ox_check,ox_exchange_mathcap;
1.1       noro       97: extern int f4_nocheck;
1.16      saito      98: extern int StrassenSize;
1.22      saito      99: extern int outputstyle;
1.3       noro      100: extern int fortran_output;
1.13      noro      101: extern int real_digit;
1.27      noro      102: extern int real_binary;
1.31      noro      103: extern int allow_laurent;
1.12      saito     104: #if defined(INTERVAL)
                    105: extern int zerorewrite;
1.38      saito     106: extern int Itvplot;
1.12      saito     107: #endif
1.11      noro      108: extern int double_output;
1.14      noro      109: extern int use_new_hensel;
1.15      noro      110: extern int print_quote;
1.20      noro      111: extern int show_crossref;
1.24      noro      112: extern Obj user_defined_prompt;
1.28      ohara     113: extern int asir_setenv;
1.32      noro      114: extern int show_orderspec;
1.34      noro      115: extern int no_debug_on_error;
1.37      noro      116: extern int diag_period;
1.40      noro      117: extern int weight_check;
1.41      ohara     118: extern char **ASIRLOADPATH;
                    119: extern int ASIRLOADPATH_LEN;
1.51      noro      120: extern int No_ox_reset;
1.54      noro      121: extern int plot_by_bigfloat;
                    122: extern int debug_plot;
1.1       noro      123:
1.56      noro      124: static struct keyval {
1.59      noro      125:   char *key;
                    126:   int *val;
                    127:   char *desc;
1.1       noro      128: } ctrls[] = {
1.59      noro      129:   {"StrassenSize",&StrassenSize,"Determines the parameter in Strassen-Winograd matrix multiplication algorithm."},
                    130:   {"allow_laurent",&allow_laurent,"If set to 1, negative exponents are allowed in monomials."},
                    131:   {"asir_setenv",&asir_setenv,"Not used."},
                    132:   {"bigfloat",&bigfloat,"If set to 1, MPFR bigfloat functions are used for floating point evaluation." },
                    133:   {"cputime",&prtime,"If set to 1, the CPU time at the toplevel is shown." },
                    134:   {"debug_up",&debug_up,"If set to 1, some debug messages are printed in special functions for univariate polynomials."},
                    135:   {"debug_window",&do_server_in_X11,"If set to 1, an input window for debugging remote server are shown."},
                    136:   {"diag_period",&diag_period,"Determines the frequency of the intermediate inter-reduction in nd_gr()."},
                    137:   {"double_output",&double_output,"If set to 1, floating point numbers are printed in the style ddd.ddd."},
                    138:   {"echo",&echoback,"If set to 1, characters read by the input function are printed."  },
                    139:   {"error_in_timer",&error_in_timer,"If set to 1, the usual error handler is executed when the timer is expired."},
                    140:   {"evalef",&evalef,"If set to 1, elementary functions are evaluated immediately."},
                    141:   {"f4_nocheck",&f4_nocheck,"If set to 1, correctness check of the result of modular computation are omitted in nd_f4()."},
                    142:   {"fake_ox_reset",&No_ox_reset,"Determines the treatment of OX reset request for a server which does not implement the reset protocol."},
                    143:   {"fortran_output",&fortran_output,"If set to 1, ** is used instead of ^ for representing the power."},
                    144:   {"hex",&hex_output,"If set to 1, integers are printed by the hexadecimal notation."},
                    145:   {"hideargs",&hideargs,"If set to 1, the arguments of a function call are not printed."},
                    146:   {"message",&do_message,"If set to 1, an opening message is printed in ox_asir and ox_plot."},
                    147:   {"new_hensel",&use_new_hensel,"If set to 1, a function hensel2() is used in the univariate factorizer over Q."},
                    148:   {"nez",&nez,"If set to 1, a new version of EZGCD implementation is used."  },
                    149:   {"no_debug_on_error",&no_debug_on_error,"If set to 1, the debug mode is not used."},
                    150:   {"no_ox_reset",&No_ox_reset,"Determines the treatment of OX reset request for a server which does not implement the reset protocol."},
                    151:   {"no_prompt",&do_quiet,"If set to 1, prompts are not shown."},
                    152:   {"outputstyle",&outputstyle,"If set to 1, structured data such as matrices and vectors are printed in the style mat(...), vect(...)."},
                    153:   {"ox_batch",&ox_batch,"If set to 1, the OX stream are not flushed at each sending of an OX data."},
                    154:   {"ox_check",&ox_check,"If set to 1, mathcap check is done for OpenXM date communication."},
                    155:   {"ox_exchange_mathcap",&ox_exchange_mathcap,"If set to 1, mathcaps are exchanged."},
                    156:   {"print_quote",&print_quote,"Determines the behavior of the printed form of a quote."},
                    157:   {"quiet_mode",&do_quiet,"If set to 1, the copyright notices are not printed at the beginning of the session."},
                    158:   {"real_binary",&real_binary,"If set to 1, a floating point number is printed by the binary notation."},
                    159:   {"real_digit",&real_digit,"Determines the number of digits to appear after the decimal point."},
                    160:   {"show_crossref",&show_crossref,"If set to 1, cross-references are shown when a program file is loaded."},
                    161:   {"show_orderspec",&show_orderspec,"If set to 1, the specification of a composite term order is printed upon its creation."},
                    162:   {"terse",&do_terse,"If set to 1, messages are not printed when user-defined callbacks are executed."},
                    163:   {"verbose",&Verbose,"If set to 1, a warning is printed if a function is redefined."},
                    164:   {"weight_check",&weight_check,"If set to 1, an overflow check for the given weight vector is done before starting the Groebner basis computation."},
1.55      noro      165: #if defined(DO_PLOT)
1.59      noro      166:   {"plot_by_bigfloat",&plot_by_bigfloat,"If set to 1, computation is done by using MPFR bigfloat functions in ox_plot."},
                    167:   {"debug_plot",&debug_plot,"If set to 1, ox_plot is executed with the message window."},
1.55      noro      168: #endif
1.12      saito     169: #if defined(INTERVAL)
1.59      noro      170:   {"zerorewrite",&zerorewrite,""},
                    171:   {"itvplotsize",&Itvplot,""},
1.12      saito     172: #endif
1.59      noro      173:   {0,0},
1.1       noro      174: };
                    175:
1.58      noro      176: LIST create_control_value(char *keystr,Obj value,char *descstr,int withdesc)
                    177: {
                    178:   STRING key,desc;
                    179:   NODE nd;
                    180:   LIST list;
                    181:
                    182:   MKSTR(key,keystr);
                    183:   if ( withdesc ) {
                    184:     MKSTR(desc,descstr);
                    185:     nd = mknode(3,key,value,desc);
                    186:   } else nd = mknode(2,key,value);
                    187:   MKLIST(list,nd);
                    188:   return list;
                    189: }
                    190:
                    191: extern Q ox_pari_stream;
                    192: extern int ox_pari_stream_initialized;
                    193: extern P ox_pari_starting_function;
                    194:
1.57      noro      195: LIST create_control_values(int withdesc)
1.56      noro      196: {
                    197:   int n,i;
1.58      noro      198:   NODE top,top1,nd,node,p;
                    199:   LIST list,l;
                    200:   STRING s;
                    201:   char *descstr;
                    202:   Q val,adj;
                    203:   int nm,dv;
                    204:   N num,den;
1.56      noro      205:
                    206:   n = sizeof(ctrls)/sizeof(struct keyval)-1;
                    207:   top = 0;
                    208:   for ( i = n-1; i >= 0; i-- ) {
                    209:     STOQ(*(ctrls[i].val),val);
1.58      noro      210:     list = create_control_value(ctrls[i].key,val,ctrls[i].desc,withdesc);
                    211:     MKNODE(top1,list,top); top = top1;
1.56      noro      212:   }
1.58      noro      213: /* adj */
                    214:   Risa_GC_get_adj(&nm,&dv); UTON(nm,num); UTON(dv,den); NDTOQ(num,den,1,adj);
                    215:   descstr = "Determines the parameter for Boehm's GC.";
                    216:   list = create_control_value("adj",adj,descstr,withdesc);
                    217:   MKNODE(top1,list,top); top = top1;
                    218:
                    219: /* prompt */
                    220:   descstr = "Determines the user-defined prompt.";
                    221:   list = create_control_value("prompt",user_defined_prompt,descstr,withdesc);
                    222:   MKNODE(top1,list,top); top = top1;
                    223:
                    224: /* loadpath */
                    225:   node = 0;
                    226:   if( ASIRLOADPATH[0] ) {
                    227:     for(i=0; ASIRLOADPATH[i]; i++);
                    228:     for(i--,p=NULL; i>=0; i--,p=node) {
                    229:       MKSTR(s,ASIRLOADPATH[i]);
                    230:       MKNODE(node,s,p);
                    231:     }
                    232:   }
                    233:   MKLIST(l,node);
                    234:   descstr = "List of paths in ASIRLOADPATHt.";
                    235:   list = create_control_value("loadpath",l,descstr,withdesc);
                    236:   MKNODE(top1,list,top); top = top1;
                    237:
                    238: /* oxpari_id */
                    239:   if(!ox_pari_stream_initialized) {
                    240:     STOQ(-1,val);
                    241:   } else val = ox_pari_stream;
                    242:   descstr = "Id of ox_pari.";
                    243:   list = create_control_value("oxpari_id",val,descstr,withdesc);
                    244:   MKNODE(top1,list,top); top = top1;
                    245:
                    246: /* oxpari_start */
                    247:   val = (Q)ox_pari_starting_function;
                    248:   descstr = "oxpari starting function.";
                    249:   list = create_control_value("oxpari_start",val,descstr,withdesc);
                    250:   MKNODE(top1,list,top); top = top1;
                    251:
1.56      noro      252:   MKLIST(list,top);
                    253:   return list;
                    254: }
                    255:
1.53      noro      256: void Pctrl(NODE arg,Q *rp)
1.1       noro      257: {
1.59      noro      258:   int t,i,n,desc=0;
                    259:   int nm,dv;
                    260:   N num,den;
                    261:   Q c;
                    262:   char *key;
                    263:   char buf[BUFSIZ];
                    264:   char *str;
                    265:   STRING s;
                    266:   NODE node,p,opt;
                    267:   LIST list;
                    268:   P f;
                    269:   Obj value;
                    270:
                    271:   if ( !arg ) {
                    272:     if ( current_option ) {
                    273:       for ( opt = current_option; opt; opt = NEXT(opt) ) {
                    274:         p = BDY((LIST)BDY(opt));
                    275:         key = BDY((STRING)BDY(p));
                    276:         value = (Obj)BDY(NEXT(p));
                    277:         if ( !strcmp(key,"desc") && value ) {
                    278:           desc = value ? 1 : 0;
                    279:           break;
                    280:         }
                    281:       }
                    282:     }
                    283:     *rp = create_control_values(desc);
                    284:     return;
                    285:   }
                    286:   key = BDY((STRING)ARG0(arg));
                    287:   if ( !strcmp(key,"adj") ) {
                    288:     /* special treatment is necessary for "adj" */
                    289:     if ( argc(arg) == 1 ) {
                    290:       Risa_GC_get_adj(&nm,&dv);
                    291:       UTON(dv,num);
                    292:       UTON(nm,den);
                    293:     } else {
                    294:       c = (Q)ARG1(arg);
                    295:       if ( !c )
                    296:         error("ctrl : adj : invalid argument");
                    297:       num = NM(c);
                    298:       den = !DN(c)?ONEN:DN(c);
                    299:       dv = BD(num)[0];
                    300:       nm = BD(den)[0];
                    301:       Risa_GC_set_adj(nm,dv);
                    302:     }
                    303:     NDTOQ(num,den,1,*rp);
                    304:     return;
                    305:   } else if ( !strcmp(key,"prompt") ) {
                    306:     /* special treatment is necessary for "prompt" */
                    307:     if ( argc(arg) == 1 ) {
                    308:       if ((!do_quiet) && (user_defined_prompt == (Obj)0)) *rp=ONE;
1.26      takayama  309:           else *rp = (Q)user_defined_prompt;
1.59      noro      310:     } else {
                    311:       c = (Q)ARG1(arg);
                    312:       if ( !c ) {
                    313:         do_quiet = 1;
                    314:         user_defined_prompt = 0;
                    315:         *rp = 0;
                    316:       } else if ( OID(c) == O_STR ) {
                    317:         str = BDY((STRING)c);
                    318:          for ( i = 0, n = 0; str[i]; i++ )
                    319:           if ( str[i] == '%' )
                    320:             n++;
                    321:         if ( n >= 2 )
                    322:           error("ctrl : prompt : invalid prompt specification");
                    323:         do_quiet = 1;
                    324:         user_defined_prompt = (Obj)c;
                    325:         *rp = c;
                    326:       } else if ( NUM(c) && RATN(c) && UNIQ(c) ) {
                    327:         user_defined_prompt = 0;
                    328:         do_quiet = 0;
                    329:       } else {
                    330:         error("ctrl : prompt : invalid argument");
                    331:       }
                    332:     }
                    333:     return;
                    334:   } else if ( !strcmp(key,"loadpath") ) {
                    335:     *rp = 0;
                    336:     if ( argc(arg) == 1 ) {
                    337:       if( ASIRLOADPATH[0] ) {
                    338:         for(i=0; ASIRLOADPATH[i]; i++) {
                    339:         }
                    340:         for(i--,p=NULL; i>=0; i--,p=node) {
                    341:           MKSTR(s,ASIRLOADPATH[i]);
                    342:           MKNODE(node,s,p);
                    343:         }
                    344:         MKLIST(list,node);
                    345:         *rp = (Q)list;
                    346:       }
                    347:     } else {
                    348:       list = (LIST)ARG1(arg);
                    349:       if ( OID(list) == O_LIST ) {
                    350:         for(i=0,p=BDY(list); p; i++,p=NEXT(p)) {
                    351:           s=(STRING)BDY(p);
                    352:           if(!s || OID(s)!=O_STR) {
                    353:             error("ctrl : loadpath : invalid argument");
                    354:           }
                    355:         }
                    356:         if(i >= ASIRLOADPATH_LEN) {
                    357:           ASIRLOADPATH_LEN = i+1;
                    358:           ASIRLOADPATH = (char **)MALLOC(sizeof(char *)*ASIRLOADPATH_LEN);
                    359:         }
                    360:         for(i=0,p=BDY(list); p; i++,p=NEXT(p)) {
                    361:           ASIRLOADPATH[i] = (char *)BDY((STRING)BDY(p));
                    362:         }
                    363:         ASIRLOADPATH[i] = NULL;
                    364:       }else {
                    365:         error("ctrl : loadpath : invalid argument");
                    366:       }
                    367:     }
                    368:     return;
                    369:   } else if ( !strcmp(key,"oxpari_id") ) {
1.49      ohara     370:         if ( argc(arg) == 1 ) {
                    371:             if(!ox_pari_stream_initialized) {
                    372:                 t=-1;
                    373:                 STOQ(t,*rp);
                    374:             }else {
                    375:                 *rp = ox_pari_stream;
                    376:             }
                    377:         }else {
1.59      noro      378:       c = (Q)ARG1(arg);
                    379:       if ( !c || ( NUM(c) && INT(c) && SGN(c)>0 ) ) {
                    380:         ox_pari_stream_initialized = 1;
                    381:         ox_pari_stream = c;
                    382:         *rp = c;
                    383:       }else {
1.49      ohara     384:                 t=-1;
                    385:                 STOQ(t,*rp);
1.59      noro      386:       }
                    387:     }
                    388:     return;
                    389:   } else if ( !strcmp(key,"oxpari_start") ) {
1.50      ohara     390:         if ( argc(arg) == 1 ) {
1.59      noro      391:       *rp = (Q)ox_pari_starting_function;
                    392:     } else {
                    393:       f = (P)ARG1(arg);
                    394:       if ( !f || OID(f) == O_P) {
                    395:         ox_pari_starting_function = f;
                    396:         *rp = (Q)f;
                    397:       }else {
                    398:         *rp = 0;
                    399:       }
                    400:     }
                    401:     return;
                    402:   }
                    403:   for ( i = 0; ctrls[i].key; i++ )
                    404:     if ( !strcmp(key,ctrls[i].key) )
                    405:       break;
                    406:   if ( ctrls[i].key ) {
                    407:     if ( argc(arg) == 1 )
                    408:       t = *ctrls[i].val;
                    409:     else
                    410:       *ctrls[i].val = t = QTOS((Q)ARG1(arg));
                    411:     STOQ(t,*rp);
                    412:   } else {
                    413:     sprintf(buf,"ctrl : %s : no such key",key);
                    414:     error(buf);
                    415:   }
1.1       noro      416: }
1.42      ohara     417:
                    418: void Psysinfo(LIST *rp)
                    419: {
                    420:     int i;
                    421:     NODE n,p;
1.44      ohara     422:     STRING s[7];
1.42      ohara     423:
                    424:     get_sysinfo();
                    425:     MKSTR(s[0],sysinfo.type);  MKSTR(s[1],sysinfo.kernel);   MKSTR(s[2],sysinfo.name);
                    426:     MKSTR(s[3],sysinfo.arch);  MKSTR(s[4],sysinfo.release);  MKSTR(s[5],sysinfo.full);
1.44      ohara     427:     MKSTR(s[6],sysinfo.lang);
                    428:     for(i=6,p=NULL; i>=0; i--,p=n) {
1.42      ohara     429:         MKNODE(n,s[i],p);
                    430:     }
                    431:     MKLIST(*rp,n);
                    432: }
                    433:
1.48      fujimoto  434: #if !defined(VISUAL) && !defined(__MINGW32__)
1.44      ohara     435:
                    436: static char *get_lang()
                    437: {
                    438:     char *c, *p, *q;
                    439:     c = setlocale(LC_ALL, NULL); /* saving current locale */
                    440:     p = setlocale(LC_ALL, "");
                    441:     q = (char *)MALLOC(strlen(p)+1);
                    442:     strcpy(q,p);
                    443:     setlocale(LC_ALL, c);        /* restoring current locale */
                    444:     return q;
                    445: }
                    446:
1.43      ohara     447: static char *myuname(char *option)
1.42      ohara     448: {
                    449:     char buf[BUFSIZ];
                    450:     char *s;
1.43      ohara     451:     int fd[2], status, pid;
1.42      ohara     452:     *buf = 0;
                    453:     if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
1.43      ohara     454:         *buf = 0; return NULL;
1.42      ohara     455:     }
1.43      ohara     456:     pid = fork();
                    457:     if (pid < 0) {
                    458:         return NULL;
                    459:     }else if (pid == 0) {
1.42      ohara     460:         dup2(fd[1], 1);
1.43      ohara     461:         close(2);
1.42      ohara     462:         execlp("uname", "uname", option, NULL);
                    463:     }
1.43      ohara     464:     waitpid(pid, &status, 0);
                    465:     if (status) { /* error */
                    466:         return NULL;
                    467:     }
1.42      ohara     468:     s = buf;
                    469:     if( !read(fd[0], s, BUFSIZ-1) || (s = strchr(s, '\n')) ) {
                    470:         *s = 0;
                    471:     }
                    472:     close(fd[0]);
                    473:     close(fd[1]);
                    474:     s = (char *)MALLOC(strlen(buf)+1);
                    475:     strcpy(s, buf);
                    476:     return s;
                    477: }
                    478:
                    479: static void get_sysinfo()
                    480: {
1.43      ohara     481:     static struct utsname u;
1.42      ohara     482:     static int initialized = 0;
                    483:     if (initialized) {
                    484:         return;
                    485:     }
                    486:     initialized = 1;
1.43      ohara     487:     uname(&u);
                    488:     sysinfo.kernel = u.sysname;
1.42      ohara     489: #if defined(__DARWIN__)
                    490:     sysinfo.type   = "macosx";
                    491:     sysinfo.name   = sysinfo.kernel;
                    492: #else
                    493:     sysinfo.type   = "unix";
1.43      ohara     494:     sysinfo.name   = myuname("-o"); // not work on Darwin
                    495:     if (!sysinfo.name) {
                    496:         sysinfo.name = sysinfo.kernel;
                    497:     }
1.42      ohara     498: #endif
1.43      ohara     499:     sysinfo.arch   = u.machine;
                    500:     sysinfo.release= u.release;
                    501:     sysinfo.full   = myuname("-a");
1.44      ohara     502:     sysinfo.lang   = get_lang();
1.42      ohara     503: }
                    504:
                    505: #else
                    506:
                    507: /* http://msdn.microsoft.com/ja-jp/library/windows/desktop/ms724834%28v=vs.85%29.aspx */
                    508: static char *osnameNT(int major, int minor)
                    509: {
                    510:     if ((major == 3 && minor == 51) || (major == 4 && minor == 0)) {
                    511:         return "WindowsNT";
                    512:     }else if (major == 5 && minor == 0) {
                    513:         return "Windows2000";
                    514:     }else if (major == 5 && minor == 1) {
                    515:         return "WindowsXP";
                    516:     }else if (major == 5 && minor == 2) {
                    517:         return "Windows2003 Server";
                    518:     }else if (major == 6 && minor == 0) {
                    519:         return "WindowsVista";
                    520:     }else if (major == 6 && minor == 1) {
                    521:         return "Windows7";
                    522:     }else if (major == 6 && minor == 2) {
                    523:         return "Windows8";
                    524:     }else if (major == 6 && minor == 3) {
                    525:         return "Windows8.1";
1.60    ! ohara     526:     }else if (major == 10 && minor == 0) {
        !           527:         return "Windows10";
1.42      ohara     528:     }
                    529:     return "unknown";
                    530: }
                    531:
                    532: static char *osname95(int major, int minor)
                    533: {
                    534:     if (major == 4 && minor == 0) {
                    535:         return "Windows95";
                    536:     }else if (major == 4 && minor == 10) {
                    537:         return "Windows98";
                    538:     }else if (major == 4 && minor == 90) {
                    539:         return "WindowsMe";
                    540:     }
                    541:     return "unknown";
                    542: }
                    543:
1.44      ohara     544: static char *get_lang()
                    545: {
                    546:     char lang[BUFSIZ];
                    547:     char *s;
                    548:     if(GetLocaleInfo(GetUserDefaultLCID(), LOCALE_SISO639LANGNAME, lang, BUFSIZ)) {
                    549:         s = (char *)MALLOC(strlen(lang)+1);
                    550:         strcpy(s,lang);
                    551:         return s;
                    552:     }
                    553:     return "en"; // English
                    554: }
                    555:
1.60    ! ohara     556: void get_version_win8(int *major,int *minor,int *build)
        !           557: {
        !           558:     char *res;
        !           559:     char fname[4096]; /* fullpath of kernel32.dll (_MAX_PATH==260)*/
        !           560:     char cmd[4096];
        !           561:     int size;
        !           562:     char *version_string;
        !           563:     struct LANGANDCODEPAGE {
        !           564:         WORD language;
        !           565:         WORD codepage;
        !           566:     }* lang;
        !           567:     sprintf(fname,"%s\\system32\\kernel32.dll", getenv("SYSTEMROOT"));
        !           568:     size=GetFileVersionInfoSize(fname,0);
        !           569:     res = (char *)MALLOC(size+1);
        !           570:     if(GetFileVersionInfo(fname,0,size+1,res)) {
        !           571:         VerQueryValue(res,"\\VarFileInfo\\Translation",&lang,&size);
        !           572:         sprintf(cmd,"\\StringFileInfo\\%04x%04x\\ProductVersion",lang->language,lang->codepage);
        !           573:         VerQueryValue(res,cmd,&version_string,&size);
        !           574:         sscanf(version_string,"%d.%d.%d.",major,minor,build);
        !           575:     }
        !           576: }
        !           577:
1.42      ohara     578: static void get_sysinfo()
                    579: {
                    580:     int arch64 = 0;
                    581:     char buf[BUFSIZ];
                    582:     OSVERSIONINFO v;
                    583:     char *s;
                    584:     static int initialized = 0;
                    585:
                    586:     if (initialized) {
                    587:         return;
                    588:     }
                    589:     initialized = 1;
                    590:
                    591:     v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
                    592:     GetVersionEx(&v);
1.60    ! ohara     593:     /* Windows8 == version 6.2 */
        !           594:     if (v.dwPlatformId == VER_PLATFORM_WIN32_NT
        !           595:         && v.dwMajorVersion == 6 && v.dwMinorVersion >= 2) {
        !           596:         get_version_win8(&v.dwMajorVersion, &v.dwMinorVersion, &v.dwBuildNumber);
        !           597:     }
        !           598:     sysinfo.ver_major=v.dwMajorVersion;
        !           599:     sysinfo.ver_minor=v.dwMinorVersion;
        !           600:     sysinfo.ver_build=v.dwBuildNumber;
1.42      ohara     601:
                    602:     sysinfo.type = "windows";
                    603:     sysinfo.arch = "x86";
                    604:     sprintf(buf, "%d.%d.%d", v.dwMajorVersion, v.dwMinorVersion, v.dwBuildNumber);
                    605:     s = (char *)MALLOC(strlen(buf)+1);
                    606:     strcpy(s, buf);
                    607:     sysinfo.release = s;
                    608:
                    609:     if (v.dwPlatformId == VER_PLATFORM_WIN32_NT) {
                    610:         sysinfo.kernel = "WindowsNT";
                    611:         sysinfo.name = osnameNT(v.dwMajorVersion, v.dwMinorVersion);
                    612:
                    613: #if defined(_WIN64)
                    614:         sysinfo.arch = "x86_64";
                    615: #else
                    616:         // IsWow64Process exists on WindowsXP SP2 or later
                    617:         if(GetProcAddress(GetModuleHandle("kernel32.dll"), "IsWow64Process")) {
                    618:             IsWow64Process(GetCurrentProcess(),&arch64);
                    619:             if(arch64) {
                    620:                 sysinfo.arch = "x86_64";
                    621:             }
                    622:         }
                    623: #endif
                    624:     }else { /* v.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS */
                    625:         sysinfo.kernel = "Windows95";
                    626:         sysinfo.name = osname95(v.dwMajorVersion, v.dwMinorVersion);
                    627:     }
                    628:     sprintf(buf, "%s %s %s %s %s", sysinfo.kernel, sysinfo.release, sysinfo.name, v.szCSDVersion, sysinfo.arch);
                    629:     s = (char *)MALLOC(strlen(buf)+1);
                    630:     strcpy(s, buf);
                    631:     sysinfo.full = s;
1.44      ohara     632:     sysinfo.lang = get_lang();
1.42      ohara     633: }
                    634:
                    635: #endif

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