source: git/Singular/iplib.cc @ 5783b1

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