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