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