]> git.sur5r.net Git - i3/i3status/blob - src/config.c
Correctly handle the order of items
[i3/i3status] / src / config.c
1 #include <sys/stat.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <ctype.h>
6
7 #include "i3status.h"
8
9 /*
10  * Reads the configuration from the given file
11  *
12  */
13 int load_configuration(const char *configfile) {
14         #define OPT(x) else if (strcasecmp(dest_name, x) == 0)
15
16         /* check if the file exists */
17         struct stat buf;
18         if (stat(configfile, &buf) < 0)
19                 return -1;
20
21         int result = 0;
22         FILE *handle = fopen(configfile, "r");
23         if (handle == NULL)
24                 die("Could not open configfile\n");
25         char dest_name[512], dest_value[512], whole_buffer[1026];
26
27         while (!feof(handle)) {
28                 char *ret;
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)
32                                 continue;
33                 } else if (ret != NULL)
34                         die("Could not read line in configuration file\n");
35
36                 /* skip comments and empty lines */
37                 if (dest_name[0] == '#' || strlen(dest_name) < 3)
38                         continue;
39
40                 OPT("wlan")
41                         wlan_interface = strdup(dest_value);
42                 OPT("eth")
43                         eth_interface = strdup(dest_value);
44                 OPT("time_format")
45                         time_format = strdup(dest_value);
46                 OPT("battery") {
47                         struct battery *new = calloc(1, sizeof(struct battery));
48                         if (new == NULL)
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");
52
53                         /* check if flags were specified for this battery */
54                         if (strstr(dest_value, ",") != NULL) {
55                                 char *flags = strstr(dest_value, ",");
56                                 flags++;
57                                 if (*flags == 'f')
58                                         new->use_last_full = true;
59                         }
60                         SIMPLEQ_INSERT_TAIL(&batteries, new, batteries);
61                 } OPT("color")
62                         use_colors = true;
63                 OPT("get_ipv6")
64                         get_ipv6 = true;
65                 OPT("get_ethspeed")
66                         get_ethspeed = true;
67                 OPT("get_cpu_temperature") {
68                         get_cpu_temperature = true;
69                         if (strlen(dest_value) > 0) {
70                                 if (asprintf(&thermal_zone, "/sys/class/thermal/thermal_zone%d/temp", atoi(dest_value)) == -1)
71                                         die("Could not build thermal_zone path\n");
72                         } else {
73                                  if (asprintf(&thermal_zone, "/sys/class/thermal/thermal_zone0/temp") == -1)
74                                         die("Could not build thermal_zone path\n");
75                         }
76                 } OPT("normcolors")
77                         wmii_normcolors = strdup(dest_value);
78                 OPT("interval")
79                         interval = atoi(dest_value);
80                 OPT("wmii_path")
81                 {
82 #ifndef DZEN
83                         static glob_t globbuf;
84                         struct stat stbuf;
85                         if (glob(dest_value, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
86                                 die("glob() failed\n");
87                         wmii_path = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : dest_value);
88                         globfree(&globbuf);
89
90                         if ((stat(wmii_path, &stbuf)) == -1) {
91                                 fprintf(stderr, "Warning: wmii_path contains an invalid path\n");
92                                 free(wmii_path);
93                                 wmii_path = strdup(dest_value);
94                         }
95                         if (wmii_path[strlen(wmii_path)-1] != '/')
96                                 die("wmii_path is not terminated by /\n");
97 #endif
98                 }
99                 OPT("run_watch")
100                 {
101                         char *name = strdup(dest_value);
102                         char *path = name;
103                         while (*path != ' ')
104                                 path++;
105                         *(path++) = '\0';
106                         num_run_watches += 2;
107                         run_watches = realloc(run_watches, sizeof(char*) * num_run_watches);
108                         run_watches[num_run_watches-2] = name;
109                         run_watches[num_run_watches-1] = path;
110                 }
111                 OPT("order")
112                 {
113                         #define SET_ORDER(opt, idx) { if (strcasecmp(token, opt) == 0) order[idx] = c++; }
114                         char *walk, *token;
115                         int c = 0;
116                         walk = token = dest_value;
117                         while (*walk != '\0') {
118                                 while ((*walk != ',') && (*walk != '\0'))
119                                         walk++;
120                                 *(walk++) = '\0';
121                                 SET_ORDER("run", ORDER_RUN);
122                                 SET_ORDER("ipv6", ORDER_IPV6);
123                                 SET_ORDER("wlan", ORDER_WLAN);
124                                 SET_ORDER("eth", ORDER_ETH);
125                                 SET_ORDER("battery", ORDER_BATTERY);
126                                 SET_ORDER("cpu_temperature", ORDER_CPU_TEMPERATURE);
127                                 SET_ORDER("load", ORDER_LOAD);
128                                 SET_ORDER("time", ORDER_TIME);
129                                 token = walk;
130                                 while (isspace((int)(*token)))
131                                         token++;
132                         }
133                 }
134                 else
135                 {
136                         result = -2;
137                         die("Unknown configfile option: %s\n", dest_name);
138                 }
139         }
140         fclose(handle);
141
142 #ifndef DZEN
143         if (wmii_path == NULL)
144                 exit(EXIT_FAILURE);
145 #endif
146
147         return result;
148 }