source: git/Singular/iplib.cc @ f5d2647

jengelh-datetimespielwiese
Last change on this file since f5d2647 was f5d2647, checked in by Hans Schoenemann <hannes@…>, 9 years ago
fix: remove some LIBSINGULAR stuff, activate option(warn) for options
  • Property mode set to 100644
File size: 36.2 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}
[f5d2647]300
[016648]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[];
[f5d2647]310
[0e1846]311
[a71a00]312BOOLEAN iiAllStart(procinfov pi, char *p,feBufferTypes t, int l)
313{
[f5d2647]314  // see below:
315  BITSET save1=si_opt_1;
316  BITSET save2=si_opt_2;
[a71a00]317  newBuffer( omStrDup(p /*pi->data.s.body*/), t /*BT_proc*/,
318               pi, l );
319  BOOLEAN err=yyparse();
320  if (sLastPrinted.rtyp!=0)
321  {
322    sLastPrinted.CleanUp();
323  }
[016648]324  // the access to optionStruct and verboseStruct do not work
[9875202]325  // on x86_64-Linux for pic-code
[65eb96f]326  if ((TEST_V_ALLWARN) &&
327  (t==BT_proc) &&
[f5d2647]328  ((save1!=si_opt_1)||(save2!=si_opt_2)) &&
[65eb96f]329  (pi->libname!=NULL) && (pi->libname[0]!='\0'))
330  {
[aee346d]331    if ((pi->libname!=NULL) && (pi->libname[0]!='\0'))
332      Warn("option changed in proc %s from %s",pi->procname,pi->libname);
333    else
334      Warn("option changed in proc %s",pi->procname);
[65eb96f]335    int i;
336    for (i=0; optionStruct[i].setval!=0; i++)
337    {
[f5d2647]338      if ((optionStruct[i].setval & si_opt_1)
[65eb96f]339      && (!(optionStruct[i].setval & save1)))
340      {
341          Print(" +%s",optionStruct[i].name);
342      }
[f5d2647]343      if (!(optionStruct[i].setval & si_opt_1)
[65eb96f]344      && ((optionStruct[i].setval & save1)))
345      {
346          Print(" -%s",optionStruct[i].name);
347      }
348    }
349    for (i=0; verboseStruct[i].setval!=0; i++)
350    {
[f5d2647]351      if ((verboseStruct[i].setval & si_opt_2)
[65eb96f]352      && (!(verboseStruct[i].setval & save2)))
353      {
354          Print(" +%s",verboseStruct[i].name);
355      }
[f5d2647]356      if (!(verboseStruct[i].setval & si_opt_2)
[65eb96f]357      && ((verboseStruct[i].setval & save2)))
358      {
359          Print(" -%s",verboseStruct[i].name);
360      }
361    }
362    PrintLn();
363  }
[a71a00]364  return err;
365}
[0e1846]366/*2
[057e93c]367* start a proc
[0e1846]368* parameters are built as exprlist
[057e93c]369* TODO:interrupt
370* return FALSE on success, TRUE if an error occurs
[0e1846]371*/
[057e93c]372BOOLEAN iiPStart(idhdl pn, sleftv  * v)
[0e1846]373{
[42c4e6]374  procinfov pi=NULL;
[057e93c]375  int old_echo=si_echo;
[42c4e6]376  BOOLEAN err=FALSE;
[64d729]377  char save_flags=0;
[0e1846]378
379  /* init febase ======================================== */
[057e93c]380  /* we do not enter this case if filename != NULL !! */
381  if (pn!=NULL)
[0e1846]382  {
[057e93c]383    pi = IDPROC(pn);
384    if(pi!=NULL)
[0e1846]385    {
[64d729]386      save_flags=pi->trace_flag;
[057e93c]387      if( pi->data.s.body==NULL )
[0e1846]388      {
[64d729]389        iiGetLibProcBuffer(pi);
390        if (pi->data.s.body==NULL) return TRUE;
[0e1846]391      }
[f09b51]392//      omUpdateInfo();
393//      int m=om_Info.UsedBytes;
394//      Print("proc %s, mem=%d\n",IDID(pn),m);
[0e1846]395    }
396  }
[a71a00]397  else return TRUE;
[0e1846]398  /* generate argument list ======================================*/
399  if (v!=NULL)
400  {
[c232af]401    iiCurrArgs=(leftv)omAllocBin(sleftv_bin);
[0e1846]402    memcpy(iiCurrArgs,v,sizeof(sleftv));
403    memset(v,0,sizeof(sleftv));
404  }
405  else
406  {
407    iiCurrArgs=NULL;
408  }
[f71681]409  iiCurrProc=pn;
[0e1846]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  {
[a71a00]419    err=iiAllStart(pi,pi->data.s.body,BT_proc,pi->data.s.body_lineno-(v!=NULL));
420
[e0749e8]421    //Print("kill locals for %s (level %d)\n",IDID(pn),myynest);
422    killlocals(myynest);
[5c5638]423#ifndef NDEBUG
[e0749e8]424    checkall();
[3b1a83c]425#endif
[e0749e8]426    //Print("end kill locals for %s (%d)\n",IDID(pn),myynest);
427  }
[057e93c]428  myynest--;
429  si_echo=old_echo;
[64d729]430  if (pi!=NULL)
431    pi->trace_flag=save_flags;
[f09b51]432//  omUpdateInfo();
433//  int m=om_Info.UsedBytes;
434//  Print("exit %s, mem=%d\n",IDID(pn),m);
[0e1846]435  return err;
436}
437
[77ff8e]438#ifdef USE_IILOCALRING
[a3bc95e]439ring    *iiLocalRing;
[77ff8e]440#endif
[f92a39]441sleftv  iiRETURNEXPR;
[0e1846]442int     iiRETURNEXPR_len=0;
443
444#ifdef RDEBUG
445static void iiShowLevRings()
446{
447  int i;
[a3bc95e]448#ifdef USE_IILOCALRING
[9bc0b8]449  for (i=0;i<=myynest;i++)
[0e1846]450  {
451    Print("lev %d:",i);
452    if (iiLocalRing[i]==NULL) PrintS("NULL");
[e9fdf6]453    else                      Print("%lx",(long)iiLocalRing[i]);
[6f83c3d]454    PrintLn();
[0e1846]455  }
[77ff8e]456#endif
[3b1a83c]457#if  0
[a3bc95e]458  i=myynest;
459  proclevel *p=procstack;
460  while (p!=NULL)
[77ff8e]461  {
[a3bc95e]462    Print("lev %d:",i);
[573da6]463    if (p->cRingHdl==NULL) PrintS("NULL");
464    else                   Print("%s",IDID(p->cRingHdl));
[a3bc95e]465    PrintLn();
466    p=p->next;
[77ff8e]467  }
[a3bc95e]468#endif
[0e1846]469  if (currRing==NULL) PrintS("curr:NULL\n");
[e9fdf6]470  else                Print ("curr:%lx\n",(long)currRing);
[0e1846]471}
[bd4cb92]472#endif /* RDEBUG */
[0e1846]473
474static void iiCheckNest()
475{
476  if (myynest >= iiRETURNEXPR_len-1)
477  {
[0a3ddd]478#ifdef USE_IILOCALRING
[c232af]479    iiLocalRing=(ring *)omreallocSize(iiLocalRing,
[0e1846]480                                   iiRETURNEXPR_len*sizeof(ring),
481                                   (iiRETURNEXPR_len+16)*sizeof(ring));
[8b96123]482    memset(&(iiLocalRing[iiRETURNEXPR_len]),0,16*sizeof(ring));
[77ff8e]483#endif
[0e1846]484    iiRETURNEXPR_len+=16;
485  }
486}
[5edb77]487BOOLEAN iiMake_proc(idhdl pn, package pack, sleftv* sl)
[0e1846]488{
489  int err;
[2ba9a6]490  procinfov pi = IDPROC(pn);
[bcd557]491  if(pi->is_static && myynest==0)
492  {
[5480da]493    Werror("'%s::%s()' is a local procedure and cannot be accessed by an user.",
[ed3c47]494           pi->libname, pi->procname);
[5edb77]495    return TRUE;
[5480da]496  }
[0e1846]497  iiCheckNest();
[77ff8e]498#ifdef USE_IILOCALRING
[0e1846]499  iiLocalRing[myynest]=currRing;
[9bc0b8]500  //Print("currRing(%d):%s(%x) in %s\n",myynest,IDID(currRingHdl),currRing,IDID(pn));
[77ff8e]501#endif
[f92a39]502  iiRETURNEXPR.Init();
[a3bc95e]503  procstack->push(pi->procname);
[50a84c]504  if ((traceit&TRACE_SHOW_PROC)
505  || (pi->trace_flag&TRACE_SHOW_PROC))
[0e1846]506  {
[fca547]507    if (traceit&TRACE_SHOW_LINENO) PrintLn();
508    Print("entering%-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest);
[0e1846]509  }
510#ifdef RDEBUG
511  if (traceit&TRACE_SHOW_RINGS) iiShowLevRings();
512#endif
[057e93c]513  switch (pi->language)
514  {
[de5fe1]515    default:
516    case LANG_NONE:
517                 WerrorS("undefined proc");
[77ff8e]518                 err=TRUE;
519                 break;
[6f83c3d]520
[77ff8e]521    case LANG_SINGULAR:
[a3bc95e]522                 if ((pi->pack!=NULL)&&(currPack!=pi->pack))
523                 {
524                   currPack=pi->pack;
[8b96123]525                   iiCheckPack(currPack);
[a3bc95e]526                   currPackHdl=packFindHdl(currPack);
527                   //Print("set pack=%s\n",IDID(currPackHdl));
528                 }
529                 else if ((pack!=NULL)&&(currPack!=pack))
530                 {
531                   currPack=pack;
[8b96123]532                   iiCheckPack(currPack);
[a3bc95e]533                   currPackHdl=packFindHdl(currPack);
[873d82]534                   //Print("set pack=%s\n",IDID(currPackHdl));
[a3bc95e]535                 }
[77ff8e]536                 err=iiPStart(pn,sl);
537                 break;
538    case LANG_C:
[c232af]539                 leftv res = (leftv)omAlloc0Bin(sleftv_bin);
[77ff8e]540                 err = (pi->data.o.function)(res, sl);
[d7bdb0]541                 memcpy(&iiRETURNEXPR,res,sizeof(iiRETURNEXPR));
[c232af]542                 omFreeBin((ADDRESS)res,  sleftv_bin);
[77ff8e]543                 break;
[2ba9a6]544  }
[50a84c]545  if ((traceit&TRACE_SHOW_PROC)
546  || (pi->trace_flag&TRACE_SHOW_PROC))
[0e1846]547  {
[fca547]548    if (traceit&TRACE_SHOW_LINENO) PrintLn();
549    Print("leaving %-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest);
[0e1846]550  }
[9bc0b8]551  //char *n="NULL";
552  //if (currRingHdl!=NULL) n=IDID(currRingHdl);
553  //Print("currRing(%d):%s(%x) after %s\n",myynest,n,currRing,IDID(pn));
[0e1846]554#ifdef RDEBUG
555  if (traceit&TRACE_SHOW_RINGS) iiShowLevRings();
556#endif
557  if (err)
558  {
[f92a39]559    iiRETURNEXPR.CleanUp();
560    //iiRETURNEXPR.Init(); //done by CleanUp
[0e1846]561  }
[77ff8e]562#ifdef USE_IILOCALRING
[3b1a83c]563#if 0
[573da6]564  if(procstack->cRing != iiLocalRing[myynest]) Print("iiMake_proc: 1 ring not saved procs:%x, iiLocal:%x\n",procstack->cRing, iiLocalRing[myynest]);
[3b1a83c]565#endif
[0e1846]566  if (iiLocalRing[myynest] != currRing)
567  {
[9bc0b8]568    if (currRing!=NULL)
[31a392]569    {
[f92a39]570      if (((iiRETURNEXPR.Typ()>BEGIN_RING)
571        && (iiRETURNEXPR.Typ()<END_RING))
572      || ((iiRETURNEXPR.Typ()==LIST_CMD)
573        && (lRingDependend((lists)iiRETURNEXPR.Data()))))
[9bc0b8]574      {
575        //idhdl hn;
[85e68dd]576        const char *n;
577        const char *o;
[9bc0b8]578        idhdl nh=NULL, oh=NULL;
579        if (iiLocalRing[myynest]!=NULL)
580          oh=rFindHdl(iiLocalRing[myynest],NULL, NULL);
581        if (oh!=NULL)          o=oh->id;
582        else                   o="none";
583        if (currRing!=NULL)
584          nh=rFindHdl(currRing,NULL, NULL);
585        if (nh!=NULL)          n=nh->id;
586        else                   n="none";
587        Werror("ring change during procedure call: %s -> %s (level %d)",o,n,myynest);
[f92a39]588        iiRETURNEXPR.CleanUp();
[9bc0b8]589        err=TRUE;
590      }
[0e1846]591    }
[9f12b01]592    currRing=iiLocalRing[myynest];
[3b1a83c]593  }
[9bc0b8]594  if ((currRing==NULL)
595  && (currRingHdl!=NULL))
596    currRing=IDRING(currRingHdl);
597  else
[3b1a83c]598  if ((currRing!=NULL) &&
[9f12b01]599    ((currRingHdl==NULL)||(IDRING(currRingHdl)!=currRing)
600     ||(IDLEV(currRingHdl)>=myynest)))
[3b1a83c]601  {
602    rSetHdl(rFindHdl(currRing,NULL, NULL));
603    iiLocalRing[myynest]=NULL;
[77ff8e]604  }
605#else /* USE_IILOCALRING */
[573da6]606  if (procstack->cRing != currRing)
[a3bc95e]607  {
[573da6]608    //if (procstack->cRingHdl!=NULL)
609    //Print("procstack:%s,",IDID(procstack->cRingHdl));
[a3bc95e]610    //if (currRingHdl!=NULL)
611    //Print(" curr:%s\n",IDID(currRingHdl));
[573da6]612    //Print("pr:%x, curr: %x\n",procstack->cRing,currRing);
[f92a39]613    if (((iiRETURNEXPR.Typ()>BEGIN_RING)
614      && (iiRETURNEXPR.Typ()<END_RING))
615    || ((iiRETURNEXPR.Typ()==LIST_CMD)
616      && (lRingDependend((lists)iiRETURNEXPR.Data()))))
[a3bc95e]617    {
618      //idhdl hn;
619      char *n;
620      char *o;
[573da6]621      if (procstack->cRing!=NULL)
[a3bc95e]622      {
623        //PrintS("reset ring\n");
[573da6]624        procstack->cRingHdl=rFindHdl(procstack->cRing,NULL, NULL);
625        if (procstack->cRingHdl==NULL)
626          procstack->cRingHdl=
627           rFindHdl(procstack->cRing,NULL,procstack->currPack->idroot);
628        if (procstack->cRingHdl==NULL)
629          procstack->cRingHdl=
630           rFindHdl(procstack->cRing,NULL,basePack->idroot);
631        o=IDID(procstack->cRingHdl);
632        currRing=procstack->cRing;
633        currRingHdl=procstack->cRingHdl;
[a3bc95e]634      }
635      else                            o="none";
636      if (currRing!=NULL)             n=IDID(currRingHdl);
637      else                            n="none";
638      if (currRing==NULL)
639      {
640        Werror("ring change during procedure call: %s -> %s",o,n);
[f92a39]641        iiRETURNEXPR.CleanUp();
[a3bc95e]642        err=TRUE;
643      }
644    }
[573da6]645    if (procstack->cRingHdl!=NULL)
[a3bc95e]646    {
[573da6]647      rSetHdl(procstack->cRingHdl);
[a3bc95e]648    }
649    else
650    { currRingHdl=NULL; currRing=NULL; }
651  }
652#endif /* USE_IILOCALRING */
[0e1846]653  if (iiCurrArgs!=NULL)
654  {
[bcd557]655    if (!err) Warn("too many arguments for %s",IDID(pn));
[0e1846]656    iiCurrArgs->CleanUp();
[c232af]657    omFreeBin((ADDRESS)iiCurrArgs, sleftv_bin);
[0e1846]658    iiCurrArgs=NULL;
659  }
[a3bc95e]660  procstack->pop();
[bcd557]661  if (err)
[5edb77]662    return TRUE;
663  return FALSE;
[0e1846]664}
665
666/*2
667* start an example (as a proc),
668* destroys the string 'example'
669*/
[057e93c]670BOOLEAN iiEStart(char* example, procinfo *pi)
[0e1846]671{
672  BOOLEAN err;
[057e93c]673  int old_echo=si_echo;
[0e1846]674
675  iiCheckNest();
[a3bc95e]676  procstack->push(example);
[77ff8e]677#ifdef USE_IILOCALRING
[0e1846]678  iiLocalRing[myynest]=currRing;
[77ff8e]679#endif
[0e1846]680  if (traceit&TRACE_SHOW_PROC)
681  {
682    if (traceit&TRACE_SHOW_LINENO) printf("\n");
683    printf("entering example (level %d)\n",myynest);
684  }
685  myynest++;
[a71a00]686
687  err=iiAllStart(pi,example,BT_example,(pi != NULL ? pi->data.s.example_lineno: 0));
688
[0e1846]689  killlocals(myynest);
690  myynest--;
[057e93c]691  si_echo=old_echo;
[0e1846]692  if (traceit&TRACE_SHOW_PROC)
693  {
694    if (traceit&TRACE_SHOW_LINENO) printf("\n");
695    printf("leaving  -example- (level %d)\n",myynest);
696  }
[77ff8e]697#ifdef USE_IILOCALRING
[0e1846]698  if (iiLocalRing[myynest] != currRing)
699  {
700    if (iiLocalRing[myynest]!=NULL)
701    {
[cf42ab1]702      rSetHdl(rFindHdl(iiLocalRing[myynest],NULL, NULL));
[0e1846]703      iiLocalRing[myynest]=NULL;
704    }
705    else
706    {
707      currRingHdl=NULL;
708      currRing=NULL;
709    }
710  }
[77ff8e]711#else /* USE_IILOCALRING */
[a3bc95e]712#endif /* USE_IILOCALRING */
[77ff8e]713  if (NS_LRING != currRing)
714  {
715    if (NS_LRING!=NULL)
716    {
[573da6]717      idhdl rh=procstack->cRingHdl;
[bd4cb92]718      if ((rh==NULL)||(IDRING(rh)!=NS_LRING))
719        rh=rFindHdl(NS_LRING,NULL, NULL);
720      rSetHdl(rh);
[77ff8e]721    }
722    else
723    {
724      currRingHdl=NULL;
725      currRing=NULL;
726    }
727  }
[a3bc95e]728//#endif /* USE_IILOCALRING */
729  procstack->pop();
[0e1846]730  return err;
731}
732
[989d6b]733int SI_MOD_INIT(staticdemo)(SModulFunctions*){ PrintS("init of staticdemo\n"); return (0); }
[57dcd6]734
735#define SI_GET_BUILTIN_MOD_INIT(name) \
[989d6b]736 if (strcmp(libname, #name ".so") == 0){ int SI_MOD_INIT(name)(SModulFunctions*); return SI_MOD_INIT(name); }
[57dcd6]737
738SModulFunc_t
[e2202ee]739iiGetBuiltinModInit(const char* libname)
[57dcd6]740{
741  SI_FOREACH_BUILTIN(SI_GET_BUILTIN_MOD_INIT)
742
743  return NULL;
744}
745
746
747#undef SI_GET_BUILTIN_MOD_INIT
748
749
[8edd35]750/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[85e68dd]751BOOLEAN iiTryLoadLib(leftv v, const char *id)
[8edd35]752{
753  BOOLEAN LoadResult = TRUE;
754  char libnamebuf[128];
[c232af]755  char *libname = (char *)omAlloc(strlen(id)+5);
[85e68dd]756  const char *suffix[] = { "", ".lib", ".so", ".sl", NULL };
[8edd35]757  int i = 0;
[6909cfb]758  // FILE *fp;
759  // package pack;
760  // idhdl packhdl;
[8edd35]761  lib_types LT;
[743c32]762  for(i=0; suffix[i] != NULL; i++)
763  {
[8edd35]764    sprintf(libname, "%s%s", id, suffix[i]);
765    *libname = mytolower(*libname);
[2166892]766    if((LT = type_of_LIB(libname, libnamebuf)) > LT_NOTFOUND)
[743c32]767    {
[2166892]768      char *s=omStrDup(libname);
769      char libnamebuf[256];
770
771      if (LT==LT_SINGULAR)
[22ed4c]772        LoadResult = iiLibCmd(s, FALSE, FALSE,TRUE);
[24590f]773      #ifdef HAVE_DYNAMIC_LOADING
[2166892]774      else if ((LT==LT_ELF) || (LT==LT_HPUX))
775        LoadResult = load_modules(s,libnamebuf,FALSE);
[24590f]776      #endif
[801d2b]777      else if (LT==LT_BUILTIN)
778      {
[57dcd6]779        LoadResult=load_builtin(s,FALSE, iiGetBuiltinModInit(s));
[801d2b]780      }
[2166892]781      if(!LoadResult )
[743c32]782      {
[8edd35]783        v->name = iiConvName(libname);
784        break;
785      }
786    }
787  }
[c232af]788  omFree(libname);
[8edd35]789  return LoadResult;
790}
791
[1d1101]792/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[50cbdc]793/* check, if library lib has already been loaded
[698457]794   if yes, writes filename of lib into where and returns TRUE,
795      no, returns FALSE
796*/
797BOOLEAN iiLocateLib(const char* lib, char* where)
798{
799  idhdl hl;
[4e4ece7]800
[5121f6]801  char *p;
[50cbdc]802
[5af2f9]803  hl = IDROOT->get("LIB", 0);
[5121f6]804  if (hl == NULL || (p=strstr(IDSTRING(hl), lib)) == NULL) return FALSE;
805  if ((p!=IDSTRING(hl)) && (*(p-1)!=',')) return FALSE;
[4e4ece7]806
[698457]807  if (strstr(IDSTRING(hl), ",") == NULL)
808  {
809    strcpy(where, IDSTRING(hl));
810  }
811  else
812  {
[c232af]813    char* tmp = omStrDup(IDSTRING(hl));
[698457]814    char* tok = strtok(tmp, ",");
815    do
816    {
817      if (strstr(tok, lib) != NULL) break;
818      tok = strtok(NULL, ",");
819    }
820    while (tok != NULL);
821    assume(tok != NULL);
822    strcpy(where, tok);
[c232af]823    omFree(tmp);
[698457]824  }
825  return TRUE;
826}
[4e4ece7]827
[22ed4c]828BOOLEAN iiLibCmd( char *newlib, BOOLEAN autoexport, BOOLEAN tellerror, BOOLEAN force )
[0e1846]829{
830  char libnamebuf[128];
[6909cfb]831  // procinfov pi;
832  // idhdl h;
[a3bc95e]833  idhdl pl;
[6909cfb]834  // idhdl hl;
835  // long pos = 0L;
[a3bc95e]836  char *plib = iiConvName(newlib);
[dfc6b54]837  FILE * fp = feFopen( newlib, "r", libnamebuf, tellerror );
[6909cfb]838  // int lines = 1;
[42c4e6]839  BOOLEAN LoadResult = TRUE;
840
[0e1846]841  if (fp==NULL)
842  {
843    return TRUE;
844  }
[a3bc95e]845  pl = basePack->idroot->get(plib,0);
846  if (pl==NULL)
847  {
[3b1a83c]848    pl = enterid( plib,0, PACKAGE_CMD,
[a3bc95e]849                  &(basePack->idroot), TRUE );
850    IDPACKAGE(pl)->language = LANG_SINGULAR;
851    IDPACKAGE(pl)->libname=omStrDup(newlib);
852  }
853  else
854  {
855    if(IDTYP(pl)!=PACKAGE_CMD)
856    {
[7a949a]857      WarnS("not of type package.");
[a3bc95e]858      fclose(fp);
859      return TRUE;
860    }
[22ed4c]861    if (!force) return FALSE;
[a3bc95e]862  }
[22ed4c]863  LoadResult = iiLoadLIB(fp, libnamebuf, newlib, pl, autoexport, tellerror);
[a3bc95e]864  omFree((ADDRESS)newlib);
865
866  if(!LoadResult) IDPACKAGE(pl)->loaded = TRUE;
[c232af]867  omFree((ADDRESS)plib);
[a3bc95e]868
[21fa0b]869 return LoadResult;
[8edd35]870}
[c3cb95]871/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[31a392]872static void iiCleanProcs(idhdl &root)
[c3cb95]873{
[2bb771]874  idhdl prev=NULL;
[c3cb95]875  loop
876  {
877    if (root==NULL) return;
878    if (IDTYP(root)==PROC_CMD)
879    {
[560432]880      procinfo *pi=(procinfo*)IDDATA(root);
[c3cb95]881      if ((pi->language == LANG_SINGULAR)
882      && (pi->data.s.body_start == 0L))
883      {
884        // procinfo data incorrect:
885        // - no proc body can start at the beginning of the file
[560432]886        killhdl(root);
[2bb771]887        if (prev==NULL)
[bd4cb92]888          root=IDROOT;
[2bb771]889        else
890        {
891          root=prev;
892          prev=NULL;
893        }
894        continue;
[c3cb95]895      }
896    }
[2bb771]897    prev=root;
[c3cb95]898    root=IDNEXT(root);
899  }
900}
[42c4e6]901static void iiRunInit(package p)
902{
[31a392]903  idhdl h=p->idroot->get("mod_init",0);
904  if (h==NULL) return;
905  if (IDTYP(h)==PROC_CMD)
[42c4e6]906  {
[31a392]907    int save=yylineno;
908    myynest++;
[6909cfb]909    // procinfo *pi=(procinfo*)IDDATA(h);
[31a392]910    //PrintS("mod_init found\n");
911    iiMake_proc(h,p,NULL);
912    myynest--;
913    yylineno=save;
[42c4e6]914  }
915}
[8edd35]916/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[e2202ee]917BOOLEAN iiLoadLIB(FILE *fp, const char *libnamebuf, const char*newlib,
[8edd35]918             idhdl pl, BOOLEAN autoexport, BOOLEAN tellerror)
919{
[6a51ef]920  extern FILE *yylpin;
921  libstackv ls_start = library_stack;
[5480da]922  lib_style_types lib_style;
[6a51ef]923
924  yylpin = fp;
[4e4ece7]925  #if YYLPDEBUG > 1
[6a51ef]926  print_init();
[4e4ece7]927  #endif
[799ce1]928  extern int lpverbose;
[4e4ece7]929  if (BVERBOSE(V_DEBUG_LIB)) lpverbose=1;
930  else lpverbose=0;
[2f8028]931  // yylplex sets also text_buffer
932  if (text_buffer!=NULL) *text_buffer='\0';
[d3e630]933  yylplex(newlib, libnamebuf, &lib_style, pl, autoexport);
[805b06c]934  if(yylp_errno)
935  {
[6a51ef]936    Werror("Library %s: ERROR occured: in line %d, %d.", newlib, yylplineno,
[5c8eae0]937         current_pos(0));
[805b06c]938    if(yylp_errno==YYLP_BAD_CHAR)
939    {
[60a3add]940      Werror(yylp_errlist[yylp_errno], *text_buffer, yylplineno);
[c232af]941      omFree((ADDRESS)text_buffer);
[9a11fe]942      text_buffer=NULL;
[805b06c]943    }
944    else
[60a3add]945      Werror(yylp_errlist[yylp_errno], yylplineno);
[6a51ef]946    Werror("Cannot load library,... aborting.");
947    reinit_yylp();
948    fclose( yylpin );
[bd4cb92]949    iiCleanProcs(IDROOT);
[6a51ef]950    return TRUE;
[5c8eae0]951  }
[4e4ece7]952  if (BVERBOSE(V_LOAD_LIB))
953    Print( "// ** loaded %s %s\n", libnamebuf, text_buffer);
[805b06c]954  if( (lib_style == OLD_LIBSTYLE) && (BVERBOSE(V_LOAD_LIB)))
955  {
[40706c]956    Warn( "library %s has old format. This format is still accepted,", newlib);
957    Warn( "but for functionality you may wish to change to the new");
958    Warn( "format. Please refer to the manual for further information.");
[0a3ddd]959  }
[6a51ef]960  reinit_yylp();
961  fclose( yylpin );
[dae004]962  fp = NULL;
[42c4e6]963  iiRunInit(IDPACKAGE(pl));
[64d729]964
[6a51ef]965  {
966    libstackv ls;
[805b06c]967    for(ls = library_stack; (ls != NULL) && (ls != ls_start); )
968    {
969      if(ls->to_be_done)
970      {
[5c8eae0]971        ls->to_be_done=FALSE;
[22ed4c]972        iiLibCmd(ls->get(),autoexport,tellerror,FALSE);
[5c8eae0]973        ls = ls->pop(newlib);
[6a51ef]974      }
975    }
976#if 0
[6f83c3d]977    PrintS("--------------------\n");
978    for(ls = library_stack; ls != NULL; ls = ls->next)
979    {
[6a51ef]980      Print("%s: LIB-stack:(%d), %s %s\n", newlib, ls->cnt, ls->get(),
981        ls->to_be_done ? "not loaded" : "loaded");
982    }
[6f83c3d]983    PrintS("--------------------\n");
[6a51ef]984#endif
985  }
[21fa0b]986
[dae004]987  if(fp != NULL) fclose(fp);
[0e1846]988  return FALSE;
989}
990
[8edd35]991
[1d1101]992/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[85e68dd]993procinfo *iiInitSingularProcinfo(procinfov pi, const char *libname,
994              const char *procname, int line, long pos, BOOLEAN pstatic)
[2ba9a6]995{
[c232af]996  pi->libname = omStrDup(libname);
[2ba9a6]997
[057e93c]998  if( strcmp(procname,"_init")==0)
999  {
[805b06c]1000    pi->procname = iiConvName(libname);
1001  }
1002  else
[c232af]1003    pi->procname = omStrDup(procname);
[2ba9a6]1004  pi->language = LANG_SINGULAR;
1005  pi->ref = 1;
[a3bc95e]1006  pi->pack = NULL;
[6a51ef]1007  pi->is_static = pstatic;
[2ba9a6]1008  pi->data.s.proc_start = pos;
[46d09b]1009  pi->data.s.def_end    = 0L;
[2ba9a6]1010  pi->data.s.help_start = 0L;
[6be769]1011  pi->data.s.help_end   = 0L;
[2ba9a6]1012  pi->data.s.body_start = 0L;
1013  pi->data.s.body_end   = 0L;
1014  pi->data.s.example_start = 0L;
1015  pi->data.s.proc_lineno = line;
1016  pi->data.s.body_lineno = 0;
1017  pi->data.s.example_lineno = 0;
1018  pi->data.s.body = NULL;
[847cad]1019  pi->data.s.help_chksum = 0;
[2ba9a6]1020  return(pi);
1021}
1022
[fe72e1]1023#if 0
1024// TODO!?
1025procinfo *iiInitSingularProcinfo(procinfo* pi, const char *libname,
1026                                 const char *procname, int line, long pos,
1027                                 BOOLEAN pstatic /*= FALSE*/)
1028{
1029  pi->libname = (char *)malloc(strlen(libname)+1);
1030  memcpy(pi->libname, libname, strlen(libname));
1031  *(pi->libname+strlen(libname)) = '\0';
1032
1033  pi->procname = (char *)malloc(strlen(procname)+1);
1034  strcpy(pi->procname, procname/*, strlen(procname)*/);
1035  pi->language = LANG_SINGULAR;
1036  pi->ref = 1;
1037  pi->is_static = pstatic;
1038  pi->data.s.proc_start = pos;
1039  pi->data.s.def_end    = 0L;
1040  pi->data.s.help_start = 0L;
1041  pi->data.s.body_start = 0L;
1042  pi->data.s.body_end   = 0L;
1043  pi->data.s.example_start = 0L;
1044  pi->data.s.proc_lineno = line;
1045  pi->data.s.body_lineno = 0;
1046  pi->data.s.example_lineno = 0;
1047  pi->data.s.body = NULL;
1048  pi->data.s.help_chksum = 0;
1049  return(pi);
1050}
1051#endif
1052
[1d1101]1053/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[ce0ca7]1054int iiAddCproc(const char *libname, const char *procname, BOOLEAN pstatic,
[ed3c47]1055               BOOLEAN(*func)(leftv res, leftv v))
[1d1101]1056{
1057  procinfov pi;
1058  idhdl h;
1059
[36d920]1060  #ifndef NDEBUG
1061  int dummy;
1062  if (IsCmd(procname,dummy))
1063  {
1064    Werror(">>%s< is a reserved name",procname);
1065    return 0;
1066  }
1067  #endif
1068
[3b1a83c]1069  h = enterid(procname,0, PROC_CMD, &IDROOT, TRUE);
[6f83c3d]1070  if ( h!= NULL )
1071  {
[1d1101]1072    pi = IDPROC(h);
[c232af]1073    pi->libname = omStrDup(libname);
1074    pi->procname = omStrDup(procname);
[1d1101]1075    pi->language = LANG_C;
1076    pi->ref = 1;
1077    pi->is_static = pstatic;
1078    pi->data.o.function = func;
1079    return(1);
[6f83c3d]1080  }
1081  else
1082  {
1083    PrintS("iiAddCproc: failed.\n");
[1d1101]1084  }
1085  return(0);
1086}
[cd6b45]1087
[ce0ca7]1088int iiAddCprocTop(const char *libname, const char *procname, BOOLEAN pstatic,
[c858fc2]1089               BOOLEAN(*func)(leftv res, leftv v))
1090{
1091  int r=iiAddCproc(libname,procname,pstatic,func);
1092  package s=currPack;
1093  currPack=basePack;
1094  if (r) r=iiAddCproc(libname,procname,pstatic,func);
1095  currPack=s;
1096  return r;
1097}
1098
[cd6b45]1099/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[825fb7]1100#ifdef HAVE_DYNAMIC_LOADING
[e2202ee]1101BOOLEAN load_modules(const char *newlib, char *fullname, BOOLEAN autoexport)
[cd6b45]1102{
[4eb5b5]1103#ifdef HAVE_STATIC
1104  WerrorS("mod_init: static version can not load modules");
1105  return TRUE;
1106#else
[9eb3048]1107/*
[ce0ca7]1108  typedef int (*fktn_t)(int(*iiAddCproc)(const char *libname, const char *procname,
[cd6b45]1109                               BOOLEAN pstatic,
1110                               BOOLEAN(*func)(leftv res, leftv v)));
[9eb3048]1111*/
[801d2b]1112  SModulFunc_t fktn;
[cd6b45]1113  idhdl pl;
1114  char *plib = iiConvName(newlib);
1115  BOOLEAN RET=TRUE;
1116  int token;
1117  char FullName[256];
[64d729]1118
[a3dfd97]1119  memset(FullName,0,256);
1120
[cd6b45]1121  if( *fullname != '/' &&  *fullname != '.' )
1122    sprintf(FullName, "./%s", newlib);
[a3dfd97]1123  else strncpy(FullName, fullname,255);
[64d729]1124
[cd6b45]1125
[bf4997]1126  if(IsCmd(plib, token))
[cd6b45]1127  {
1128    Werror("'%s' is resered identifier\n", plib);
1129    goto load_modules_end;
1130  }
[1bd25e]1131  pl = IDROOT->get(plib,0);
[cd6b45]1132  if (pl==NULL)
1133  {
[3b1a83c]1134    pl = enterid( plib,0, PACKAGE_CMD, &IDROOT,
[1bd25e]1135                  TRUE );
[cd6b45]1136    IDPACKAGE(pl)->language = LANG_C;
[c232af]1137    IDPACKAGE(pl)->libname=omStrDup(newlib);
[cd6b45]1138  }
1139  else
1140  {
1141    if(IDTYP(pl)!=PACKAGE_CMD)
1142    {
[7a949a]1143      Warn("not of type package.");
[64d729]1144      goto load_modules_end;
[cd6b45]1145    }
1146  }
[f4399e]1147  if (dynl_check_opened(FullName)) {
1148    if (BVERBOSE(V_LOAD_LIB)) Warn( "%s already loaded", fullname);
1149    return FALSE;
1150  }
[7497447]1151  if((IDPACKAGE(pl)->handle=dynl_open(FullName))==(void *)NULL)
[cd6b45]1152  {
[46ef83]1153    Werror("dynl_open failed:%s", dynl_error());
[cd6b45]1154    Werror("%s not found", newlib);
[64d729]1155    goto load_modules_end;
[cd6b45]1156  }
1157  else
1158  {
[b6e51b]1159    SModulFunctions sModulFunctions;
[31a392]1160
[a3dfd97]1161    package s=currPack;
1162    currPack=IDPACKAGE(pl);
[801d2b]1163    fktn = (SModulFunc_t)dynl_sym(IDPACKAGE(pl)->handle, "mod_init");
[c858fc2]1164    if( fktn!= NULL)
1165    {
[b6e51b]1166      sModulFunctions.iiArithAddCmd = iiArithAddCmd;
1167      if (autoexport) sModulFunctions.iiAddCproc = iiAddCprocTop;
1168      else            sModulFunctions.iiAddCproc = iiAddCproc;
1169      (*fktn)(&sModulFunctions);
[c858fc2]1170    }
[1fb78b]1171    else Werror("mod_init: %s\n", dynl_error());
[cd6b45]1172    if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loaded %s \n", fullname);
[a3dfd97]1173    currPack->loaded=1;
1174    currPack=s;
[cd6b45]1175  }
1176  RET=FALSE;
1177
1178  load_modules_end:
1179  return RET;
[31a392]1180#endif /*STATIC */
[cd6b45]1181}
[825fb7]1182#endif /* HAVE_DYNAMIC_LOADING */
[801d2b]1183/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[e2202ee]1184BOOLEAN load_builtin(const char *newlib, BOOLEAN autoexport, SModulFunc_t init)
[801d2b]1185{
1186  int iiAddCproc(const char *libname, const char *procname, BOOLEAN pstatic,
1187                 BOOLEAN(*func)(leftv res, leftv v));
[9eb3048]1188/*
[801d2b]1189  typedef int (*fktn_t)(int(*iiAddCproc)(const char *libname, const char *procname,
1190                               BOOLEAN pstatic,
1191                               BOOLEAN(*func)(leftv res, leftv v)));
[9eb3048]1192*/
[6909cfb]1193  // SModulFunc_t fktn;
[801d2b]1194  idhdl pl;
1195  char *plib = iiConvName(newlib);
[6909cfb]1196  // BOOLEAN RET=TRUE;
1197  // int token;
[801d2b]1198
1199  pl = IDROOT->get(plib,0);
[f4399e]1200  if (pl!=NULL)
[801d2b]1201  {
[f4399e]1202    if (BVERBOSE(V_LOAD_LIB)) Warn( "(builtin) %s already loaded", newlib);
1203    omFree(plib);
1204    return FALSE;
[801d2b]1205  }
[f4399e]1206
1207  pl = enterid( plib,0, PACKAGE_CMD, &IDROOT,
1208                TRUE );
1209  IDPACKAGE(pl)->language = LANG_C;
1210  IDPACKAGE(pl)->libname=omStrDup(newlib);
1211
[801d2b]1212  IDPACKAGE(pl)->handle=(void *)NULL;
1213  SModulFunctions sModulFunctions;
1214
1215  package s=currPack;
1216  currPack=IDPACKAGE(pl);
1217  if( init!= NULL)
1218  {
1219    sModulFunctions.iiArithAddCmd = iiArithAddCmd;
1220    if (autoexport) sModulFunctions.iiAddCproc = iiAddCprocTop;
1221    else            sModulFunctions.iiAddCproc = iiAddCproc;
1222    (*init)(&sModulFunctions);
1223  }
[57dcd6]1224  if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loaded (builtin) %s \n", newlib);
[801d2b]1225  currPack->loaded=1;
1226  currPack=s;
1227
1228  return FALSE;
1229}
1230/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[36d920]1231void module_help_main(const char *newlib,const char *help)
[801d2b]1232{
1233  char *plib = iiConvName(newlib);
[36d920]1234  idhdl pl = basePack->idroot->get(plib,0);
[801d2b]1235  if ((pl==NULL)||(IDTYP(pl)!=PACKAGE_CMD))
[36d920]1236    Werror(">>%s<< is not a package (trying to add package help)",plib);
[801d2b]1237  else
1238  {
1239    package s=currPack;
1240    currPack=IDPACKAGE(pl);
1241    idhdl h=enterid(omStrDup("info"),0,STRING_CMD,&IDROOT,FALSE);
1242    IDSTRING(h)=omStrDup(help);
1243    currPack=s;
1244  }
1245}
[36d920]1246void module_help_proc(const char *newlib,const char *p, const char *help)
[801d2b]1247{
1248  char *plib = iiConvName(newlib);
[36d920]1249  idhdl pl = basePack->idroot->get(plib,0);
[801d2b]1250  if ((pl==NULL)||(IDTYP(pl)!=PACKAGE_CMD))
[36d920]1251    Werror(">>%s<< is not a package(trying to add help for %s)",plib,p);
[801d2b]1252  else
1253  {
1254    package s=currPack;
1255    currPack=IDPACKAGE(pl);
1256    char buff[256];
1257    buff[255]='\0';
1258    strncpy(buff,p,255);
1259    strncat(buff,"_help",255-strlen(p));
1260    idhdl h=enterid(omStrDup(buff),0,STRING_CMD,&IDROOT,FALSE);
1261    IDSTRING(h)=omStrDup(help);
1262    currPack=s;
1263  }
1264}
1265/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[1cb879]1266
1267// loads a dynamic module from the binary path and returns a named function
1268// returns NULL, if something fails
1269void* binary_module_function(const char* newlib, const char* funcname)
1270{
1271  void* result = NULL;
1272
[825fb7]1273#if defined(HAVE_STATIC) || !defined(HAVE_DYNAMIC_LOADING)
[1cb879]1274  WerrorS("static version can not load function from dynamic modules");
1275#else
1276  const char* bin_dir = feGetResource('b');
1277  if (!bin_dir)  { return NULL; }
[a71a00]1278
1279  char path_name[MAXPATHLEN];
[1cb879]1280  sprintf(path_name, "%s%s%s.%s", bin_dir, DIR_SEPP, newlib, MODULE_SUFFIX_STRING);
1281
1282  void* openlib = dynl_open(path_name);
1283  if(!openlib)
1284  {
1285    Werror("dynl_open of %s failed:%s", path_name, dynl_error());
1286    return NULL;
1287  }
1288  result = dynl_sym(openlib, funcname);
1289  if (!result) Werror("%s: %s\n", funcname, dynl_error());
1290#endif
1291
1292  return result;
1293}
1294
[1d1101]1295
[2ba9a6]1296/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[77ff8e]1297char mytoupper(char c)
1298{
1299  if(c>=97 && c<=(97+26)) c-=32;
1300  return(c);
1301}
1302
[8edd35]1303char mytolower(char c)
1304{
1305  if(c>=65 && c<=(65+26)) c+=32;
1306  return(c);
1307}
1308
[77ff8e]1309/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[75c0dc]1310//#if defined(WINNT)
1311//#  define  FS_SEP '\\'
1312//#else
1313//#  define FS_SEP '/'
1314//#endif
[77ff8e]1315
[f43a74]1316char *iiConvName(const char *libname)
[2ba9a6]1317{
[c232af]1318  char *tmpname = omStrDup(libname);
[9c35ef]1319  char *p = strrchr(tmpname, DIR_SEP);
[ed3c47]1320  char *r;
[77ff8e]1321  if(p==NULL) p = tmpname;
1322  else p++;
[08c2d6]1323  r = (char *)strchr(p, '.');
[77ff8e]1324  if( r!= NULL) *r = '\0';
[c232af]1325  r = omStrDup(p);
[77ff8e]1326  *r = mytoupper(*r);
1327  // printf("iiConvName: '%s' '%s' => '%s'\n", libname, tmpname, r);
[c232af]1328  omFree((ADDRESS)tmpname);
[6f83c3d]1329
[77ff8e]1330  return(r);
[2ba9a6]1331}
1332
1333/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[c599b4]1334#if 0 /* debug only */
[fca547]1335void piShowProcList()
[2ba9a6]1336{
1337  idhdl h;
1338  procinfo *proc;
1339  char *name;
1340
1341  Print( "%-15s  %20s      %s,%s  %s,%s   %s,%s\n", "Library", "function",
[13e3243]1342         "line", "start", "line", "body", "line", "example");
[46d09b]1343  for(h = IDROOT; h != NULL; h = IDNEXT(h))
[057e93c]1344  {
1345    if(IDTYP(h) == PROC_CMD)
1346    {
[2ba9a6]1347      proc = IDPROC(h);
[057e93c]1348      if(strcmp(proc->procname, IDID(h))!=0)
1349      {
[c232af]1350        name = (char *)omAlloc(strlen(IDID(h))+strlen(proc->procname)+4);
[13e3243]1351        sprintf(name, "%s -> %s", IDID(h), proc->procname);
[46d09b]1352        Print( "%d %-15s  %20s ", proc->is_static ? 1 : 0, proc->libname, name);
[c232af]1353        omFree((ADDRESS)name);
[0077d9d]1354      }
1355      else
[46d09b]1356        Print( "%d %-15s  %20s ", proc->is_static ? 1 : 0, proc->libname,
1357               proc->procname);
[13e3243]1358      if(proc->language==LANG_SINGULAR)
1359        Print("line %4d,%-5ld  %4d,%-5ld  %4d,%-5ld\n",
[2ba9a6]1360              proc->data.s.proc_lineno, proc->data.s.proc_start,
1361              proc->data.s.body_lineno, proc->data.s.body_start,
1362              proc->data.s.example_lineno, proc->data.s.example_start);
[0077d9d]1363      else if(proc->language==LANG_C)
1364        Print("type: object\n");
[2ba9a6]1365    }
1366  }
1367}
[c599b4]1368#endif
[2ba9a6]1369
1370/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
[fca547]1371//char *iiLineNo(char *procname, int lineno)
1372//{
1373//  char buf[256];
1374//  idhdl pn = ggetid(procname);
1375//  procinfo *pi = IDPROC(pn);
1376//
1377//  sprintf(buf, "%s %3d\0", procname, lineno);
1378//  //sprintf(buf, "%s::%s %3d\0", pi->libname, pi->procname,
1379//  //  lineno + pi->data.s.body_lineno);
1380//  return(buf);
1381//}
[6a51ef]1382/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1383#ifdef HAVE_LIBPARSER
[e2202ee]1384void libstack::push(const char */*p*/, char *libn)
[6a51ef]1385{
1386  libstackv lp;
[5f4463]1387  if( !iiGetLibStatus(libn))
[743c32]1388  {
1389    for(lp = this;lp!=NULL;lp=lp->next)
1390    {
[5f4463]1391      if(strcmp(lp->get(), libn)==0) break;
[6a51ef]1392    }
[743c32]1393    if(lp==NULL)
1394    {
[c232af]1395      libstackv ls = (libstack *)omAlloc0Bin(libstack_bin);
[6a51ef]1396      ls->next = this;
[5f4463]1397      ls->libname = omStrDup(libn);
[6a51ef]1398      ls->to_be_done = TRUE;
1399      if(this != NULL) ls->cnt = this->cnt+1; else ls->cnt = 0;
1400      library_stack = ls;
[5c8eae0]1401    }
[6a51ef]1402  }
1403}
1404
[e2202ee]1405libstackv libstack::pop(const char */*p*/)
[6a51ef]1406{
1407  libstackv ls = this;
[c232af]1408  //omFree((ADDRESS)ls->libname);
[6a51ef]1409  library_stack = ls->next;
[c232af]1410  omFreeBin((ADDRESS)ls,  libstack_bin);
[6a51ef]1411  return(library_stack);
1412}
1413
1414#endif /* HAVE_LIBPARSER */
1415/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
Note: See TracBrowser for help on using the repository browser.