Index: trunk/psLib/src/sys/psTrace.c
===================================================================
--- trunk/psLib/src/sys/psTrace.c	(revision 481)
+++ trunk/psLib/src/sys/psTrace.c	(revision 556)
@@ -16,4 +16,12 @@
  not the same thing as a node's "level", which corresponds to the
  trace level of that node.  I should probably rename some variables.
+ 
+ This code is obduscated by the fact that component names are of the form
+ ".a.b.c.d" where "." is always the root of the tree, and for tree that have
+ a depth of 3 or higher (including the root ".") the "."  character is also
+ the deliminator between different individual nodes in the full component
+ name.  So, for ".a.b.c.d", the first "." signifies the root of the tree,
+ while the last three dots merely act as separators between the node names
+ "b", "c", and "d".
  *****************************************************************************/
 #include <stdlib.h>
@@ -28,5 +36,4 @@
 #define CACHE_NAME_LEN 50
 #define NO_CACHE "\a"                     // no name is cached
-#define UNKNOWN_LEVEL -9999               // we don't know this name's level
 /*****************************************************************************
     A component is a string of the form aaa.bbb.ccc, and may itself contain
@@ -47,10 +54,8 @@
 // tree.
 
-// NOTE: the next tree globals exist
-// for optimization purposes only.
+// NOTE: the next tree globals exist for optimization purposes only.
 // Consider removing them.
 static int highest_level = 0;             // Highest level requested.  Defining
-// this variable allows us to reduce
-// component-tree searches that tell
+// this variable allows us to reduce component-tree searches that tell
 // us if a message should be printed.
 static char cachedName[CACHE_NAME_LEN + 1] = NO_CACHE;
@@ -103,5 +108,5 @@
 {
     if (croot == NULL) {
-        croot = componentAlloc("", UNKNOWN_LEVEL);
+        croot = componentAlloc(".", UNKNOWN_TRACE_LEVEL);
     }
 }
@@ -119,5 +124,10 @@
 
 /*****************************************************************************
-    Add a new component to the tree
+    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.
  
     NOTE: replace the call to strsep() with a call to strtok(), which conforms
@@ -126,69 +136,48 @@
     NOTE: Should we check in comp == NULL?
  *****************************************************************************/
-static void componentAdd(Component  *comp,
-                         const char *aname,
+static void componentAdd(const char *addNodeName,
                          int         level)
 {
-    char name[strlen(aname) + 1];      // need a writeable copy
-    strcpy(name, aname);
     int i = 0;
-    int j = 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
-    char *rest = name;                 // rest of name
-
+    Component *currentNode = croot;
+    int nodeExists = 0;
+
+    printf("calling componentAdd(%s, %s, %d)\n", addNodeName, level);
+    // Is this the root node?  If so, simply set level and return.
+    if (strcmp(".", addNodeName) == 0) {
+        croot->level = level;
+        return;
+    }
+
+    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, ".");
-    // After this call, firstComponent
-    // points to the first component of the
-    // name, rest points to everything else.
-
-    // Does firstComponent match this level?
-    if (strcmp(comp->name, firstComponent) == 0) {
-        if (rest == NULL) {
-            comp->level = level;
-        } else {
-            componentAdd(comp, rest, level);
-        }
-        return;
-    }
-
-    // Look for a match for firstComponent in this level's subcomps
+    // 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(comp->subcomp[i]->name, firstComponent) == 0) {
-            if (rest == NULL) {
-                comp->subcomp[i]->level = level;
-            } else {
-                componentAdd(comp->subcomp[i], rest, level);
-            }
-
-            return;
-        }
-    }
-
-    // No match; add firstComponent to this level.  Ensure that subcomp is
-    // sorted.
-    if (comp->n >= comp->dimen - 1) {
-        comp->dimen += 5;
-
-        comp->subcomp = psRealloc(comp->subcomp,
-                                  comp->dimen*sizeof(Component *));
-    }
-
-    Component *firstComponent2 = componentAlloc(firstComponent, UNKNOWN_LEVEL);
-    for (i = 0; i < comp->n; i++) {
-        if (strcmp(firstComponent, comp->subcomp[i]->name) > 0) {
-            for (j = comp->n; j > i; j--) {
-                comp->subcomp[j] = comp->subcomp[j - 1];
-            }
-            break;
-        }
-    }
-    comp->subcomp[i] = firstComponent2;
-    comp->n++;
-
-    if (rest == NULL) {
-        firstComponent2->level = level;
-    } else {
-        componentAdd(firstComponent2, rest, level);
-    }
+        if (strcmp(currentNode->subcomp[i]->name, firstComponent) == 0) {
+            currentNode = currentNode->subcomp[i];
+            nodeExists = 1;
+        }
+        if (nodeExists == 0) {
+            // GUS: add this component to this level of currentNode.
+        }
+        name = rest;
+    }
+    currentNode->level = level;
 }
 
@@ -239,9 +228,10 @@
     }
 
+    printf("Calling psSetTraceLevel(%s, %d)\n", comp, level);
     // invalidate cache
     strncpy(cachedName, NO_CACHE, CACHE_NAME_LEN);
 
     // Add the new component to the component tree.
-    componentAdd(croot, comp, level);
+    componentAdd(comp, level);
 
     // Save this search info in our depth-one cache.
@@ -249,5 +239,5 @@
         highest_level = level;
     } else {
-        highest_level = UNKNOWN_LEVEL;
+        highest_level = UNKNOWN_TRACE_LEVEL;
         setHighestLevel(croot);
     }
@@ -280,14 +270,15 @@
     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 (int i = 0; i < comp->n; i++) {
+    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_LEVEL) ? comp->level : level;
+                return (level == UNKNOWN_TRACE_LEVEL) ? comp->level : level;
             } else {
                 // We have matched this component.  Now we must recurse a
@@ -330,4 +321,6 @@
     if (strcmp(name, cachedName) == 0) {
         return cachedLevel;
+    } else if (strcmp(name, croot->name) == 0) {
+        return(croot->level);
     }
 
@@ -360,9 +353,12 @@
     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,
-               "(root)", (comp->level == UNKNOWN_LEVEL) ? 0 : comp->level);
+               "(root)", (comp->level == UNKNOWN_TRACE_LEVEL) ? 0 : comp->level);
     } else {
-        if (comp->level == UNKNOWN_LEVEL) {
+        if (comp->level == UNKNOWN_TRACE_LEVEL) {
             printf("%*s%-*s %s\n", depth, "", 20 - depth,
                    comp->name, ".");
@@ -372,8 +368,11 @@
         }
     }
+    printf("        ]\n");
+
 
     for (i = 0; i < comp->n; i++) {
         doPrintTraceLevels(comp->subcomp[i], depth+1);
     }
+    printf("CALLED (%d): doPrintTraceLevels()\n", depth);
 }
 
@@ -428,5 +427,5 @@
 
     if ((level <= highest_level) &&
-            (psGetTraceLevel(comp) >= level)) {
+            (level <= psGetTraceLevel(comp))) {
         va_start(ap, level);
 
