source: git/Singular/iplib.cc @ 95ba43

spielwiese
Last change on this file since 95ba43 was 95ba43, checked in by Olaf Bachmann <obachman@…>, 26 years ago
some changes with Kai git-svn-id: file:///usr/local/Singular/svn/trunk@1606 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.22 1998-05-05 13:20:46 obachman 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(yylp_errno) {
511    Werror("Library %s: ERROR occured: in line %d, %d.", newlib, yylplineno,
512         current_pos(0));
513    Werror(yylp_errlist[yylp_errno], yylplineno);
514    Werror("Cannot load library,... aborting.");
515    reinit_yylp();
516    fclose( yylpin );
517    FreeL((ADDRESS)newlib);
518    return TRUE;
519  }
520  if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loaded %s", libnamebuf);
521  if( (lib_style == OLD_LIBSTYLE) && (BVERBOSE(V_LOAD_LIB)))
522    Warn( "library %s has an old format. Please fix it for the next time",
523          newlib);
524  else {
525    if (BVERBOSE(V_LOAD_LIB)) Print("\n");
526  }
527  reinit_yylp();
528  fclose( yylpin );
529  {
530    libstackv ls;
531    for(ls = library_stack; (ls != NULL) && (ls != ls_start); ) {
532      if(ls->to_be_done) {
533        //Print("// Processing id %d LIB:%s\n", ls->cnt, ls->get());
534        ls->to_be_done=FALSE;
535        iiLibCmd(ls->get());
536        ls = ls->pop(newlib);
537        //Print("Done\n");
538      }
539    }
540#if 0
541    Print("--------------------\n");
542    for(ls = library_stack; ls != NULL; ls = ls->next) {
543      Print("%s: LIB-stack:(%d), %s %s\n", newlib, ls->cnt, ls->get(),
544        ls->to_be_done ? "not loaded" : "loaded");
545    }
546    Print("--------------------\n");
547#endif
548  }
549#else /* HAVE_LIBPARSER */
550  // processing head section
551  if (fgets( buf, sizeof(buf), fp))
552  {
553    if (BVERBOSE(V_LOAD_LIB))
554    {
555      if (strncmp( buf, "// $Id", 5) == 0)
556      {
557        char ver[10];
558        char date[16];
559        ver[0]='?'; ver[1]='.'; ver[2]='?'; ver[3]='\0';
560        date[0]='?'; date[1]='\0';
561        sscanf(buf,"// %*s %*s %10s %16s",ver,date);
562        strcat(libnamebuf,"(");
563        strcat(libnamebuf,ver);
564        strcat(libnamebuf,",");
565        strcat(libnamebuf,date);
566        strcat(libnamebuf,")");
567      }
568      else
569      {
570        strcat(libnamebuf,"(**unknown version**)");
571      }
572      Warn( "loading %s", libnamebuf );
573    }
574  }
575
576  #define IN_HEADER 1
577  #define IN_BODY   2
578  #define IN_EXAMPLE      3
579  #define IN_EXAMPLE_BODY 4
580  #define IN_LIB_HEADER   5
581  int v=-1,otherLines=0,inBlock=IN_LIB_HEADER;
582  do /*while (fgets( buf, sizeof(buf), fp))*/
583  {
584    int  offset;
585    if (buf[0]!='\n')
586    {
587      if ((inBlock==0)||(inBlock==IN_LIB_HEADER))
588      {
589        if (strncmp( buf, "LIB ", 4) == 0)
590        {
591          char *s=buf+5;
592          char *f=strchr(s,'"');
593          if (f!=NULL)
594            *f='\0';
595          else
596            return TRUE;
597          // if (BVERBOSE(V_LOAD_LIB)) Print("// requires %s",s);
598          f=strstr(IDSTRING(hl),s);
599          if (f == NULL)
600          {
601            // if (BVERBOSE(V_LOAD_LIB)) PrintLn();
602            iiLibCmd(mstrdup(s));
603            // if (BVERBOSE(V_LOAD_LIB)) Print( "// loading %s\n", newlib);
604          }
605          //else if (BVERBOSE(V_LOAD_LIB)) PrintS(" -> already loaded\n");
606        }
607        else if (strncmp( buf, "proc ", 5) == 0)
608        {
609          char proc[256];
610          char ct1, *e;
611          sscanf( buf, "proc %s", proc);
612          offset = 2;
613          char *ct=strchr(proc,'(');
614          if (ct!=NULL) { *ct='\0'; offset=3; }
615          sprintf( buf, "LIB:%s", newlib);
616#if 0
617          if(strcmp(proc, "_init")==0)
618          {
619            char *p =  iiConvName(newlib);
620            Print("Init found:%s;\n", p);
621            h = enterid( mstrdup(p), myynest, PROC_CMD, &idroot, FALSE );
622            FreeL((ADDRESS)p);
623          } else
624#endif
625            h = enterid( mstrdup(proc), myynest, PROC_CMD, &idroot, FALSE );
626          if (h!=NULL)
627          {
628            iiInitSingularProcinfo(IDPROC(h),newlib,proc,lines,pos);
629            if (BVERBOSE(V_LOAD_PROC)) Warn( "     proc %s loaded", proc );
630          }
631          inBlock=IN_HEADER;
632        }
633        else if (strncmp( buf, "// ver:", 7) == 0)
634        {
635          v=0;
636          sscanf( buf+7, "%d", &v);
637          if(v!=(SINGULAR_VERSION/100))
638            Warn("version mismatch - library `%s` requires:%d.%d",
639                  newlib,v/1000,(v%1000)/100);
640        }
641        else if (strncmp( buf, "example", 7) == 0)
642        {
643          IDPROC(h)->data.s.example_start = pos;
644          IDPROC(h)->data.s.example_lineno = lines;
645          inBlock=IN_EXAMPLE;
646        }
647        else if (strncmp( buf, "//", 2) != 0)
648        {
649          if (inBlock==0)
650          {
651            otherLines++;
652          }
653        }
654      }
655      else if ((inBlock==IN_HEADER) || (inBlock==IN_EXAMPLE))
656      {
657        if (buf[0]=='{')
658        {
659          if(inBlock==IN_HEADER)
660          {
661            IDPROC(h)->data.s.body_start = pos;
662            IDPROC(h)->data.s.body_lineno = lines-offset;
663            // Print("%s: %d-%d\n", pi->procname, lines, offset);
664          }
665          inBlock=IN_BODY;
666        }
667      }
668      else if ((inBlock==IN_BODY) || (inBlock==IN_EXAMPLE_BODY))
669      {
670        if (buf[0]=='}')
671        {
672          if(IDPROC(h)->data.s.example_start==0)
673            IDPROC(h)->data.s.example_start=pos;
674          if(IDPROC(h)->data.s.body_end==0) IDPROC(h)->data.s.body_end=pos;
675          IDPROC(h)->data.s.proc_end = pos;
676          inBlock=0;
677        }
678      }
679    }
680    lines++;
681    pos = ftell(fp);
682  } while (fgets( buf, sizeof(buf), fp));
683  fclose( fp );
684
685  //if (h!=NULL) IDPROC(h) = pi;
686  if (BVERBOSE(V_DEBUG_LIB))
687  {
688    if (inBlock!=0)
689      Warn("LIB `%s` ends within block",newlib);
690    if (otherLines!=0)
691      Warn("%d lines not recognised in LIB `%s`",otherLines,newlib);
692    if(v==-1)
693      Warn("LIB `%s` has no version flag",newlib);
694  }
695#endif /* HAVE_LIBPARSER */
696  FreeL((ADDRESS)newlib);
697  return FALSE;
698}
699
700/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
701procinfo *iiInitSingularProcinfo(procinfov pi, char *libname, char *procname,
702                                 int line, long pos, BOOLEAN pstatic)
703{
704  pi->libname = mstrdup(libname);
705
706  if( strcmp(procname,"_init")==0)
707  {
708    char *p = iiConvName(libname);
709    pi->procname = mstrdup(p);
710    FreeL((ADDRESS)p);
711  } else pi->procname = mstrdup(procname);
712  pi->language = LANG_SINGULAR;
713  pi->ref = 1;
714  pi->is_static = pstatic;
715  pi->data.s.proc_start = pos;
716  pi->data.s.help_start = 0L;
717  pi->data.s.body_start = 0L;
718  pi->data.s.body_end   = 0L;
719  pi->data.s.example_start = 0L;
720  pi->data.s.proc_lineno = line;
721  pi->data.s.body_lineno = 0;
722  pi->data.s.example_lineno = 0;
723  pi->data.s.body = NULL;
724  return(pi);
725}
726
727#ifdef HAVE_DYNAMIC_LOADING
728/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
729int iiAddCproc(char *libname, char *procname, BOOLEAN pstatic,
730               BOOLEAN(*func)(leftv res, leftv v))
731{
732  procinfov pi;
733  idhdl h;
734
735  h = enterid(mstrdup(procname),0, PROC_CMD, &idroot, FALSE);
736  if ( h!= NULL ) {
737    Print("register binary proc: %s::%s\n", libname, procname);
738    pi = IDPROC(h);
739    pi->libname = mstrdup(libname);
740    pi->procname = mstrdup(procname);
741    pi->language = LANG_C;
742    pi->ref = 1;
743    pi->is_static = pstatic;
744    pi->data.o.function = func;
745    return(1);
746  } else {
747    Print("iiAddCproc: failed.\n");
748  }
749  return(0);
750}
751#endif /* HAVE_DYNAMIC_LOADING */
752
753/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
754char *iiConvName(char *libname)
755{
756  char *p = (char *)AllocL(strlen(libname)+7);
757  char *q = mstrdup(libname);
758  char *r = q;
759  for(; *r!='\0'; r++)
760  {
761    if(*r=='.') *r='_';
762    if(*r==':') *r='_';
763  }
764  sprintf(p, "%s_init\0", q);
765  FreeL((ADDRESS)q);
766  return(p);
767}
768
769/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
770void piShowProcList()
771{
772  idhdl h;
773  procinfo *proc;
774  char *name;
775
776  Print( "%-15s  %20s      %s,%s  %s,%s   %s,%s\n", "Library", "function",
777         "line", "start", "line", "body", "line", "example");
778  for(h = idroot; h != NULL; h = IDNEXT(h))
779  {
780    if(IDTYP(h) == PROC_CMD)
781    {
782      proc = IDPROC(h);
783      if(strcmp(proc->procname, IDID(h))!=0)
784      {
785        name = (char *)AllocL(strlen(IDID(h))+strlen(proc->procname)+4);
786        sprintf(name, "%s -> %s", IDID(h), proc->procname);
787        Print( "%-15s  %20s ", proc->libname, name);
788        FreeL(name);
789      }
790      else
791        Print( "%-15s  %20s ", proc->libname, proc->procname);
792      if(proc->language==LANG_SINGULAR)
793        Print("line %4d,%-5ld  %4d,%-5ld  %4d,%-5ld\n",
794              proc->data.s.proc_lineno, proc->data.s.proc_start,
795              proc->data.s.body_lineno, proc->data.s.body_start,
796              proc->data.s.example_lineno, proc->data.s.example_start);
797      else if(proc->language==LANG_C)
798        Print("type: object\n");
799
800    }
801  }
802}
803
804/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
805//char *iiLineNo(char *procname, int lineno)
806//{
807//  char buf[256];
808//  idhdl pn = ggetid(procname);
809//  procinfo *pi = IDPROC(pn);
810//
811//  sprintf(buf, "%s %3d\0", procname, lineno);
812//  //sprintf(buf, "%s::%s %3d\0", pi->libname, pi->procname,
813//  //  lineno + pi->data.s.body_lineno);
814//  return(buf);
815//}
816/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
817#ifdef HAVE_LIBPARSER
818void libstack::push(char *p, char *libname)
819{
820  idhdl hl = idroot->get("LIB",0);
821  libstackv lp;
822  char *f = NULL;
823  if(hl!=NULL) f = strstr(IDSTRING(hl),libname);
824  if( (hl==NULL) || (f == NULL)) {
825    for(lp = this;lp!=NULL;lp=lp->next) {
826      if(strcmp(lp->get(), libname)==0) break;
827    }
828    if(lp==NULL) {
829      libstackv ls = (libstack *)Alloc0(sizeof(libstack));
830      ls->next = this;
831      ls->libname = mstrdup(libname);
832      ls->to_be_done = TRUE;
833      if(this != NULL) ls->cnt = this->cnt+1; else ls->cnt = 0;
834      library_stack = ls;
835    }
836  }
837}
838
839libstackv libstack::pop(char *p)
840{
841  libstackv ls = this;
842  //FreeL(ls->libname);
843  library_stack = ls->next;
844  Free((ADDRESS)ls, sizeof(libstack));
845  return(library_stack);
846}
847
848#endif /* HAVE_LIBPARSER */
849/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
Note: See TracBrowser for help on using the repository browser.