X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=support%2Flibrarian%2Fsdcclib.c;h=9367f8f32b462ad06ac9070a6626cee3bdb55873;hb=f3a44fc094dc5ce393e2029d3c7c6587a4b1a835;hp=ef058e7ae165d6425d2ce21225b3f9299ec23c5f;hpb=cdb212f367843cf9bcd9f9f27ab31568483100e3;p=fw%2Fsdcc diff --git a/support/librarian/sdcclib.c b/support/librarian/sdcclib.c index ef058e7a..9367f8f3 100644 --- a/support/librarian/sdcclib.c +++ b/support/librarian/sdcclib.c @@ -15,63 +15,88 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#define _POSIX_ +#include #include #include #include -#define _POSIX_ -#include +#if !defined(__BORLANDC__) && !defined(_MSC_VER) +#include +#endif char ProgName[PATH_MAX]; char LibName[PATH_MAX]; char LibNameTmp[PATH_MAX]; char IndexName[PATH_MAX]; -char RelName[PATH_MAX]; char AdbName[PATH_MAX]; +char ListName[PATH_MAX]; + +char **RelName; +int NumRelFiles=0; -#define version "0.1" +#define version "1.2" -#define OPT_ADD_REL 0 -#define OPT_EXT_REL 1 -#define OPT_DEL_REL 2 -#define OPT_DUMP_SYM 3 -#define OPT_DUMP_MOD 4 +#define OPT_NONE 0 +#define OPT_ADD_REL 1 +#define OPT_EXT_REL 2 +#define OPT_DEL_REL 3 +#define OPT_ADD_LIST 4 +#define OPT_DUMP_SYM 5 +#define OPT_DUMP_MOD 6 #define MAXLINE 254 #define EQ(A,B) !strcmp((A),(B)) #define NEQ(A,B) strcmp((A),(B)) -int action=0; +#if 1 +#include +#define __assert(COND) do { assert(COND); } while (0) +#else +#define __assert(COND) do { (void)(COND); } while (0) +#endif + +#define fgets(S, SIZE, STREAM) do { \ + char *__s4567 = S; \ + char *__res4567 = fgets(__s4567, SIZE, STREAM); \ + __assert(__s4567 == __res4567); \ +} while (0) + +#define system(CMD) do { \ + int __res4568 = system(CMD); \ + __assert(-1 != __res4568); \ +} while (0) + +int action=OPT_NONE; FILE *lib, *newlib, *rel, *adb, *libindex; char FLine[MAXLINE+1]; char ModName[MAXLINE+1]; -int state=0; void GetNameFromPath(char * path, char * name) { - int i, j; + int i, j; - for(i=0; path[i]!=0; i++); - for(; (path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--); - for(j=0, i++; (path[i]!='.')&&(path[i]!=0); i++, j++) name[j]=path[i]; - name[j]=0; + for(i=0; path[i]!=0; i++); + for(; (path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--); + for(j=0, i++; (path[i]!='.')&&(path[i]!=0); i++, j++) name[j]=path[i]; + name[j]=0; } void ChangeExtension(char * path, char * ext) { - int i, j; + int i; - for(i=0; path[i]!=0; i++); - for(; (path[i]!='.')&&(path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--); - if(path[i]=='.') - { - path[i+1]=0; + for(i=0; path[i]!=0; i++); + for(; (path[i]!='.')&&(path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--); + if(path[i]=='.') + { + path[i+1]=0; strcat(path, ext); - } - else - { - printf("ERROR: Filename '%s' must have an extension\n", path); - exit(1); - } + } + else + { + printf("ERROR: Filename '%s' must have an extension\n", path); + exit(1); + } } void CleanLine(char * buff) @@ -86,106 +111,150 @@ void CleanLine(char * buff) int set_options (char * opt) { - char temp[255]; - int rvalue=0, unknown=0; - static char Help[] = - "Usage: %s [-options] library relfile\n\n" - "available options:\n" - "a - Adds relfile to library. If relfile exists, replaces it.\n" - "d - Deletes relfile from library.\n" - "e - Extracts relfile from library.\n" - "s - Dumps symbols of library.\n" - "m - Dumps modules of library.\n" - "v - Displays program version.\n" - "h - This help.\n"; - - switch (opt[0]) - { - default: - unknown=1; - case 'h': - case '?': - case 'v': - printf("%s: SDCC librarian version %s\n", ProgName, version); - printf("by Jesus Calvino-Fraga\n\n"); - if (unknown) printf("Unknown option: %c\n", opt[0]); - if (opt[0]=='v') exit(0); - printf(Help, ProgName); - exit(1); - break; - case 'a': - action=OPT_ADD_REL; - break; - case 'e': - action=OPT_EXT_REL; - break; - case 'd': - action=OPT_DEL_REL; - break; - case 's': - action=OPT_DUMP_SYM; - break; - case 'm': - action=OPT_DUMP_MOD; - break; - } - return (rvalue); + int rvalue=0, unknown=0; + static char Help[] = + "Usage: %s [option|-options] library relfile1 relfile2 relfile3 ...\n\n" + "available options:\n" + "a,r - Adds relfile(s) to library. If relfile exists, replaces it.\n" + " l - Adds relfile list to library.\n" + " d - Deletes relfile(s) from library.\n" + "e,x - Extracts relfile(s) from library.\n" + " s - Dumps symbols of library.\n" + " m - Dumps modules of library.\n" + " v - Displays program version.\n" + "h,? - This help.\n"; + + switch (opt[0]) + { + default: + unknown=1; + case 'h': + case '?': + case 'v': + printf("%s: SDCC librarian version %s\n", ProgName, version); + printf("by Jesus Calvino-Fraga\n\n"); + if (unknown) printf("Unknown option: %c\n", opt[0]); + if (opt[0]=='v') exit(0); + printf(Help, ProgName); + exit(1); + break; + case 'a': + case 'r': + action=OPT_ADD_REL; + break; + case 'l': + action=OPT_ADD_LIST; + break; + case 'e': + case 'x': + action=OPT_EXT_REL; + break; + case 'd': + action=OPT_DEL_REL; + break; + case 's': + action=OPT_DUMP_SYM; + break; + case 'm': + action=OPT_DUMP_MOD; + break; + } + return (rvalue); } void ProcLineOptions (int argc, char **argv) { - int cont_par=0; - int i, j; + int cont_par=0; + int i, j; - /*Get the program name*/ + /*Get the program name*/ GetNameFromPath(argv[0], ProgName); - for (j=1; j=OPT_DUMP_SYM) ) - { - printf("Error: Too %s arguments.\n", cont_par<1?"few":"many"); - set_options("h"); /*Show help and exit*/ - } + for (j=1; j=OPT_DUMP_SYM) ) + { + printf("ERROR: Too %s arguments.\n", cont_par<1?"few":"many"); + set_options("h"); /*Show help and exit*/ + } } -void AddRel(void) +void AddRel(char * RelName) { int inrel=0; + int state=0; long newlibpos, indexsize; char symname[MAXLINE+1]; char c; + int IsDOSStyle=0; strcpy(LibNameTmp, LibName); ChangeExtension(LibNameTmp, "__L"); @@ -203,7 +272,7 @@ void AddRel(void) rel=fopen(RelName, "r"); if(rel==NULL) { - printf("ERROR: Couldn't open file '%s'", RelName); + printf("ERROR: Couldn't open file '%s'\n", RelName); if(lib!=NULL) fclose(lib); return; } @@ -213,7 +282,7 @@ void AddRel(void) newlib=fopen(LibNameTmp, "w"); if(newlib==NULL) { - printf("ERROR: Couldn't create temporary file '%s'", LibNameTmp); + printf("ERROR: Couldn't create temporary file '%s'\n", LibNameTmp); if(lib!=NULL) fclose(lib); fclose(rel); return; @@ -223,7 +292,7 @@ void AddRel(void) libindex=fopen(IndexName, "w"); if(libindex==NULL) { - printf("ERROR: Couldn't create temporary file '%s'", IndexName); + printf("ERROR: Couldn't create temporary file '%s'\n", IndexName); if(lib!=NULL) fclose(lib); fclose(newlib); fclose(rel); @@ -330,10 +399,17 @@ void AddRel(void) lib=fopen(LibName, "w"); libindex=fopen(IndexName, "r"); - fprintf(lib, "\n\n"); - fprintf(lib, "\n"); + fprintf(lib, "\n\n\n"); + + /*Find out if the \n is expanded to \r\n or not*/ + if(ftell(lib)!=(long)strlen("\n\n\n")) + { + IsDOSStyle=1; + } + + indexsize+=ftell(lib)+strlen("0123456789\n\n\n\n"); + if(IsDOSStyle) indexsize+=4; - indexsize+=ftell(lib)+12+14; fprintf(lib, "%10ld\n", indexsize); while(!feof(libindex)) @@ -357,26 +433,28 @@ void AddRel(void) fclose(lib); fclose(libindex); - unlink(LibNameTmp); - unlink(IndexName); + remove(LibNameTmp); + remove(IndexName); } -void ExtractRel(void) +void ExtractRel(char * RelName) { + int state=0; + strcpy(AdbName, RelName); ChangeExtension(AdbName, "adb"); lib=fopen(LibName, "r"); if(lib==NULL) { - printf("ERROR: Couldn't open file '%s'", LibName); + printf("ERROR: Couldn't open file '%s'\n", LibName); return; } rel=fopen(RelName, "w"); if(rel==NULL) { - printf("ERROR: Couldn't create file '%s'", RelName); + printf("ERROR: Couldn't create file '%s'\n", RelName); fclose(lib); return; } @@ -385,7 +463,7 @@ void ExtractRel(void) adb=fopen(AdbName, "w"); if(adb==NULL) { - printf("ERROR: Couldn't create file '%s'", AdbName); + printf("ERROR: Couldn't create file '%s'\n", AdbName); fclose(lib); fclose(rel); return; @@ -437,13 +515,24 @@ void ExtractRel(void) void DumpSymbols(void) { + int state=0; + lib=fopen(LibName, "r"); if(lib==NULL) { - printf("ERROR: Couldn't open file '%s'", LibName); + printf("ERROR: Couldn't open file '%s'\n", LibName); return; } + FLine[0]=0; + fgets(FLine, MAXLINE, lib); + CleanLine(FLine); + if(NEQ(FLine, "")) + { + printf("ERROR: File '%s' was not created with '%s'\n", LibName, ProgName); + return; + } + while(!feof(lib)) { if(state==3) break; @@ -492,27 +581,119 @@ void DumpSymbols(void) fclose(lib); } +int fileexist(char * fname) +{ + FILE * fp; + + fp=fopen(fname, "r"); + if(fp==NULL) return 0; + fclose(fp); + return 1; +} + +void AddList(void) +{ + FILE * list; + char *cc; + char *as; + char CmdLine[1024]; + char SrcName[PATH_MAX]; + char RelName[PATH_MAX]; + + list=fopen(ListName, "r"); + if(list==NULL) + { + printf("ERROR: Couldn't open list file '%s'\n", ListName); + return; + } + + cc = getenv("SDCCLIB_CC"); + as = getenv("SDCCLIB_AS"); + + action=OPT_ADD_REL; + while(!feof(list)) + { + RelName[0]=0; + fgets(RelName, PATH_MAX, list); + CleanLine(RelName); + if(strlen(RelName)>0) //Skip empty lines + { + if((cc!=NULL)||(as!=NULL)) + { + strcpy(SrcName, RelName); + if(strchr(SrcName,'.')==NULL) + strcat(SrcName,".src"); + } + + if(cc!=NULL) + { + ChangeExtension(SrcName, "c"); + if(fileexist(SrcName)) + { + sprintf(CmdLine, "%s %s", cc, SrcName); + printf("%s\n", CmdLine); + system(CmdLine); + } + } + else if(as!=NULL) + { + ChangeExtension(SrcName, "asm"); + if(fileexist(SrcName)) + { + sprintf(CmdLine, "%s %s", as, SrcName); + printf("%s\n", CmdLine); + system(CmdLine); + } + } + + if(strchr(RelName,'.')==NULL) + { + //Try adding the default sdcc extensions + strcat(RelName,".o"); + if(!fileexist(RelName)) + ChangeExtension(RelName, "rel"); + } + + printf("Adding: %s\n", RelName); + AddRel(RelName); + } + } + action=OPT_ADD_LIST; + fclose(list); +} + int main(int argc, char **argv) { - ProcLineOptions (argc, argv); - - switch(action) - { - default: - action=OPT_ADD_REL; - case OPT_ADD_REL: - case OPT_DEL_REL: - AddRel(); - break; - - case OPT_EXT_REL: - ExtractRel(); + int j; + ProcLineOptions (argc, argv); + + switch(action) + { + default: + action=OPT_ADD_REL; + case OPT_ADD_REL: + case OPT_DEL_REL: + for(j=0; j