Index: trunk/psModules/src/config/pmConfigMask.c
===================================================================
--- trunk/psModules/src/config/pmConfigMask.c	(revision 21183)
+++ trunk/psModules/src/config/pmConfigMask.c	(revision 21363)
@@ -10,15 +10,15 @@
 
 static pmConfigMaskInfo masks[] = {
-    { "DETECTOR",  NULL,       0x00, true  },	// Something is wrong with the detector
-    { "DARK",      "DETECTOR", 0x00, true  },	// Pixel doesn't dark-subtract properly
-    { "FLAT",  	   "DETECTOR", 0x01, true  },	// Pixel doesn't flat-field properly
-    { "BLANK", 	   "DETECTOR", 0x01, true  },	// Pixel doesn't contain valid data
-    { "RANGE",     NULL,       0x00, true  },	// Pixel is out-of-range of linearity
-    { "SAT",       "RANGE",    0x01, true  },	// Pixel is saturated
-    { "BAD",       "RANGE",    0x01, true  },	// Pixel is low
-    { "BAD.WARP",  NULL,       0x01, true  },	// Pixel is bad after convolution with a bad pixel
-    { "CR",        NULL,       0x00, true  },	// Pixel contains a cosmic ray
-    { "GHOST",     NULL,       0x00, true  },	// Pixel contains an optical ghost
-    { "POOR.WARP", NULL,       0x00, false },	// Pixel is poor after convolution with a bad pixel
+    { "DETECTOR",  NULL,       0x00, true  },   // Something is wrong with the detector
+    { "DARK",      "DETECTOR", 0x00, true  },   // Pixel doesn't dark-subtract properly
+    { "FLAT",      "DETECTOR", 0x01, true  },   // Pixel doesn't flat-field properly
+    { "BLANK",     "DETECTOR", 0x01, true  },   // Pixel doesn't contain valid data
+    { "RANGE",     NULL,       0x00, true  },   // Pixel is out-of-range of linearity
+    { "SAT",       "RANGE",    0x01, true  },   // Pixel is saturated
+    { "BAD",       "RANGE",    0x01, true  },   // Pixel is low
+    { "BAD.WARP",  NULL,       0x01, true  },   // Pixel is bad after convolution with a bad pixel
+    { "CR",        NULL,       0x00, true  },   // Pixel contains a cosmic ray
+    { "GHOST",     NULL,       0x00, true  },   // Pixel contains an optical ghost
+    { "POOR.WARP", NULL,       0x00, false },   // Pixel is poor after convolution with a bad pixel
     // "LOW"  Pixel is low
     // "CONV" Pixel is bad after convolution with a bad pixel
@@ -31,5 +31,5 @@
 // bits in 8-bits of space).
 
-// XXX this file does not have psError vs psWarning worked out correctly.  some of the 
+// XXX this file does not have psError vs psWarning worked out correctly.  some of the
 // failure modes should result in errors, not just warnings.
 
@@ -40,13 +40,13 @@
 
 bool pmConfigMaskSetInMetadata(psImageMaskType *outMaskValue, // Value of MASK.VALUE, returned
-			       psImageMaskType *outMarkValue, // Value of MARK.VALUE, returned
-			       psMetadata *source  // Source of mask bits
+                               psImageMaskType *outMarkValue, // Value of MARK.VALUE, returned
+                               psMetadata *source  // Source of mask bits
     )
 {
     PS_ASSERT_METADATA_NON_NULL(source, false);
-    
+
     // Ensure all the bad mask names exist, and set the value to catch all bad pixels
     psImageMaskType maskValue = 0;           // Value to mask to catch all the bad pixels
-    psImageMaskType allMasks = 0;	     // Value to mask to catch all masked bits (to set MARK)
+    psImageMaskType allMasks = 0;            // Value to mask to catch all masked bits (to set MARK)
 
     int nMasks = sizeof (masks) / sizeof (pmConfigMaskInfo);
@@ -55,21 +55,21 @@
         bool mdok;                      // Status of MD lookup
         psImageMaskType value = psMetadataLookupImageMaskFromGeneric(&mdok, source, masks[i].badMaskName); // Value of mask
-	if (!mdok) {
-	    psWarning ("problem with mask value %s\n", masks[i].badMaskName);
-	}
+        if (!mdok) {
+            psWarning ("problem with mask value %s\n", masks[i].badMaskName);
+        }
 
         if (!value) {
-	    if (masks[i].fallbackName) {
-		value = psMetadataLookupImageMaskFromGeneric(&mdok, source, masks[i].fallbackName);
-	    }
-	    if (!value) {
-		value = masks[i].defaultMaskValue;
-	    }
-	    psMetadataAddImageMask(source, PS_LIST_TAIL, masks[i].badMaskName, PS_META_REPLACE, NULL, value);
-        }
-	if (masks[i].isBad) {
-	    maskValue |= value;
-	}
-	allMasks |= value;
+            if (masks[i].fallbackName) {
+                value = psMetadataLookupImageMaskFromGeneric(&mdok, source, masks[i].fallbackName);
+            }
+            if (!value) {
+                value = masks[i].defaultMaskValue;
+            }
+            psMetadataAddImageMask(source, PS_LIST_TAIL, masks[i].badMaskName, PS_META_REPLACE, NULL, value);
+        }
+        if (masks[i].isBad) {
+            maskValue |= value;
+        }
+        allMasks |= value;
     }
 
