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