struct s_built_in_vars {char *var_name; int code; int (*func)(JCR *jcr, int code,
                         const char **val_ptr, int *val_len, int *val_size);};
 
+/*
+ * Table of build in variables
+ */
 static struct s_built_in_vars built_in_vars[] = {
    { N_("Year"),       1, date_item},
    { N_("Month"),      2, date_item},
 };
 
 
+/*
+ * Search the table of built-in variables, and if found,
+ *   call the appropriate subroutine to do the work.
+ */
 static var_rc_t lookup_built_in_var(var_t *ctx, void *my_ctx, 
          const char *var_ptr, int var_len, int var_index, 
          const char **val_ptr, int *val_len, int *val_size)
         *val_ptr = bstrdup(buf);
         *val_len = strlen(buf);
         *val_size = *val_len;
-        if (var_inc && counter->Catalog) {
-           COUNTER_DBR cr;
-           JCR *jcr = (JCR *)my_ctx;
-           memset(&cr, 0, sizeof(cr));
-           bstrncpy(cr.Counter, counter->hdr.name, sizeof(cr.Counter));
-           cr.MinValue = counter->MinValue;
-           cr.MaxValue = counter->MaxValue;
+        if (var_inc) {               /* increment the variable? */
            if (counter->CurrentValue == counter->MaxValue) {
               counter->CurrentValue = counter->MinValue;
            } else {
               counter->CurrentValue++;
            }
-           cr.CurrentValue = counter->CurrentValue;
-            Dmsg1(100, "New value=%d\n", cr.CurrentValue);
-           if (counter->WrapCounter) {
-              bstrncpy(cr.WrapCounter, counter->WrapCounter->hdr.name, sizeof(cr.WrapCounter));
-           } else {
-              cr.WrapCounter[0] = 0;
-           }
-           if (!db_update_counter_record(jcr, jcr->db, &cr)) {
-               Jmsg(jcr, M_ERROR, 0, _("Count not update counter %s: ERR=%s\n"),
-                 counter->hdr.name, db_strerror(jcr->db));
+           if (counter->Catalog) {   /* update catalog if need be */
+              COUNTER_DBR cr;
+              JCR *jcr = (JCR *)my_ctx;
+              memset(&cr, 0, sizeof(cr));
+              bstrncpy(cr.Counter, counter->hdr.name, sizeof(cr.Counter));
+              cr.MinValue = counter->MinValue;
+              cr.MaxValue = counter->MaxValue;
+              cr.CurrentValue = counter->CurrentValue;
+               Dmsg1(100, "New value=%d\n", cr.CurrentValue);
+              if (counter->WrapCounter) {
+                 bstrncpy(cr.WrapCounter, counter->WrapCounter->hdr.name, sizeof(cr.WrapCounter));
+              } else {
+                 cr.WrapCounter[0] = 0;
+              }
+              if (!db_update_counter_record(jcr, jcr->db, &cr)) {
+                  Jmsg(jcr, M_ERROR, 0, _("Count not update counter %s: ERR=%s\n"),
+                    counter->hdr.name, db_strerror(jcr->db));
+              }
            }
-        }  
+        }       
         stat = VAR_OK;
         break;
       }
 
 
 /*
- * Called here to look up a variable   
+ * Called here from "core" expand code to look up a variable   
  */
 static var_rc_t lookup_var(var_t *ctx, void *my_ctx, 
          const char *var_ptr, int var_len, int var_inc, int var_index, 
    var_rc_t stat;
    int count;
 
+   /* Note, if val_size > 0 and val_ptr!=NULL, the core code will free() it */
    if ((stat = lookup_built_in_var(ctx, my_ctx, var_ptr, var_len, var_index,
        val_ptr, val_len, val_size)) == VAR_OK) {
       return VAR_OK;
    if (var_index == 0) {
       *val_ptr = val;
       *val_len = strlen(val);
-      *val_size = 0;
+      *val_size = 0;                  /* don't try to free val_ptr */
       return VAR_OK;
    }
    /* He wants to index the "array" */
 
                    PSTR CmdLine, int iCmdShow)
 {
    char *szCmdLine = CmdLine;
-   char *wordPtr,*tempPtr;
-   int i,quote;
+   char *wordPtr, *tempPtr;
+   int i, quote;
 
    /* Save the application instance and main thread id */
    hAppInstance = hInstance;
     * the last character before first space is a double
     * quote, we throw away the junk.
     */
+
    wordPtr = szCmdLine;
    while (*wordPtr && *wordPtr != ' ')
       wordPtr++;
    if (wordPtr > szCmdLine)      /* backup to char before space */
       wordPtr--;
    /* if first character is not a quote and last is, junk it */
-   if (*szCmdLine != '"' && *wordPtr == '"')
+   if (*szCmdLine != '"' && *wordPtr == '"') {
       szCmdLine = wordPtr + 1;
-   //      MessageBox(NULL, szCmdLine, "Cmdline", MB_OK);
+   }
+// MessageBox(NULL, szCmdLine, "Cmdline", MB_OK);
 
    /* Build Unix style argc *argv[] */      
 
 
                goto error_return;
            }
        }
