New Memory Allocation functions
[fw/sdcc] / src / SDCCmain.c
index 299cdafe10b3a2d151438d41777721a2f9709e70..2d44d46607a1bb8eabeb11f9a582e2666a07f77b 100644 (file)
    what you give them.   Help stamp out software-hoarding!  
 -------------------------------------------------------------------------*/
 
+#define USE_SYSTEM_SYSTEM_CALLS
+
 #include "common.h"
 #include <ctype.h>
+#include "newalloc.h"
 
 #if NATIVE_WIN32
 #include <process.h>
@@ -69,6 +72,14 @@ int nrelFiles = 0;
 bool verboseExec = FALSE;
 char    *preOutName;
 
+// In MSC VC6 default search path for exe's to path for this
+
+#if defined(_MSC_VER)
+
+char DefaultExePath[_MAX_PATH] ;
+
+#endif
+
 /* Far functions, far data */
 #define OPTION_LARGE_MODEL "-model-large"
 /* Far functions, near data */
@@ -195,6 +206,60 @@ static void _validatePorts(void)
     }
 }
 
+#ifdef USE_SYSTEM_SYSTEM_CALLS
+void buildCmdLine(char *into, const char **cmds, 
+                         const char *p1, const char *p2, 
+                         const char *p3, const char **list)
+{
+    const char *p, *from;
+
+    *into = '\0';
+
+    while (*cmds) {
+
+       from = *cmds;
+       cmds++;
+
+       /* See if it has a '$' anywhere - if not, just copy */
+       if ((p = strchr(from, '$'))) {
+           strncat(into, from, p - from);
+           /* seperate it */
+           strcat(into, " ");
+           from = p+2;
+           p++;
+           switch (*p) {
+           case '1':
+               if (p1)
+                   strcat(into, p1);
+               break;
+           case '2':
+               if (p2)
+                   strcat(into, p2);
+               break;
+           case '3':
+               if (p3)
+                   strcat(into, p3);
+               break;
+           case 'l': {
+               const char **tmp = list;
+               if (tmp) {
+                   while (*tmp) {
+                       strcat(into, *tmp);
+                       strcat(into, " ");
+                       tmp++;
+                   }
+               }
+               break;
+           }
+           default:
+               assert(0);
+           }
+       }
+       strcat(into, from); // this includes the ".asm" from "$1.asm"
+       strcat(into, " ");
+    }
+}
+#else
 void buildCmdLine(char *into, char **args, const char **cmds, 
                          const char *p1, const char *p2, 
                          const char *p3, const char **list)
@@ -253,6 +318,7 @@ void buildCmdLine(char *into, char **args, const char **cmds,
     }
     *args = NULL;
 }
+#endif
 
 /*-----------------------------------------------------------------*/
 /* printVersionInfo - prints the version info                     */
@@ -417,7 +483,7 @@ static void processFile (char *s)
        
        /* get rid of the "." */
        strtok(buffer,".");
