Dumb refactoring of manifest loading.
authorTom Balzer <niebieskitrociny@gmail.com>
Tue, 26 Jun 2018 12:55:29 +0000 (07:55 -0500)
committerTom Balzer <niebieskitrociny@gmail.com>
Tue, 26 Jun 2018 12:55:29 +0000 (07:55 -0500)
include/macros.h
include/scm_interface.h [new file with mode: 0644]
src/common/guile_helpers.c

index 9c9fa4e..aabab76 100644 (file)
@@ -3,4 +3,15 @@
 
 #define tomd_p(...) {printf("[tomd] "); printf(__VA_ARGS__); printf("\n");}
 #define SCM_ARR(arr, index) (scm_list_ref(arr, scm_from_int(index)))
+#define SCM_LIST_LEN(list) (scm_to_int(scm_length(list)))
+#define WRAP_SCM_FUNCTION_1(module, scm_name, name)    \
+SCM name(SCM obj) {                                  \
+  static SCM func_ref = SCM_UNDEFINED;               \
+  if(func_ref == SCM_UNDEFINED){                     \
+    func_ref = scm_c_public_ref(module, scm_name);   \
+  }                                                  \
+  return scm_call(func_ref, obj, SCM_UNDEFINED);     \
+}
+  
+  
 #endif
diff --git a/include/scm_interface.h b/include/scm_interface.h
new file mode 100644 (file)
index 0000000..17e6b78
--- /dev/null
@@ -0,0 +1,28 @@
+/* Copyright (C) 2018 Thomas Balzer */
+
+/* This file is part of tomd. */
+
+/* tomd 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, either version 3 of the License, or */
+/* (at your option) any later version. */
+
+/* tomd 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 tomd.  If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef _SCM_INTERFACE_
+#define _SCM_INTERFACE_
+#include "macros.h"
+
+WRAP_SCM_FUNCTION_1("tomd job", "c-check-job", job_predicate)
+WRAP_SCM_FUNCTION_1("tomd job", "c-job-cmd", get_cmd)
+WRAP_SCM_FUNCTION_1("tomd job", "c-job-args", get_args)
+WRAP_SCM_FUNCTION_1("tomd job", "c-job-start-trigger", get_start_trigger)
+WRAP_SCM_FUNCTION_1("tomd job", "c-job-end-trigger", get_end_trigger)
+
+#endif
index 0f2b4d1..e2bd14f 100644 (file)
 #include <pwd.h>
 
 #include "../../include/macros.h"
+#include "../../include/scm_interface.h"
 
 #define MANIFEST_LOC "/.config/tomd/init/manifest.scm"
 
-static void load_job(void)
+static void load_job(SCM job_list, int index)
 {
-  
+  SCM scm_i =
+    scm_from_signed_integer(index);
+  SCM scm_cur_job =
+    SCM_ARR(job_list, index);
+
+  /* scm_cur_job should be a job */    
+  if(scm_is_false(job_predicate(scm_cur_job))){
+    tomd_p("job %d wasn't a real job type.", index);
+    return;
+  }
+
+  SCM scm_cmd =
+    get_cmd(scm_cur_job);
+  SCM scm_args =
+    get_args(scm_cur_job);
+  SCM scm_start_trigger =
+    get_start_trigger(scm_cur_job);
+  SCM scm_end_trigger =
+    get_end_trigger(scm_cur_job);
+
+  char *job_cmd =
+    scm_to_locale_string(scm_cmd);
+
+  char *real_args[10];
+  int jlen = scm_to_int(scm_length(scm_args));
+  for(int j = 0;
+      j < jlen;
+      j++){
+    real_args[j] =
+      scm_to_locale_string(SCM_ARR(scm_args, j));
+  }
+
+  tomd_p("JOB <%d>:", index);
+  tomd_p("     cmd:'%s'", job_cmd);
+  for(int j = 0;
+      j < jlen;
+      j++){
+    tomd_p(" arg [%d]:'%s'", j, real_args[j]);
+  }
 }
 
 static void *load_manifest(void *args)
@@ -54,75 +93,12 @@ static void *load_manifest(void *args)
     return NULL;
   }
 
-  SCM scm_job_predicate =
-    scm_c_public_ref("tomd job", "c-check-job");
-
-  SCM scm_job_cmd =
-    scm_c_public_ref("tomd job", "c-job-cmd");
-
-  SCM scm_job_args =
-    scm_c_public_ref("tomd job", "c-job-args");
-
-  SCM scm_job_start_trigger =
-    scm_c_public_ref("tomd job", "c-job-start-trigger");
-
-  SCM scm_job_end_trigger =
-    scm_c_public_ref("tomd job", "c-job-end-trigger");
-
-  
-  SCM scm_len = scm_length(scm_job_list);
-  int len = scm_to_int(scm_len);
   int i;
+  int len = SCM_LIST_LEN(scm_job_list);
   for(i = 0;
       i < len;
       i++){
-    SCM scm_i =
-      scm_from_signed_integer(i);
-    SCM scm_cur_job =
-      SCM_ARR(scm_job_list, i);
-
-    /* scm_cur_job should be a job */    
-    if(scm_is_false(scm_call(scm_job_predicate, scm_cur_job, SCM_UNDEFINED))){
-      tomd_p("job %d wasn't a real job type.", i);
-      continue;
-    }
-
-    SCM scm_cmd =
-      scm_call(scm_job_cmd,
-               scm_cur_job,
-               SCM_UNDEFINED);
-    SCM scm_args =
-      scm_call(scm_job_args,
-               scm_cur_job,
-               SCM_UNDEFINED);
-    SCM scm_start_trigger =
-      scm_call(scm_job_start_trigger,
-               scm_cur_job,
-               SCM_UNDEFINED);
-    SCM scm_end_trigger =
-      scm_call(scm_job_end_trigger,
-               scm_cur_job,
-               SCM_UNDEFINED);
-
-    char *job_cmd =
-      scm_to_locale_string(scm_cmd);
-
-    char *real_args[10];
-    int jlen = scm_to_int(scm_length(scm_args));
-    for(int j = 0;
-        j < jlen;
-        j++){
-      real_args[j] =
-        scm_to_locale_string(SCM_ARR(scm_args, j));
-    }
-
-    tomd_p("JOB <%d>:", i);
-    tomd_p("     cmd:'%s'", job_cmd);
-    for(int j = 0;
-        j < jlen;
-        j++){
-      tomd_p(" arg [%d]:'%s'", j, real_args[j]);
-    }
+    load_job(scm_job_list, i);
   }
   tomd_p("looked at %d jobs.", i);
 }