source: git/Singular/iplib.cc @ d2b2a7

spielwiese
Last change on this file since d2b2a7 was 1d1101, checked in by Kai Krüger <krueger@…>, 26 years ago
Fixed exmaple bug Added HAVE_DYNAMIC_LOADING git-svn-id: file:///usr/local/Singular/svn/trunk@1490 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 21.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: iplib.cc,v 1.21 1998-04-27 14:58:09 krueger 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
14#include "mod2.h"
15#include "tok.h"
16#include "ipid.h"
17#include "mmemory.h"
18#include "febase.h"
19#include "ring.h"
20#include "subexpr.h"
21#include "ipshell.h"
22#include "lists.h"
23#ifdef HAVE_LIBPARSER
24#  include "libparse.h"
25#else /* HAVE_LIBPARSER */
26   procinfo *iiInitSingularProcinfo(procinfov pi, char *libname,
27                                    char *procname, int line, long pos,
28                                    BOOLEAN pstatic = FALSE);
29#endif /* HAVE_LIBPARSER */
30
31char *iiConvName(char *p);
32#ifdef HAVE_LIBPARSER
33void yylprestart (FILE *input_file );
34int current_pos(int i=0);
35extern int yylp_errno;
36extern int yylplineno;
37extern char *yylp_errlist[];
38void print_init();
39libstackv library_stack;
40#endif
41
42/*2
43* find the library of an proc:
44*  => return (pi->libname)
45*/
46char * iiGetLibName(procinfov pi)
47{
48  char *res=NULL;
49
50  res = pi->libname;
51  return res;
52}
53/*2
54* given a line 'proc[ ]+{name}[ \t]*'
55* return a pointer to name and set the end of '\0'
56* changes the input!
57* returns: e: pointer to 'end of name'
58*          ct: changed char at the end of s
59*/
60char* iiProcName(char *buf, char & ct, char* &e)
61{
62  char *s=buf+5;
63  while (*s==' ') s++;
64  e=s+1;
65  while ((*e>' ') && (*e!='(')) e++;
66  ct=*e;
67  *e='\0';
68  return s;
69}
70
71/*2
72* given a line with args, return the argstr
73*/
74char * iiProcArgs(char *e,BOOLEAN withParenth)
75{
76  while ((*e==' ') || (*e=='\t') || (*e=='(')) e++;
77  if (*e<' ')
78  {
79    if (withParenth)
80    {
81      // no argument list, allow list #
82      return mstrdup("parameter list #;");
83    }
84    else
85    {
86      // empty list
87      return mstrdup("");
88    }
89  }
90  BOOLEAN in_args;
91  BOOLEAN args_found;
92  char *s;
93  char *argstr=(char *)AllocL(200);
94  *argstr='\0';
95  do
96  {
97    args_found=FALSE;
98    s=e; // set s to the starting point of the arg
99         // and search for the end
100    while ((*e!=',')&&(*e!=')')&&(*e!='\0'))
101    {
102      args_found=args_found || (*e>' ');
103      e++;
104    }
105    in_args=(*e==',');
106    if (args_found)
107    {
108      *e='\0';
109      // copy the result to argstr
110      strcat(argstr,"parameter ");
111      strcat(argstr,s);
112      strcat(argstr,"; ");
113      e++; // e was pointing to ','
114    }
115  } while (in_args);
116  return argstr;
117}
118
119/*2
120* locate `procname` in lib `libname` and find the part `part`:
121*  part=0: help, between, but excluding the line "proc ..." and "{...":
122*    => return
123*  part=1: body, between "{ ..." and "}", including the 1. line, w/o "{"
124*    => set pi->data.s.body, return NULL
125*  part=2: example, between, but excluding the line "exapmle {..." and "}":
126*    => return
127*/
128char* iiGetLibProcBuffer(procinfo *pi, int part )
129{
130  char buf[256], *s = NULL, *p;
131  long procbuflen;
132
133  FILE * fp = feFopen( pi->libname, "rb", NULL, TRUE );
134  if (fp==NULL)
135  {
136    return NULL;
137  }
138
139  fseek(fp, pi->data.s.proc_start, SEEK_SET);
140  if(part==0)
141  { // load help string
142    procbuflen = pi->data.s.body_start - pi->data.s.proc_start;
143    //Print("Help=%ld-%ld=%d\n", pi->data.s.body_start,
144    //    pi->data.s.proc_start, procbuflen);
145    s = (char *)AllocL(procbuflen);
146    myfread(s, procbuflen, 1, fp);
147    s[procbuflen] = '\0';
148    return(s);
149  }
150  if(part==1)
151  { // load proc part
152    procbuflen = pi->data.s.def_end - pi->data.s.proc_start;
153    //fgets(buf, sizeof(buf), fp);
154    myfread( buf, procbuflen, 1, fp);
155    char ct;
156    char *e;
157    s=iiProcName(buf,ct,e);
158    char *argstr=NULL;
159    *e=ct;
160    argstr=iiProcArgs(e,TRUE);
161    procbuflen = pi->data.s.body_end - pi->data.s.body_start;
162    pi->data.s.body = (char *)AllocL( strlen(argstr)+procbuflen+15+
163                                      strlen(pi->libname) );
164    //Print("Body=%ld-%ld=%d\n", pi->data.s.body_end,
165    //    pi->data.s.body_start, procbuflen);
166    if (pi->data.s.body==NULL)
167    {
168      Werror( "unable to allocate proc buffer `%s`", pi->procname );
169      return NULL;
170    }
171    fseek(fp, pi->data.s.body_start, SEEK_SET);
172    strcpy(pi->data.s.body,argstr);
173    myfread( pi->data.s.body+strlen(argstr), procbuflen, 1, fp);
174    procbuflen+=strlen(argstr);
175    FreeL(argstr);
176    fclose( fp );
177    pi->data.s.body[procbuflen] = '\0';
178    strcat( pi->data.s.body+procbuflen, "\n;return();\n\n" );
179    strcat( pi->data.s.body+procbuflen+13,pi->libname);
180    s=strchr(pi->data.s.body,'{');
181    if (s!=NULL) *s=' ';
182    return NULL;
183  }
184  if(part==2)
185  { // load example
186    fseek(fp, pi->data.s.example_start, SEEK_SET);
187    fgets(buf, sizeof(buf), fp);
188    procbuflen = pi->data.s.proc_end - pi->data.s.example_start - strlen(buf);
189    //Print("Example=%ld-%ld=%d\n", pi->data.s.proc_end,
190    //  pi->data.s.example_start, procbuflen);
191    s = (char *)AllocL(procbuflen+14);
192    myfread(s, procbuflen, 1, fp);
193    s[procbuflen] = '\0';
194    strcat(s+procbuflen-3, "\n;return();\n\n" );
195    p=strchr(s,'{');
196    if (p!=NULL) *p=' ';
197    return(s);
198  }
199  return NULL;
200}
201
202/*2
203* start a proc
204* parameters are built as exprlist
205* TODO:interrupt
206* return FALSE on success, TRUE if an error occurs
207*/
208BOOLEAN iiPStart(idhdl pn, sleftv  * v)
209{
210  char * str;
211  BOOLEAN err=FALSE;
212  int old_echo=si_echo;
213
214  /* init febase ======================================== */
215  /* we do not enter this case if filename != NULL !! */
216  if (pn!=NULL)
217  {
218    procinfov pi;
219    pi = IDPROC(pn);
220    if(pi!=NULL)
221    {
222      if( pi->data.s.body==NULL )
223      {
224        iiGetLibProcBuffer(IDPROC(pn));
225        if (IDPROC(pn)->data.s.body==NULL) return TRUE;
226      }
227      newBuffer( mstrdup(IDPROC(pn)->data.s.body), BT_proc,
228                 pi, IDPROC(pn)->data.s.body_lineno );
229    }
230    //else
231    //{ // for security only
232    //  newBuffer( mstrdup(IDSTRING(pn)), BT_proc, IDID(pn) );
233    //}
234  }
235  /* generate argument list ======================================*/
236  if (v!=NULL)
237  {
238    iiCurrArgs=(leftv)Alloc(sizeof(sleftv));
239    memcpy(iiCurrArgs,v,sizeof(sleftv));
240    memset(v,0,sizeof(sleftv));
241  }
242  else
243  {
244    iiCurrArgs=NULL;
245  }
246  /* start interpreter ======================================*/
247  myynest++;
248  //Print("%-*.*s PStart <<%s>>, level:%d\n",2*myynest,2*myynest," ",IDID(pn),myynest);
249  err=yyparse();
250  killlocals(myynest);
251  myynest--;
252  si_echo=old_echo;
253  //Print("%-*.*s PEnd  <<%s>>, level:%d\n",2*myynest,2*myynest," ",IDID(pn),myynest);
254
255  return err;
256}
257
258ring    *iiLocalRing
259#ifdef TEST
260                    =NULL
261#endif
262                   ;
263sleftv  *iiRETURNEXPR
264#ifdef TEST
265                    =NULL
266#endif
267                   ;
268int     iiRETURNEXPR_len=0;
269
270#ifdef RDEBUG
271static void iiShowLevRings()
272{
273  int i;
274  for (i=1;i<=myynest;i++)
275  {
276    Print("lev %d:",i);
277    if (iiLocalRing[i]==NULL) PrintS("NULL");
278    else                      Print("%d",iiLocalRing[i]);
279    Print("\n");
280  }
281  if (currRing==NULL) PrintS("curr:NULL\n");
282  else                Print ("curr:%d\n",currRing->no);
283}
284#endif
285
286static void iiCheckNest()
287{
288  if (myynest >= iiRETURNEXPR_len-1)
289  {
290    iiRETURNEXPR=(sleftv *)ReAlloc(iiRETURNEXPR,
291                                   iiRETURNEXPR_len*sizeof(sleftv),
292                                   (iiRETURNEXPR_len+16)*sizeof(sleftv));
293    iiLocalRing=(ring *)ReAlloc(iiLocalRing,
294                                   iiRETURNEXPR_len*sizeof(ring),
295                                   (iiRETURNEXPR_len+16)*sizeof(ring));
296    iiRETURNEXPR_len+=16;
297  }
298}
299sleftv * iiMake_proc(idhdl pn, sleftv* sl)
300{
301  int err;
302  procinfov pi = IDPROC(pn);
303  if(pi->is_static && myynest==0) {
304    Werror("'%s::%s()' is a local procedure and cannot be accessed by an user.",
305           pi->libname, pi->procname);
306    return NULL;
307  }
308  iiCheckNest();
309  iiLocalRing[myynest]=currRing;
310  iiRETURNEXPR[myynest+1].Init();
311  if (traceit&TRACE_SHOW_PROC)
312  {
313    if (traceit&TRACE_SHOW_LINENO) PrintLn();
314    Print("entering%-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest);
315  }
316#ifdef RDEBUG
317  if (traceit&TRACE_SHOW_RINGS) iiShowLevRings();
318#endif
319#if 1
320  if(pi->language == LANG_SINGULAR) err=iiPStart(pn,sl);
321  if(pi->language == LANG_C)
322  {
323    leftv res = (leftv)Alloc0(sizeof(sleftv));
324    res->rtyp=NONE;
325    err = (pi->data.o.function)(res, sl);
326    iiRETURNEXPR[myynest+1].Copy(res);
327    Free((ADDRESS)res, sizeof(sleftv));
328  }
329#else
330  switch (pi->language)
331  {
332    case LANG_SINGULAR: err=iiPStart(pn,sl); break;
333    case LANG_C: leftv res = (leftv)Alloc0(sizeof(sleftv));
334      err = (pi->data.o.function)(res, sl);
335      iiRETURNEXPR[myynest+1].Copy(res);
336      Free((ADDRESS)res, sizeof(sleftv));
337      break;
338    default: err=TRUE;
339  }
340#endif
341  if (traceit&TRACE_SHOW_PROC)
342  {
343    if (traceit&TRACE_SHOW_LINENO) PrintLn();
344    Print("leaving %-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest);
345  }
346#ifdef RDEBUG
347  if (traceit&TRACE_SHOW_RINGS) iiShowLevRings();
348#endif
349  if (err)
350  {
351    iiRETURNEXPR[myynest+1].CleanUp();
352    iiRETURNEXPR[myynest+1].Init();
353  }
354  if (iiLocalRing[myynest] != currRing)
355  {
356    if (((iiRETURNEXPR[myynest+1].Typ()>BEGIN_RING)
357      && (iiRETURNEXPR[myynest+1].Typ()<END_RING))
358    || ((iiRETURNEXPR[myynest+1].Typ()==LIST_CMD)
359      && (lRingDependend((lists)iiRETURNEXPR[myynest+1].Data()))))
360    {
361      //idhdl hn;
362      char *n;
363      char *o;
364      if (iiLocalRing[myynest]!=NULL) o=rFindHdl(iiLocalRing[myynest],NULL)->id;
365      else                            o="none";
366      if (currRing!=NULL)             n=rFindHdl(currRing,NULL)->id;
367      else                            n="none";
368      Werror("ring change during procedure call: %s -> %s",o,n);
369      iiRETURNEXPR[myynest+1].CleanUp();
370      err=TRUE;
371    }
372    if (iiLocalRing[myynest]!=NULL)
373    {
374      rSetHdl(rFindHdl(iiLocalRing[myynest],NULL),TRUE);
375      iiLocalRing[myynest]=NULL;
376    }
377    else
378    { currRingHdl=NULL; currRing=NULL; }
379  }
380  if (iiCurrArgs!=NULL)
381  {
382    Warn("too many arguments for %s",IDID(pn));
383    iiCurrArgs->CleanUp();
384    Free((ADDRESS)iiCurrArgs,sizeof(sleftv));
385    iiCurrArgs=NULL;
386  }
387  if (err) return NULL;
388  return &iiRETURNEXPR[myynest+1];
389}
390
391/*2
392* start an example (as a proc),
393* destroys the string 'example'
394*/
395BOOLEAN iiEStart(char* example, procinfo *pi)
396{
397  BOOLEAN err;
398  int old_echo=si_echo;
399
400  newBuffer( example, BT_example, pi, pi->data.s.example_lineno );
401  iiCheckNest();
402  iiLocalRing[myynest]=currRing;
403  if (traceit&TRACE_SHOW_PROC)
404  {
405    if (traceit&TRACE_SHOW_LINENO) printf("\n");
406    printf("entering example (level %d)\n",myynest);
407  }
408  myynest++;
409  err=yyparse();
410  killlocals(myynest);
411  myynest--;
412  si_echo=old_echo;
413  if (traceit&TRACE_SHOW_PROC)
414  {
415    if (traceit&TRACE_SHOW_LINENO) printf("\n");
416    printf("leaving  -example- (level %d)\n",myynest);
417  }
418  if (iiLocalRing[myynest] != currRing)
419  {
420    if (iiLocalRing[myynest]!=NULL)
421    {
422      rSetHdl(rFindHdl(iiLocalRing[myynest],NULL),TRUE);
423      iiLocalRing[myynest]=NULL;
424    }
425    else
426    {
427      currRingHdl=NULL;
428      currRing=NULL;
429    }
430  }
431  return err;
432}
433
434/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
435BOOLEAN iiLibCmd( char *newlib, BOOLEAN tellerror )
436{
437  char buf[256];
438  char libnamebuf[128];
439  idhdl h,hl;
440  int lines = 1;
441  long pos = 0L;
442  procinfov pi;
443  FILE * fp = feFopen( newlib, "r", libnamebuf, tellerror );
444  if (fp==NULL)
445  {
446    return TRUE;
447  }
448  hl = idroot->get("LIB",0);
449  if (hl==NULL)
450  {
451    hl = enterid( mstrdup("LIB"),0, STRING_CMD, &idroot, FALSE );
452    IDSTRING(hl) = mstrdup(newlib);
453  }
454  else
455  {
456#ifdef TEST
457    if (IDSTRING(hl) != NULL)
458#endif
459    {
460      char *s = (char *)AllocL( strlen(newlib) + strlen(IDSTRING(hl)) + 2 );
461      strcpy(s,IDSTRING(hl));
462      BOOLEAN f=FALSE;
463      if(strchr(s,',')==NULL)
464      {
465        if (strcmp(s,newlib)==0)
466          f=TRUE;
467      }
468      else
469      {
470        char *p=strtok(s,",");
471        do
472        {
473          if(strcmp(p,newlib)==0)
474          {
475            f=TRUE;
476            break;
477          }
478          p=strtok(NULL,",");
479        } while (p!=NULL);
480      }
481      if (f)
482        FreeL(s);
483      else
484      {
485        sprintf( s, "%s,%s", IDSTRING(hl), newlib);
486        FreeL((ADDRESS)IDSTRING(hl));
487        IDSTRING(hl) = s;
488      }
489    }
490#ifdef TEST
491    else
492    {
493      PrintS("## empty LIB string\n");
494      IDSTRING(hl) = mstrdup(newlib);
495    }
496#endif
497  }
498
499#ifdef HAVE_LIBPARSER
500  extern FILE *yylpin;
501  libstackv ls_start = library_stack;
502  lib_style_types lib_style;
503
504  yylpin = fp;
505# if YYLPDEBUG > 1
506  print_init();
507#  endif
508  if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loading %s...", libnamebuf);
509  yylplex(newlib, libnamebuf, &lib_style);
510  if( (lib_style == OLD_LIBSTYLE) && (BVERBOSE(V_LOAD_LIB)))
511    Warn( "library %s has an old format. Please fix it for the next time",
512          newlib);
513  else {
514    if (BVERBOSE(V_LOAD_LIB)) Print("done.\n");
515  }
516  if(yylp_errno) {
517    Werror("Library %s: ERROR occured: in line %d, %d.", newlib, yylplineno,
518         current_pos(0));
519    Werror(yylp_errlist[yylp_errno], yylplineno);
520    Werror("Cannot load library,... aborting.");
521    reinit_yylp();
522    fclose( yylpin );
523    FreeL((ADDRESS)newlib);
524    return TRUE;
525  }
526  reinit_yylp();
527  fclose( yylpin );
528  {
529    libstackv ls;
530    for(ls = library_stack; (ls != NULL) && (ls != ls_start); ) {
531      if(ls->to_be_done) {
532        //Print("// Processing id %d LIB:%s\n", ls->cnt, ls->get());
533        ls->to_be_done=FALSE;
534        iiLibCmd(ls->get());
535        ls = ls->pop(newlib);
536        //Print("Done\n");
537      }
538    }
539#if 0
540    Print("--------------------\n");
541    for(ls = library_stack; ls != NULL; ls = ls->next) {
542      Print("%s: LIB-stack:(%d), %s %s\n", newlib, ls->cnt, ls->get(),
543        ls->to_be_done ? "not loaded" : "loaded");
544    }
545    Print("--------------------\n");
546#endif
547  }
548#else /* HAVE_LIBPARSER */
549  // processing head section
550  if (fgets( buf, sizeof(buf), fp))
551  {
552    if (BVERBOSE(V_LOAD_LIB))
553    {
554      if (strncmp( buf, "// $Id", 5) == 0)
555      {
556        char ver[10];
557        char date[16];
558        ver[0]='?'; ver[1]='.'; ver[2]='?'; ver[3]='\0';
559        date[0]='?'; date[1]='\0';
560        sscanf(buf,"// %*s %*s %10s %16s",ver,date);
561        strcat(libnamebuf,"(");
562        strcat(libnamebuf,ver);
563        strcat(libnamebuf,",");
564        strcat(libnamebuf,date);
565        strcat(libnamebuf,")");
566      }
567      else
568      {
569        strcat(libnamebuf,"(**unknown version**)");
570      }
571      Warn( "loading %s", libnamebuf );
572    }
573  }
574
575  #define IN_HEADER 1
576  #define IN_BODY   2
577  #define IN_EXAMPLE      3
578  #define IN_EXAMPLE_BODY 4
579  #define IN_LIB_HEADER   5
580  int v=-1,otherLines=0,inBlock=IN_LIB_HEADER;
581  do /*while (fgets( buf, sizeof(buf), fp))*/
582  {
583    int  offset;
584    if (buf[0]!='\n')
585    {
586      if ((inBlock==0)||(inBlock==IN_LIB_HEADER))
587      {
588        if (strncmp( buf, "LIB ", 4) == 0)
589        {
590          char *s=buf+5;
591          char *f=strchr(s,'"');
592          if (f!=NULL)
593            *f='\0';
594          else
595            return TRUE;
596          // if (BVERBOSE(V_LOAD_LIB)) Print("// requires %s",s);
597          f=strstr(IDSTRING(hl),s);
598          if (f == NULL)
599          {
600            // if (BVERBOSE(V_LOAD_LIB)) PrintLn();
601            iiLibCmd(mstrdup(s));
602            // if (BVERBOSE(V_LOAD_LIB)) Print( "// loading %s\n", newlib);
603          }
604          //else if (BVERBOSE(V_LOAD_LIB)) PrintS(" -> already loaded\n");
605        }
606        else if (strncmp( buf, "proc ", 5) == 0)
607        {
608          char proc[256];
609          char ct1, *e;
610          sscanf( buf, "proc %s", proc);
611          offset = 2;
612          char *ct=strchr(proc,'(');
613          if (ct!=NULL) { *ct='\0'; offset=3; }
614          sprintf( buf, "LIB:%s", newlib);
615#if 0
616          if(strcmp(proc, "_init")==0)
617          {
618            char *p =  iiConvName(newlib);
619            Print("Init found:%s;\n", p);
620            h = enterid( mstrdup(p), myynest, PROC_CMD, &idroot, FALSE );
621            FreeL((ADDRESS)p);
622          } else
623#endif
624            h = enterid( mstrdup(proc), myynest, PROC_CMD, &idroot, FALSE );
625          if (h!=NULL)
626          {
627            iiInitSingularProcinfo(IDPROC(h),newlib,proc,lines,pos);
628            if (BVERBOSE(V_LOAD_PROC)) Warn( "     proc %s loaded", proc );
629          }
630          inBlock=IN_HEADER;
631        }
632        else if (strncmp( buf, "// ver:", 7) == 0)
633        {
634          v=0;
635          sscanf( buf+7, "%d", &v);
636          if(v!=(SINGULAR_VERSION/100))
637            Warn("version mismatch - library `%s` requires:%d.%d",
638                  newlib,v/1000,(v%1000)/100);
639        }
640        else if (strncmp( buf, "example", 7) == 0)
641        {
642          IDPROC(h)->data.s.example_start = pos;
643          IDPROC(h)->data.s.example_lineno = lines;
644          inBlock=IN_EXAMPLE;
645        }
646        else if (strncmp( buf, "//", 2) != 0)
647        {
648          if (inBlock==0)
649          {
650            otherLines++;
651          }
652        }
653      }
654      else if ((inBlock==IN_HEADER) || (inBlock==IN_EXAMPLE))
655      {
656        if (buf[0]=='{')
657        {
658          if(inBlock==IN_HEADER)
659          {
660            IDPROC(h)->data.s.body_start = pos;
661            IDPROC(h)->data.s.body_lineno = lines-offset;
662            // Print("%s: %d-%d\n", pi->procname, lines, offset);
663          }
664          inBlock=IN_BODY;
665        }
666      }
667      else if ((inBlock==IN_BODY) || (inBlock==IN_EXAMPLE_BODY))
668      {
669        if (buf[0]=='}')
670        {
671          if(IDPROC(h)->data.s.example_start==0)
672            IDPROC(h)->data.s.example_start=pos;
673          if(IDPROC(h)->data.s.body_end==0) IDPROC(h)->data.s.body_end=pos;
674          IDPROC(h)->data.s.proc_end = pos;
675          inBlock=0;
676        }
677      }
678    }
679    lines++;
680    pos = ftell(fp);
681  } while (fgets( buf, sizeof(buf), fp));
682  fclose( fp );
683
684  //if (h!=NULL) IDPROC(h) = pi;
685  if (BVERBOSE(V_DEBUG_LIB))
686  {
687    if (inBlock!=0)
688      Warn("LIB `%s` ends within block",newlib);
689    if (otherLines!=0)
690      Warn("%d lines not recognised in LIB `%s`",otherLines,newlib);
691    if(v==-1)
692      Warn("LIB `%s` has no version flag",newlib);
693  }
694#endif /* HAVE_LIBPARSER */
695  FreeL((ADDRESS)newlib);
696  return FALSE;
697}
698
699/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
700procinfo *iiInitSingularProcinfo(procinfov pi, char *libname, char *procname,
701                                 int line, long pos, BOOLEAN pstatic)
702{
703  pi->libname = mstrdup(libname);
704
705  if( strcmp(procname,"_init")==0)
706  {
707    char *p = iiConvName(libname);
708    pi->procname = mstrdup(p);
709    FreeL((ADDRESS)p);
710  } else pi->procname = mstrdup(procname);
711  pi->language = LANG_SINGULAR;
712  pi->ref = 1;
713  pi->is_static = pstatic;
714  pi->data.s.proc_start = pos;
715  pi->data.s.help_start = 0L;
716  pi->data.s.body_start = 0L;
717  pi->data.s.body_end   = 0L;
718  pi->data.s.example_start = 0L;
719  pi->data.s.proc_lineno = line;
720  pi->data.s.body_lineno = 0;
721  pi->data.s.example_lineno = 0;
722  pi->data.s.body = NULL;
723  return(pi);
724}
725
726#ifdef HAVE_DYNAMIC_LOADING
727/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
728int iiAddCproc(char *libname, char *procname, BOOLEAN pstatic,
729               BOOLEAN(*func)(leftv res, leftv v))
730{
731  procinfov pi;
732  idhdl h;
733
734  h = enterid(mstrdup(procname),0, PROC_CMD, &idroot, FALSE);
735  if ( h!= NULL ) {
736    Print("register binary proc: %s::%s\n", libname, procname);
737    pi = IDPROC(h);
738    pi->libname = mstrdup(libname);
739    pi->procname = mstrdup(procname);
740    pi->language = LANG_C;
741    pi->ref = 1;
742    pi->is_static = pstatic;
743    pi->data.o.function = func;
744    return(1);
745  } else {
746    Print("iiAddCproc: failed.\n");
747  }
748  return(0);
749}
750#endif /* HAVE_DYNAMIC_LOADING */
751
752/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
753char *iiConvName(char *libname)
754{
755  char *p = (char *)AllocL(strlen(libname)+7);
756  char *q = mstrdup(libname);
757  char *r = q;
758  for(; *r!='\0'; r++)
759  {
760    if(*r=='.') *r='_';
761    if(*r==':') *r='_';
762  }
763  sprintf(p, "%s_init\0", q);
764  FreeL((ADDRESS)q);
765  return(p);
766}
767
768/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
769void piShowProcList()
770{
771  idhdl h;
772  procinfo *proc;
773  char *name;
774
775  Print( "%-15s  %20s      %s,%s  %s,%s   %s,%s\n", "Library", "function",
776         "line", "start", "line", "body", "line", "example");
777  for(h = idroot; h != NULL; h = IDNEXT(h))
778  {
779    if(IDTYP(h) == PROC_CMD)
780    {
781      proc = IDPROC(h);
782      if(strcmp(proc->procname, IDID(h))!=0)
783      {
784        name = (char *)AllocL(strlen(IDID(h))+strlen(proc->procname)+4);
785        sprintf(name, "%s -> %s", IDID(h), proc->procname);
786        Print( "%-15s  %20s ", proc->libname, name);
787        FreeL(name);
788      }
789      else
790        Print( "%-15s  %20s ", proc->libname, proc->procname);
791      if(proc->language==LANG_SINGULAR)
792        Print("line %4d,%-5ld  %4d,%-5ld  %4d,%-5ld\n",
793              proc->data.s.proc_lineno, proc->data.s.proc_start,
794              proc->data.s.body_lineno, proc->data.s.body_start,
795              proc->data.s.example_lineno, proc->data.s.example_start);
796      else if(proc->language==LANG_C)
797        Print("type: object\n");
798
799    }
800  }
801}
802
803/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
804//char *iiLineNo(char *procname, int lineno)
805//{
806//  char buf[256];
807//  idhdl pn = ggetid(procname);
808//  procinfo *pi = IDPROC(pn);
809//
810//  sprintf(buf, "%s %3d\0", procname, lineno);
811//  //sprintf(buf, "%s::%s %3d\0", pi->libname, pi->procname,
812//  //  lineno + pi->data.s.body_lineno);
813//  return(buf);
814//}
815/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
816#ifdef HAVE_LIBPARSER
817void libstack::push(char *p, char *libname)
818{
819  idhdl hl = idroot->get("LIB",0);
820  libstackv lp;
821  char *f = NULL;
822  if(hl!=NULL) f = strstr(IDSTRING(hl),libname);
823  if( (hl==NULL) || (f == NULL)) {
824    for(lp = this;lp!=NULL;lp=lp->next) {
825      if(strcmp(lp->get(), libname)==0) break;
826    }
827    if(lp==NULL) {
828      libstackv ls = (libstack *)Alloc0(sizeof(libstack));
829      ls->next = this;
830      ls->libname = mstrdup(libname);
831      ls->to_be_done = TRUE;
832      if(this != NULL) ls->cnt = this->cnt+1; else ls->cnt = 0;
833      library_stack = ls;
834    }
835  }
836}
837
838libstackv libstack::pop(char *p)
839{
840  libstackv ls = this;
841  //FreeL(ls->libname);
842  library_stack = ls->next;
843  Free((ADDRESS)ls, sizeof(libstack));
844  return(library_stack);
845}
846
847#endif /* HAVE_LIBPARSER */
848/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
Note: See TracBrowser for help on using the repository browser.