Index: trunk/ippTools/src/pstamptool.c
===================================================================
--- trunk/ippTools/src/pstamptool.c	(revision 27856)
+++ trunk/ippTools/src/pstamptool.c	(revision 27874)
@@ -40,4 +40,5 @@
 static bool updatereqMode(pxConfig *config);
 static bool revertreqMode(pxConfig *config);
+static bool pendingcleanupMode(pxConfig *config);
 static bool addjobMode(pxConfig *config);
 static bool listjobMode(pxConfig *config);
@@ -81,4 +82,5 @@
         MODECASE(PSTAMPTOOL_MODE_UPDATEREQ, updatereqMode);
         MODECASE(PSTAMPTOOL_MODE_REVERTREQ, revertreqMode);
+        MODECASE(PSTAMPTOOL_MODE_PENDINGCLEANUP, pendingcleanupMode);
         MODECASE(PSTAMPTOOL_MODE_ADDJOB, addjobMode);
         MODECASE(PSTAMPTOOL_MODE_LISTJOB, listjobMode);
@@ -265,4 +267,5 @@
         NULL,   // outProduct
         uri,
+        NULL,   // outdir
         0       // fault
         )) {
@@ -341,5 +344,5 @@
 }
 
-static bool listreqMode(pxConfig *config)
+static bool pendingcleanupMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -347,21 +350,23 @@
     psMetadata *where = psMetadataAlloc();
     PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "==");
-    PXOPT_COPY_S64(config->args, where, "-not_req_id", "req_id", "!=");
-    PXOPT_COPY_STR(config->args, where, "-name", "name", "==");
-
-    PXOPT_LOOKUP_U64(limit,   config->args, "-limit",  false, false);
+    pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE");
+
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
-    if (!psListLength(where->list)) {
-        psError(PS_ERR_UNKNOWN, true, "-req_id or -name must be supplied");
-        return false;
-    }
-
-    psString query = psStringCopy("SELECT * from pstampRequest");
-
-    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-    psStringAppend(&query, " WHERE %s", whereClause);
-    psFree(whereClause);
-    psFree(where);
+    psString query = pxDataGet("pstamptool_pendingcleanup.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "pstampRequest");
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+    psFree(where);
+
+    psStringAppend(&query, " ORDER BY priority DESC, req_id");
 
     // treat limit == 0 as "no limit"
@@ -385,10 +390,7 @@
     }
     if (!psArrayLength(output)) {
-        psTrace("pstamptool", PS_LOG_INFO, "request not found");
-        // This causes main to exit with PS_EXIT_DATA_ERROR which the script is looking for
-        psError(PXTOOLS_ERR_CONFIG, true, "request not found");
-        psFree(output);
-        // we return false so that the caller can determine that a request does not exist
-        return false;
+        psTrace("pstamptool", PS_LOG_INFO, "no rows found");
+        psFree(output);
+        return true;
     }
 
@@ -405,24 +407,27 @@
 }
 
-static bool completedreqMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    psMetadata *where = psMetadataAlloc();
-    pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE");
-
-    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+static bool listreqMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-not_req_id", "req_id", "!=");
+    PXOPT_COPY_STR(config->args, where, "-name", "name", "==");
+
+    PXOPT_LOOKUP_U64(limit,   config->args, "-limit",  false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
-    psString query = pxDataGet("pstamptool_completedreq.sql");
-
-    if (psListLength(where->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-    psFree(where);
-
-    psStringAppend(&query, " ORDER BY priority DESC, req_id");
+    if (!psListLength(where->list)) {
+        psError(PS_ERR_UNKNOWN, true, "-req_id or -name must be supplied");
+        return false;
+    }
+
+    psString query = psStringCopy("SELECT * from pstampRequest");
+
+    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+    psStringAppend(&query, " WHERE %s", whereClause);
+    psFree(whereClause);
+    psFree(where);
 
     // treat limit == 0 as "no limit"
@@ -438,4 +443,5 @@
         return false;
     }
+    psFree(query);
 
     psArray *output = p_psDBFetchResult(config->dbh);
@@ -444,9 +450,11 @@
         return false;
     }
-
     if (!psArrayLength(output)) {
-        psTrace("pstamptool", PS_LOG_INFO, "no rows found");
-        psFree(output);
-        return true;
+        psTrace("pstamptool", PS_LOG_INFO, "request not found");
+        // This causes main to exit with PS_EXIT_DATA_ERROR which the script is looking for
+        psError(PXTOOLS_ERR_CONFIG, true, "request not found");
+        psFree(output);
+        // we return false so that the caller can determine that a request does not exist
+        return false;
     }
 
@@ -463,23 +471,103 @@
 }
 
