You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ge...@apache.org on 2006/12/05 12:39:29 UTC

svn commit: r482606 - /harmony/enhanced/jdktools/trunk/modules/samsa/src/main/native/samsa/samsa.c

Author: geirm
Date: Tue Dec  5 03:39:24 2006
New Revision: 482606

URL: http://svn.apache.org/viewvc?view=rev&rev=482606
Log:
more tweaks and cleanup


Modified:
    harmony/enhanced/jdktools/trunk/modules/samsa/src/main/native/samsa/samsa.c

Modified: harmony/enhanced/jdktools/trunk/modules/samsa/src/main/native/samsa/samsa.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/jdktools/trunk/modules/samsa/src/main/native/samsa/samsa.c?view=diff&rev=482606&r1=482605&r2=482606
==============================================================================
--- harmony/enhanced/jdktools/trunk/modules/samsa/src/main/native/samsa/samsa.c (original)
+++ harmony/enhanced/jdktools/trunk/modules/samsa/src/main/native/samsa/samsa.c Tue Dec  5 03:39:24 2006
@@ -29,11 +29,6 @@
 #define CLASS_PREFIX  "org.apache.harmony.tools."
 #define CLASS_POSTFIX ".Main"
 
-typedef struct ToolData {
-    int numJars; 
-    char **jarList;
-} TOOLDATA;
-
 #if defined(LINUX)
 #define PATH_SEPARATOR_CHAR '/'
 #define PATH_SEPARATOR      "/"
@@ -50,11 +45,32 @@
 #define CLASSPATH_SEP       ";"
 #endif
 
-char *cleanToolName(const char *);
-char *getExeDir();
-char *getJDKRoot();
+typedef struct ToolData {
+    int numJars; 
+    char **jarList;
+} TOOLDATA;
+
+char     *cleanToolName(const char *);
+char     *getExeDir();
+char     *getJDKRoot();
 TOOLDATA *getToolData(const char *, const char *);
 
+/**
+ *  main
+ * 
+ *  based on invocation name (ex. 'javac') discovers jars needed
+ *  for invocation - creates classpath and classname for invoking
+ *  the JVM in jre/bin via an exec() or CreateProcess(), 
+ *  and does so with the effective command  line pattern : 
+ * 
+ *      java -cp <created class path> <created Class name> <tool args>
+ * 
+ *  where 'created Class name' follows the convention of 
+ * 
+ *      org.apache.harmony.tools.<toolname>.Main
+ * 
+ *  where <toolname> is 'javac', 'javah', 'javap'
+ */
 
 int main (int argc, char **argv, char **envp)
 {
@@ -70,16 +86,6 @@
     char *jdkRoot = NULL;
     char *fullExePath = NULL;
     TOOLDATA *pToolData = (TOOLDATA *) malloc(sizeof(TOOLDATA));
-        
-    /* 
-     * if we can't figure out what tool we are, just bail
-     */    
-    toolName = cleanToolName(argv[0]);
-
-    if (toolName == NULL) { 
-        fprintf(stderr, "Uknown tool name %s\n", argv[0]);
-        return 1;
-    }
 
     /*
      *  get the jdkroot and the construct invocation path for exe
@@ -87,16 +93,37 @@
      *  from anywhere
      */    
     jdkRoot = getJDKRoot();
-    printf("root = %s\n", jdkRoot);
+
+//    printf("root = %s\n", jdkRoot);
     
     if (!jdkRoot) { 
         fprintf(stderr, "Unable to find JDK Root");
         return 2;
     }
+        
+    /* 
+     * if we can't figure out what tool we are, just bail
+     */    
+    toolName = cleanToolName(argv[0]);
 
-    pToolData = getToolData(toolName, jdkRoot);
+//    printf("tool name = %s\n", toolName);
     
+    if (toolName == NULL) { 
+        fprintf(stderr, "Uknown tool name %s\n", argv[0]);
+        return 1;
+    }
+
+    /*
+     *  get the 'tool data' - right now, this is just the jars
+     *  specificly needed by this tool
+     */
+    pToolData = getToolData(toolName, jdkRoot);
        
+    if (pToolData == NULL) { 
+        fprintf(stderr, "error : unable to get tool data for %s");
+        return 2;
+    }
+    
     fullExePath = (char *) malloc(strlen(jdkRoot) + strlen(EXE_POSTFIX) + 1);
     
     strcpy(fullExePath, jdkRoot);
@@ -155,16 +182,23 @@
     
     myArgv[newIndex] = '\0';
 
-    for (i=0; i < myArgvCount; i++) { 
-        printf(" %d = %s\n", i, myArgv[i]);
-    }
-    
+//    for (i=0; i < myArgvCount; i++) { 
+//        printf(" %d = %s\n", i, myArgv[i]);
+//    }
+
+    free(toolName);
+        
     /*
      * now simply execv() the java app w/ the new params
      */ 
      
 #if defined(WIN32)
 
+    /*
+     * win32 - CreateProcess() needs a cmd line string
+     *  so simply build one
+     */
+     
     j = 0;
     for (i=1; i < myArgvCount; i++) {
         if (myArgv[i] != NULL) {
@@ -175,6 +209,10 @@
     
     toolName = (char *) malloc(sizeof(char) * j);
     
+    if (toolName == NULL) { 
+        return 4;
+    }
+    
     strcpy(toolName,myArgv[1]);
     strcat(toolName, " ");
         
@@ -188,10 +226,6 @@
         }
     }
    
-    printf("Calling %s\n", fullExePath);
-    printf("cmdline %s\n", toolName);
-    
-    
     memset(&procInfo, 0, sizeof(PROCESS_INFORMATION));
     memset(&startInfo, 0, sizeof(STARTUPINFO));
     startInfo.cb = sizeof(STARTUPINFO);
@@ -204,32 +238,59 @@
 
 }
 
