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 system(CMD) do { \
55 int __res4568 = system(CMD); \
56 assert(-1 != __res4568); \
61 FILE *lib, *newlib, *rel, *adb, *libindex;
62 char FLine[MAXLINE+1];
63 char ModName[MAXLINE+1];
65 void GetNameFromPath(char * path, char * name)
69 for(i=0; path[i]!=0; i++);
70 for(; (path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--);
71 for(j=0, i++; (path[i]!='.')&&(path[i]!=0); i++, j++) name[j]=path[i];
75 void ChangeExtension(char * path, char * ext)
79 for(i=0; path[i]!=0; i++);
80 for(; (path[i]!='.')&&(path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--);
88 printf("ERROR: Filename '%s' must have an extension\n", path);
93 void CleanLine(char * buff)
99 if((buff[j]=='\n')||(buff[j]=='\r')) buff[j]=0;
103 int set_options (char * opt)
105 int rvalue=0, unknown=0;
107 "Usage: %s [option|-options] library relfile1 relfile2 relfile3 ...\n\n"
108 "available options:\n"
109 "a,r - Adds relfile(s) to library. If relfile exists, replaces it.\n"
110 " l - Adds relfile list to library.\n"
111 " d - Deletes relfile(s) from library.\n"
112 "e,x - Extracts relfile(s) from library.\n"
113 " s - Dumps symbols of library.\n"
114 " m - Dumps modules of library.\n"
115 " v - Displays program version.\n"
116 "h,? - This help.\n";
125 printf("%s: SDCC librarian version %s\n", ProgName, version);
126 printf("by Jesus Calvino-Fraga\n\n");
127 if (unknown) printf("Unknown option: %c\n", opt[0]);
128 if (opt[0]=='v') exit(0);
129 printf(Help, ProgName);
156 void ProcLineOptions (int argc, char **argv)
161 /*Get the program name*/
162 GetNameFromPath(argv[0], ProgName);
164 for (j=1; j<argc; j++)
168 for(i=1; argv[j][i]!=0 ; i++)
169 if (set_options(&argv[j][i])) break;
176 if ((strlen(argv[j])==1) && (action==OPT_NONE))
177 set_options(argv[j]);
181 strcpy(LibName, argv[j]);
187 if(action==OPT_ADD_LIST)
188 strcpy(ListName, argv[j]);
192 RelName = (char **) calloc (1, sizeof (char *));
195 printf("ERROR: Insuficient memory.\n");
198 RelName[0]=(char *)malloc(PATH_MAX);
201 printf("ERROR: Insuficient memory.\n");
204 strcpy(RelName[0], argv[j]);
211 RelName = (char **) realloc (RelName, NumRelFiles * sizeof (char *));
214 printf("ERROR: Insuficient memory.\n");
217 RelName[NumRelFiles-1]=(char *)malloc(PATH_MAX);
218 if(RelName[NumRelFiles-1]==NULL)
220 printf("ERROR: Insuficient memory.\n");
223 strcpy(RelName[NumRelFiles-1], argv[j]);
229 if ( (cont_par<2) && (action<OPT_DUMP_SYM) )
231 printf("ERROR: Too few arguments.\n");
232 set_options("h"); /*Show help and exit*/
234 else if ( (cont_par!=1) && (action>=OPT_DUMP_SYM) )
236 printf("ERROR: Too %s arguments.\n", cont_par<1?"few":"many");
237 set_options("h"); /*Show help and exit*/
241 void AddRel(char * RelName)
245 long newlibpos, indexsize;
246 char symname[MAXLINE+1];
250 strcpy(LibNameTmp, LibName);
251 ChangeExtension(LibNameTmp, "__L");
253 strcpy(IndexName, LibName);
254 ChangeExtension(IndexName, "__I");
256 strcpy(AdbName, RelName);
257 ChangeExtension(AdbName, "adb");
259 lib=fopen(LibName, "r");
261 if(action==OPT_ADD_REL)
263 rel=fopen(RelName, "r");
266 printf("ERROR: Couldn't open file '%s'\n", RelName);
267 if(lib!=NULL) fclose(lib);
271 GetNameFromPath(RelName, ModName);
273 newlib=fopen(LibNameTmp, "w");
276 printf("ERROR: Couldn't create temporary file '%s'\n", LibNameTmp);
277 if(lib!=NULL) fclose(lib);
281 fprintf(newlib, "<FILES>\n\n");
283 libindex=fopen(IndexName, "w");
286 printf("ERROR: Couldn't create temporary file '%s'\n", IndexName);
287 if(lib!=NULL) fclose(lib);
293 if(lib!=NULL) while(!feof(lib))
296 fgets(FLine, MAXLINE, lib);
302 if(EQ(FLine, "<FILE>"))
305 fgets(FLine, MAXLINE, lib);
307 if(NEQ(FLine, ModName))
309 newlibpos=ftell(newlib);
310 fprintf(newlib, "<FILE>\n%s\n", FLine);
311 fprintf(libindex, "<MODULE>\n%s %ld\n", FLine, newlibpos);
317 fprintf(newlib, "%s\n", FLine);
318 if(EQ(FLine, "</FILE>"))
320 fprintf(newlib, "\n");
321 fprintf(libindex, "</MODULE>\n\n");
325 else if(EQ(FLine, "<REL>")) inrel=1;
326 else if(EQ(FLine, "</REL>")) inrel=0;
331 sscanf(FLine, "S %s %c", symname, &c);
332 if(c=='D') fprintf(libindex, "%s\n", symname);
339 if(action==OPT_ADD_REL)
341 newlibpos=ftell(newlib);
342 fprintf(newlib, "<FILE>\n%s\n<REL>\n", ModName);
343 fprintf(libindex, "<MODULE>\n%s %ld\n", ModName, newlibpos);
347 fgets(FLine, MAXLINE, rel);
351 fprintf(newlib, "%s\n", FLine);
355 sscanf(FLine, "S %s %c", symname, &c);
356 if(c=='D') fprintf(libindex, "%s\n", symname);
360 fprintf(libindex, "</MODULE>\n");
361 fprintf(newlib, "</REL>\n<ADB>\n");
363 adb=fopen(AdbName, "r");
369 fgets(FLine, MAXLINE, adb);
373 fprintf(newlib, "%s\n", FLine);
378 fprintf(newlib, "</ADB>\n</FILE>\n");
381 /*Put the temporary files together as a new library file*/
382 indexsize=ftell(libindex);
386 if(lib!=NULL) fclose(lib);
389 newlib=fopen(LibNameTmp, "r");
390 lib=fopen(LibName, "w");
391 libindex=fopen(IndexName, "r");
393 fprintf(lib, "<SDCCLIB>\n\n<INDEX>\n");
395 /*Find out if the \n is expanded to \r\n or not*/
396 if(ftell(lib)!=(long)strlen("<SDCCLIB>\n\n<INDEX>\n"))
401 indexsize+=ftell(lib)+strlen("0123456789\n\n</INDEX>\n\n");
402 if(IsDOSStyle) indexsize+=4;
404 fprintf(lib, "%10ld\n", indexsize);
406 while(!feof(libindex))
409 fgets(FLine, MAXLINE, libindex);
410 fprintf(lib, "%s", FLine);
412 fprintf(lib, "\n</INDEX>\n\n");
417 fgets(FLine, MAXLINE, newlib);
418 fprintf(lib, "%s", FLine);
420 fprintf(lib, "\n</FILES>\n\n");
421 fprintf(lib, "</SDCCLIB>\n");
431 void ExtractRel(char * RelName)
435 strcpy(AdbName, RelName);
436 ChangeExtension(AdbName, "adb");
438 lib=fopen(LibName, "r");
441 printf("ERROR: Couldn't open file '%s'\n", LibName);
445 rel=fopen(RelName, "w");
448 printf("ERROR: Couldn't create file '%s'\n", RelName);
452 GetNameFromPath(RelName, ModName);
454 adb=fopen(AdbName, "w");
457 printf("ERROR: Couldn't create file '%s'\n", AdbName);
467 fgets(FLine, MAXLINE, lib);
473 if(EQ(FLine, "<FILE>"))
476 fgets(FLine, MAXLINE, lib);
478 if(EQ(FLine, ModName)) state=1;
482 if(EQ(FLine, "<REL>")) state=2;
485 if(EQ(FLine, "</REL>"))
488 fprintf(rel, "%s\n", FLine);
491 if(EQ(FLine, "<ADB>")) state=4;
494 if(EQ(FLine, "</ADB>"))
497 fprintf(adb, "%s\n", FLine);
507 void DumpSymbols(void)
511 lib=fopen(LibName, "r");
514 printf("ERROR: Couldn't open file '%s'\n", LibName);
519 fgets(FLine, MAXLINE, lib);
521 if(NEQ(FLine, "<SDCCLIB>"))
523 printf("ERROR: File '%s' was not created with '%s'\n", LibName, ProgName);
531 fgets(FLine, MAXLINE, lib);
537 if(EQ(FLine, "<INDEX>")) state=1;
540 if(EQ(FLine, "<MODULE>"))
543 fgets(FLine, MAXLINE, lib);
544 sscanf(FLine, "%s", ModName);
545 if(action==OPT_DUMP_SYM)
547 printf("%s.rel:\n", ModName);
552 printf("%s.rel\n", ModName);
555 else if(EQ(FLine, "</INDEX>")) state=3;
558 if(EQ(FLine, "</MODULE>"))
563 else printf(" %s\n", FLine);
575 int fileexist(char * fname)
579 fp=fopen(fname, "r");
580 if(fp==NULL) return 0;
591 char SrcName[PATH_MAX];
592 char RelName[PATH_MAX];
594 list=fopen(ListName, "r");
597 printf("ERROR: Couldn't open list file '%s'\n", ListName);
601 cc = getenv("SDCCLIB_CC");
602 as = getenv("SDCCLIB_AS");
608 fgets(RelName, PATH_MAX, list);
610 if(strlen(RelName)>0) //Skip empty lines
612 if((cc!=NULL)||(as!=NULL))
614 strcpy(SrcName, RelName);
615 if(strchr(SrcName,'.')==NULL)
616 strcat(SrcName,".src");
621 ChangeExtension(SrcName, "c");
622 if(fileexist(SrcName))
624 sprintf(CmdLine, "%s %s", cc, SrcName);
625 printf("%s\n", CmdLine);
631 ChangeExtension(SrcName, "asm");
632 if(fileexist(SrcName))
634 sprintf(CmdLine, "%s %s", as, SrcName);
635 printf("%s\n", CmdLine);
640 if(strchr(RelName,'.')==NULL)
642 //Try adding the default sdcc extensions
643 strcat(RelName,".o");
644 if(!fileexist(RelName))
645 ChangeExtension(RelName, "rel");
648 printf("Adding: %s\n", RelName);
656 int main(int argc, char **argv)
659 ProcLineOptions (argc, argv);
667 for(j=0; j<NumRelFiles; j++) AddRel(RelName[j]);
669 for(j=0; j<NumRelFiles; j++) free(RelName[j]);
678 for(j=0; j<NumRelFiles; j++) ExtractRel(RelName[j]);
680 for(j=0; j<NumRelFiles; j++) free(RelName[j]);
689 return 0; //Success!!!