source: git/Singular/iplib.cc @ ce21e4a

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