Index: trunk/psLib/src/sys/psTrace.c
===================================================================
--- trunk/psLib/src/sys/psTrace.c	(revision 556)
+++ trunk/psLib/src/sys/psTrace.c	(revision 559)
@@ -67,7 +67,8 @@
     componentAlloc(): allocate memory for a new node, and initialize members.
  *****************************************************************************/
-static Component *componentAlloc(const char *name,
-                                 int level)
-{
+Component *componentAlloc(const char *name,
+                          int level)
+{
+    //printf("Calling componentAlloc(%s, %d)\n", name, level);
     Component *comp = psAlloc(sizeof(Component));
     comp->name = psStringCopy(name);
@@ -75,5 +76,4 @@
     comp->dimen = comp->n = 0;
     comp->subcomp = NULL;
-
     return comp;
 }
@@ -124,27 +124,21 @@
 
 /*****************************************************************************
-    componentAdd(): Adds the component named "addNodeName" to the tree "comp".
- This procedure works by matching the current level of the component
- tree, pointed to by "comp", with the top level of the component
- name, pointed to by "addNodeName", and then recursively searching the
- component tree and pruning "addNodeName" until either a match is found,
- and then the level is set, or a new component is created.
+    componentAdd(): Adds the component named "addNodeName" to the root tree.
  
     NOTE: replace the call to strsep() with a call to strtok(), which conforms
     to ANSI-C.
- 
-    NOTE: Should we check in comp == NULL?
  *****************************************************************************/
 static void componentAdd(const char *addNodeName,
                          int         level)
 {
-    int i = 0;
-    char name[strlen(addNodeName) + 1]; // buffer for writeable copy of name.
-    char *rest = name;                 // rest of name
-    char *firstComponent = NULL;       // first component of name
+    int        i = 0;
+    char       name[strlen(addNodeName) + 1];
+    // buffer for writeable copy.
+    char      *pname=name;
+    char      *firstComponent = NULL;       // first component of name
     Component *currentNode = croot;
-    int nodeExists = 0;
-
-    printf("calling componentAdd(%s, %s, %d)\n", addNodeName, level);
+    int        nodeExists = 0;
+
+    // printf("Calling componentAdd(%s, %d)\n", addNodeName, level);
     // Is this the root node?  If so, simply set level and return.
     if (strcmp(".", addNodeName) == 0) {
@@ -153,31 +147,38 @@
     }
 
-    if (addNodeName[0] != '.')
-        printf("ERROR: failed to add %s to the root component tree.\n", addNodeName);
-    exit(1);
-}
-
-strcpy(name, addNodeName);
-// Iterate through the components of addNodeName.
-while (0 != strcmp(name, ""))
-{
-    rest = name;
-    firstComponent = strsep(&rest, ".");
-    // firstComponent points to the first component of the name, unless
-    // it was root, in which case it points to the next component.  The
-    // point rest points to everything else.
-
-    nodeExists = 0;
-    for (i = 0; i < comp->n; i++) {
-        if (strcmp(currentNode->subcomp[i]->name, firstComponent) == 0) {
-            currentNode = currentNode->subcomp[i];
-            nodeExists = 1;
-        }
+    if (addNodeName[0] != '.') {
+        printf("ERROR: failed to add %s to the root component tree.\n",
+               addNodeName);
+        exit(1);
+    }
+
+    strcpy(name, addNodeName);
+    pname = &name[1];
+    // Iterate through the components of addNodeName.  Strip off the first
+    // component of the name, find that in the root tree, or add it if it
+    // does not exist, then move to the next component in the name.
+
+    while (pname != NULL) {
+        firstComponent = strsep(&pname, ".");
+        nodeExists = 0;
+        for (i = 0; i < currentNode->n; i++) {
+            if (strcmp(currentNode->subcomp[i]->name, firstComponent) == 0) {
+                currentNode = currentNode->subcomp[i];
+                nodeExists = 1;
+                if (pname == NULL) {
+                    currentNode->level = level;
+                }
+            }
+        }
+
         if (nodeExists == 0) {
-            // GUS: add this component to this level of currentNode.
-        }
-        name = rest;
-    }
-    currentNode->level = level;
+            currentNode->subcomp = psRealloc(currentNode->subcomp,
+                                             (currentNode->n + 1) * sizeof(Component *));
+            currentNode->n = (currentNode->n)+1;
+
+            currentNode->subcomp[(currentNode->n)-1] =
+                componentAlloc(firstComponent, level);
+        }
+    }
 }
 
