source: git/Singular/iplib.cc @ b3594cb

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