16 const char *wlan_interface;
17 const char *eth_interface;
18 const char *wmii_path;
19 const char *time_format;
20 const char *battery_path;
23 const char *wmii_normcolors = "#222222 #333333";
24 char order[MAX_ORDER][2];
25 const char **run_watches;
26 unsigned int num_run_watches;
27 unsigned int interval = 1;
29 void die(const char *fmt, ...);
32 * This function exists primarily for resolving ~ in pathnames. However, you
33 * can also specify ~/Movies/ *, which will only return the first match!
36 char *glob_path(const char *path) {
37 static glob_t globbuf;
38 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
40 char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
46 * Loads configuration from configfile
49 static void get_next_config_entry(FILE *handle, char **dest_name, char **dest_value, char *whole_buffer, int whole_buffer_size) {
51 if ((ret = fgets(whole_buffer, whole_buffer_size, handle)) == whole_buffer) {
52 char *c = whole_buffer;
53 /* Skip whitespaces in the beginning */
54 while (isspace((int)*c) && *c != '\0')
57 while (!isspace((int)*c))
59 /* Terminate string as soon as whitespaces begin or it's terminated anyway */
62 /* Same for the value: strip whitespaces */
63 while (isspace((int)*c) && *c != '\0')
66 /* Whitespace is allowed, newline/carriage return is not */
67 while ((*c != '\n') && (*c != '\r') && (*c != '\0'))
70 } else if (ret != NULL)
71 die("Could not read line in configuration file");
75 * Reads the configuration from the given file
78 int load_configuration(const char *configfile) {
79 #define OPT(x) else if (strcasecmp(dest_name, x) == 0)
81 /* Check if the file exists */
83 if (stat(configfile, &buf) < 0)
87 FILE *handle = fopen(configfile, "r");
89 die("Could not open configfile");
90 char *dest_name = NULL, *dest_value = NULL, whole_buffer[1026];
92 while (!feof(handle)) {
93 get_next_config_entry(handle, &dest_name, &dest_value, whole_buffer, 1024);
94 /* No more entries? We're done! */
95 if (dest_name == NULL)
97 /* Skip comments and empty lines */
98 if (dest_name[0] == '#' || strlen(dest_name) < 3)
103 wlan_interface = strdup(dest_value);
107 eth_interface = strdup(dest_value);
111 char *globbed = glob_path(dest_value);
112 if ((stat(globbed, &stbuf)) == -1)
113 die("wmii_path contains an invalid path");
114 if (globbed[strlen(globbed)-1] != '/')
115 die("wmii_path is not terminated by /");
120 time_format = strdup(dest_value);
124 if ((stat(dest_value, &stbuf)) == -1)
125 die("battery_path contains an invalid path");
126 battery_path = strdup(dest_value);
130 char *name = strdup(dest_value);
135 num_run_watches += 2;
136 run_watches = realloc(run_watches, sizeof(char*) * num_run_watches);
137 run_watches[num_run_watches-2] = name;
138 run_watches[num_run_watches-1] = path;
142 #define SET_ORDER(opt, idx) { if (strcasecmp(token, opt) == 0) sprintf(order[idx], "%d", c++); }
145 walk = token = dest_value;
146 while (*walk != '\0') {
147 while ((*walk != ',') && (*walk != '\0'))
150 SET_ORDER("run", ORDER_RUN);
151 SET_ORDER("wlan", ORDER_WLAN);
152 SET_ORDER("eth", ORDER_ETH);
153 SET_ORDER("battery", ORDER_BATTERY);
154 SET_ORDER("load", ORDER_LOAD);
155 SET_ORDER("time", ORDER_TIME);
157 while (isspace((int)(*token)))
171 wmii_normcolors = strdup(dest_value);
175 interval = atoi(dest_value);
180 die("Unknown configfile option: %s\n", dest_name);
182 dest_name = dest_value = NULL;
186 if (wmii_path == NULL)