Index: trunk/ippTools/src/Makefile.am
===================================================================
--- trunk/ippTools/src/Makefile.am	(revision 11034)
+++ trunk/ippTools/src/Makefile.am	(revision 11037)
@@ -1,2 +1,3 @@
+
 bin_PROGRAMS = \
 	detselect \
@@ -12,5 +13,8 @@
 	pztool 
 
-include_HEADERS = pxtools.h
+include_HEADERS = \
+	pxtoolsErrorCodes.h \
+	pxtools.h
+
 noinst_HEADERS = \
 	dettool.h \
@@ -31,4 +35,6 @@
 libpxtools_la_LDFLAGS   = -release $(PACKAGE_VERSION)
 libpxtools_la_SOURCES   = \
+	pxtoolsErrorCodes.c \
+	pxerrors.c \
 	pxconfig.c \
 	pxfault.c \
@@ -117,2 +123,12 @@
 tags:
 	etags `find . -name \*.[ch] -print`
+
+# Error codes.
+BUILT_SOURCES = pxtoolsErrorCodes.h pxtoolsErrorCodes.c
+CLEANFILES = pxtoolsErrorCodes.h pxtoolsErrorCodes.c
+
+pxtoolsErrorCodes.h : pxtoolsErrorCodes.dat pxtoolsErrorCodes.h.in
+	$(ERRORCODES) --data=pxtoolsErrorCodes.dat --outdir=. pxtoolsErrorCodes.h
+
+pxtoolsErrorCodes.c : pxtoolsErrorCodes.dat pxtoolsErrorCodes.c.in pxtoolsErrorCodes.h
+	$(ERRORCODES) --data=pxtoolsErrorCodes.dat --outdir=. pxtoolsErrorCodes.c
Index: trunk/ippTools/src/pxerrors.c
===================================================================
--- trunk/ippTools/src/pxerrors.c	(revision 11037)
+++ trunk/ippTools/src/pxerrors.c	(revision 11037)
@@ -0,0 +1,42 @@
+/*
+ * pxerrors.c
+ *
+ * Copyright (C) 2006  Eugene Magnier
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * program; see the file COPYING. If not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "pxtools.h"
+
+psExit pxerrorGetExitStatus () {
+
+    psErrorCode err = psErrorCodeLast ();
+    switch (err) {
+      case PXTOOLS_ERR_SYS:
+	return PS_EXIT_SYS_ERROR;
+      case PXTOOLS_ERR_CONFIG:
+	return PS_EXIT_CONFIG_ERROR;
+      case PXTOOLS_ERR_PROG:
+	return PS_EXIT_PROG_ERROR;
+      case PXTOOLS_ERR_DATA:
+	return PS_EXIT_DATA_ERROR;
+      default:
+	return PS_EXIT_UNKNOWN_ERROR;
+    }	
+    return PS_EXIT_UNKNOWN_ERROR;
+}
Index: trunk/ippTools/src/pxtools.h
===================================================================
--- trunk/ippTools/src/pxtools.h	(revision 11034)
+++ trunk/ippTools/src/pxtools.h	(revision 11037)
@@ -27,11 +27,5 @@
 # include <psmodules.h>
 # include <ippdb.h>
-
-typedef enum {
-    PX_ERROR_NONE           = 0x00,
-    PX_ERROR_UNKNOWN        = 0x01,
-    PX_ERROR_BAD_DATA       = 0x02,
-    PX_ERROR_ID10T          = 0x04,
-} pxErrorCode;
+# include "pxtoolsErrorCodes.h"
 
 // load these values from the db in the init stage
@@ -71,3 +65,5 @@
 bool pxSetFaultCode(psDB *dbh, const char *tableName, psMetadata *where, psS8 code);
 
+psExit pxerrorGetExitStatus ();
+
 #endif // PXTOOLS_H
Index: trunk/ippTools/src/pxtoolsErrorCodes.c.in
===================================================================
--- trunk/ippTools/src/pxtoolsErrorCodes.c.in	(revision 11037)
+++ trunk/ippTools/src/pxtoolsErrorCodes.c.in	(revision 11037)
@@ -0,0 +1,26 @@
+/*
+ * The line
+    { PXTOOLS_ERR_$X{ErrorCode}, "$X{ErrorDescription}"},
+ * (without the Xs)
+ * will be replaced by values from errorCodes.dat
+ */
+#include "pslib.h"
+#include "pxtoolsErrorCodes.h"
+
+void pxtoolsErrorRegister(void)
+{
+    static psErrorDescription errors[] = {
+       { PXTOOLS_ERR_BASE, "First value we use; lower values belong to psLib" },
+       { PXTOOLS_ERR_${ErrorCode}, "${ErrorDescription}"},
+    };
+    static int nerror = PXTOOLS_ERR_NERROR - PXTOOLS_ERR_BASE; // number of values in enum
+
+    for (int i = 0; i < nerror; i++) {
+       psErrorDescription *tmp = psAlloc(sizeof(psErrorDescription));
+       p_psMemSetPersistent(tmp, true);
+       *tmp = errors[i];
+       psErrorRegister(tmp, 1);
+       psFree(tmp);			/* it's on the internal list */
+    }
+    nerror = 0;			                // don't register more than once
+}
Index: trunk/ippTools/src/pxtoolsErrorCodes.dat
===================================================================
--- trunk/ippTools/src/pxtoolsErrorCodes.dat	(revision 11037)
+++ trunk/ippTools/src/pxtoolsErrorCodes.dat	(revision 11037)
@@ -0,0 +1,10 @@
+#
+# This file is used to generate pxtoolsErrorCodes.h
+#
+BASE = 1024		First value we use; lower values belong to psLib
+UNKNOWN			Unknown PM error code
+ARGUMENTS		Incorrect arguments
+SYS			System error
+CONFIG			Problem in configure files
+PROG			Programming error
+DATA   			invalid data
Index: trunk/ippTools/src/pxtoolsErrorCodes.h.in
===================================================================
--- trunk/ippTools/src/pxtoolsErrorCodes.h.in	(revision 11037)
+++ trunk/ippTools/src/pxtoolsErrorCodes.h.in	(revision 11037)
@@ -0,0 +1,18 @@
+#if !defined(PXTOOLS_ERROR_CODES_H)
+#define PXTOOLS_ERROR_CODES_H
+/*
+ * The line
+ *  PXTOOLS_ERR_$X{ErrorCode},
+ * (without the X)
+ *
+ * will be replaced by values from errorCodes.dat
+ */
+typedef enum {
+    PXTOOLS_ERR_BASE = 512,
+    PXTOOLS_ERR_${ErrorCode},
+    PXTOOLS_ERR_NERROR
+} pxtoolsErrorCode;
+
+void pxtoolsErrorRegister(void);
+
+#endif
Index: trunk/ippTools/src/regtool.c
===================================================================
--- trunk/ippTools/src/regtool.c	(revision 11034)
+++ trunk/ippTools/src/regtool.c	(revision 11037)
@@ -52,4 +52,6 @@
 int main(int argc, char **argv)
 {
+    psExit exit_status = PS_EXIT_SUCCESS;
+
     psLibInit(NULL);
 
@@ -72,7 +74,10 @@
     psLibFinalize();
 
-    exit(EXIT_SUCCESS);
+    exit(exit_status);
 
 FAIL:
+    // use the top-most error to determine the exit status
+    exit_status = pxerrorGetExitStatus();
+
     psFree(config);
     pmConfigDone();
@@ -80,5 +85,5 @@
     psLibFinalize();
 
-    exit(EXIT_FAILURE);
+    exit(exit_status);
 }
 
