Dumb refactoring of manifest loading.
[tlb/tomd.git] / src / common / guile_helpers.c
1 /* Copyright (C) 2018 Thomas Balzer */
2
3 /* This file is part of tomd. */
4
5 /* tomd is free software: you can redistribute it and/or modify */
6 /* it under the terms of the GNU General Public License as published by */
7 /* the Free Software Foundation, either version 3 of the License, or */
8 /* (at your option) any later version. */
9
10 /* tomd is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU General Public License for more details. */
14
15 /* You should have received a copy of the GNU General Public License */
16 /* along with tomd. If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <libguile.h>
19 #include <pwd.h>
20
21 #include "../../include/macros.h"
22 #include "../../include/scm_interface.h"
23
24 #define MANIFEST_LOC "/.config/tomd/init/manifest.scm"
25
26 static void load_job(SCM job_list, int index)
27 {
28 SCM scm_i =
29 scm_from_signed_integer(index);
30 SCM scm_cur_job =
31 SCM_ARR(job_list, index);
32
33 /* scm_cur_job should be a job */
34 if(scm_is_false(job_predicate(scm_cur_job))){
35 tomd_p("job %d wasn't a real job type.", index);
36 return;
37 }
38
39 SCM scm_cmd =
40 get_cmd(scm_cur_job);
41 SCM scm_args =
42 get_args(scm_cur_job);
43 SCM scm_start_trigger =
44 get_start_trigger(scm_cur_job);
45 SCM scm_end_trigger =
46 get_end_trigger(scm_cur_job);
47
48 char *job_cmd =
49 scm_to_locale_string(scm_cmd);
50
51 char *real_args[10];
52 int jlen = scm_to_int(scm_length(scm_args));
53 for(int j = 0;
54 j < jlen;
55 j++){
56 real_args[j] =
57 scm_to_locale_string(SCM_ARR(scm_args, j));
58 }
59
60 tomd_p("JOB <%d>:", index);
61 tomd_p(" cmd:'%s'", job_cmd);
62 for(int j = 0;
63 j < jlen;
64 j++){
65 tomd_p(" arg [%d]:'%s'", j, real_args[j]);
66 }
67 }
68
69 static void *load_manifest(void *args)
70 {
71 char *manifest_loc;
72 if(!args){
73 tomd_p("arg to load_manifest is NULL");
74 exit(EXIT_FAILURE);
75 }
76 manifest_loc = (char *)args;
77
78 /* tomd_p("load_manifest called with arg '%s'", args); */
79
80 scm_c_primitive_load(args);
81
82 /* now that we have the file in memory, lets grab the value of job-list */
83 SCM scm_job_list =
84 scm_c_public_ref("tomd manifest", "job-list");
85
86 if(scm_is_false(scm_job_list)){
87 tomd_p("no job-list found in manifest.scm");
88 return NULL;
89 }
90
91 if(scm_is_false(scm_list_p(scm_job_list))){
92 tomd_p("job-list found, but isn't a list.");
93 return NULL;
94 }
95
96 int i;
97 int len = SCM_LIST_LEN(scm_job_list);
98 for(i = 0;
99 i < len;
100 i++){
101 load_job(scm_job_list, i);
102 }
103 tomd_p("looked at %d jobs.", i);
104 }
105
106 static char *lookup_user_folder(void)
107 {
108 uid_t user = getuid();
109 struct passwd *userpasswd =
110 getpwuid(user);
111 /* tomd_p("pw_name='%s'", userpasswd->pw_name); */
112 /* tomd_p("pw_passwd='%s'", userpasswd->pw_passwd); */
113 /* tomd_p("pw_uid='%d'", userpasswd->pw_uid); */
114 /* tomd_p("pw_gid='%d'", userpasswd->pw_gid); */
115 /* tomd_p("pw_gecos='%s'", userpasswd->pw_gecos); */
116 /* tomd_p("pw_dir='%s'", userpasswd->pw_dir); */
117 /* tomd_p("pw_shell='%s'", userpasswd->pw_shell); */
118
119 /* take the manifest location and sub ~ for pw_dir */
120 char *buffer = (char *)malloc(sizeof(char) * 300);
121 strcpy(buffer, userpasswd->pw_dir);
122 int len = strlen(userpasswd->pw_dir);
123 strcpy(buffer + len, MANIFEST_LOC);
124 /* tomd_p("full config path:'%s'", buffer); */
125 return buffer;
126 }
127
128 void load_jobs(void)
129 {
130 char *manifest = lookup_user_folder();
131 tomd_p("Loading jobs from '%s'", manifest);
132
133 /* get into the manifest.scm file */
134 void *res =
135 scm_with_guile(load_manifest,
136 (void *) manifest);
137 /* check if the variable 'job-list' is defined */
138
139 tomd_p("Finished loading.");
140 }