@@ -107,5 +107,5 @@
 // Get a mask value by name(s)
 psImageMaskType pmConfigMaskGetFromMetadata(psMetadata *source, // Source of masks
-					    const char *masks // Mask values to get
+                                            const char *masks // Mask values to get
     )
 {
@@ -139,66 +139,91 @@
 }
 
-// lookup an image mask value by name from a psMetadata, without requiring the entry to 
+// lookup an image mask value by name from a psMetadata, without requiring the entry to
 // be of type psImageMaskType, but verifying that it will fit in psImageMaskType
-psImageMaskType psMetadataLookupImageMaskFromGeneric (bool *status, const psMetadata *md, const char *name) {
-
+psImageMaskType psMetadataLookupImageMaskFromGeneric(bool *status, const psMetadata *md, const char *name)
+{
+    if (!md) {
+        psError(PS_ERR_UNEXPECTED_NULL, true, "Metadata is NULL.");
+        if (status) {
+            *status = false;
+        }
+        return 0;
+    }
+    if (!name) {
+        psError(PS_ERR_UNEXPECTED_NULL, true, "Keyword is NULL.");
+        if (status) {
+            *status = false;
+        }
+        return 0;
+    }
     *status = true;
 
-	// select the mask bit name from the header
-	psMetadataItem *item = psMetadataLookup (md, name);
-        if (!item) {
-            psWarning("Unable to find header keyword %s when parsing mask", name);
-	    *status = false;
-	    return 0;
-        }
-
-	// the value may be any of the U8, U16, U32, U64 types : accept the value regardless of type size
-	psU64 fullValue = 0;
-	switch (item->type) {
-	  case PS_DATA_U8:
-	    fullValue = item->data.U8;
-	    break;
-	  case PS_DATA_U16:
-	    fullValue = item->data.U16;
-	    break;
-	  case PS_DATA_U32:
-	    fullValue = item->data.U32;
-	    break;
-	  case PS_DATA_U64:
-	    fullValue = item->data.U64;
-	    break;
-	  case PS_DATA_S8:
-	    fullValue = item->data.S8;
-	    break;
-	  case PS_DATA_S16:
-	    fullValue = item->data.S16;
-	    break;
-	  case PS_DATA_S32:
-	    fullValue = item->data.S32;
-	    break;
-	  case PS_DATA_S64:
-	    fullValue = item->data.S64;
-	    break;
-	  default:
-            psWarning("Mask entry %s in metadata is not of a mask type", name);
-	    *status = false;
-	    return 0;
-	}
-
-	// will the incoming value fit within the current image mask type?
-	if (fullValue > PS_MAX_IMAGE_MASK_TYPE) {
-            psWarning("Mask entry %s in metadata is larger than allowed by the psImageMaskType", name);
-	    *status = false;
-	    return 0;
-        }
-	psImageMaskType value = fullValue;
-        // XXX validate that value is a 2^n value?
-
-	return value;
+    // select the mask bit name from the header
+    psMetadataItem *item = psMetadataLookup(md, name);
+    if (!item) {
+        if (status) {
+            *status = false;
+        } else {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, "Unable to find keyword %s when parsing mask", name);
+        }
+        return 0;
+    }
+
+    // the value may be any of the U8, U16, U32, U64 types : accept the value regardless of type size
+    psU64 fullValue = 0;
+    switch (item->type) {
+      case PS_DATA_U8:
+        fullValue = item->data.U8;
+        break;
+      case PS_DATA_U16:
+        fullValue = item->data.U16;
+        break;
+      case PS_DATA_U32:
+        fullValue = item->data.U32;
+        break;
+      case PS_DATA_U64:
+        fullValue = item->data.U64;
+        break;
+      case PS_DATA_S8:
+        fullValue = item->data.S8;
+        break;
+      case PS_DATA_S16:
+        fullValue = item->data.S16;
+        break;
+      case PS_DATA_S32:
+        fullValue = item->data.S32;
+        break;
+      case PS_DATA_S64:
+        fullValue = item->data.S64;
+        break;
+      default:
+        if (status) {
+            *status = false;
+        } else {
+            psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                    "Mask entry %s in metadata is not of a mask type", name);
+        }
+        return 0;
+    }
+
+    // will the incoming value fit within the current image mask type?
+    if (fullValue > PS_MAX_IMAGE_MASK_TYPE) {
+        if (status) {
+            *status = false;
+        } else {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                    "Mask entry %s in metadata is larger than allowed by the psImageMaskType", name);
+        }
+        return 0;
+    }
+    psImageMaskType value = fullValue;
+    // XXX validate that value is a 2^n value?
+
+    return value;
 }
 
 // Remove from the header keywords starting with the provided string
 int pmConfigMaskRemoveHeaderKeywords(psMetadata *header, // Header from which to remove keywords
-				     const char *start // Remove keywords that start with this string
+                                     const char *start // Remove keywords that start with this string
     )
 {
@@ -229,5 +254,5 @@
         return 0;
     }