@@ -90,5 +95,5 @@
     psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
     if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -limit");
+        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
         return false;
     }
@@ -96,5 +101,5 @@
     bool faulted = psMetadataLookupU64(&status, config->args, "-faulted");
     if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -faulted");
+        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -faulted");
         return false;
     }
@@ -141,5 +146,6 @@
 
     if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
+	// XXX PS_EXIT_PROG_ERROR (incorrect SQL) or SYS_ERROR (database comms)
+        psError(PXTOOLS_ERR_PROG, false, "database error");
         psFree(query);
         return false;
@@ -149,10 +155,9 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
+	// XXX PS_EXIT_PROG_ERROR (incorrect SQL) or SYS_ERROR (database comms)
+        psError(PXTOOLS_ERR_PROG, false, "database error");
         return false;
     }
     if (!psArrayLength(output)) {
-        // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no pending newExp rows found");
         psFree(output);
         return true;
@@ -160,16 +165,13 @@
 
     bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
+    simple = psMetadataLookupBool(&status, config->args, "-simple");
+    if (!status) {
+	psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -simple");
+	return false;
     }
 
     // negate simple so the default is true
     if (!ippdbPrintMetadatas(stdout, output, "p0PendingExp", !simple)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psError(PXTOOLS_ERR_PROG, false, "failed to print array");
         psFree(output);
         return false;
@@ -188,5 +190,5 @@
     psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
     if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -limit");
+        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
         return false;
     }
@@ -194,5 +196,5 @@
     bool faulted = psMetadataLookupU64(&status, config->args, "-faulted");
     if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -faulted");
+        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -faulted");
         return false;
     }
@@ -231,5 +233,6 @@
 
     if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
+	// XXX PS_EXIT_PROG_ERROR (incorrect SQL) or SYS_ERROR (database comms)
+        psError(PXTOOLS_ERR_PROG, false, "database error");
         psFree(query);
         return false;
@@ -239,10 +242,9 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
+	// XXX PS_EXIT_PROG_ERROR (incorrect SQL) or SYS_ERROR (database comms)
+        psError(PXTOOLS_ERR_PROG, false, "database error");
         return false;
     }
     if (!psArrayLength(output)) {
-        // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no pending newImfile rows found");
         psFree(output);
         return true;
@@ -250,16 +252,13 @@
 
     bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
+    simple = psMetadataLookupBool(&status, config->args, "-simple");
+    if (!status) {
+	psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -simple");
+	return false;
     }
 
     // negate simple so the default is true
     if (!ippdbPrintMetadatas(stdout, output, "newImfile", !simple)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psError(PXTOOLS_ERR_PROG, false, "failed to print array");
         psFree(output);
         return false;
@@ -1094,21 +1093,2 @@
     return raw;
 }
-
-#if 0
-static psU32 mapCodeStrToInt(const char *codeStr)
-{
-    if (strcasestr(codeStr, "none")) {
-        return PX_ERROR_NONE;
-    } else if (strcasestr(codeStr, "unknown")) {
-        return PX_ERROR_UNKNOWN;
-    } else if (strcasestr(codeStr, "bad_data")) {
-        return PX_ERROR_BAD_DATA;
-    } else if (strcasestr(codeStr, "id10t")) {
-        return PX_ERROR_ID10T;
-    }
-
-    psError(PS_ERR_UNKNOWN, true, "invalid fault code string");
-
-    return (psU32) -1;
-}
-#endif
