source: git/Singular/iplib.cc @ aee346d

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