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