source: git/Singular/iplib.cc @ b08fdca

fieker-DuValspielwiese
Last change on this file since b08fdca was e84b19, checked in by Max Horn <max@…>, 5 years ago
iiAllStart: accept const char *
  • Property mode set to 100644
File size: 38.7 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(const 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, const 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 args)
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,args);
534                 break;
535    case LANG_C:
536                 leftv res = (leftv)omAlloc0Bin(sleftv_bin);
537                 err = (pi->data.o.function)(res, args);
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 ((currRingHdl!=NULL) && (IDRING(currRingHdl)!=currRing))
577    {
578      // clean up things depending on currRingHdl:
579      sLastPrinted.CleanUp(IDRING(currRingHdl));
580      sLastPrinted.Init();
581    }
582    // need to define a ring-hdl for currRingHdl
583    tmp_ring=enterid(" tmpRing",myynest,RING_CMD,&IDROOT,FALSE);
584    IDRING(tmp_ring)=currRing;
585    currRing->ref++;
586    rSetHdl(tmp_ring);
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  }
606  currRingHdl=save_ringhdl;
607  currRing=save_ring;
608}
609
610void* iiCallLibProc1(const char*n, void *arg, int arg_type, BOOLEAN &err)
611{
612  idhdl h=ggetid(n);
613  if ((h==NULL)
614  || (IDTYP(h)!=PROC_CMD))
615  {
616    err=2;
617    return NULL;
618  }
619  // ring handling
620  idhdl save_ringhdl=currRingHdl;
621  ring save_ring=currRing;
622  iiCallLibProcBegin();
623  // argument:
624  sleftv tmp;
625  tmp.Init();
626  tmp.data=arg;
627  tmp.rtyp=arg_type;
628  // call proc
629  err=iiMake_proc(h,currPack,&tmp);
630  // clean up ring
631  iiCallLibProcEnd(save_ringhdl,save_ring);
632  // return
633  if (err==FALSE)
634  {
635    void*r=iiRETURNEXPR.data;
636    iiRETURNEXPR.data=NULL;
637    iiRETURNEXPR.CleanUp();
638    return r;
639  }
640  return NULL;
641}
642
643// return NULL on failure
644ideal ii_CallProcId2Id(const char *lib,const char *proc, ideal arg, const ring R)
645{
646  char *plib = iiConvName(lib);
647  idhdl h=ggetid(plib);
648  omFree(plib);
649  if (h==NULL)
650  {
651    BOOLEAN bo=iiLibCmd(omStrDup(lib),TRUE,TRUE,FALSE);
652    if (bo) return NULL;
653  }
654  ring oldR=currRing;
655  rChangeCurrRing(R);
656  BOOLEAN err;
657  ideal I=(ideal)iiCallLibProc1(proc,idCopy(arg),IDEAL_CMD,err);
658  rChangeCurrRing(oldR);
659  if (err) return NULL;
660  return I;
661}
662
663int ii_CallProcId2Int(const char *lib,const char *proc, ideal arg, const ring R)
664{
665  char *plib = iiConvName(lib);
666  idhdl h=ggetid(plib);
667  omFree(plib);
668  if (h==NULL)
669  {
670    BOOLEAN bo=iiLibCmd(omStrDup(lib),TRUE,TRUE,FALSE);
671    if (bo) return 0;
672  }
673  BOOLEAN err;
674  ring oldR=currRing;
675  rChangeCurrRing(R);
676  int I=(int)(long)iiCallLibProc1(proc,idCopy(arg),IDEAL_CMD,err);
677  rChangeCurrRing(oldR);
678  if (err) return 0;
679  return I;
680}
681
682/// args: NULL terminated array of arguments
683/// arg_types: 0 terminated array of corresponding types
684leftv ii_CallLibProcM(const char*n, void **args, int* arg_types, const ring R, BOOLEAN &err)
685{
686  idhdl h=ggetid(n);
687  if ((h==NULL)
688  || (IDTYP(h)!=PROC_CMD))
689  {
690    err=2;
691    return NULL;
692  }
693  // ring handling
694  idhdl save_ringhdl=currRingHdl;
695  ring save_ring=currRing;
696  rChangeCurrRing(R);
697  iiCallLibProcBegin();
698  // argument:
699  if (arg_types[0]!=0)
700  {
701    sleftv tmp;
702    leftv tt=&tmp;
703    int i=1;
704    tmp.Init();
705    tmp.data=args[0];
706    tmp.rtyp=arg_types[0];
707    while(arg_types[i]!=0)
708    {
709      tt->next=(leftv)omAlloc0Bin(sleftv_bin);
710      tt=tt->next;
711      tt->rtyp=arg_types[i];
712      tt->data=args[i];
713      i++;
714    }
715    // call proc
716    err=iiMake_proc(h,currPack,&tmp);
717  }
718  else
719  // call proc
720    err=iiMake_proc(h,currPack,NULL);
721  // clean up ring
722  iiCallLibProcEnd(save_ringhdl,save_ring);
723  // return
724  if (err==FALSE)
725  {
726    leftv h=(leftv)omAllocBin(sleftv_bin);
727    memcpy(h,&iiRETURNEXPR,sizeof(sleftv));
728    memset(&iiRETURNEXPR,0,sizeof(sleftv));
729    return h;
730  }
731  return NULL;
732}
733/*2
734* start an example (as a proc),
735* destroys the string 'example'
736*/
737BOOLEAN iiEStart(char* example, procinfo *pi)
738{
739  BOOLEAN err;
740  int old_echo=si_echo;
741
742  iiCheckNest();
743  procstack->push(example);
744  iiLocalRing[myynest]=currRing;
745  if (traceit&TRACE_SHOW_PROC)
746  {
747    if (traceit&TRACE_SHOW_LINENO) printf("\n");
748    printf("entering example (level %d)\n",myynest);
749  }
750  myynest++;
751
752  err=iiAllStart(pi,example,BT_example,(pi != NULL ? pi->data.s.example_lineno: 0));
753
754  killlocals(myynest);
755  myynest--;
756  si_echo=old_echo;
757  if (traceit&TRACE_SHOW_PROC)
758  {
759    if (traceit&TRACE_SHOW_LINENO) printf("\n");
760    printf("leaving  -example- (level %d)\n",myynest);
761  }
762  if (iiLocalRing[myynest] != currRing)
763  {
764    if (iiLocalRing[myynest]!=NULL)
765    {
766      rSetHdl(rFindHdl(iiLocalRing[myynest],NULL));
767      iiLocalRing[myynest]=NULL;
768    }
769    else
770    {
771      currRingHdl=NULL;
772      currRing=NULL;
773    }
774  }
775  procstack->pop();
776  return err;
777}
778
779
780extern "C"
781{
782#  define SI_GET_BUILTIN_MOD_INIT0(name) int SI_MOD_INIT0(name)(SModulFunctions*);
783          SI_FOREACH_BUILTIN(SI_GET_BUILTIN_MOD_INIT0)
784#  undef  SI_GET_BUILTIN_MOD_INIT0
785};
786
787
788SModulFunc_t
789iiGetBuiltinModInit(const char* libname)
790{
791#  define SI_GET_BUILTIN_MOD_INIT(name) if (strcmp(libname, #name ".so") == 0){ return SI_MOD_INIT0(name); }
792          SI_FOREACH_BUILTIN(SI_GET_BUILTIN_MOD_INIT)
793#  undef  SI_GET_BUILTIN_MOD_INIT
794
795  return NULL;
796}
797
798
799
800
801/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
802BOOLEAN iiTryLoadLib(leftv v, const char *id)
803{
804  BOOLEAN LoadResult = TRUE;
805  char libnamebuf[1024];
806  char *libname = (char *)omAlloc(strlen(id)+5);
807  const char *suffix[] = { "", ".lib", ".so", ".sl", NULL };
808  int i = 0;
809  // FILE *fp;
810  // package pack;
811  // idhdl packhdl;
812  lib_types LT;
813  for(i=0; suffix[i] != NULL; i++)
814  {
815    sprintf(libname, "%s%s", id, suffix[i]);
816    *libname = mytolower(*libname);
817    if((LT = type_of_LIB(libname, libnamebuf)) > LT_NOTFOUND)
818    {
819      char *s=omStrDup(libname);
820      #ifdef HAVE_DYNAMIC_LOADING
821      char libnamebuf[1024];
822      #endif
823
824      if (LT==LT_SINGULAR)
825        LoadResult = iiLibCmd(s, FALSE, FALSE,TRUE);
826      #ifdef HAVE_DYNAMIC_LOADING
827      else if ((LT==LT_ELF) || (LT==LT_HPUX))
828        LoadResult = load_modules(s,libnamebuf,FALSE);
829      #endif
830      else if (LT==LT_BUILTIN)
831      {
832        LoadResult=load_builtin(s,FALSE, iiGetBuiltinModInit(s));
833      }
834      if(!LoadResult )
835      {
836        v->name = iiConvName(libname);
837        break;
838      }
839    }
840  }
841  omFree(libname);
842  return LoadResult;
843}
844
845/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
846/* check, if library lib has already been loaded
847   if yes, writes filename of lib into where and returns TRUE,
848      no, returns FALSE
849*/
850BOOLEAN iiLocateLib(const char* lib, char* where)
851{
852  char *plib = iiConvName(lib);
853  idhdl pl = basePack->idroot->get(plib,0);
854  if( (pl!=NULL) && (IDTYP(pl)==PACKAGE_CMD) &&
855    (IDPACKAGE(pl)->language == LANG_SINGULAR))
856  {
857    strncpy(where,IDPACKAGE(pl)->libname,127);
858    return TRUE;
859  }
860  else
861    return FALSE;;
862}
863
864BOOLEAN iiLibCmd( char *newlib, BOOLEAN autoexport, BOOLEAN tellerror, BOOLEAN force )
865{
866  char libnamebuf[1024];
867  // procinfov pi;
868  // idhdl h;
869  idhdl pl;
870  // idhdl hl;
871  // long pos = 0L;
872  char *plib = iiConvName(newlib);
873  FILE * fp = feFopen( newlib, "r", libnamebuf, tellerror );
874  // int lines = 1;
875  BOOLEAN LoadResult = TRUE;
876
877  if (fp==NULL)
878  {
879    return TRUE;
880  }
881  pl = basePack->idroot->get(plib,0);
882  if (pl==NULL)
883  {
884    pl = enterid( plib,0, PACKAGE_CMD,
885                  &(basePack->idroot), TRUE );
886    IDPACKAGE(pl)->language = LANG_SINGULAR;
887    IDPACKAGE(pl)->libname=omStrDup(newlib);
888  }
889  else
890  {
891    if(IDTYP(pl)!=PACKAGE_CMD)
892    {
893      WarnS("not of type package.");
894      fclose(fp);
895      return TRUE;
896    }
897    if (!force) return FALSE;
898  }
899  LoadResult = iiLoadLIB(fp, libnamebuf, newlib, pl, autoexport, tellerror);
900  omFree((ADDRESS)newlib);
901
902  if(!LoadResult) IDPACKAGE(pl)->loaded = TRUE;
903  omFree((ADDRESS)plib);
904
905 return LoadResult;
906}
907/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
908static void iiCleanProcs(idhdl &root)
909{
910  idhdl prev=NULL;
911  loop
912  {
913    if (root==NULL) return;
914    if (IDTYP(root)==PROC_CMD)
915    {
916      procinfo *pi=(procinfo*)IDDATA(root);
917      if ((pi->language == LANG_SINGULAR)
918      && (pi->data.s.body_start == 0L))
919      {
920        // procinfo data incorrect:
921        // - no proc body can start at the beginning of the file
922        killhdl(root);
923        if (prev==NULL)
924          root=IDROOT;
925        else
926        {
927          root=prev;
928          prev=NULL;
929        }
930        continue;
931      }
932    }
933    prev=root;
934    root=IDNEXT(root);
935  }
936}
937static void iiRunInit(package p)
938{
939  idhdl h=p->idroot->get("mod_init",0);
940  if (h==NULL) return;
941  if (IDTYP(h)==PROC_CMD)
942  {
943    int save=yylineno;
944    myynest++;
945    // procinfo *pi=(procinfo*)IDDATA(h);
946    //PrintS("mod_init found\n");
947    iiMake_proc(h,p,NULL);
948    myynest--;
949    yylineno=save;
950  }
951}
952/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
953BOOLEAN iiLoadLIB(FILE *fp, const char *libnamebuf, const char*newlib,
954             idhdl pl, BOOLEAN autoexport, BOOLEAN tellerror)
955{
956  EXTERN_VAR FILE *yylpin;
957  libstackv ls_start = library_stack;
958  lib_style_types lib_style;
959
960  yylpin = fp;
961  #if YYLPDEBUG > 1
962  print_init();
963  #endif
964  EXTERN_VAR int lpverbose;
965  if (BVERBOSE(V_DEBUG_LIB)) lpverbose=1;
966  else lpverbose=0;
967  // yylplex sets also text_buffer
968  if (text_buffer!=NULL) *text_buffer='\0';
969  yylplex(newlib, libnamebuf, &lib_style, pl, autoexport);
970  if(yylp_errno)
971  {
972    Werror("Library %s: ERROR occurred: in line %d, %d.", newlib, yylplineno,
973         current_pos(0));
974    if(yylp_errno==YYLP_BAD_CHAR)
975    {
976      Werror(yylp_errlist[yylp_errno], *text_buffer, yylplineno);
977      omFree((ADDRESS)text_buffer);
978      text_buffer=NULL;
979    }
980    else
981      Werror(yylp_errlist[yylp_errno], yylplineno);
982    WerrorS("Cannot load library,... aborting.");
983    reinit_yylp();
984    fclose( yylpin );
985    iiCleanProcs(IDROOT);
986    return TRUE;
987  }
988  if (BVERBOSE(V_LOAD_LIB))
989    Print( "// ** loaded %s %s\n", libnamebuf, text_buffer);
990  if( (lib_style == OLD_LIBSTYLE) && (BVERBOSE(V_LOAD_LIB)))
991  {
992    Warn( "library %s has old format. This format is still accepted,", newlib);
993    WarnS( "but for functionality you may wish to change to the new");
994    WarnS( "format. Please refer to the manual for further information.");
995  }
996  reinit_yylp();
997  fclose( yylpin );
998  fp = NULL;
999  iiRunInit(IDPACKAGE(pl));
1000
1001  {
1002    libstackv ls;
1003    for(ls = library_stack; (ls != NULL) && (ls != ls_start); )
1004    {
1005      if(ls->to_be_done)
1006      {
1007        ls->to_be_done=FALSE;
1008        iiLibCmd(ls->get(),autoexport,tellerror,FALSE);
1009        ls = ls->pop(newlib);
1010      }
1011    }
1012#if 0
1013    PrintS("--------------------\n");
1014    for(ls = library_stack; ls != NULL; ls = ls->next)
1015    {
1016      Print("%s: LIB-stack:(%d), %s %s\n", newlib, ls->cnt, ls->get(),
1017        ls->to_be_done ? "not loaded" : "loaded");
1018    }
1019    PrintS("--------------------\n");
1020#endif
1021  }
1022
1023  if(fp != NULL) fclose(fp);
1024  return FALSE;
1025}
1026
1027
1028/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1029procinfo *iiInitSingularProcinfo(procinfov pi, const char *libname,
1030              const char *procname, int, long pos, BOOLEAN pstatic)
1031{
1032  memset(pi,0,sizeof(*pi));
1033  pi->libname = omStrDup(libname);
1034  pi->procname = omStrDup(procname);
1035  pi->language = LANG_SINGULAR;
1036  pi->ref = 1;
1037  pi->is_static = pstatic;
1038  pi->data.s.proc_start = pos;
1039  return(pi);
1040}
1041
1042/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1043int iiAddCproc(const char *libname, const char *procname, BOOLEAN pstatic,
1044               BOOLEAN(*func)(leftv res, leftv v))
1045{
1046  procinfov pi;
1047  idhdl h;
1048
1049  #ifndef SING_NDEBUG
1050  int dummy;
1051  if (IsCmd(procname,dummy))
1052  {
1053    Werror(">>%s< is a reserved name",procname);
1054    return 0;
1055  }
1056  #endif
1057
1058  h=IDROOT->get(procname,0);
1059  if ((h!=NULL)
1060  && (IDTYP(h)==PROC_CMD))
1061  {
1062    pi = IDPROC(h);
1063    #if 0
1064    if ((pi->language == LANG_SINGULAR)
1065    &&(BVERBOSE(V_REDEFINE)))
1066      Warn("extend `%s`",procname);
1067    #endif
1068  }
1069  else
1070  {
1071    h = enterid(procname,0, PROC_CMD, &IDROOT, TRUE);
1072  }
1073  if ( h!= NULL )
1074  {
1075    pi = IDPROC(h);
1076    if((pi->language == LANG_SINGULAR)
1077    ||(pi->language == LANG_NONE))
1078    {
1079      omfree(pi->libname);
1080      pi->libname = omStrDup(libname);
1081      omfree(pi->procname);
1082      pi->procname = omStrDup(procname);
1083      pi->language = LANG_C;
1084      pi->ref = 1;
1085      pi->is_static = pstatic;
1086      pi->data.o.function = func;
1087    }
1088    else if(pi->language == LANG_C)
1089    {
1090      if(pi->data.o.function == func)
1091      {
1092        pi->ref++;
1093      }
1094      else
1095      {
1096        omfree(pi->libname);
1097        pi->libname = omStrDup(libname);
1098        omfree(pi->procname);
1099        pi->procname = omStrDup(procname);
1100        pi->language = LANG_C;
1101        pi->ref = 1;
1102        pi->is_static = pstatic;
1103        pi->data.o.function = func;
1104      }
1105    }
1106    else
1107      Warn("internal error: unknown procedure type %d",pi->language);
1108    if (currPack->language==LANG_SINGULAR) currPack->language=LANG_MIX;
1109    return(1);
1110  }
1111  else
1112  {
1113    WarnS("iiAddCproc: failed.");
1114  }
1115  return(0);
1116}
1117
1118int iiAddCprocTop(const char *libname, const char *procname, BOOLEAN pstatic,
1119               BOOLEAN(*func)(leftv res, leftv v))
1120{
1121  int r=iiAddCproc(libname,procname,pstatic,func);
1122  package s=currPack;
1123  currPack=basePack;
1124  if (r) r=iiAddCproc(libname,procname,pstatic,func);
1125  currPack=s;
1126  return r;
1127}
1128
1129/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1130#ifdef HAVE_DYNAMIC_LOADING
1131#include <map>
1132#include <string>
1133#include <pthread.h>
1134
1135THREAD_VAR std::map<std::string, void *> *dyn_modules;
1136
1137bool registered_dyn_module(char *fullname) {
1138  if (dyn_modules == NULL)
1139    return false;
1140  std::string fname = fullname;
1141  return dyn_modules->count(fname) != 0;
1142}
1143
1144void register_dyn_module(char *fullname, void * handle) {
1145  std::string fname = fullname;
1146  if (dyn_modules == NULL)
1147    dyn_modules = new std::map<std::string, void *>();
1148  dyn_modules->insert(std::pair<std::string, void *>(fname, handle));
1149}
1150
1151void close_all_dyn_modules() {
1152  for (std::map<std::string, void *>::iterator it = dyn_modules->begin();
1153       it != dyn_modules->end();
1154       it++)
1155  {
1156    dynl_close(it->second);
1157  }
1158  delete dyn_modules;
1159  dyn_modules = NULL;
1160}
1161BOOLEAN load_modules_aux(const char *newlib, char *fullname, BOOLEAN autoexport)
1162{
1163#ifdef HAVE_STATIC
1164  WerrorS("mod_init: static version can not load modules");
1165  return TRUE;
1166#else
1167/*
1168  typedef int (*fktn_t)(int(*iiAddCproc)(const char *libname, const char *procname,
1169                               BOOLEAN pstatic,
1170                               BOOLEAN(*func)(leftv res, leftv v)));
1171*/
1172  SModulFunc_t fktn;
1173  idhdl pl;
1174  char *plib = iiConvName(newlib);
1175  BOOLEAN RET=TRUE;
1176  int token;
1177  char FullName[256];
1178
1179  memset(FullName,0,256);
1180
1181  if( *fullname != '/' &&  *fullname != '.' )
1182    sprintf(FullName, "./%s", newlib);
1183  else strncpy(FullName, fullname,255);
1184
1185
1186  if(IsCmd(plib, token))
1187  {
1188    Werror("'%s' is resered identifier\n", plib);
1189    goto load_modules_end;
1190  }
1191  pl = basePack->idroot->get(plib,0); /* packages only in top level
1192                                        (see enterid) */
1193  if ((pl!=NULL)
1194  &&(IDTYP(pl)==PACKAGE_CMD))
1195  {
1196    if(IDPACKAGE(pl)->language==LANG_C)
1197    {
1198      if (BVERBOSE(V_LOAD_LIB)) Warn( "%s already loaded as package", newlib);
1199      omFree(plib);
1200      return FALSE;
1201    }
1202    else if(IDPACKAGE(pl)->language==LANG_MIX)
1203    {
1204      if (BVERBOSE(V_LOAD_LIB)) Warn( "%s contain binary parts, cannot load", newlib);
1205      omFree(plib);
1206      return FALSE;
1207    }
1208  }
1209  else
1210  {
1211    pl = enterid( plib,0, PACKAGE_CMD, &IDROOT, TRUE );
1212    omFree(plib); /* enterid copied plib*/
1213    IDPACKAGE(pl)->libname=omStrDup(newlib);
1214  }
1215  IDPACKAGE(pl)->language = LANG_C;
1216  if (dynl_check_opened(FullName))
1217  {
1218    if (BVERBOSE(V_LOAD_LIB)) Warn( "%s already loaded as C library", fullname);
1219    return FALSE;
1220  }
1221  if((IDPACKAGE(pl)->handle=dynl_open(FullName))==(void *)NULL)
1222  {
1223    Werror("dynl_open failed:%s", dynl_error());
1224    Werror("%s not found", newlib);
1225    killhdl2(pl,&(basePack->idroot),NULL); // remove package
1226    goto load_modules_end;
1227  }
1228  else
1229  {
1230    SModulFunctions sModulFunctions;
1231
1232    package s=currPack;
1233    currPack=IDPACKAGE(pl);
1234    fktn = (SModulFunc_t)dynl_sym(IDPACKAGE(pl)->handle, "mod_init");
1235    if( fktn!= NULL)
1236    {
1237      sModulFunctions.iiArithAddCmd = iiArithAddCmd;
1238      if (autoexport) sModulFunctions.iiAddCproc = iiAddCprocTop;
1239      else            sModulFunctions.iiAddCproc = iiAddCproc;
1240      int ver=(*fktn)(&sModulFunctions);
1241      if (ver==MAX_TOK)
1242      {
1243        if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loaded %s\n", fullname);
1244      }
1245      else
1246      {
1247        Warn("loaded %s for a different version of Singular(expected MAX_TOK: %d, got %d)",fullname,MAX_TOK,ver);
1248      }
1249      currPack->loaded=1;
1250      currPack=s; /* reset currPack to previous */
1251      register_dyn_module(fullname, IDPACKAGE(pl)->handle);
1252      RET=FALSE;
1253    }
1254    else
1255    {
1256      Werror("mod_init not found:: %s\nThis is probably not a dynamic module for Singular!\n", dynl_error());
1257      errorreported=0;
1258      if(IDPACKAGE(pl)->idroot==NULL)
1259        killhdl2(pl,&(basePack->idroot),NULL); // remove package
1260    }
1261  }
1262
1263  load_modules_end:
1264  return RET;
1265#endif /*STATIC */
1266}
1267
1268BOOLEAN load_modules(const char *newlib, char *fullname, BOOLEAN autoexport) {
1269  GLOBAL_VAR static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1270  pthread_mutex_lock(&mutex);
1271  BOOLEAN r = load_modules_aux(newlib, fullname, autoexport);
1272  pthread_mutex_unlock(&mutex);
1273  return r;
1274}
1275#endif /* HAVE_DYNAMIC_LOADING */
1276/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1277BOOLEAN load_builtin(const char *newlib, BOOLEAN autoexport, SModulFunc_t init)
1278{
1279  int iiAddCproc(const char *libname, const char *procname, BOOLEAN pstatic,
1280                 BOOLEAN(*func)(leftv res, leftv v));
1281/*
1282  typedef int (*fktn_t)(int(*iiAddCproc)(const char *libname, const char *procname,
1283                               BOOLEAN pstatic,
1284                               BOOLEAN(*func)(leftv res, leftv v)));
1285*/
1286  // SModulFunc_t fktn;
1287  idhdl pl;
1288  char *plib = iiConvName(newlib);
1289  // BOOLEAN RET=TRUE;
1290  // int token;
1291
1292  pl = basePack->idroot->get(plib,0); // search PACKAGE only in Top
1293  if ((pl!=NULL)
1294  &&(IDTYP(pl)==PACKAGE_CMD))
1295  {
1296    if(IDPACKAGE(pl)->language==LANG_C)
1297    {
1298      if (BVERBOSE(V_LOAD_LIB)) Warn( "(builtin) %s already loaded", newlib);
1299      omFree(plib);
1300      return FALSE;
1301    }
1302  }
1303  else
1304  {
1305    pl = enterid( plib,0, PACKAGE_CMD, &IDROOT, TRUE );
1306    IDPACKAGE(pl)->libname=omStrDup(newlib);
1307  }
1308  IDPACKAGE(pl)->language = LANG_C;
1309
1310  IDPACKAGE(pl)->handle=(void *)NULL;
1311  SModulFunctions sModulFunctions;
1312
1313  package s=currPack;
1314  currPack=IDPACKAGE(pl);
1315  if( init!= NULL)
1316  {
1317    sModulFunctions.iiArithAddCmd = iiArithAddCmd;
1318    if (autoexport) sModulFunctions.iiAddCproc = iiAddCprocTop;
1319    else            sModulFunctions.iiAddCproc = iiAddCproc;
1320    (*init)(&sModulFunctions);
1321  }
1322  if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loaded (builtin) %s \n", newlib);
1323  currPack->loaded=1;
1324  currPack=s;
1325
1326  return FALSE;
1327}
1328/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1329void module_help_main(const char *newlib,const char *help)
1330{
1331  char *plib = iiConvName(newlib);
1332  idhdl pl = basePack->idroot->get(plib,0);
1333  if ((pl==NULL)||(IDTYP(pl)!=PACKAGE_CMD))
1334    Werror(">>%s<< is not a package (trying to add package help)",plib);
1335  else
1336  {
1337    package s=currPack;
1338    currPack=IDPACKAGE(pl);
1339    idhdl h=enterid("info",0,STRING_CMD,&IDROOT,FALSE);
1340    IDSTRING(h)=omStrDup(help);
1341    currPack=s;
1342  }
1343}
1344void module_help_proc(const char *newlib,const char *p, const char *help)
1345{
1346  char *plib = iiConvName(newlib);
1347  idhdl pl = basePack->idroot->get(plib,0);
1348  if ((pl==NULL)||(IDTYP(pl)!=PACKAGE_CMD))
1349    Werror(">>%s<< is not a package(trying to add help for %s)",plib,p);
1350  else
1351  {
1352    package s=currPack;
1353    currPack=IDPACKAGE(pl);
1354    char buff[256];
1355    buff[255]='\0';
1356    strncpy(buff,p,255);
1357    strncat(buff,"_help",255-strlen(p));
1358    idhdl h=enterid(buff,0,STRING_CMD,&IDROOT,FALSE);
1359    IDSTRING(h)=omStrDup(help);
1360    currPack=s;
1361  }
1362}
1363/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1364
1365#ifdef HAVE_DYNAMIC_LOADING
1366// loads a dynamic module from the binary path and returns a named function
1367// returns NULL, if something fails
1368void* binary_module_function(const char* newlib, const char* funcname)
1369{
1370  void* result = NULL;
1371
1372  const char* bin_dir = feGetResource('b');
1373  if (!bin_dir)  { return NULL; }
1374
1375  char path_name[MAXPATHLEN];
1376  sprintf(path_name, "%s%s%s.%s", bin_dir, DIR_SEPP, newlib, MODULE_SUFFIX_STRING);
1377
1378  void* openlib = dynl_open(path_name);
1379  if(!openlib)
1380  {
1381    Werror("dynl_open of %s failed:%s", path_name, dynl_error());
1382    return NULL;
1383  }
1384  result = dynl_sym(openlib, funcname);
1385  if (!result) Werror("%s: %s\n", funcname, dynl_error());
1386
1387  return result;
1388}
1389#endif
1390
1391/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1392char mytoupper(char c)
1393{
1394  if(c>=97 && c<=(97+26)) c-=32;
1395  return(c);
1396}
1397
1398char mytolower(char c)
1399{
1400  if(c>=65 && c<=(65+26)) c+=32;
1401  return(c);
1402}
1403
1404/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1405//#if defined(WINNT)
1406//#  define  FS_SEP '\\'
1407//#else
1408//#  define FS_SEP '/'
1409//#endif
1410
1411char *iiConvName(const char *libname)
1412{
1413  char *tmpname = omStrDup(libname);
1414  char *p = strrchr(tmpname, DIR_SEP);
1415  char *r;
1416  if(p==NULL) p = tmpname; else p++;
1417  // p is now the start of the file name (without path)
1418  r=p;
1419  while(isalnum(*r)||(*r=='_')) r++;
1420  // r point the the end of the main part of the filename
1421  *r = '\0';
1422  r = omStrDup(p);
1423  *r = mytoupper(*r);
1424  // printf("iiConvName: '%s' '%s' => '%s'\n", libname, tmpname, r);
1425  omFree((ADDRESS)tmpname);
1426
1427  return(r);
1428}
1429
1430/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1431#if 0 /* debug only */
1432void piShowProcList()
1433{
1434  idhdl h;
1435  procinfo *proc;
1436  char *name;
1437
1438  Print( "%-15s  %20s      %s,%s  %s,%s   %s,%s\n", "Library", "function",
1439         "line", "start", "line", "body", "line", "example");
1440  for(h = IDROOT; h != NULL; h = IDNEXT(h))
1441  {
1442    if(IDTYP(h) == PROC_CMD)
1443    {
1444      proc = IDPROC(h);
1445      if(strcmp(proc->procname, IDID(h))!=0)
1446      {
1447        name = (char *)omAlloc(strlen(IDID(h))+strlen(proc->procname)+4);
1448        sprintf(name, "%s -> %s", IDID(h), proc->procname);
1449        Print( "%d %-15s  %20s ", proc->is_static ? 1 : 0, proc->libname, name);
1450        omFree((ADDRESS)name);
1451      }
1452      else
1453        Print( "%d %-15s  %20s ", proc->is_static ? 1 : 0, proc->libname,
1454               proc->procname);
1455      if(proc->language==LANG_SINGULAR)
1456        Print("line %-5ld  %4d,%-5ld  %4d,%-5ld\n",
1457              proc->data.s.proc_start,
1458              proc->data.s.body_lineno, proc->data.s.body_start,
1459              proc->data.s.example_lineno, proc->data.s.example_start);
1460      else if(proc->language==LANG_C)
1461        PrintS("type: object\n");
1462    }
1463  }
1464}
1465#endif
1466
1467/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1468//char *iiLineNo(char *procname, int lineno)
1469//{
1470//  char buf[256];
1471//  idhdl pn = ggetid(procname);
1472//  procinfo *pi = IDPROC(pn);
1473//
1474//  sprintf(buf, "%s %3d\0", procname, lineno);
1475//  //sprintf(buf, "%s::%s %3d\0", pi->libname, pi->procname,
1476//  //  lineno + pi->data.s.body_lineno);
1477//  return(buf);
1478//}
1479/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
1480#ifdef HAVE_LIBPARSER
1481void libstack::push(const char */*p*/, char *libn)
1482{
1483  libstackv lp;
1484  if( !iiGetLibStatus(libn))
1485  {
1486    for(lp = this;lp!=NULL;lp=lp->next)
1487    {
1488      if(strcmp(lp->get(), libn)==0) break;
1489    }
1490    if(lp==NULL)
1491    {
1492      libstackv ls = (libstack *)omAlloc0Bin(libstack_bin);
1493      ls->next = this;
1494      ls->libname = omStrDup(libn);
1495      ls->to_be_done = TRUE;
1496      if(library_stack != NULL) ls->cnt = library_stack->cnt+1; else ls->cnt = 0;
1497      library_stack = ls;
1498    }
1499  }
1500}
1501
1502libstackv libstack::pop(const char */*p*/)
1503{
1504  libstackv ls = this;
1505  //omFree((ADDRESS)ls->libname);
1506  library_stack = ls->next;
1507  omFreeBin((ADDRESS)ls,  libstack_bin);
1508  return(library_stack);
1509}
1510
1511#endif /* HAVE_LIBPARSER */
1512/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
Note: See TracBrowser for help on using the repository browser.