+static bool completedreqMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psMetadata *where = psMetadataAlloc();
+    pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE");
+
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+
+    psString query = pxDataGet("pstamptool_completedreq.sql");
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+    psFree(where);
+
+    psStringAppend(&query, " ORDER BY priority DESC, req_id");
+
+    // treat limit == 0 as "no limit"
+    if (limit) {
+        psString limitString = psDBGenerateLimitSQL(limit);
+        psStringAppend(&query, " %s", limitString);
+        psFree(limitString);
+    }
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    }
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    if (!psArrayLength(output)) {
+        psTrace("pstamptool", PS_LOG_INFO, "no rows found");
+        psFree(output);
+        return true;
+    }
+
+    // negative simple so the default is true
+    if (!ippdbPrintMetadatas(stdout, output, "pstampRequest", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
+
+    return true;
+}
+
 static bool updatereqMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    PXOPT_LOOKUP_S64(req_id,     config->args, "-req_id",     true, false);
-    PXOPT_LOOKUP_STR(state,      config->args, "-state",      false, false);
-    PXOPT_LOOKUP_STR(outProduct, config->args, "-outProduct", false, false);
-    PXOPT_LOOKUP_S16(fault,      config->args, "-fault",      false, false);
-    PXOPT_LOOKUP_STR(uri,        config->args, "-uri",        false, false);
-    PXOPT_LOOKUP_STR(name,       config->args, "-name",       false, false);
-    PXOPT_LOOKUP_STR(reqType,    config->args, "-reqType",    false, false);
-
-    psString query = NULL;
-    psStringAppend(&query, "UPDATE pstampRequest SET");
-
+    // PXOPT_LOOKUP_S64(req_id,     config->args, "-req_id",   true, false);
+    PXOPT_LOOKUP_STR(state,      config->args, "-set_state",      false, false);
+    PXOPT_LOOKUP_STR(label,      config->args, "-set_label",      false, false);
+    PXOPT_LOOKUP_STR(outProduct, config->args, "-set_outProduct", false, false);
+    PXOPT_LOOKUP_S16(fault,      config->args, "-set_fault",          false, false);
+    PXOPT_LOOKUP_STR(uri,        config->args, "-set_uri",        false, false);
+    PXOPT_LOOKUP_STR(outdir,     config->args, "-set_outdir",        false, false);
+    PXOPT_LOOKUP_STR(name,       config->args, "-set_name",       false, false);
+    PXOPT_LOOKUP_STR(reqType,    config->args, "-set_reqType",    false, false);
+    PXOPT_LOOKUP_BOOL(clearfault,config->args, "-clearfault",    false);
+
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_S64(config->args, where, "-req_id",     "req_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-req_id_max", "req_id", "<=");
+    PXOPT_COPY_S32(config->args, where, "-fault",      "fault", "==");
+    PXOPT_COPY_STR(config->args, where, "-state",      "state", "==");
+    pxAddLabelSearchArgs(config, where, "-label",      "pstampRequest.label", "LIKE");
+    if (!psListLength(where->list)) {
+        psFree(where);
+        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
+        return false;
+    }
+
+    psString query = psStringCopy("UPDATE pstampRequest SET");
+
+    psString stateCheck = NULL;
     char c = ' ';
     if (state) {
         psStringAppend(&query, "%c state = '%s'", c, state);
         c = ',';
+        if (!strcmp(state, "goto_cleaned")) {
+            psStringAppend(&stateCheck, " AND state != 'cleaned'");
+        }
+    }
+    if (label) {
+        psStringAppend(&query, "%c label = '%s'", c, label);
+        c = ',';
     }
     if (outProduct) {
@@ -487,5 +575,16 @@
         c = ',';
     }
-    if (fault) {
+    if (outdir) {
+        psStringAppend(&query, "%c outdir = '%s'", c, outdir);
+        c = ',';
+    }
+    if (clearfault) {
+        if (fault) {
+            psError(PXTOOLS_ERR_CONFIG, true, "only one of -fault and -clearfault is allowed");
+            return false;
+        }
+        psStringAppend(&query, "%c fault = 0", c);
+        c = ',';
+    } else if (fault) {
         psStringAppend(&query, "%c fault = %d", c, fault);
         c = ',';
@@ -503,10 +602,17 @@
         c = ',';
     }
-    if (!state && !outProduct && !fault && !uri && !name && !reqType) {
-        psError(PS_ERR_UNKNOWN, true, "at least one of state, outProduct, fault, uri, name, and reqType must be specified");
-        return false;
-    }
-
-    psStringAppend(&query, " WHERE req_id = %" PRId64, req_id);
+    if (c != ',') {
+        psError(PS_ERR_UNKNOWN, true, "at least one set option is required");
+        return false;
+    }
+
+    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+    psStringAppend(&query, "\nWHERE %s", whereClause);
+    psFree(whereClause);
+    psFree(where);
+
+    if (stateCheck) {
+        psStringAppend(&query, stateCheck);
+    }
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -516,11 +622,6 @@
     }
 
-    psU64 affected = psDBAffectedRows(config->dbh);
-    // note zero is not an error
-    if (affected > 1) {
-        psError(PS_ERR_UNKNOWN, false, "should have affected one row but %"
-                                        PRIu64 " rows were modified", affected);
-        return false;
-    }
+    // psU64 affected = psDBAffectedRows(config->dbh);
+    // psLogMsg("pstamptool", PS_LOG_INFO, "Updated %" PRIu64 " pstampRequests", affected);
 
     return true;
@@ -778,6 +879,12 @@
         return false;
     }
