[0e1846] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[341696] | 4 | /* $Id$ */ |
---|
[0e1846] | 5 | /* |
---|
[e2f1c7] | 6 | * ABSTRACT: interpreter: LIB and help |
---|
[0e1846] | 7 | */ |
---|
| 8 | |
---|
| 9 | //#include <stdlib.h> |
---|
| 10 | #include <stdio.h> |
---|
| 11 | #include <string.h> |
---|
[c4041ef] | 12 | #include <ctype.h> |
---|
[cd6b45] | 13 | #include <sys/stat.h> |
---|
[0e1846] | 14 | |
---|
| 15 | #include "mod2.h" |
---|
[4eb5b5] | 16 | #include "static.h" |
---|
[0e1846] | 17 | #include "tok.h" |
---|
[210bd9] | 18 | #include "options.h" |
---|
[0e1846] | 19 | #include "ipid.h" |
---|
[512a2b] | 20 | #include "omalloc.h" |
---|
[0e1846] | 21 | #include "febase.h" |
---|
| 22 | #include "ring.h" |
---|
| 23 | #include "subexpr.h" |
---|
| 24 | #include "ipshell.h" |
---|
| 25 | #include "lists.h" |
---|
[cd6b45] | 26 | |
---|
[2ff076] | 27 | #if SIZEOF_LONG == 8 |
---|
| 28 | #define SI_MAX_NEST 500 |
---|
| 29 | #else |
---|
| 30 | #define SI_MAX_NEST 1000 |
---|
| 31 | #endif |
---|
| 32 | |
---|
[24590f] | 33 | #ifdef HAVE_DYNAMIC_LOADING |
---|
[c858fc2] | 34 | BOOLEAN load_modules(char *newlib, char *fullname, BOOLEAN autoexport); |
---|
[24590f] | 35 | #endif |
---|
[730df2] | 36 | |
---|
[5480da] | 37 | #ifdef HAVE_LIBPARSER |
---|
| 38 | # include "libparse.h" |
---|
[c4bbf1f] | 39 | #else /* HAVE_LIBPARSER */ |
---|
[889f6e] | 40 | procinfo *iiInitSingularProcinfo(procinfov pi, const char *libname, |
---|
| 41 | const char *procname, int line, long pos, BOOLEAN pstatic=FALSE); |
---|
[5480da] | 42 | #endif /* HAVE_LIBPARSER */ |
---|
[573da6] | 43 | #define NS_LRING (procstack->cRing) |
---|
[0e1846] | 44 | |
---|
[85e68dd] | 45 | extern int iiArithAddCmd(const char *szName, short nAlias, short nTokval, |
---|
[b6e51b] | 46 | short nToktype, short nPos); |
---|
| 47 | |
---|
[69a954] | 48 | #include "mod_raw.h" |
---|
[cd6b45] | 49 | |
---|
[6a51ef] | 50 | #ifdef HAVE_LIBPARSER |
---|
| 51 | void yylprestart (FILE *input_file ); |
---|
| 52 | int current_pos(int i=0); |
---|
| 53 | extern int yylp_errno; |
---|
| 54 | extern int yylplineno; |
---|
| 55 | extern char *yylp_errlist[]; |
---|
| 56 | void print_init(); |
---|
| 57 | libstackv library_stack; |
---|
| 58 | #endif |
---|
[2ba9a6] | 59 | |
---|
[c4041ef] | 60 | //int IsCmd(char *n, int tok); |
---|
[8edd35] | 61 | char mytolower(char c); |
---|
| 62 | |
---|
[50cbdc] | 63 | /*2 |
---|
| 64 | * return TRUE if the libray libname is already loaded |
---|
| 65 | */ |
---|
| 66 | BOOLEAN iiGetLibStatus(char *lib) |
---|
| 67 | { |
---|
| 68 | idhdl hl; |
---|
| 69 | |
---|
[a3bc95e] | 70 | char *plib = iiConvName(lib); |
---|
| 71 | hl = basePack->idroot->get(plib,0); |
---|
| 72 | if((hl==NULL) ||(IDTYP(hl)!=PACKAGE_CMD)) |
---|
| 73 | { |
---|
| 74 | omFree(plib); |
---|
| 75 | return FALSE; |
---|
| 76 | } |
---|
| 77 | omFree(plib); |
---|
| 78 | return (strcmp(lib,IDPACKAGE(hl)->libname)==0); |
---|
[50cbdc] | 79 | } |
---|
[cd6b45] | 80 | |
---|
[0e1846] | 81 | /*2 |
---|
| 82 | * find the library of an proc: |
---|
[2ba9a6] | 83 | * => return (pi->libname) |
---|
[0e1846] | 84 | */ |
---|
[2ba9a6] | 85 | char * iiGetLibName(procinfov pi) |
---|
[0e1846] | 86 | { |
---|
[f2dff02] | 87 | return pi->libname; |
---|
[0e1846] | 88 | } |
---|
[f2dff02] | 89 | |
---|
[0e1846] | 90 | /*2 |
---|
| 91 | * given a line 'proc[ ]+{name}[ \t]*' |
---|
| 92 | * return a pointer to name and set the end of '\0' |
---|
| 93 | * changes the input! |
---|
| 94 | * returns: e: pointer to 'end of name' |
---|
| 95 | * ct: changed char at the end of s |
---|
| 96 | */ |
---|
| 97 | char* iiProcName(char *buf, char & ct, char* &e) |
---|
| 98 | { |
---|
| 99 | char *s=buf+5; |
---|
| 100 | while (*s==' ') s++; |
---|
| 101 | e=s+1; |
---|
| 102 | while ((*e>' ') && (*e!='(')) e++; |
---|
| 103 | ct=*e; |
---|
| 104 | *e='\0'; |
---|
| 105 | return s; |
---|
| 106 | } |
---|
[057e93c] | 107 | |
---|
[0e1846] | 108 | /*2 |
---|
| 109 | * given a line with args, return the argstr |
---|
| 110 | */ |
---|
| 111 | char * iiProcArgs(char *e,BOOLEAN withParenth) |
---|
| 112 | { |
---|
[33e521] | 113 | while ((*e==' ') || (*e=='\t') || (*e=='(')) e++; |
---|
[0e1846] | 114 | if (*e<' ') |
---|
| 115 | { |
---|
| 116 | if (withParenth) |
---|
| 117 | { |
---|
| 118 | // no argument list, allow list # |
---|
[c232af] | 119 | return omStrDup("parameter list #;"); |
---|
[0e1846] | 120 | } |
---|
| 121 | else |
---|
| 122 | { |
---|
| 123 | // empty list |
---|
[c232af] | 124 | return omStrDup(""); |
---|
[0e1846] | 125 | } |
---|
| 126 | } |
---|
| 127 | BOOLEAN in_args; |
---|
| 128 | BOOLEAN args_found; |
---|
| 129 | char *s; |
---|
[f2dff02] | 130 | char *argstr=(char *)omAlloc(127); // see ../omalloc/omTables.inc |
---|
| 131 | int argstrlen=127; |
---|
[0e1846] | 132 | *argstr='\0'; |
---|
[938688] | 133 | int par=0; |
---|
[0e1846] | 134 | do |
---|
| 135 | { |
---|
| 136 | args_found=FALSE; |
---|
| 137 | s=e; // set s to the starting point of the arg |
---|
| 138 | // and search for the end |
---|
[938688] | 139 | while ((*e!=',') |
---|
| 140 | &&((par!=0) || (*e!=')')) |
---|
| 141 | &&(*e!='\0')) |
---|
[0e1846] | 142 | { |
---|
[938688] | 143 | if (*e=='(') par++; |
---|
| 144 | else if (*e==')') par--; |
---|
[0e1846] | 145 | args_found=args_found || (*e>' '); |
---|
| 146 | e++; |
---|
| 147 | } |
---|
| 148 | in_args=(*e==','); |
---|
| 149 | if (args_found) |
---|
| 150 | { |
---|
| 151 | *e='\0'; |
---|
[474fb0f] | 152 | // check for space: |
---|
[24c186a] | 153 | if ((int)strlen(argstr)+12 /* parameter + ;*/ +(int)strlen(s)>= argstrlen) |
---|
[474fb0f] | 154 | { |
---|
| 155 | argstrlen*=2; |
---|
[c232af] | 156 | char *a=(char *)omAlloc( argstrlen); |
---|
[474fb0f] | 157 | strcpy(a,argstr); |
---|
[c232af] | 158 | omFree((ADDRESS)argstr); |
---|
[474fb0f] | 159 | argstr=a; |
---|
| 160 | } |
---|
[0e1846] | 161 | // copy the result to argstr |
---|
| 162 | strcat(argstr,"parameter "); |
---|
| 163 | strcat(argstr,s); |
---|
[0348347] | 164 | strcat(argstr,"; "); |
---|
[0e1846] | 165 | e++; // e was pointing to ',' |
---|
| 166 | } |
---|
| 167 | } while (in_args); |
---|
| 168 | return argstr; |
---|
| 169 | } |
---|
[2ba9a6] | 170 | |
---|
[0e1846] | 171 | /*2 |
---|
| 172 | * locate `procname` in lib `libname` and find the part `part`: |
---|
| 173 | * part=0: help, between, but excluding the line "proc ..." and "{...": |
---|
[2ba9a6] | 174 | * => return |
---|
[0e1846] | 175 | * part=1: body, between "{ ..." and "}", including the 1. line, w/o "{" |
---|
[2ba9a6] | 176 | * => set pi->data.s.body, return NULL |
---|
[0e1846] | 177 | * part=2: example, between, but excluding the line "exapmle {..." and "}": |
---|
| 178 | * => return |
---|
| 179 | */ |
---|
[2ba9a6] | 180 | char* iiGetLibProcBuffer(procinfo *pi, int part ) |
---|
[0e1846] | 181 | { |
---|
[2ba9a6] | 182 | char buf[256], *s = NULL, *p; |
---|
| 183 | long procbuflen; |
---|
[bcd557] | 184 | |
---|
[2ba9a6] | 185 | FILE * fp = feFopen( pi->libname, "rb", NULL, TRUE ); |
---|
[0e1846] | 186 | if (fp==NULL) |
---|
| 187 | { |
---|
| 188 | return NULL; |
---|
| 189 | } |
---|
| 190 | |
---|
[2ba9a6] | 191 | fseek(fp, pi->data.s.proc_start, SEEK_SET); |
---|
[057e93c] | 192 | if(part==0) |
---|
| 193 | { // load help string |
---|
[7fd611] | 194 | int i, offset=0; |
---|
[6be769] | 195 | long head = pi->data.s.def_end - pi->data.s.proc_start; |
---|
| 196 | procbuflen = pi->data.s.help_end - pi->data.s.help_start; |
---|
[09ab91d] | 197 | if (procbuflen<5) |
---|
[ce21e4a] | 198 | { |
---|
| 199 | fclose(fp); |
---|
[09ab91d] | 200 | return NULL; // help part does not exist |
---|
[ce21e4a] | 201 | } |
---|
[2ba9a6] | 202 | //Print("Help=%ld-%ld=%d\n", pi->data.s.body_start, |
---|
| 203 | // pi->data.s.proc_start, procbuflen); |
---|
[c232af] | 204 | s = (char *)omAlloc(procbuflen+head+3); |
---|
[6be769] | 205 | myfread(s, head, 1, fp); |
---|
| 206 | s[head] = '\n'; |
---|
| 207 | fseek(fp, pi->data.s.help_start, SEEK_SET); |
---|
| 208 | myfread(s+head+1, procbuflen, 1, fp); |
---|
[ce21e4a] | 209 | fclose(fp); |
---|
[6be769] | 210 | s[procbuflen+head+1] = '\n'; |
---|
| 211 | s[procbuflen+head+2] = '\0'; |
---|
[7fd611] | 212 | offset=0; |
---|
[743c32] | 213 | for(i=0;i<=procbuflen+head+2; i++) |
---|
| 214 | { |
---|
[7fd611] | 215 | if(s[i]=='\\' && |
---|
[743c32] | 216 | (s[i+1]=='"' || s[i+1]=='{' || s[i+1]=='}' || s[i+1]=='\\')) |
---|
| 217 | { |
---|
[7fd611] | 218 | i++; |
---|
| 219 | offset++; |
---|
| 220 | } |
---|
| 221 | if(offset>0) s[i-offset] = s[i]; |
---|
| 222 | } |
---|
[2ba9a6] | 223 | return(s); |
---|
| 224 | } |
---|
[09ab91d] | 225 | else if(part==1) |
---|
| 226 | { // load proc part - must exist |
---|
[6a51ef] | 227 | procbuflen = pi->data.s.def_end - pi->data.s.proc_start; |
---|
[873d82] | 228 | char *ss=(char *)omAlloc(procbuflen+2); |
---|
[6a51ef] | 229 | //fgets(buf, sizeof(buf), fp); |
---|
[873d82] | 230 | myfread( ss, procbuflen, 1, fp); |
---|
[2ba9a6] | 231 | char ct; |
---|
| 232 | char *e; |
---|
[873d82] | 233 | s=iiProcName(ss,ct,e); |
---|
[2ba9a6] | 234 | char *argstr=NULL; |
---|
| 235 | *e=ct; |
---|
| 236 | argstr=iiProcArgs(e,TRUE); |
---|
[752a5a] | 237 | |
---|
| 238 | assume(pi->data.s.body_end > pi->data.s.body_start); |
---|
| 239 | |
---|
[2ba9a6] | 240 | procbuflen = pi->data.s.body_end - pi->data.s.body_start; |
---|
[c232af] | 241 | pi->data.s.body = (char *)omAlloc( strlen(argstr)+procbuflen+15+ |
---|
[13e3243] | 242 | strlen(pi->libname) ); |
---|
[2ba9a6] | 243 | //Print("Body=%ld-%ld=%d\n", pi->data.s.body_end, |
---|
| 244 | // pi->data.s.body_start, procbuflen); |
---|
[752a5a] | 245 | assume(pi->data.s.body != NULL); |
---|
[2ba9a6] | 246 | fseek(fp, pi->data.s.body_start, SEEK_SET); |
---|
| 247 | strcpy(pi->data.s.body,argstr); |
---|
[d5f35ac] | 248 | myfread( pi->data.s.body+strlen(argstr), procbuflen, 1, fp); |
---|
[ce21e4a] | 249 | fclose( fp ); |
---|
[2ba9a6] | 250 | procbuflen+=strlen(argstr); |
---|
[c232af] | 251 | omFree(argstr); |
---|
[873d82] | 252 | omFree(ss); |
---|
[2ba9a6] | 253 | pi->data.s.body[procbuflen] = '\0'; |
---|
[057e93c] | 254 | strcat( pi->data.s.body+procbuflen, "\n;return();\n\n" ); |
---|
[2ba9a6] | 255 | strcat( pi->data.s.body+procbuflen+13,pi->libname); |
---|
[08c2d6] | 256 | s=(char *)strchr(pi->data.s.body,'{'); |
---|
[2ba9a6] | 257 | if (s!=NULL) *s=' '; |
---|
| 258 | return NULL; |
---|
| 259 | } |
---|
[09ab91d] | 260 | else if(part==2) |
---|
| 261 | { // example |
---|
| 262 | if ( pi->data.s.example_lineno == 0) |
---|
| 263 | return NULL; // example part does not exist |
---|
| 264 | // load example |
---|
[2ba9a6] | 265 | fseek(fp, pi->data.s.example_start, SEEK_SET); |
---|
[6b4ff12] | 266 | char *dummy=fgets(buf, sizeof(buf), fp); // skip line with "example" |
---|
[2ba9a6] | 267 | procbuflen = pi->data.s.proc_end - pi->data.s.example_start - strlen(buf); |
---|
| 268 | //Print("Example=%ld-%ld=%d\n", pi->data.s.proc_end, |
---|
| 269 | // pi->data.s.example_start, procbuflen); |
---|
[c232af] | 270 | s = (char *)omAlloc(procbuflen+14); |
---|
[d5f35ac] | 271 | myfread(s, procbuflen, 1, fp); |
---|
[2ba9a6] | 272 | s[procbuflen] = '\0'; |
---|
[057e93c] | 273 | strcat(s+procbuflen-3, "\n;return();\n\n" ); |
---|
[08c2d6] | 274 | p=(char *)strchr(s,'{'); |
---|
[1d1101] | 275 | if (p!=NULL) *p=' '; |
---|
[2ba9a6] | 276 | return(s); |
---|
[0e1846] | 277 | } |
---|
| 278 | return NULL; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | /*2 |
---|
[057e93c] | 282 | * start a proc |
---|
[0e1846] | 283 | * parameters are built as exprlist |
---|
[057e93c] | 284 | * TODO:interrupt |
---|
| 285 | * return FALSE on success, TRUE if an error occurs |
---|
[0e1846] | 286 | */ |
---|
[057e93c] | 287 | BOOLEAN iiPStart(idhdl pn, sleftv * v) |
---|
[0e1846] | 288 | { |
---|
| 289 | BOOLEAN err=FALSE; |
---|
[057e93c] | 290 | int old_echo=si_echo; |
---|
[64d729] | 291 | char save_flags=0; |
---|
| 292 | procinfov pi=NULL; |
---|
[0e1846] | 293 | |
---|
| 294 | /* init febase ======================================== */ |
---|
[057e93c] | 295 | /* we do not enter this case if filename != NULL !! */ |
---|
| 296 | if (pn!=NULL) |
---|
[0e1846] | 297 | { |
---|
[057e93c] | 298 | pi = IDPROC(pn); |
---|
| 299 | if(pi!=NULL) |
---|
[0e1846] | 300 | { |
---|
[64d729] | 301 | save_flags=pi->trace_flag; |
---|
[057e93c] | 302 | if( pi->data.s.body==NULL ) |
---|
[0e1846] | 303 | { |
---|
[64d729] | 304 | iiGetLibProcBuffer(pi); |
---|
| 305 | if (pi->data.s.body==NULL) return TRUE; |
---|
[0e1846] | 306 | } |
---|
[f09b51] | 307 | // omUpdateInfo(); |
---|
| 308 | // int m=om_Info.UsedBytes; |
---|
| 309 | // Print("proc %s, mem=%d\n",IDID(pn),m); |
---|
[c232af] | 310 | newBuffer( omStrDup(pi->data.s.body), BT_proc, |
---|
[64d729] | 311 | pi, pi->data.s.body_lineno-(v!=NULL) ); |
---|
[0e1846] | 312 | } |
---|
| 313 | } |
---|
| 314 | /* generate argument list ======================================*/ |
---|
| 315 | if (v!=NULL) |
---|
| 316 | { |
---|
[c232af] | 317 | iiCurrArgs=(leftv)omAllocBin(sleftv_bin); |
---|
[0e1846] | 318 | memcpy(iiCurrArgs,v,sizeof(sleftv)); |
---|
| 319 | memset(v,0,sizeof(sleftv)); |
---|
| 320 | } |
---|
| 321 | else |
---|
| 322 | { |
---|
| 323 | iiCurrArgs=NULL; |
---|
| 324 | } |
---|
[f71681] | 325 | iiCurrProc=pn; |
---|
[0e1846] | 326 | /* start interpreter ======================================*/ |
---|
[057e93c] | 327 | myynest++; |
---|
[2ff076] | 328 | if (myynest > SI_MAX_NEST) |
---|
[e0749e8] | 329 | { |
---|
[2ff076] | 330 | WerrorS("nesting too deep"); |
---|
[e0749e8] | 331 | err=TRUE; |
---|
| 332 | } |
---|
| 333 | else |
---|
| 334 | { |
---|
| 335 | err=yyparse(); |
---|
[5c5638] | 336 | #ifndef NDEBUG |
---|
[e0749e8] | 337 | checkall(); |
---|
[3b1a83c] | 338 | #endif |
---|
[e0749e8] | 339 | if (sLastPrinted.rtyp!=0) |
---|
| 340 | { |
---|
| 341 | sLastPrinted.CleanUp(); |
---|
| 342 | } |
---|
| 343 | //Print("kill locals for %s (level %d)\n",IDID(pn),myynest); |
---|
| 344 | killlocals(myynest); |
---|
[5c5638] | 345 | #ifndef NDEBUG |
---|
[e0749e8] | 346 | checkall(); |
---|
[3b1a83c] | 347 | #endif |
---|
[e0749e8] | 348 | //Print("end kill locals for %s (%d)\n",IDID(pn),myynest); |
---|
| 349 | } |
---|
[057e93c] | 350 | myynest--; |
---|
| 351 | si_echo=old_echo; |
---|
[64d729] | 352 | if (pi!=NULL) |
---|
| 353 | pi->trace_flag=save_flags; |
---|
[f09b51] | 354 | // omUpdateInfo(); |
---|
| 355 | // int m=om_Info.UsedBytes; |
---|
| 356 | // Print("exit %s, mem=%d\n",IDID(pn),m); |
---|
[0e1846] | 357 | return err; |
---|
| 358 | } |
---|
| 359 | |
---|
[77ff8e] | 360 | #ifdef USE_IILOCALRING |
---|
[a3bc95e] | 361 | ring *iiLocalRing; |
---|
[77ff8e] | 362 | #endif |
---|
[a3bc95e] | 363 | sleftv *iiRETURNEXPR; |
---|
[0e1846] | 364 | int iiRETURNEXPR_len=0; |
---|
| 365 | |
---|
| 366 | #ifdef RDEBUG |
---|
| 367 | static void iiShowLevRings() |
---|
| 368 | { |
---|
| 369 | int i; |
---|
[a3bc95e] | 370 | #ifdef USE_IILOCALRING |
---|
[9bc0b8] | 371 | for (i=0;i<=myynest;i++) |
---|
[0e1846] | 372 | { |
---|
| 373 | Print("lev %d:",i); |
---|
| 374 | if (iiLocalRing[i]==NULL) PrintS("NULL"); |
---|
[e9fdf6] | 375 | else Print("%lx",(long)iiLocalRing[i]); |
---|
[6f83c3d] | 376 | PrintLn(); |
---|
[0e1846] | 377 | } |
---|
[77ff8e] | 378 | #endif |
---|
[3b1a83c] | 379 | #if 0 |
---|
[a3bc95e] | 380 | i=myynest; |
---|
| 381 | proclevel *p=procstack; |
---|
| 382 | while (p!=NULL) |
---|
[77ff8e] | 383 | { |
---|
[a3bc95e] | 384 | Print("lev %d:",i); |
---|
[573da6] | 385 | if (p->cRingHdl==NULL) PrintS("NULL"); |
---|
| 386 | else Print("%s",IDID(p->cRingHdl)); |
---|
[a3bc95e] | 387 | PrintLn(); |
---|
| 388 | p=p->next; |
---|
[77ff8e] | 389 | } |
---|
[a3bc95e] | 390 | #endif |
---|
[0e1846] | 391 | if (currRing==NULL) PrintS("curr:NULL\n"); |
---|
[e9fdf6] | 392 | else Print ("curr:%lx\n",(long)currRing); |
---|
[0e1846] | 393 | } |
---|
[bd4cb92] | 394 | #endif /* RDEBUG */ |
---|
[0e1846] | 395 | |
---|
| 396 | static void iiCheckNest() |
---|
| 397 | { |
---|
| 398 | if (myynest >= iiRETURNEXPR_len-1) |
---|
| 399 | { |
---|
[c232af] | 400 | iiRETURNEXPR=(sleftv *)omreallocSize(iiRETURNEXPR, |
---|
[0e1846] | 401 | iiRETURNEXPR_len*sizeof(sleftv), |
---|
| 402 | (iiRETURNEXPR_len+16)*sizeof(sleftv)); |
---|
[ec7aac] | 403 | omMarkAsStaticAddr(iiRETURNEXPR); |
---|
[8b96123] | 404 | memset(&(iiRETURNEXPR[iiRETURNEXPR_len]),0,16*sizeof(sleftv)); |
---|
[0a3ddd] | 405 | #ifdef USE_IILOCALRING |
---|
[c232af] | 406 | iiLocalRing=(ring *)omreallocSize(iiLocalRing, |
---|
[0e1846] | 407 | iiRETURNEXPR_len*sizeof(ring), |
---|
| 408 | (iiRETURNEXPR_len+16)*sizeof(ring)); |
---|
[8b96123] | 409 | memset(&(iiLocalRing[iiRETURNEXPR_len]),0,16*sizeof(ring)); |
---|
[77ff8e] | 410 | #endif |
---|
[0e1846] | 411 | iiRETURNEXPR_len+=16; |
---|
| 412 | } |
---|
| 413 | } |
---|
[a3bc95e] | 414 | sleftv * iiMake_proc(idhdl pn, package pack, sleftv* sl) |
---|
[0e1846] | 415 | { |
---|
| 416 | int err; |
---|
[2ba9a6] | 417 | procinfov pi = IDPROC(pn); |
---|
[bcd557] | 418 | if(pi->is_static && myynest==0) |
---|
| 419 | { |
---|
[5480da] | 420 | Werror("'%s::%s()' is a local procedure and cannot be accessed by an user.", |
---|
[ed3c47] | 421 | pi->libname, pi->procname); |
---|
[5480da] | 422 | return NULL; |
---|
| 423 | } |
---|
[0e1846] | 424 | iiCheckNest(); |
---|
[77ff8e] | 425 | #ifdef USE_IILOCALRING |
---|
[0e1846] | 426 | iiLocalRing[myynest]=currRing; |
---|
[9bc0b8] | 427 | //Print("currRing(%d):%s(%x) in %s\n",myynest,IDID(currRingHdl),currRing,IDID(pn)); |
---|
[77ff8e] | 428 | #endif |
---|
[0e1846] | 429 | iiRETURNEXPR[myynest+1].Init(); |
---|
[a3bc95e] | 430 | procstack->push(pi->procname); |
---|
[50a84c] | 431 | if ((traceit&TRACE_SHOW_PROC) |
---|
| 432 | || (pi->trace_flag&TRACE_SHOW_PROC)) |
---|
[0e1846] | 433 | { |
---|
[fca547] | 434 | if (traceit&TRACE_SHOW_LINENO) PrintLn(); |
---|
| 435 | Print("entering%-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest); |
---|
[0e1846] | 436 | } |
---|
| 437 | #ifdef RDEBUG |
---|
| 438 | if (traceit&TRACE_SHOW_RINGS) iiShowLevRings(); |
---|
| 439 | #endif |
---|
[057e93c] | 440 | switch (pi->language) |
---|
| 441 | { |
---|
[de5fe1] | 442 | default: |
---|
| 443 | case LANG_NONE: |
---|
| 444 | WerrorS("undefined proc"); |
---|
[77ff8e] | 445 | err=TRUE; |
---|
| 446 | break; |
---|
[6f83c3d] | 447 | |
---|
[77ff8e] | 448 | case LANG_SINGULAR: |
---|
[a3bc95e] | 449 | if ((pi->pack!=NULL)&&(currPack!=pi->pack)) |
---|
| 450 | { |
---|
| 451 | currPack=pi->pack; |
---|
[8b96123] | 452 | iiCheckPack(currPack); |
---|
[a3bc95e] | 453 | currPackHdl=packFindHdl(currPack); |
---|
| 454 | //Print("set pack=%s\n",IDID(currPackHdl)); |
---|
| 455 | } |
---|
| 456 | else if ((pack!=NULL)&&(currPack!=pack)) |
---|
| 457 | { |
---|
| 458 | currPack=pack; |
---|
[8b96123] | 459 | iiCheckPack(currPack); |
---|
[a3bc95e] | 460 | currPackHdl=packFindHdl(currPack); |
---|
[873d82] | 461 | //Print("set pack=%s\n",IDID(currPackHdl)); |
---|
[a3bc95e] | 462 | } |
---|
[77ff8e] | 463 | err=iiPStart(pn,sl); |
---|
| 464 | break; |
---|
| 465 | case LANG_C: |
---|
[c232af] | 466 | leftv res = (leftv)omAlloc0Bin(sleftv_bin); |
---|
[77ff8e] | 467 | err = (pi->data.o.function)(res, sl); |
---|
| 468 | iiRETURNEXPR[myynest+1].Copy(res); |
---|
[c232af] | 469 | omFreeBin((ADDRESS)res, sleftv_bin); |
---|
[77ff8e] | 470 | break; |
---|
[2ba9a6] | 471 | } |
---|
[50a84c] | 472 | if ((traceit&TRACE_SHOW_PROC) |
---|
| 473 | || (pi->trace_flag&TRACE_SHOW_PROC)) |
---|
[0e1846] | 474 | { |
---|
[fca547] | 475 | if (traceit&TRACE_SHOW_LINENO) PrintLn(); |
---|
| 476 | Print("leaving %-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest); |
---|
[0e1846] | 477 | } |
---|
[9bc0b8] | 478 | //char *n="NULL"; |
---|
| 479 | //if (currRingHdl!=NULL) n=IDID(currRingHdl); |
---|
| 480 | //Print("currRing(%d):%s(%x) after %s\n",myynest,n,currRing,IDID(pn)); |
---|
[0e1846] | 481 | #ifdef RDEBUG |
---|
| 482 | if (traceit&TRACE_SHOW_RINGS) iiShowLevRings(); |
---|
| 483 | #endif |
---|
| 484 | if (err) |
---|
| 485 | { |
---|
| 486 | iiRETURNEXPR[myynest+1].CleanUp(); |
---|
[355f86] | 487 | //iiRETURNEXPR[myynest+1].Init(); //done by CleanUp |
---|
[0e1846] | 488 | } |
---|
[77ff8e] | 489 | #ifdef USE_IILOCALRING |
---|
[3b1a83c] | 490 | #if 0 |
---|
[573da6] | 491 | if(procstack->cRing != iiLocalRing[myynest]) Print("iiMake_proc: 1 ring not saved procs:%x, iiLocal:%x\n",procstack->cRing, iiLocalRing[myynest]); |
---|
[3b1a83c] | 492 | #endif |
---|
[0e1846] | 493 | if (iiLocalRing[myynest] != currRing) |
---|
| 494 | { |
---|
[9bc0b8] | 495 | if (currRing!=NULL) |
---|
| 496 | { |
---|
| 497 | if (((iiRETURNEXPR[myynest+1].Typ()>BEGIN_RING) |
---|
| 498 | && (iiRETURNEXPR[myynest+1].Typ()<END_RING)) |
---|
| 499 | || ((iiRETURNEXPR[myynest+1].Typ()==LIST_CMD) |
---|
| 500 | && (lRingDependend((lists)iiRETURNEXPR[myynest+1].Data())))) |
---|
| 501 | { |
---|
| 502 | //idhdl hn; |
---|
[85e68dd] | 503 | const char *n; |
---|
| 504 | const char *o; |
---|
[9bc0b8] | 505 | idhdl nh=NULL, oh=NULL; |
---|
| 506 | if (iiLocalRing[myynest]!=NULL) |
---|
| 507 | oh=rFindHdl(iiLocalRing[myynest],NULL, NULL); |
---|
| 508 | if (oh!=NULL) o=oh->id; |
---|
| 509 | else o="none"; |
---|
| 510 | if (currRing!=NULL) |
---|
| 511 | nh=rFindHdl(currRing,NULL, NULL); |
---|
| 512 | if (nh!=NULL) n=nh->id; |
---|
| 513 | else n="none"; |
---|
| 514 | Werror("ring change during procedure call: %s -> %s (level %d)",o,n,myynest); |
---|
| 515 | iiRETURNEXPR[myynest+1].CleanUp(); |
---|
| 516 | err=TRUE; |
---|
| 517 | } |
---|
[0e1846] | 518 | } |
---|
[9f12b01] | 519 | currRing=iiLocalRing[myynest]; |
---|
[3b1a83c] | 520 | } |
---|
[9bc0b8] | 521 | if ((currRing==NULL) |
---|
| 522 | && (currRingHdl!=NULL)) |
---|
| 523 | currRing=IDRING(currRingHdl); |
---|
| 524 | else |
---|
[3b1a83c] | 525 | if ((currRing!=NULL) && |
---|
[9f12b01] | 526 | ((currRingHdl==NULL)||(IDRING(currRingHdl)!=currRing) |
---|
| 527 | ||(IDLEV(currRingHdl)>=myynest))) |
---|
[3b1a83c] | 528 | { |
---|
| 529 | rSetHdl(rFindHdl(currRing,NULL, NULL)); |
---|
| 530 | iiLocalRing[myynest]=NULL; |
---|
[77ff8e] | 531 | } |
---|
| 532 | #else /* USE_IILOCALRING */ |
---|
[573da6] | 533 | if (procstack->cRing != currRing) |
---|
[a3bc95e] | 534 | { |
---|
[573da6] | 535 | //if (procstack->cRingHdl!=NULL) |
---|
| 536 | //Print("procstack:%s,",IDID(procstack->cRingHdl)); |
---|
[a3bc95e] | 537 | //if (currRingHdl!=NULL) |
---|
| 538 | //Print(" curr:%s\n",IDID(currRingHdl)); |
---|
[573da6] | 539 | //Print("pr:%x, curr: %x\n",procstack->cRing,currRing); |
---|
[a3bc95e] | 540 | if (((iiRETURNEXPR[myynest+1].Typ()>BEGIN_RING) |
---|
| 541 | && (iiRETURNEXPR[myynest+1].Typ()<END_RING)) |
---|
| 542 | || ((iiRETURNEXPR[myynest+1].Typ()==LIST_CMD) |
---|
| 543 | && (lRingDependend((lists)iiRETURNEXPR[myynest+1].Data())))) |
---|
| 544 | { |
---|
| 545 | //idhdl hn; |
---|
| 546 | char *n; |
---|
| 547 | char *o; |
---|
[573da6] | 548 | if (procstack->cRing!=NULL) |
---|
[a3bc95e] | 549 | { |
---|
| 550 | //PrintS("reset ring\n"); |
---|
[573da6] | 551 | procstack->cRingHdl=rFindHdl(procstack->cRing,NULL, NULL); |
---|
| 552 | if (procstack->cRingHdl==NULL) |
---|
| 553 | procstack->cRingHdl= |
---|
| 554 | rFindHdl(procstack->cRing,NULL,procstack->currPack->idroot); |
---|
| 555 | if (procstack->cRingHdl==NULL) |
---|
| 556 | procstack->cRingHdl= |
---|
| 557 | rFindHdl(procstack->cRing,NULL,basePack->idroot); |
---|
| 558 | o=IDID(procstack->cRingHdl); |
---|
| 559 | currRing=procstack->cRing; |
---|
| 560 | currRingHdl=procstack->cRingHdl; |
---|
[a3bc95e] | 561 | } |
---|
| 562 | else o="none"; |
---|
| 563 | if (currRing!=NULL) n=IDID(currRingHdl); |
---|
| 564 | else n="none"; |
---|
| 565 | if (currRing==NULL) |
---|
| 566 | { |
---|
| 567 | Werror("ring change during procedure call: %s -> %s",o,n); |
---|
| 568 | iiRETURNEXPR[myynest+1].CleanUp(); |
---|
| 569 | err=TRUE; |
---|
| 570 | } |
---|
| 571 | } |
---|
[573da6] | 572 | if (procstack->cRingHdl!=NULL) |
---|
[a3bc95e] | 573 | { |
---|
[573da6] | 574 | rSetHdl(procstack->cRingHdl); |
---|
[a3bc95e] | 575 | } |
---|
| 576 | else |
---|
| 577 | { currRingHdl=NULL; currRing=NULL; } |
---|
| 578 | } |
---|
| 579 | #endif /* USE_IILOCALRING */ |
---|
[0e1846] | 580 | if (iiCurrArgs!=NULL) |
---|
| 581 | { |
---|
[bcd557] | 582 | if (!err) Warn("too many arguments for %s",IDID(pn)); |
---|
[0e1846] | 583 | iiCurrArgs->CleanUp(); |
---|
[c232af] | 584 | omFreeBin((ADDRESS)iiCurrArgs, sleftv_bin); |
---|
[0e1846] | 585 | iiCurrArgs=NULL; |
---|
| 586 | } |
---|
[a3bc95e] | 587 | procstack->pop(); |
---|
[bcd557] | 588 | if (err) |
---|
| 589 | return NULL; |
---|
[0e1846] | 590 | return &iiRETURNEXPR[myynest+1]; |
---|
| 591 | } |
---|
| 592 | |
---|
| 593 | /*2 |
---|
| 594 | * start an example (as a proc), |
---|
| 595 | * destroys the string 'example' |
---|
| 596 | */ |
---|
[057e93c] | 597 | BOOLEAN iiEStart(char* example, procinfo *pi) |
---|
[0e1846] | 598 | { |
---|
| 599 | BOOLEAN err; |
---|
[057e93c] | 600 | int old_echo=si_echo; |
---|
[0e1846] | 601 | |
---|
[4e4ece7] | 602 | newBuffer( example, BT_example, pi, |
---|
[c04b94] | 603 | (pi != NULL ? pi->data.s.example_lineno: 0)); |
---|
| 604 | |
---|
[0e1846] | 605 | iiCheckNest(); |
---|
[a3bc95e] | 606 | procstack->push(example); |
---|
[77ff8e] | 607 | #ifdef USE_IILOCALRING |
---|
[0e1846] | 608 | iiLocalRing[myynest]=currRing; |
---|
[77ff8e] | 609 | #endif |
---|
[0e1846] | 610 | if (traceit&TRACE_SHOW_PROC) |
---|
| 611 | { |
---|
| 612 | if (traceit&TRACE_SHOW_LINENO) printf("\n"); |
---|
| 613 | printf("entering example (level %d)\n",myynest); |
---|
| 614 | } |
---|
| 615 | myynest++; |
---|
[8c242f] | 616 | iiRETURNEXPR[myynest].Init(); |
---|
[0e1846] | 617 | err=yyparse(); |
---|
[b89cf5d] | 618 | if (sLastPrinted.rtyp!=0) |
---|
| 619 | { |
---|
| 620 | sLastPrinted.CleanUp(); |
---|
[355f86] | 621 | //memset(&sLastPrinted,0,sizeof(sleftv)); //done by CleanUp |
---|
[b89cf5d] | 622 | } |
---|
[0e1846] | 623 | killlocals(myynest); |
---|
| 624 | myynest--; |
---|
[057e93c] | 625 | si_echo=old_echo; |
---|
[0e1846] | 626 | if (traceit&TRACE_SHOW_PROC) |
---|
| 627 | { |
---|
| 628 | if (traceit&TRACE_SHOW_LINENO) printf("\n"); |
---|
| 629 | printf("leaving -example- (level %d)\n",myynest); |
---|
| 630 | } |
---|
[77ff8e] | 631 | #ifdef USE_IILOCALRING |
---|
[0e1846] | 632 | if (iiLocalRing[myynest] != currRing) |
---|
| 633 | { |
---|
| 634 | if (iiLocalRing[myynest]!=NULL) |
---|
| 635 | { |
---|
[cf42ab1] | 636 | rSetHdl(rFindHdl(iiLocalRing[myynest],NULL, NULL)); |
---|
[0e1846] | 637 | iiLocalRing[myynest]=NULL; |
---|
| 638 | } |
---|
| 639 | else |
---|
| 640 | { |
---|
| 641 | currRingHdl=NULL; |
---|
| 642 | currRing=NULL; |
---|
| 643 | } |
---|
| 644 | } |
---|
[77ff8e] | 645 | #else /* USE_IILOCALRING */ |
---|
[a3bc95e] | 646 | #endif /* USE_IILOCALRING */ |
---|
[77ff8e] | 647 | if (NS_LRING != currRing) |
---|
| 648 | { |
---|
| 649 | if (NS_LRING!=NULL) |
---|
| 650 | { |
---|
[573da6] | 651 | idhdl rh=procstack->cRingHdl; |
---|
[bd4cb92] | 652 | if ((rh==NULL)||(IDRING(rh)!=NS_LRING)) |
---|
| 653 | rh=rFindHdl(NS_LRING,NULL, NULL); |
---|
| 654 | rSetHdl(rh); |
---|
[77ff8e] | 655 | } |
---|
| 656 | else |
---|
| 657 | { |
---|
| 658 | currRingHdl=NULL; |
---|
| 659 | currRing=NULL; |
---|
| 660 | } |
---|
| 661 | } |
---|
[a3bc95e] | 662 | //#endif /* USE_IILOCALRING */ |
---|
| 663 | procstack->pop(); |
---|
[0e1846] | 664 | return err; |
---|
| 665 | } |
---|
| 666 | |
---|
[8edd35] | 667 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
[85e68dd] | 668 | BOOLEAN iiTryLoadLib(leftv v, const char *id) |
---|
[8edd35] | 669 | { |
---|
| 670 | BOOLEAN LoadResult = TRUE; |
---|
| 671 | char libnamebuf[128]; |
---|
[c232af] | 672 | char *libname = (char *)omAlloc(strlen(id)+5); |
---|
[85e68dd] | 673 | const char *suffix[] = { "", ".lib", ".so", ".sl", NULL }; |
---|
[8edd35] | 674 | int i = 0; |
---|
| 675 | FILE *fp; |
---|
| 676 | package pack; |
---|
| 677 | idhdl packhdl; |
---|
| 678 | lib_types LT; |
---|
| 679 | |
---|
[743c32] | 680 | for(i=0; suffix[i] != NULL; i++) |
---|
| 681 | { |
---|
[8edd35] | 682 | sprintf(libname, "%s%s", id, suffix[i]); |
---|
| 683 | *libname = mytolower(*libname); |
---|
[2166892] | 684 | if((LT = type_of_LIB(libname, libnamebuf)) > LT_NOTFOUND) |
---|
[743c32] | 685 | { |
---|
[2166892] | 686 | char *s=omStrDup(libname); |
---|
| 687 | char libnamebuf[256]; |
---|
| 688 | |
---|
| 689 | if (LT==LT_SINGULAR) |
---|
[22ed4c] | 690 | LoadResult = iiLibCmd(s, FALSE, FALSE,TRUE); |
---|
[24590f] | 691 | #ifdef HAVE_DYNAMIC_LOADING |
---|
[2166892] | 692 | else if ((LT==LT_ELF) || (LT==LT_HPUX)) |
---|
| 693 | LoadResult = load_modules(s,libnamebuf,FALSE); |
---|
[24590f] | 694 | #endif |
---|
[2166892] | 695 | if(!LoadResult ) |
---|
[743c32] | 696 | { |
---|
[8edd35] | 697 | v->name = iiConvName(libname); |
---|
| 698 | break; |
---|
| 699 | } |
---|
| 700 | } |
---|
| 701 | } |
---|
[c232af] | 702 | omFree(libname); |
---|
[8edd35] | 703 | return LoadResult; |
---|
| 704 | } |
---|
| 705 | |
---|
[1d1101] | 706 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
[50cbdc] | 707 | /* check, if library lib has already been loaded |
---|
[698457] | 708 | if yes, writes filename of lib into where and returns TRUE, |
---|
| 709 | no, returns FALSE |
---|
| 710 | */ |
---|
| 711 | BOOLEAN iiLocateLib(const char* lib, char* where) |
---|
| 712 | { |
---|
| 713 | idhdl hl; |
---|
[4e4ece7] | 714 | |
---|
[5121f6] | 715 | char *p; |
---|
[50cbdc] | 716 | |
---|
[5af2f9] | 717 | hl = IDROOT->get("LIB", 0); |
---|
[5121f6] | 718 | if (hl == NULL || (p=strstr(IDSTRING(hl), lib)) == NULL) return FALSE; |
---|
| 719 | if ((p!=IDSTRING(hl)) && (*(p-1)!=',')) return FALSE; |
---|
[4e4ece7] | 720 | |
---|
[698457] | 721 | if (strstr(IDSTRING(hl), ",") == NULL) |
---|
| 722 | { |
---|
| 723 | strcpy(where, IDSTRING(hl)); |
---|
| 724 | } |
---|
| 725 | else |
---|
| 726 | { |
---|
[c232af] | 727 | char* tmp = omStrDup(IDSTRING(hl)); |
---|
[698457] | 728 | char* tok = strtok(tmp, ","); |
---|
| 729 | do |
---|
| 730 | { |
---|
| 731 | if (strstr(tok, lib) != NULL) break; |
---|
| 732 | tok = strtok(NULL, ","); |
---|
| 733 | } |
---|
| 734 | while (tok != NULL); |
---|
| 735 | assume(tok != NULL); |
---|
| 736 | strcpy(where, tok); |
---|
[c232af] | 737 | omFree(tmp); |
---|
[698457] | 738 | } |
---|
| 739 | return TRUE; |
---|
| 740 | } |
---|
[4e4ece7] | 741 | |
---|
[22ed4c] | 742 | BOOLEAN iiLibCmd( char *newlib, BOOLEAN autoexport, BOOLEAN tellerror, BOOLEAN force ) |
---|
[0e1846] | 743 | { |
---|
| 744 | char buf[256]; |
---|
| 745 | char libnamebuf[128]; |
---|
[1bd25e] | 746 | idhdl h; |
---|
[8edd35] | 747 | BOOLEAN LoadResult = TRUE; |
---|
[a3bc95e] | 748 | idhdl pl; |
---|
[1bd25e] | 749 | idhdl hl; |
---|
[2ba9a6] | 750 | int lines = 1; |
---|
| 751 | long pos = 0L; |
---|
| 752 | procinfov pi; |
---|
[a3bc95e] | 753 | char *plib = iiConvName(newlib); |
---|
[dfc6b54] | 754 | FILE * fp = feFopen( newlib, "r", libnamebuf, tellerror ); |
---|
[0e1846] | 755 | if (fp==NULL) |
---|
| 756 | { |
---|
| 757 | return TRUE; |
---|
| 758 | } |
---|
[1e3015] | 759 | #ifdef HAVE_TCL |
---|
| 760 | if (tclmode) |
---|
| 761 | { |
---|
| 762 | PrintTCLS('L',newlib); |
---|
| 763 | } |
---|
| 764 | #endif |
---|
[a3bc95e] | 765 | pl = basePack->idroot->get(plib,0); |
---|
| 766 | if (pl==NULL) |
---|
| 767 | { |
---|
[3b1a83c] | 768 | pl = enterid( plib,0, PACKAGE_CMD, |
---|
[a3bc95e] | 769 | &(basePack->idroot), TRUE ); |
---|
| 770 | IDPACKAGE(pl)->language = LANG_SINGULAR; |
---|
| 771 | IDPACKAGE(pl)->libname=omStrDup(newlib); |
---|
| 772 | } |
---|
| 773 | else |
---|
| 774 | { |
---|
| 775 | if(IDTYP(pl)!=PACKAGE_CMD) |
---|
| 776 | { |
---|
[7a949a] | 777 | WarnS("not of type package."); |
---|
[a3bc95e] | 778 | fclose(fp); |
---|
| 779 | return TRUE; |
---|
| 780 | } |
---|
[22ed4c] | 781 | if (!force) return FALSE; |
---|
[a3bc95e] | 782 | } |
---|
[22ed4c] | 783 | LoadResult = iiLoadLIB(fp, libnamebuf, newlib, pl, autoexport, tellerror); |
---|
[a3bc95e] | 784 | omFree((ADDRESS)newlib); |
---|
| 785 | |
---|
| 786 | if(!LoadResult) IDPACKAGE(pl)->loaded = TRUE; |
---|
[c232af] | 787 | omFree((ADDRESS)plib); |
---|
[a3bc95e] | 788 | |
---|
[21fa0b] | 789 | return LoadResult; |
---|
[8edd35] | 790 | } |
---|
[c3cb95] | 791 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
| 792 | static void iiCleanProcs(idhdl &root) |
---|
| 793 | { |
---|
[2bb771] | 794 | idhdl prev=NULL; |
---|
[c3cb95] | 795 | loop |
---|
| 796 | { |
---|
| 797 | if (root==NULL) return; |
---|
| 798 | if (IDTYP(root)==PROC_CMD) |
---|
| 799 | { |
---|
[560432] | 800 | procinfo *pi=(procinfo*)IDDATA(root); |
---|
[c3cb95] | 801 | if ((pi->language == LANG_SINGULAR) |
---|
| 802 | && (pi->data.s.body_start == 0L)) |
---|
| 803 | { |
---|
| 804 | // procinfo data incorrect: |
---|
| 805 | // - no proc body can start at the beginning of the file |
---|
[560432] | 806 | killhdl(root); |
---|
[2bb771] | 807 | if (prev==NULL) |
---|
[bd4cb92] | 808 | root=IDROOT; |
---|
[2bb771] | 809 | else |
---|
| 810 | { |
---|
| 811 | root=prev; |
---|
| 812 | prev=NULL; |
---|
| 813 | } |
---|
| 814 | continue; |
---|
[c3cb95] | 815 | } |
---|
| 816 | } |
---|
[2bb771] | 817 | prev=root; |
---|
[c3cb95] | 818 | root=IDNEXT(root); |
---|
| 819 | } |
---|
| 820 | } |
---|
[8edd35] | 821 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
[bf4ff72] | 822 | BOOLEAN iiLoadLIB(FILE *fp, char *libnamebuf, char*newlib, |
---|
[8edd35] | 823 | idhdl pl, BOOLEAN autoexport, BOOLEAN tellerror) |
---|
| 824 | { |
---|
| 825 | char buf[256]; |
---|
[6a51ef] | 826 | extern FILE *yylpin; |
---|
| 827 | libstackv ls_start = library_stack; |
---|
[5480da] | 828 | lib_style_types lib_style; |
---|
[6a51ef] | 829 | |
---|
| 830 | yylpin = fp; |
---|
[4e4ece7] | 831 | #if YYLPDEBUG > 1 |
---|
[6a51ef] | 832 | print_init(); |
---|
[4e4ece7] | 833 | #endif |
---|
[799ce1] | 834 | extern int lpverbose; |
---|
[4e4ece7] | 835 | if (BVERBOSE(V_DEBUG_LIB)) lpverbose=1; |
---|
| 836 | else lpverbose=0; |
---|
[d3e630] | 837 | yylplex(newlib, libnamebuf, &lib_style, pl, autoexport); |
---|
[805b06c] | 838 | if(yylp_errno) |
---|
| 839 | { |
---|
[6a51ef] | 840 | Werror("Library %s: ERROR occured: in line %d, %d.", newlib, yylplineno, |
---|
[5c8eae0] | 841 | current_pos(0)); |
---|
[805b06c] | 842 | if(yylp_errno==YYLP_BAD_CHAR) |
---|
| 843 | { |
---|
[60a3add] | 844 | Werror(yylp_errlist[yylp_errno], *text_buffer, yylplineno); |
---|
[c232af] | 845 | omFree((ADDRESS)text_buffer); |
---|
[9a11fe] | 846 | text_buffer=NULL; |
---|
[805b06c] | 847 | } |
---|
| 848 | else |
---|
[60a3add] | 849 | Werror(yylp_errlist[yylp_errno], yylplineno); |
---|
[6a51ef] | 850 | Werror("Cannot load library,... aborting."); |
---|
| 851 | reinit_yylp(); |
---|
| 852 | fclose( yylpin ); |
---|
[bd4cb92] | 853 | iiCleanProcs(IDROOT); |
---|
[6a51ef] | 854 | return TRUE; |
---|
[5c8eae0] | 855 | } |
---|
[4e4ece7] | 856 | if (BVERBOSE(V_LOAD_LIB)) |
---|
| 857 | Print( "// ** loaded %s %s\n", libnamebuf, text_buffer); |
---|
[805b06c] | 858 | if( (lib_style == OLD_LIBSTYLE) && (BVERBOSE(V_LOAD_LIB))) |
---|
| 859 | { |
---|
[40706c] | 860 | Warn( "library %s has old format. This format is still accepted,", newlib); |
---|
| 861 | Warn( "but for functionality you may wish to change to the new"); |
---|
| 862 | Warn( "format. Please refer to the manual for further information."); |
---|
[0a3ddd] | 863 | } |
---|
[6a51ef] | 864 | reinit_yylp(); |
---|
| 865 | fclose( yylpin ); |
---|
[dae004] | 866 | fp = NULL; |
---|
[64d729] | 867 | |
---|
[6a51ef] | 868 | { |
---|
| 869 | libstackv ls; |
---|
[805b06c] | 870 | for(ls = library_stack; (ls != NULL) && (ls != ls_start); ) |
---|
| 871 | { |
---|
| 872 | if(ls->to_be_done) |
---|
| 873 | { |
---|
[5c8eae0] | 874 | ls->to_be_done=FALSE; |
---|
[22ed4c] | 875 | iiLibCmd(ls->get(),autoexport,tellerror,FALSE); |
---|
[5c8eae0] | 876 | ls = ls->pop(newlib); |
---|
[6a51ef] | 877 | } |
---|
| 878 | } |
---|
| 879 | #if 0 |
---|
[6f83c3d] | 880 | PrintS("--------------------\n"); |
---|
| 881 | for(ls = library_stack; ls != NULL; ls = ls->next) |
---|
| 882 | { |
---|
[6a51ef] | 883 | Print("%s: LIB-stack:(%d), %s %s\n", newlib, ls->cnt, ls->get(), |
---|
| 884 | ls->to_be_done ? "not loaded" : "loaded"); |
---|
| 885 | } |
---|
[6f83c3d] | 886 | PrintS("--------------------\n"); |
---|
[6a51ef] | 887 | #endif |
---|
| 888 | } |
---|
[21fa0b] | 889 | |
---|
[dae004] | 890 | if(fp != NULL) fclose(fp); |
---|
[0e1846] | 891 | return FALSE; |
---|
| 892 | } |
---|
| 893 | |
---|
[8edd35] | 894 | |
---|
[1d1101] | 895 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
[85e68dd] | 896 | procinfo *iiInitSingularProcinfo(procinfov pi, const char *libname, |
---|
| 897 | const char *procname, int line, long pos, BOOLEAN pstatic) |
---|
[2ba9a6] | 898 | { |
---|
[c232af] | 899 | pi->libname = omStrDup(libname); |
---|
[2ba9a6] | 900 | |
---|
[057e93c] | 901 | if( strcmp(procname,"_init")==0) |
---|
| 902 | { |
---|
[805b06c] | 903 | pi->procname = iiConvName(libname); |
---|
| 904 | } |
---|
| 905 | else |
---|
[c232af] | 906 | pi->procname = omStrDup(procname); |
---|
[2ba9a6] | 907 | pi->language = LANG_SINGULAR; |
---|
| 908 | pi->ref = 1; |
---|
[a3bc95e] | 909 | pi->pack = NULL; |
---|
[6a51ef] | 910 | pi->is_static = pstatic; |
---|
[2ba9a6] | 911 | pi->data.s.proc_start = pos; |
---|
[46d09b] | 912 | pi->data.s.def_end = 0L; |
---|
[2ba9a6] | 913 | pi->data.s.help_start = 0L; |
---|
[6be769] | 914 | pi->data.s.help_end = 0L; |
---|
[2ba9a6] | 915 | pi->data.s.body_start = 0L; |
---|
| 916 | pi->data.s.body_end = 0L; |
---|
| 917 | pi->data.s.example_start = 0L; |
---|
| 918 | pi->data.s.proc_lineno = line; |
---|
| 919 | pi->data.s.body_lineno = 0; |
---|
| 920 | pi->data.s.example_lineno = 0; |
---|
| 921 | pi->data.s.body = NULL; |
---|
[847cad] | 922 | pi->data.s.help_chksum = 0; |
---|
[2ba9a6] | 923 | return(pi); |
---|
| 924 | } |
---|
| 925 | |
---|
[1d1101] | 926 | #ifdef HAVE_DYNAMIC_LOADING |
---|
| 927 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
| 928 | int iiAddCproc(char *libname, char *procname, BOOLEAN pstatic, |
---|
[ed3c47] | 929 | BOOLEAN(*func)(leftv res, leftv v)) |
---|
[1d1101] | 930 | { |
---|
| 931 | procinfov pi; |
---|
| 932 | idhdl h; |
---|
| 933 | |
---|
[3b1a83c] | 934 | h = enterid(procname,0, PROC_CMD, &IDROOT, TRUE); |
---|
[6f83c3d] | 935 | if ( h!= NULL ) |
---|
| 936 | { |
---|
[1d1101] | 937 | pi = IDPROC(h); |
---|
[c232af] | 938 | pi->libname = omStrDup(libname); |
---|
| 939 | pi->procname = omStrDup(procname); |
---|
[1d1101] | 940 | pi->language = LANG_C; |
---|
| 941 | pi->ref = 1; |
---|
| 942 | pi->is_static = pstatic; |
---|
| 943 | pi->data.o.function = func; |
---|
| 944 | return(1); |
---|
[6f83c3d] | 945 | } |
---|
| 946 | else |
---|
| 947 | { |
---|
| 948 | PrintS("iiAddCproc: failed.\n"); |
---|
[1d1101] | 949 | } |
---|
| 950 | return(0); |
---|
| 951 | } |
---|
[cd6b45] | 952 | |
---|
[c858fc2] | 953 | int iiAddCprocTop(char *libname, char *procname, BOOLEAN pstatic, |
---|
| 954 | BOOLEAN(*func)(leftv res, leftv v)) |
---|
| 955 | { |
---|
| 956 | int r=iiAddCproc(libname,procname,pstatic,func); |
---|
| 957 | package s=currPack; |
---|
| 958 | currPack=basePack; |
---|
| 959 | if (r) r=iiAddCproc(libname,procname,pstatic,func); |
---|
| 960 | currPack=s; |
---|
| 961 | return r; |
---|
| 962 | } |
---|
| 963 | |
---|
[cd6b45] | 964 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
[c858fc2] | 965 | BOOLEAN load_modules(char *newlib, char *fullname, BOOLEAN autoexport) |
---|
[cd6b45] | 966 | { |
---|
[4eb5b5] | 967 | #ifdef HAVE_STATIC |
---|
| 968 | WerrorS("mod_init: static version can not load modules"); |
---|
| 969 | return TRUE; |
---|
| 970 | #else |
---|
[cd6b45] | 971 | int iiAddCproc(char *libname, char *procname, BOOLEAN pstatic, |
---|
| 972 | BOOLEAN(*func)(leftv res, leftv v)); |
---|
[bf4997] | 973 | typedef int (*fktn_t)(int(*iiAddCproc)(char *libname, char *procname, |
---|
[cd6b45] | 974 | BOOLEAN pstatic, |
---|
| 975 | BOOLEAN(*func)(leftv res, leftv v))); |
---|
[b6e51b] | 976 | typedef int (*fktn2_t)(SModulFunctions*); |
---|
| 977 | fktn2_t fktn; |
---|
[cd6b45] | 978 | idhdl pl; |
---|
| 979 | char *plib = iiConvName(newlib); |
---|
| 980 | BOOLEAN RET=TRUE; |
---|
| 981 | int token; |
---|
| 982 | char FullName[256]; |
---|
[64d729] | 983 | |
---|
[a3dfd97] | 984 | memset(FullName,0,256); |
---|
| 985 | |
---|
[cd6b45] | 986 | if( *fullname != '/' && *fullname != '.' ) |
---|
| 987 | sprintf(FullName, "./%s", newlib); |
---|
[a3dfd97] | 988 | else strncpy(FullName, fullname,255); |
---|
[64d729] | 989 | |
---|
[cd6b45] | 990 | |
---|
[bf4997] | 991 | if(IsCmd(plib, token)) |
---|
[cd6b45] | 992 | { |
---|
| 993 | Werror("'%s' is resered identifier\n", plib); |
---|
| 994 | goto load_modules_end; |
---|
| 995 | } |
---|
[1bd25e] | 996 | pl = IDROOT->get(plib,0); |
---|
[cd6b45] | 997 | if (pl==NULL) |
---|
| 998 | { |
---|
[3b1a83c] | 999 | pl = enterid( plib,0, PACKAGE_CMD, &IDROOT, |
---|
[1bd25e] | 1000 | TRUE ); |
---|
[cd6b45] | 1001 | IDPACKAGE(pl)->language = LANG_C; |
---|
[c232af] | 1002 | IDPACKAGE(pl)->libname=omStrDup(newlib); |
---|
[cd6b45] | 1003 | } |
---|
| 1004 | else |
---|
| 1005 | { |
---|
| 1006 | if(IDTYP(pl)!=PACKAGE_CMD) |
---|
| 1007 | { |
---|
[7a949a] | 1008 | Warn("not of type package."); |
---|
[64d729] | 1009 | goto load_modules_end; |
---|
[cd6b45] | 1010 | } |
---|
| 1011 | } |
---|
[7497447] | 1012 | if((IDPACKAGE(pl)->handle=dynl_open(FullName))==(void *)NULL) |
---|
[cd6b45] | 1013 | { |
---|
[46ef83] | 1014 | Werror("dynl_open failed:%s", dynl_error()); |
---|
[cd6b45] | 1015 | Werror("%s not found", newlib); |
---|
[64d729] | 1016 | goto load_modules_end; |
---|
[cd6b45] | 1017 | } |
---|
| 1018 | else |
---|
| 1019 | { |
---|
[b6e51b] | 1020 | SModulFunctions sModulFunctions; |
---|
| 1021 | |
---|
[a3dfd97] | 1022 | package s=currPack; |
---|
| 1023 | currPack=IDPACKAGE(pl); |
---|
[b6e51b] | 1024 | fktn = (fktn2_t)dynl_sym(IDPACKAGE(pl)->handle, "mod_init"); |
---|
[c858fc2] | 1025 | if( fktn!= NULL) |
---|
| 1026 | { |
---|
[b6e51b] | 1027 | sModulFunctions.iiArithAddCmd = iiArithAddCmd; |
---|
| 1028 | if (autoexport) sModulFunctions.iiAddCproc = iiAddCprocTop; |
---|
| 1029 | else sModulFunctions.iiAddCproc = iiAddCproc; |
---|
| 1030 | (*fktn)(&sModulFunctions); |
---|
[c858fc2] | 1031 | } |
---|
[1fb78b] | 1032 | else Werror("mod_init: %s\n", dynl_error()); |
---|
[cd6b45] | 1033 | if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loaded %s \n", fullname); |
---|
[a3dfd97] | 1034 | currPack->loaded=1; |
---|
| 1035 | currPack=s; |
---|
[cd6b45] | 1036 | } |
---|
| 1037 | RET=FALSE; |
---|
| 1038 | |
---|
| 1039 | load_modules_end: |
---|
| 1040 | return RET; |
---|
[4eb5b5] | 1041 | #endif /*STATIC */ |
---|
[cd6b45] | 1042 | } |
---|
[1d1101] | 1043 | #endif /* HAVE_DYNAMIC_LOADING */ |
---|
| 1044 | |
---|
[2ba9a6] | 1045 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
[77ff8e] | 1046 | char mytoupper(char c) |
---|
| 1047 | { |
---|
| 1048 | if(c>=97 && c<=(97+26)) c-=32; |
---|
| 1049 | return(c); |
---|
| 1050 | } |
---|
| 1051 | |
---|
[8edd35] | 1052 | char mytolower(char c) |
---|
| 1053 | { |
---|
| 1054 | if(c>=65 && c<=(65+26)) c+=32; |
---|
| 1055 | return(c); |
---|
| 1056 | } |
---|
| 1057 | |
---|
[77ff8e] | 1058 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
[75c0dc] | 1059 | //#if defined(WINNT) |
---|
| 1060 | //# define FS_SEP '\\' |
---|
| 1061 | //#else |
---|
| 1062 | //# define FS_SEP '/' |
---|
| 1063 | //#endif |
---|
[77ff8e] | 1064 | |
---|
[f43a74] | 1065 | char *iiConvName(const char *libname) |
---|
[2ba9a6] | 1066 | { |
---|
[c232af] | 1067 | char *tmpname = omStrDup(libname); |
---|
[9c35ef] | 1068 | char *p = strrchr(tmpname, DIR_SEP); |
---|
[ed3c47] | 1069 | char *r; |
---|
[77ff8e] | 1070 | if(p==NULL) p = tmpname; |
---|
| 1071 | else p++; |
---|
[08c2d6] | 1072 | r = (char *)strchr(p, '.'); |
---|
[77ff8e] | 1073 | if( r!= NULL) *r = '\0'; |
---|
[c232af] | 1074 | r = omStrDup(p); |
---|
[77ff8e] | 1075 | *r = mytoupper(*r); |
---|
| 1076 | // printf("iiConvName: '%s' '%s' => '%s'\n", libname, tmpname, r); |
---|
[c232af] | 1077 | omFree((ADDRESS)tmpname); |
---|
[6f83c3d] | 1078 | |
---|
[77ff8e] | 1079 | return(r); |
---|
[2ba9a6] | 1080 | } |
---|
| 1081 | |
---|
| 1082 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
[fca547] | 1083 | void piShowProcList() |
---|
[2ba9a6] | 1084 | { |
---|
| 1085 | idhdl h; |
---|
| 1086 | procinfo *proc; |
---|
| 1087 | char *name; |
---|
| 1088 | |
---|
| 1089 | Print( "%-15s %20s %s,%s %s,%s %s,%s\n", "Library", "function", |
---|
[13e3243] | 1090 | "line", "start", "line", "body", "line", "example"); |
---|
[46d09b] | 1091 | for(h = IDROOT; h != NULL; h = IDNEXT(h)) |
---|
[057e93c] | 1092 | { |
---|
| 1093 | if(IDTYP(h) == PROC_CMD) |
---|
| 1094 | { |
---|
[2ba9a6] | 1095 | proc = IDPROC(h); |
---|
[057e93c] | 1096 | if(strcmp(proc->procname, IDID(h))!=0) |
---|
| 1097 | { |
---|
[c232af] | 1098 | name = (char *)omAlloc(strlen(IDID(h))+strlen(proc->procname)+4); |
---|
[13e3243] | 1099 | sprintf(name, "%s -> %s", IDID(h), proc->procname); |
---|
[46d09b] | 1100 | Print( "%d %-15s %20s ", proc->is_static ? 1 : 0, proc->libname, name); |
---|
[c232af] | 1101 | omFree((ADDRESS)name); |
---|
[0077d9d] | 1102 | } |
---|
| 1103 | else |
---|
[46d09b] | 1104 | Print( "%d %-15s %20s ", proc->is_static ? 1 : 0, proc->libname, |
---|
| 1105 | proc->procname); |
---|
[13e3243] | 1106 | if(proc->language==LANG_SINGULAR) |
---|
| 1107 | Print("line %4d,%-5ld %4d,%-5ld %4d,%-5ld\n", |
---|
[2ba9a6] | 1108 | proc->data.s.proc_lineno, proc->data.s.proc_start, |
---|
| 1109 | proc->data.s.body_lineno, proc->data.s.body_start, |
---|
| 1110 | proc->data.s.example_lineno, proc->data.s.example_start); |
---|
[0077d9d] | 1111 | else if(proc->language==LANG_C) |
---|
| 1112 | Print("type: object\n"); |
---|
[2ba9a6] | 1113 | } |
---|
| 1114 | } |
---|
| 1115 | } |
---|
| 1116 | |
---|
| 1117 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
[fca547] | 1118 | //char *iiLineNo(char *procname, int lineno) |
---|
| 1119 | //{ |
---|
| 1120 | // char buf[256]; |
---|
| 1121 | // idhdl pn = ggetid(procname); |
---|
| 1122 | // procinfo *pi = IDPROC(pn); |
---|
| 1123 | // |
---|
| 1124 | // sprintf(buf, "%s %3d\0", procname, lineno); |
---|
| 1125 | // //sprintf(buf, "%s::%s %3d\0", pi->libname, pi->procname, |
---|
| 1126 | // // lineno + pi->data.s.body_lineno); |
---|
| 1127 | // return(buf); |
---|
| 1128 | //} |
---|
[6a51ef] | 1129 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
| 1130 | #ifdef HAVE_LIBPARSER |
---|
| 1131 | void libstack::push(char *p, char *libname) |
---|
| 1132 | { |
---|
| 1133 | libstackv lp; |
---|
[50cbdc] | 1134 | if( !iiGetLibStatus(libname)) |
---|
[743c32] | 1135 | { |
---|
| 1136 | for(lp = this;lp!=NULL;lp=lp->next) |
---|
| 1137 | { |
---|
[6a51ef] | 1138 | if(strcmp(lp->get(), libname)==0) break; |
---|
| 1139 | } |
---|
[743c32] | 1140 | if(lp==NULL) |
---|
| 1141 | { |
---|
[c232af] | 1142 | libstackv ls = (libstack *)omAlloc0Bin(libstack_bin); |
---|
[6a51ef] | 1143 | ls->next = this; |
---|
[c232af] | 1144 | ls->libname = omStrDup(libname); |
---|
[6a51ef] | 1145 | ls->to_be_done = TRUE; |
---|
| 1146 | if(this != NULL) ls->cnt = this->cnt+1; else ls->cnt = 0; |
---|
| 1147 | library_stack = ls; |
---|
[5c8eae0] | 1148 | } |
---|
[6a51ef] | 1149 | } |
---|
| 1150 | } |
---|
| 1151 | |
---|
| 1152 | libstackv libstack::pop(char *p) |
---|
| 1153 | { |
---|
| 1154 | libstackv ls = this; |
---|
[c232af] | 1155 | //omFree((ADDRESS)ls->libname); |
---|
[6a51ef] | 1156 | library_stack = ls->next; |
---|
[c232af] | 1157 | omFreeBin((ADDRESS)ls, libstack_bin); |
---|
[6a51ef] | 1158 | return(library_stack); |
---|
| 1159 | } |
---|
| 1160 | |
---|
| 1161 | #endif /* HAVE_LIBPARSER */ |
---|
| 1162 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|