source: git/Singular/iplib.cc @ b2aa08

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