-    PXOPT_LOOKUP_STR(state,     config->args, "-state",  true, false);
-    PXOPT_LOOKUP_S32(fault,     config->args, "-fault",  false, false);
+
+    PXOPT_LOOKUP_STR(state,     config->args, "-set_state",  false, false);
+    PXOPT_LOOKUP_S32(fault,     config->args, "-set_fault",  false, false);
+    
+    if (!state && !fault) {
+        psError(PS_ERR_UNKNOWN, true, "at least one of -set_state and -set_fault is required");
+        return false;
+    }
 
     psMetadata *where = psMetadataAlloc();
@@ -786,13 +893,16 @@
     PXOPT_COPY_S64(config->args, where, "-dep_id", "dep_id", "==");
 
-    psString faultStr = NULL;
-    if (!fault) {
-        faultStr = psStringCopy("");
-    } else {
-        psStringAppend(&faultStr, "\n, pstampJob.fault = %d", fault);
-    }
-
     psString query = pxDataGet("pstamptool_updatejob.sql");
 
+    char c = ' ';
+    if (state) {
+        psStringAppend(&query, "\n %c pstampJob.state = '%s'", c, state);
+        c = ',';
+    }
+    if (fault) {
+        psStringAppend(&query, "\n %c pstampJob.fault = %d", c, fault);
+        c = ',';
+    }
+    
     psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     psStringAppend(&query, " WHERE %s", whereClause);
@@ -800,10 +910,9 @@
     psFree(where);
 
-    if (!p_psDBRunQueryF(config->dbh, query, state, faultStr)) {
+    if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(query);
         return false;
     }
-    psFree(faultStr);
     psFree(query);
 
@@ -1159,5 +1268,5 @@
     PXOPT_LOOKUP_S64(dep_id,    config->args, "-dep_id", true, false);
     PXOPT_LOOKUP_STR(state,     config->args, "-set_state",  false, false);
-    PXOPT_LOOKUP_S16(fault,     config->args, "-fault",  false, false);
+    PXOPT_LOOKUP_S16(fault,     config->args, "-set_fault",  false, false);
 
     if (!fault && !state) {
