source: git/Singular/iplib.cc @ 3542f7

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