From a2c92b161cccb78adee5899de69cb4c196dec6f2 Mon Sep 17 00:00:00 2001
From: Dinyar Rabady <dinyar.rabady@cern.ch>
Date: Sat, 24 Sep 2022 13:09:50 +0200
Subject: [PATCH] Quit more gracefully if configuration incomplete

Belongs to #15.
---
 etc/scdaq/scdaq.conf |  6 +++---
 scripts/runSCdaq.sh  |  4 ++++
 src/config.cc        | 10 ++++++++--
 src/config.h         |  9 ++++++---
 src/scdaq.cc         |  5 +++++
 5 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/etc/scdaq/scdaq.conf b/etc/scdaq/scdaq.conf
index 63fabdf3..36f37f4f 100644
--- a/etc/scdaq/scdaq.conf
+++ b/etc/scdaq/scdaq.conf
@@ -59,7 +59,7 @@ enable_stream_processor:yes
 # Note: When changing the processing type, change also "output_filename_prefix"
 # in the file output section.
 #
-#processor_type:GMT
+processor_type:
 
 # Enable software zero-supression
 doZS:yes
@@ -70,9 +70,9 @@ doZS:yes
 ##
 ################################################################################
 
-#output_filename_prefix:scout_GMT
+output_filename_prefix:
 
-#output_filename_base:/fff/BU0/ramdisk/scdaq
+output_filename_base:
 
 max_file_size:8589934592
 
diff --git a/scripts/runSCdaq.sh b/scripts/runSCdaq.sh
index ef52faf6..a247598e 100755
--- a/scripts/runSCdaq.sh
+++ b/scripts/runSCdaq.sh
@@ -15,6 +15,10 @@ while true
 do
     echo "Starting scdaq..."
     /opt/scdaq/bin/scdaq --config /etc/scdaq/scdaq.conf 2>&1
+    if [[ ${PIPESTATUS[0]} == 78 ]]; then # Numerical value of EX_CONFIG.
+      # If the configuration is incomplete there's no point in us retrying forever..
+      exit 1
+    fi
     echo "Resetting the board..."
     ../scripts/reset-firmware.sh
     echo "Clearing caches..."
diff --git a/src/config.cc b/src/config.cc
index 5e3a1482..80627a54 100644
--- a/src/config.cc
+++ b/src/config.cc
@@ -31,12 +31,15 @@ config::config(std::string filename){
     std::string key = line.substr(0, delim);
     std::string value = line.substr(delim + 1);
 
-    if (key.empty() || value.empty()) {
+    if (key.empty()) {
       // Skip ill formated lines
        continue;      
     }
 
-    //std::cout << "key: " << key << ", value: " << value << "\n";
+    if (value.empty()) {
+      LOG(ERROR) << "Configuration entry " << key << " has no value!";
+      valid_ &= false;
+    }
 
     vmap[key] = value;
   }
@@ -45,5 +48,8 @@ void config::print()const {
   for (std::map<std::string,std::string>::const_iterator it = vmap.begin(); it!=vmap.end(); it++){
     LOG(INFO) << "key " << it->first << " value " << it->second;
   }
+}
 
+bool config::valid() const {
+  return valid_;
 }
diff --git a/src/config.h b/src/config.h
index e25680e5..c488d75f 100644
--- a/src/config.h
+++ b/src/config.h
@@ -10,13 +10,15 @@
 
 class config{
 public:
-  
+
   enum class InputType { WZDMA, DMA, FILEDMA, MICRONDMA, FILE };
 
   config(std::string filename);
 
   void print() const;
 
+  bool valid() const;
+
   InputType getInput() const {
     const std::string& input = vmap.at("input");
     if (input == "wzdma") {
@@ -144,9 +146,10 @@ public:
     return boost::lexical_cast<uint32_t>(v.c_str());
   }
 private:
-  
+
   std::map<std::string,std::string> vmap;
-  
+  bool valid_ = true;
+
 
 };
 #endif
diff --git a/src/scdaq.cc b/src/scdaq.cc
index 21f4c28a..58d4ac13 100644
--- a/src/scdaq.cc
+++ b/src/scdaq.cc
@@ -9,6 +9,7 @@
 #include <cctype>
 #include <string>
 #include <iostream>
+#include <sysexits.h>
 
 #include <boost/bind.hpp>
 #include <boost/asio.hpp>
@@ -147,6 +148,10 @@ if(argc < 2){
   try {
     config conf(argv[2]);
     conf.print();
+    if (!conf.valid()) {
+      LOG(ERROR) << "Configuration invalid! Bailing out.";
+      return EX_CONFIG;
+    }
     LOG(DEBUG) << "Configuration loaded";
     ctrl control;
     //    tbb::tick_count mainStartTime = tbb::tick_count::now();
-- 
GitLab