source: git/Singular/iplib.cc @ 08e898

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