My Project
iplib.cc
Go to the documentation of this file.
1 /****************************************
2 * Computer Algebra System SINGULAR *
3 ****************************************/
4 /*
5 * ABSTRACT: interpreter: LIB and help
6 */
7 
8 #include "kernel/mod2.h"
9 
10 #include "Singular/tok.h"
11 #include "misc/options.h"
12 #include "Singular/ipid.h"
13 #include "polys/monomials/ring.h"
14 #include "Singular/subexpr.h"
15 #include "Singular/ipid.h"
16 #include "Singular/ipshell.h"
17 #include "Singular/fevoices.h"
18 #include "Singular/lists.h"
19 
20 #include <ctype.h>
21 
22 #if SIZEOF_LONG == 8
23 #define SI_MAX_NEST 500
24 #elif defined(__CYGWIN__)
25 #define SI_MAX_NEST 480
26 #else
27 #define SI_MAX_NEST 1000
28 #endif
29 
30 #if defined(ix86Mac_darwin) || defined(x86_64Mac_darwin) || defined(ppcMac_darwin)
31 # define MODULE_SUFFIX bundle
32 #elif defined(__CYGWIN__)
33 # define MODULE_SUFFIX dll
34 #else
35 # define MODULE_SUFFIX so
36 #endif
37 
38 #define MODULE_SUFFIX_STRING EXPANDED_STRINGIFY(MODULE_SUFFIX)
39 
40 
41 #ifdef HAVE_DYNAMIC_LOADING
42 BOOLEAN load_modules(const char *newlib, char *fullname, BOOLEAN autoexport);
43 #endif
44 
45 #ifdef HAVE_LIBPARSER
46 # include "libparse.h"
47 #else /* HAVE_LIBPARSER */
48 procinfo *iiInitSingularProcinfo(procinfov pi, const char *libname,
49  const char *procname, int line, long pos, BOOLEAN pstatic=FALSE);
50 #endif /* HAVE_LIBPARSER */
51 
52 extern int iiArithAddCmd(const char *szName, short nAlias, short nTokval,
53  short nToktype, short nPos);
54 
55 #include "Singular/mod_lib.h"
56 
57 #ifdef HAVE_LIBPARSER
58 void yylprestart (FILE *input_file );
59 int current_pos(int i=0);
62 extern const char *yylp_errlist[];
63 void print_init();
65 #endif
66 
67 //int IsCmd(char *n, int tok);
68 char mytolower(char c);
69 
70 /*2
71 * return TRUE if the libray libname is already loaded
72 */
73 BOOLEAN iiGetLibStatus(const char *lib)
74 {
75  idhdl hl;
76 
77  char *plib = iiConvName(lib);
78  hl = basePack->idroot->get(plib,0);
79  omFree(plib);
80  if((hl==NULL) ||(IDTYP(hl)!=PACKAGE_CMD))
81  {
82  return FALSE;
83  }
84  if ((IDPACKAGE(hl)->language!=LANG_C)&&(IDPACKAGE(hl)->libname!=NULL))
85  return (strcmp(lib,IDPACKAGE(hl)->libname)==0);
86  return FALSE;
87 }
88 
89 /*2
90 * given a line 'proc[ ]+{name}[ \t]*'
91 * return a pointer to name and set the end of '\0'
92 * changes the input!
93 * returns: e: pointer to 'end of name'
94 * ct: changed char at the end of s
95 */
96 char* iiProcName(char *buf, char & ct, char* &e)
97 {
98  char *s=buf+5;
99  while (*s==' ') s++;
100  e=s+1;
101  while ((*e>' ') && (*e!='(')) e++;
102  ct=*e;
103  *e='\0';
104  return s;
105 }
106 
107 /*2
108 * given a line with args, return the argstr
109 */
110 char * iiProcArgs(char *e,BOOLEAN withParenth)
111 {
112  while ((*e==' ') || (*e=='\t') || (*e=='(')) e++;
113  if (*e<' ')
114  {
115  if (withParenth)
116  {
117  // no argument list, allow list #
118  return omStrDup("parameter list #;");
119  }
120  else
121  {
122  // empty list
123  return omStrDup("");
124  }
125  }
126  BOOLEAN in_args;
127  BOOLEAN args_found;
128  char *s;
129  char *argstr=(char *)omAlloc(127); // see ../omalloc/omTables.inc
130  int argstrlen=127;
131  *argstr='\0';
132  int par=0;
133  do
134  {
135  args_found=FALSE;
136  s=e; // set s to the starting point of the arg
137  // and search for the end
138  // skip leading spaces:
139  loop
140  {
141  if ((*s==' ')||(*s=='\t'))
142  s++;
143  else if ((*s=='\n')&&(*(s+1)==' '))
144  s+=2;
145  else // start of new arg or \0 or )
146  break;
147  }
148  e=s;
149  while ((*e!=',')
150  &&((par!=0) || (*e!=')'))
151  &&(*e!='\0'))
152  {
153  if (*e=='(') par++;
154  else if (*e==')') par--;
155  args_found=args_found || (*e>' ');
156  e++;
157  }
158  in_args=(*e==',');
159  if (args_found)
160  {
161  *e='\0';
162  // check for space:
163  if ((int)strlen(argstr)+12 /* parameter + ;*/ +(int)strlen(s)>= argstrlen)
164  {
165  argstrlen*=2;
166  char *a=(char *)omAlloc( argstrlen);
167  strcpy(a,argstr);
168  omFree((ADDRESS)argstr);
169  argstr=a;
170  }
171  // copy the result to argstr
172  if(strncmp(s,"alias ",6)!=0)
173  {
174  strcat(argstr,"parameter ");
175  }
176  strcat(argstr,s);
177  strcat(argstr,"; ");
178  e++; // e was pointing to ','
179  }
180  } while (in_args);
181  return argstr;
182 }
183 
184 /*2
185 * locate `procname` in lib `libname` and find the part `part`:
186 * part=0: help, between, but excluding the line "proc ..." and "{...":
187 * => return
188 * part=1: body, between "{ ..." and "}", including the 1. line, w/o "{"
189 * => set pi->data.s.body, return NULL
190 * part=2: example, between, but excluding the line "exapmle {..." and "}":
191 * => return
192 */
193 char* iiGetLibProcBuffer(procinfo *pi, int part )
194 {
195  char buf[256], *s = NULL, *p;
196  long procbuflen;
197 
198  FILE * fp = feFopen( pi->libname, "rb", NULL, TRUE );
199  if (fp==NULL)
200  {
201  return NULL;
202  }
203 
204  fseek(fp, pi->data.s.proc_start, SEEK_SET);
205  if(part==0)
206  { // load help string
207  int i, offset=0;
208  long head = pi->data.s.def_end - pi->data.s.proc_start;
209  procbuflen = pi->data.s.help_end - pi->data.s.help_start;
210  if (procbuflen<5)
211  {
212  fclose(fp);
213  return NULL; // help part does not exist
214  }
215  //Print("Help=%ld-%ld=%d\n", pi->data.s.body_start,
216  // pi->data.s.proc_start, procbuflen);
217  s = (char *)omAlloc(procbuflen+head+3);
218  myfread(s, head, 1, fp);
219  s[head] = '\n';
220  fseek(fp, pi->data.s.help_start, SEEK_SET);
221  myfread(s+head+1, procbuflen, 1, fp);
222  fclose(fp);
223  s[procbuflen+head+1] = '\n';
224  s[procbuflen+head+2] = '\0';
225  offset=0;
226  for(i=0;i<=procbuflen+head+2; i++)
227  {
228  if(s[i]=='\\' &&
229  (s[i+1]=='"' || s[i+1]=='{' || s[i+1]=='}' || s[i+1]=='\\'))
230  {
231  i++;
232  offset++;
233  }
234  if(offset>0) s[i-offset] = s[i];
235  }
236  return(s);
237  }
238  else if(part==1)
239  { // load proc part - must exist
240  procbuflen = pi->data.s.def_end - pi->data.s.proc_start;
241  char *ss=(char *)omAlloc(procbuflen+2);
242  //fgets(buf, sizeof(buf), fp);
243  myfread( ss, procbuflen, 1, fp);
244  char ct;
245  char *e;
246  s=iiProcName(ss,ct,e);
247  char *argstr=NULL;
248  *e=ct;
249  argstr=iiProcArgs(e,TRUE);
250 
251  assume(pi->data.s.body_end > pi->data.s.body_start);
252 
253  procbuflen = pi->data.s.body_end - pi->data.s.body_start;
254  pi->data.s.body = (char *)omAlloc( strlen(argstr)+procbuflen+15+
255  strlen(pi->libname) );
256  //Print("Body=%ld-%ld=%d\n", pi->data.s.body_end,
257  // pi->data.s.body_start, procbuflen);
258  assume(pi->data.s.body != NULL);
259  fseek(fp, pi->data.s.body_start, SEEK_SET);
260  strcpy(pi->data.s.body,argstr);
261  myfread( pi->data.s.body+strlen(argstr), procbuflen, 1, fp);
262  fclose( fp );
263  procbuflen+=strlen(argstr);
264  omFree(argstr);
265  omFree(ss);
266  pi->data.s.body[procbuflen] = '\0';
267  strcat( pi->data.s.body+procbuflen, "\n;return();\n\n" );
268  strcat( pi->data.s.body+procbuflen+13,pi->libname);
269  s=(char *)strchr(pi->data.s.body,'{');
270  if (s!=NULL) *s=' ';
271  return NULL;
272  }
273  else if(part==2)
274  { // example
275  if ( pi->data.s.example_lineno == 0)
276  return NULL; // example part does not exist
277  // load example
278  fseek(fp, pi->data.s.example_start, SEEK_SET);
279  /*char *dummy=*/ (void) fgets(buf, sizeof(buf), fp); // skip line with "example"
280  procbuflen = pi->data.s.proc_end - pi->data.s.example_start - strlen(buf);
281  //Print("Example=%ld-%ld=%d\n", pi->data.s.proc_end,
282  // pi->data.s.example_start, procbuflen);
283  s = (char *)omAlloc(procbuflen+14);
284  myfread(s, procbuflen, 1, fp);
285  s[procbuflen] = '\0';
286  strcat(s+procbuflen-3, "\n;return();\n\n" );
287  p=(char *)strchr(s,'{');
288  if (p!=NULL) *p=' ';
289  return(s);
290  }
291  return NULL;
292 }
293 
295 {
296  int save_trace=traceit;
297  int restore_traceit=0;
298  if (traceit_stop
299  && (traceit & TRACE_SHOW_LINE))
300  {
302  traceit_stop=0;
303  restore_traceit=1;
304  }
305  // see below:
306  BITSET save1=si_opt_1;
307  BITSET save2=si_opt_2;
308  newBuffer( omStrDup(p /*pi->data.s.body*/), t /*BT_proc*/,
309  pi, l );
310  BOOLEAN err=yyparse();
311 
312  if (sLastPrinted.rtyp!=0)
313  {
315  }
316 
317  if (restore_traceit) traceit=save_trace;
318 
319  // the access to optionStruct and verboseStruct do not work
320  // on x86_64-Linux for pic-code
321  if ((TEST_V_ALLWARN) &&
322  (t==BT_proc) &&
323  ((save1!=si_opt_1)||(save2!=si_opt_2)) &&
324  (pi->libname!=NULL) && (pi->libname[0]!='\0'))
325  {
326  if ((pi->libname!=NULL) && (pi->libname[0]!='\0'))
327  Warn("option changed in proc %s from %s",pi->procname,pi->libname);
328  else
329  Warn("option changed in proc %s",pi->procname);
330  int i;
331  for (i=0; optionStruct[i].setval!=0; i++)
332  {
333  if ((optionStruct[i].setval & si_opt_1)
334  && (!(optionStruct[i].setval & save1)))
335  {
336  Print(" +%s",optionStruct[i].name);
337  }
338  if (!(optionStruct[i].setval & si_opt_1)
339  && ((optionStruct[i].setval & save1)))
340  {
341  Print(" -%s",optionStruct[i].name);
342  }
343  }
344  for (i=0; verboseStruct[i].setval!=0; i++)
345  {
346  if ((verboseStruct[i].setval & si_opt_2)
347  && (!(verboseStruct[i].setval & save2)))
348  {
349  Print(" +%s",verboseStruct[i].name);
350  }
351  if (!(verboseStruct[i].setval & si_opt_2)
352  && ((verboseStruct[i].setval & save2)))
353  {
354  Print(" -%s",verboseStruct[i].name);
355  }
356  }
357  PrintLn();
358  }
359  return err;
360 }
361 /*2
362 * start a proc
363 * parameters are built as exprlist
364 * TODO:interrupt
365 * return FALSE on success, TRUE if an error occurs
366 */
368 {
369  procinfov pi=NULL;
370  int old_echo=si_echo;
371  BOOLEAN err=FALSE;
372  char save_flags=0;
373 
374  /* init febase ======================================== */
375  /* we do not enter this case if filename != NULL !! */
376  if (pn!=NULL)
377  {
378  pi = IDPROC(pn);
379  if(pi!=NULL)
380  {
381  save_flags=pi->trace_flag;
382  if( pi->data.s.body==NULL )
383  {
385  if (pi->data.s.body==NULL) return TRUE;
386  }
387 // omUpdateInfo();
388 // int m=om_Info.UsedBytes;
389 // Print("proc %s, mem=%d\n",IDID(pn),m);
390  }
391  }
392  else return TRUE;
393  /* generate argument list ======================================*/
394  //iiCurrArgs should be NULL here, as the assignment for the parameters
395  // of the prevouis call are already done befor calling another routine
396  if (v!=NULL)
397  {
399  memcpy(iiCurrArgs,v,sizeof(sleftv)); // keeps track of v->next etc.
400  v->Init();
401  }
402  else
403  {
405  }
406  /* start interpreter ======================================*/
407  myynest++;
408  if (myynest > SI_MAX_NEST)
409  {
410  WerrorS("nesting too deep");
411  err=TRUE;
412  }
413  else
414  {
415  iiCurrProc=pn;
416  err=iiAllStart(pi,pi->data.s.body,BT_proc,pi->data.s.body_lineno-(v!=NULL));
418 
419  if (iiLocalRing[myynest-1] != currRing)
420  {
422  {
423  //idhdl hn;
424  const char *n;
425  const char *o;
426  idhdl nh=NULL, oh=NULL;
427  if (iiLocalRing[myynest-1]!=NULL)
429  if (oh!=NULL) o=oh->id;
430  else o="none";
431  if (currRing!=NULL)
432  nh=rFindHdl(currRing,NULL);
433  if (nh!=NULL) n=nh->id;
434  else n="none";
435  Werror("ring change during procedure call %s: %s -> %s (level %d)",pi->procname,o,n,myynest);
437  err=TRUE;
438  }
440  }
441  if ((currRing==NULL)
442  && (currRingHdl!=NULL))
444  else
445  if ((currRing!=NULL) &&
447  ||(IDLEV(currRingHdl)>=myynest-1)))
448  {
451  }
452  //Print("kill locals for %s (level %d)\n",IDID(pn),myynest);
454 #ifndef SING_NDEBUG
455  checkall();
456 #endif
457  //Print("end kill locals for %s (%d)\n",IDID(pn),myynest);
458  }
459  myynest--;
460  si_echo=old_echo;
461  if (pi!=NULL)
462  pi->trace_flag=save_flags;
463 // omUpdateInfo();
464 // int m=om_Info.UsedBytes;
465 // Print("exit %s, mem=%d\n",IDID(pn),m);
466  return err;
467 }
468 
472 
473 #ifdef RDEBUG
474 static void iiShowLevRings()
475 {
476  int i;
477  for (i=0;i<=myynest;i++)
478  {
479  Print("lev %d:",i);
480  if (iiLocalRing[i]==NULL) PrintS("NULL");
481  else Print("%lx",(long)iiLocalRing[i]);
482  PrintLn();
483  }
484  if (currRing==NULL) PrintS("curr:NULL\n");
485  else Print ("curr:%lx\n",(long)currRing);
486 }
487 #endif /* RDEBUG */
488 
489 static void iiCheckNest()
490 {
491  if (myynest >= iiRETURNEXPR_len-1)
492  {
494  iiRETURNEXPR_len*sizeof(ring),
495  (iiRETURNEXPR_len+16)*sizeof(ring));
496  memset(&(iiLocalRing[iiRETURNEXPR_len]),0,16*sizeof(ring));
497  iiRETURNEXPR_len+=16;
498  }
499 }
501 {
502  int err;
503  procinfov pi = IDPROC(pn);
504  if(pi->is_static && myynest==0)
505  {
506  Werror("'%s::%s()' is a local procedure and cannot be accessed by an user.",
507  pi->libname, pi->procname);
508  return TRUE;
509  }
510  iiCheckNest();
512  //Print("currRing(%d):%s(%x) in %s\n",myynest,IDID(currRingHdl),currRing,IDID(pn));
513  iiRETURNEXPR.Init();
514  procstack->push(pi->procname);
516  || (pi->trace_flag&TRACE_SHOW_PROC))
517  {
519  Print("entering%-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest);
520  }
521 #ifdef RDEBUG
523 #endif
524  switch (pi->language)
525  {
526  default:
527  case LANG_NONE:
528  WerrorS("undefined proc");
529  err=TRUE;
530  break;
531 
532  case LANG_SINGULAR:
533  if ((pi->pack!=NULL)&&(currPack!=pi->pack))
534  {
535  currPack=pi->pack;
538  //Print("set pack=%s\n",IDID(currPackHdl));
539  }
540  else if ((pack!=NULL)&&(currPack!=pack))
541  {
542  currPack=pack;
545  //Print("set pack=%s\n",IDID(currPackHdl));
546  }
547  err=iiPStart(pn,args);
548  break;
549  case LANG_C:
551  err = (pi->data.o.function)(res, args);
552  memcpy(&iiRETURNEXPR,res,sizeof(iiRETURNEXPR));
554  break;
555  }
557  || (pi->trace_flag&TRACE_SHOW_PROC))
558  {
560  Print("leaving %-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest);
561  }
562  //const char *n="NULL";
563  //if (currRingHdl!=NULL) n=IDID(currRingHdl);
564  //Print("currRing(%d):%s(%x) after %s\n",myynest,n,currRing,IDID(pn));
565 #ifdef RDEBUG
567 #endif
568  if (err)
569  {
571  //iiRETURNEXPR.Init(); //done by CleanUp
572  }
573  if (iiCurrArgs!=NULL)
574  {
575  if (!err) Warn("too many arguments for %s",IDID(pn));
576  iiCurrArgs->CleanUp();
579  }
580  procstack->pop();
581  if (err)
582  return TRUE;
583  return FALSE;
584 }
585 static void iiCallLibProcBegin()
586 {
587  idhdl tmp_ring=NULL;
588  if (currRing!=NULL)
589  {
591  {
592  // clean up things depending on currRingHdl:
594  sLastPrinted.Init();
595  }
596  // need to define a ring-hdl for currRingHdl
597  tmp_ring=enterid(" tmpRing",myynest,RING_CMD,&IDROOT,FALSE);
598  IDRING(tmp_ring)=rIncRefCnt(currRing);
599  rSetHdl(tmp_ring);
600  }
601 }
602 static void iiCallLibProcEnd(idhdl save_ringhdl, ring save_ring)
603 {
604  if ((currRing!=NULL)
605  &&(currRing!=save_ring))
606  {
608  idhdl hh=IDROOT;
609  idhdl prev=NULL;
610  while((hh!=currRingHdl) && (hh!=NULL)) { prev=hh; hh=hh->next; }
611  if (hh!=NULL)
612  {
613  if (prev==NULL) IDROOT=hh->next;
614  else prev->next=hh->next;
615  omFree((ADDRESS)IDID(hh));
617  }
618  }
619  currRingHdl=save_ringhdl;
620  currRing=save_ring;
621 }
622 
623 void* iiCallLibProc1(const char*n, void *arg, int arg_type, BOOLEAN &err)
624 {
625  idhdl h=ggetid(n);
626  if ((h==NULL)
627  || (IDTYP(h)!=PROC_CMD))
628  {
629  err=2;
630  return NULL;
631  }
632  // ring handling
633  idhdl save_ringhdl=currRingHdl;
634  ring save_ring=currRing;
636  // argument:
637  sleftv tmp;
638  tmp.Init();
639  tmp.data=arg;
640  tmp.rtyp=arg_type;
641  // call proc
642  err=iiMake_proc(h,currPack,&tmp);
643  // clean up ring
644  iiCallLibProcEnd(save_ringhdl,save_ring);
645  // return
646  if (err==FALSE)
647  {
648  void*r=iiRETURNEXPR.data;
651  return r;
652  }
653  return NULL;
654 }
655 
656 // return NULL on failure
657 ideal ii_CallProcId2Id(const char *lib,const char *proc, ideal arg, const ring R)
658 {
659  char *plib = iiConvName(lib);
660  idhdl h=ggetid(plib);
661  omFree(plib);
662  if (h==NULL)
663  {
664  BOOLEAN bo=iiLibCmd(lib,TRUE,TRUE,FALSE);
665  if (bo) return NULL;
666  }
667  ring oldR=currRing;
669  BOOLEAN err;
670  ideal I=(ideal)iiCallLibProc1(proc,idCopy(arg),IDEAL_CMD,err);
671  rChangeCurrRing(oldR);
672  if (err) return NULL;
673  return I;
674 }
675 
676 int ii_CallProcId2Int(const char *lib,const char *proc, ideal arg, const ring R)
677 {
678  char *plib = iiConvName(lib);
679  idhdl h=ggetid(plib);
680  omFree(plib);
681  if (h==NULL)
682  {
683  BOOLEAN bo=iiLibCmd(lib,TRUE,TRUE,FALSE);
684  if (bo) return 0;
685  }
686  BOOLEAN err;
687  ring oldR=currRing;
689  int I=(int)(long)iiCallLibProc1(proc,idCopy(arg),IDEAL_CMD,err);
690  rChangeCurrRing(oldR);
691  if (err) return 0;
692  return I;
693 }
694 
695 /// args: NULL terminated array of arguments
696 /// arg_types: 0 terminated array of corresponding types
697 leftv ii_CallLibProcM(const char*n, void **args, int* arg_types, const ring R, BOOLEAN &err)
698 {
699  idhdl h=ggetid(n);
700  if ((h==NULL)
701  || (IDTYP(h)!=PROC_CMD))
702  {
703  err=2;
704  return NULL;
705  }
706  // ring handling
707  idhdl save_ringhdl=currRingHdl;
708  ring save_ring=currRing;
711  // argument:
712  if (arg_types[0]!=0)
713  {
714  sleftv tmp;
715  leftv tt=&tmp;
716  int i=1;
717  tmp.Init();
718  tmp.data=args[0];
719  tmp.rtyp=arg_types[0];
720  while(arg_types[i]!=0)
721  {
723  tt=tt->next;
724  tt->rtyp=arg_types[i];
725  tt->data=args[i];
726  i++;
727  }
728  // call proc
729  err=iiMake_proc(h,currPack,&tmp);
730  }
731  else
732  // call proc
733  err=iiMake_proc(h,currPack,NULL);
734  // clean up ring
735  iiCallLibProcEnd(save_ringhdl,save_ring);
736  // return
737  if (err==FALSE)
738  {
740  memcpy(h,&iiRETURNEXPR,sizeof(sleftv));
741  iiRETURNEXPR.Init();
742  return h;
743  }
744  return NULL;
745 }
746 /*2
747 * start an example (as a proc),
748 * destroys the string 'example'
749 */
750 BOOLEAN iiEStart(char* example, procinfo *pi)
751 {
752  BOOLEAN err;
753  int old_echo=si_echo;
754 
755  iiCheckNest();
756  procstack->push(example);
759  {
760  if (traceit&TRACE_SHOW_LINENO) printf("\n");
761  printf("entering example (level %d)\n",myynest);
762  }
763  myynest++;
764 
765  err=iiAllStart(pi,example,BT_example,(pi != NULL ? pi->data.s.example_lineno: 0));
766 
768  myynest--;
769  si_echo=old_echo;
771  {
772  if (traceit&TRACE_SHOW_LINENO) printf("\n");
773  printf("leaving -example- (level %d)\n",myynest);
774  }
775  if (iiLocalRing[myynest] != currRing)
776  {
777  if (iiLocalRing[myynest]!=NULL)
778  {
781  }
782  else
783  {
785  currRing=NULL;
786  }
787  }
788  procstack->pop();
789  return err;
790 }
791 
792 
793 extern "C"
794 {
795 # define SI_GET_BUILTIN_MOD_INIT0(name) int SI_MOD_INIT0(name)(SModulFunctions*);
797 # undef SI_GET_BUILTIN_MOD_INIT0
798 };
799 
800 extern "C" int flint_mod_init(SModulFunctions* psModulFunctions);
801 
803 iiGetBuiltinModInit(const char* libname)
804 {
805 #ifdef HAVE_FLINT
806  if (strcmp(libname,"flint.so")==0) return SI_MOD_INIT0(flint);
807 #endif
808 # define SI_GET_BUILTIN_MOD_INIT(name) if (strcmp(libname, #name ".so") == 0){ return SI_MOD_INIT0(name); }
810 # undef SI_GET_BUILTIN_MOD_INIT
811 
812  return NULL;
813 }
814 
815 
816 
817 
818 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
819 BOOLEAN iiTryLoadLib(leftv v, const char *id)
820 {
821  BOOLEAN LoadResult = TRUE;
822  char libnamebuf[1024];
823  char *libname = (char *)omAlloc(strlen(id)+5);
824  const char *suffix[] = { "", ".lib", ".so", ".sl", NULL };
825  int i = 0;
826  // FILE *fp;
827  // package pack;
828  // idhdl packhdl;
829  lib_types LT;
830  for(i=0; suffix[i] != NULL; i++)
831  {
832  sprintf(libname, "%s%s", id, suffix[i]);
833  *libname = mytolower(*libname);
834  if((LT = type_of_LIB(libname, libnamebuf)) > LT_NOTFOUND)
835  {
836  #ifdef HAVE_DYNAMIC_LOADING
837  char libnamebuf[1024];
838  #endif
839 
840  if (LT==LT_SINGULAR)
841  LoadResult = iiLibCmd(libname, FALSE, FALSE,TRUE);
842  #ifdef HAVE_DYNAMIC_LOADING
843  else if ((LT==LT_ELF) || (LT==LT_HPUX))
844  LoadResult = load_modules(libname,libnamebuf,FALSE);
845  #endif
846  else if (LT==LT_BUILTIN)
847  {
848  LoadResult=load_builtin(libname,FALSE, iiGetBuiltinModInit(libname));
849  }
850  if(!LoadResult )
851  {
852  v->name = iiConvName(libname);
853  break;
854  }
855  }
856  }
857  omFree(libname);
858  return LoadResult;
859 }
860 
861 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
862 /* check, if library lib has already been loaded
863  if yes, writes filename of lib into where and returns TRUE,
864  no, returns FALSE
865 */
866 BOOLEAN iiLocateLib(const char* lib, char* where)
867 {
868  char *plib = iiConvName(lib);
869  idhdl pl = basePack->idroot->get(plib,0);
870  if( (pl!=NULL) && (IDTYP(pl)==PACKAGE_CMD) &&
871  (IDPACKAGE(pl)->language == LANG_SINGULAR))
872  {
873  strncpy(where,IDPACKAGE(pl)->libname,127);
874  return TRUE;
875  }
876  else
877  return FALSE;;
878 }
879 
880 BOOLEAN iiLibCmd( const char *newlib, BOOLEAN autoexport, BOOLEAN tellerror, BOOLEAN force )
881 {
882  if (strcmp(newlib,"Singular")==0) return FALSE;
883  char libnamebuf[1024];
884  idhdl pl;
885  char *plib = iiConvName(newlib);
886  FILE * fp = feFopen( newlib, "r", libnamebuf, tellerror );
887  // int lines = 1;
888  BOOLEAN LoadResult = TRUE;
889 
890  if (fp==NULL)
891  {
892  return TRUE;
893  }
894  pl = basePack->idroot->get(plib,0);
895  if (pl==NULL)
896  {
897  pl = enterid( plib,0, PACKAGE_CMD,
898  &(basePack->idroot), TRUE );
899  IDPACKAGE(pl)->language = LANG_SINGULAR;
900  IDPACKAGE(pl)->libname=omStrDup(newlib);
901  }
902  else
903  {
904  if(IDTYP(pl)!=PACKAGE_CMD)
905  {
906  omFree(plib);
907  WarnS("not of type package.");
908  fclose(fp);
909  return TRUE;
910  }
911  if (!force)
912  {
913  omFree(plib);
914  return FALSE;
915  }
916  }
917  LoadResult = iiLoadLIB(fp, libnamebuf, newlib, pl, autoexport, tellerror);
918 
919  if(!LoadResult) IDPACKAGE(pl)->loaded = TRUE;
920  omFree((ADDRESS)plib);
921  return LoadResult;
922 }
923 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
924 static void iiCleanProcs(idhdl &root)
925 {
926  idhdl prev=NULL;
927  loop
928  {
929  if (root==NULL) return;
930  if (IDTYP(root)==PROC_CMD)
931  {
932  procinfo *pi=(procinfo*)IDDATA(root);
933  if ((pi->language == LANG_SINGULAR)
934  && (pi->data.s.body_start == 0L))
935  {
936  // procinfo data incorrect:
937  // - no proc body can start at the beginning of the file
938  killhdl(root);
939  if (prev==NULL)
940  root=IDROOT;
941  else
942  {
943  root=prev;
944  prev=NULL;
945  }
946  continue;
947  }
948  }
949  prev=root;
950  root=IDNEXT(root);
951  }
952 }
953 static void iiRunInit(package p)
954 {
955  idhdl h=p->idroot->get("mod_init",0);
956  if (h==NULL) return;
957  if (IDTYP(h)==PROC_CMD)
958  {
959  int save=yylineno;
960  myynest++;
961  // procinfo *pi=(procinfo*)IDDATA(h);
962  //PrintS("mod_init found\n");
963  iiMake_proc(h,p,NULL);
964  myynest--;
965  yylineno=save;
966  }
967 }
968 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
969 BOOLEAN iiLoadLIB(FILE *fp, const char *libnamebuf, const char*newlib,
970  idhdl pl, BOOLEAN autoexport, BOOLEAN tellerror)
971 {
972  EXTERN_VAR FILE *yylpin;
973  libstackv ls_start = library_stack;
974  lib_style_types lib_style;
975 
976  yylpin = fp;
977  #if YYLPDEBUG > 1
978  print_init();
979  #endif
980  EXTERN_VAR int lpverbose;
982  else lpverbose=0;
983  // yylplex sets also text_buffer
984  if (text_buffer!=NULL) *text_buffer='\0';
985  yylplex(newlib, libnamebuf, &lib_style, pl, autoexport);
986  if(yylp_errno)
987  {
988  Werror("Library %s: ERROR occurred: in line %d, %d.", newlib, yylplineno,
989  current_pos(0));
991  {
995  }
996  else
998  WerrorS("Cannot load library,... aborting.");
999  reinit_yylp();
1000  fclose( yylpin );
1002  return TRUE;
1003  }
1004  if (BVERBOSE(V_LOAD_LIB))
1005  Print( "// ** loaded %s %s\n", libnamebuf, text_buffer);
1006  if( (lib_style == OLD_LIBSTYLE) && (BVERBOSE(V_LOAD_LIB)))
1007  {
1008  Warn( "library %s has old format. This format is still accepted,", newlib);
1009  WarnS( "but for functionality you may wish to change to the new");
1010  WarnS( "format. Please refer to the manual for further information.");
1011  }
1012  reinit_yylp();
1013  fclose( yylpin );
1014  fp = NULL;
1015  iiRunInit(IDPACKAGE(pl));
1016 
1017  {
1018  libstackv ls;
1019  for(ls = library_stack; (ls != NULL) && (ls != ls_start); )
1020  {
1021  if(ls->to_be_done)
1022  {
1023  ls->to_be_done=FALSE;
1024  iiLibCmd(ls->get(),autoexport,tellerror,FALSE);
1025  ls = ls->pop(newlib);
1026  }
1027  }
1028 #if 0
1029  PrintS("--------------------\n");
1030  for(ls = library_stack; ls != NULL; ls = ls->next)
1031  {
1032  Print("%s: LIB-stack:(%d), %s %s\n", newlib, ls->cnt, ls->get(),
1033  ls->to_be_done ? "not loaded" : "loaded");
1034  }
1035  PrintS("--------------------\n");
1036 #endif
1037  }
1038 
1039  if(fp != NULL) fclose(fp);
1040  return FALSE;
1041 }
1042 
1043 
1044 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1046  const char *procname, int, long pos, BOOLEAN pstatic)
1047 {
1048  memset(pi,0,sizeof(*pi));
1049  pi->libname = omStrDup(libname);
1050  pi->procname = omStrDup(procname);
1051  pi->language = LANG_SINGULAR;
1052  pi->ref = 1;
1053  pi->is_static = pstatic;
1054  pi->data.s.proc_start = pos;
1055  return(pi);
1056 }
1057 
1058 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1059 int iiAddCproc(const char *libname, const char *procname, BOOLEAN pstatic,
1060  BOOLEAN(*func)(leftv res, leftv v))
1061 {
1062  procinfov pi;
1063  idhdl h;
1064 
1065  #ifndef SING_NDEBUG
1066  int dummy;
1067  if (IsCmd(procname,dummy))
1068  {
1069  Werror(">>%s< is a reserved name",procname);
1070  return 0;
1071  }
1072  #endif
1073 
1074  h=IDROOT->get(procname,0);
1075  if ((h!=NULL)
1076  && (IDTYP(h)==PROC_CMD))
1077  {
1078  pi = IDPROC(h);
1079  #if 0
1080  if ((pi->language == LANG_SINGULAR)
1081  &&(BVERBOSE(V_REDEFINE)))
1082  Warn("extend `%s`",procname);
1083  #endif
1084  }
1085  else
1086  {
1087  h = enterid(procname,0, PROC_CMD, &IDROOT, TRUE);
1088  }
1089  if ( h!= NULL )
1090  {
1091  pi = IDPROC(h);
1092  if((pi->language == LANG_SINGULAR)
1093  ||(pi->language == LANG_NONE))
1094  {
1095  omfree(pi->libname);
1096  pi->libname = omStrDup(libname);
1097  omfree(pi->procname);
1098  pi->procname = omStrDup(procname);
1099  pi->language = LANG_C;
1100  pi->ref = 1;
1101  pi->is_static = pstatic;
1102  pi->data.o.function = func;
1103  }
1104  else if(pi->language == LANG_C)
1105  {
1106  if(pi->data.o.function == func)
1107  {
1108  pi->ref++;
1109  }
1110  else
1111  {
1112  omfree(pi->libname);
1113  pi->libname = omStrDup(libname);
1114  omfree(pi->procname);
1115  pi->procname = omStrDup(procname);
1116  pi->language = LANG_C;
1117  pi->ref = 1;
1118  pi->is_static = pstatic;
1119  pi->data.o.function = func;
1120  }
1121  }
1122  else
1123  Warn("internal error: unknown procedure type %d",pi->language);
1124  if (currPack->language==LANG_SINGULAR) currPack->language=LANG_MIX;
1125  return(1);
1126  }
1127  else
1128  {
1129  WarnS("iiAddCproc: failed.");
1130  }
1131  return(0);
1132 }
1133 
1134 int iiAddCprocTop(const char *libname, const char *procname, BOOLEAN pstatic,
1135  BOOLEAN(*func)(leftv res, leftv v))
1136 {
1137  int r=iiAddCproc(libname,procname,pstatic,func);
1138  package s=currPack;
1140  if (r) r=iiAddCproc(libname,procname,pstatic,func);
1141  currPack=s;
1142  return r;
1143 }
1144 
1145 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1146 #ifdef HAVE_DYNAMIC_LOADING
1147 #include <map>
1148 #include <string>
1149 #include <pthread.h>
1150 
1151 THREAD_VAR std::map<std::string, void *> *dyn_modules;
1152 
1153 bool registered_dyn_module(char *fullname) {
1154  if (dyn_modules == NULL)
1155  return false;
1156  std::string fname = fullname;
1157  return dyn_modules->count(fname) != 0;
1158 }
1159 
1160 void register_dyn_module(char *fullname, void * handle) {
1161  std::string fname = fullname;
1162  if (dyn_modules == NULL)
1163  dyn_modules = new std::map<std::string, void *>();
1164  dyn_modules->insert(std::pair<std::string, void *>(fname, handle));
1165 }
1166 
1168  for (std::map<std::string, void *>::iterator it = dyn_modules->begin();
1169  it != dyn_modules->end();
1170  it++)
1171  {
1172  dynl_close(it->second);
1173  }
1174  delete dyn_modules;
1175  dyn_modules = NULL;
1176 }
1177 BOOLEAN load_modules_aux(const char *newlib, char *fullname, BOOLEAN autoexport)
1178 {
1179 #ifdef HAVE_STATIC
1180  WerrorS("mod_init: static version can not load modules");
1181  return TRUE;
1182 #else
1183 /*
1184  typedef int (*fktn_t)(int(*iiAddCproc)(const char *libname, const char *procname,
1185  BOOLEAN pstatic,
1186  BOOLEAN(*func)(leftv res, leftv v)));
1187 */
1188  SModulFunc_t fktn;
1189  idhdl pl;
1190  char *plib = iiConvName(newlib);
1191  BOOLEAN RET=TRUE;
1192  int token;
1193  char FullName[256];
1194 
1195  memset(FullName,0,sizeof(FullName));
1196 
1197  if( *fullname != '/' && *fullname != '.' )
1198  sprintf(FullName, "./%s", newlib);
1199  else strncpy(FullName, fullname,255);
1200 
1201 
1202  if(IsCmd(plib, token))
1203  {
1204  Werror("'%s' is resered identifier\n", plib);
1205  goto load_modules_end;
1206  }
1207  pl = basePack->idroot->get(plib,0); /* packages only in top level
1208  (see enterid) */
1209  if ((pl!=NULL)
1210  &&(IDTYP(pl)==PACKAGE_CMD))
1211  {
1212  if(IDPACKAGE(pl)->language==LANG_C)
1213  {
1214  if (BVERBOSE(V_LOAD_LIB)) Warn( "%s already loaded as package", newlib);
1215  omFree(plib);
1216  return FALSE;
1217  }
1218  else if(IDPACKAGE(pl)->language==LANG_MIX)
1219  {
1220  if (BVERBOSE(V_LOAD_LIB)) Warn( "%s contain binary parts, cannot load", newlib);
1221  omFree(plib);
1222  return FALSE;
1223  }
1224  }
1225  else
1226  {
1227  pl = enterid( plib,0, PACKAGE_CMD, &IDROOT, TRUE );
1228  omFree(plib); /* enterid copied plib*/
1229  IDPACKAGE(pl)->libname=omStrDup(newlib);
1230  }
1231  IDPACKAGE(pl)->language = LANG_C;
1232  if (dynl_check_opened(FullName))
1233  {
1234  if (BVERBOSE(V_LOAD_LIB)) Warn( "%s already loaded as C library", fullname);
1235  return FALSE;
1236  }
1237  if((IDPACKAGE(pl)->handle=dynl_open(FullName))==(void *)NULL)
1238  {
1239  Werror("dynl_open failed:%s", dynl_error());
1240  Werror("%s not found", newlib);
1241  killhdl2(pl,&(basePack->idroot),NULL); // remove package
1242  goto load_modules_end;
1243  }
1244  else
1245  {
1246  SModulFunctions sModulFunctions;
1247 
1248  package s=currPack;
1249  currPack=IDPACKAGE(pl);
1250  fktn = (SModulFunc_t)dynl_sym(IDPACKAGE(pl)->handle, "mod_init");
1251  if( fktn!= NULL)
1252  {
1253  sModulFunctions.iiArithAddCmd = iiArithAddCmd;
1254  if (autoexport) sModulFunctions.iiAddCproc = iiAddCprocTop;
1255  else sModulFunctions.iiAddCproc = iiAddCproc;
1256  int ver=(*fktn)(&sModulFunctions);
1257  if (ver==MAX_TOK)
1258  {
1259  if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loaded %s\n", fullname);
1260  }
1261  else
1262  {
1263  Warn("loaded %s for a different version of Singular(expected MAX_TOK: %d, got %d)",fullname,MAX_TOK,ver);
1264  }
1265  currPack->loaded=1;
1266  currPack=s; /* reset currPack to previous */
1267  register_dyn_module(fullname, IDPACKAGE(pl)->handle);
1268  RET=FALSE;
1269  }
1270  else
1271  {
1272  Werror("mod_init not found:: %s\nThis is probably not a dynamic module for Singular!\n", dynl_error());
1273  errorreported=0;
1274  if(IDPACKAGE(pl)->idroot==NULL)
1275  killhdl2(pl,&(basePack->idroot),NULL); // remove package
1276  }
1277  }
1278 
1279  load_modules_end:
1280  return RET;
1281 #endif /*STATIC */
1282 }
1283 
1284 BOOLEAN load_modules(const char *newlib, char *fullname, BOOLEAN autoexport)
1285 {
1286  GLOBAL_VAR static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1287  pthread_mutex_lock(&mutex);
1288  BOOLEAN r = load_modules_aux(newlib, fullname, autoexport);
1289  pthread_mutex_unlock(&mutex);
1290  return r;
1291 }
1292 #endif /* HAVE_DYNAMIC_LOADING */
1293 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1294 BOOLEAN load_builtin(const char *newlib, BOOLEAN autoexport, SModulFunc_t init)
1295 {
1296  int iiAddCproc(const char *libname, const char *procname, BOOLEAN pstatic,
1297  BOOLEAN(*func)(leftv res, leftv v));
1298 /*
1299  typedef int (*fktn_t)(int(*iiAddCproc)(const char *libname, const char *procname,
1300  BOOLEAN pstatic,
1301  BOOLEAN(*func)(leftv res, leftv v)));
1302 */
1303  // SModulFunc_t fktn;
1304  idhdl pl;
1305  char *plib = iiConvName(newlib);
1306  // BOOLEAN RET=TRUE;
1307  // int token;
1308 
1309  pl = basePack->idroot->get(plib,0); // search PACKAGE only in Top
1310  if ((pl!=NULL)
1311  &&(IDTYP(pl)==PACKAGE_CMD))
1312  {
1313  if(IDPACKAGE(pl)->language==LANG_C)
1314  {
1315  if (BVERBOSE(V_LOAD_LIB)) Warn( "(builtin) %s already loaded", newlib);
1316  omFree(plib);
1317  return FALSE;
1318  }
1319  }
1320  else
1321  {
1322  pl = enterid( plib,0, PACKAGE_CMD, &IDROOT, TRUE );
1323  IDPACKAGE(pl)->libname=omStrDup(newlib);
1324  }
1325  omFree(plib);
1326  IDPACKAGE(pl)->language = LANG_C;
1327 
1328  IDPACKAGE(pl)->handle=(void *)NULL;
1329  SModulFunctions sModulFunctions;
1330 
1331  package s=currPack;
1332  currPack=IDPACKAGE(pl);
1333  if( init!= NULL)
1334  {
1335  sModulFunctions.iiArithAddCmd = iiArithAddCmd;
1336  if (autoexport) sModulFunctions.iiAddCproc = iiAddCprocTop;
1337  else sModulFunctions.iiAddCproc = iiAddCproc;
1338  (*init)(&sModulFunctions);
1339  }
1340  if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loaded (builtin) %s \n", newlib);
1341  currPack->loaded=1;
1342  currPack=s;
1343 
1344  return FALSE;
1345 }
1346 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1347 void module_help_main(const char *newlib,const char *help)
1348 {
1349  char *plib = iiConvName(newlib);
1350  idhdl pl = basePack->idroot->get(plib,0);
1351  if ((pl==NULL)||(IDTYP(pl)!=PACKAGE_CMD))
1352  Werror(">>%s<< is not a package (trying to add package help)",plib);
1353  else
1354  {
1355  package s=currPack;
1356  currPack=IDPACKAGE(pl);
1357  idhdl h=enterid("info",0,STRING_CMD,&IDROOT,FALSE);
1358  IDSTRING(h)=omStrDup(help);
1359  currPack=s;
1360  }
1361 }
1362 void module_help_proc(const char *newlib,const char *p, const char *help)
1363 {
1364  char *plib = iiConvName(newlib);
1365  idhdl pl = basePack->idroot->get(plib,0);
1366  if ((pl==NULL)||(IDTYP(pl)!=PACKAGE_CMD))
1367  Werror(">>%s<< is not a package(trying to add help for %s)",plib,p);
1368  else
1369  {
1370  package s=currPack;
1371  currPack=IDPACKAGE(pl);
1372  char buff[256];
1373  buff[255]='\0';
1374  strncpy(buff,p,255);
1375  strncat(buff,"_help",255-strlen(p));
1376  idhdl h=enterid(buff,0,STRING_CMD,&IDROOT,FALSE);
1377  IDSTRING(h)=omStrDup(help);
1378  currPack=s;
1379  }
1380 }
1381 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1382 
1383 #ifdef HAVE_DYNAMIC_LOADING
1384 // loads a dynamic module from the binary path and returns a named function
1385 // returns NULL, if something fails
1386 void* binary_module_function(const char* newlib, const char* funcname)
1387 {
1388  void* result = NULL;
1389 
1390  const char* bin_dir = feGetResource('b');
1391  if (!bin_dir) { return NULL; }
1392 
1393  char path_name[MAXPATHLEN];
1394  sprintf(path_name, "%s%s%s.%s", bin_dir, DIR_SEPP, newlib, MODULE_SUFFIX_STRING);
1395 
1396  void* openlib = dynl_open(path_name);
1397  if(!openlib)
1398  {
1399  Werror("dynl_open of %s failed:%s", path_name, dynl_error());
1400  return NULL;
1401  }
1402  result = dynl_sym(openlib, funcname);
1403  if (!result) Werror("%s: %s\n", funcname, dynl_error());
1404 
1405  return result;
1406 }
1407 #endif
1408 
1409 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1410 char mytoupper(char c)
1411 {
1412  if(c>=97 && c<=(97+26)) c-=32;
1413  return(c);
1414 }
1415 
1416 char mytolower(char c)
1417 {
1418  if(c>=65 && c<=(65+26)) c+=32;
1419  return(c);
1420 }
1421 
1422 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1423 //#if defined(WINNT)
1424 //# define FS_SEP '\\'
1425 //#else
1426 //# define FS_SEP '/'
1427 //#endif
1428 
1429 char *iiConvName(const char *libname)
1430 {
1431  char *tmpname = omStrDup(libname);
1432  char *p = strrchr(tmpname, DIR_SEP);
1433  char *r;
1434  if(p==NULL) p = tmpname; else p++;
1435  // p is now the start of the file name (without path)
1436  r=p;
1437  while(isalnum(*r)||(*r=='_')) r++;
1438  // r point the the end of the main part of the filename
1439  *r = '\0';
1440  r = omStrDup(p);
1441  *r = mytoupper(*r);
1442  // printf("iiConvName: '%s' '%s' => '%s'\n", libname, tmpname, r);
1443  omFree((ADDRESS)tmpname);
1444 
1445  return(r);
1446 }
1447 
1448 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1449 #if 0 /* debug only */
1450 void piShowProcList()
1451 {
1452  idhdl h;
1453  procinfo *proc;
1454  char *name;
1455 
1456  Print( "%-15s %20s %s,%s %s,%s %s,%s\n", "Library", "function",
1457  "line", "start", "line", "body", "line", "example");
1458  for(h = IDROOT; h != NULL; h = IDNEXT(h))
1459  {
1460  if(IDTYP(h) == PROC_CMD)
1461  {
1462  proc = IDPROC(h);
1463  if(strcmp(proc->procname, IDID(h))!=0)
1464  {
1465  name = (char *)omAlloc(strlen(IDID(h))+strlen(proc->procname)+4);
1466  sprintf(name, "%s -> %s", IDID(h), proc->procname);
1467  Print( "%d %-15s %20s ", proc->is_static ? 1 : 0, proc->libname, name);
1468  omFree((ADDRESS)name);
1469  }
1470  else
1471  Print( "%d %-15s %20s ", proc->is_static ? 1 : 0, proc->libname,
1472  proc->procname);
1473  if(proc->language==LANG_SINGULAR)
1474  Print("line %-5ld %4d,%-5ld %4d,%-5ld\n",
1475  proc->data.s.proc_start,
1476  proc->data.s.body_lineno, proc->data.s.body_start,
1477  proc->data.s.example_lineno, proc->data.s.example_start);
1478  else if(proc->language==LANG_C)
1479  PrintS("type: object\n");
1480  }
1481  }
1482 }
1483 #endif
1484 
1485 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1486 //char *iiLineNo(char *procname, int lineno)
1487 //{
1488 // char buf[256];
1489 // idhdl pn = ggetid(procname);
1490 // procinfo *pi = IDPROC(pn);
1491 //
1492 // sprintf(buf, "%s %3d\0", procname, lineno);
1493 // //sprintf(buf, "%s::%s %3d\0", pi->libname, pi->procname,
1494 // // lineno + pi->data.s.body_lineno);
1495 // return(buf);
1496 //}
1497 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1498 #ifdef HAVE_LIBPARSER
1499 void libstack::push(const char */*p*/, char *libn)
1500 {
1501  libstackv lp;
1502  if( !iiGetLibStatus(libn))
1503  {
1504  for(lp = this;lp!=NULL;lp=lp->next)
1505  {
1506  if(strcmp(lp->get(), libn)==0) break;
1507  }
1508  if(lp==NULL)
1509  {
1511  ls->next = this;
1512  ls->libname = omStrDup(libn);
1513  ls->to_be_done = TRUE;
1514  if(library_stack != NULL) ls->cnt = library_stack->cnt+1; else ls->cnt = 0;
1515  library_stack = ls;
1516  }
1517  }
1518 }
1519 
1520 libstackv libstack::pop(const char */*p*/)
1521 {
1522  libstackv ls = this;
1523  //omFree((ADDRESS)ls->libname);
1524  library_stack = ls->next;
1526  return(library_stack);
1527 }
1528 
1529 #endif /* HAVE_LIBPARSER */
1530 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
int BOOLEAN
Definition: auxiliary.h:87
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
void * ADDRESS
Definition: auxiliary.h:119
CanonicalForm head(const CanonicalForm &f)
int l
Definition: cfEzgcd.cc:100
int i
Definition: cfEzgcd.cc:132
int p
Definition: cfModGcd.cc:4080
CanonicalForm fp
Definition: cfModGcd.cc:4104
unsigned char * proc[NUM_PROC]
Definition: checklibs.c:16
char name() const
Definition: variable.cc:122
Definition: idrec.h:35
idhdl next
Definition: idrec.h:38
const char * id
Definition: idrec.h:39
void push(const char *p, char *libname)
Definition: iplib.cc:1499
libstackv next
Definition: subexpr.h:164
libstackv pop(const char *p)
Definition: iplib.cc:1520
int cnt
Definition: subexpr.h:167
char * get()
Definition: subexpr.h:170
char * libname
Definition: subexpr.h:165
BOOLEAN to_be_done
Definition: subexpr.h:166
void pop()
Definition: ipid.cc:805
void push(char *)
Definition: ipid.cc:795
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
int rtyp
Definition: subexpr.h:91
void Init()
Definition: subexpr.h:107
BOOLEAN RingDependend()
Definition: subexpr.cc:418
leftv next
Definition: subexpr.h:86
void * data
Definition: subexpr.h:88
void CleanUp(ring r=currRing)
Definition: subexpr.cc:348
#define Print
Definition: emacs.cc:80
#define Warn
Definition: emacs.cc:77
#define WarnS
Definition: emacs.cc:78
return result
Definition: facAbsBiFact.cc:75
const CanonicalForm int s
Definition: facAbsFact.cc:51
CanonicalForm res
Definition: facAbsFact.cc:60
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:39
char name(const Variable &v)
Definition: factory.h:196
VAR short errorreported
Definition: feFopen.cc:23
void WerrorS(const char *s)
Definition: feFopen.cc:24
FILE * feFopen(const char *path, const char *mode, char *where, short useWerror, short path_only)
Definition: feFopen.cc:47
size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition: feFopen.cc:195
char * feGetResource(const char id, int warn)
Definition: feResource.cc:155
#define DIR_SEPP
Definition: feResource.h:7
#define DIR_SEP
Definition: feResource.h:6
VAR int yylineno
Definition: febase.cc:40
VAR int si_echo
Definition: febase.cc:35
VAR int myynest
Definition: febase.cc:41
void newBuffer(char *s, feBufferTypes t, procinfo *pi, int lineno)
Definition: fevoices.cc:164
feBufferTypes
Definition: fevoices.h:17
@ BT_example
Definition: fevoices.h:21
@ BT_proc
Definition: fevoices.h:20
#define THREAD_VAR
Definition: globaldefs.h:12
#define GLOBAL_VAR
Definition: globaldefs.h:11
#define EXTERN_VAR
Definition: globaldefs.h:6
#define INST_VAR
Definition: globaldefs.h:8
#define VAR
Definition: globaldefs.h:5
@ IDEAL_CMD
Definition: grammar.cc:284
@ PROC_CMD
Definition: grammar.cc:280
@ RING_CMD
Definition: grammar.cc:281
int yyparse(void)
Definition: grammar.cc:2111
ideal idCopy(ideal A)
Definition: ideals.h:60
int IsCmd(const char *n, int &tok)
Definition: iparith.cc:9447
idhdl ggetid(const char *n)
Definition: ipid.cc:571
void killhdl2(idhdl h, idhdl *ih, ring r)
Definition: ipid.cc:437
idhdl enterid(const char *s, int lev, int t, idhdl *root, BOOLEAN init, BOOLEAN search)
Definition: ipid.cc:278
VAR package basePack
Definition: ipid.cc:58
VAR omBin idrec_bin
Definition: ipid.cc:48
VAR proclevel * procstack
Definition: ipid.cc:52
VAR idhdl currRingHdl
Definition: ipid.cc:59
VAR package currPack
Definition: ipid.cc:57
void killhdl(idhdl h, package proot)
Definition: ipid.cc:406
VAR idhdl currPackHdl
Definition: ipid.cc:55
idhdl packFindHdl(package r)
Definition: ipid.cc:823
#define IDSTRING(a)
Definition: ipid.h:136
#define IDNEXT(a)
Definition: ipid.h:118
EXTERN_VAR omBin sleftv_bin
Definition: ipid.h:145
#define IDDATA(a)
Definition: ipid.h:126
const struct soptionStruct verboseStruct[]
Definition: misc_ip.cc:550
#define IDPROC(a)
Definition: ipid.h:140
#define IDID(a)
Definition: ipid.h:122
#define IDROOT
Definition: ipid.h:19
#define IDPACKAGE(a)
Definition: ipid.h:139
#define IDLEV(a)
Definition: ipid.h:121
unsigned setval
Definition: ipid.h:153
int(* SModulFunc_t)(SModulFunctions *)
Definition: ipid.h:81
#define IDRING(a)
Definition: ipid.h:127
const struct soptionStruct optionStruct[]
Definition: misc_ip.cc:519
#define IDTYP(a)
Definition: ipid.h:119
int iiAddCproc(const char *libname, const char *procname, BOOLEAN pstatic, BOOLEAN(*func)(leftv res, leftv v))
Definition: iplib.cc:1059
char * iiProcName(char *buf, char &ct, char *&e)
Definition: iplib.cc:96
#define SI_MAX_NEST
Definition: iplib.cc:23
void yylprestart(FILE *input_file)
bool registered_dyn_module(char *fullname)
Definition: iplib.cc:1153
ideal ii_CallProcId2Id(const char *lib, const char *proc, ideal arg, const ring R)
Definition: iplib.cc:657
void register_dyn_module(char *fullname, void *handle)
Definition: iplib.cc:1160
BOOLEAN iiLoadLIB(FILE *fp, const char *libnamebuf, const char *newlib, idhdl pl, BOOLEAN autoexport, BOOLEAN tellerror)
Definition: iplib.cc:969
void * iiCallLibProc1(const char *n, void *arg, int arg_type, BOOLEAN &err)
Definition: iplib.cc:623
VAR int iiRETURNEXPR_len
Definition: iplib.cc:471
char * iiConvName(const char *libname)
Definition: iplib.cc:1429
static void iiCallLibProcEnd(idhdl save_ringhdl, ring save_ring)
Definition: iplib.cc:602
BOOLEAN iiEStart(char *example, procinfo *pi)
Definition: iplib.cc:750
#define SI_GET_BUILTIN_MOD_INIT0(name)
Definition: iplib.cc:795
int flint_mod_init(SModulFunctions *psModulFunctions)
Definition: misc_ip.cc:1269
BOOLEAN load_modules_aux(const char *newlib, char *fullname, BOOLEAN autoexport)
Definition: iplib.cc:1177
static void iiCheckNest()
Definition: iplib.cc:489
int current_pos(int i=0)
Definition: libparse.cc:3346
#define SI_GET_BUILTIN_MOD_INIT(name)
BOOLEAN iiLibCmd(const char *newlib, BOOLEAN autoexport, BOOLEAN tellerror, BOOLEAN force)
Definition: iplib.cc:880
BOOLEAN iiGetLibStatus(const char *lib)
Definition: iplib.cc:73
leftv ii_CallLibProcM(const char *n, void **args, int *arg_types, const ring R, BOOLEAN &err)
args: NULL terminated array of arguments arg_types: 0 terminated array of corresponding types
Definition: iplib.cc:697
void print_init()
Definition: libparse.cc:3482
BOOLEAN iiMake_proc(idhdl pn, package pack, leftv args)
Definition: iplib.cc:500
BOOLEAN iiTryLoadLib(leftv v, const char *id)
Definition: iplib.cc:819
procinfo * iiInitSingularProcinfo(procinfov pi, const char *libname, const char *procname, int, long pos, BOOLEAN pstatic)
Definition: iplib.cc:1045
#define MODULE_SUFFIX_STRING
Definition: iplib.cc:38
static void iiCleanProcs(idhdl &root)
Definition: iplib.cc:924
BOOLEAN load_modules(const char *newlib, char *fullname, BOOLEAN autoexport)
Definition: iplib.cc:1284
SI_FOREACH_BUILTIN(SI_GET_BUILTIN_MOD_INIT0) }
int ii_CallProcId2Int(const char *lib, const char *proc, ideal arg, const ring R)
Definition: iplib.cc:676
THREAD_VAR std::map< std::string, void * > * dyn_modules
Definition: iplib.cc:1151
VAR libstackv library_stack
Definition: iplib.cc:64
int iiAddCprocTop(const char *libname, const char *procname, BOOLEAN pstatic, BOOLEAN(*func)(leftv res, leftv v))
Definition: iplib.cc:1134
INST_VAR sleftv iiRETURNEXPR
Definition: iplib.cc:470
void close_all_dyn_modules()
Definition: iplib.cc:1167
char mytolower(char c)
Definition: iplib.cc:1416
const char * yylp_errlist[]
Definition: libparse.cc:1114
BOOLEAN load_builtin(const char *newlib, BOOLEAN autoexport, SModulFunc_t init)
Definition: iplib.cc:1294
EXTERN_VAR int yylplineno
Definition: iplib.cc:61
char * iiProcArgs(char *e, BOOLEAN withParenth)
Definition: iplib.cc:110
BOOLEAN iiLocateLib(const char *lib, char *where)
Definition: iplib.cc:866
SModulFunc_t iiGetBuiltinModInit(const char *libname)
Definition: iplib.cc:803
VAR ring * iiLocalRing
Definition: iplib.cc:469
void module_help_main(const char *newlib, const char *help)
Definition: iplib.cc:1347
static void iiShowLevRings()
Definition: iplib.cc:474
BOOLEAN iiAllStart(procinfov pi, const char *p, feBufferTypes t, int l)
Definition: iplib.cc:294
void * binary_module_function(const char *newlib, const char *funcname)
Definition: iplib.cc:1386
static void iiCallLibProcBegin()
Definition: iplib.cc:585
void module_help_proc(const char *newlib, const char *p, const char *help)
Definition: iplib.cc:1362
static void iiRunInit(package p)
Definition: iplib.cc:953
char * iiGetLibProcBuffer(procinfo *pi, int part)
Definition: iplib.cc:193
EXTERN_VAR int yylp_errno
Definition: iplib.cc:60
BOOLEAN iiPStart(idhdl pn, leftv v)
Definition: iplib.cc:367
char mytoupper(char c)
Definition: iplib.cc:1410
int iiArithAddCmd(const char *szName, short nAlias, short nTokval, short nToktype, short nPos)
Definition: iparith.cc:9792
VAR idhdl iiCurrProc
Definition: ipshell.cc:79
void iiCheckPack(package &p)
Definition: ipshell.cc:1561
void killlocals(int v)
Definition: ipshell.cc:384
VAR leftv iiCurrArgs
Definition: ipshell.cc:78
idhdl rFindHdl(ring r, idhdl n)
Definition: ipshell.cc:1632
void rSetHdl(idhdl h)
Definition: ipshell.cc:5135
STATIC_VAR int offset
Definition: janet.cc:29
STATIC_VAR Poly * h
Definition: janet.cc:971
#define pi
Definition: libparse.cc:1145
#define help
Definition: libparse.cc:1230
#define string
Definition: libparse.cc:1252
void reinit_yylp()
Definition: libparse.cc:3376
VAR char * text_buffer
Definition: libparse.cc:1099
VAR int lpverbose
Definition: libparse.cc:1106
VAR char libnamebuf[1024]
Definition: libparse.cc:1098
lib_style_types
Definition: libparse.h:9
@ OLD_LIBSTYLE
Definition: libparse.h:9
#define YYLP_BAD_CHAR
Definition: libparse.h:93
int yylplex(const char *libname, const char *libfile, lib_style_types *lib_style, idhdl pl, BOOLEAN autoexport=FALSE, lp_modes=LOAD_LIB)
#define SEEK_SET
Definition: mod2.h:113
#define assume(x)
Definition: mod2.h:387
lib_types type_of_LIB(const char *newlib, char *libnamebuf)
Definition: mod_lib.cc:27
#define SI_MOD_INIT0(name)
Definition: mod_lib.h:4
void * dynl_sym(void *handle, const char *symbol)
Definition: mod_raw.cc:162
int dynl_check_opened(char *filename)
Definition: mod_raw.cc:138
const char * dynl_error()
Definition: mod_raw.cc:178
int dynl_close(void *handle)
Definition: mod_raw.cc:173
void * dynl_open(char *filename)
Definition: mod_raw.cc:145
lib_types
Definition: mod_raw.h:16
@ LT_HPUX
Definition: mod_raw.h:16
@ LT_SINGULAR
Definition: mod_raw.h:16
@ LT_BUILTIN
Definition: mod_raw.h:16
@ LT_ELF
Definition: mod_raw.h:16
@ LT_NOTFOUND
Definition: mod_raw.h:16
void init()
Definition: lintree.cc:864
#define omStrDup(s)
Definition: omAllocDecl.h:263
#define omfree(addr)
Definition: omAllocDecl.h:237
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omreallocSize(addr, o_size, size)
Definition: omAllocDecl.h:231
#define omAllocBin(bin)
Definition: omAllocDecl.h:205
#define omAlloc0Bin(bin)
Definition: omAllocDecl.h:206
#define omFree(addr)
Definition: omAllocDecl.h:261
#define omFreeBin(addr, bin)
Definition: omAllocDecl.h:259
#define NULL
Definition: omList.c:12
#define MAXPATHLEN
Definition: omRet2Info.c:22
VAR unsigned si_opt_2
Definition: options.c:6
VAR unsigned si_opt_1
Definition: options.c:5
#define BVERBOSE(a)
Definition: options.h:34
#define TEST_V_ALLWARN
Definition: options.h:141
#define V_DEBUG_LIB
Definition: options.h:47
#define V_REDEFINE
Definition: options.h:44
#define V_LOAD_LIB
Definition: options.h:46
void rChangeCurrRing(ring r)
Definition: polys.cc:15
VAR ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:13
void PrintS(const char *s)
Definition: reporter.cc:284
void PrintLn()
Definition: reporter.cc:310
void Werror(const char *fmt,...)
Definition: reporter.cc:189
#define TRACE_SHOW_LINENO
Definition: reporter.h:31
#define TRACE_SHOW_LINE
Definition: reporter.h:33
EXTERN_VAR int traceit
Definition: reporter.h:24
EXTERN_VAR int traceit_stop
Definition: reporter.h:25
#define TRACE_SHOW_PROC
Definition: reporter.h:29
#define TRACE_SHOW_RINGS
Definition: reporter.h:36
static ring rIncRefCnt(ring r)
Definition: ring.h:847
static void rDecRefCnt(ring r)
Definition: ring.h:848
int status int void * buf
Definition: si_signals.h:59
#define R
Definition: sirandom.c:27
int(* iiArithAddCmd)(const char *szName, short nAlias, short nTokval, short nToktype, short nPos)
Definition: ipid.h:72
int(* iiAddCproc)(const char *libname, const char *procname, BOOLEAN pstatic, BOOLEAN(*func)(leftv res, leftv v))
Definition: ipid.h:70
ip_package * package
Definition: structs.h:48
sleftv * leftv
Definition: structs.h:62
#define BITSET
Definition: structs.h:20
#define loop
Definition: structs.h:80
VAR omBin libstack_bin
Definition: subexpr.cc:43
INST_VAR sleftv sLastPrinted
Definition: subexpr.cc:46
@ LANG_SINGULAR
Definition: subexpr.h:22
@ LANG_NONE
Definition: subexpr.h:22
@ LANG_MIX
Definition: subexpr.h:22
@ LANG_C
Definition: subexpr.h:22
@ PACKAGE_CMD
Definition: tok.h:149
@ STRING_CMD
Definition: tok.h:185
@ MAX_TOK
Definition: tok.h:218