source: git/Singular/iplib.cc @ 24590f

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