source: git/Singular/iplib.cc @ 2f8028

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