This repository has been archived by the owner on Nov 28, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserconfig.c
173 lines (163 loc) · 5.01 KB
/
userconfig.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* userconfig.c - find, read and parse user configuration file
*
* Copyright (C) 2017 Pochang Chen
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "userconfig.h"
#include <basedir_fs.h>
#include <confuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "die.h"
#include "tasks.h"
static char *get_config_path(const char *const_config_path);
static int parse_time(const char *s, struct timespec *t);
static int config_validate_time(cfg_t *cfg, cfg_opt_t *opt);
static int config_validate_task(cfg_t *cfg, cfg_opt_t *opt);
static cfg_opt_t task_opts[] = {
CFG_STR("time", "600s", CFGF_NONE),
CFG_STR("command", NULL, CFGF_NODEFAULT),
CFG_END()
};
static cfg_opt_t opts[] = {
CFG_SEC("task", task_opts, CFGF_MULTI | CFGF_TITLE),
CFG_END()
};
cfg_t *read_config(const char *const_config_file) {
cfg_t *config = cfg_init(opts, CFGF_NONE);
cfg_set_validate_func(config, "task|time", config_validate_time);
cfg_set_validate_func(config, "task", config_validate_task);
char *config_file = get_config_path(const_config_file);
if(*config_file == '\0') {
fprintf(stderr,
"No configuration file found.\n"
"Using default configuration instead.\n");
free(config_file);
return config;
}
switch(cfg_parse(config, config_file)) {
case CFG_FILE_ERROR:
fprintf(stderr,
"Configuration file (%s) cannot be opened for reading.\n"
"Using default configuration instead.\n",
config_file);
break;
case CFG_PARSE_ERROR:
die("Failed to parse configuration file.\n");
}
free(config_file);
return config;
}
unsigned get_tasks(cfg_t *config, struct Task **tasks_ptr) {
unsigned n = cfg_size(config, "task");
*tasks_ptr = calloc(n, sizeof(struct Task));
for(unsigned i = 0; i < n; i++) {
cfg_t *task = cfg_getnsec(config, "task", i);
(*tasks_ptr)[i].name = cfg_title(task);
parse_time(cfg_getstr(task, "time"), &(*tasks_ptr)[i].time);
(*tasks_ptr)[i].command = cfg_getstr(task, "command");
}
return n;
}
// if const_config_path is not NULL, return a freeable copy
// otherwise return default configuration path
static char *get_config_path(const char *const_config_path) {
if(const_config_path)
return strdup(const_config_path);
return xdgConfigFind("jautolock/config", NULL);
}
// parse time duration of format \(\d+\(d\|h\|m\|s\|ms\|ns\)\)+
static int parse_time(const char *s, struct timespec *t) {
if(!*s)
return -1;
t->tv_sec = t->tv_nsec = 0;
while(*s) {
char *ep;
long x = strtol(s, &ep, 10);
if(ep == s)
return -1;
s = ep;
if(!*s)
return -1;
switch(*s) {
case 'd':
t->tv_sec += x * 86400;
s++;
break;
case 'h':
t->tv_sec += x * 3600;
s++;
break;
case 'm':
if(s[1] == 's') {
t->tv_nsec += x * ((int) 1e6);
s += 2;
break;
}
t->tv_sec += x * 60;
s++;
break;
case 's':
t->tv_sec += x;
s++;
break;
case 'n':
if(s[1] != 's')
return -1;
t->tv_nsec += x;
s += 2;
break;
default:
return -1;
}
}
t->tv_sec += t->tv_nsec / ((int) 1e9);
t->tv_nsec %= ((int) 1e9);
if(t->tv_nsec < 0) {
t->tv_sec--;
t->tv_nsec += ((int) 1e9);
}
return 0;
}
// validate the time option (must be positive)
static int config_validate_time(cfg_t *cfg, cfg_opt_t *opt) {
const char *s = cfg_opt_getnstr(opt, 0);
struct timespec t;
if(parse_time(s, &t) < 0) {
cfg_error(cfg, "bad time format");
return -1;
}
if(t.tv_sec < 0) {
cfg_error(cfg, "negative time");
return -1;
}
if(t.tv_sec == 0 && t.tv_nsec == 0) {
cfg_error(cfg, "zero time");
return -1;
}
return 0;
}
// validate the task section (command option required)
static int config_validate_task(cfg_t *cfg, cfg_opt_t *opt) {
cfg_t *task = cfg_opt_getnsec(opt, cfg_opt_size(opt) - 1);
if (cfg_size(task, "command") == 0) {
cfg_error(cfg, "missing required option 'command' in task");
return -1;
}
return 0;
}