-       ALLOC(srcFileName,strlen(buffer)+1);
+       srcFileName = Safe_calloc(strlen(buffer)+1);
        strcpy(srcFileName,buffer);
 
        /* get rid of any path information 
@@ -430,7 +496,7 @@ static void processFile (char *s)
               *(fext-1) != '/'   &&
               *(fext-1) != ':')
            fext--;
-       ALLOC(moduleName,strlen(fext)+1);
+       moduleName = Safe_calloc(strlen(fext)+1);
        strcpy(moduleName,fext);
        
        return ;
@@ -1081,21 +1147,76 @@ int   parseCmdLine ( int argc, char **argv )
 
 #if defined(_MSC_VER)
 
-char *try_dir[]= {NULL};                       // TODO : Fill in some default search list
+char *try_dir[]= {DefaultExePath, NULL};                       // TODO : Fill in some default search list
 
 #else
 
-char *try_dir[]= {SRCDIR "/bin",PREFIX "/bin", NULL};
+//char *try_dir[]= {SRCDIR "/bin",PREFIX "/bin", NULL};
+char *try_dir[]= {NULL};
 
 #endif
 
+#ifdef USE_SYSTEM_SYSTEM_CALLS
+int my_system (const char *cmd)
+{    
+  int argsStart, e, i=0;
+  char *cmdLine=NULL;
+
+  argsStart=strstr(cmd, " ")-cmd;
+  
+  // try to find the command in predefined path's
+  while (try_dir[i]) {
+    cmdLine = (char*)malloc(strlen(try_dir[i])+strlen(cmd)+10);
+    strcpy(cmdLine, try_dir[i]); // the path
+    strcat(cmdLine, "/");
+    strncat(cmdLine, cmd, argsStart); // the command
+#if NATIVE_WIN32
+    strcat(cmdLine, ".exe");
+    /* Mung slashes into backslashes to keep WIndoze happy. */
+    {
+      char *r=cmdLine;
+      while (*r) {
+       if (*r == '/') {
+         *r = '\\';
+       }
+       r++;
+      }
+    }
+#endif
+    if (access(cmdLine, X_OK) == 0) {
+      // the arguments
+      strcat(cmdLine, cmd+argsStart);
+      break;
+    }
+    free(cmdLine);
+    cmdLine=NULL;
+    i++;
+  }
+  
+  if (verboseExec) {
+    printf ("+ %s\n", cmdLine ? cmdLine : cmd);
+  }
+  
+  if (cmdLine) {
+    // command found in predefined path
+    e=system(cmdLine);
+    free(cmdLine);
+  } else {
+    // trust on $PATH
+    e=system(cmd);
+  }
+  return e;
+}
+
+#else
+
 int my_system (const char *cmd, char **cmd_argv)
 {    
     char *dir, *got= NULL; int i= 0;
 
     while (!got && try_dir[i])
     {
-        dir= (char*)malloc(strlen(try_dir[i])+strlen(cmd)+10);
+        dir= (char*)Safe_malloc(strlen(try_dir[i])+strlen(cmd)+10);
         strcpy(dir, try_dir[i]);
         strcat(dir, "/");
         strcat(dir, cmd);
@@ -1151,6 +1272,7 @@ int my_system (const char *cmd, char **cmd_argv)
     
     return 0;
 }
+#endif
 
 /*-----------------------------------------------------------------*/
 /* linkEdit : - calls the linkage editor  with options             */
@@ -1158,7 +1280,9 @@ int my_system (const char *cmd, char **cmd_argv)
 static void linkEdit (char **envp)
 {
     FILE *lnkfile ;
+#ifndef USE_SYSTEM_SYSTEM_CALLS
     char *argv[128];
+#endif
     char *segName, *c;
 
     int i;
@@ -1259,15 +1383,23 @@ static void linkEdit (char **envp)
     fprintf (lnkfile,"\n-e\n");
     fclose(lnkfile);
 
-    buildCmdLine(buffer, argv, port->linker.cmd, srcFileName, NULL, NULL, NULL);
-
     if (options.verbose)
       printf ("sdcc: Calling linker...\n");
+
+#ifdef USE_SYSTEM_SYSTEM_CALLS
+    buildCmdLine(buffer, port->linker.cmd, srcFileName, NULL, NULL, NULL);
+    if (my_system(buffer)) {
+      exit(1);
+    }
+#else
+    buildCmdLine(buffer, argv, port->linker.cmd, srcFileName, NULL, NULL, NULL);
     if (my_system(argv[0], argv)) {
        perror("Cannot exec linker");
        exit(1);
     }
 
+#endif
+
     if (strcmp(srcFileName,"temp") == 0) {
        /* rename "temp.cdb" to "firstRelFile.cdb" */
        char *f = strtok(strdup(relFiles[0]),".");
@@ -1282,14 +1414,21 @@ static void linkEdit (char **envp)
 /*-----------------------------------------------------------------*/
 static void assemble (char **envp)
 {
+#ifdef USE_SYSTEM_SYSTEM_CALLS
+    buildCmdLine(buffer, port->assembler.cmd, srcFileName, NULL, NULL, asmOptions);
+    if (my_system(buffer)) {
+      exit(1);
+    }
+#else
     char *argv[128];  /* assembler arguments */
 
     buildCmdLine(buffer, argv, port->assembler.cmd, srcFileName, NULL, NULL, asmOptions);
 
     if (my_system(argv[0], argv)) {
-       perror("Cannot exec assember");
+       perror("Cannot exec assembler");
        exit(1);
     }
+#endif
 }
 
 
@@ -1299,7 +1438,9 @@ static void assemble (char **envp)
 /*-----------------------------------------------------------------*/
 static int preProcess (char **envp)
 {
+#ifndef USE_SYSTEM_SYSTEM_CALLS
     char *argv[128];
+#endif
     char procDef[128];
 
     preOutName = NULL;
@@ -1348,18 +1489,26 @@ static int preProcess (char **envp)
        if (!preProcOnly)
            preOutName = strdup(tmpnam(NULL));
 
-       buildCmdLine(buffer, argv, _preCmd, fullSrcFileName, 
-                     preOutName, srcFileName, preArgv);
-
        if (options.verbose)
          printf ("sdcc: Calling preprocessor...\n");
 
+#ifdef USE_SYSTEM_SYSTEM_CALLS
+       buildCmdLine(buffer, _preCmd, fullSrcFileName, 
+                     preOutName, srcFileName, preArgv);
+       if (my_system(buffer)) {
+         exit(1);
+       }
+#else
+       buildCmdLine(buffer, argv, _preCmd, fullSrcFileName, 
+                     preOutName, srcFileName, preArgv);
+
        if (my_system(argv[0], argv)) {
            unlink (preOutName);
            perror("Cannot exec Preprocessor");
            exit(1);
        }
 
+#endif
        if (preProcOnly)
            exit(0);
     }
@@ -1410,6 +1559,28 @@ int main ( int argc, char **argv , char **envp)
     if (port->init)
        port->init();
 
+#if defined(_MSC_VER)
+
+      {
+      int i ;
+
+      // Create a default exe search path from the path to the sdcc command
+
+      strcpy(DefaultExePath,argv[0]) ;
+
+      for(i = strlen(DefaultExePath) ; i > 0 ; i--)
+       if (DefaultExePath[i] == '\\')
+         {
+         DefaultExePath[i] = '\0' ;
+         break ;
+         }
+
+      if (i == 0)
+       DefaultExePath[0] = '\0' ;
+      }
+
+#endif
+
     setDefaultOptions();
     parseCmdLine(argc,argv);
 
@@ -1492,4 +1663,4 @@ int main ( int argc, char **argv , char **envp)
 
     return 0;
     
-}
+  }