void ldpkg(Biobuf *f, char *pkg, int64 len, char *filename, int whence) { char *data, *p0, *p1, *name; if(debug['g']) return; if((int)len != len) { fprint(2, "%s: too much pkg data in %s\n", argv0, filename); if(debug['u']) errorexit(); return; } data = mal(len+1); if(Bread(f, data, len) != len) { fprint(2, "%s: short pkg read %s\n", argv0, filename); if(debug['u']) errorexit(); return; } data[len] = '\0'; // first \n$$ marks beginning of exports - skip rest of line p0 = strstr(data, "\n$$"); if(p0 == nil) { if(debug['u'] && whence != ArchiveObj) { fprint(2, "%s: cannot find export data in %s\n", argv0, filename); errorexit(); } return; } p0 += 3; while(*p0 != '\n' && *p0 != '\0') p0++; // second marks end of exports / beginning of local data p1 = strstr(p0, "\n$$"); if(p1 == nil) { fprint(2, "%s: cannot find end of exports in %s\n", argv0, filename); if(debug['u']) errorexit(); return; } while(p0 < p1 && (*p0 == ' ' || *p0 == '\t' || *p0 == '\n')) p0++; if(p0 < p1) { if(strncmp(p0, "package ", 8) != 0) { fprint(2, "%s: bad package section in %s - %s\n", argv0, filename, p0); if(debug['u']) errorexit(); return; } p0 += 8; while(p0 < p1 && (*p0 == ' ' || *p0 == '\t' || *p0 == '\n')) p0++; name = p0; while(p0 < p1 && *p0 != ' ' && *p0 != '\t' && *p0 != '\n') p0++; if(debug['u'] && whence != ArchiveObj && (p0+6 > p1 || memcmp(p0, " safe\n", 6) != 0)) { fprint(2, "%s: load of unsafe package %s\n", argv0, filename); nerrors++; errorexit(); } if(p0 < p1) { if(*p0 == '\n') *p0++ = '\0'; else { *p0++ = '\0'; while(p0 < p1 && *p0++ != '\n') ; } } if(strcmp(pkg, "main") == 0 && strcmp(name, "main") != 0) fprint(2, "%s: %s: not package main (package %s)\n", argv0, filename, name); loadpkgdata(filename, pkg, p0, p1 - p0); } // The __.PKGDEF archive summary has no local types. if(whence == Pkgdef) return; // local types begin where exports end. // skip rest of line after $$ we found above p0 = p1 + 3; while(*p0 != '\n' && *p0 != '\0') p0++; // local types end at next \n$$. p1 = strstr(p0, "\n$$"); if(p1 == nil) { fprint(2, "%s: cannot find end of local types in %s\n", argv0, filename); if(debug['u']) errorexit(); return; } loadpkgdata(filename, pkg, p0, p1 - p0); // look for dynimport section p0 = strstr(p1, "\n$$ // dynimport"); if(p0 != nil) { p0 = strchr(p0+1, '\n'); if(p0 == nil) { fprint(2, "%s: found $$ // dynimport but no newline in %s\n", argv0, filename); if(debug['u']) errorexit(); return; } p1 = strstr(p0, "\n$$"); if(p1 == nil) p1 = strstr(p0, "\n!\n"); if(p1 == nil) { fprint(2, "%s: cannot find end of // dynimport section in %s\n", argv0, filename); if(debug['u']) errorexit(); return; } loaddynimport(filename, pkg, p0 + 1, p1 - (p0+1)); } // look for dynexp section p0 = strstr(p1, "\n$$ // dynexport"); if(p0 != nil) { p0 = strchr(p0+1, '\n'); if(p0 == nil) { fprint(2, "%s: found $$ // dynexporg but no newline in %s\n", argv0, filename); if(debug['u']) errorexit(); return; } p1 = strstr(p0, "\n$$"); if(p1 == nil) p1 = strstr(p0, "\n!\n"); if(p1 == nil) { fprint(2, "%s: cannot find end of // dynexporg section in %s\n", argv0, filename); if(debug['u']) errorexit(); return; } loaddynexport(filename, pkg, p0 + 1, p1 - (p0+1)); } }
void ldpkg(Biobuf *f, int64 len, char *filename) { char *data, *p0, *p1; if(debug['g']) return; if((int)len != len) { fprint(2, "%s: too much pkg data in %s\n", argv0, filename); return; } data = mal(len+1); if(Bread(f, data, len) != len) { fprint(2, "%s: short pkg read %s\n", argv0, filename); return; } data[len] = '\0'; // first \n$$ marks beginning of exports - skip rest of line p0 = strstr(data, "\n$$"); if(p0 == nil) return; p0 += 3; while(*p0 != '\n' && *p0 != '\0') p0++; // second marks end of exports / beginning of local data p1 = strstr(p0, "\n$$"); if(p1 == nil) { fprint(2, "%s: cannot find end of exports in %s\n", argv0, filename); return; } while(p0 < p1 && (*p0 == ' ' || *p0 == '\t' || *p0 == '\n')) p0++; if(p0 < p1) { if(strncmp(p0, "package ", 8) != 0) { fprint(2, "%s: bad package section in %s - %s\n", argv0, filename, p0); return; } p0 += 8; while(*p0 == ' ' || *p0 == '\t' || *p0 == '\n') p0++; while(*p0 != ' ' && *p0 != '\t' && *p0 != '\n') p0++; loadpkgdata(filename, p0, p1 - p0); } // local types begin where exports end. // skip rest of line after $$ we found above p0 = p1 + 3; while(*p0 != '\n' && *p0 != '\0') p0++; // local types end at next \n$$. p1 = strstr(p0, "\n$$"); if(p1 == nil) { fprint(2, "%s: cannot find end of local types in %s\n", argv0, filename); return; } loadpkgdata(filename, p0, p1 - p0); // look for dynld section p0 = strstr(p1, "\n$$ // dynld"); if(p0 != nil) { p0 = strchr(p0+1, '\n'); if(p0 == nil) { fprint(2, "%s: found $$ // dynld but no newline in %s\n", argv0, filename); return; } p1 = strstr(p0, "\n$$"); if(p1 == nil) p1 = strstr(p0, "\n!\n"); if(p1 == nil) { fprint(2, "%s: cannot find end of // dynld section in %s\n", argv0, filename); return; } loaddynld(filename, p0 + 1, p1 - p0); } }