source: git/Singular/iplib.cc @ 64dfab7

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