Annotation of OpenXM_contrib2/asir2000/builtin/ctrl.c, Revision 1.43
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.43 ! ohara 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/ctrl.c,v 1.42 2014/05/13 15:02:28 ohara Exp $
1.8 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "parse.h"
1.42 ohara 52: #include <string.h>
53: #if defined(VISUAL)
54: #include <windows.h>
55: #else
56: #include <unistd.h>
57: #include <sys/types.h>
58: #include <sys/socket.h>
59: #include <sys/wait.h>
1.43 ! ohara 60: #include <sys/utsname.h>
1.42 ohara 61: #endif
62:
63: static struct {
64: char *type;
65: char *kernel;
66: char *name;
67: char *arch;
68: char *release;
69: char *full;
70: } sysinfo;
1.1 noro 71:
72: void Pctrl();
1.42 ohara 73: void Psysinfo(LIST *rp);
74: static void get_sysinfo();
1.1 noro 75:
76: struct ftab ctrl_tab[] = {
77: {"ctrl",Pctrl,-2},
1.30 noro 78: {"asir_env",Pctrl,-2},
1.42 ohara 79: {"sysinfo", Psysinfo, 0},
1.1 noro 80: {0,0,0},
81: };
82:
1.33 noro 83: extern int error_in_timer;
1.1 noro 84: extern int prtime,nez,echoback,bigfloat;
1.39 ohara 85: extern int debug_up;
1.1 noro 86: extern int GC_max_heap_size,Verbose,hideargs,hex_output,do_server_in_X11;
1.36 noro 87: extern int do_message,do_terse;
1.4 noro 88: extern int ox_batch,ox_check,ox_exchange_mathcap;
1.1 noro 89: extern int f4_nocheck;
1.16 saito 90: extern int StrassenSize;
1.22 saito 91: extern int outputstyle;
1.3 noro 92: extern int fortran_output;
1.13 noro 93: extern int real_digit;
1.27 noro 94: extern int real_binary;
1.31 noro 95: extern int allow_laurent;
1.12 saito 96: #if defined(INTERVAL)
97: extern int zerorewrite;
1.38 saito 98: extern int Itvplot;
1.12 saito 99: #endif
1.11 noro 100: extern int double_output;
1.14 noro 101: extern int use_new_hensel;
1.15 noro 102: extern int print_quote;
1.20 noro 103: extern int show_crossref;
1.24 noro 104: extern Obj user_defined_prompt;
1.28 ohara 105: extern int asir_setenv;
1.32 noro 106: extern int show_orderspec;
1.34 noro 107: extern int no_debug_on_error;
1.37 noro 108: extern int diag_period;
1.40 noro 109: extern int weight_check;
1.41 ohara 110: extern char **ASIRLOADPATH;
111: extern int ASIRLOADPATH_LEN;
1.1 noro 112:
113: static struct {
114: char *key;
115: int *val;
116: } ctrls[] = {
1.33 noro 117: {"error_in_timer",&error_in_timer},
1.1 noro 118: {"cputime",&prtime},
119: {"nez",&nez},
120: {"echo",&echoback},
1.35 noro 121: #if defined(PARI)
1.1 noro 122: {"bigfloat",&bigfloat},
1.6 noro 123: #endif
1.1 noro 124: {"verbose",&Verbose},
1.23 ohara 125: {"quiet_mode",&do_quiet},
1.1 noro 126: {"hideargs",&hideargs},
127: {"hex",&hex_output},
128: {"debug_window",&do_server_in_X11},
129: {"message",&do_message},
1.36 noro 130: {"terse",&do_terse},
1.1 noro 131: {"debug_up",&debug_up},
1.24 noro 132: {"no_prompt",&do_quiet},
1.28 ohara 133: {"asir_setenv",&asir_setenv},
1.1 noro 134: {"ox_batch",&ox_batch},
135: {"ox_check",&ox_check},
1.5 noro 136: {"ox_exchange_mathcap",&ox_exchange_mathcap},
1.1 noro 137: {"f4_nocheck",&f4_nocheck},
1.16 saito 138: {"StrassenSize",&StrassenSize},
1.22 saito 139: {"outputstyle",&outputstyle},
1.11 noro 140: {"double_output",&double_output},
1.13 noro 141: {"real_digit",&real_digit},
1.27 noro 142: {"real_binary",&real_binary},
1.3 noro 143: {"fortran_output",&fortran_output},
1.14 noro 144: {"new_hensel",&use_new_hensel},
1.15 noro 145: {"print_quote",&print_quote},
1.20 noro 146: {"show_crossref",&show_crossref},
1.31 noro 147: {"allow_laurent",&allow_laurent},
1.32 noro 148: {"show_orderspec",&show_orderspec},
1.34 noro 149: {"no_debug_on_error",&no_debug_on_error},
1.37 noro 150: {"diag_period",&diag_period},
1.40 noro 151: {"weight_check",&weight_check},
1.12 saito 152: #if defined(INTERVAL)
153: {"zerorewrite",&zerorewrite},
1.38 saito 154: {"itvplotsize",&Itvplot},
1.12 saito 155: #endif
1.1 noro 156: {0,0},
157: };
158:
159: void Pctrl(arg,rp)
160: NODE arg;
161: Q *rp;
162: {
1.24 noro 163: int t,i,n;
1.39 ohara 164: int nm,dv;
1.1 noro 165: N num,den;
166: Q c;
167: char *key;
168: char buf[BUFSIZ];
1.24 noro 169: char *str;
1.41 ohara 170: STRING s;
171: NODE node,p;
172: LIST list;
1.1 noro 173:
174: if ( !arg ) {
175: *rp = 0;
176: return;
177: }
178: key = BDY((STRING)ARG0(arg));
179: if ( !strcmp(key,"adj") ) {
180: /* special treatment is necessary for "adj" */
181: if ( argc(arg) == 1 ) {
1.39 ohara 182: Risa_GC_get_adj(&nm,&dv);
183: UTON(dv,num);
184: UTON(nm,den);
1.1 noro 185: } else {
186: c = (Q)ARG1(arg);
187: if ( !c )
188: error("ctrl : adj : invalid argument");
189: num = NM(c);
190: den = !DN(c)?ONEN:DN(c);
1.39 ohara 191: dv = BD(num)[0];
192: nm = BD(den)[0];
193: Risa_GC_set_adj(nm,dv);
1.1 noro 194: }
195: NDTOQ(num,den,1,*rp);
1.24 noro 196: return;
197: } else if ( !strcmp(key,"prompt") ) {
198: /* special treatment is necessary for "prompt" */
199: if ( argc(arg) == 1 ) {
1.26 takayama 200: if ((!do_quiet) && (user_defined_prompt == (Obj)0)) *rp=ONE;
201: else *rp = (Q)user_defined_prompt;
1.24 noro 202: } else {
203: c = (Q)ARG1(arg);
1.25 noro 204: if ( !c ) {
205: do_quiet = 1;
206: user_defined_prompt = 0;
207: *rp = 0;
208: } else if ( OID(c) == O_STR ) {
209: str = BDY((STRING)c);
210: for ( i = 0, n = 0; str[i]; i++ )
211: if ( str[i] == '%' )
212: n++;
213: if ( n >= 2 )
214: error("ctrl : prompt : invalid prompt specification");
1.24 noro 215: do_quiet = 1;
216: user_defined_prompt = (Obj)c;
217: *rp = c;
218: } else if ( NUM(c) && RATN(c) && UNIQ(c) ) {
219: user_defined_prompt = 0;
220: do_quiet = 0;
221: } else {
222: error("ctrl : prompt : invalid argument");
223: }
224: }
1.1 noro 225: return;
1.41 ohara 226: } else if ( !strcmp(key,"loadpath") ) {
227: *rp = 0;
228: if ( argc(arg) == 1 ) {
229: if( ASIRLOADPATH[0] ) {
230: for(i=0; ASIRLOADPATH[i]; i++) {
231: }
232: for(i--,p=NULL; i>=0; i--,p=node) {
233: MKSTR(s,ASIRLOADPATH[i]);
234: MKNODE(node,s,p);
235: }
236: MKLIST(list,node);
237: *rp = list;
238: }
239: } else {
240: list = (LIST)ARG1(arg);
241: if ( OID(list) == O_LIST ) {
242: for(i=0,p=BDY(list); p; i++,p=NEXT(p)) {
243: s=(STRING)BDY(p);
244: if(!s || OID(s)!=O_STR) {
245: error("ctrl : loadpath : invalid argument");
246: }
247: }
248: if(i >= ASIRLOADPATH_LEN) {
249: ASIRLOADPATH_LEN = i+1;
250: ASIRLOADPATH = (char **)MALLOC(sizeof(char *)*ASIRLOADPATH_LEN);
251: }
252: for(i=0,p=BDY(list); p; i++,p=NEXT(p)) {
253: ASIRLOADPATH[i] = (char *)BDY((STRING)BDY(p));
254: }
255: ASIRLOADPATH[i] = NULL;
256: }else {
257: error("ctrl : loadpath : invalid argument");
258: }
259: }
260: return;
1.1 noro 261: }
262: for ( i = 0; ctrls[i].key; i++ )
263: if ( !strcmp(key,ctrls[i].key) )
264: break;
265: if ( ctrls[i].key ) {
266: if ( argc(arg) == 1 )
267: t = *ctrls[i].val;
268: else
269: *ctrls[i].val = t = QTOS((Q)ARG1(arg));
270: STOQ(t,*rp);
271: } else {
272: sprintf(buf,"ctrl : %s : no such key",key);
273: error(buf);
274: }
275: }
1.42 ohara 276:
277: void Psysinfo(LIST *rp)
278: {
279: int i;
280: NODE n,p;
281: STRING s[6];
282:
283: get_sysinfo();
284: MKSTR(s[0],sysinfo.type); MKSTR(s[1],sysinfo.kernel); MKSTR(s[2],sysinfo.name);
285: MKSTR(s[3],sysinfo.arch); MKSTR(s[4],sysinfo.release); MKSTR(s[5],sysinfo.full);
286: for(i=5,p=NULL; i>=0; i--,p=n) {
287: MKNODE(n,s[i],p);
288: }
289: MKLIST(*rp,n);
290: }
291:
292: #if !defined(VISUAL)
1.43 ! ohara 293: static char *myuname(char *option)
1.42 ohara 294: {
295: char buf[BUFSIZ];
296: char *s;
1.43 ! ohara 297: int fd[2], status, pid;
1.42 ohara 298: *buf = 0;
299: if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
1.43 ! ohara 300: *buf = 0; return NULL;
1.42 ohara 301: }
1.43 ! ohara 302: pid = fork();
! 303: if (pid < 0) {
! 304: return NULL;
! 305: }else if (pid == 0) {
1.42 ohara 306: dup2(fd[1], 1);
1.43 ! ohara 307: close(2);
1.42 ohara 308: execlp("uname", "uname", option, NULL);
309: }
1.43 ! ohara 310: waitpid(pid, &status, 0);
! 311: if (status) { /* error */
! 312: return NULL;
! 313: }
1.42 ohara 314: s = buf;
315: if( !read(fd[0], s, BUFSIZ-1) || (s = strchr(s, '\n')) ) {
316: *s = 0;
317: }
318: close(fd[0]);
319: close(fd[1]);
320: s = (char *)MALLOC(strlen(buf)+1);
321: strcpy(s, buf);
322: return s;
323: }
324:
325: static void get_sysinfo()
326: {
1.43 ! ohara 327: static struct utsname u;
1.42 ohara 328: static int initialized = 0;
329: if (initialized) {
330: return;
331: }
332: initialized = 1;
1.43 ! ohara 333: uname(&u);
! 334: sysinfo.kernel = u.sysname;
1.42 ohara 335: #if defined(__DARWIN__)
336: sysinfo.type = "macosx";
337: sysinfo.name = sysinfo.kernel;
338: #else
339: sysinfo.type = "unix";
1.43 ! ohara 340: sysinfo.name = myuname("-o"); // not work on Darwin
! 341: if (!sysinfo.name) {
! 342: sysinfo.name = sysinfo.kernel;
! 343: }
1.42 ohara 344: #endif
1.43 ! ohara 345: sysinfo.arch = u.machine;
! 346: sysinfo.release= u.release;
! 347: sysinfo.full = myuname("-a");
1.42 ohara 348: }
349:
350: #else
351:
352: /* http://msdn.microsoft.com/ja-jp/library/windows/desktop/ms724834%28v=vs.85%29.aspx */
353: static char *osnameNT(int major, int minor)
354: {
355: if ((major == 3 && minor == 51) || (major == 4 && minor == 0)) {
356: return "WindowsNT";
357: }else if (major == 5 && minor == 0) {
358: return "Windows2000";
359: }else if (major == 5 && minor == 1) {
360: return "WindowsXP";
361: }else if (major == 5 && minor == 2) {
362: return "Windows2003 Server";
363: }else if (major == 6 && minor == 0) {
364: return "WindowsVista";
365: }else if (major == 6 && minor == 1) {
366: return "Windows7";
367: }else if (major == 6 && minor == 2) {
368: return "Windows8";
369: }else if (major == 6 && minor == 3) {
370: return "Windows8.1";
371: }
372: return "unknown";
373: }
374:
375: static char *osname95(int major, int minor)
376: {
377: if (major == 4 && minor == 0) {
378: return "Windows95";
379: }else if (major == 4 && minor == 10) {
380: return "Windows98";
381: }else if (major == 4 && minor == 90) {
382: return "WindowsMe";
383: }
384: return "unknown";
385: }
386:
387: static void get_sysinfo()
388: {
389: int arch64 = 0;
390: char buf[BUFSIZ];
391: OSVERSIONINFO v;
392: char *s;
393: static int initialized = 0;
394:
395: if (initialized) {
396: return;
397: }
398: initialized = 1;
399:
400: v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
401: GetVersionEx(&v);
402:
403: sysinfo.type = "windows";
404: sysinfo.arch = "x86";
405: sprintf(buf, "%d.%d.%d", v.dwMajorVersion, v.dwMinorVersion, v.dwBuildNumber);
406: s = (char *)MALLOC(strlen(buf)+1);
407: strcpy(s, buf);
408: sysinfo.release = s;
409:
410: if (v.dwPlatformId == VER_PLATFORM_WIN32_NT) {
411: sysinfo.kernel = "WindowsNT";
412: sysinfo.name = osnameNT(v.dwMajorVersion, v.dwMinorVersion);
413:
414: #if defined(_WIN64)
415: sysinfo.arch = "x86_64";
416: #else
417: // IsWow64Process exists on WindowsXP SP2 or later
418: if(GetProcAddress(GetModuleHandle("kernel32.dll"), "IsWow64Process")) {
419: IsWow64Process(GetCurrentProcess(),&arch64);
420: if(arch64) {
421: sysinfo.arch = "x86_64";
422: }
423: }
424: #endif
425: }else { /* v.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS */
426: sysinfo.kernel = "Windows95";
427: sysinfo.name = osname95(v.dwMajorVersion, v.dwMinorVersion);
428: }
429: sprintf(buf, "%s %s %s %s %s", sysinfo.kernel, sysinfo.release, sysinfo.name, v.szCSDVersion, sysinfo.arch);
430: s = (char *)MALLOC(strlen(buf)+1);
431: strcpy(s, buf);
432: sysinfo.full = s;
433: }
434:
435: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>