source: git/Singular/iplib.cc @ 5abb79f

spielwiese
Last change on this file since 5abb79f was 5abb79f, checked in by Hans Schoenemann <hannes@…>, 5 years ago
silence "extend .... warning for dyn. modules
  • Property mode set to 100644
File size: 36.5 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: interpreter: LIB and help
6*/
7
8#include "kernel/mod2.h"
9
10#include "Singular/tok.h"
11#include "misc/options.h"
12#include "Singular/ipid.h"
13#include "polys/monomials/ring.h"
14#include "Singular/subexpr.h"
15#include "Singular/ipid.h"
16#include "Singular/ipshell.h"
17#include "Singular/fevoices.h"
18#include "Singular/lists.h"
19
20#include <ctype.h>
21
22#if SIZEOF_LONG == 8
23#define SI_MAX_NEST 500
24#elif defined(__CYGWIN__)
25#define SI_MAX_NEST 480
26#else
27#define SI_MAX_NEST 1000
28#endif
29
30#if defined(ix86Mac_darwin) || defined(x86_64Mac_darwin) || defined(ppcMac_darwin)
31#  define MODULE_SUFFIX bundle
32#elif defined(__CYGWIN__)
33#  define MODULE_SUFFIX dll
34#else
35#  define MODULE_SUFFIX so
36#endif
37
38#define MODULE_SUFFIX_STRING EXPANDED_STRINGIFY(MODULE_SUFFIX)
39
40
41#ifdef HAVE_DYNAMIC_LOADING
42BOOLEAN load_modules(const char *newlib, char *fullname, BOOLEAN autoexport);
43#endif
44
45#ifdef HAVE_LIBPARSER
46#  include "libparse.h"
47#else /* HAVE_LIBPARSER */
48procinfo *iiInitSingularProcinfo(procinfov pi, const char *libname,
49              const char *procname, int line, long pos, BOOLEAN pstatic=FALSE);
50#endif /* HAVE_LIBPARSER */
51
52extern int iiArithAddCmd(const char *szName, short nAlias, short nTokval,
53                         short nToktype, short nPos);
54
55#include "Singular/mod_lib.h"
56
57#ifdef HAVE_LIBPARSER
58void yylprestart (FILE *input_file );
59int current_pos(int i=0);
60extern int yylp_errno;
61extern int yylplineno;
62extern char *yylp_errlist[];
63void print_init();
64libstackv library_stack;
65#endif
66
67//int IsCmd(char *n, int tok);
68char mytolower(char c);
69
70/*2
71* return TRUE if the libray libname is already loaded
72*/
73BOOLEAN iiGetLibStatus(char *lib)
74{
75  idhdl hl;
76
77  char *plib = iiConvName(lib);
78  hl = basePack->idroot->get(plib,0);
79  omFree(plib);
80  if((hl==NULL) ||(IDTYP(hl)!=PACKAGE_CMD))
81  {
82    return FALSE;
83  }
84  return (strcmp(lib,IDPACKAGE(hl)->libname)==0);
85}
86
87/*2
88* given a line 'proc[ ]+{name}[ \t]*'
89* return a pointer to name and set the end of '\0'
90* changes the input!
91* returns: e: pointer to 'end of name'
92*          ct: changed char at the end of s
93*/
94char* iiProcName(char *buf, char & ct, char* &e)
95{
96  char *s=buf+5;
97  while (*s==' ') s++;
98  e=s+1;
99  while ((*e>' ') && (*e!='(')) e++;
100  ct=*e;
101  *e='\0';
102  return s;
103}
104
105/*2
106* given a line with args, return the argstr
107*/
108char * iiProcArgs(char *e,BOOLEAN withParenth)
109{
110  while ((*e==' ') || (*e=='\t') || (*e=='(')) e++;
111  if (*e<' ')
112  {
113    if (withParenth)
114    {
115      // no argument list, allow list #
116      return omStrDup("parameter list #;");
117    }
118    else
119    {
120      // empty list
121      return omStrDup("");
122    }
123  }
124  BOOLEAN in_args;
125  BOOLEAN args_found;
126  char *s;
127  char *argstr=(char *)omAlloc(127); // see ../omalloc/omTables.inc
128  int argstrlen=127;
129  *argstr='\0';
130  int par=0;
131  do
132  {
133    args_found=FALSE;
134    s=e; // set s to the starting point of the arg
135         // and search for the end
136    // skip leading spaces:
137    loop
138    {
139      if ((*s==' ')||(*s=='\t'))
140        s++;
141      else if ((*s=='\n')&&(*(s+1)==' '))
142        s+=2;
143      else // start of new arg or \0 or )
144        break;
145    }
146    e=s;
147    while ((*e!=',')
148    &&((par!=0) || (*e!=')'))
149    &&(*e!='\0'))
150    {
151      if (*e=='(') par++;
152      else if (*e==')') par--;
153      args_found=args_found || (*e>' ');
154      e++;
155    }
156    in_args=(*e==',');
157    if (args_found)
158    {
159      *e='\0';
160      // check for space:
161      if ((int)strlen(argstr)+12 /* parameter + ;*/ +(int)strlen(s)>= argstrlen)
162      {
163        argstrlen*=2;
164        char *a=(char *)omAlloc( argstrlen);
165        strcpy(a,argstr);
166        omFree((ADDRESS)argstr);
167        argstr=a;
168      }
169      // copy the result to argstr
170      if(strncmp(s,"alias ",6)!=0)
171      {
172        strcat(argstr,"parameter ");
173      }
174      strcat(argstr,s);
175      strcat(argstr,"; ");
176      e++; // e was pointing to ','
177    }
178  } while (in_args);
179  return argstr;
180}
181
182/*2
183* locate `procname` in lib `libname` and find the part `part`:
184*  part=0: help, between, but excluding the line "proc ..." and "{...":
185*    => return
186*  part=1: body, between "{ ..." and "}", including the 1. line, w/o "{"
187*    => set pi->data.s.body, return NULL
188*  part=2: example, between, but excluding the line "exapmle {..." and "}":
189*    => return
190*/
191char* iiGetLibProcBuffer(procinfo *pi, int part )
192{
193  char buf[256], *s = NULL, *p;
194  long procbuflen;
195
196  FILE * fp = feFopen( pi->libname, "rb", NULL, TRUE );
197  if (fp==NULL)
198  {
199    return NULL;
200  }
201
202  fseek(fp, pi->data.s.proc_start, SEEK_SET);
203  if(part==0)
204  { // load help string
205    int i, offset=0;
206    long head = pi->data.s.def_end - pi->data.s.proc_start;
207    procbuflen = pi->data.s.help_end - pi->data.s.help_start;
208    if (procbuflen<5)
209    {
210      fclose(fp);
211      return NULL; // help part does not exist
212    }
213    //Print("Help=%ld-%ld=%d\n", pi->data.s.body_start,
214    //    pi->data.s.proc_start, procbuflen);
215    s = (char *)omAlloc(procbuflen+head+3);
216    myfread(s, head, 1, fp);
217    s[head] = '\n';
218    fseek(fp, pi->data.s.help_start, SEEK_SET);
219    myfread(s+head+1, procbuflen, 1, fp);
220    fclose(fp);
221    s[procbuflen+head+1] = '\n';
222    s[procbuflen+head+2] = '\0';
223    offset=0;
224    for(i=0;i<=procbuflen+head+2; i++)
225    {
226      if(s[i]=='\\' &&
227         (s[i+1]=='"' || s[i+1]=='{' || s[i+1]=='}' || s[i+1]=='\\'))
228      {
229        i++;
230        offset++;
231      }
232      if(offset>0) s[i-offset] = s[i];
233    }
234    return(s);
235  }
236  else if(part==1)
237  { // load proc part - must exist
238    procbuflen = pi->data.s.def_end - pi->data.s.proc_start;
239    char *ss=(char *)omAlloc(procbuflen+2);
240    //fgets(buf, sizeof(buf), fp);
241    myfread( ss, procbuflen, 1, fp);
242    char ct;
243    char *e;
244    s=iiProcName(ss,ct,e);
245    char *argstr=NULL;
246    *e=ct;
247    argstr=iiProcArgs(e,TRUE);
248
249    assume(pi->data.s.body_end > pi->data.s.body_start);
250
251    procbuflen = pi->data.s.body_end - pi->data.s.body_start;
252    pi->data.s.body = (char *)omAlloc( strlen(argstr)+procbuflen+15+
253                                      strlen(pi->libname) );
254    //Print("Body=%ld-%ld=%d\n", pi->data.s.body_end,
255    //    pi->data.s.body_start, procbuflen);
256    assume(pi->data.s.body != NULL);
257    fseek(fp, pi->data.s.body_start, SEEK_SET);
258    strcpy(pi->data.s.body,argstr);
259    myfread( pi->data.s.body+strlen(argstr), procbuflen, 1, fp);
260    fclose( fp );
261    procbuflen+=strlen(argstr);
262    omFree(argstr);
263    omFree(ss);
264    pi->data.s.body[procbuflen] = '\0';
265    strcat( pi->data.s.body+procbuflen, "\n;return();\n\n" );
266    strcat( pi->data.s.body+procbuflen+13,pi->libname);
267    s=(char *)strchr(pi->data.s.body,'{');
268    if (s!=NULL) *s=' ';
269    return NULL;
270  }
271  else if(part==2)
272  { // example
273    if ( pi->data.s.example_lineno == 0)
274      return NULL; // example part does not exist
275    // load example
276    fseek(fp, pi->data.s.example_start, SEEK_SET);
277    /*char *dummy=*/ (void) fgets(buf, sizeof(buf), fp); // skip line with "example"
278    procbuflen = pi->data.s.proc_end - pi->data.s.example_start - strlen(buf);
279    //Print("Example=%ld-%ld=%d\n", pi->data.s.proc_end,
280    //  pi->data.s.example_start, procbuflen);
281    s = (char *)omAlloc(procbuflen+14);
282    myfread(s, procbuflen, 1, fp);
283    s[procbuflen] = '\0';
284    strcat(s+procbuflen-3, "\n;return();\n\n" );
285    p=(char *)strchr(s,'{');
286    if (p!=NULL) *p=' ';
287    return(s);
288  }
289  return NULL;
290}
291
292BOOLEAN iiAllStart(procinfov pi, char *p,feBufferTypes t, int l)
293{
294  // see below:
295  BITSET save1=si_opt_1;
296  BITSET save2=si_opt_2;
297  newBuffer( omStrDup(p /*pi->data.s.body*/), t /*BT_proc*/,
298               pi, l );
299  BOOLEAN err=yyparse();
300  if (sLastPrinted.rtyp!=0)
301  {
302    sLastPrinted.CleanUp();
303  }
304  // the access to optionStruct and verboseStruct do not work
305  // on x86_64-Linux for pic-code
306  if ((TEST_V_ALLWARN) &&
307  (t==BT_proc) &&
308  ((save1!=si_opt_1)||(save2!=si_opt_2)) &&
309  (pi->libname!=NULL) && (pi->libname[0]!='\0'))
310  {
311    if ((pi->libname!=NULL) && (pi->libname[0]!='\0'))
312      Warn("option changed in proc %s from %s",pi->procname,pi->libname);
313    else
314      Warn("option changed in proc %s",pi->procname);
315    int i;
316    for (i=0; optionStruct[i].setval!=0; i++)
317    {
318      if ((optionStruct[i].setval & si_opt_1)
319      && (!(optionStruct[i].setval & save1)))
320      {
321          Print(" +%s",optionStruct[i].name);
322      }
323      if (!(optionStruct[i].setval & si_opt_1)
324      && ((optionStruct[i].setval & save1)))
325      {
326          Print(" -%s",optionStruct[i].name);
327      }
328    }
329    for (i=0; verboseStruct[i].setval!=0; i++)
330    {
331      if ((verboseStruct[i].setval & si_opt_2)
332      && (!(verboseStruct[i].setval & save2)))
333      {
334          Print(" +%s",verboseStruct[i].name);
335      }
336      if (!(verboseStruct[i].setval & si_opt_2)
337      && ((verboseStruct[i].setval & save2)))
338      {
339          Print(" -%s",verboseStruct[i].name);
340      }
341    }
342    PrintLn();
343  }
344  return err;
345}
346/*2
347* start a proc
348* parameters are built as exprlist
349* TODO:interrupt
350* return FALSE on success, TRUE if an error occurs
351*/
352BOOLEAN iiPStart(idhdl pn, leftv v)
353{
354  procinfov pi=NULL;
355  int old_echo=si_echo;
356  BOOLEAN err=FALSE;
357  char save_flags=0;
358
359  /* init febase ======================================== */
360  /* we do not enter this case if filename != NULL !! */
361  if (pn!=NULL)
362  {
363    pi = IDPROC(pn);
364    if(pi!=NULL)
365    {
366      save_flags=pi->trace_flag;
367      if( pi->data.s.body==NULL )
368      {
369        iiGetLibProcBuffer(pi);
370        if (pi->data.s.body==NULL) return TRUE;
371      }
372//      omUpdateInfo();
373//      int m=om_Info.UsedBytes;
374//      Print("proc %s, mem=%d\n",IDID(pn),m);
375    }
376  }
377  else return TRUE;
378  /* generate argument list ======================================*/
379  //iiCurrArgs should be NULL here, as the assignment for the parameters
380  // of the prevouis call are already done befor calling another routine
381  if (v!=NULL)
382  {
383    iiCurrArgs=(leftv)omAllocBin(sleftv_bin);
384    memcpy(iiCurrArgs,v,sizeof(sleftv)); // keeps track of v->next etc.
385    memset(v,0,sizeof(sleftv));
386  }
387  else
388  {
389    iiCurrArgs=NULL;
390  }
391  iiCurrProc=pn;
392  /* start interpreter ======================================*/
393  myynest++;
394  if (myynest > SI_MAX_NEST)
395  {
396    WerrorS("nesting too deep");
397    err=TRUE;
398  }
399  else
400  {
401    err=iiAllStart(pi,pi->data.s.body,BT_proc,pi->data.s.body_lineno-(v!=NULL));
402
403    if (iiLocalRing[myynest-1] != currRing)
404    {
405      if (iiRETURNEXPR.RingDependend())
406      {
407        //idhdl hn;
408        const char *n;
409        const char *o;
410        idhdl nh=NULL, oh=NULL;
411        if (iiLocalRing[myynest-1]!=NULL)
412          oh=rFindHdl(iiLocalRing[myynest-1],NULL);
413        if (oh!=NULL)          o=oh->id;
414        else                   o="none";
415        if (currRing!=NULL)
416          nh=rFindHdl(currRing,NULL);
417        if (nh!=NULL)          n=nh->id;
418        else                   n="none";
419        Werror("ring change during procedure call %s: %s -> %s (level %d)",pi->procname,o,n,myynest);
420        iiRETURNEXPR.CleanUp();
421        err=TRUE;
422      }
423      currRing=iiLocalRing[myynest-1];
424    }
425    if ((currRing==NULL)
426    && (currRingHdl!=NULL))
427      currRing=IDRING(currRingHdl);
428    else
429    if ((currRing!=NULL) &&
430      ((currRingHdl==NULL)||(IDRING(currRingHdl)!=currRing)
431       ||(IDLEV(currRingHdl)>=myynest-1)))
432    {
433      rSetHdl(rFindHdl(currRing,NULL));
434      iiLocalRing[myynest-1]=NULL;
435    }
436    //Print("kill locals for %s (level %d)\n",IDID(pn),myynest);
437    killlocals(myynest);
438#ifndef SING_NDEBUG
439    checkall();
440#endif
441    //Print("end kill locals for %s (%d)\n",IDID(pn),myynest);
442  }
443  myynest--;
444  si_echo=old_echo;
445  if (pi!=NULL)
446    pi->trace_flag=save_flags;
447//  omUpdateInfo();
448//  int m=om_Info.UsedBytes;
449//  Print("exit %s, mem=%d\n",IDID(pn),m);
450  return err;
451}
452
453ring    *iiLocalRing;
454sleftv  iiRETURNEXPR;
455int     iiRETURNEXPR_len=0;
456
457#ifdef RDEBUG
458static void iiShowLevRings()
459{
460  int i;
461  for (i=0;i<=myynest;i++)
462  {
463    Print("lev %d:",i);
464    if (iiLocalRing[i]==NULL) PrintS("NULL");
465    else                      Print("%lx",(long)iiLocalRing[i]);
466    PrintLn();
467  }
468  if (currRing==NULL) PrintS("curr:NULL\n");
469  else                Print ("curr:%lx\n",(long)currRing);
470}
471#endif /* RDEBUG */
472
473static void iiCheckNest()
474{
475  if (myynest >= iiRETURNEXPR_len-1)
476  {
477    iiLocalRing=(ring *)omreallocSize(iiLocalRing,
478                                   iiRETURNEXPR_len*sizeof(ring),
479                                   (iiRETURNEXPR_len+16)*sizeof(ring));
480    memset(&(iiLocalRing[iiRETURNEXPR_len]),0,16*sizeof(ring));
481    iiRETURNEXPR_len+=16;
482  }
483}
484BOOLEAN iiMake_proc(idhdl pn, package pack, leftv sl)
485{
486  int err;
487  procinfov pi = IDPROC(pn);
488  if(pi->is_static && myynest==0)
489  {
490    Werror("'%s::%s()' is a local procedure and cannot be accessed by an user.",
491           pi->libname, pi->procname);
492    return TRUE;
493  }
494  iiCheckNest();
495  iiLocalRing[myynest]=currRing;
496  //Print("currRing(%d):%s(%x) in %s\n",myynest,IDID(currRingHdl),currRing,IDID(pn));
497  iiRETURNEXPR.Init();
498  procstack->push(pi->procname);
499  if ((traceit&TRACE_SHOW_PROC)
500  || (pi->trace_flag&TRACE_SHOW_PROC))
501  {
502    if (traceit&TRACE_SHOW_LINENO) PrintLn();
503    Print("entering%-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest);
504  }
505#ifdef RDEBUG
506  if (traceit&TRACE_SHOW_RINGS) iiShowLevRings();
507#endif
508  switch (pi->language)
509  {
510    default:
511    case LANG_NONE:
512                 WerrorS("undefined proc");
513                 err=TRUE;
514                 break;
515
516    case LANG_SINGULAR:
517                 if ((pi->pack!=NULL)&&(currPack!=pi->pack))
518                 {
519                   currPack=pi->pack;
520                   iiCheckPack(currPack);
521                   currPackHdl=packFindHdl(currPack);
522                   //Print("set pack=%s\n",IDID(currPackHdl));
523                 }
524                 else if ((pack!=NULL)&&(currPack!=pack))
525                 {
526                   currPack=pack;
527                   iiCheckPack(currPack);
528                   currPackHdl=packFindHdl(currPack);
529                   //Print("set pack=%s\n",IDID(currPackHdl));
530                 }
531                 err=iiPStart(pn,sl);
532                 break;
533    case LANG_C:
534                 leftv res = (leftv)omAlloc0Bin(sleftv_bin);
535                 err = (pi->data.o.function)(res, sl);
536                 memcpy(&iiRETURNEXPR,res,sizeof(iiRETURNEXPR));
537                 omFreeBin((ADDRESS)res,  sleftv_bin);
538                 break;
539  }
540  if ((traceit&TRACE_SHOW_PROC)
541  || (pi->trace_flag&TRACE_SHOW_PROC))
542  {
543    if (traceit&TRACE_SHOW_LINENO) PrintLn();
544    Print("leaving %-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest);
545  }
546  //const char *n="NULL";
547  //if (currRingHdl!=NULL) n=IDID(currRingHdl);
548  //Print("currRing(%d):%s(%x) after %s\n",myynest,n,currRing,IDID(pn));
549#ifdef RDEBUG
550  if (traceit&TRACE_SHOW_RINGS) iiShowLevRings();
551#endif
552  if (err)
553  {
554    iiRETURNEXPR.CleanUp();
555    //iiRETURNEXPR.Init(); //done by CleanUp
556  }
557  if (iiCurrArgs!=NULL)
558  {
559    if (!err) Warn("too many arguments for %s",IDID(pn));
560    iiCurrArgs->CleanUp();
561    omFreeBin((ADDRESS)iiCurrArgs, sleftv_bin);
562    iiCurrArgs=NULL;
563  }
564  procstack->pop();
565  if (err)
566    return TRUE;
567  return FALSE;
568}
569static void iiCallLibProcBegin()
570{
571  idhdl tmp_ring=NULL;
572  if (currRing!=NULL)
573  {
574    if (IDRING(currRingHdl)!=currRing)
575    {
576      // clean up things depending on currRingHdl:
577      sLastPrinted.CleanUp(IDRING(currRingHdl));
578      sLastPrinted.Init();
579      // need to define a ring-hdl for currRingHdl
580      tmp_ring=enterid(" tmpRing",myynest,RING_CMD,&IDROOT,FALSE);
581      IDRING(tmp_ring)=currRing;
582      currRing->ref++;
583      rSetHdl(tmp_ring);
584    }
585  }
586}
587static void iiCallLibProcEnd(idhdl save_ringhdl, ring save_ring)
588{
589  if ((currRing!=NULL)
590  &&(currRing!=save_ring))
591  {
592    currRing->ref--;
593    idhdl hh=IDROOT;
594    idhdl prev=NULL;
595    while((hh!=currRingHdl) && (hh!=NULL)) { prev=hh; hh=hh->next; }
596    if (hh!=NULL)
597    {
598      if (prev==NULL) IDROOT=hh->next;
599      else prev->next=hh->next;
600      omFree((ADDRESS)IDID(hh));
601      omFreeBin((ADDRESS)hh, idrec_bin);
602    }
603    else
604    {
605      WarnS("internal: lost ring in iiCallLib");
606    }
607  }
608  currRingHdl=save_ringhdl;
609  currRing=save_ring;
610}
611
612void* iiCallLibProc1(const char*n, void *arg, int arg_type, BOOLEAN &err)
613{
614  idhdl h=ggetid(n);
615  if ((h==NULL)
616  || (IDTYP(h)!=PROC_CMD))
617  {
618    err=2;
619    return NULL;
620  }
621  // ring handling
622  idhdl save_ringhdl=currRingHdl;
623  ring save_ring=currRing;
624  iiCallLibProcBegin();
625  // argument:
626  sleftv tmp;
627  tmp.Init();
628  tmp.data=arg;
629  tmp.rtyp=arg_type;
630  // call proc
631  err=iiMake_proc(h,currPack,&tmp);
632  // clean up ring
633  iiCallLibProcEnd(save_ringhdl,save_ring);
634  // return
635  if (err==FALSE)
636  {
637    void*r=iiRETURNEXPR.data;
638    iiRETURNEXPR.data=NULL;
639    iiRETURNEXPR.CleanUp();
640    return r;
641  }
642  return NULL;
643}
644/// args: NULL terminated arry of arguments
645/// arg_types: 0 terminated array of corresponding types
646void* iiCallLibProcM(const char*n, void **args, int* arg_types, BOOLEAN &err)
647{
648  idhdl h=ggetid(n);
649  if ((h==NULL)
650  || (IDTYP(h)!=PROC_CMD))
651  {
652    err=2;
653    return NULL;
654  }
655  // ring handling
656  idhdl save_ringhdl=currRingHdl;
657  ring save_ring=currRing;
658  iiCallLibProcBegin();
659  // argument:
660  if (arg_types[0]!=0)
661  {
662    sleftv tmp;
663    leftv tt=&tmp;
664    int i=1;
665    tmp.Init();
666    tmp.data=args[0];
667    tmp.rtyp=arg_types[0];
668    while(arg_types[i]!=0)
669    {
670      tt->next=(leftv)omAlloc0(sizeof(sleftv));
671      tt=tt->next;
672      tt->rtyp=arg_types[i];
673      tt->data=args[i];
674      i++;
675    }
676    // call proc
677    err=iiMake_proc(h,currPack,&tmp);
678  }
679  else
680  // call proc
681    err=iiMake_proc(h,currPack,NULL);
682  // clean up ring
683  iiCallLibProcEnd(save_ringhdl,save_ring);
684  // return
685  if (err==FALSE)
686  {
687    void*r=iiRETURNEXPR.data;
688    iiRETURNEXPR.data=NULL;
689    iiRETURNEXPR.CleanUp();
690    return r;
691  }
692  return NULL;
693}
694/*2
695* start an example (as a proc),
696* destroys the string 'example'
697*/
698BOOLEAN iiEStart(char* example, procinfo *pi)
699{
700  BOOLEAN err;
701  int old_echo=si_echo;
702
703  iiCheckNest();
704  procstack->push(example);
705  iiLocalRing[myynest]=currRing;
706  if (traceit&TRACE_SHOW_PROC)
707  {
708    if (traceit&TRACE_SHOW_LINENO) printf("\n");
709    printf("entering example (level %d)\n",myynest);
710  }
711  myynest++;
712
713  err=iiAllStart(pi,example,BT_example,(pi != NULL ? pi->data.s.example_lineno: 0));
714
715  killlocals(myynest);
716  myynest--;
717  si_echo=old_echo;
718  if (traceit&TRACE_SHOW_PROC)
719  {
720    if (traceit&TRACE_SHOW_LINENO) printf("\n");
721    printf("leaving  -example- (level %d)\n",myynest);
722  }
723  if (iiLocalRing[myynest] != currRing)
724  {
725    if (iiLocalRing[myynest]!=NULL)
726    {
727      rSetHdl(rFindHdl(iiLocalRing[myynest],NULL));
728      iiLocalRing[myynest]=NULL;
729    }
730    else
731    {
732      currRingHdl=NULL;
733      currRing=NULL;
734    }
735  }
736  procstack->pop();
737  return err;
738}
739
740
741extern "C"
742{
743#  define SI_GET_BUILTIN_MOD_INIT0(name) int SI_MOD_INIT0(name)(SModulFunctions*);
744          SI_FOREACH_BUILTIN(SI_GET_BUILTIN_MOD_INIT0)
745#  undef  SI_GET_BUILTIN_MOD_INIT0
746};
747
748
749SModulFunc_t
750iiGetBuiltinModInit(const char* libname)
751{
752#  define SI_GET_BUILTIN_MOD_INIT(name) if (strcmp(libname, #name ".so") == 0){ return SI_MOD_INIT0(name); }
753          SI_FOREACH_BUILTIN(SI_GET_BUILTIN_MOD_INIT)
754#  undef  SI_GET_BUILTIN_MOD_INIT
755
756  return NULL;
757}
758
759
760
761
762/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
763BOOLEAN iiTryLoadLib(leftv v, const char *id)
764{
765  BOOLEAN LoadResult = TRUE;
766  char libnamebuf[1024];
767  char *libname = (char *)omAlloc(strlen(id)+5);
768  const char *suffix[] = { "", ".lib", ".so", ".sl", NULL };
769  int i = 0;
770  // FILE *fp;
771  // package pack;
772  // idhdl packhdl;
773  lib_types LT;
774  for(i=0; suffix[i] != NULL; i++)
775  {
776    sprintf(libname, "%s%s", id, suffix[i]);
777    *libname = mytolower(*libname);
778    if((LT = type_of_LIB(libname, libnamebuf)) > LT_NOTFOUND)
779    {
780      char *s=omStrDup(libname);
781      #ifdef HAVE_DYNAMIC_LOADING
782      char libnamebuf[1024];
783      #endif
784
785      if (LT==LT_SINGULAR)
786        LoadResult = iiLibCmd(s, FALSE, FALSE,TRUE);
787      #ifdef HAVE_DYNAMIC_LOADING
788      else if ((LT==LT_ELF) || (LT==LT_HPUX))
789        LoadResult = load_modules(s,libnamebuf,FALSE);
790      #endif
791      else if (LT==LT_BUILTIN)
792      {
793        LoadResult=load_builtin(s,FALSE, iiGetBuiltinModInit(s));
794      }
795      if(!LoadResult )
796      {
797        v->name = iiConvName(libname);
798        break;
799      }
800    }
801  }
802  omFree(libname);
803  return LoadResult;
804}
805
806/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
807/* check, if library lib has already been loaded
808   if yes, writes filename of lib into where and returns TRUE,
809      no, returns FALSE
810*/
811BOOLEAN iiLocateLib(const char* lib, char* where)
812{
813  char *plib = iiConvName(lib);
814  idhdl pl = basePack->idroot->get(plib,0);
815  if( (pl!=NULL) && (IDTYP(pl)==PACKAGE_CMD) &&
816    (IDPACKAGE(pl)->language == LANG_SINGULAR))
817  {
818    strncpy(where,IDPACKAGE(pl)->libname,127);
819    return TRUE;
820  }
821  else
822    return FALSE;;
823}
824
825BOOLEAN iiLibCmd( char *newlib, BOOLEAN autoexport, BOOLEAN tellerror, BOOLEAN force )
826{
827  char libnamebuf[1024];
828  // procinfov pi;
829  // idhdl h;
830  idhdl pl;
831  // idhdl hl;
832  // long pos = 0L;
833  char *plib = iiConvName(newlib);
834  FILE * fp = feFopen( newlib, "r", libnamebuf, tellerror );
835  // int lines = 1;
836  BOOLEAN LoadResult = TRUE;
837
838  if (fp==NULL)
839  {
840    return TRUE;
841  }
842  pl = basePack->idroot->get(plib,0);
843  if (pl==NULL)
844  {
845    pl = enterid( plib,0, PACKAGE_CMD,
846                  &(basePack->idroot), TRUE );
847    IDPACKAGE(pl)->language = LANG_SINGULAR;
848    IDPACKAGE(pl)->libname=omStrDup(newlib);
849  }
850  else
851  {
852    if(IDTYP(pl)!=PACKAGE_CMD)
853    {
854      WarnS("not of type package.");
855      fclose(fp);
856      return TRUE;
857    }
858    if (!force) return FALSE;
859  }
860  LoadResult = iiLoadLIB(fp, libnamebuf, newlib, pl, autoexport, tellerror);
861  omFree((ADDRESS)newlib);
862
863  if(!LoadResult) IDPACKAGE(pl)->loaded = TRUE;
864  omFree((ADDRESS)plib);
865
866 return LoadResult;
867}
868/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
869static void iiCleanProcs(idhdl &root)
870{
871  idhdl prev=NULL;
872  loop
873  {
874    if (root==NULL) return;
875    if (IDTYP(root)==PROC_CMD)
876    {
877      procinfo *pi=(procinfo*)IDDATA(root);
878      if ((pi->language == LANG_SINGULAR)
879      && (pi->data.s.body_start == 0L))
880      {
881        // procinfo data incorrect:
882        // - no proc body can start at the beginning of the file
883        killhdl(root);
884        if (prev==NULL)
885          root=IDROOT;
886        else
887        {
888          root=prev;
889          prev=NULL;
890        }
891        continue;
892      }
893    }
894    prev=root;
895    root=IDNEXT(root);
896  }
897}
898static void iiRunInit(package p)
899{
900  idhdl h=p->idroot->get("mod_init",0);
901  if (h==NULL) return;
902  if (IDTYP(h)==PROC_CMD)
903  {
904    int save=yylineno;
905    myynest++;
906    // procinfo *pi=(procinfo*)IDDATA(h);
907    //PrintS("mod_init found\n");
908    iiMake_proc(h,p,NULL);
909    myynest--;
910    yylineno=save;
911  }
912}
913/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
914BOOLEAN iiLoadLIB(FILE *fp, const char *libnamebuf, const char*newlib,
915             idhdl pl, BOOLEAN autoexport, BOOLEAN tellerror)
916{
917  extern FILE *yylpin;
918  libstackv ls_start = library_stack;
919  lib_style_types lib_style;
920
921  yylpin = fp;
922  #if YYLPDEBUG > 1
923  print_init();
924  #endif
925  extern int lpverbose;
926  if (BVERBOSE(V_DEBUG_LIB)) lpverbose=1;
927  else lpverbose=0;
928  // yylplex sets also text_buffer
929  if (text_buffer!=NULL) *text_buffer='\0';
930  yylplex(newlib, libnamebuf, &lib_style, pl, autoexport);
931  if(yylp_errno)
932  {
933    Werror("Library %s: ERROR occurred: in line %d, %d.", newlib, yylplineno,
934         current_pos(0));
935    if(yylp_errno==YYLP_BAD_CHAR)
936    {
937      Werror(yylp_errlist[yylp_errno], *text_buffer, yylplineno);
938      omFree((ADDRESS)text_buffer);
939      text_buffer=NULL;
940    }
941    else
942      Werror(yylp_errlist[yylp_errno], yylplineno);
943    WerrorS("Cannot load library,... aborting.");
944    reinit_yylp();
945    fclose( yylpin );
946    iiCleanProcs(IDROOT);
947    return TRUE;
948  }
949  if (BVERBOSE(V_LOAD_LIB))
950    Print( "// ** loaded %s %s\n", libnamebuf, text_buffer);
951  if( (lib_style == OLD_LIBSTYLE) && (BVERBOSE(V_LOAD_LIB)))
952  {
953    Warn( "library %s has old format. This format is still accepted,", newlib);
954    WarnS( "but for functionality you may wish to change to the new");
955    WarnS( "format. Please refer to the manual for further information.");
956  }
957  reinit_yylp();
958  fclose( yylpin );
959  fp = NULL;
960  iiRunInit(IDPACKAGE(pl));
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, const char *libname,
991              const char *procname, int, long pos, BOOLEAN pstatic)
992{
993  memset(pi,0,sizeof(*pi));
994  pi->libname = omStrDup(libname);
995  pi->procname = omStrDup(procname);
996  pi->language = LANG_SINGULAR;
997  pi->ref = 1;
998  pi->is_static = pstatic;
999  pi->data.s.proc_start = pos;
1000  return(pi);
1001}
1002
1003/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1004int iiAddCproc(const char *libname, const char *procname, BOOLEAN pstatic,
1005               BOOLEAN(*func)(leftv res, leftv v))
1006{
1007  procinfov pi;
1008  idhdl h;
1009
1010  #ifndef SING_NDEBUG
1011  int dummy;
1012  if (IsCmd(procname,dummy))
1013  {
1014    Werror(">>%s< is a reserved name",procname);
1015    return 0;
1016  }
1017  #endif
1018
1019  h=IDROOT->get(procname,0);
1020  if ((h!=NULL)
1021  && (IDTYP(h)==PROC_CMD))
1022  {
1023    pi = IDPROC(h);
1024    #if 0
1025    if ((pi->language == LANG_SINGULAR)
1026    &&(BVERBOSE(V_REDEFINE)))
1027      Warn("extend `%s`",procname);
1028    #endif
1029  }
1030  else
1031  {
1032    h = enterid(procname,0, PROC_CMD, &IDROOT, TRUE);
1033  }
1034  if ( h!= NULL )
1035  {
1036    pi = IDPROC(h);
1037    if((pi->language == LANG_SINGULAR)
1038    ||(pi->language == LANG_NONE))
1039    {
1040      omfree(pi->libname);
1041      pi->libname = omStrDup(libname);
1042      omfree(pi->procname);
1043      pi->procname = omStrDup(procname);
1044      pi->language = LANG_C;
1045      pi->ref = 1;
1046      pi->is_static = pstatic;
1047      pi->data.o.function = func;
1048    }
1049    else if(pi->language == LANG_C)
1050    {
1051      if(pi->data.o.function == func)
1052      {
1053        pi->ref++;
1054      }
1055      else
1056      {
1057        omfree(pi->libname);
1058        pi->libname = omStrDup(libname);
1059        omfree(pi->procname);
1060        pi->procname = omStrDup(procname);
1061        pi->language = LANG_C;
1062        pi->ref = 1;
1063        pi->is_static = pstatic;
1064        pi->data.o.function = func;
1065      }
1066    }
1067    else
1068      Warn("internal error: unknown procedure type %d",pi->language);
1069    if (currPack->language==LANG_SINGULAR) currPack->language=LANG_MIX;
1070    return(1);
1071  }
1072  else
1073  {
1074    WarnS("iiAddCproc: failed.");
1075  }
1076  return(0);
1077}
1078
1079int iiAddCprocTop(const char *libname, const char *procname, BOOLEAN pstatic,
1080               BOOLEAN(*func)(leftv res, leftv v))
1081{
1082  int r=iiAddCproc(libname,procname,pstatic,func);
1083  package s=currPack;
1084  currPack=basePack;
1085  if (r) r=iiAddCproc(libname,procname,pstatic,func);
1086  currPack=s;
1087  return r;
1088}
1089
1090/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1091#ifdef HAVE_DYNAMIC_LOADING
1092BOOLEAN load_modules(const char *newlib, char *fullname, BOOLEAN autoexport)
1093{
1094#ifdef HAVE_STATIC
1095  WerrorS("mod_init: static version can not load modules");
1096  return TRUE;
1097#else
1098/*
1099  typedef int (*fktn_t)(int(*iiAddCproc)(const char *libname, const char *procname,
1100                               BOOLEAN pstatic,
1101                               BOOLEAN(*func)(leftv res, leftv v)));
1102*/
1103  SModulFunc_t fktn;
1104  idhdl pl;
1105  char *plib = iiConvName(newlib);
1106  BOOLEAN RET=TRUE;
1107  int token;
1108  char FullName[256];
1109
1110  memset(FullName,0,256);
1111
1112  if( *fullname != '/' &&  *fullname != '.' )
1113    sprintf(FullName, "./%s", newlib);
1114  else strncpy(FullName, fullname,255);
1115
1116
1117  if(IsCmd(plib, token))
1118  {
1119    Werror("'%s' is resered identifier\n", plib);
1120    goto load_modules_end;
1121  }
1122  pl = basePack->idroot->get(plib,0); /* packages only in top level
1123                                        (see enterid) */
1124  if ((pl!=NULL)
1125  &&(IDTYP(pl)==PACKAGE_CMD))
1126  {
1127    if(IDPACKAGE(pl)->language==LANG_C)
1128    {
1129      if (BVERBOSE(V_LOAD_LIB)) Warn( "%s already loaded as package", newlib);
1130      omFree(plib);
1131      return FALSE;
1132    }
1133    else if(IDPACKAGE(pl)->language==LANG_MIX)
1134    {
1135      if (BVERBOSE(V_LOAD_LIB)) Warn( "%s contain binary parts, cannot load", newlib);
1136      omFree(plib);
1137      return FALSE;
1138    }
1139  }
1140  else
1141  {
1142    pl = enterid( plib,0, PACKAGE_CMD, &IDROOT, TRUE );
1143    omFree(plib); /* enterid copied plib*/
1144    IDPACKAGE(pl)->libname=omStrDup(newlib);
1145  }
1146  IDPACKAGE(pl)->language = LANG_C;
1147  if (dynl_check_opened(FullName))
1148  {
1149    if (BVERBOSE(V_LOAD_LIB)) Warn( "%s already loaded as C library", fullname);
1150    return FALSE;
1151  }
1152  if((IDPACKAGE(pl)->handle=dynl_open(FullName))==(void *)NULL)
1153  {
1154    Werror("dynl_open failed:%s", dynl_error());
1155    Werror("%s not found", newlib);
1156    killhdl2(pl,&(basePack->idroot),NULL); // remove package
1157    goto load_modules_end;
1158  }
1159  else
1160  {
1161    SModulFunctions sModulFunctions;
1162
1163    package s=currPack;
1164    currPack=IDPACKAGE(pl);
1165    fktn = (SModulFunc_t)dynl_sym(IDPACKAGE(pl)->handle, "mod_init");
1166    if( fktn!= NULL)
1167    {
1168      sModulFunctions.iiArithAddCmd = iiArithAddCmd;
1169      if (autoexport) sModulFunctions.iiAddCproc = iiAddCprocTop;
1170      else            sModulFunctions.iiAddCproc = iiAddCproc;
1171      int ver=(*fktn)(&sModulFunctions);
1172      if (ver==MAX_TOK)
1173      {
1174        if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loaded %s\n", fullname);
1175      }
1176      else
1177      {
1178        Warn("loaded %s for a different version of Singular(expected MAX_TOK: %d, got %d)",fullname,MAX_TOK,ver);
1179      }
1180      currPack->loaded=1;
1181      currPack=s; /* reset currPack to previous */
1182      RET=FALSE;
1183    }
1184    else
1185    {
1186      Werror("mod_init not found:: %s\nThis is probably not a dynamic module for Singular!\n", dynl_error());
1187      errorreported=0;
1188      if(IDPACKAGE(pl)->idroot==NULL)
1189        killhdl2(pl,&(basePack->idroot),NULL); // remove package
1190    }
1191  }
1192
1193  load_modules_end:
1194  return RET;
1195#endif /*STATIC */
1196}
1197#endif /* HAVE_DYNAMIC_LOADING */
1198/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1199BOOLEAN load_builtin(const char *newlib, BOOLEAN autoexport, SModulFunc_t init)
1200{
1201  int iiAddCproc(const char *libname, const char *procname, BOOLEAN pstatic,
1202                 BOOLEAN(*func)(leftv res, leftv v));
1203/*
1204  typedef int (*fktn_t)(int(*iiAddCproc)(const char *libname, const char *procname,
1205                               BOOLEAN pstatic,
1206                               BOOLEAN(*func)(leftv res, leftv v)));
1207*/
1208  // SModulFunc_t fktn;
1209  idhdl pl;
1210  char *plib = iiConvName(newlib);
1211  // BOOLEAN RET=TRUE;
1212  // int token;
1213
1214  pl = basePack->idroot->get(plib,0); // search PACKAGE only in Top
1215  if ((pl!=NULL)
1216  &&(IDTYP(pl)==PACKAGE_CMD))
1217  {
1218    if(IDPACKAGE(pl)->language==LANG_C)
1219    {
1220      if (BVERBOSE(V_LOAD_LIB)) Warn( "(builtin) %s already loaded", newlib);
1221      omFree(plib);
1222      return FALSE;
1223    }
1224  }
1225  else
1226  {
1227    pl = enterid( plib,0, PACKAGE_CMD, &IDROOT, TRUE );
1228    IDPACKAGE(pl)->libname=omStrDup(newlib);
1229  }
1230  IDPACKAGE(pl)->language = LANG_C;
1231
1232  IDPACKAGE(pl)->handle=(void *)NULL;
1233  SModulFunctions sModulFunctions;
1234
1235  package s=currPack;
1236  currPack=IDPACKAGE(pl);
1237  if( init!= NULL)
1238  {
1239    sModulFunctions.iiArithAddCmd = iiArithAddCmd;
1240    if (autoexport) sModulFunctions.iiAddCproc = iiAddCprocTop;
1241    else            sModulFunctions.iiAddCproc = iiAddCproc;
1242    (*init)(&sModulFunctions);
1243  }
1244  if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loaded (builtin) %s \n", newlib);
1245  currPack->loaded=1;
1246  currPack=s;
1247
1248  return FALSE;
1249}
1250/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1251void module_help_main(const char *newlib,const char *help)
1252{
1253  char *plib = iiConvName(newlib);
1254  idhdl pl = basePack->idroot->get(plib,0);
1255  if ((pl==NULL)||(IDTYP(pl)!=PACKAGE_CMD))
1256    Werror(">>%s<< is not a package (trying to add package help)",plib);
1257  else
1258  {
1259    package s=currPack;
1260    currPack=IDPACKAGE(pl);
1261    idhdl h=enterid("info",0,STRING_CMD,&IDROOT,FALSE);
1262    IDSTRING(h)=omStrDup(help);
1263    currPack=s;
1264  }
1265}
1266void module_help_proc(const char *newlib,const char *p, const char *help)
1267{
1268  char *plib = iiConvName(newlib);
1269  idhdl pl = basePack->idroot->get(plib,0);
1270  if ((pl==NULL)||(IDTYP(pl)!=PACKAGE_CMD))
1271    Werror(">>%s<< is not a package(trying to add help for %s)",plib,p);
1272  else
1273  {
1274    package s=currPack;
1275    currPack=IDPACKAGE(pl);
1276    char buff[256];
1277    buff[255]='\0';
1278    strncpy(buff,p,255);
1279    strncat(buff,"_help",255-strlen(p));
1280    idhdl h=enterid(buff,0,STRING_CMD,&IDROOT,FALSE);
1281    IDSTRING(h)=omStrDup(help);
1282    currPack=s;
1283  }
1284}
1285/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1286
1287#ifdef HAVE_DYNAMIC_LOADING
1288// loads a dynamic module from the binary path and returns a named function
1289// returns NULL, if something fails
1290void* binary_module_function(const char* newlib, const char* funcname)
1291{
1292  void* result = NULL;
1293
1294  const char* bin_dir = feGetResource('b');
1295  if (!bin_dir)  { return NULL; }
1296
1297  char path_name[MAXPATHLEN];
1298  sprintf(path_name, "%s%s%s.%s", bin_dir, DIR_SEPP, newlib, MODULE_SUFFIX_STRING);
1299
1300  void* openlib = dynl_open(path_name);
1301  if(!openlib)
1302  {
1303    Werror("dynl_open of %s failed:%s", path_name, dynl_error());
1304    return NULL;
1305  }
1306  result = dynl_sym(openlib, funcname);
1307  if (!result) Werror("%s: %s\n", funcname, dynl_error());
1308
1309  return result;
1310}
1311#endif
1312
1313/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1314char mytoupper(char c)
1315{
1316  if(c>=97 && c<=(97+26)) c-=32;
1317  return(c);
1318}
1319
1320char mytolower(char c)
1321{
1322  if(c>=65 && c<=(65+26)) c+=32;
1323  return(c);
1324}
1325
1326/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1327//#if defined(WINNT)
1328//#  define  FS_SEP '\\'
1329//#else
1330//#  define FS_SEP '/'
1331//#endif
1332
1333char *iiConvName(const char *libname)
1334{
1335  char *tmpname = omStrDup(libname);
1336  char *p = strrchr(tmpname, DIR_SEP);
1337  char *r;
1338  if(p==NULL) p = tmpname; else p++;
1339  // p is now the start of the file name (without path)
1340  r=p;
1341  while(isalnum(*r)||(*r=='_')) r++;
1342  // r point the the end of the main part of the filename
1343  *r = '\0';
1344  r = omStrDup(p);
1345  *r = mytoupper(*r);
1346  // printf("iiConvName: '%s' '%s' => '%s'\n", libname, tmpname, r);
1347  omFree((ADDRESS)tmpname);
1348
1349  return(r);
1350}
1351
1352/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1353#if 0 /* debug only */
1354void piShowProcList()
1355{
1356  idhdl h;
1357  procinfo *proc;
1358  char *name;
1359
1360  Print( "%-15s  %20s      %s,%s  %s,%s   %s,%s\n", "Library", "function",
1361         "line", "start", "line", "body", "line", "example");
1362  for(h = IDROOT; h != NULL; h = IDNEXT(h))
1363  {
1364    if(IDTYP(h) == PROC_CMD)
1365    {
1366      proc = IDPROC(h);
1367      if(strcmp(proc->procname, IDID(h))!=0)
1368      {
1369        name = (char *)omAlloc(strlen(IDID(h))+strlen(proc->procname)+4);
1370        sprintf(name, "%s -> %s", IDID(h), proc->procname);
1371        Print( "%d %-15s  %20s ", proc->is_static ? 1 : 0, proc->libname, name);
1372        omFree((ADDRESS)name);
1373      }
1374      else
1375        Print( "%d %-15s  %20s ", proc->is_static ? 1 : 0, proc->libname,
1376               proc->procname);
1377      if(proc->language==LANG_SINGULAR)
1378        Print("line %-5ld  %4d,%-5ld  %4d,%-5ld\n",
1379              proc->data.s.proc_start,
1380              proc->data.s.body_lineno, proc->data.s.body_start,
1381              proc->data.s.example_lineno, proc->data.s.example_start);
1382      else if(proc->language==LANG_C)
1383        PrintS("type: object\n");
1384    }
1385  }
1386}
1387#endif
1388
1389/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1390//char *iiLineNo(char *procname, int lineno)
1391//{
1392//  char buf[256];
1393//  idhdl pn = ggetid(procname);
1394//  procinfo *pi = IDPROC(pn);
1395//
1396//  sprintf(buf, "%s %3d\0", procname, lineno);
1397//  //sprintf(buf, "%s::%s %3d\0", pi->libname, pi->procname,
1398//  //  lineno + pi->data.s.body_lineno);
1399//  return(buf);
1400//}
1401/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1402#ifdef HAVE_LIBPARSER
1403void libstack::push(const char */*p*/, char *libn)
1404{
1405  libstackv lp;
1406  if( !iiGetLibStatus(libn))
1407  {
1408    for(lp = this;lp!=NULL;lp=lp->next)
1409    {
1410      if(strcmp(lp->get(), libn)==0) break;
1411    }
1412    if(lp==NULL)
1413    {
1414      libstackv ls = (libstack *)omAlloc0Bin(libstack_bin);
1415      ls->next = this;
1416      ls->libname = omStrDup(libn);
1417      ls->to_be_done = TRUE;
1418      if(library_stack != NULL) ls->cnt = library_stack->cnt+1; else ls->cnt = 0;
1419      library_stack = ls;
1420    }
1421  }
1422}
1423
1424libstackv libstack::pop(const char */*p*/)
1425{
1426  libstackv ls = this;
1427  //omFree((ADDRESS)ls->libname);
1428  library_stack = ls->next;
1429  omFreeBin((ADDRESS)ls,  libstack_bin);
1430  return(library_stack);
1431}
1432
1433#endif /* HAVE_LIBPARSER */
1434/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
Note: See TracBrowser for help on using the repository browser.