Index: trunk/ippTools/src/chiptool.c
===================================================================
--- trunk/ippTools/src/chiptool.c	(revision 6137)
+++ trunk/ippTools/src/chiptool.c	(revision 6139)
@@ -2,4 +2,9 @@
 
 #include "p2tools.h"
+
+static bool quickMode(p2Config *config);
+static bool defineMode(p2Config *config);
+static bool pendingMode(p2Config *config);
+static bool doneMode(p2Config *config);
 
 int main(int argc, char **argv) {
@@ -10,99 +15,17 @@
     switch (config.mode) {
         case P2_MODE_QUICK:
-        {
-            psArray *rawFrames = p2searchRawFrames(&config);
-            if (!rawFrames) {
-                psError(PS_ERR_UNKNOWN, false, "no ppRawFrames found");
+            if (!quickMode(&config)) {
                 exit(EXIT_FAILURE);
             }
-            psArray *pendingFrames = p2rawToPending(&config, rawFrames);
-            if (!pendingFrames) {
-                psError(PS_ERR_UNKNOWN, false, "no p2PendingFrames found");
+            break;
+        case P2_MODE_DEFINE:
+            if (!defineMode(&config)) {
                 exit(EXIT_FAILURE);
             }
-            bool status = p2writePendingFrames(&config, pendingFrames);
-            if (!status) {
-                psError(PS_ERR_UNKNOWN, false, "p2writePendingFrames() failed");
+            break;
+        case P2_MODE_PENDING:
+            if (!pendingMode(&config)) {
                 exit(EXIT_FAILURE);
             }
-        }
-            break;
-        case P2_MODE_DEFINE:
-        {
-            psArray *rawFrames = p2searchRawFrames(&config);
-            if (!rawFrames) {
-                psError(PS_ERR_UNKNOWN, false, "no ppRawFrames found");
-                exit(EXIT_FAILURE);
-            }
-            psArray *pendingFrames = p2searchPendingFrames(&config);
-            // XXX compare raw frames to pending frames and remove duplicate 
-            // frames from the raw set.  This may not be quiet right as it's
-            // possible (likely?) that a rawScienceExp is inserted into the
-            // database prior to /ALL/ of that exposure's Imfiles
-            if (pendingFrames) {
-                for (int i = 0; i < rawFrames->n; i++) {
-                    ppRawFrame *rawFrame = rawFrames->data[i];
-                    for (int j = 0; j < pendingFrames->n; j++) {
-                        p2PendingFrame *pendingFrame = pendingFrames->data[j];
-                        if (strcmp(rawFrame->exposure->exp_id,
-                                   pendingFrame->exposure->exp_id) == 0) {
-                            psArrayRemove(rawFrames, rawFrame);
-                            // dec the counter as the array just got shorter
-                            // and we don't want to skip elemnts
-                            i--;
-                            break;
-                        } 
-                    }
-                }
-
-                psFree(pendingFrames);
-            }
-
-            psArray *doneFrames = p2searchDoneFrames(&config);
-            if (doneFrames && (rawFrames->n > 0)) {
-                for (int i = 0; i < rawFrames->n; i++) {
-                    ppRawFrame *rawFrame = rawFrames->data[i];
-                    for (int j = 0; j < pendingFrames->n; j++) {
-                        p2DoneFrame *doneFrame = pendingFrames->data[j];
-                        if (strcmp(rawFrame->exposure->exp_id,
-                                   doneFrame->exposure->exp_id) == 0) {
-                            psArrayRemove(rawFrames, rawFrame);
-                            // dec the counter as the array just got shorter
-                            // and we don't want to skip elemnts
-                            i--;
-                            break;
-                        } 
-                    }
-                }
-
-                psFree(doneFrames);
-            }
-
-            if (!rawFrames->n > 0) {
-                psError(PS_ERR_UNKNOWN, false, "no unprocessed ppRawFrames found");
-                psFree(rawFrames);
-                exit(EXIT_FAILURE);
-            }
-
-            bool status = p2insertPendingFrames(&config, rawFrames);
-            if (!status) {
-                psError(PS_ERR_UNKNOWN, false,"p2insertPendingFrames() failed");
-                exit(EXIT_FAILURE);
-            }
-        }
-            break;
-        case P2_MODE_PENDING:
-        {
-            psArray *pendingFrames = p2searchPendingFrames(&config);
-            if (!pendingFrames) {
-                psError(PS_ERR_UNKNOWN, false, "no p2PendingFrames found");
-                exit(EXIT_FAILURE);
-            }
-            bool status = p2writePendingFrames(&config, pendingFrames);
-            if (!status) {
-                psError(PS_ERR_UNKNOWN, false,"p2insertPendingFrames() failed");
-                exit(EXIT_FAILURE);
-            }
-        }
             break;
     /*
@@ -115,21 +38,7 @@
     */
         case P2_MODE_DONE:
-        {
-            psArray *pendingFrames = p2searchPendingFrames(&config);
-            if (!pendingFrames) {
-                psError(PS_ERR_UNKNOWN, false, "no p2PendingFrames found");
+            if (!doneMode(&config)) {
                 exit(EXIT_FAILURE);
             }
-            psArray *doneFrames = p2pendingToDone(&config, pendingFrames);
-            if (!doneFrames) {
-                psError(PS_ERR_UNKNOWN, false, "p2pendingToDone() failed");
-                exit(EXIT_FAILURE);
-            }
-            bool status = p2insertDoneFrames(&config, doneFrames);
-            if (!status) {
-                psError(PS_ERR_UNKNOWN, false, "p2insertDoneFrames() failed");
-                exit(EXIT_FAILURE);
-            }
-        }
             break;
         default:
@@ -139,2 +48,126 @@
     exit(EXIT_SUCCESS);
 }
+
+static bool quickMode(p2Config *config)
+{
+    psArray *rawFrames = p2searchRawFrames(config);
+    if (!rawFrames) {
+        psError(PS_ERR_UNKNOWN, false, "no ppRawFrames found");
+        return false;
+    }
+    psArray *pendingFrames = p2rawToPending(config, rawFrames);
+    if (!pendingFrames) {
+        psError(PS_ERR_UNKNOWN, false, "no p2PendingFrames found");
+        return false;
+    }
+    bool status = p2writePendingFrames(config, pendingFrames);
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "p2writePendingFrames() failed");
+        return false;
+    }
+
+    return true;
+}
+
+static bool defineMode(p2Config *config)
+{
+    psArray *rawFrames = p2searchRawFrames(config);
+    if (!rawFrames) {
+        psError(PS_ERR_UNKNOWN, false, "no ppRawFrames found");
+        return false;
+    }
+    psArray *pendingFrames = p2searchPendingFrames(config);
+    // XXX compare raw frames to pending frames and remove duplicate 
+    // frames from the raw set.  This may not be quiet right as it's
+    // possible (likely?) that a rawScienceExp is inserted into the
+    // database prior to /ALL/ of that exposure's Imfiles
+    if (pendingFrames) {
+        for (int i = 0; i < rawFrames->n; i++) {
+            ppRawFrame *rawFrame = rawFrames->data[i];
+            for (int j = 0; j < pendingFrames->n; j++) {
+                p2PendingFrame *pendingFrame = pendingFrames->data[j];
+                if (strcmp(rawFrame->exposure->exp_id,
+                           pendingFrame->exposure->exp_id) == 0) {
+                    psArrayRemove(rawFrames, rawFrame);
+                    // dec the counter as the array just got shorter
+                    // and we don't want to skip elemnts
+                    i--;
+                    break;
+                } 
+            }
+        }
+
+        psFree(pendingFrames);
+    }
+
+    psArray *doneFrames = p2searchDoneFrames(config);
+    if (doneFrames && (rawFrames->n > 0)) {
+        for (int i = 0; i < rawFrames->n; i++) {
+            ppRawFrame *rawFrame = rawFrames->data[i];
+            for (int j = 0; j < pendingFrames->n; j++) {
+                p2DoneFrame *doneFrame = pendingFrames->data[j];
+                if (strcmp(rawFrame->exposure->exp_id,
+                           doneFrame->exposure->exp_id) == 0) {
+                    psArrayRemove(rawFrames, rawFrame);
+                    // dec the counter as the array just got shorter
+                    // and we don't want to skip elemnts
+                    i--;
+                    break;
+                } 
+            }
+        }
+
+        psFree(doneFrames);
+    }
+
+    if (!rawFrames->n > 0) {
+        psError(PS_ERR_UNKNOWN, false, "no unprocessed ppRawFrames found");
+        psFree(rawFrames);
+        return false;
+    }
+
+    bool status = p2insertPendingFrames(config, rawFrames);
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false,"p2insertPendingFrames() failed");
+        return false;
+    }
+
+    return true;
+}
+
+static bool pendingMode(p2Config *config)
+{
+    psArray *pendingFrames = p2searchPendingFrames(config);
+    if (!pendingFrames) {
+        psError(PS_ERR_UNKNOWN, false, "no p2PendingFrames found");
+        return false;
+    }
+    bool status = p2writePendingFrames(config, pendingFrames);
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false,"p2insertPendingFrames() failed");
+        return false;
+    }
+
+    return true;
+}
+
+static bool doneMode(p2Config *config)
+{
+    psArray *pendingFrames = p2searchPendingFrames(config);
+    if (!pendingFrames) {
+        psError(PS_ERR_UNKNOWN, false, "no p2PendingFrames found");
+        return false;
+    }
+    psArray *doneFrames = p2pendingToDone(config, pendingFrames);
+    if (!doneFrames) {
+        psError(PS_ERR_UNKNOWN, false, "p2pendingToDone() failed");
+        return false;
+    }
+    bool status = p2insertDoneFrames(config, doneFrames);
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "p2insertDoneFrames() failed");
+        return false;
+    }
+
+    return true;
+}
