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