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 #define fgets(S, SIZE, STREAM) do { \
54 char *__res4567 = fgets(__s4567, SIZE, STREAM); \
55 assert(__s4567 == __res4567); \
58 #define system(CMD) do { \
59 int __res4568 = system(CMD); \
60 assert(-1 != __res4568); \
65 FILE *lib, *newlib, *rel, *adb, *libindex;
66 char FLine[MAXLINE+1];
67 char ModName[MAXLINE+1];
69 void GetNameFromPath(char * path, char * name)
73 for(i=0; path[i]!=0; i++);
74 for(; (path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--);
75 for(j=0, i++; (path[i]!='.')&&(path[i]!=0); i++, j++) name[j]=path[i];
79 void ChangeExtension(char * path, char * ext)
83 for(i=0; path[i]!=0; i++);
84 for(; (path[i]!='.')&&(path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--);
92 printf("ERROR: Filename '%s' must have an extension\n", path);
97 void CleanLine(char * buff)
103 if((buff[j]=='\n')||(buff[j]=='\r')) buff[j]=0;
107 int set_options (char * opt)
109 int rvalue=0, unknown=0;
111 "Usage: %s [option|-options] library relfile1 relfile2 relfile3 ...\n\n"
112 "available options:\n"
113 "a,r - Adds relfile(s) to library. If relfile exists, replaces it.\n"
114 " l - Adds relfile list to library.\n"
115 " d - Deletes relfile(s) from library.\n"
116 "e,x - Extracts relfile(s) from library.\n"
117 " s - Dumps symbols of library.\n"
118 " m - Dumps modules of library.\n"
119 " v - Displays program version.\n"
120 "h,? - This help.\n";
129 printf("%s: SDCC librarian version %s\n", ProgName, version);
130 printf("by Jesus Calvino-Fraga\n\n");
131 if (unknown) printf("Unknown option: %c\n", opt[0]);
132 if (opt[0]=='v') exit(0);
133 printf(Help, ProgName);
160 void ProcLineOptions (int argc, char **argv)
165 /*Get the program name*/
166 GetNameFromPath(argv[0], ProgName);
168 for (j=1; j<argc; j++)
172 for(i=1; argv[j][i]!=0 ; i++)
173 if (set_options(&argv[j][i])) break;
180 if ((strlen(argv[j])==1) && (action==OPT_NONE))
181 set_options(argv[j]);
185 strcpy(LibName, argv[j]);
191 if(action==OPT_ADD_LIST)
192 strcpy(ListName, argv[j]);
196 RelName = (char **) calloc (1, sizeof (char *));
199 printf("ERROR: Insuficient memory.\n");
202 RelName[0]=(char *)malloc(PATH_MAX);
205 printf("ERROR: Insuficient memory.\n");
208 strcpy(RelName[0], argv[j]);
215 RelName = (char **) realloc (RelName, NumRelFiles * sizeof (char *));
218 printf("ERROR: Insuficient memory.\n");
221 RelName[NumRelFiles-1]=(char *)malloc(PATH_MAX);
222 if(RelName[NumRelFiles-1]==NULL)
224 printf("ERROR: Insuficient memory.\n");
227 strcpy(RelName[NumRelFiles-1], argv[j]);
233 if ( (cont_par<2) && (action<OPT_DUMP_SYM) )
235 printf("ERROR: Too few arguments.\n");
236 set_options("h"); /*Show help and exit*/
238 else if ( (cont_par!=1) && (action>=OPT_DUMP_SYM) )
240 printf("ERROR: Too %s arguments.\n", cont_par<1?"few":"many");
241 set_options("h"); /*Show help and exit*/
245 void AddRel(char * RelName)
249 long newlibpos, indexsize;
250 char symname[MAXLINE+1];
254 strcpy(LibNameTmp, LibName);
255 ChangeExtension(LibNameTmp, "__L");
257 strcpy(IndexName, LibName);
258 ChangeExtension(IndexName, "__I");
260 strcpy(AdbName, RelName);
261 ChangeExtension(AdbName, "adb");
263 lib=fopen(LibName, "r");
265 if(action==OPT_ADD_REL)
267 rel=fopen(RelName, "r");
270 printf("ERROR: Couldn't open file '%s'\n", RelName);
271 if(lib!=NULL) fclose(lib);
275 GetNameFromPath(RelName, ModName);
277 newlib=fopen(LibNameTmp, "w");
280 printf("ERROR: Couldn't create temporary file '%s'\n", LibNameTmp);
281 if(lib!=NULL) fclose(lib);
285 fprintf(newlib, "<FILES>\n\n");
287 libindex=fopen(IndexName, "w");
290 printf("ERROR: Couldn't create temporary file '%s'\n", IndexName);
291 if(lib!=NULL) fclose(lib);
297 if(lib!=NULL) while(!feof(lib))
300 fgets(FLine, MAXLINE, lib);
306 if(EQ(FLine, "<FILE>"))
309 fgets(FLine, MAXLINE, lib);
311 if(NEQ(FLine, ModName))
313 newlibpos=ftell(newlib);
314 fprintf(newlib, "<FILE>\n%s\n", FLine);
315 fprintf(libindex, "<MODULE>\n%s %ld\n", FLine, newlibpos);
321 fprintf(newlib, "%s\n", FLine);
322 if(EQ(FLine, "</FILE>"))
324 fprintf(newlib, "\n");
325 fprintf(libindex, "</MODULE>\n\n");
329 else if(EQ(FLine, "<REL>")) inrel=1;
330 else if(EQ(FLine, "</REL>")) inrel=0;
335 sscanf(FLine, "S %s %c", symname, &c);
336 if(c=='D') fprintf(libindex, "%s\n", symname);
343 if(action==OPT_ADD_REL)
345 newlibpos=ftell(newlib);
346 fprintf(newlib, "<FILE>\n%s\n<REL>\n", ModName);
347 fprintf(libindex, "<MODULE>\n%s %ld\n", ModName, newlibpos);
351 fgets(FLine, MAXLINE, rel);
355 fprintf(newlib, "%s\n", FLine);
359 sscanf(FLine, "S %s %c", symname, &c);
360 if(c=='D') fprintf(libindex, "%s\n", symname);
364 fprintf(libindex, "</MODULE>\n");
365 fprintf(newlib, "</REL>\n<ADB>\n");
367 adb=fopen(AdbName, "r");
373 fgets(FLine, MAXLINE, adb);
377 fprintf(newlib, "%s\n", FLine);
382 fprintf(newlib, "</ADB>\n</FILE>\n");
385 /*Put the temporary files together as a new library file*/
386 indexsize=ftell(libindex);
390 if(lib!=NULL) fclose(lib);
393 newlib=fopen(LibNameTmp, "r");
394 lib=fopen(LibName, "w");
395 libindex=fopen(IndexName, "r");
397 fprintf(lib, "<SDCCLIB>\n\n<INDEX>\n");
399 /*Find out if the \n is expanded to \r\n or not*/
400 if(ftell(lib)!=(long)strlen("<SDCCLIB>\n\n<INDEX>\n"))
405 indexsize+=ftell(lib)+strlen("0123456789\n\n</INDEX>\n\n");
406 if(IsDOSStyle) indexsize+=4;
408 fprintf(lib, "%10ld\n", indexsize);
410 while(!feof(libindex))
413 fgets(FLine, MAXLINE, libindex);
414 fprintf(lib, "%s", FLine);
416 fprintf(lib, "\n</INDEX>\n\n");
421 fgets(FLine, MAXLINE, newlib);
422 fprintf(lib, "%s", FLine);
424 fprintf(lib, "\n</FILES>\n\n");
425 fprintf(lib, "</SDCCLIB>\n");
435 void ExtractRel(char * RelName)
439 strcpy(AdbName, RelName);
440 ChangeExtension(AdbName, "adb");
442 lib=fopen(LibName, "r");
445 printf("ERROR: Couldn't open file '%s'\n", LibName);
449 rel=fopen(RelName, "w");
452 printf("ERROR: Couldn't create file '%s'\n", RelName);
456 GetNameFromPath(RelName, ModName);
458 adb=fopen(AdbName, "w");
461 printf("ERROR: Couldn't create file '%s'\n", AdbName);
471 fgets(FLine, MAXLINE, lib);
477 if(EQ(FLine, "<FILE>"))
480 fgets(FLine, MAXLINE, lib);
482 if(EQ(FLine, ModName)) state=1;
486 if(EQ(FLine, "<REL>")) state=2;
489 if(EQ(FLine, "</REL>"))
492 fprintf(rel, "%s\n", FLine);
495 if(EQ(FLine, "<ADB>")) state=4;
498 if(EQ(FLine, "</ADB>"))
501 fprintf(adb, "%s\n", FLine);
511 void DumpSymbols(void)
515 lib=fopen(LibName, "r");
518 printf("ERROR: Couldn't open file '%s'\n", LibName);
523 fgets(FLine, MAXLINE, lib);
525 if(NEQ(FLine, "<SDCCLIB>"))
527 printf("ERROR: File '%s' was not created with '%s'\n", LibName, ProgName);
535 fgets(FLine, MAXLINE, lib);
541 if(EQ(FLine, "<INDEX>")) state=1;
544 if(EQ(FLine, "<MODULE>"))
547 fgets(FLine, MAXLINE, lib);
548 sscanf(FLine, "%s", ModName);
549 if(action==OPT_DUMP_SYM)
551 printf("%s.rel:\n", ModName);
556 printf("%s.rel\n", ModName);
559 else if(EQ(FLine, "</INDEX>")) state=3;
562 if(EQ(FLine, "</MODULE>"))
567 else printf(" %s\n", FLine);
579 int fileexist(char * fname)
583 fp=fopen(fname, "r");
584 if(fp==NULL) return 0;
595 char SrcName[PATH_MAX];
596 char RelName[PATH_MAX];
598 list=fopen(ListName, "r");
601 printf("ERROR: Couldn't open list file '%s'\n", ListName);
605 cc = getenv("SDCCLIB_CC");
606 as = getenv("SDCCLIB_AS");
612 fgets(RelName, PATH_MAX, list);
614 if(strlen(RelName)>0) //Skip empty lines
616 if((cc!=NULL)||(as!=NULL))
618 strcpy(SrcName, RelName);
619 if(strchr(SrcName,'.')==NULL)
620 strcat(SrcName,".src");
625 ChangeExtension(SrcName, "c");
626 if(fileexist(SrcName))
628 sprintf(CmdLine, "%s %s", cc, SrcName);
629 printf("%s\n", CmdLine);
635 ChangeExtension(SrcName, "asm");
636 if(fileexist(SrcName))
638 sprintf(CmdLine, "%s %s", as, SrcName);
639 printf("%s\n", CmdLine);
644 if(strchr(RelName,'.')==NULL)
646 //Try adding the default sdcc extensions
647 strcat(RelName,".o");
648 if(!fileexist(RelName))
649 ChangeExtension(RelName, "rel");
652 printf("Adding: %s\n", RelName);
660 int main(int argc, char **argv)
663 ProcLineOptions (argc, argv);
671 for(j=0; j<NumRelFiles; j++) AddRel(RelName[j]);
673 for(j=0; j<NumRelFiles; j++) free(RelName[j]);
682 for(j=0; j<NumRelFiles; j++) ExtractRel(RelName[j]);
684 for(j=0; j<NumRelFiles; j++) free(RelName[j]);
693 return 0; //Success!!!