wmclockmon: update `getbool` not to modify its argument
[dockapps.git] / wmclockmon / wmclockmon-config / tools.c
blob5d88642def26bf15ea9d5ad240a0889faef9b600
1 /*
2 * Tools : memory management, file loading and saving
3 */
5 #include "../config.h"
6 #include "defines.h"
7 #include "variables.h"
8 #include "tools.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <sys/types.h>
14 #include <ctype.h>
15 #include <pwd.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include "configfile.h"
20 int free_light_color;
21 int free_command;
22 int time_mode;
23 char *msgcmd;
25 void alrm_add(Alarm **list, const char *value) {
26 Alarm *lst = *list;
27 int ok = TRUE;
28 char *time = NULL, *date = NULL, *ison = NULL, *mesg = NULL, *at;
29 char *tokstr;
30 char *toksav;
32 if (! value) return;
33 if (! lst) {
34 lst = xmalloc(sizeof(Alarm));
35 *list = lst;
36 } else {
37 if (strcmp(value, lst->entry) == 0) ok = FALSE;
38 while ( (lst->next) && ok) {
39 lst = lst->next;
40 if (strcmp(value, lst->entry) == 0) ok = FALSE;
42 if (! ok) return;
43 lst->next = xmalloc(sizeof(Alarm));
44 lst = lst->next;
46 toksav = tokstr = xstrdup(value);
47 at = strchr(value, '@');
48 if (at) ison = strtok(tokstr, "@");
49 time = strtok(at ? NULL : tokstr, "-.");
50 if (strchr(value, '-')) date = strtok(NULL, ".");
51 mesg = strtok(NULL, "\n\0");
52 lst->entry = xstrdup(value);
53 lst->time = time ? xstrdup(time) : NULL;
54 lst->date = date ? xstrdup(date) : NULL;
55 lst->on = ison ? getbool(ison) : TRUE;
56 lst->msg = mesg ? xstrdup(mesg) : NULL;
57 lst->next = NULL;
58 FREE(toksav);
62 void free_alrm(Alarm **list) {
63 Alarm *lst = *list, *next;
64 while (lst) {
65 next = lst->next;
66 FREE(lst->entry);
67 FREE(lst->time);
68 FREE(lst->date);
69 FREE(lst->msg);
70 free(lst);
71 lst = next;
73 *list = NULL;
77 void *xmalloc(size_t size) {
78 void *ret = malloc(size);
79 if (ret == NULL) {
80 perror("malloc() ");
81 exit(-1);
82 } else
83 return ret;
87 char *xstrdup(const char *string) {
88 char *ret = strdup(string);
89 if (ret == NULL) {
90 perror("strdup() ");
91 exit(-1);
92 } else
93 return ret;
97 int getbool(const char *value) {
98 if (strcmp(value, "0") == 0) return FALSE;
99 if (strcmp(value, "1") == 0) return TRUE;
100 if (strcasecmp(value, "true") == 0) return TRUE;
101 if (strcasecmp(value, "false") == 0) return FALSE;
102 if (strcasecmp(value, "yes") == 0) return TRUE;
103 if (strcasecmp(value, "no") == 0) return FALSE;
104 if (strcasecmp(value, "on") == 0) return TRUE;
105 if (strcasecmp(value, "off") == 0) return FALSE;
106 printf("Error in converting \"%s\" to boolean value.\n", value);
107 return FALSE;
111 void load_cfgfile() {
112 FILE *file;
113 int i = 0;
114 char line[MAXSTRLEN + 1];
115 char *value;
117 if ((file = fopen(config_file, "r")) == NULL) {
118 if (strstr(config_file, "/"DEFAULT_CFGFILE) == NULL)
119 printf("Unable to open configuration file \"%s\".\n", config_file);
120 return;
122 free_light_color = free_command = 0;
123 while (! feof(file)) {
124 memset(line, 0, MAXSTRLEN + 1);
125 fgets(line, MAXSTRLEN, file);
126 i++;
127 if (line[strlen(line) - 1] == '\n') line[strlen(line) - 1] = 0;
128 if ((line[0] == '#') || (line[0] == 0)) continue;
129 value = strchr(line, '=') + 1;
130 while ((value[0] == ' ') && (value[0] != 0)) value++;
131 if (value[0] == 0) continue;
133 if (strncmp(line, "Backlight", 9) == 0)
134 backlight = getbool(value);
135 else if (strncmp(line, "Color", 5) == 0) {
136 light_color = xstrdup(value);
137 free_light_color = 1;
138 } else if (strncmp(line, "Alarm", 5) == 0)
139 alrm_add(&alarms, value);
140 else if (strncmp(line, "Command", 7) == 0) {
141 command = xstrdup(value);
142 free_command = 1;
143 } else if (strncmp(line, "MessageCmd", 10) == 0)
144 msgcmd = xstrdup(value);
145 else if (strncmp(line, "Blink", 5) == 0)
146 switch_authorized = getbool(value);
147 else if (strncmp(line, "H12", 3) == 0)
148 h12 = getbool(value);
149 else if (strncmp(line, "Locale", 6) == 0)
150 use_locale = getbool(value);
151 else if (strncmp(line, "StyleDir", 8) == 0)
152 style_dir = xstrdup(value);
153 else if (strncmp(line, "Style", 5) == 0)
154 style_name = xstrdup(value);
155 else if (strncmp(line, "TimeMode", 5) == 0)
156 time_mode = atoi(value);
157 else if (strncmp(line, "ShowCal", 7) == 0)
158 showcal = getbool(value);
159 else if (strncmp(line, "CalAlrms", 8) == 0)
160 calalrms = getbool(value);
161 else
162 printf("Error in %s at line %d :\n[%s].\n", config_file, i, line);
167 char *robust_home() {
168 if (getenv("HOME"))
169 return getenv("HOME");
170 else if (getenv("USER") && getpwnam(getenv("USER")))
171 return getpwnam (getenv ("USER") )->pw_dir;
172 else if (getenv ("LOGNAME") && getpwnam(getenv("LOGNAME")))
173 return getpwnam(getenv("LOGNAME"))->pw_dir;
174 else if ((getuid() != -1) && (getpwuid(getuid())))
175 return getpwuid(getuid())->pw_dir;
176 else
177 return "/";
181 void save_cfgfile() {
182 FILE *file;
183 Alarm *alrm = alarms;
185 if ((file = fopen(config_file, "w")) == NULL) {
186 if (strstr(config_file, "/"DEFAULT_CFGFILE) == NULL)
187 printf("Unable to open configuration file \"%s\".\n", config_file);
188 return;
191 fprintf(file, configfile,
192 backlight ? "On" : "Off",
193 light_color ? light_color : "", /*"#6ec63b",*/
194 command ? command : "",
195 msgcmd ? msgcmd : "",
196 switch_authorized ? "Yes" : "No",
197 h12 ? "True" : "False",
198 time_mode,
199 use_locale ? "Yes" : "No",
200 style_dir ? style_dir : "",
201 style_name ? style_name : "",
202 showcal ? "Yes" : "No",
203 calalrms ? "On" : "Off");
204 while (alrm) {
205 fprintf(file, alarmline,
206 alrm->on ? "On" : "Off",
207 alrm->time ? alrm->time : "12:00",
208 alrm->date ? "-" : "",
209 alrm->date ? alrm->date : "",
210 alrm->msg ? "." : "",
211 alrm->msg ? alrm->msg : "");
212 alrm = alrm->next;
214 fclose(file);