My Project  UNKNOWN_GIT_VERSION
asciiLink.cc
Go to the documentation of this file.
1 /****************************************
2  * * Computer Algebra System SINGULAR *
3  * ****************************************/
4 
5 /*
6  * ABSTRACT: ascii links (standard)
7  */
8 
9 #include "kernel/mod2.h"
10 #include "misc/options.h"
11 #include "omalloc/omalloc.h"
12 
13 #include "Singular/tok.h"
14 #include "Singular/subexpr.h"
15 #include "Singular/ipshell.h"
16 #include "Singular/ipid.h"
17 #include "Singular/fevoices.h"
19 #include "Singular/ipshell.h"
20 #include "Singular/links/silink.h"
21 
22 /* declarations */
23 static BOOLEAN DumpAscii(FILE *fd, idhdl h,char ***list_of_libs);
24 static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h,char ***list_of_libs);
25 static const char* GetIdString(idhdl h);
26 static int DumpRhs(FILE *fd, idhdl h);
27 static BOOLEAN DumpQring(FILE *fd, idhdl h, const char *type_str);
28 static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl);
29 static BOOLEAN CollectLibs(char *name, char ***list_of_libs);
30 //static BOOLEAN DumpLibs(FILE *fd, char ***list_of_libs);
31 
32 extern si_link_extension si_link_root;
33 
34 /* =============== ASCII ============================================= */
35 BOOLEAN slOpenAscii(si_link l, short flag, leftv /*h*/)
36 {
37  const char *mode;
38  if (flag & SI_LINK_OPEN)
39  {
40  if (l->mode[0] != '\0' && (strcmp(l->mode, "r") == 0))
41  flag = SI_LINK_READ;
42  else flag = SI_LINK_WRITE;
43  }
44 
45  if (flag == SI_LINK_READ) mode = "r";
46  else if (strcmp(l->mode, "w") == 0) mode = "w";
47  else mode = "a";
48 
49 
50  if (l->name[0] == '\0')
51  {
52  // stdin or stdout
53  if (flag == SI_LINK_READ)
54  {
55  l->data = (void *) stdin;
56  mode = "r";
57  }
58  else
59  {
60  l->data = (void *) stdout;
61  mode = "a";
62  }
63  }
64  else
65  {
66  // normal ascii link to a file
67  FILE *outfile;
68  char *filename=l->name;
69 
70  if(filename[0]=='>')
71  {
72  if (filename[1]=='>')
73  {
74  filename+=2;
75  mode = "a";
76  }
77  else
78  {
79  filename++;
80  mode="w";
81  }
82  }
83  outfile=myfopen(filename,mode);
84  if (outfile!=NULL)
85  l->data = (void *) outfile;
86  else
87  return TRUE;
88  }
89 
90  omFree(l->mode);
91  l->mode = omStrDup(mode);
92  SI_LINK_SET_OPEN_P(l, flag);
93  return FALSE;
94 }
95 
97 {
99  if (l->name[0] != '\0')
100  {
101  return (fclose((FILE *)l->data)!=0);
102  }
103  return FALSE;
104 }
105 
107 {
108  FILE * fp=(FILE *)l->data;
109  char * buf=NULL;
110  if (fp!=NULL && l->name[0] != '\0')
111  {
112  fseek(fp,0L,SEEK_END);
113  long len=ftell(fp);
114  if (len<0) len=0;
115  fseek(fp,0L,SEEK_SET);
116  buf=(char *)omAlloc((int)len+1);
117  if (BVERBOSE(V_READING))
118  Print("//Reading %ld chars\n",len);
119  if (len>0) myfread( buf, len, 1, fp);
120  buf[len]='\0';
121  }
122  else
123  {
124  if (pr->Typ()==STRING_CMD)
125  {
126  buf=(char *)omAlloc(80);
127  fe_fgets_stdin((char *)pr->Data(),buf,80);
128  }
129  else
130  {
131  WerrorS("read(<link>,<string>) expected");
132  buf=omStrDup("");
133  }
134  }
136  v->rtyp=STRING_CMD;
137  v->data=buf;
138  return v;
139 }
140 
142 {
143  sleftv tmp;
144  memset(&tmp,0,sizeof(sleftv));
145  tmp.rtyp=STRING_CMD;
146  tmp.data=(void*) "? ";
147  return slReadAscii2(l,&tmp);
148 }
149 
151 {
152  FILE *outfile=(FILE *)l->data;
153  BOOLEAN err=FALSE;
154  char *s;
155  while (v!=NULL)
156  {
157  switch(v->Typ())
158  {
159  case IDEAL_CMD:
160  case MODUL_CMD:
161  case MATRIX_CMD:
162  {
163  ideal I=(ideal)v->Data();
164  for(int i=0;i<IDELEMS(I);i++)
165  {
166  char *s=pString(I->m[i]);
167  fwrite(s,strlen(s),1,outfile);
168  omFree(s);
169  if (i<IDELEMS(I)-1) fwrite(",",1,1,outfile);
170  }
171  break;
172  }
173  default:
174  s = v->String();
175  // free v ??
176  if (s!=NULL)
177  {
178  fputs(s,outfile);
179  fputc('\n',outfile);
180  omFree((ADDRESS)s);
181  }
182  else
183  {
184  WerrorS("cannot convert to string");
185  err=TRUE;
186  }
187  }
188  v = v->next;
189  }
190  fflush(outfile);
191  return err;
192 }
193 
194 const char* slStatusAscii(si_link l, const char* request)
195 {
196  if (strcmp(request, "read") == 0)
197  {
198  if (SI_LINK_R_OPEN_P(l)) return "ready";
199  else return "not ready";
200  }
201  else if (strcmp(request, "write") == 0)
202  {
203  if (SI_LINK_W_OPEN_P(l)) return "ready";
204  else return "not ready";
205  }
206  else return "unknown status request";
207 }
208 
209 /*------------------ Dumping in Ascii format -----------------------*/
210 
212 {
213  FILE *fd = (FILE *) l->data;
214  idhdl h = IDROOT, rh = currRingHdl;
215  char **list_of_libs=NULL;
216  BOOLEAN status = DumpAscii(fd, h, &list_of_libs);
217 
218  if (! status ) status = DumpAsciiMaps(fd, h, NULL);
219 
220  if (currRingHdl != rh) rSetHdl(rh);
221  fprintf(fd, "option(set, intvec(%d, %d));\n", si_opt_1, si_opt_2);
222  char **p=list_of_libs;
223  if (p!=NULL)
224  {
225  while((*p!=NULL) && (*p!=(char*)1))
226  {
227  fprintf(fd,"load(\"%s\",\"try\");\n",*p);
228  p++;
229  }
230  omFree(list_of_libs);
231  }
232  fputs("RETURN();\n",fd);
233  fflush(fd);
234 
235  return status;
236 }
237 
238 // we do that recursively, to dump ids in the the order in which they
239 // were actually defined
240 static BOOLEAN DumpAscii(FILE *fd, idhdl h, char ***list_of_libs)
241 {
242  if (h == NULL) return FALSE;
243 
244  if (DumpAscii(fd, IDNEXT(h),list_of_libs)) return TRUE;
245 
246  // need to set the ring before writing it, otherwise we get in
247  // trouble with minpoly
248  if (IDTYP(h) == RING_CMD)
249  rSetHdl(h);
250 
251  if (DumpAsciiIdhdl(fd, h,list_of_libs)) return TRUE;
252 
253  if (IDTYP(h) == RING_CMD)
254  return DumpAscii(fd, IDRING(h)->idroot,list_of_libs);
255  else
256  return FALSE;
257 }
258 
259 static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl)
260 {
261  if (h == NULL) return FALSE;
262  if (DumpAsciiMaps(fd, IDNEXT(h), rhdl)) return TRUE;
263 
264  if (IDTYP(h) == RING_CMD)
265  return DumpAsciiMaps(fd, IDRING(h)->idroot, h);
266  else if (IDTYP(h) == MAP_CMD)
267  {
268  char *rhs;
269  rSetHdl(rhdl);
270  rhs = h->String();
271 
272  if (fprintf(fd, "setring %s;\n", IDID(rhdl)) == EOF) return TRUE;
273  if (fprintf(fd, "%s %s = %s, %s;\n", Tok2Cmdname(MAP_CMD), IDID(h),
274  IDMAP(h)->preimage, rhs) == EOF)
275  {
276  omFree(rhs);
277  return TRUE;
278  }
279  else
280  {
281  omFree(rhs);
282  return FALSE;
283  }
284  }
285  else return FALSE;
286 }
287 
288 static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h, char ***list_of_libs)
289 {
290  const char *type_str = GetIdString(h);
291  int type_id = IDTYP(h);
292 
293  if (type_id == PACKAGE_CMD)
294  {
295  if (strcmp(IDID(h),"Top")==0) return FALSE; // do not dump "Top"
296  if (IDPACKAGE(h)->language==LANG_SINGULAR) return FALSE;
297  }
298  if (type_id == CRING_CMD)
299  {
300  // do not dump the default CRINGs:
301  if (strcmp(IDID(h),"QQ")==0) return FALSE;
302  if (strcmp(IDID(h),"ZZ")==0) return FALSE;
303  #ifdef SINGULAR_4_2
304  if (strcmp(IDID(h),"AE")==0) return FALSE;
305  if (strcmp(IDID(h),"QAE")==0) return FALSE;
306  #endif
307  }
308 
309  // we do not throw an error if a wrong type was attempted to be dumped
310  if (type_str == NULL)
311  return FALSE;
312 
313  // handle qrings separately
314  if ((type_id == RING_CMD)&&(IDRING(h)->qideal!=NULL))
315  return DumpQring(fd, h, type_str);
316 
317  // C-proc not to be dumped
318  if ((type_id == PROC_CMD) && (IDPROC(h)->language == LANG_C))
319  return FALSE;
320 
321  // handle libraries
322  if ((type_id == PROC_CMD)
323  && (IDPROC(h)->language == LANG_SINGULAR)
324  && (IDPROC(h)->libname!=NULL))
325  return CollectLibs(IDPROC(h)->libname,list_of_libs);
326 
327  // put type and name
328  if (fprintf(fd, "%s %s", type_str, IDID(h)) == EOF)
329  return TRUE;
330  // for matricies, append the dimension
331  if (type_id == MATRIX_CMD)
332  {
333  ideal id = IDIDEAL(h);
334  if (fprintf(fd, "[%d][%d]", id->nrows, id->ncols)== EOF) return TRUE;
335  }
336  else if (type_id == INTMAT_CMD)
337  {
338  if (fprintf(fd, "[%d][%d]", IDINTVEC(h)->rows(), IDINTVEC(h)->cols())
339  == EOF) return TRUE;
340  }
341  else if (type_id == SMATRIX_CMD)
342  {
343  ideal id = IDIDEAL(h);
344  if (fprintf(fd, "[%d][%d]", (int)id->rank, IDELEMS(id))== EOF) return TRUE;
345  }
346 
347  if (type_id == PACKAGE_CMD)
348  {
349  return (fputs(";\n",fd) == EOF);
350  }
351 
352  // write the equal sign
353  if (fputs(" = ",fd) == EOF) return TRUE;
354 
355  // and the right hand side
356  if (DumpRhs(fd, h) == EOF) return TRUE;
357 
358  // semicolon und tschuess
359  if (fputs(";\n",fd) == EOF) return TRUE;
360 
361  return FALSE;
362 }
363 
364 static const char* GetIdString(idhdl h)
365 {
366  int type = IDTYP(h);
367 
368  switch(type)
369  {
370  case LIST_CMD:
371  {
372  lists l = IDLIST(h);
373  int i, nl = l->nr + 1;
374 
375  for (i=0; i<nl; i++)
376  if (GetIdString((idhdl) &(l->m[i])) == NULL) return NULL;
377  }
378  case CRING_CMD:
379  #ifdef SINGULAR_4_2
380  case CNUMBER_CMD:
381  case CMATRIX_CMD:
382  #endif
383  case BIGINT_CMD:
384  case PACKAGE_CMD:
385  case INT_CMD:
386  case INTVEC_CMD:
387  case INTMAT_CMD:
388  case STRING_CMD:
389  case RING_CMD:
390  case QRING_CMD:
391  case PROC_CMD:
392  case NUMBER_CMD:
393  case POLY_CMD:
394  case IDEAL_CMD:
395  case VECTOR_CMD:
396  case MODUL_CMD:
397  case MATRIX_CMD:
398  case SMATRIX_CMD:
399  return Tok2Cmdname(type);
400 
401  case MAP_CMD:
402  case LINK_CMD:
403  return NULL;
404 
405  default:
406  Warn("Error dump data of type %s", Tok2Cmdname(IDTYP(h)));
407  return NULL;
408  }
409 }
410 
411 static BOOLEAN DumpQring(FILE *fd, idhdl h, const char *type_str)
412 {
413  char *ring_str = h->String();
414  if (fprintf(fd, "%s temp_ring = %s;\n", Tok2Cmdname(RING_CMD), ring_str)
415  == EOF) return TRUE;
416  if (fprintf(fd, "%s temp_ideal = %s;\n", Tok2Cmdname(IDEAL_CMD),
417  iiStringMatrix((matrix) IDRING(h)->qideal, 1, currRing, n_GetChar(currRing->cf)))
418  == EOF) return TRUE;
419  if (fputs("attrib(temp_ideal, \"isSB\", 1);\n",fd) == EOF) return TRUE;
420  if (fprintf(fd, "%s %s = temp_ideal;\n", type_str, IDID(h)) == EOF)
421  return TRUE;
422  if (fputs("kill temp_ring;\n",fd) == EOF) return TRUE;
423  else
424  {
425  omFree(ring_str);
426  return FALSE;
427  }
428 }
429 
430 static BOOLEAN CollectLibs(char *name, char *** list_of_libs)
431 {
432  if (*list_of_libs==NULL)
433  {
434  #define MAX_LIBS 256
435  (*list_of_libs)=(char**)omAlloc0(MAX_LIBS*sizeof(char**));
436  (*list_of_libs)[0]=name;
437  (*list_of_libs)[MAX_LIBS-1]=(char*)1;
438  return FALSE;
439  }
440  else
441  {
442  char **p=*list_of_libs;
443  while (((*p)!=NULL)&&((*p!=(char*)1)))
444  {
445  if (strcmp((*p),name)==0) return FALSE;
446  p++;
447  }
448  if (*p==(char*)1)
449  {
450  WerrorS("too many libs");
451  return TRUE;
452  }
453  else
454  {
455  *p=name;
456  }
457  }
458  return FALSE;
459 }
460 
461 
462 static int DumpRhs(FILE *fd, idhdl h)
463 {
464  int type_id = IDTYP(h);
465 
466  if (type_id == LIST_CMD)
467  {
468  lists l = IDLIST(h);
469  int i, nl = l->nr;
470 
471  fputs("list(",fd);
472 
473  for (i=0; i<nl; i++)
474  {
475  if (DumpRhs(fd, (idhdl) &(l->m[i])) == EOF) return EOF;
476  fputs(",",fd);
477  }
478  if (nl > 0)
479  {
480  if (DumpRhs(fd, (idhdl) &(l->m[nl])) == EOF) return EOF;
481  }
482  fputs(")",fd);
483  }
484  else if (type_id == STRING_CMD)
485  {
486  char *pstr = IDSTRING(h);
487  fputc('"', fd);
488  while (*pstr != '\0')
489  {
490  if (*pstr == '"' || *pstr == '\\') fputc('\\', fd);
491  fputc(*pstr, fd);
492  pstr++;
493  }
494  fputc('"', fd);
495  }
496  else if (type_id == PROC_CMD)
497  {
498  procinfov pi = IDPROC(h);
499  if (pi->language == LANG_SINGULAR)
500  {
501  /* pi-Libname==NULL */
502  char *pstr = pi->data.s.body;
503  fputc('"', fd);
504  while (*pstr != '\0')
505  {
506  if (*pstr == '"' || *pstr == '\\') fputc('\\', fd);
507  fputc(*pstr, fd);
508  pstr++;
509  }
510  fputc('"', fd);
511  }
512  else fputs("(null)", fd);
513  }
514  else
515  {
516  char *rhs = h->String();
517 
518  if (rhs == NULL) return EOF;
519 
520  BOOLEAN need_klammer=FALSE;
521  if (type_id == INTVEC_CMD) { fputs("intvec(",fd);need_klammer=TRUE; }
522  else if (type_id == IDEAL_CMD) { fputs("ideal(",fd);need_klammer=TRUE; }
523  else if ((type_id == MODUL_CMD)||(type_id == SMATRIX_CMD))
524  { fputs("module(",fd);need_klammer=TRUE; }
525  else if (type_id == BIGINT_CMD) { fputs("bigint(",fd);need_klammer=TRUE; }
526 
527  if (fputs(rhs,fd) == EOF) return EOF;
528  omFree(rhs);
529 
530  if ((type_id == RING_CMD) &&
531  IDRING(h)->cf->type==n_algExt)
532  {
533  StringSetS("");
534  p_Write(IDRING(h)->cf->extRing->qideal->m[0],IDRING(h)->cf->extRing);
535  rhs = StringEndS();
536  if (fprintf(fd, "; minpoly = %s", rhs) == EOF) { omFree(rhs); return EOF;}
537  omFree(rhs);
538  }
539  else if (need_klammer) fputc(')',fd);
540  }
541  return 1;
542 }
543 
545 {
546  if (l->name[0] == '\0')
547  {
548  WerrorS("getdump: Can not get dump from stdin");
549  return TRUE;
550  }
551  else
552  {
553  BOOLEAN status = newFile(l->name);
554  if (status)
555  return TRUE;
556 
557  int old_echo=si_echo;
558  si_echo=0;
559 
560  status=yyparse();
561 
562  si_echo=old_echo;
563 
564  if (status)
565  return TRUE;
566  else
567  {
568  // lets reset the file pointer to the end to reflect that
569  // we are finished with reading
570  FILE *f = (FILE *) l->data;
571  fseek(f, 0L, SEEK_END);
572  return FALSE;
573  }
574  }
575 }
576 
577 
579 {
580  si_link_extension s;
583  si_link_root->Close=slCloseAscii;
584  si_link_root->Kill=NULL;
586  si_link_root->Read2=slReadAscii2;
587  si_link_root->Write=slWriteAscii;
589  si_link_root->GetDump=slGetDumpAscii;
590  si_link_root->Status=slStatusAscii;
591  si_link_root->type="ASCII";
592  s = si_link_root;
593  s->next = NULL;
594 }
int BOOLEAN
Definition: auxiliary.h:85
#define TRUE
Definition: auxiliary.h:98
#define FALSE
Definition: auxiliary.h:94
void * ADDRESS
Definition: auxiliary.h:133
int l
Definition: cfEzgcd.cc:93
int i
Definition: cfEzgcd.cc:125
int p
Definition: cfModGcd.cc:4019
CanonicalForm fp
Definition: cfModGcd.cc:4043
CanonicalForm cf
Definition: cfModGcd.cc:4024
FILE * f
Definition: checklibs.c:9
Variable next() const
Definition: factory.h:137
Definition: idrec.h:35
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
int Typ()
Definition: subexpr.cc:1039
int rtyp
Definition: subexpr.h:91
void * Data()
Definition: subexpr.cc:1182
void * data
Definition: subexpr.h:88
Definition: lists.h:23
@ n_algExt
used for all algebraic extensions, i.e., the top-most extension in an extension tower is algebraic
Definition: coeffs.h:36
static FORCE_INLINE int n_GetChar(const coeffs r)
Return the characteristic of the coeff. domain.
Definition: coeffs.h:444
#define Print
Definition: emacs.cc:80
#define Warn
Definition: emacs.cc:77
const CanonicalForm int s
Definition: facAbsFact.cc:55
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:37
char name(const Variable &v)
Definition: factory.h:180
FILE * myfopen(const char *path, const char *mode)
Definition: feFopen.cc:167
void WerrorS(const char *s)
Definition: feFopen.cc:24
size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition: feFopen.cc:195
int si_echo
Definition: febase.cc:35
char *(* fe_fgets_stdin)(const char *pr, char *s, int size)
Definition: feread.cc:34
BOOLEAN newFile(char *fname)
Definition: fevoices.cc:120
const char * Tok2Cmdname(int tok)
Definition: gentable.cc:140
@ IDEAL_CMD
Definition: grammar.cc:284
@ MATRIX_CMD
Definition: grammar.cc:286
@ MAP_CMD
Definition: grammar.cc:285
@ PROC_CMD
Definition: grammar.cc:280
@ INTMAT_CMD
Definition: grammar.cc:279
@ MODUL_CMD
Definition: grammar.cc:287
@ SMATRIX_CMD
Definition: grammar.cc:291
@ VECTOR_CMD
Definition: grammar.cc:292
@ NUMBER_CMD
Definition: grammar.cc:288
@ POLY_CMD
Definition: grammar.cc:289
@ RING_CMD
Definition: grammar.cc:281
int yyparse(void)
Definition: grammar.cc:2111
idhdl currRingHdl
Definition: ipid.cc:61
#define IDMAP(a)
Definition: ipid.h:130
#define IDSTRING(a)
Definition: ipid.h:131
#define IDNEXT(a)
Definition: ipid.h:113
#define IDPROC(a)
Definition: ipid.h:135
#define IDINTVEC(a)
Definition: ipid.h:123
#define IDIDEAL(a)
Definition: ipid.h:128
omBin sleftv_bin
Definition: subexpr.cc:47
#define IDID(a)
Definition: ipid.h:117
#define IDROOT
Definition: ipid.h:18
#define IDPACKAGE(a)
Definition: ipid.h:134
#define IDRING(a)
Definition: ipid.h:122
#define IDTYP(a)
Definition: ipid.h:114
#define IDLIST(a)
Definition: ipid.h:132
void rSetHdl(idhdl h)
Definition: ipshell.cc:5081
static Poly * h
Definition: janet.cc:972
#define pstr
Definition: libparse.cc:1244
#define pi
Definition: libparse.cc:1143
if(yy_init)
Definition: libparse.cc:1418
while(1)
Definition: libparse.cc:1442
char * iiStringMatrix(matrix im, int dim, const ring r, char ch)
Definition: matpol.cc:858
#define SEEK_SET
Definition: mod2.h:116
#define SEEK_END
Definition: mod2.h:112
#define omStrDup(s)
Definition: omAllocDecl.h:263
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omAlloc0Bin(bin)
Definition: omAllocDecl.h:206
#define omFree(addr)
Definition: omAllocDecl.h:261
#define omAlloc0(size)
Definition: omAllocDecl.h:211
#define NULL
Definition: omList.c:10
unsigned si_opt_2
Definition: options.c:6
unsigned si_opt_1
Definition: options.c:5
#define BVERBOSE(a)
Definition: options.h:35
#define V_READING
Definition: options.h:46
void p_Write(poly p, ring lmRing, ring tailRing)
Definition: polys0.cc:204
ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:13
char * pString(poly p)
Definition: polys.h:300
void StringSetS(const char *st)
Definition: reporter.cc:128
char * StringEndS()
Definition: reporter.cc:151
int * status
Definition: si_signals.h:51
int status int void * buf
Definition: si_signals.h:59
int status int fd
Definition: si_signals.h:59
#define IDELEMS(i)
Definition: simpleideals.h:24
sleftv * leftv
Definition: structs.h:60
@ LANG_SINGULAR
Definition: subexpr.h:22
@ LANG_C
Definition: subexpr.h:22
@ BIGINT_CMD
Definition: tok.h:38
@ CRING_CMD
Definition: tok.h:56
@ LIST_CMD
Definition: tok.h:118
@ INTVEC_CMD
Definition: tok.h:101
@ PACKAGE_CMD
Definition: tok.h:149
@ CMATRIX_CMD
Definition: tok.h:46
@ CNUMBER_CMD
Definition: tok.h:47
@ LINK_CMD
Definition: tok.h:117
@ QRING_CMD
Definition: tok.h:158
@ STRING_CMD
Definition: tok.h:183
@ INT_CMD
Definition: tok.h:96