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