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