1 /* sdcclib.c: sdcc librarian
2 Copyright (C) 2003, Jesus Calvino-Fraga jesusc(at)ece.ubc.ca
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #if !defined(__BORLANDC__) && !defined(_MSC_VER)
27 char ProgName[PATH_MAX];
28 char LibName[PATH_MAX];
29 char LibNameTmp[PATH_MAX];
30 char IndexName[PATH_MAX];
31 char AdbName[PATH_MAX];
32 char ListName[PATH_MAX];
43 #define OPT_ADD_LIST 4
44 #define OPT_DUMP_SYM 5
45 #define OPT_DUMP_MOD 6
48 #define EQ(A,B) !strcmp((A),(B))
49 #define NEQ(A,B) strcmp((A),(B))
54 #define fgets(S, SIZE, STREAM) do { \
56 char *__res4567 = fgets(__s4567, SIZE, STREAM); \
57 assert(__s4567 == __res4567); \
60 #define system(CMD) do { \
61 int __res4568 = system(CMD); \
62 assert(-1 != __res4568); \
67 FILE *lib, *newlib, *rel, *adb, *libindex;
68 char FLine[MAXLINE+1];
69 char ModName[MAXLINE+1];
71 void GetNameFromPath(char * path, char * name)
75 for(i=0; path[i]!=0; i++);
76 for(; (path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--);
77 for(j=0, i++; (path[i]!='.')&&(path[i]!=0); i++, j++) name[j]=path[i];
81 void ChangeExtension(char * path, char * ext)
85 for(i=0; path[i]!=0; i++);
86 for(; (path[i]!='.')&&(path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--);
94 printf("ERROR: Filename '%s' must have an extension\n", path);
99 void CleanLine(char * buff)
105 if((buff[j]=='\n')||(buff[j]=='\r')) buff[j]=0;
109 int set_options (char * opt)
111 int rvalue=0, unknown=0;
113 "Usage: %s [option|-options] library relfile1 relfile2 relfile3 ...\n\n"
114 "available options:\n"
115 "a,r - Adds relfile(s) to library. If relfile exists, replaces it.\n"
116 " l - Adds relfile list to library.\n"
117 " d - Deletes relfile(s) from library.\n"
118 "e,x - Extracts relfile(s) from library.\n"
119 " s - Dumps symbols of library.\n"
120 " m - Dumps modules of library.\n"
121 " v - Displays program version.\n"
122 "h,? - This help.\n";
131 printf("%s: SDCC librarian version %s\n", ProgName, version);
132 printf("by Jesus Calvino-Fraga\n\n");
133 if (unknown) printf("Unknown option: %c\n", opt[0]);
134 if (opt[0]=='v') exit(0);
135 printf(Help, ProgName);
162 void ProcLineOptions (int argc, char **argv)
167 /*Get the program name*/
168 GetNameFromPath(argv[0], ProgName);
170 for (j=1; j<argc; j++)
174 for(i=1; argv[j][i]!=0 ; i++)
175 if (set_options(&argv[j][i])) break;
182 if ((strlen(argv[j])==1) && (action==OPT_NONE))
183 set_options(argv[j]);
187 strcpy(LibName, argv[j]);
193 if(action==OPT_ADD_LIST)
194 strcpy(ListName, argv[j]);
198 RelName = (char **) calloc (1, sizeof (char *));
201 printf("ERROR: Insuficient memory.\n");
204 RelName[0]=(char *)malloc(PATH_MAX);
207 printf("ERROR: Insuficient memory.\n");
210 strcpy(RelName[0], argv[j]);
217 RelName = (char **) realloc (RelName, NumRelFiles * sizeof (char *));
220 printf("ERROR: Insuficient memory.\n");
223 RelName[NumRelFiles-1]=(char *)malloc(PATH_MAX);
224 if(RelName[NumRelFiles-1]==NULL)
226 printf("ERROR: Insuficient memory.\n");
229 strcpy(RelName[NumRelFiles-1], argv[j]);
235 if ( (cont_par<2) && (action<OPT_DUMP_SYM) )
237 printf("ERROR: Too few arguments.\n");
238 set_options("h"); /*Show help and exit*/
240 else if ( (cont_par!=1) && (action>=OPT_DUMP_SYM) )
242 printf("ERROR: Too %s arguments.\n", cont_par<1?"few":"many");
243 set_options("h"); /*Show help and exit*/
247 void AddRel(char * RelName)
251 long newlibpos, indexsize;
252 char symname[MAXLINE+1];
256 strcpy(LibNameTmp, LibName);
257 ChangeExtension(LibNameTmp, "__L");
259 strcpy(IndexName, LibName);
260 ChangeExtension(IndexName, "__I");
262 strcpy(AdbName, RelName);
263 ChangeExtension(AdbName, "adb");
265 lib=fopen(LibName, "r");
267 if(action==OPT_ADD_REL)
269 rel=fopen(RelName, "r");
272 printf("ERROR: Couldn't open file '%s'\n", RelName);
273 if(lib!=NULL) fclose(lib);
277 GetNameFromPath(RelName, ModName);
279 newlib=fopen(LibNameTmp, "w");
282 printf("ERROR: Couldn't create temporary file '%s'\n", LibNameTmp);
283 if(lib!=NULL) fclose(lib);
287 fprintf(newlib, "<FILES>\n\n");
289 libindex=fopen(IndexName, "w");
292 printf("ERROR: Couldn't create temporary file '%s'\n", IndexName);
293 if(lib!=NULL) fclose(lib);
299 if(lib!=NULL) while(!feof(lib))
302 fgets(FLine, MAXLINE, lib);
308 if(EQ(FLine, "<FILE>"))
311 fgets(FLine, MAXLINE, lib);
313 if(NEQ(FLine, ModName))
315 newlibpos=ftell(newlib);
316 fprintf(newlib, "<FILE>\n%s\n", FLine);
317 fprintf(libindex, "<MODULE>\n%s %ld\n", FLine, newlibpos);
323 fprintf(newlib, "%s\n", FLine);
324 if(EQ(FLine, "</FILE>"))
326 fprintf(newlib, "\n");
327 fprintf(libindex, "</MODULE>\n\n");
331 else if(EQ(FLine, "<REL>")) inrel=1;
332 else if(EQ(FLine, "</REL>")) inrel=0;
337 sscanf(FLine, "S %s %c", symname, &c);
338 if(c=='D') fprintf(libindex, "%s\n", symname);
345 if(action==OPT_ADD_REL)
347 newlibpos=ftell(newlib);
348 fprintf(newlib, "<FILE>\n%s\n<REL>\n", ModName);
349 fprintf(libindex, "<MODULE>\n%s %ld\n", ModName, newlibpos);
353 fgets(FLine, MAXLINE, rel);
357 fprintf(newlib, "%s\n", FLine);
361 sscanf(FLine, "S %s %c", symname, &c);
362 if(c=='D') fprintf(libindex, "%s\n", symname);
366 fprintf(libindex, "</MODULE>\n");
367 fprintf(newlib, "</REL>\n<ADB>\n");
369 adb=fopen(AdbName, "r");
375 fgets(FLine, MAXLINE, adb);
379 fprintf(newlib, "%s\n", FLine);
384 fprintf(newlib, "</ADB>\n</FILE>\n");
387 /*Put the temporary files together as a new library file*/
388 indexsize=ftell(libindex);
392 if(lib!=NULL) fclose(lib);
395 newlib=fopen(LibNameTmp, "r");
396 lib=fopen(LibName, "w");
397 libindex=fopen(IndexName, "r");
399 fprintf(lib, "<SDCCLIB>\n\n<INDEX>\n");
401 /*Find out if the \n is expanded to \r\n or not*/
402 if(ftell(lib)!=(long)strlen("<SDCCLIB>\n\n<INDEX>\n"))
407 indexsize+=ftell(lib)+strlen("0123456789\n\n</INDEX>\n\n");
408 if(IsDOSStyle) indexsize+=4;
410 fprintf(lib, "%10ld\n", indexsize);
412 while(!feof(libindex))
415 fgets(FLine, MAXLINE, libindex);
416 fprintf(lib, "%s", FLine);
418 fprintf(lib, "\n</INDEX>\n\n");
423 fgets(FLine, MAXLINE, newlib);
424 fprintf(lib, "%s", FLine);
426 fprintf(lib, "\n</FILES>\n\n");
427 fprintf(lib, "</SDCCLIB>\n");
437 void ExtractRel(char * RelName)
441 strcpy(AdbName, RelName);
442 ChangeExtension(AdbName, "adb");
444 lib=fopen(LibName, "r");
447 printf("ERROR: Couldn't open file '%s'\n", LibName);
451 rel=fopen(RelName, "w");
454 printf("ERROR: Couldn't create file '%s'\n", RelName);
458 GetNameFromPath(RelName, ModName);
460 adb=fopen(AdbName, "w");
463 printf("ERROR: Couldn't create file '%s'\n", AdbName);
473 fgets(FLine, MAXLINE, lib);
479 if(EQ(FLine, "<FILE>"))
482 fgets(FLine, MAXLINE, lib);
484 if(EQ(FLine, ModName)) state=1;
488 if(EQ(FLine, "<REL>")) state=2;
491 if(EQ(FLine, "</REL>"))
494 fprintf(rel, "%s\n", FLine);
497 if(EQ(FLine, "<ADB>")) state=4;
500 if(EQ(FLine, "</ADB>"))
503 fprintf(adb, "%s\n", FLine);
513 void DumpSymbols(void)
517 lib=fopen(LibName, "r");
520 printf("ERROR: Couldn't open file '%s'\n", LibName);
525 fgets(FLine, MAXLINE, lib);
527 if(NEQ(FLine, "<SDCCLIB>"))
529 printf("ERROR: File '%s' was not created with '%s'\n", LibName, ProgName);
537 fgets(FLine, MAXLINE, lib);
543 if(EQ(FLine, "<INDEX>")) state=1;
546 if(EQ(FLine, "<MODULE>"))
549 fgets(FLine, MAXLINE, lib);
550 sscanf(FLine, "%s", ModName);
551 if(action==OPT_DUMP_SYM)
553 printf("%s.rel:\n", ModName);
558 printf("%s.rel\n", ModName);
561 else if(EQ(FLine, "</INDEX>")) state=3;
564 if(EQ(FLine, "</MODULE>"))
569 else printf(" %s\n", FLine);
581 int fileexist(char * fname)
585 fp=fopen(fname, "r");
586 if(fp==NULL) return 0;
597 char SrcName[PATH_MAX];
598 char RelName[PATH_MAX];
600 list=fopen(ListName, "r");
603 printf("ERROR: Couldn't open list file '%s'\n", ListName);
607 cc = getenv("SDCCLIB_CC");
608 as = getenv("SDCCLIB_AS");
614 fgets(RelName, PATH_MAX, list);
616 if(strlen(RelName)>0) //Skip empty lines
618 if((cc!=NULL)||(as!=NULL))
620 strcpy(SrcName, RelName);
621 if(strchr(SrcName,'.')==NULL)
622 strcat(SrcName,".src");
627 ChangeExtension(SrcName, "c");
628 if(fileexist(SrcName))
630 sprintf(CmdLine, "%s %s", cc, SrcName);
631 printf("%s\n", CmdLine);
637 ChangeExtension(SrcName, "asm");
638 if(fileexist(SrcName))
640 sprintf(CmdLine, "%s %s", as, SrcName);
641 printf("%s\n", CmdLine);
646 if(strchr(RelName,'.')==NULL)
648 //Try adding the default sdcc extensions
649 strcat(RelName,".o");
650 if(!fileexist(RelName))
651 ChangeExtension(RelName, "rel");
654 printf("Adding: %s\n", RelName);
662 int main(int argc, char **argv)
665 ProcLineOptions (argc, argv);
673 for(j=0; j<NumRelFiles; j++) AddRel(RelName[j]);
675 for(j=0; j<NumRelFiles; j++) free(RelName[j]);
684 for(j=0; j<NumRelFiles; j++) ExtractRel(RelName[j]);
686 for(j=0; j<NumRelFiles; j++) free(RelName[j]);
695 return 0; //Success!!!