Changes to the manifest loader to support the scheme format.
authorTom Balzer <niebie@localhost.localdomain>
Tue, 26 Jun 2018 11:26:43 +0000 (06:26 -0500)
committerTom Balzer <niebie@localhost.localdomain>
Tue, 26 Jun 2018 11:26:43 +0000 (06:26 -0500)
include/manifest.h [new file with mode: 0644]
src/common/guile_helpers.c

diff --git a/include/manifest.h b/include/manifest.h
new file mode 100644 (file)
index 0000000..17c6dba
--- /dev/null
@@ -0,0 +1,20 @@
+/* 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/>. */
+
+/* SUMMARY: */
+/* Load jobs from config file into tomd main memory. */
+void load_jobs(void);
index 8d3df8b..0984fdd 100644 (file)
 /* You should have received a copy of the GNU General Public License */
 /* along with tomd.  If not, see <http://www.gnu.org/licenses/>. */
 
-static load_job(void)
-{
+#include <libguile.h>
+#include <pwd.h>
+
+#include "../../include/macros.h"
 
+#define MANIFEST_LOC "/.config/tomd/init/manifest.scm"
+
+static void load_job(void)
+{
+  
 }
 
-static void load_manifest(void *args)
+static void *load_manifest(void *args)
 {
+  char *manifest_loc;
   if(!args){
-    
+    tomd_p("arg to load_manifest is NULL");
+    exit(EXIT_FAILURE);
+  }
+  manifest_loc = (char *)args;
+
+  /* tomd_p("load_manifest called with arg '%s'", args); */
+
+  scm_c_primitive_load(args);
+
+  /* now that we have the file in memory, lets grab the value of job-list */
+  SCM scm_job_list =
+    scm_c_public_ref("tomd manifest", "job-list");
+
+  if(scm_is_false(scm_job_list)){
+    tomd_p("no job-list found in manifest.scm");
+    return NULL;
+  }
+
+  if(scm_is_false(scm_list_p(scm_job_list))){
+    tomd_p("job-list found, but isn't a list.");
+    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;
+  for(i = 0;
+      i < len;
+      i++){
+    SCM scm_i =
+      scm_from_signed_integer(i);
+    SCM scm_cur_job =
+      scm_list_ref(scm_job_list, scm_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_list_ref(scm_args, scm_from_int(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]);
+    }
+  }
+  tomd_p("looked at %d jobs.", i);
+}
+
+static char *lookup_user_folder(void)
+{
+  uid_t user = getuid();
+  struct passwd *userpasswd =
+    getpwuid(user);
+  /* tomd_p("pw_name='%s'", userpasswd->pw_name); */
+  /* tomd_p("pw_passwd='%s'", userpasswd->pw_passwd); */
+  /* tomd_p("pw_uid='%d'", userpasswd->pw_uid); */
+  /* tomd_p("pw_gid='%d'", userpasswd->pw_gid); */
+  /* tomd_p("pw_gecos='%s'", userpasswd->pw_gecos); */
+  /* tomd_p("pw_dir='%s'", userpasswd->pw_dir); */
+  /* tomd_p("pw_shell='%s'", userpasswd->pw_shell); */
+
+  /* take the manifest location and sub ~ for pw_dir */
+  char *buffer = (char *)malloc(sizeof(char) * 300);
+  strcpy(buffer, userpasswd->pw_dir);
+  int len = strlen(userpasswd->pw_dir);
+  strcpy(buffer + len, MANIFEST_LOC);
+  /* tomd_p("full config path:'%s'", buffer); */
+  return buffer;
 }
 
 void load_jobs(void)
 {
+  char *manifest = lookup_user_folder();
+  tomd_p("Loading jobs from '%s'", manifest);
+  
   /* get into the manifest.scm file */
   void *res =
     scm_with_guile(load_manifest,
-                   (void *)config_file);
+                   (void *) manifest);
   /* check if the variable 'job-list' is defined */
+
+  tomd_p("Finished loading.");
 }