source: git/Singular/iplib.cc @ f92a39

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