10 * Reads the configuration from the given file
13 int load_configuration(const char *configfile) {
14 #define OPT(x) else if (strcasecmp(dest_name, x) == 0)
16 /* check if the file exists */
18 if (stat(configfile, &buf) < 0)
22 FILE *handle = fopen(configfile, "r");
24 die("Could not open configfile\n");
25 char dest_name[512], dest_value[512], whole_buffer[1026];
27 while (!feof(handle)) {
29 if ((ret = fgets(whole_buffer, 1024, handle)) == whole_buffer) {
30 /* sscanf implicitly strips whitespace */
31 if (sscanf(whole_buffer, "%s %[^\n]", dest_name, dest_value) < 1)
33 } else if (ret != NULL)
34 die("Could not read line in configuration file\n");
36 /* skip comments and empty lines */
37 if (dest_name[0] == '#' || strlen(dest_name) < 3)
41 wlan_interface = strdup(dest_value);
43 eth_interface = strdup(dest_value);
45 time_format = strdup(dest_value);
47 struct battery *new = calloc(1, sizeof(struct battery));
49 die("Could not allocate memory\n");
50 if (asprintf(&(new->path), "/sys/class/power_supply/BAT%d/uevent", atoi(dest_value)) == -1)
51 die("Could not build battery path\n");
53 /* check if flags were specified for this battery */
54 if (strstr(dest_value, ",") != NULL) {
55 char *flags = strstr(dest_value, ",");
58 new->use_last_full = true;
60 SIMPLEQ_INSERT_TAIL(&batteries, new, batteries);
65 OPT("get_cpu_temperature") {
66 get_cpu_temperature = true;
67 if (strlen(dest_value) > 0) {
68 if (asprintf(&thermal_zone, "/sys/class/thermal/thermal_zone%d/temp", atoi(dest_value)) == -1)
69 die("Could not build thermal_zone path\n");
71 if (asprintf(&thermal_zone, "/sys/class/thermal/thermal_zone0/temp") == -1)
72 die("Could not build thermal_zone path\n");
75 wmii_normcolors = strdup(dest_value);
77 interval = atoi(dest_value);
81 static glob_t globbuf;
83 if (glob(dest_value, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
84 die("glob() failed\n");
85 wmii_path = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : dest_value);
88 if ((stat(wmii_path, &stbuf)) == -1) {
89 fprintf(stderr, "Warning: wmii_path contains an invalid path\n");
91 wmii_path = strdup(dest_value);
93 if (wmii_path[strlen(wmii_path)-1] != '/')
94 die("wmii_path is not terminated by /\n");
99 char *name = strdup(dest_value);
104 num_run_watches += 2;
105 run_watches = realloc(run_watches, sizeof(char*) * num_run_watches);
106 run_watches[num_run_watches-2] = name;
107 run_watches[num_run_watches-1] = path;
111 #define SET_ORDER(opt, idx) { if (strcasecmp(token, opt) == 0) sprintf(order[idx], "%d", c++); }
114 walk = token = dest_value;
115 while (*walk != '\0') {
116 while ((*walk != ',') && (*walk != '\0'))
119 SET_ORDER("run", ORDER_RUN);
120 SET_ORDER("wlan", ORDER_WLAN);
121 SET_ORDER("eth", ORDER_ETH);
122 SET_ORDER("battery", ORDER_BATTERY);
123 SET_ORDER("cpu_temperature", ORDER_CPU_TEMPERATURE);
124 SET_ORDER("load", ORDER_LOAD);
125 SET_ORDER("time", ORDER_TIME);
127 while (isspace((int)(*token)))
134 die("Unknown configfile option: %s\n", dest_name);
140 if (wmii_path == NULL)