-/**
- * cleanToolName
+/***********************************************************************
+ * cleanToolName()
  * 
  * takes a executable name and finds the tool name
- * in it
+ * in it.
  * 
- * returns real tool name, or NULL if not found
+ * returns new string with real tool name, or NULL if not found
  */
 char *cleanToolName(const char *name) 
 {
-    int i;
-    char *toolNames[] = { "javac", "javap", "javah", "java" };
+    char *last = strrchr(name, PATH_SEPARATOR_CHAR);
+
+ #if defined(WIN32)
+    char *temp;
+    char *exe;
+         
+    if (last && *(last + 1)) {
+        temp = strdup(last + 1);
+    }
+    else {
+        temp = strdup(name);
+    }
+    
+    char *exe = strcasestr(temp, ".exe");
+         
+     if (exe) { 
+        *exe = '\0';
+     }
+         
+     return temp;     
+ #endif
     
-    /* 
-     *  FIXME :  this is an awful hack, easy to fool, but for now...
+ #if defined(LINUX)
+ 
+    /*
+     *  if we found a slash (and someone didn't do something 
+     *  stupid like invoke "java/"?)
      */
-    for (i=0; i < sizeof(toolNames)/sizeof(toolNames[0]); i++) { 
-        if (strstr(name, toolNames[i])) {
-            return toolNames[i];
-        }
+    if (last && *(last +1)) { 
+        return strdup(last +1);
     }
-    
-    return NULL;    
+    else { 
+        return strdup(name);
+    }
+ #endif
 }
 
-
+/******************************************************************
+ *  getJDKRoot()
+ * 
+ *  returns the root of the JDK if it can figure it out
+ *  or NULL if it can't
+ */
 char *getJDKRoot() { 
     
     char *exeDir = getExeDir();
@@ -243,8 +304,9 @@
     
     return NULL;
 }
-/**
- * getExeDir
+
+/*****************************************************************
+ * getExeDir()
  * 
  *  returns directory of running exe
  */
@@ -261,10 +323,10 @@
 #endif
 
 #if defined(WIN32)
-    char buffer[256];
-    DWORD dwRet = GetModuleFileName(NULL, buffer, 256);
+    char buffer[512];
+    DWORD dwRet = GetModuleFileName(NULL, buffer, 512);
         
-    // FIXME - handle this right
+    // FIXME - handle this right - it could be that 512 isn't enough
 #endif
 
     last = strrchr(buffer, PATH_SEPARATOR_CHAR);
@@ -277,7 +339,9 @@
     return NULL;
 }
 
-/**
+/***********************************************************************
+ *  getToolData()
+ * 
  *  Read the jdk/bin/data/<toolname>.data file and 
  *  return the list of jars needed for this tool
  *  Format : 
@@ -295,13 +359,17 @@
     int count = 0;
     char *temp = NULL;
     TOOLDATA *pToolData = (TOOLDATA *) malloc(sizeof(TOOLDATA));
-    
-    memset(pToolData, 0, sizeof(TOOLDATA));    
-        
+            
     if (toolName == NULL || jdkRoot == NULL) { 
         return NULL;
     }
    
+    if (pToolData == NULL) { 
+        return NULL;
+    }
+    
+    memset(pToolData, 0, sizeof(TOOLDATA));    
+    
    /*
     *  assumes that the data files are in jdk/bin/data with a ".dat" extension
     */ 
@@ -309,6 +377,10 @@
             + strlen(PATH_SEPARATOR) + strlen("data") + strlen(PATH_SEPARATOR) + strlen(toolName) 
             + strlen(".dat") + 1);
                 
+    if (temp == NULL) { 
+        return NULL;
+    }
+    
     strcpy(temp, jdkRoot);
     strcat(temp, PATH_SEPARATOR);
     strcat(temp, "bin");