1 | /***************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | *****************************************/ |
---|
4 | /* $Id: extra.cc,v 1.11 1997-05-02 07:44:48 Singular Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: general interface to internals of Singular ("system" command) |
---|
7 | */ |
---|
8 | |
---|
9 | #include <stdlib.h> |
---|
10 | #include <stdio.h> |
---|
11 | #include <string.h> |
---|
12 | #include <ctype.h> |
---|
13 | #include "mod2.h" |
---|
14 | |
---|
15 | #ifndef macintosh |
---|
16 | #ifdef TIME_WITH_SYS_TIME |
---|
17 | # include <time.h> |
---|
18 | # ifdef HAVE_SYS_TIME_H |
---|
19 | # include <sys/time.h> |
---|
20 | # endif |
---|
21 | #else |
---|
22 | # ifdef HAVE_SYS_TIME_H |
---|
23 | # include <sys/time.h> |
---|
24 | # else |
---|
25 | # include <time.h> |
---|
26 | # endif |
---|
27 | #endif |
---|
28 | #ifdef HAVE_SYS_TIMES_H |
---|
29 | #include <sys/times.h> |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <unistd.h> |
---|
33 | #endif |
---|
34 | |
---|
35 | #include "tok.h" |
---|
36 | #include "ipid.h" |
---|
37 | #include "kutil.h" |
---|
38 | #include "cntrlc.h" |
---|
39 | #include "stairc.h" |
---|
40 | #include "ipshell.h" |
---|
41 | #include "algmap.h" |
---|
42 | #include "modulop.h" |
---|
43 | #include "febase.h" |
---|
44 | #include "matpol.h" |
---|
45 | #include "longalg.h" |
---|
46 | #include "ideals.h" |
---|
47 | #include "kstd1.h" |
---|
48 | |
---|
49 | #ifdef HAVE_FACTORY |
---|
50 | #define SI_DONT_HAVE_GLOBAL_VARS |
---|
51 | #include "clapsing.h" |
---|
52 | #include "clapconv.h" |
---|
53 | #include "kstdfac.h" |
---|
54 | #endif |
---|
55 | |
---|
56 | //void emStart(); |
---|
57 | /*2 |
---|
58 | * the "system" command |
---|
59 | */ |
---|
60 | BOOLEAN jjSYSTEM(leftv res, leftv h) |
---|
61 | { |
---|
62 | if(h->Typ() == STRING_CMD) |
---|
63 | { |
---|
64 | /*==================== lib ==================================*/ |
---|
65 | if(strcmp((char*)(h->Data()),"LIB")==0) |
---|
66 | { |
---|
67 | idhdl hh=idroot->get((char*)h->next->Data(),0); |
---|
68 | if ((hh!=NULL)&&(IDTYP(hh)==PROC_CMD)) |
---|
69 | { |
---|
70 | res->rtyp=STRING_CMD; |
---|
71 | res->data=mstrdup(iiGetLibName(IDSTRING(hh))); |
---|
72 | if (res->data==NULL) res->data=mstrdup(""); |
---|
73 | return FALSE; |
---|
74 | } |
---|
75 | else |
---|
76 | Warn("`%s` not found",(char*)h->next->Data()); |
---|
77 | } |
---|
78 | else |
---|
79 | /*==================== version ==================================*/ |
---|
80 | if(strcmp((char*)(h->Data()),"version")==0) |
---|
81 | { |
---|
82 | res->rtyp=INT_CMD; |
---|
83 | res->data=(void *)SINGULAR_VERSION; |
---|
84 | return FALSE; |
---|
85 | } |
---|
86 | else |
---|
87 | /*==================== gen ==================================*/ |
---|
88 | if(strcmp((char*)(h->Data()),"gen")==0) |
---|
89 | { |
---|
90 | res->rtyp=INT_CMD; |
---|
91 | res->data=(void *)npGen; |
---|
92 | return FALSE; |
---|
93 | } |
---|
94 | else |
---|
95 | /*==================== sh ==================================*/ |
---|
96 | #ifndef macintosh |
---|
97 | if(strcmp((char*)(h->Data()),"sh")==0) |
---|
98 | { |
---|
99 | #ifndef MSDOS |
---|
100 | #ifdef HAVE_FEREAD |
---|
101 | fe_temp_reset(); |
---|
102 | #endif |
---|
103 | #endif |
---|
104 | res->rtyp=INT_CMD; |
---|
105 | if (h->next==NULL) res->data = (void *)system("/bin/sh"); |
---|
106 | else res->data = (void*) system((char*)(h->next->Data())); |
---|
107 | #ifndef MSDOS |
---|
108 | #ifdef HAVE_FEREAD |
---|
109 | fe_temp_set(); |
---|
110 | #endif |
---|
111 | #endif |
---|
112 | return FALSE; |
---|
113 | } |
---|
114 | else |
---|
115 | #endif |
---|
116 | /*==================== with ==================================*/ |
---|
117 | if(strcmp((char*)(h->Data()),"with")==0) |
---|
118 | { |
---|
119 | if (h->next==NULL) |
---|
120 | { |
---|
121 | char with_str[]="" |
---|
122 | #ifdef DRING |
---|
123 | "DRING " |
---|
124 | #endif |
---|
125 | #ifdef HAVE_DBM |
---|
126 | "DBM " |
---|
127 | #endif |
---|
128 | #ifdef HAVE_DLD |
---|
129 | "DLD " |
---|
130 | #endif |
---|
131 | #ifdef HAVE_GMP |
---|
132 | "gmp " |
---|
133 | #endif |
---|
134 | #ifdef HAVE_FACTORY |
---|
135 | "factory " |
---|
136 | #endif |
---|
137 | #ifdef HAVE_LIBFAC_P |
---|
138 | "libfac " |
---|
139 | #endif |
---|
140 | #ifdef HAVE_MPSR |
---|
141 | "MP " |
---|
142 | #endif |
---|
143 | #ifdef HAVE_READLINE |
---|
144 | "readline " |
---|
145 | #endif |
---|
146 | #ifdef HAVE_TCL |
---|
147 | "tcl " |
---|
148 | #endif |
---|
149 | #ifdef SRING |
---|
150 | "SRING " |
---|
151 | #endif |
---|
152 | ; |
---|
153 | res->rtyp=STRING_CMD; |
---|
154 | char *s=mstrdup(with_str); |
---|
155 | s[strlen(s)-1]='\0'; |
---|
156 | res->data=(void *)s; |
---|
157 | return FALSE; |
---|
158 | } |
---|
159 | else if (h->next->Typ()==STRING_CMD) |
---|
160 | { |
---|
161 | #define TEST_FOR(A) if(strcmp(s,A)==0) res->data=(void *)1; else |
---|
162 | char *s=(char *)h->next->Data(); |
---|
163 | res->rtyp=INT_CMD; |
---|
164 | #ifdef DRING |
---|
165 | TEST_FOR("DRING") |
---|
166 | #endif |
---|
167 | #ifdef HAVE_DBM |
---|
168 | TEST_FOR("DBM") |
---|
169 | #endif |
---|
170 | #ifdef HAVE_DLD |
---|
171 | TEST_FOR("DLD") |
---|
172 | #endif |
---|
173 | #ifdef HAVE_GMP |
---|
174 | TEST_FOR("gmp") |
---|
175 | #endif |
---|
176 | #ifdef HAVE_FACTORY |
---|
177 | TEST_FOR("factory") |
---|
178 | #endif |
---|
179 | #ifdef HAVE_LIBFAC_P |
---|
180 | TEST_FOR("libfac") |
---|
181 | #endif |
---|
182 | #ifdef HAVE_MPSR |
---|
183 | TEST_FOR("MP") |
---|
184 | #endif |
---|
185 | #ifdef HAVE_READLINE |
---|
186 | TEST_FOR("readline") |
---|
187 | #endif |
---|
188 | #ifdef HAVE_TCL |
---|
189 | TEST_FOR("tcl") |
---|
190 | #endif |
---|
191 | #ifdef SRING |
---|
192 | TEST_FOR("SRING") |
---|
193 | #endif |
---|
194 | ; |
---|
195 | return FALSE; |
---|
196 | #undef TEST_FOR |
---|
197 | } |
---|
198 | return TRUE; |
---|
199 | } |
---|
200 | else |
---|
201 | /*==================== pid ==================================*/ |
---|
202 | #ifndef MSDOS |
---|
203 | #ifndef macintosh |
---|
204 | if (strcmp((char*)(h->Data()),"pid")==0) |
---|
205 | { |
---|
206 | res->rtyp=INT_CMD; |
---|
207 | res->data=(void *)getpid(); |
---|
208 | return FALSE; |
---|
209 | } |
---|
210 | else |
---|
211 | #endif |
---|
212 | #endif |
---|
213 | /*==================== getenv ==================================*/ |
---|
214 | if (strcmp((char*)(h->Data()),"getenv")==0) |
---|
215 | { |
---|
216 | if ((h->next!=NULL) && (h->next->Typ()==STRING_CMD)) |
---|
217 | { |
---|
218 | res->rtyp=STRING_CMD; |
---|
219 | char *r=getenv((char *)h->next->Data()); |
---|
220 | if (r==NULL) r=""; |
---|
221 | res->data=(void *)mstrdup(r); |
---|
222 | return FALSE; |
---|
223 | } |
---|
224 | else |
---|
225 | { |
---|
226 | WerrorS("string expected"); |
---|
227 | } |
---|
228 | } |
---|
229 | else |
---|
230 | /*==================== tty ==================================*/ |
---|
231 | #ifndef macintosh |
---|
232 | #ifndef MSDOS |
---|
233 | if (strcmp((char*)(h->Data()),"tty")==0) |
---|
234 | { |
---|
235 | #ifdef HAVE_FEREAD |
---|
236 | #ifdef HAVE_ATEXIT |
---|
237 | fe_reset_input_mode(); |
---|
238 | #else |
---|
239 | fe_reset_input_mode(0,NULL); |
---|
240 | #endif |
---|
241 | if ((h->next!=NULL)&&(h->next->Typ()==INT_CMD)) |
---|
242 | { |
---|
243 | fe_use_fgets=(int)h->next->Data(); |
---|
244 | fe_set_input_mode(); |
---|
245 | } |
---|
246 | #elif HAVE_READLINE |
---|
247 | system("stty sane"); |
---|
248 | #endif |
---|
249 | return FALSE; |
---|
250 | } |
---|
251 | else |
---|
252 | #endif |
---|
253 | #endif |
---|
254 | /*==================== HC ==================================*/ |
---|
255 | if (strcmp((char*)(h->data),"HC")==0) |
---|
256 | { |
---|
257 | res->rtyp=INT_CMD; |
---|
258 | res->data=(void *)HCord; |
---|
259 | return FALSE; |
---|
260 | } |
---|
261 | else |
---|
262 | /*==================== random ==================================*/ |
---|
263 | if(strcmp((char*)(h->Data()),"random")==0) |
---|
264 | { |
---|
265 | if ((h->next!=NULL) &&(h->next->Typ()==INT_CMD)) |
---|
266 | { |
---|
267 | siRandomStart=(int)h->next->Data(); |
---|
268 | #ifdef buildin_rand |
---|
269 | siSeed=siRandomStart; |
---|
270 | #else |
---|
271 | srand((unsigned int)siRandomStart); |
---|
272 | #endif |
---|
273 | return FALSE; |
---|
274 | } |
---|
275 | else |
---|
276 | WerrorS("int expected"); |
---|
277 | } |
---|
278 | else |
---|
279 | /*==================== naIdeal ==================================*/ |
---|
280 | if(strcmp((char*)(h->Data()),"naIdeal")==0) |
---|
281 | { |
---|
282 | if ((h->next!=NULL) &&(h->next->Typ()==IDEAL_CMD)) |
---|
283 | { |
---|
284 | naSetIdeal((ideal)h->next->Data()); |
---|
285 | return FALSE; |
---|
286 | } |
---|
287 | else |
---|
288 | WerrorS("ideal expected"); |
---|
289 | } |
---|
290 | else |
---|
291 | /*==================== isSqrFree =============================*/ |
---|
292 | #ifdef HAVE_FACTORY |
---|
293 | if(strcmp((char*)(h->Data()),"isSqrFree")==0) |
---|
294 | { |
---|
295 | if ((h->next!=NULL) &&(h->next->Typ()==POLY_CMD)) |
---|
296 | { |
---|
297 | res->rtyp=INT_CMD; |
---|
298 | res->data=(void *)singclap_isSqrFree((poly)h->next->Data()); |
---|
299 | return FALSE; |
---|
300 | } |
---|
301 | else |
---|
302 | WerrorS("poly expected"); |
---|
303 | } |
---|
304 | else |
---|
305 | #endif |
---|
306 | /*==================== neworder =============================*/ |
---|
307 | #ifdef HAVE_LIBFAC_P |
---|
308 | if(strcmp((char*)(h->Data()),"neworder")==0) |
---|
309 | { |
---|
310 | if ((h->next!=NULL) &&(h->next->Typ()==IDEAL_CMD)) |
---|
311 | { |
---|
312 | res->rtyp=STRING_CMD; |
---|
313 | res->data=(void *)singclap_neworder((ideal)h->next->Data()); |
---|
314 | return FALSE; |
---|
315 | } |
---|
316 | else |
---|
317 | WerrorS("ideal expected"); |
---|
318 | } |
---|
319 | else |
---|
320 | #endif |
---|
321 | /*==================== alarm ==================================*/ |
---|
322 | #ifndef macintosh |
---|
323 | #ifndef MSDOS |
---|
324 | #ifndef atarist |
---|
325 | if(strcmp((char*)(h->Data()),"alarm")==0) |
---|
326 | { |
---|
327 | if ((h->next!=NULL) &&(h->next->Typ()==INT_CMD)) |
---|
328 | { |
---|
329 | // standard variant -> SIGALARM (standard: abort) |
---|
330 | //alarm((unsigned)h->next->Data()); |
---|
331 | // process time (user +system): SIGVTALARM |
---|
332 | struct itimerval t,o; |
---|
333 | memset(&t,0,sizeof(t)); |
---|
334 | t.it_value.tv_sec =(unsigned)h->next->Data(); |
---|
335 | setitimer(ITIMER_VIRTUAL,&t,&o); |
---|
336 | return FALSE; |
---|
337 | } |
---|
338 | else |
---|
339 | WerrorS("int expected"); |
---|
340 | } |
---|
341 | else |
---|
342 | #endif |
---|
343 | #endif |
---|
344 | #endif |
---|
345 | /*==================== std =============================*/ |
---|
346 | #if 0 |
---|
347 | #ifdef HAVE_FACTORY |
---|
348 | if(strcmp((char*)(h->Data()),"std")==0) |
---|
349 | { |
---|
350 | if ((h->next!=NULL) &&(h->next->Typ()==IDEAL_CMD)) |
---|
351 | { |
---|
352 | res->rtyp=LIST_CMD; |
---|
353 | res->data=(void *)stdfac((ideal)h->next->Data(),NULL,testHomog,NULL); |
---|
354 | return FALSE; |
---|
355 | } |
---|
356 | else |
---|
357 | WerrorS("ideal expected"); |
---|
358 | } |
---|
359 | else |
---|
360 | #endif |
---|
361 | #endif |
---|
362 | /*==================== red =============================*/ |
---|
363 | #if 0 |
---|
364 | if(strcmp((char*)(h->Data()),"red")==0) |
---|
365 | { |
---|
366 | if ((h->next!=NULL) &&(h->next->Typ()==IDEAL_CMD)) |
---|
367 | { |
---|
368 | res->rtyp=IDEAL_CMD; |
---|
369 | res->data=(void *)stdred((ideal)h->next->Data(),NULL,testHomog,NULL); |
---|
370 | setFlag(res,FLAG_STD); |
---|
371 | return FALSE; |
---|
372 | } |
---|
373 | else |
---|
374 | WerrorS("ideal expected"); |
---|
375 | } |
---|
376 | else |
---|
377 | #endif |
---|
378 | /*==================== min_std =============================*/ |
---|
379 | #if 0 |
---|
380 | if(strcmp((char*)(h->Data()),"min_std")==0) |
---|
381 | { |
---|
382 | if ((h->next!=NULL) &&(h->next->Typ()==IDEAL_CMD)) |
---|
383 | { |
---|
384 | res->rtyp=LIST_CMD; |
---|
385 | res->data=(void *)min_std((ideal)h->next->Data(),currQuotient,testHomog,NULL); |
---|
386 | return FALSE; |
---|
387 | } |
---|
388 | else |
---|
389 | WerrorS("ideal expected"); |
---|
390 | } |
---|
391 | else |
---|
392 | #endif |
---|
393 | /*==================== writemat ==================================*/ |
---|
394 | if(strcmp((char*)(h->Data()),"writemat")==0) |
---|
395 | { |
---|
396 | if (h->next!=NULL) |
---|
397 | { |
---|
398 | leftv v=h->next; |
---|
399 | if (v->Typ() == STRING_CMD) |
---|
400 | { |
---|
401 | char *filename = (char *)v->Data(); |
---|
402 | v = v->next; |
---|
403 | if (v->Typ()==MATRIX_CMD) |
---|
404 | { |
---|
405 | FILE *outfile = fopen(filename,"a"); |
---|
406 | if (outfile==NULL) |
---|
407 | { |
---|
408 | Werror("cannot write to file %s",filename); |
---|
409 | return TRUE; |
---|
410 | } |
---|
411 | matrix m=(matrix)v->Data(); |
---|
412 | fprintf(outfile,"%d\n%d\n",MATROWS(m),MATCOLS(m)); |
---|
413 | char *s = iiStringMatrix(m,2); |
---|
414 | fprintf(outfile,"%s\n",s); |
---|
415 | FreeL((ADDRESS)s); |
---|
416 | fclose(outfile); |
---|
417 | return FALSE; |
---|
418 | } |
---|
419 | else |
---|
420 | { |
---|
421 | WerrorS("matrix expected"); |
---|
422 | } |
---|
423 | } |
---|
424 | else |
---|
425 | { |
---|
426 | WerrorS("string expected"); |
---|
427 | } |
---|
428 | } |
---|
429 | else |
---|
430 | WerrorS("matrix expected"); |
---|
431 | } |
---|
432 | else |
---|
433 | #ifdef HAVE_FACTORY |
---|
434 | /*==================== pdivide ====================*/ |
---|
435 | if (strcmp((char*)(h->Data()),"pdivide")==0) |
---|
436 | { |
---|
437 | if (h->next!=NULL && h->next->next!=NULL && |
---|
438 | h->next->Typ()==POLY_CMD && h->next->next->Typ()==POLY_CMD) |
---|
439 | { |
---|
440 | res->rtyp=POLY_CMD; |
---|
441 | res->data=(void*)(singclap_pdivide((poly)(h->next->Data()), |
---|
442 | (poly)(h->next->next->Data()))); |
---|
443 | return FALSE; |
---|
444 | } |
---|
445 | else |
---|
446 | WerrorS("poly expected"); |
---|
447 | } |
---|
448 | else |
---|
449 | #endif |
---|
450 | /*==================== algfetch =====================*/ |
---|
451 | if (strcmp((char*)(h->Data()),"algfetch")==0) |
---|
452 | { |
---|
453 | int k; |
---|
454 | idhdl w; |
---|
455 | ideal i0, i1; |
---|
456 | leftv v=h->next; |
---|
457 | ring r0=(ring)v->Data(); |
---|
458 | v = v->next; |
---|
459 | w = r0->idroot->get(v->Name(),myynest); |
---|
460 | i0 = IDIDEAL(w); |
---|
461 | i1 = idInit(IDELEMS(i0),i0->rank); |
---|
462 | for (k=0; k<IDELEMS(i1); k++) |
---|
463 | { |
---|
464 | i1->m[k] = maAlgpolyFetch(r0, i0->m[k]); |
---|
465 | } |
---|
466 | res->rtyp = IDEAL_CMD; |
---|
467 | res->data = (void*)i1; |
---|
468 | return FALSE; |
---|
469 | } |
---|
470 | else |
---|
471 | /*==================== algmap =======================*/ |
---|
472 | if (strcmp((char*)(h->Data()),"algmap")==0) |
---|
473 | { |
---|
474 | int k; |
---|
475 | idhdl w; |
---|
476 | ideal i0, i1, i, j; |
---|
477 | leftv v=h->next; |
---|
478 | ring r0=(ring)v->Data(); |
---|
479 | v = v->next; |
---|
480 | w = r0->idroot->get(v->Name(),myynest); |
---|
481 | i0 = IDIDEAL(w); |
---|
482 | v = v->next; |
---|
483 | i = (ideal)v->Data(); |
---|
484 | v = v->next; |
---|
485 | j = (ideal)v->Data(); |
---|
486 | i1 = idInit(IDELEMS(i0),i0->rank); |
---|
487 | for (k=0; k<IDELEMS(i1); k++) |
---|
488 | { |
---|
489 | i1->m[k] = maAlgpolyMap(r0, i0->m[k], i, j); |
---|
490 | } |
---|
491 | res->rtyp = IDEAL_CMD; |
---|
492 | res->data = (void*)i1; |
---|
493 | return FALSE; |
---|
494 | } |
---|
495 | else |
---|
496 | /*==================== indsetall =============================*/ |
---|
497 | if(strcmp((char*)(h->Data()),"indsetall")==0) |
---|
498 | { |
---|
499 | if ((h->next!=NULL) &&(h->next->Typ()==IDEAL_CMD)) |
---|
500 | { |
---|
501 | int i=0; |
---|
502 | if (h->next->next!=NULL) |
---|
503 | { |
---|
504 | if (h->next->next->Typ()==INT_CMD) |
---|
505 | i=(int)h->next->next->Data(); |
---|
506 | else |
---|
507 | { |
---|
508 | Warn("int expected"); |
---|
509 | } |
---|
510 | } |
---|
511 | res->rtyp=LIST_CMD; |
---|
512 | res->data=(void *)scIndIndset((ideal)h->next->Data(),i,currQuotient); |
---|
513 | return FALSE; |
---|
514 | } |
---|
515 | else |
---|
516 | WerrorS("ideal expected"); |
---|
517 | } |
---|
518 | else |
---|
519 | /*============================================================*/ |
---|
520 | WerrorS("not implemented\n"); |
---|
521 | } |
---|
522 | return TRUE; |
---|
523 | } |
---|