Corrected file name issue.
[tlb/tomd.git] / src / common / guile_helpers.c
CommitLineData
4f839c09
TB
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
82ff7d81
TB
18#include <libguile.h>
19#include <pwd.h>
20
21#include "../../include/macros.h"
3024ed65 22#include "../../include/scm_interface.h"
81643445 23
82ff7d81
TB
24#define MANIFEST_LOC "/.config/tomd/init/manifest.scm"
25
3024ed65 26static void load_job(SCM job_list, int index)
82ff7d81 27{
64694f43 28 SCM scm_cur_job = SCM_ARR(job_list, index);
3024ed65 29
3024ed65
TB
30 if(scm_is_false(job_predicate(scm_cur_job))){
31 tomd_p("job %d wasn't a real job type.", index);
32 return;
33 }
34
64694f43
TB
35 SCM scm_cmd = get_cmd(scm_cur_job);
36 SCM scm_args = get_args(scm_cur_job);
3024ed65 37
64694f43
TB
38 /* TODO > Handle these. */
39 /* planned is to be able to run at different points, and at */
40 /* the request of a user. for testing we do things all at */
41 /* boot up. */
42 SCM scm_start_trigger = get_start_trigger(scm_cur_job);
43 SCM scm_end_trigger = get_end_trigger(scm_cur_job);
44
45 char *job_cmd = scm_to_locale_string(scm_cmd);
3024ed65
TB
46
47 char *real_args[10];
48 int jlen = scm_to_int(scm_length(scm_args));
49 for(int j = 0;
50 j < jlen;
51 j++){
52 real_args[j] =
53 scm_to_locale_string(SCM_ARR(scm_args, j));
54 }
55
56 tomd_p("JOB <%d>:", index);
57 tomd_p(" cmd:'%s'", job_cmd);
58 for(int j = 0;
59 j < jlen;
60 j++){
61 tomd_p(" arg [%d]:'%s'", j, real_args[j]);
62 }
81643445
TB
63}
64
82ff7d81 65static void *load_manifest(void *args)
81643445 66{
82ff7d81 67 char *manifest_loc;
81643445 68 if(!args){
82ff7d81
TB
69 tomd_p("arg to load_manifest is NULL");
70 exit(EXIT_FAILURE);
71 }
72 manifest_loc = (char *)args;
73
82ff7d81
TB
74 scm_c_primitive_load(args);
75
82ff7d81
TB
76 SCM scm_job_list =
77 scm_c_public_ref("tomd manifest", "job-list");
78
79 if(scm_is_false(scm_job_list)){
80 tomd_p("no job-list found in manifest.scm");
81 return NULL;
82 }
83
84 if(scm_is_false(scm_list_p(scm_job_list))){
85 tomd_p("job-list found, but isn't a list.");
86 return NULL;
87 }
88
82ff7d81 89 int i;
3024ed65 90 int len = SCM_LIST_LEN(scm_job_list);
82ff7d81
TB
91 for(i = 0;
92 i < len;
93 i++){
3024ed65 94 load_job(scm_job_list, i);
82ff7d81
TB
95 }
96 tomd_p("looked at %d jobs.", i);
97}
98
99static char *lookup_user_folder(void)
100{
101 uid_t user = getuid();
102 struct passwd *userpasswd =
103 getpwuid(user);
82ff7d81
TB
104
105 /* take the manifest location and sub ~ for pw_dir */
106 char *buffer = (char *)malloc(sizeof(char) * 300);
107 strcpy(buffer, userpasswd->pw_dir);
108 int len = strlen(userpasswd->pw_dir);
109 strcpy(buffer + len, MANIFEST_LOC);
82ff7d81 110 return buffer;
81643445
TB
111}
112
113void load_jobs(void)
114{
82ff7d81
TB
115 char *manifest = lookup_user_folder();
116 tomd_p("Loading jobs from '%s'", manifest);
117
81643445
TB
118 /* get into the manifest.scm file */
119 void *res =
120 scm_with_guile(load_manifest,
82ff7d81 121 (void *) manifest);
82ff7d81
TB
122
123 tomd_p("Finished loading.");
81643445 124}