wmclockmon: update change-log
[dockapps.git] / wmclockmon / wmclockmon-config / tools.c
blobf481a7666dfb927bab28953fad06cb8dec17b268
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 time_mode;
21 char *msgcmd;
23 void alrm_add(Alarm **list, const char *value) {
24 Alarm *lst = *list;
25 int ok = TRUE;
26 char *time = NULL, *date = NULL, *ison = NULL, *mesg = NULL, *at;
27 char *tokstr;
28 char *toksav;
30 if (! value) return;
31 if (! lst) {
32 lst = xmalloc(sizeof(Alarm));
33 *list = lst;
34 } else {
35 if (strcmp(value, lst->entry) == 0) ok = FALSE;
36 while ( (lst->next) && ok) {
37 lst = lst->next;
38 if (strcmp(value, lst->entry) == 0) ok = FALSE;
40 if (! ok) return;
41 lst->next = xmalloc(sizeof(Alarm));
42 lst = lst->next;
44 toksav = tokstr = xstrdup(value);
45 at = strchr(value, '@');
46 if (at) ison = strtok(tokstr, "@");
47 time = strtok(at ? NULL : tokstr, "-.");
48 if (strchr(value, '-')) date = strtok(NULL, ".");
49 mesg = strtok(NULL, "\n\0");
50 lst->entry = xstrdup(value);
51 lst->time = time ? xstrdup(time) : NULL;
52 lst->date = date ? xstrdup(date) : NULL;
53 lst->on = ison ? getbool(ison) : TRUE;
54 lst->msg = mesg ? xstrdup(mesg) : NULL;
55 lst->next = NULL;
56 FREE(toksav);
60 void free_alrm(Alarm **list) {
61 Alarm *lst = *list, *next;
62 while (lst) {
63 next = lst->next;
64 FREE(lst->entry);
65 FREE(lst->time);
66 FREE(lst->date);
67 FREE(lst->msg);
68 free(lst);
69 lst = next;
71 *list = NULL;
75 void *xmalloc(size_t size) {
76 void *ret = malloc(size);
77 if (ret == NULL) {
78 perror("malloc() ");
79 exit(-1);
80 } else
81 return ret;
85 char *xstrdup(const char *string) {
86 char *ret = strdup(string);
87 if (ret == NULL) {
88 perror("strdup() ");
89 exit(-1);
90 } else
91 return ret;
95 int getbool(const char *value) {
96 if (strcmp(value, "0") == 0) return FALSE;
97 if (strcmp(value, "1") == 0) return TRUE;
98 if (strcasecmp(value, "true") == 0) return TRUE;
99 if (strcasecmp(value, "false") == 0) return FALSE;
100 if (strcasecmp(value, "yes") == 0) return TRUE;
101 if (strcasecmp(value, "no") == 0) return FALSE;
102 if (strcasecmp(value, "on") == 0) return TRUE;
103 if (strcasecmp(value, "off") == 0) return FALSE;
104 printf("Error in converting \"%s\" to boolean value.\n", value);
105 return FALSE;
109 void load_cfgfile(void) {
110 FILE *file;
111 int i = 0;
112 char line[MAXSTRLEN + 1];
113 char *value;
115 if ((file = fopen(config_file, "r")) == NULL) {
116 if (strstr(config_file, "/"DEFAULT_CFGFILE) == NULL)
117 printf("Unable to open configuration file \"%s\".\n", config_file);
118 return;
120 while (! feof(file)) {
121 size_t n;
123 if (!fgets(line, MAXSTRLEN, file))
124 break;
126 i++;
127 n = strlen(line);
128 if (n == 0)
129 continue;
130 if (line[n - 1] == '\n') line[n - 1] = 0;
131 if ((line[0] == '#') || (line[0] == 0)) continue;
132 value = strchr(line, '=') + 1;
133 while ((value[0] == ' ') && (value[0] != 0)) value++;
134 if (value[0] == 0) continue;
136 if (strncmp(line, "Backlight", 9) == 0)
137 backlight = getbool(value);
138 else if (strncmp(line, "Color", 5) == 0)
139 light_color = xstrdup(value);
140 else if (strncmp(line, "Alarm", 5) == 0)
141 alrm_add(&alarms, value);
142 else if (strncmp(line, "Command", 7) == 0)
143 command = xstrdup(value);
144 else if (strncmp(line, "MessageCmd", 10) == 0)
145 msgcmd = xstrdup(value);
146 else if (strncmp(line, "Blink", 5) == 0)
147 switch_authorized = getbool(value);
148 else if (strncmp(line, "H12", 3) == 0)
149 h12 = getbool(value);
150 else if (strncmp(line, "Locale", 6) == 0)
151 use_locale = getbool(value);
152 else if (strncmp(line, "StyleDir", 8) == 0)
153 style_dir = xstrdup(value);
154 else if (strncmp(line, "Style", 5) == 0)
155 style_name = xstrdup(value);
156 else if (strncmp(line, "TimeMode", 5) == 0)
157 time_mode = atoi(value);
158 else if (strncmp(line, "ShowCal", 7) == 0)
159 showcal = getbool(value);
160 else if (strncmp(line, "CalAlrms", 8) == 0)
161 calalrms = getbool(value);
162 else
163 printf("Error in %s at line %d :\n[%s].\n", config_file, i, line);
168 char *robust_home(void) {
169 if (getenv("HOME"))
170 return getenv("HOME");
171 else if (getenv("USER") && getpwnam(getenv("USER")))
172 return getpwnam(getenv ("USER"))->pw_dir;
173 else if (getenv("LOGNAME") && getpwnam(getenv("LOGNAME")))
174 return getpwnam(getenv("LOGNAME"))->pw_dir;
175 else if (getpwuid(getuid()))
176 return getpwuid(getuid())->pw_dir;
177 else
178 return "/";
182 void save_cfgfile(void) {
183 FILE *file;
184 Alarm *alrm = alarms;
186 if ((file = fopen(config_file, "w")) == NULL) {
187 if (strstr(config_file, "/"DEFAULT_CFGFILE) == NULL)
188 printf("Unable to open configuration file \"%s\".\n", config_file);
189 return;
192 fprintf(file, configfile,
193 backlight ? "On" : "Off",
194 light_color ? light_color : "", /*"#6ec63b",*/
195 command ? command : "",
196 msgcmd ? msgcmd : "",
197 switch_authorized ? "Yes" : "No",
198 h12 ? "True" : "False",
199 time_mode,
200 use_locale ? "Yes" : "No",
201 style_dir ? style_dir : "",
202 style_name ? style_name : "",
203 showcal ? "Yes" : "No",
204 calalrms ? "On" : "Off");
205 while (alrm) {
206 fprintf(file, alarmline,
207 alrm->on ? "On" : "Off",
208 alrm->time ? alrm->time : "12:00",
209 alrm->date ? "-" : "",
210 alrm->date ? alrm->date : "",
211 alrm->msg ? "." : "",
212 alrm->msg ? alrm->msg : "");
213 alrm = alrm->next;
215 fclose(file);