source: git/Singular/iplib.cc @ c5bae4

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