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