* support/regression/tests/arithcsi.c: added regression test
[fw/sdcc] / support / librarian / sdcclib.c
index 7598431fa4f4b52f35d18dec83af208fb480fdc2..0ac4251c2eb4cbff0ef3eb695a88640f079c339f 100644 (file)
@@ -15,11 +15,11 @@ You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
 
+#define _POSIX_
+#include <limits.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#define _POSIX_
-#include <limits.h>
 #if !defined(__BORLANDC__) && !defined(_MSC_VER)
 #include <unistd.h>
 #endif
@@ -28,16 +28,20 @@ char ProgName[PATH_MAX];
 char LibName[PATH_MAX];
 char LibNameTmp[PATH_MAX];
 char IndexName[PATH_MAX];
-char RelName[PATH_MAX];
 char AdbName[PATH_MAX];
+char ListName[PATH_MAX];
+
+char **RelName;
+int NumRelFiles=0;
 
-#define version "0.1"
+#define version "1.1"
 
 #define OPT_ADD_REL 0
 #define OPT_EXT_REL 1
 #define OPT_DEL_REL 2
-#define OPT_DUMP_SYM 3
-#define OPT_DUMP_MOD 4
+#define OPT_ADD_LIST 3
+#define OPT_DUMP_SYM 4
+#define OPT_DUMP_MOD 5
 
 #define MAXLINE 254
 #define EQ(A,B) !strcmp((A),(B))
@@ -47,7 +51,6 @@ int action=0;
 FILE *lib, *newlib, *rel, *adb, *libindex;
 char FLine[MAXLINE+1];
 char ModName[MAXLINE+1];
-int state=0;
 
 void GetNameFromPath(char * path, char * name)
 {
@@ -91,11 +94,12 @@ int set_options (char * opt)
 {
        int rvalue=0, unknown=0;
        static char Help[] =
-       "Usage: %s [-options] library relfile\n\n"
+       "Usage: %s [-options] library relfile1 relfile2 relfile3 ...\n\n"
        "available options:\n"
-       "a - Adds relfile to library.  If relfile exists, replaces it.\n"
-       "d - Deletes relfile from library.\n"
-       "e - Extracts relfile from library.\n"
+       "a - Adds relfile(s) to library.  If relfile exists, replaces it.\n"
+       "l - Adds relfile list to library.\n"
+       "d - Deletes relfile(s) from library.\n"
+       "e - Extracts relfile(s) from library.\n"
        "s - Dumps symbols of library.\n"
        "m - Dumps modules of library.\n"
        "v - Displays program version.\n"
@@ -118,6 +122,9 @@ int set_options (char * opt)
                case 'a':
                    action=OPT_ADD_REL;
                break;
+               case 'l':
+                   action=OPT_ADD_LIST;
+               break;
                case 'e':
                    action=OPT_EXT_REL;
                break;
@@ -160,31 +167,64 @@ void ProcLineOptions (int argc, char **argv)
 
                                case 1:
                                        cont_par++;
-                                       strcpy(RelName, argv[j]);
+                    if(action==OPT_ADD_LIST)
+                        strcpy(ListName, argv[j]);
+                    else
+                                       {
+                                               NumRelFiles=1;
+                                               RelName = (char **) calloc (1, sizeof (char *));
+                                               if(RelName==NULL)
+                                               {
+                                                       printf("ERROR: Insuficient memory.\n");
+                                                       exit(2);
+                                               }
+                                               RelName[0]=(char *)malloc(PATH_MAX);
+                                               if(RelName[0]==NULL)
+                                               {
+                                                       printf("ERROR: Insuficient memory.\n");
+                                                       exit(2);
+                                               }
+                                           strcpy(RelName[0], argv[j]);
+                                       }
                                break;
 
                                default:
                                        cont_par++;
+                                       NumRelFiles++;
+                                       RelName = (char **) realloc (RelName, NumRelFiles * sizeof (char *));
+                                       if(RelName==NULL)
+                                       {
+                                               printf("ERROR: Insuficient memory.\n");
+                                               exit(2);
+                                       }
+                                       RelName[NumRelFiles-1]=(char *)malloc(PATH_MAX);
+                                       if(RelName[NumRelFiles-1]==NULL)
+                                       {
+                                               printf("ERROR: Insuficient memory.\n");
+                                               exit(2);
+                                       }
+                                   strcpy(RelName[NumRelFiles-1], argv[j]);
                                break;
                        }
                }
        }
 
