Browse Source

Re-factor to make tests compile again

Davin McCall 1 year ago
parent
commit
924045ebc7
9 changed files with 120 additions and 116 deletions
  1. 1 1
      src/Makefile
  2. 1 0
      src/control.cc
  3. 106 0
      src/dinit-env.cc
  4. 0 101
      src/dinit.cc
  5. 10 1
      src/includes/dinit-env.h
  6. 0 7
      src/includes/dinit.h
  7. 1 1
      src/tests/Makefile
  8. 1 1
      src/tests/cptests/Makefile
  9. 0 4
      src/tests/test-includes/dinit.h

+ 1 - 1
src/Makefile

@@ -8,7 +8,7 @@ ifeq ($(BUILD_SHUTDOWN),yes)
 endif
 
 dinit_objects = dinit.o load-service.o service.o proc-service.o baseproc-service.o control.o dinit-log.o \
-		dinit-main.o run-child-proc.o options-processing.o
+		dinit-main.o run-child-proc.o options-processing.o dinit-env.o
 
 objects = $(dinit_objects) dinitctl.o dinitcheck.o shutdown.o dinit-monitor.o
 

+ 1 - 0
src/control.cc

@@ -2,6 +2,7 @@
 #include <unordered_set>
 #include <climits>
 
+#include "dinit-env.h"
 #include "control.h"
 #include "service.h"
 #include "proc-service.h"

+ 106 - 0
src/dinit-env.cc

@@ -0,0 +1,106 @@
+#include <iostream>
+#include <fstream>
+
+#include "dinit-log.h"
+#include "dinit-env.h"
+
+environment main_env;
+
+// Log a parse error when reading the environment file.
+static void log_bad_env(int linenum)
+{
+    log(loglevel_t::ERROR, "Invalid environment variable setting in environment file (line ", linenum, ")");
+}
+
+static void log_bad_env_cmd(int linenum)
+{
+    log(loglevel_t::ERROR, "Unknown command in environment file (line ", linenum, ")");
+}
+
+// Read and set environment variables from a file. May throw std::bad_alloc, std::system_error.
+void read_env_file(const char *env_file_path, bool log_warnings, environment &env)
+{
+    std::ifstream env_file(env_file_path);
+    if (! env_file) return;
+
+    env_file.exceptions(std::ios::badbit);
+
+    auto &clocale = std::locale::classic();
+    std::string line;
+    int linenum = 0;
+
+    while (std::getline(env_file, line)) {
+        linenum++;
+        auto lpos = line.begin();
+        auto lend = line.end();
+        while (lpos != lend && std::isspace(*lpos, clocale)) {
+            ++lpos;
+        }
+
+        if (lpos == lend) continue; // empty line
+
+        if (*lpos == '#') {
+            continue;
+        }
+        if (*lpos == '=') {
+            if (log_warnings) {
+                log_bad_env(linenum);
+            }
+            continue;
+        }
+
+        // "!COMMAND" form.
+        if (*lpos == '!') {
+            ++lpos; // lpos = first char of command
+            auto epos = lpos;
+            do {
+                ++epos;
+            } while(epos != lend && !std::isspace(*epos, clocale));
+
+            const char *lpos_p = line.data() + (lpos - line.begin());
+            string_view cmd {lpos_p, (size_t)(epos - lpos)};
+
+            if (cmd == "clear") {
+                env.clear_no_inherit();
+            }
+            else if (log_warnings) {
+                log_bad_env_cmd(linenum);
+            }
+
+            continue;
+        }
+
+        // ENV=VALUE form.
+        auto name_begin = lpos++;
+        // skip until '=' or whitespace:
+        while (lpos != lend && *lpos != '=' && !std::isspace(*lpos, clocale)) ++lpos;
+        auto name_end = lpos;
+        //  skip whitespace:
+        while (lpos != lend && std::isspace(*lpos, clocale)) ++lpos;
+        if (lpos == lend || *lpos != '=') {
+            if (log_warnings) {
+                log_bad_env(linenum);
+            }
+            continue;
+        }
+
+        ++lpos;
+        auto val_begin = lpos;
+        while (lpos != lend && *lpos != '\n') ++lpos;
+        auto val_end = lpos;
+
+        if (val_begin != (name_end + 1) || val_begin != line.begin() || name_end != lend) {
+            // there are spaces that we need to eliminate
+            std::string name_and_val;
+            name_and_val.reserve((name_end - name_begin) + 1 + (val_end - val_begin));
+            name_and_val = line.substr(name_begin - line.begin(), name_end - name_begin);
+            name_and_val.append(1, '=');
+            name_and_val.append(val_begin, val_end);
+            env.set_var(std::move(name_and_val));
+        }
+        else {
+            line.shrink_to_fit();
+            env.set_var(std::move(line));
+        }
+    }
+}

+ 0 - 101
src/dinit.cc

@@ -56,8 +56,6 @@ using eventloop_t = dasynq::event_loop<dasynq::null_mutex>;
 
 eventloop_t event_loop(dasynq::delayed_init {});
 
-environment main_env;
-
 static void sigint_reboot_cb(eventloop_t &eloop) noexcept;
 static void sigquit_cb(eventloop_t &eloop) noexcept;
 static void sigterm_cb(eventloop_t &eloop) noexcept;
@@ -688,105 +686,6 @@ int dinit_main(int argc, char **argv)
     return 0;
 }
 
