source: git/Singular/iplib.cc @ a71a00

spielwiese
Last change on this file since a71a00 was a71a00, checked in by Hans Schoenemann <hannes@…>, 13 years ago
iiAllStart for iiPStart and iiEStart git-svn-id: file:///usr/local/Singular/svn/trunk@14348 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
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    iiRETURNEXPR=(sleftv *)omreallocSize(iiRETURNEXPR,
420                                   iiRETURNEXPR_len*sizeof(sleftv),
421                                   (iiRETURNEXPR_len+16)*sizeof(sleftv));
422    omMarkAsStaticAddr(iiRETURNEXPR);
423    memset(&(iiRETURNEXPR[iiRETURNEXPR_len]),0,16*sizeof(sleftv));
424#ifdef USE_IILOCALRING
425    iiLocalRing=(ring *)omreallocSize(iiLocalRing,
426                                   iiRETURNEXPR_len*sizeof(ring),
427                                   (iiRETURNEXPR_len+16)*sizeof(ring));
428    memset(&(iiLocalRing[iiRETURNEXPR_len]),0,16*sizeof(ring));
429#endif
430    iiRETURNEXPR_len+=16;
431  }
432}
433sleftv * iiMake_proc(idhdl pn, package pack, sleftv* sl)
434{
435  int err;
436  procinfov pi = IDPROC(pn);
437  if(pi->is_static && myynest==0)
438  {
439    Werror("'%s::%s()' is a local procedure and cannot be accessed by an user.",
440           pi->libname, pi->procname);
441    return NULL;
442  }
443  iiCheckNest();
444#ifdef USE_IILOCALRING
445  iiLocalRing[myynest]=currRing;
446  //Print("currRing(%d):%s(%x) in %s\n",myynest,IDID(currRingHdl),currRing,IDID(pn));
447#endif
448  iiRETURNEXPR[myynest+1].Init();
449  procstack->push(pi->procname);
450  if ((traceit&TRACE_SHOW_PROC)
451  || (pi->trace_flag&TRACE_SHOW_PROC))
452  {
453    if (traceit&TRACE_SHOW_LINENO) PrintLn();
454    Print("entering%-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest);
455  }
456#ifdef RDEBUG
457  if (traceit&TRACE_SHOW_RINGS) iiShowLevRings();
458#endif
459  switch (pi->language)
460  {
461    default:
462    case LANG_NONE:
463                 WerrorS("undefined proc");
464                 err=TRUE;
465                 break;
466
467    case LANG_SINGULAR:
468                 if ((pi->pack!=NULL)&&(currPack!=pi->pack))
469                 {
470                   currPack=pi->pack;
471                   iiCheckPack(currPack);
472                   currPackHdl=packFindHdl(currPack);
473                   //Print("set pack=%s\n",IDID(currPackHdl));
474                 }
475                 else if ((pack!=NULL)&&(currPack!=pack))
476                 {
477                   currPack=pack;
478                   iiCheckPack(currPack);
479                   currPackHdl=packFindHdl(currPack);
480                   //Print("set pack=%s\n",IDID(currPackHdl));
481                 }
482                 err=iiPStart(pn,sl);
483                 break;
484    case LANG_C:
485                 leftv res = (leftv)omAlloc0Bin(sleftv_bin);
486                 err = (pi->data.o.function)(res, sl);
487                 iiRETURNEXPR[myynest+1].Copy(res);
488                 omFreeBin((ADDRESS)res,  sleftv_bin);
489                 break;
490  }
491  if ((traceit&TRACE_SHOW_PROC)
492  || (pi->trace_flag&TRACE_SHOW_PROC))
493  {
494    if (traceit&TRACE_SHOW_LINENO) PrintLn();
495    Print("leaving %-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest);
496  }
497  //char *n="NULL";
498  //if (currRingHdl!=NULL) n=IDID(currRingHdl);
499  //Print("currRing(%d):%s(%x) after %s\n",myynest,n,currRing,IDID(pn));
500#ifdef RDEBUG
501  if (traceit&TRACE_SHOW_RINGS) iiShowLevRings();
502#endif
503  if (err)
504  {
505    iiRETURNEXPR[myynest+1].CleanUp();
506    //iiRETURNEXPR[myynest+1].Init(); //done by CleanUp
507  }
508#ifdef USE_IILOCALRING
509#if 0
510  if(procstack->cRing != iiLocalRing[myynest]) Print("iiMake_proc: 1 ring not saved procs:%x, iiLocal:%x\n",procstack->cRing, iiLocalRing[myynest]);
511#endif
512  if (iiLocalRing[myynest] != currRing)
513  {
514    if (currRing!=NULL)
515    {
516      if (((iiRETURNEXPR[myynest+1].Typ()>BEGIN_RING)
517        && (iiRETURNEXPR[myynest+1].Typ()<END_RING))
518      || ((iiRETURNEXPR[myynest+1].Typ()==LIST_CMD)
519        && (lRingDependend((lists)iiRETURNEXPR[myynest+1].Data()))))
520      {
521        //idhdl hn;
522        const char *n;
523        const char *o;
524        idhdl nh=NULL, oh=NULL;
525        if (iiLocalRing[myynest]!=NULL)
526          oh=rFindHdl(iiLocalRing[myynest],NULL, NULL);
527        if (oh!=NULL)          o=oh->id;
528        else                   o="none";
529        if (currRing!=NULL)
530          nh=rFindHdl(currRing,NULL, NULL);
531        if (nh!=NULL)          n=nh->id;
532        else                   n="none";
533        Werror("ring change during procedure call: %s -> %s (level %d)",o,n,myynest);
534        iiRETURNEXPR[myynest+1].CleanUp();
535        err=TRUE;
536      }
537    }
538    currRing=iiLocalRing[myynest];
539  }
540  if ((currRing==NULL)
541  && (currRingHdl!=NULL))
542    currRing=IDRING(currRingHdl);
543  else
544  if ((currRing!=NULL) &&
545    ((currRingHdl==NULL)||(IDRING(currRingHdl)!=currRing)
546     ||(IDLEV(currRingHdl)>=myynest)))
547  {
548    rSetHdl(rFindHdl(currRing,NULL, NULL));
549    iiLocalRing[myynest]=NULL;
550  }
551#else /* USE_IILOCALRING */
552  if (procstack->cRing != currRing)
553  {
554    //if (procstack->cRingHdl!=NULL)
555    //Print("procstack:%s,",IDID(procstack->cRingHdl));
556    //if (currRingHdl!=NULL)
557    //Print(" curr:%s\n",IDID(currRingHdl));
558    //Print("pr:%x, curr: %x\n",procstack->cRing,currRing);
559    if (((iiRETURNEXPR[myynest+1].Typ()>BEGIN_RING)
560      && (iiRETURNEXPR[myynest+1].Typ()<END_RING))
561    || ((iiRETURNEXPR[myynest+1].Typ()==LIST_CMD)
562      && (lRingDependend((lists)iiRETURNEXPR[myynest+1].Data()))))
563    {
564      //idhdl hn;
565      char *n;
566      char *o;
567      if (procstack->cRing!=NULL)
568      {
569        //PrintS("reset ring\n");
570        procstack->cRingHdl=rFindHdl(procstack->cRing,NULL, NULL);
571        if (procstack->cRingHdl==NULL)
572          procstack->cRingHdl=
573           rFindHdl(procstack->cRing,NULL,procstack->currPack->idroot);
574        if (procstack->cRingHdl==NULL)
575          procstack->cRingHdl=
576           rFindHdl(procstack->cRing,NULL,basePack->idroot);
577        o=IDID(procstack->cRingHdl);
578        currRing=procstack->cRing;
579        currRingHdl=procstack->cRingHdl;
580      }
581      else                            o="none";
582      if (currRing!=NULL)             n=IDID(currRingHdl);
583      else                            n="none";
584      if (currRing==NULL)
585      {
586        Werror("ring change during procedure call: %s -> %s",o,n);
587        iiRETURNEXPR[myynest+1].CleanUp();
588        err=TRUE;
589      }
590    }
591    if (procstack->cRingHdl!=NULL)
592    {
593      rSetHdl(procstack->cRingHdl);
594    }
595    else
596    { currRingHdl=NULL; currRing=NULL; }
597  }
598#endif /* USE_IILOCALRING */
599  if (iiCurrArgs!=NULL)
600  {
601    if (!err) Warn("too many arguments for %s",IDID(pn));
602    iiCurrArgs->CleanUp();
603    omFreeBin((ADDRESS)iiCurrArgs, sleftv_bin);
604    iiCurrArgs=NULL;
605  }
606  procstack->pop();
607  if (err)
608    return NULL;
609  return &iiRETURNEXPR[myynest+1];
610}
611
612/*2
613* start an example (as a proc),
614* destroys the string 'example'
615*/
616BOOLEAN iiEStart(char* example, procinfo *pi)
617{
618  BOOLEAN err;
619  int old_echo=si_echo;
620
621  iiCheckNest();
622  procstack->push(example);
623#ifdef USE_IILOCALRING
624  iiLocalRing[myynest]=currRing;
625#endif
626  if (traceit&TRACE_SHOW_PROC)
627  {
628    if (traceit&TRACE_SHOW_LINENO) printf("\n");
629    printf("entering example (level %d)\n",myynest);
630  }
631  myynest++;
632  iiRETURNEXPR[myynest].Init();
633
634  err=iiAllStart(pi,example,BT_example,(pi != NULL ? pi->data.s.example_lineno: 0));
635
636  killlocals(myynest);
637  myynest--;
638  si_echo=old_echo;
639  if (traceit&TRACE_SHOW_PROC)
640  {
641    if (traceit&TRACE_SHOW_LINENO) printf("\n");
642    printf("leaving  -example- (level %d)\n",myynest);
643  }
644#ifdef USE_IILOCALRING
645  if (iiLocalRing[myynest] != currRing)
646  {
647    if (iiLocalRing[myynest]!=NULL)
648    {
649      rSetHdl(rFindHdl(iiLocalRing[myynest],NULL, NULL));
650      iiLocalRing[myynest]=NULL;
651    }
652    else
653    {
654      currRingHdl=NULL;
655      currRing=NULL;
656    }
657  }
658#else /* USE_IILOCALRING */
659#endif /* USE_IILOCALRING */
660  if (NS_LRING != currRing)
661  {
662    if (NS_LRING!=NULL)
663    {
664      idhdl rh=procstack->cRingHdl;
665      if ((rh==NULL)||(IDRING(rh)!=NS_LRING))
666        rh=rFindHdl(NS_LRING,NULL, NULL);
667      rSetHdl(rh);
668    }
669    else
670    {
671      currRingHdl=NULL;
672      currRing=NULL;
673    }
674  }
675//#endif /* USE_IILOCALRING */
676  procstack->pop();
677  return err;
678}
679
680/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
681BOOLEAN iiTryLoadLib(leftv v, const char *id)
682{
683  BOOLEAN LoadResult = TRUE;
684  char libnamebuf[128];
685  char *libname = (char *)omAlloc(strlen(id)+5);
686  const char *suffix[] = { "", ".lib", ".so", ".sl", NULL };
687  int i = 0;
688  FILE *fp;
689  package pack;
690  idhdl packhdl;
691  lib_types LT;
692
693  for(i=0; suffix[i] != NULL; i++)
694  {
695    sprintf(libname, "%s%s", id, suffix[i]);
696    *libname = mytolower(*libname);
697    if((LT = type_of_LIB(libname, libnamebuf)) > LT_NOTFOUND)
698    {
699      char *s=omStrDup(libname);
700      char libnamebuf[256];
701
702      if (LT==LT_SINGULAR)
703        LoadResult = iiLibCmd(s, FALSE, FALSE,TRUE);
704      #ifdef HAVE_DYNAMIC_LOADING
705      else if ((LT==LT_ELF) || (LT==LT_HPUX))
706        LoadResult = load_modules(s,libnamebuf,FALSE);
707      #endif
708      if(!LoadResult )
709      {
710        v->name = iiConvName(libname);
711        break;
712      }
713    }
714  }
715  omFree(libname);
716  return LoadResult;
717}
718
719/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
720/* check, if library lib has already been loaded
721   if yes, writes filename of lib into where and returns TRUE,
722      no, returns FALSE
723*/
724BOOLEAN iiLocateLib(const char* lib, char* where)
725{
726  idhdl hl;
727
728  char *p;
729
730  hl = IDROOT->get("LIB", 0);
731  if (hl == NULL || (p=strstr(IDSTRING(hl), lib)) == NULL) return FALSE;
732  if ((p!=IDSTRING(hl)) && (*(p-1)!=',')) return FALSE;
733
734  if (strstr(IDSTRING(hl), ",") == NULL)
735  {
736    strcpy(where, IDSTRING(hl));
737  }
738  else
739  {
740    char* tmp = omStrDup(IDSTRING(hl));
741    char* tok = strtok(tmp, ",");
742    do
743    {
744      if (strstr(tok, lib) != NULL) break;
745      tok = strtok(NULL, ",");
746    }
747    while (tok != NULL);
748    assume(tok != NULL);
749    strcpy(where, tok);
750    omFree(tmp);
751  }
752  return TRUE;
753}
754
755BOOLEAN iiLibCmd( char *newlib, BOOLEAN autoexport, BOOLEAN tellerror, BOOLEAN force )
756{
757  char libnamebuf[128];
758  procinfov pi;
759  idhdl h;
760  idhdl pl;
761  idhdl hl;
762  long pos = 0L;
763  char *plib = iiConvName(newlib);
764  FILE * fp = feFopen( newlib, "r", libnamebuf, tellerror );
765  int lines = 1;
766  BOOLEAN LoadResult = TRUE;
767
768  if (fp==NULL)
769  {
770    return TRUE;
771  }
772  pl = basePack->idroot->get(plib,0);
773  if (pl==NULL)
774  {
775    pl = enterid( plib,0, PACKAGE_CMD,
776                  &(basePack->idroot), TRUE );
777    IDPACKAGE(pl)->language = LANG_SINGULAR;
778    IDPACKAGE(pl)->libname=omStrDup(newlib);
779  }
780  else
781  {
782    if(IDTYP(pl)!=PACKAGE_CMD)
783    {
784      WarnS("not of type package.");
785      fclose(fp);
786      return TRUE;
787    }
788    if (!force) return FALSE;
789  }
790  LoadResult = iiLoadLIB(fp, libnamebuf, newlib, pl, autoexport, tellerror);
791  omFree((ADDRESS)newlib);
792
793  if(!LoadResult) IDPACKAGE(pl)->loaded = TRUE;
794  omFree((ADDRESS)plib);
795
796 return LoadResult;
797}
798/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
799static void iiCleanProcs(idhdl &root)
800{
801  idhdl prev=NULL;
802  loop
803  {
804    if (root==NULL) return;
805    if (IDTYP(root)==PROC_CMD)
806    {
807      procinfo *pi=(procinfo*)IDDATA(root);
808      if ((pi->language == LANG_SINGULAR)
809      && (pi->data.s.body_start == 0L))
810      {
811        // procinfo data incorrect:
812        // - no proc body can start at the beginning of the file
813        killhdl(root);
814        if (prev==NULL)
815          root=IDROOT;
816        else
817        {
818          root=prev;
819          prev=NULL;
820        }
821        continue;
822      }
823    }
824    prev=root;
825    root=IDNEXT(root);
826  }
827}
828static void iiRunInit(package p)
829{
830  idhdl h=p->idroot->get("mod_init",0);
831  if (h==NULL) return;
832  if (IDTYP(h)==PROC_CMD)
833  {
834    int save=yylineno;
835    myynest++;
836    procinfo *pi=(procinfo*)IDDATA(h);
837    //PrintS("mod_init found\n");
838    iiMake_proc(h,p,NULL);
839    myynest--;
840    yylineno=save;
841  }
842}
843/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
844BOOLEAN iiLoadLIB(FILE *fp, char *libnamebuf, char*newlib,
845             idhdl pl, BOOLEAN autoexport, BOOLEAN tellerror)
846{
847  extern FILE *yylpin;
848  libstackv ls_start = library_stack;
849  lib_style_types lib_style;
850
851  yylpin = fp;
852  #if YYLPDEBUG > 1
853  print_init();
854  #endif
855  extern int lpverbose;
856  if (BVERBOSE(V_DEBUG_LIB)) lpverbose=1;
857  else lpverbose=0;
858  // yylplex sets also text_buffer
859  if (text_buffer!=NULL) *text_buffer='\0';
860  yylplex(newlib, libnamebuf, &lib_style, pl, autoexport);
861  if(yylp_errno)
862  {
863    Werror("Library %s: ERROR occured: in line %d, %d.", newlib, yylplineno,
864         current_pos(0));
865    if(yylp_errno==YYLP_BAD_CHAR)
866    {
867      Werror(yylp_errlist[yylp_errno], *text_buffer, yylplineno);
868      omFree((ADDRESS)text_buffer);
869      text_buffer=NULL;
870    }
871    else
872      Werror(yylp_errlist[yylp_errno], yylplineno);
873    Werror("Cannot load library,... aborting.");
874    reinit_yylp();
875    fclose( yylpin );
876    iiCleanProcs(IDROOT);
877    return TRUE;
878  }
879  if (BVERBOSE(V_LOAD_LIB))
880    Print( "// ** loaded %s %s\n", libnamebuf, text_buffer);
881  if( (lib_style == OLD_LIBSTYLE) && (BVERBOSE(V_LOAD_LIB)))
882  {
883    Warn( "library %s has old format. This format is still accepted,", newlib);
884    Warn( "but for functionality you may wish to change to the new");
885    Warn( "format. Please refer to the manual for further information.");
886  }
887  reinit_yylp();
888  fclose( yylpin );
889  fp = NULL;
890  iiRunInit(IDPACKAGE(pl));
891
892  {
893    libstackv ls;
894    for(ls = library_stack; (ls != NULL) && (ls != ls_start); )
895    {
896      if(ls->to_be_done)
897      {
898        ls->to_be_done=FALSE;
899        iiLibCmd(ls->get(),autoexport,tellerror,FALSE);
900        ls = ls->pop(newlib);
901      }
902    }
903#if 0
904    PrintS("--------------------\n");
905    for(ls = library_stack; ls != NULL; ls = ls->next)
906    {
907      Print("%s: LIB-stack:(%d), %s %s\n", newlib, ls->cnt, ls->get(),
908        ls->to_be_done ? "not loaded" : "loaded");
909    }
910    PrintS("--------------------\n");
911#endif
912  }
913
914  if(fp != NULL) fclose(fp);
915  return FALSE;
916}
917
918
919/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
920procinfo *iiInitSingularProcinfo(procinfov pi, const char *libname,
921              const char *procname, int line, long pos, BOOLEAN pstatic)
922{
923  pi->libname = omStrDup(libname);
924
925  if( strcmp(procname,"_init")==0)
926  {
927    pi->procname = iiConvName(libname);
928  }
929  else
930    pi->procname = omStrDup(procname);
931  pi->language = LANG_SINGULAR;
932  pi->ref = 1;
933  pi->pack = NULL;
934  pi->is_static = pstatic;
935  pi->data.s.proc_start = pos;
936  pi->data.s.def_end    = 0L;
937  pi->data.s.help_start = 0L;
938  pi->data.s.help_end   = 0L;
939  pi->data.s.body_start = 0L;
940  pi->data.s.body_end   = 0L;
941  pi->data.s.example_start = 0L;
942  pi->data.s.proc_lineno = line;
943  pi->data.s.body_lineno = 0;
944  pi->data.s.example_lineno = 0;
945  pi->data.s.body = NULL;
946  pi->data.s.help_chksum = 0;
947  return(pi);
948}
949
950/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
951int iiAddCproc(char *libname, char *procname, BOOLEAN pstatic,
952               BOOLEAN(*func)(leftv res, leftv v))
953{
954  procinfov pi;
955  idhdl h;
956
957  h = enterid(procname,0, PROC_CMD, &IDROOT, TRUE);
958  if ( h!= NULL )
959  {
960    pi = IDPROC(h);
961    pi->libname = omStrDup(libname);
962    pi->procname = omStrDup(procname);
963    pi->language = LANG_C;
964    pi->ref = 1;
965    pi->is_static = pstatic;
966    pi->data.o.function = func;
967    return(1);
968  }
969  else
970  {
971    PrintS("iiAddCproc: failed.\n");
972  }
973  return(0);
974}
975
976#ifdef HAVE_DYNAMIC_LOADING
977int iiAddCprocTop(char *libname, char *procname, BOOLEAN pstatic,
978               BOOLEAN(*func)(leftv res, leftv v))
979{
980  int r=iiAddCproc(libname,procname,pstatic,func);
981  package s=currPack;
982  currPack=basePack;
983  if (r) r=iiAddCproc(libname,procname,pstatic,func);
984  currPack=s;
985  return r;
986}
987
988/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
989BOOLEAN load_modules(char *newlib, char *fullname, BOOLEAN autoexport)
990{
991#ifdef HAVE_STATIC
992  WerrorS("mod_init: static version can not load modules");
993  return TRUE;
994#else
995  int iiAddCproc(char *libname, char *procname, BOOLEAN pstatic,
996                 BOOLEAN(*func)(leftv res, leftv v));
997  typedef int (*fktn_t)(int(*iiAddCproc)(char *libname, char *procname,
998                               BOOLEAN pstatic,
999                               BOOLEAN(*func)(leftv res, leftv v)));
1000  typedef int (*fktn2_t)(SModulFunctions*);
1001  fktn2_t fktn;
1002  idhdl pl;
1003  char *plib = iiConvName(newlib);
1004  BOOLEAN RET=TRUE;
1005  int token;
1006  char FullName[256];
1007
1008  memset(FullName,0,256);
1009
1010  if( *fullname != '/' &&  *fullname != '.' )
1011    sprintf(FullName, "./%s", newlib);
1012  else strncpy(FullName, fullname,255);
1013
1014
1015  if(IsCmd(plib, token))
1016  {
1017    Werror("'%s' is resered identifier\n", plib);
1018    goto load_modules_end;
1019  }
1020  pl = IDROOT->get(plib,0);
1021  if (pl==NULL)
1022  {
1023    pl = enterid( plib,0, PACKAGE_CMD, &IDROOT,
1024                  TRUE );
1025    IDPACKAGE(pl)->language = LANG_C;
1026    IDPACKAGE(pl)->libname=omStrDup(newlib);
1027  }
1028  else
1029  {
1030    if(IDTYP(pl)!=PACKAGE_CMD)
1031    {
1032      Warn("not of type package.");
1033      goto load_modules_end;
1034    }
1035  }
1036  if((IDPACKAGE(pl)->handle=dynl_open(FullName))==(void *)NULL)
1037  {
1038    Werror("dynl_open failed:%s", dynl_error());
1039    Werror("%s not found", newlib);
1040    goto load_modules_end;
1041  }
1042  else
1043  {
1044    SModulFunctions sModulFunctions;
1045
1046    package s=currPack;
1047    currPack=IDPACKAGE(pl);
1048    fktn = (fktn2_t)dynl_sym(IDPACKAGE(pl)->handle, "mod_init");
1049    if( fktn!= NULL)
1050    {
1051      sModulFunctions.iiArithAddCmd = iiArithAddCmd;
1052      if (autoexport) sModulFunctions.iiAddCproc = iiAddCprocTop;
1053      else            sModulFunctions.iiAddCproc = iiAddCproc;
1054      (*fktn)(&sModulFunctions);
1055    }
1056    else Werror("mod_init: %s\n", dynl_error());
1057    if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loaded %s \n", fullname);
1058    currPack->loaded=1;
1059    currPack=s;
1060  }
1061  RET=FALSE;
1062
1063  load_modules_end:
1064  return RET;
1065#endif /*STATIC */
1066}
1067
1068// loads a dynamic module from the binary path and returns a named function
1069// returns NULL, if something fails
1070void* binary_module_function(const char* newlib, const char* funcname)
1071{
1072  void* result = NULL;
1073
1074#ifdef HAVE_STATIC
1075  WerrorS("static version can not load function from dynamic modules");
1076#else
1077  const char* bin_dir = feGetResource('b');
1078  if (!bin_dir)  { return NULL; }
1079
1080  char path_name[MAXPATHLEN];
1081  sprintf(path_name, "%s%s%s.%s", bin_dir, DIR_SEPP, newlib, MODULE_SUFFIX_STRING);
1082
1083  void* openlib = dynl_open(path_name);
1084  if(!openlib)
1085  {
1086    Werror("dynl_open of %s failed:%s", path_name, dynl_error());
1087    return NULL;
1088  }
1089  result = dynl_sym(openlib, funcname);
1090  if (!result) Werror("%s: %s\n", funcname, dynl_error());
1091#endif
1092
1093  return result;
1094}
1095
1096#endif /* HAVE_DYNAMIC_LOADING */
1097
1098/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1099char mytoupper(char c)
1100{
1101  if(c>=97 && c<=(97+26)) c-=32;
1102  return(c);
1103}
1104
1105char mytolower(char c)
1106{
1107  if(c>=65 && c<=(65+26)) c+=32;
1108  return(c);
1109}
1110
1111/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1112//#if defined(WINNT)
1113//#  define  FS_SEP '\\'
1114//#else
1115//#  define FS_SEP '/'
1116//#endif
1117
1118char *iiConvName(const char *libname)
1119{
1120  char *tmpname = omStrDup(libname);
1121  char *p = strrchr(tmpname, DIR_SEP);
1122  char *r;
1123  if(p==NULL) p = tmpname;
1124  else p++;
1125  r = (char *)strchr(p, '.');
1126  if( r!= NULL) *r = '\0';
1127  r = omStrDup(p);
1128  *r = mytoupper(*r);
1129  // printf("iiConvName: '%s' '%s' => '%s'\n", libname, tmpname, r);
1130  omFree((ADDRESS)tmpname);
1131
1132  return(r);
1133}
1134
1135/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1136#if 0 /* debug only */
1137void piShowProcList()
1138{
1139  idhdl h;
1140  procinfo *proc;
1141  char *name;
1142
1143  Print( "%-15s  %20s      %s,%s  %s,%s   %s,%s\n", "Library", "function",
1144         "line", "start", "line", "body", "line", "example");
1145  for(h = IDROOT; h != NULL; h = IDNEXT(h))
1146  {
1147    if(IDTYP(h) == PROC_CMD)
1148    {
1149      proc = IDPROC(h);
1150      if(strcmp(proc->procname, IDID(h))!=0)
1151      {
1152        name = (char *)omAlloc(strlen(IDID(h))+strlen(proc->procname)+4);
1153        sprintf(name, "%s -> %s", IDID(h), proc->procname);
1154        Print( "%d %-15s  %20s ", proc->is_static ? 1 : 0, proc->libname, name);
1155        omFree((ADDRESS)name);
1156      }
1157      else
1158        Print( "%d %-15s  %20s ", proc->is_static ? 1 : 0, proc->libname,
1159               proc->procname);
1160      if(proc->language==LANG_SINGULAR)
1161        Print("line %4d,%-5ld  %4d,%-5ld  %4d,%-5ld\n",
1162              proc->data.s.proc_lineno, proc->data.s.proc_start,
1163              proc->data.s.body_lineno, proc->data.s.body_start,
1164              proc->data.s.example_lineno, proc->data.s.example_start);
1165      else if(proc->language==LANG_C)
1166        Print("type: object\n");
1167    }
1168  }
1169}
1170#endif
1171
1172/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1173//char *iiLineNo(char *procname, int lineno)
1174//{
1175//  char buf[256];
1176//  idhdl pn = ggetid(procname);
1177//  procinfo *pi = IDPROC(pn);
1178//
1179//  sprintf(buf, "%s %3d\0", procname, lineno);
1180//  //sprintf(buf, "%s::%s %3d\0", pi->libname, pi->procname,
1181//  //  lineno + pi->data.s.body_lineno);
1182//  return(buf);
1183//}
1184/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1185#ifdef HAVE_LIBPARSER
1186void libstack::push(char *p, char *libn)
1187{
1188  libstackv lp;
1189  if( !iiGetLibStatus(libn))
1190  {
1191    for(lp = this;lp!=NULL;lp=lp->next)
1192    {
1193      if(strcmp(lp->get(), libn)==0) break;
1194    }
1195    if(lp==NULL)
1196    {
1197      libstackv ls = (libstack *)omAlloc0Bin(libstack_bin);
1198      ls->next = this;
1199      ls->libname = omStrDup(libn);
1200      ls->to_be_done = TRUE;
1201      if(this != NULL) ls->cnt = this->cnt+1; else ls->cnt = 0;
1202      library_stack = ls;
1203    }
1204  }
1205}
1206
1207libstackv libstack::pop(char *p)
1208{
1209  libstackv ls = this;
1210  //omFree((ADDRESS)ls->libname);
1211  library_stack = ls->next;
1212  omFreeBin((ADDRESS)ls,  libstack_bin);
1213  return(library_stack);
1214}
1215
1216#endif /* HAVE_LIBPARSER */
1217/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
Note: See TracBrowser for help on using the repository browser.