+       tokenbuf_free(&tmp);          /* KES 11/9/2003 */
     } while (rc > 0);
     tokenbuf_free(&tmp);
     return (p - begin);
 
-    error_return:
+error_return:
     tokenbuf_free(&tmp);
     tokenbuf_free(result);
     return rc;
                goto error_return;
            }
        }
+       tokenbuf_free(&tmp);          /* KES 11/9/2003 */
     } while (rc > 0);
 
     tokenbuf_free(&tmp);
     return (p - begin);
 
-    error_return:
+error_return:
     tokenbuf_free(&tmp);
     tokenbuf_free(result);
     return rc;
                goto error_return;
            }
        }
+       tokenbuf_free(&tmp);          /* KES 11/9/2003 */
     } while (rc > 0);
 
     tokenbuf_free(&tmp);
     return (p - begin);
 
-    error_return:
+error_return:
     tokenbuf_free(&tmp);
     tokenbuf_free(result);
     return rc;
     tokenbuf_free(&dstclass);
     return VAR_OK;
 
-    error_return:
+error_return:
     tokenbuf_free(search);
     tokenbuf_free(replace);
     tokenbuf_free(&srcclass);
     return (p - begin);
 
     /* return with an error */
-    error_return:
+error_return:
     tokenbuf_free(data);
     tokenbuf_free(&tmptokbuf);
     tokenbuf_free(&search);
                return rc;
            p += rc;
            *result = 0;
-       }
-       else if (rc < 0)
+           tokenbuf_free(&tmp);      /* KES 11/9/2003 */
+       } else if (rc < 0) {
            return rc;
-       else {
+       } else {
            p += rc;
            /* parse remaining numerical expression */
            rc = parse_numexp(var, ctx, tmp.begin, tmp.end, result, failed);
            }
            p += rc;
        }
+       tokenbuf_free(&tmp);          /* KES 11/9/2003 */
     } while (rc > 0);
 
     /* we must have the complete expanded variable name now, 
     }
 
     /* parse optional post-operations */
-    goahead:
+goahead:
     if (p[-1] == ':') {
        tokenbuf_free(&tmp);
        tokenbuf_init(&tmp);
        if (ctx->force_expand) {
            rc = VAR_ERR_UNDEFINED_VARIABLE;
            goto error_return;
-       }
-       else
+       } else {
            tokenbuf_set(result, begin - 1, p, 0);
+       }
     }
 
     /* return successfully */
     return (p - begin);
 
     /* return with an error */
-    error_return:
+error_return:
     tokenbuf_free(&name);
     tokenbuf_free(&tmp);
     tokenbuf_free(result);