-// Log a parse error when reading the environment file.
-static void log_bad_env(int linenum)
-{
-    log(loglevel_t::ERROR, "Invalid environment variable setting in environment file (line ", linenum, ")");
-}
-
-static void log_bad_env_cmd(int linenum)
-{
-    log(loglevel_t::ERROR, "Unknown command in environment file (line ", linenum, ")");
-}
-
-// Read and set environment variables from a file. May throw std::bad_alloc, std::system_error.
-void read_env_file(const char *env_file_path, bool log_warnings, environment &env)
-{
-    std::ifstream env_file(env_file_path);
-    if (! env_file) return;
-
-    env_file.exceptions(std::ios::badbit);
-
-    auto &clocale = std::locale::classic();
-    std::string line;
-    int linenum = 0;
-
-    while (std::getline(env_file, line)) {
-        linenum++;
-        auto lpos = line.begin();
-        auto lend = line.end();
-        while (lpos != lend && std::isspace(*lpos, clocale)) {
-            ++lpos;
-        }
-
-        if (lpos == lend) continue; // empty line
-
-        if (*lpos == '#') {
-            continue;
-        }
-        if (*lpos == '=') {
-            if (log_warnings) {
-                log_bad_env(linenum);
-            }
-            continue;
-        }
-
-        // "!COMMAND" form.
-        if (*lpos == '!') {
-            ++lpos; // lpos = first char of command
-            auto epos = lpos;
-            do {
-                ++epos;
-            } while(epos != lend && !std::isspace(*epos, clocale));
-
-            const char *lpos_p = line.data() + (lpos - line.begin());
-            string_view cmd {lpos_p, (size_t)(epos - lpos)};
-
-            if (cmd == "clear") {
-                env.clear_no_inherit();
-            }
-            else if (log_warnings) {
-                log_bad_env_cmd(linenum);
-            }
-
-            continue;
-        }
-
-        // ENV=VALUE form.
-        auto name_begin = lpos++;
-        // skip until '=' or whitespace:
-        while (lpos != lend && *lpos != '=' && !std::isspace(*lpos, clocale)) ++lpos;
-        auto name_end = lpos;
-        //  skip whitespace:
-        while (lpos != lend && std::isspace(*lpos, clocale)) ++lpos;
-        if (lpos == lend || *lpos != '=') {
-            if (log_warnings) {
-                log_bad_env(linenum);
-            }
-            continue;
-        }
-
-        ++lpos;
-        auto val_begin = lpos;
-        while (lpos != lend && *lpos != '\n') ++lpos;
-        auto val_end = lpos;
-
-        if (val_begin != (name_end + 1) || val_begin != line.begin() || name_end != lend) {
-            // there are spaces that we need to eliminate
-            std::string name_and_val;
-            name_and_val.reserve((name_end - name_begin) + 1 + (val_end - val_begin));
-            name_and_val = line.substr(name_begin - line.begin(), name_end - name_begin);
-            name_and_val.append(1, '=');
-            name_and_val.append(val_begin, val_end);
-            env.set_var(std::move(name_and_val));
-        }
-        else {
-            line.shrink_to_fit();
-            env.set_var(std::move(line));
-        }
-    }
-}
-
 // Get user confirmation before proceeding with restarting boot sequence.
 // Returns after confirmation, possibly with shutdown type altered.
 static void confirm_restart_boot() noexcept

+ 10 - 1
src/includes/dinit-env.h

@@ -1,13 +1,22 @@
 #ifndef DINIT_ENV_H_INCLUDED
 #define DINIT_ENV_H_INCLUDED 1
 
-#include <unordered_set>
+#include <unordered_map>
 #include <string>
 
 #include "dinit-util.h"
 
 extern char **environ;
 
+class environment;
+extern environment main_env;
+
+// Read an environment file and set variables in the current environment.
+//   file - the file to read
+//   log_warnings - if true, syntactic errors are logged
+// May throw bad_alloc or system_error.
+void read_env_file(const char *file, bool log_warnings, environment &env);
+
 // Hash environment variable name only (not including value)
 struct hash_env_name
 {

+ 0 - 7
src/includes/dinit.h

@@ -18,13 +18,6 @@ class environment;
 void rootfs_is_rw() noexcept;
 void setup_external_log() noexcept;
 
-// Read an environment file and set variables in the current environment.
-//   file - the file to read
-//   log_warnings - if true, syntactic errors are logged
-// May throw bad_alloc or system_error.
-void read_env_file(const char *file, bool log_warnings, environment &env);
-
 extern eventloop_t event_loop;
-extern environment main_env;
 
 #endif

+ 1 - 1
src/tests/Makefile

@@ -1,7 +1,7 @@
 -include ../../mconfig
 
 objects = tests.o test-dinit.o proctests.o loadtests.o test-run-child-proc.o test-bpsys.o
-parent_objs = service.o proc-service.o dinit-log.o load-service.o baseproc-service.o
+parent_objs = service.o proc-service.o dinit-log.o load-service.o baseproc-service.o dinit-env.o
 
 check: build-tests run-tests
 

+ 1 - 1
src/tests/cptests/Makefile

@@ -2,7 +2,7 @@
 
 objects = cptests.o
 parent_test_objects = ../test-bpsys.o ../test-dinit.o
-parent_objs = control.o dinit-log.o service.o load-service.o proc-service.o baseproc-service.o run-child-proc.o
+parent_objs = control.o dinit-log.o service.o load-service.o proc-service.o baseproc-service.o run-child-proc.o dinit-env.o
 
 check: build-tests run-tests
 

+ 0 - 4
src/tests/test-includes/dinit.h

@@ -231,10 +231,6 @@ inline void setup_external_log() noexcept
 {
 }
 
-inline void read_env_file(const char *env_file_path, bool do_log)
-{
-}
-
 extern eventloop_t event_loop;
 
 #endif