0f2b4d18e47d44fe51b8f34e47dfe43e7ac5abe6
[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
23 #define MANIFEST_LOC "/.config/tomd/init/manifest.scm"
24
25 static void load_job(void)
26 {
27
28 }
29
30 static void *load_manifest(void *args)
31 {
32 char *manifest_loc;
33 if(!args){
34 tomd_p("arg to load_manifest is NULL");
35 exit(EXIT_FAILURE);
36 }
37 manifest_loc = (char *)args;
38
39 /* tomd_p("load_manifest called with arg '%s'", args); */
40
41 scm_c_primitive_load(args);
42
43 /* now that we have the file in memory, lets grab the value of job-list */
44 SCM scm_job_list =
45 scm_c_public_ref("tomd manifest", "job-list");
46
47 if(scm_is_false(scm_job_list)){
48 tomd_p("no job-list found in manifest.scm");
49 return NULL;
50 }
51
52 if(scm_is_false(scm_list_p(scm_job_list))){
53 tomd_p("job-list found, but isn't a list.");
54 return NULL;
55 }
56
57 SCM scm_job_predicate =
58 scm_c_public_ref("tomd job", "c-check-job");
59
60 SCM scm_job_cmd =
61 scm_c_public_ref("tomd job", "c-job-cmd");
62
63 SCM scm_job_args =
64 scm_c_public_ref("tomd job", "c-job-args");
65
66 SCM scm_job_start_trigger =
67 scm_c_public_ref("tomd job", "c-job-start-trigger");
68
69 SCM scm_job_end_trigger =
70 scm_c_public_ref("tomd job", "c-job-end-trigger");
71
72
73 SCM scm_len = scm_length(scm_job_list);
74 int len = scm_to_int(scm_len);
75 int i;
76 for(i = 0;
77 i < len;
78 i++){
79 SCM scm_i =
80 scm_from_signed_integer(i);
81 SCM scm_cur_job =
82 SCM_ARR(scm_job_list, i);
83
84 /* scm_cur_job should be a job */
85 if(scm_is_false(scm_call(scm_job_predicate, scm_cur_job, SCM_UNDEFINED))){
86 tomd_p("job %d wasn't a real job type.", i);
87 continue;
88 }
89
90 SCM scm_cmd =
91 scm_call(scm_job_cmd,
92 scm_cur_job,
93 SCM_UNDEFINED);
94 SCM scm_args =
95 scm_call(scm_job_args,
96 scm_cur_job,
97 SCM_UNDEFINED);
98 SCM scm_start_trigger =
99 scm_call(scm_job_start_trigger,
100 scm_cur_job,
101 SCM_UNDEFINED);
102 SCM scm_end_trigger =
103 scm_call(scm_job_end_trigger,
104 scm_cur_job,
105 SCM_UNDEFINED);
106
107 char *job_cmd =
108 scm_to_locale_string(scm_cmd);
109
110 char *real_args[10];
111 int jlen = scm_to_int(scm_length(scm_args));
112 for(int j = 0;
113 j < jlen;
114 j++){
115 real_args[j] =
116 scm_to_locale_string(SCM_ARR(scm_args, j));
117 }
118
119 tomd_p("JOB <%d>:", i);
120 tomd_p(" cmd:'%s'", job_cmd);
121 for(int j = 0;
122 j < jlen;
123 j++){
124 tomd_p(" arg [%d]:'%s'", j, real_args[j]);
125 }
126 }
127 tomd_p("looked at %d jobs.", i);
128 }
129
130 static char *lookup_user_folder(void)
131 {
132 uid_t user = getuid();
133 struct passwd *userpasswd =
134 getpwuid(user);
135 /* tomd_p("pw_name='%s'", userpasswd->pw_name); */
136 /* tomd_p("pw_passwd='%s'", userpasswd->pw_passwd); */
137 /* tomd_p("pw_uid='%d'", userpasswd->pw_uid); */
138 /* tomd_p("pw_gid='%d'", userpasswd->pw_gid); */
139 /* tomd_p("pw_gecos='%s'", userpasswd->pw_gecos); */
140 /* tomd_p("pw_dir='%s'", userpasswd->pw_dir); */
141 /* tomd_p("pw_shell='%s'", userpasswd->pw_shell); */
142
143 /* take the manifest location and sub ~ for pw_dir */
144 char *buffer = (char *)malloc(sizeof(char) * 300);
145 strcpy(buffer, userpasswd->pw_dir);
146 int len = strlen(userpasswd->pw_dir);
147 strcpy(buffer + len, MANIFEST_LOC);
148 /* tomd_p("full config path:'%s'", buffer); */
149 return buffer;
150 }
151
152 void load_jobs(void)
153 {
154 char *manifest = lookup_user_folder();
155 tomd_p("Loading jobs from '%s'", manifest);
156
157 /* get into the manifest.scm file */
158 void *res =
159 scm_with_guile(load_manifest,
160 (void *) manifest);
161 /* check if the variable 'job-list' is defined */
162
163 tomd_p("Finished loading.");
164 }