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,r - 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,x - 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);
146 void ProcLineOptions (int argc, char **argv)
151 /*Get the program name*/
152 GetNameFromPath(argv[0], ProgName);
154 for (j=1; j<argc; j++)
158 for(i=1; argv[j][i]!=0 ; i++)
159 if (set_options(&argv[j][i])) break;
167 strcpy(LibName, argv[j]);
172 if(action==OPT_ADD_LIST)
173 strcpy(ListName, argv[j]);
177 RelName = (char **) calloc (1, sizeof (char *));
180 printf("ERROR: Insuficient memory.\n");
183 RelName[0]=(char *)malloc(PATH_MAX);
186 printf("ERROR: Insuficient memory.\n");
189 strcpy(RelName[0], argv[j]);
196 RelName = (char **) realloc (RelName, NumRelFiles * sizeof (char *));
199 printf("ERROR: Insuficient memory.\n");
202 RelName[NumRelFiles-1]=(char *)malloc(PATH_MAX);
203 if(RelName[NumRelFiles-1]==NULL)
205 printf("ERROR: Insuficient memory.\n");
208 strcpy(RelName[NumRelFiles-1], argv[j]);
214 if ( (cont_par<2) && (action<OPT_DUMP_SYM) )
216 printf("ERROR: Too few arguments.\n");
217 set_options("h"); /*Show help and exit*/
219 else if ( (cont_par!=1) && (action>=OPT_DUMP_SYM) )
221 printf("ERROR: Too %s arguments.\n", cont_par<1?"few":"many");
222 set_options("h"); /*Show help and exit*/
226 void AddRel(char * RelName)
230 long newlibpos, indexsize;
231 char symname[MAXLINE+1];
235 strcpy(LibNameTmp, LibName);
236 ChangeExtension(LibNameTmp, "__L");
238 strcpy(IndexName, LibName);
239 ChangeExtension(IndexName, "__I");
241 strcpy(AdbName, RelName);
242 ChangeExtension(AdbName, "adb");
244 lib=fopen(LibName, "r");
246 if(action==OPT_ADD_REL)
248 rel=fopen(RelName, "r");
251 printf("ERROR: Couldn't open file '%s'\n", RelName);
252 if(lib!=NULL) fclose(lib);
256 GetNameFromPath(RelName, ModName);
258 newlib=fopen(LibNameTmp, "w");
261 printf("ERROR: Couldn't create temporary file '%s'\n", LibNameTmp);
262 if(lib!=NULL) fclose(lib);
266 fprintf(newlib, "<FILES>\n\n");
268 libindex=fopen(IndexName, "w");
271 printf("ERROR: Couldn't create temporary file '%s'\n", IndexName);
272 if(lib!=NULL) fclose(lib);
278 if(lib!=NULL) while(!feof(lib))
281 fgets(FLine, MAXLINE, lib);
287 if(EQ(FLine, "<FILE>"))
290 fgets(FLine, MAXLINE, lib);
292 if(NEQ(FLine, ModName))
294 newlibpos=ftell(newlib);
295 fprintf(newlib, "<FILE>\n%s\n", FLine);
296 fprintf(libindex, "<MODULE>\n%s %ld\n", FLine, newlibpos);
302 fprintf(newlib, "%s\n", FLine);
303 if(EQ(FLine, "</FILE>"))
305 fprintf(newlib, "\n");
306 fprintf(libindex, "</MODULE>\n\n");
310 else if(EQ(FLine, "<REL>")) inrel=1;
311 else if(EQ(FLine, "</REL>")) inrel=0;
316 sscanf(FLine, "S %s %c", symname, &c);
317 if(c=='D') fprintf(libindex, "%s\n", symname);
324 if(action==OPT_ADD_REL)
326 newlibpos=ftell(newlib);
327 fprintf(newlib, "<FILE>\n%s\n<REL>\n", ModName);
328 fprintf(libindex, "<MODULE>\n%s %ld\n", ModName, newlibpos);
332 fgets(FLine, MAXLINE, rel);
336 fprintf(newlib, "%s\n", FLine);
340 sscanf(FLine, "S %s %c", symname, &c);
341 if(c=='D') fprintf(libindex, "%s\n", symname);
345 fprintf(libindex, "</MODULE>\n");
346 fprintf(newlib, "</REL>\n<ADB>\n");
348 adb=fopen(AdbName, "r");
354 fgets(FLine, MAXLINE, adb);
358 fprintf(newlib, "%s\n", FLine);
363 fprintf(newlib, "</ADB>\n</FILE>\n");
366 /*Put the temporary files together as a new library file*/
367 indexsize=ftell(libindex);
371 if(lib!=NULL) fclose(lib);
374 newlib=fopen(LibNameTmp, "r");
375 lib=fopen(LibName, "w");
376 libindex=fopen(IndexName, "r");
378 fprintf(lib, "<SDCCLIB>\n\n<INDEX>\n");
380 /*Find out if the \n is expanded to \r\n or not*/
381 if(ftell(lib)!=(long)strlen("<SDCCLIB>\n\n<INDEX>\n"))
386 indexsize+=ftell(lib)+strlen("0123456789\n\n</INDEX>\n\n");
387 if(IsDOSStyle) indexsize+=4;
389 fprintf(lib, "%10ld\n", indexsize);
391 while(!feof(libindex))
394 fgets(FLine, MAXLINE, libindex);
395 fprintf(lib, "%s", FLine);
397 fprintf(lib, "\n</INDEX>\n\n");
402 fgets(FLine, MAXLINE, newlib);
403 fprintf(lib, "%s", FLine);
405 fprintf(lib, "\n</FILES>\n\n");
406 fprintf(lib, "</SDCCLIB>\n");
416 void ExtractRel(char * RelName)
420 strcpy(AdbName, RelName);
421 ChangeExtension(AdbName, "adb");
423 lib=fopen(LibName, "r");
426 printf("ERROR: Couldn't open file '%s'\n", LibName);
430 rel=fopen(RelName, "w");
433 printf("ERROR: Couldn't create file '%s'\n", RelName);
437 GetNameFromPath(RelName, ModName);
439 adb=fopen(AdbName, "w");
442 printf("ERROR: Couldn't create file '%s'\n", AdbName);
452 fgets(FLine, MAXLINE, lib);
458 if(EQ(FLine, "<FILE>"))
461 fgets(FLine, MAXLINE, lib);
463 if(EQ(FLine, ModName)) state=1;
467 if(EQ(FLine, "<REL>")) state=2;
470 if(EQ(FLine, "</REL>"))
473 fprintf(rel, "%s\n", FLine);
476 if(EQ(FLine, "<ADB>")) state=4;
479 if(EQ(FLine, "</ADB>"))
482 fprintf(adb, "%s\n", FLine);
492 void DumpSymbols(void)
496 lib=fopen(LibName, "r");
499 printf("ERROR: Couldn't open file '%s'\n", LibName);
504 fgets(FLine, MAXLINE, lib);
506 if(NEQ(FLine, "<SDCCLIB>"))
508 printf("ERROR: File '%s' was not created with '%s'\n", LibName, ProgName);
516 fgets(FLine, MAXLINE, lib);
522 if(EQ(FLine, "<INDEX>")) state=1;
525 if(EQ(FLine, "<MODULE>"))
528 fgets(FLine, MAXLINE, lib);
529 sscanf(FLine, "%s", ModName);
530 if(action==OPT_DUMP_SYM)
532 printf("%s.rel:\n", ModName);
537 printf("%s.rel\n", ModName);
540 else if(EQ(FLine, "</INDEX>")) state=3;
543 if(EQ(FLine, "</MODULE>"))
548 else printf(" %s\n", FLine);
560 int fileexist(char * fname)
564 fp=fopen(fname, "r");
565 if(fp==NULL) return 0;
576 char SrcName[PATH_MAX];
577 char RelName[PATH_MAX];
579 list=fopen(ListName, "r");
582 printf("ERROR: Couldn't open list file '%s'\n", ListName);
586 cc = getenv("SDCCLIB_CC");
587 as = getenv("SDCCLIB_AS");
593 fgets(RelName, PATH_MAX, list);
595 if(strlen(RelName)>0) //Skip empty lines
597 if((cc!=NULL)||(as!=NULL))
599 strcpy(SrcName, RelName);
600 if(strchr(SrcName,'.')==NULL)
601 strcat(SrcName,".src");
606 ChangeExtension(SrcName, "c");
607 if(fileexist(SrcName))
609 sprintf(CmdLine, "%s %s", cc, SrcName);
610 printf("%s\n", CmdLine);
616 ChangeExtension(SrcName, "asm");
617 if(fileexist(SrcName))
619 sprintf(CmdLine, "%s %s", as, SrcName);
620 printf("%s\n", CmdLine);
625 if(strchr(RelName,'.')==NULL)
627 //Try adding the default sdcc extensions
628 strcat(RelName,".o");
629 if(!fileexist(RelName))
630 ChangeExtension(RelName, "rel");
633 printf("Adding: %s\n", RelName);
641 int main(int argc, char **argv)
644 ProcLineOptions (argc, argv);
652 for(j=0; j<NumRelFiles; j++) AddRel(RelName[j]);
654 for(j=0; j<NumRelFiles; j++) free(RelName[j]);
663 for(j=0; j<NumRelFiles; j++) ExtractRel(RelName[j]);
665 for(j=0; j<NumRelFiles; j++) free(RelName[j]);
674 return 0; //Success!!!