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