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];
42 #define OPT_ADD_LIST 3
43 #define OPT_DUMP_SYM 4
44 #define OPT_DUMP_MOD 5
47 #define EQ(A,B) !strcmp((A),(B))
48 #define NEQ(A,B) strcmp((A),(B))
51 FILE *lib, *newlib, *rel, *adb, *libindex;
52 char FLine[MAXLINE+1];
53 char ModName[MAXLINE+1];
55 void GetNameFromPath(char * path, char * name)
59 for(i=0; path[i]!=0; i++);
60 for(; (path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--);
61 for(j=0, i++; (path[i]!='.')&&(path[i]!=0); i++, j++) name[j]=path[i];
65 void ChangeExtension(char * path, char * ext)
69 for(i=0; path[i]!=0; i++);
70 for(; (path[i]!='.')&&(path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--);
78 printf("ERROR: Filename '%s' must have an extension\n", path);
83 void CleanLine(char * buff)
89 if((buff[j]=='\n')||(buff[j]=='\r')) buff[j]=0;
93 int set_options (char * opt)
95 int rvalue=0, unknown=0;
97 "Usage: %s [-options] library relfile1 relfile2 relfile3 ...\n\n"
98 "available options:\n"
99 "a - Adds relfile(s) to library. If relfile exists, replaces it.\n"
100 "l - Adds relfile list to library.\n"
101 "d - Deletes relfile(s) from library.\n"
102 "e - Extracts relfile(s) from library.\n"
103 "s - Dumps symbols of library.\n"
104 "m - Dumps modules of library.\n"
105 "v - Displays program version.\n"
115 printf("%s: SDCC librarian version %s\n", ProgName, version);
116 printf("by Jesus Calvino-Fraga\n\n");
117 if (unknown) printf("Unknown option: %c\n", opt[0]);
118 if (opt[0]=='v') exit(0);
119 printf(Help, ProgName);
144 void ProcLineOptions (int argc, char **argv)
149 /*Get the program name*/
150 GetNameFromPath(argv[0], ProgName);
152 for (j=1; j<argc; j++)
156 for(i=1; argv[j][i]!=0 ; i++)
157 if (set_options(&argv[j][i])) break;
165 strcpy(LibName, argv[j]);
170 if(action==OPT_ADD_LIST)
171 strcpy(ListName, argv[j]);
175 RelName = (char **) calloc (1, sizeof (char *));
178 printf("ERROR: Insuficient memory.\n");
181 RelName[0]=(char *)malloc(PATH_MAX);
184 printf("ERROR: Insuficient memory.\n");
187 strcpy(RelName[0], argv[j]);
194 RelName = (char **) realloc (RelName, NumRelFiles * sizeof (char *));
197 printf("ERROR: Insuficient memory.\n");
200 RelName[NumRelFiles-1]=(char *)malloc(PATH_MAX);
201 if(RelName[NumRelFiles-1]==NULL)
203 printf("ERROR: Insuficient memory.\n");
206 strcpy(RelName[NumRelFiles-1], argv[j]);
212 if ( (cont_par<2) && (action<OPT_DUMP_SYM) )
214 printf("ERROR: Too few arguments.\n");
215 set_options("h"); /*Show help and exit*/
217 else if ( (cont_par!=1) && (action>=OPT_DUMP_SYM) )
219 printf("ERROR: Too %s arguments.\n", cont_par<1?"few":"many");
220 set_options("h"); /*Show help and exit*/
224 void AddRel(char * RelName)
228 long newlibpos, indexsize;
229 char symname[MAXLINE+1];
233 strcpy(LibNameTmp, LibName);
234 ChangeExtension(LibNameTmp, "__L");
236 strcpy(IndexName, LibName);
237 ChangeExtension(IndexName, "__I");
239 strcpy(AdbName, RelName);
240 ChangeExtension(AdbName, "adb");
242 lib=fopen(LibName, "r");
244 if(action==OPT_ADD_REL)
246 rel=fopen(RelName, "r");
249 printf("ERROR: Couldn't open file '%s'\n", RelName);
250 if(lib!=NULL) fclose(lib);
254 GetNameFromPath(RelName, ModName);
256 newlib=fopen(LibNameTmp, "w");
259 printf("ERROR: Couldn't create temporary file '%s'\n", LibNameTmp);
260 if(lib!=NULL) fclose(lib);
264 fprintf(newlib, "<FILES>\n\n");
266 libindex=fopen(IndexName, "w");
269 printf("ERROR: Couldn't create temporary file '%s'\n", IndexName);
270 if(lib!=NULL) fclose(lib);
276 if(lib!=NULL) while(!feof(lib))
279 fgets(FLine, MAXLINE, lib);
285 if(EQ(FLine, "<FILE>"))
288 fgets(FLine, MAXLINE, lib);
290 if(NEQ(FLine, ModName))
292 newlibpos=ftell(newlib);
293 fprintf(newlib, "<FILE>\n%s\n", FLine);
294 fprintf(libindex, "<MODULE>\n%s %ld\n", FLine, newlibpos);
300 fprintf(newlib, "%s\n", FLine);
301 if(EQ(FLine, "</FILE>"))
303 fprintf(newlib, "\n");
304 fprintf(libindex, "</MODULE>\n\n");
308 else if(EQ(FLine, "<REL>")) inrel=1;
309 else if(EQ(FLine, "</REL>")) inrel=0;
314 sscanf(FLine, "S %s %c", symname, &c);
315 if(c=='D') fprintf(libindex, "%s\n", symname);
322 if(action==OPT_ADD_REL)
324 newlibpos=ftell(newlib);
325 fprintf(newlib, "<FILE>\n%s\n<REL>\n", ModName);
326 fprintf(libindex, "<MODULE>\n%s %ld\n", ModName, newlibpos);
330 fgets(FLine, MAXLINE, rel);
334 fprintf(newlib, "%s\n", FLine);
338 sscanf(FLine, "S %s %c", symname, &c);
339 if(c=='D') fprintf(libindex, "%s\n", symname);
343 fprintf(libindex, "</MODULE>\n");
344 fprintf(newlib, "</REL>\n<ADB>\n");
346 adb=fopen(AdbName, "r");
352 fgets(FLine, MAXLINE, adb);
356 fprintf(newlib, "%s\n", FLine);
361 fprintf(newlib, "</ADB>\n</FILE>\n");
364 /*Put the temporary files together as a new library file*/
365 indexsize=ftell(libindex);
369 if(lib!=NULL) fclose(lib);
372 newlib=fopen(LibNameTmp, "r");
373 lib=fopen(LibName, "w");
374 libindex=fopen(IndexName, "r");
376 fprintf(lib, "<SDCCLIB>\n\n<INDEX>\n");
378 /*Find out if the \n is expanded to \r\n or not*/
379 if(ftell(lib)!=(long)strlen("<SDCCLIB>\n\n<INDEX>\n"))
384 indexsize+=ftell(lib)+strlen("0123456789\n\n</INDEX>\n\n");
385 if(IsDOSStyle) indexsize+=4;
387 fprintf(lib, "%10ld\n", indexsize);
389 while(!feof(libindex))
392 fgets(FLine, MAXLINE, libindex);
393 fprintf(lib, "%s", FLine);
395 fprintf(lib, "\n</INDEX>\n\n");
400 fgets(FLine, MAXLINE, newlib);
401 fprintf(lib, "%s", FLine);
403 fprintf(lib, "\n</FILES>\n\n");
404 fprintf(lib, "</SDCCLIB>\n");
414 void ExtractRel(char * RelName)
418 strcpy(AdbName, RelName);
419 ChangeExtension(AdbName, "adb");
421 lib=fopen(LibName, "r");
424 printf("ERROR: Couldn't open file '%s'\n", LibName);
428 rel=fopen(RelName, "w");
431 printf("ERROR: Couldn't create file '%s'\n", RelName);
435 GetNameFromPath(RelName, ModName);
437 adb=fopen(AdbName, "w");
440 printf("ERROR: Couldn't create file '%s'\n", AdbName);
450 fgets(FLine, MAXLINE, lib);
456 if(EQ(FLine, "<FILE>"))
459 fgets(FLine, MAXLINE, lib);
461 if(EQ(FLine, ModName)) state=1;
465 if(EQ(FLine, "<REL>")) state=2;
468 if(EQ(FLine, "</REL>"))
471 fprintf(rel, "%s\n", FLine);
474 if(EQ(FLine, "<ADB>")) state=4;
477 if(EQ(FLine, "</ADB>"))
480 fprintf(adb, "%s\n", FLine);
490 void DumpSymbols(void)
494 lib=fopen(LibName, "r");
497 printf("ERROR: Couldn't open file '%s'\n", LibName);
502 fgets(FLine, MAXLINE, lib);
504 if(NEQ(FLine, "<SDCCLIB>"))
506 printf("ERROR: File '%s' was not created with '%s'\n", LibName, ProgName);
514 fgets(FLine, MAXLINE, lib);
520 if(EQ(FLine, "<INDEX>")) state=1;
523 if(EQ(FLine, "<MODULE>"))
526 fgets(FLine, MAXLINE, lib);
527 sscanf(FLine, "%s", ModName);
528 if(action==OPT_DUMP_SYM)
530 printf("%s.rel:\n", ModName);
535 printf("%s.rel\n", ModName);
538 else if(EQ(FLine, "</INDEX>")) state=3;
541 if(EQ(FLine, "</MODULE>"))
546 else printf(" %s\n", FLine);
558 int fileexist(char * fname)
562 fp=fopen(fname, "r");
563 if(fp==NULL) return 0;
574 char SrcName[PATH_MAX];
575 char RelName[PATH_MAX];
577 list=fopen(ListName, "r");
580 printf("ERROR: Couldn't open list file '%s'\n", ListName);
584 cc = getenv("SDCCLIB_CC");
585 as = getenv("SDCCLIB_AS");
591 fgets(RelName, PATH_MAX, list);
593 if(strlen(RelName)>0) //Skip empty lines
595 if((cc!=NULL)||(as!=NULL))
597 strcpy(SrcName, RelName);
598 if(strchr(SrcName,'.')==NULL)
599 strcat(SrcName,".src");
604 ChangeExtension(SrcName, "c");
605 if(fileexist(SrcName))
607 sprintf(CmdLine, "%s %s", cc, SrcName);
608 printf("%s\n", CmdLine);
614 ChangeExtension(SrcName, "asm");
615 if(fileexist(SrcName))
617 sprintf(CmdLine, "%s %s", as, SrcName);
618 printf("%s\n", CmdLine);
623 if(strchr(RelName,'.')==NULL)
625 //Try adding the default sdcc extensions
626 strcat(RelName,".o");
627 if(!fileexist(RelName))
628 ChangeExtension(RelName, "rel");
631 printf("Adding: %s\n", RelName);
639 int main(int argc, char **argv)
642 ProcLineOptions (argc, argv);
650 for(j=0; j<NumRelFiles; j++) AddRel(RelName[j]);
652 for(j=0; j<NumRelFiles; j++) free(RelName[j]);
661 for(j=0; j<NumRelFiles; j++) ExtractRel(RelName[j]);
663 for(j=0; j<NumRelFiles; j++) free(RelName[j]);
672 return 0; //Success!!!