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