-       if ( (cont_par!=2) && (action<OPT_DUMP_SYM) )
+       if ( (cont_par<2) && (action<OPT_DUMP_SYM) )
        {
-               printf("Error: Too %s arguments.\n", cont_par<2?"few":"many");
+               printf("ERROR: Too few arguments.\n");
                set_options("h"); /*Show help and exit*/
        }
        else if ( (cont_par!=1) && (action>=OPT_DUMP_SYM) )
        {
-               printf("Error: Too %s arguments.\n", cont_par<1?"few":"many");
+               printf("ERROR: Too %s arguments.\n", cont_par<1?"few":"many");
                set_options("h"); /*Show help and exit*/
        }
 }
 
-void AddRel(void)
+void AddRel(char * RelName)
 {
     int inrel=0;
+       int state=0;
     long newlibpos, indexsize;
     char symname[MAXLINE+1];
     char c;
@@ -206,7 +246,7 @@ void AddRel(void)
         rel=fopen(RelName, "r");
         if(rel==NULL)
         {
-            printf("ERROR: Couldn't open file '%s'", RelName);
+            printf("ERROR: Couldn't open file '%s'\n", RelName);
             if(lib!=NULL) fclose(lib);
             return;
         }
@@ -216,7 +256,7 @@ void AddRel(void)
     newlib=fopen(LibNameTmp, "w");
     if(newlib==NULL)
     {
-        printf("ERROR: Couldn't create temporary file '%s'", LibNameTmp);
+        printf("ERROR: Couldn't create temporary file '%s'\n", LibNameTmp);
         if(lib!=NULL) fclose(lib);
         fclose(rel);
         return;
@@ -226,7 +266,7 @@ void AddRel(void)
     libindex=fopen(IndexName, "w");
     if(libindex==NULL)
     {
-        printf("ERROR: Couldn't create temporary file '%s'", IndexName);
+        printf("ERROR: Couldn't create temporary file '%s'\n", IndexName);
         if(lib!=NULL) fclose(lib);
         fclose(newlib);
         fclose(rel);
@@ -367,26 +407,28 @@ void AddRel(void)
     fclose(lib);
     fclose(libindex);
 
-    unlink(LibNameTmp);
-    unlink(IndexName);
+    remove(LibNameTmp);
+    remove(IndexName);
 }
 
-void ExtractRel(void)
+void ExtractRel(char * RelName)
 {
+       int state=0;
+
     strcpy(AdbName, RelName);
     ChangeExtension(AdbName, "adb");
 
     lib=fopen(LibName, "r");
     if(lib==NULL)
     {
-        printf("ERROR: Couldn't open file '%s'", LibName);
+        printf("ERROR: Couldn't open file '%s'\n", LibName);
         return;
     }
 
     rel=fopen(RelName, "w");
     if(rel==NULL)
     {
-        printf("ERROR: Couldn't create file '%s'", RelName);
+        printf("ERROR: Couldn't create file '%s'\n", RelName);
         fclose(lib);
         return;
     }
@@ -395,7 +437,7 @@ void ExtractRel(void)
     adb=fopen(AdbName, "w");
     if(adb==NULL)
     {
-        printf("ERROR: Couldn't create file '%s'", AdbName);
+        printf("ERROR: Couldn't create file '%s'\n", AdbName);
         fclose(lib);
         fclose(rel);
         return;
@@ -447,13 +489,24 @@ void ExtractRel(void)
 
 void DumpSymbols(void)
 {
+       int state=0;
+
     lib=fopen(LibName, "r");
     if(lib==NULL)
     {
-        printf("ERROR: Couldn't open file '%s'", LibName);
+        printf("ERROR: Couldn't open file '%s'\n", LibName);
         return;
     }
 
+    FLine[0]=0;
+    fgets(FLine, MAXLINE, lib);
+    CleanLine(FLine);
+    if(NEQ(FLine, "<SDCCLIB>"))
+    {
+        printf("ERROR: File '%s' was not created with '%s'\n", LibName, ProgName); 
+        return;
+    }
+    
     while(!feof(lib))
     {
         if(state==3) break;
@@ -502,8 +555,90 @@ void DumpSymbols(void)
     fclose(lib);
 }
 
+int fileexist(char * fname)
+{
+    FILE * fp;
+    
+    fp=fopen(fname, "r");
+    if(fp==NULL) return 0;
+    fclose(fp);
+    return 1;
+}
+
+void AddList(void)
+{
+    FILE * list;
+    char *cc;
+    char *as;
+    char CmdLine[1024];
+    char SrcName[PATH_MAX];
+       char RelName[PATH_MAX];
+
+    list=fopen(ListName, "r");
+    if(list==NULL)
+    {
+        printf("ERROR: Couldn't open list file '%s'\n", ListName);
+        return;
+    }
+
+    cc = getenv("SDCCLIB_CC");
+    as = getenv("SDCCLIB_AS");
+
+    action=OPT_ADD_REL;
+    while(!feof(list))
+    {
+        RelName[0]=0;
+        fgets(RelName, PATH_MAX, list);
+        CleanLine(RelName);
+        if(strlen(RelName)>0) //Skip empty lines
+        {
+            if((cc!=NULL)||(as!=NULL))
+            {
+                strcpy(SrcName, RelName);
+                if(strchr(SrcName,'.')==NULL)
+                    strcat(SrcName,".src");
+            }
+
+            if(cc!=NULL)
+            {
+                ChangeExtension(SrcName, "c");
+                if(fileexist(SrcName))
+                {
+                    sprintf(CmdLine, "%s %s", cc, SrcName);
+                    printf("%s\n", CmdLine);
+                    system(CmdLine);
+                }
+            }
+            else if(as!=NULL)
+            {
+                ChangeExtension(SrcName, "asm");
+                if(fileexist(SrcName))
+                {
+                    sprintf(CmdLine, "%s %s", as, SrcName);
+                    printf("%s\n", CmdLine);
+                    system(CmdLine);
+                }
+            }
+
+            if(strchr(RelName,'.')==NULL)
+            {
+                //Try adding the default sdcc extensions
+                strcat(RelName,".o");
+                if(!fileexist(RelName))
+                    ChangeExtension(RelName, "rel");
+            }
+
+            printf("Adding: %s\n", RelName);
+            AddRel(RelName);
+        }
+    }
+    action=OPT_ADD_LIST;
+    fclose(list);
+}
+
 int main(int argc, char **argv)
 {
+       int j;
        ProcLineOptions (argc, argv);
 
        switch(action)
@@ -512,11 +647,21 @@ int main(int argc, char **argv)
                action=OPT_ADD_REL;
                case OPT_ADD_REL:
                case OPT_DEL_REL:
-                   AddRel();
+                       for(j=0; j<NumRelFiles; j++) AddRel(RelName[j]);
+                       //Clean up
+                       for(j=0; j<NumRelFiles; j++) free(RelName[j]);
+                       free(RelName);
                break;
                
+        case OPT_ADD_LIST:
+            AddList();
+        break;
+
                case OPT_EXT_REL:
-            ExtractRel();
+            for(j=0; j<NumRelFiles; j++) ExtractRel(RelName[j]);
+                       //Clean up
+                       for(j=0; j<NumRelFiles; j++) free(RelName[j]);
+                       free(RelName);
         break;
         
                case OPT_DUMP_SYM: