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))
52 FILE *lib, *newlib, *rel, *adb, *libindex;
53 char FLine[MAXLINE+1];
54 char ModName[MAXLINE+1];
56 void GetNameFromPath(char * path, char * name)
60 for(i=0; path[i]!=0; i++);
61 for(; (path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--);
62 for(j=0, i++; (path[i]!='.')&&(path[i]!=0); i++, j++) name[j]=path[i];
66 void ChangeExtension(char * path, char * ext)
70 for(i=0; path[i]!=0; i++);
71 for(; (path[i]!='.')&&(path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--);
79 printf("ERROR: Filename '%s' must have an extension\n", path);
84 void CleanLine(char * buff)
90 if((buff[j]=='\n')||(buff[j]=='\r')) buff[j]=0;
94 int set_options (char * opt)
96 int rvalue=0, unknown=0;
98 "Usage: %s [option|-options] library relfile1 relfile2 relfile3 ...\n\n"
99 "available options:\n"
100 "a,r - Adds relfile(s) to library. If relfile exists, replaces it.\n"
101 " l - Adds relfile list to library.\n"
102 " d - Deletes relfile(s) from library.\n"
103 "e,x - Extracts relfile(s) from library.\n"
104 " s - Dumps symbols of library.\n"
105 " m - Dumps modules of library.\n"
106 " v - Displays program version.\n"
107 "h,? - This help.\n";
116 printf("%s: SDCC librarian version %s\n", ProgName, version);
117 printf("by Jesus Calvino-Fraga\n\n");
118 if (unknown) printf("Unknown option: %c\n", opt[0]);
119 if (opt[0]=='v') exit(0);
120 printf(Help, ProgName);
147 void ProcLineOptions (int argc, char **argv)
152 /*Get the program name*/
153 GetNameFromPath(argv[0], ProgName);
155 for (j=1; j<argc; j++)
159 for(i=1; argv[j][i]!=0 ; i++)
160 if (set_options(&argv[j][i])) break;
167 if ((strlen(argv[j])==1) && (action==OPT_NONE))
168 set_options(argv[j]);
172 strcpy(LibName, argv[j]);
178 if(action==OPT_ADD_LIST)
179 strcpy(ListName, argv[j]);
183 RelName = (char **) calloc (1, sizeof (char *));
186 printf("ERROR: Insuficient memory.\n");
189 RelName[0]=(char *)malloc(PATH_MAX);
192 printf("ERROR: Insuficient memory.\n");
195 strcpy(RelName[0], argv[j]);
202 RelName = (char **) realloc (RelName, NumRelFiles * sizeof (char *));
205 printf("ERROR: Insuficient memory.\n");
208 RelName[NumRelFiles-1]=(char *)malloc(PATH_MAX);
209 if(RelName[NumRelFiles-1]==NULL)
211 printf("ERROR: Insuficient memory.\n");
214 strcpy(RelName[NumRelFiles-1], argv[j]);
220 if ( (cont_par<2) && (action<OPT_DUMP_SYM) )
222 printf("ERROR: Too few arguments.\n");
223 set_options("h"); /*Show help and exit*/
225 else if ( (cont_par!=1) && (action>=OPT_DUMP_SYM) )
227 printf("ERROR: Too %s arguments.\n", cont_par<1?"few":"many");
228 set_options("h"); /*Show help and exit*/
232 void AddRel(char * RelName)
236 long newlibpos, indexsize;
237 char symname[MAXLINE+1];
241 strcpy(LibNameTmp, LibName);
242 ChangeExtension(LibNameTmp, "__L");
244 strcpy(IndexName, LibName);
245 ChangeExtension(IndexName, "__I");
247 strcpy(AdbName, RelName);
248 ChangeExtension(AdbName, "adb");
250 lib=fopen(LibName, "r");
252 if(action==OPT_ADD_REL)
254 rel=fopen(RelName, "r");
257 printf("ERROR: Couldn't open file '%s'\n", RelName);
258 if(lib!=NULL) fclose(lib);
262 GetNameFromPath(RelName, ModName);
264 newlib=fopen(LibNameTmp, "w");
267 printf("ERROR: Couldn't create temporary file '%s'\n", LibNameTmp);
268 if(lib!=NULL) fclose(lib);
272 fprintf(newlib, "<FILES>\n\n");
274 libindex=fopen(IndexName, "w");
277 printf("ERROR: Couldn't create temporary file '%s'\n", IndexName);
278 if(lib!=NULL) fclose(lib);
284 if(lib!=NULL) while(!feof(lib))
287 fgets(FLine, MAXLINE, lib);
293 if(EQ(FLine, "<FILE>"))
296 fgets(FLine, MAXLINE, lib);
298 if(NEQ(FLine, ModName))
300 newlibpos=ftell(newlib);
301 fprintf(newlib, "<FILE>\n%s\n", FLine);
302 fprintf(libindex, "<MODULE>\n%s %ld\n", FLine, newlibpos);
308 fprintf(newlib, "%s\n", FLine);
309 if(EQ(FLine, "</FILE>"))
311 fprintf(newlib, "\n");
312 fprintf(libindex, "</MODULE>\n\n");
316 else if(EQ(FLine, "<REL>")) inrel=1;
317 else if(EQ(FLine, "</REL>")) inrel=0;
322 sscanf(FLine, "S %s %c", symname, &c);
323 if(c=='D') fprintf(libindex, "%s\n", symname);
330 if(action==OPT_ADD_REL)
332 newlibpos=ftell(newlib);
333 fprintf(newlib, "<FILE>\n%s\n<REL>\n", ModName);
334 fprintf(libindex, "<MODULE>\n%s %ld\n", ModName, newlibpos);
338 fgets(FLine, MAXLINE, rel);
342 fprintf(newlib, "%s\n", FLine);
346 sscanf(FLine, "S %s %c", symname, &c);
347 if(c=='D') fprintf(libindex, "%s\n", symname);
351 fprintf(libindex, "</MODULE>\n");
352 fprintf(newlib, "</REL>\n<ADB>\n");
354 adb=fopen(AdbName, "r");
360 fgets(FLine, MAXLINE, adb);
364 fprintf(newlib, "%s\n", FLine);
369 fprintf(newlib, "</ADB>\n</FILE>\n");
372 /*Put the temporary files together as a new library file*/
373 indexsize=ftell(libindex);
377 if(lib!=NULL) fclose(lib);
380 newlib=fopen(LibNameTmp, "r");
381 lib=fopen(LibName, "w");
382 libindex=fopen(IndexName, "r");
384 fprintf(lib, "<SDCCLIB>\n\n<INDEX>\n");
386 /*Find out if the \n is expanded to \r\n or not*/
387 if(ftell(lib)!=(long)strlen("<SDCCLIB>\n\n<INDEX>\n"))
392 indexsize+=ftell(lib)+strlen("0123456789\n\n</INDEX>\n\n");
393 if(IsDOSStyle) indexsize+=4;
395 fprintf(lib, "%10ld\n", indexsize);
397 while(!feof(libindex))
400 fgets(FLine, MAXLINE, libindex);
401 fprintf(lib, "%s", FLine);
403 fprintf(lib, "\n</INDEX>\n\n");
408 fgets(FLine, MAXLINE, newlib);
409 fprintf(lib, "%s", FLine);
411 fprintf(lib, "\n</FILES>\n\n");
412 fprintf(lib, "</SDCCLIB>\n");
422 void ExtractRel(char * RelName)
426 strcpy(AdbName, RelName);
427 ChangeExtension(AdbName, "adb");
429 lib=fopen(LibName, "r");
432 printf("ERROR: Couldn't open file '%s'\n", LibName);
436 rel=fopen(RelName, "w");
439 printf("ERROR: Couldn't create file '%s'\n", RelName);
443 GetNameFromPath(RelName, ModName);
445 adb=fopen(AdbName, "w");
448 printf("ERROR: Couldn't create file '%s'\n", AdbName);
458 fgets(FLine, MAXLINE, lib);
464 if(EQ(FLine, "<FILE>"))
467 fgets(FLine, MAXLINE, lib);
469 if(EQ(FLine, ModName)) state=1;
473 if(EQ(FLine, "<REL>")) state=2;
476 if(EQ(FLine, "</REL>"))
479 fprintf(rel, "%s\n", FLine);
482 if(EQ(FLine, "<ADB>")) state=4;
485 if(EQ(FLine, "</ADB>"))
488 fprintf(adb, "%s\n", FLine);
498 void DumpSymbols(void)
502 lib=fopen(LibName, "r");
505 printf("ERROR: Couldn't open file '%s'\n", LibName);
510 fgets(FLine, MAXLINE, lib);
512 if(NEQ(FLine, "<SDCCLIB>"))
514 printf("ERROR: File '%s' was not created with '%s'\n", LibName, ProgName);
522 fgets(FLine, MAXLINE, lib);
528 if(EQ(FLine, "<INDEX>")) state=1;
531 if(EQ(FLine, "<MODULE>"))
534 fgets(FLine, MAXLINE, lib);
535 sscanf(FLine, "%s", ModName);
536 if(action==OPT_DUMP_SYM)
538 printf("%s.rel:\n", ModName);
543 printf("%s.rel\n", ModName);
546 else if(EQ(FLine, "</INDEX>")) state=3;
549 if(EQ(FLine, "</MODULE>"))
554 else printf(" %s\n", FLine);
566 int fileexist(char * fname)
570 fp=fopen(fname, "r");
571 if(fp==NULL) return 0;
582 char SrcName[PATH_MAX];
583 char RelName[PATH_MAX];
585 list=fopen(ListName, "r");
588 printf("ERROR: Couldn't open list file '%s'\n", ListName);
592 cc = getenv("SDCCLIB_CC");
593 as = getenv("SDCCLIB_AS");
599 fgets(RelName, PATH_MAX, list);
601 if(strlen(RelName)>0) //Skip empty lines
603 if((cc!=NULL)||(as!=NULL))
605 strcpy(SrcName, RelName);
606 if(strchr(SrcName,'.')==NULL)
607 strcat(SrcName,".src");
612 ChangeExtension(SrcName, "c");
613 if(fileexist(SrcName))
615 sprintf(CmdLine, "%s %s", cc, SrcName);
616 printf("%s\n", CmdLine);
622 ChangeExtension(SrcName, "asm");
623 if(fileexist(SrcName))
625 sprintf(CmdLine, "%s %s", as, SrcName);
626 printf("%s\n", CmdLine);
631 if(strchr(RelName,'.')==NULL)
633 //Try adding the default sdcc extensions
634 strcat(RelName,".o");
635 if(!fileexist(RelName))
636 ChangeExtension(RelName, "rel");
639 printf("Adding: %s\n", RelName);
647 int main(int argc, char **argv)
650 ProcLineOptions (argc, argv);
658 for(j=0; j<NumRelFiles; j++) AddRel(RelName[j]);
660 for(j=0; j<NumRelFiles; j++) free(RelName[j]);
669 for(j=0; j<NumRelFiles; j++) ExtractRel(RelName[j]);
671 for(j=0; j<NumRelFiles; j++) free(RelName[j]);
680 return 0; //Success!!!