-    
+
     psImageMaskType mask = pmConfigMaskGetFromMetadata (recipe, masks);
     return mask;
@@ -274,5 +299,5 @@
     }
 
-    // How many mask values do we need to read?  We raise an error if this is not found, 
+    // How many mask values do we need to read?  We raise an error if this is not found,
     // unless the MASK.FORCE is set to true in the camera config
     int nMask = psMetadataLookupS32(&status, header, "MSKNUM");
@@ -300,17 +325,17 @@
         }
 
-	psImageMaskType headerValue = psMetadataLookupImageMaskFromGeneric (&status, header, valuekey);
-	if (!status) {
+        psImageMaskType headerValue = psMetadataLookupImageMaskFromGeneric (&status, header, valuekey);
+        if (!status) {
             psWarning("Failed to get mask value %s from header, skipping", valuekey);
-	    continue;
-	}	    
-
-	// since we may read multiple mask files, we need to warn (or error?) if any of the
-	// header mask values conflict with other header mask values; However, the original
-	// mask values from the recipe do not need to match the header values.
-
-	// when we add a header mask value, we will also add the NAME.ALREADY entry; check for
-	// the NAME.ALREADY entry to see if we have previously added this mask value from a
-	// header.
+            continue;
+        }
+
+        // since we may read multiple mask files, we need to warn (or error?) if any of the
+        // header mask values conflict with other header mask values; However, the original
+        // mask values from the recipe do not need to match the header values.
+
+        // when we add a header mask value, we will also add the NAME.ALREADY entry; check for
+        // the NAME.ALREADY entry to see if we have previously added this mask value from a
+        // header.
 
         psString nameAlready = NULL;    // Name of key with ".ALREADY" added
@@ -318,16 +343,16 @@
         bool already = psMetadataLookupBool(&status, recipe, nameAlready); // Already read this one?
 
-	bool inRecipe = false;
-	psImageMaskType recipeValue = psMetadataLookupImageMaskFromGeneric (&inRecipe, recipe, name);
-	if (!inRecipe) {
+        bool inRecipe = false;
+        psImageMaskType recipeValue = psMetadataLookupImageMaskFromGeneric (&inRecipe, recipe, name);
+        if (!inRecipe) {
             psWarning("Mask value %s is not defined in the recipe", name);
-	}	    
+        }
 
         if (already) {
-	    assert (inRecipe); // XXX makes no sense for NAME.ALREADY to be in without NAME
+            assert (inRecipe); // XXX makes no sense for NAME.ALREADY to be in without NAME
             if (recipeValue != headerValue) {
                 psWarning("New mask header value does not match previously loaded entry: %x vs %x", headerValue, recipeValue);
-		psMetadataAddImageMask(recipe, PS_LIST_TAIL, name, PS_META_REPLACE, "Bitmask bit value", headerValue);
-		// XXX alternatively, error here
+                psMetadataAddImageMask(recipe, PS_LIST_TAIL, name, PS_META_REPLACE, "Bitmask bit value", headerValue);
+                // XXX alternatively, error here
             }
         } else {
@@ -369,27 +394,27 @@
     while ((item = psMetadataGetAndIncrement(iter))) {
 
-	// XXX this would give a false positive for mask which include '.ALREADY' in their names
-	char *ptr = strstr (item->name, ".ALREADY");
-	if (ptr) continue;
-
-	psU64 fullValue = 0;
+        // XXX this would give a false positive for mask which include '.ALREADY' in their names
+        char *ptr = strstr (item->name, ".ALREADY");
+        if (ptr) continue;
+
+        psU64 fullValue = 0;
         switch (item->type) {
-	  case PS_DATA_U8:
-	    fullValue = item->data.U8;
-	    break;
-	  case PS_DATA_U16:
-	    fullValue = item->data.U16;
-	    break;
-	  case PS_DATA_U32:
-	    fullValue = item->data.U32;
-	    break;
-	  case PS_DATA_U64:
-	    fullValue = item->data.U64;
-	    break;
-	  default:
+          case PS_DATA_U8:
+            fullValue = item->data.U8;
+            break;
+          case PS_DATA_U16:
+            fullValue = item->data.U16;
+            break;
+          case PS_DATA_U32:
+            fullValue = item->data.U32;
+            break;
+          case PS_DATA_U64:
+            fullValue = item->data.U64;
+            break;
+          default:
             psWarning("mask recipe entry %s is not a bit value\n", item->name);
             continue;
         }
-	assert (fullValue <= PS_MAX_IMAGE_MASK_TYPE); // this should have been asserted on read...
+        assert (fullValue <= PS_MAX_IMAGE_MASK_TYPE); // this should have been asserted on read...
 
         snprintf(namekey,  64, "MSKNAM%02d", nMask);