@@ -197,4 +198,11 @@
 static void setHighestLevel(const Component *comp)
 {
+    int i = 0;
+
+    if (comp == NULL) {
+        printf("ERROR: setHighestLevel(NULL)\n");
+        exit(1);
+    }
+
     // If this nodes trace level is the max so far, save it in highest_level
     if (comp->level > highest_level) {
@@ -203,5 +211,5 @@
 
     // Do the same for all children nodes.
-    for (int i = 0; i < comp->n; i++) {
+    for (i = 0; i < comp->n; i++) {
         setHighestLevel(comp->subcomp[i]);
     }
@@ -228,5 +236,5 @@
     }
 
-    printf("Calling psSetTraceLevel(%s, %d)\n", comp, level);
+    //printf("Calling psSetTraceLevel(%s, %d)\n", comp, level);
     // invalidate cache
     strncpy(cachedName, NO_CACHE, CACHE_NAME_LEN);
@@ -244,4 +252,5 @@
 
     // return 0 on success.
+    //printf("Called psSetTraceLevel(%s, %d)\n", comp, level);
     return 0;
 }
@@ -262,35 +271,34 @@
  The trace level of the "name" component.
  *****************************************************************************/
-static int doGetTraceLevel(const Component *comp, // the component to search
-                           const char *aname)     // the name to find
-{
-    char name[strlen(aname) + 1];       // need a writeable copy: for strsep()
+static int doGetTraceLevel(const char *aname)
+{
+    char       name[strlen(aname) + 1]; // need a writeable copy: for strsep()
+    char      *pname=name;
+    char      *firstComponent = NULL;   // first component of name
+    Component *currentNode = croot;
+    int        i = 0;
+
+    if (strcmp(".", aname) == 0) {
+        return(croot->level);
+    }
+
+    if (aname[0] != '.') {
+        return(UNKNOWN_TRACE_LEVEL);
+    }
+
     strcpy(name, aname);
-    char *firstComponent;               // first component of name
-    char *rest = name;                  // rest of name
-    int level = -1;
-    int i = 0;
-
-    firstComponent = strsep(&rest, ".");
-
-    // Look for a match for firstComponent in this level's subcomps
-    for (i = 0; i < comp->n; i++) {
-        if (strcmp(comp->subcomp[i]->name, firstComponent) == 0) {
-            if (rest == NULL) {
-                // Cool.  We have matched the full name.
-                level = comp->subcomp[i]->level;
-                return (level == UNKNOWN_TRACE_LEVEL) ? comp->level : level;
-            } else {
-                // We have matched this component.  Now we must recurse a
-                // level deeper and match the next component.
-
-                return doGetTraceLevel(comp->subcomp[i], rest);
+    pname = &name[1];
+    while (pname != NULL) {
+        firstComponent = strsep(&pname, ".");
+        for (i = 0; i < currentNode->n; i++) {
+            if (strcmp(currentNode->subcomp[i]->name, firstComponent) == 0) {
+                currentNode = currentNode->subcomp[i];
+                if (pname == NULL) {
+                    return(currentNode->level);
+                }
             }
         }
     }
-
-    // There was no exact match.  We return the level of the deepest
-    // component that has matched.
-    return comp->level;
+    return(UNKNOWN_TRACE_LEVEL);
 }
 
@@ -312,4 +320,5 @@
     int level = -1;
 
+    //printf("Calling psGetTraceLevel(%s)\n", name);
     // If the root component tree does not exist, then initialize it.
     if (croot == NULL) {
@@ -321,10 +330,8 @@
     if (strcmp(name, cachedName) == 0) {
         return cachedLevel;
-    } else if (strcmp(name, croot->name) == 0) {
-        return(croot->level);
     }
 
     // Search the component root tree, determine the trace level.
-    level = doGetTraceLevel(croot, name);
+    level = doGetTraceLevel(name);
 
     // Save this search info in our depth-one cache.
@@ -332,4 +339,5 @@
     cachedLevel = level;
 
+    //printf("Called psGetTraceLevel(%s) (level was %d)\n", name, level);
     return level;
 }
@@ -353,7 +361,4 @@
     int i = 0;
 
-    printf("CALLING (%d): doPrintTraceLevels(%s, %d)\n", depth, comp->name, depth);
-    printf("SHIT: comp->n is %d\n", comp->n);
-    printf("        [\n");
     if (comp->name[0] == '\0') {
         printf("%*s%-*s %d\n", depth, "", 20 - depth,
@@ -368,11 +373,8 @@
         }
     }
-    printf("        ]\n");
-
 
     for (i = 0; i < comp->n; i++) {
         doPrintTraceLevels(comp->subcomp[i], depth+1);
     }
-    printf("CALLED (%d): doPrintTraceLevels()\n", depth);
 }
 
