Move configuration file parsing into wmiistatus.c, saves a lot of hassle/SLOC/files

This commit is contained in:
Michael Stapelberg 2009-01-25 23:18:25 +01:00
parent 2abe135288
commit bbd9e7f85d
5 changed files with 152 additions and 211 deletions

View File

@ -1,4 +1,4 @@
CFLAGS+=-Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wshadow -Wpointer-arith -Wcast-qual -Wsign-compare
CFLAGS+=-Wall -Wshadow -Wpointer-arith -Wcast-qual -Wsign-compare
CFLAGS+=-g
CFLAGS+=-DPREFIX=\"\"
@ -6,7 +6,7 @@ ifeq ($(shell uname),Linux)
CFLAGS+=-DLINUX
endif
wmiistatus: wmiistatus.o wmiistatus.h config.h config.o
wmiistatus: wmiistatus.o wmiistatus.h
clean:
rm -f *.o
@ -24,7 +24,6 @@ install:
install -m 644 wmiistatus.1 $(DESTDIR)/usr/share/man/man1
release:
tar cf wmiistatus.tar *.c *.h *.1 *.conf *.init Makefile
bzip2 -9 wmiistatus.tar
tar cjf wmiistatus.tar.bz2 *.c *.h *.1 *.conf *.init Makefile
all: wmiistatus

139
config.c
View File

@ -1,139 +0,0 @@
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <glob.h>
#include <unistd.h>
#define _IS_CONFIG_C
#include "config.h"
#undef _IS_CONFIG_C
const char *wlan_interface;
const char *eth_interface;
char *wmii_path;
const char *time_format;
const char *battery_path;
bool use_colors;
bool get_ethspeed;
const char *wmii_normcolors = "#222222 #333333";
char order[MAX_ORDER][2];
const char **run_watches;
unsigned int num_run_watches;
unsigned int interval = 1;
void die(const char *fmt, ...);
/*
* Reads the configuration from the given file
*
*/
int load_configuration(const char *configfile) {
#define OPT(x) else if (strcasecmp(dest_name, x) == 0)
/* check if the file exists */
struct stat buf;
if (stat(configfile, &buf) < 0)
return -1;
int result = 0;
FILE *handle = fopen(configfile, "r");
if (handle == NULL)
die("Could not open configfile");
char dest_name[512], dest_value[512], whole_buffer[1026];
struct stat stbuf;
while (!feof(handle)) {
char *ret;
if ((ret = fgets(whole_buffer, 1024, handle)) == whole_buffer) {
/* sscanf implicitly strips whitespace */
if (sscanf(whole_buffer, "%s %[^\n]", dest_name, dest_value) < 1)
continue;
} else if (ret != NULL)
die("Could not read line in configuration file");
/* skip comments and empty lines */
if (dest_name[0] == '#' || strlen(dest_name) < 3)
continue;
OPT("wlan")
wlan_interface = strdup(dest_value);
OPT("eth")
eth_interface = strdup(dest_value);
OPT("time_format")
time_format = strdup(dest_value);
OPT("battery_path")
battery_path = strdup(dest_value);
OPT("color")
use_colors = true;
OPT("get_ethspeed")
get_ethspeed = true;
OPT("normcolors")
wmii_normcolors = strdup(dest_value);
OPT("interval")
interval = atoi(dest_value);
OPT("wmii_path")
{
static glob_t globbuf;
if (glob(dest_value, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
die("glob() failed");
wmii_path = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : dest_value);
globfree(&globbuf);
if ((stat(wmii_path, &stbuf)) == -1) {
fprintf(stderr, "Warning: wmii_path contains an invalid path\n");
free(wmii_path);
wmii_path = strdup(dest_value);
}
if (wmii_path[strlen(wmii_path)-1] != '/')
die("wmii_path is not terminated by /");
}
OPT("run_watch")
{
char *name = strdup(dest_value);
char *path = name;
while (*path != ' ')
path++;
*(path++) = '\0';
num_run_watches += 2;
run_watches = realloc(run_watches, sizeof(char*) * num_run_watches);
run_watches[num_run_watches-2] = name;
run_watches[num_run_watches-1] = path;
}
OPT("order")
{
#define SET_ORDER(opt, idx) { if (strcasecmp(token, opt) == 0) sprintf(order[idx], "%d", c++); }
char *walk, *token;
int c = 0;
walk = token = dest_value;
while (*walk != '\0') {
while ((*walk != ',') && (*walk != '\0'))
walk++;
*(walk++) = '\0';
SET_ORDER("run", ORDER_RUN);
SET_ORDER("wlan", ORDER_WLAN);
SET_ORDER("eth", ORDER_ETH);
SET_ORDER("battery", ORDER_BATTERY);
SET_ORDER("load", ORDER_LOAD);
SET_ORDER("time", ORDER_TIME);
token = walk;
while (isspace((int)(*token)))
token++;
}
}
else
{
result = -2;
die("Unknown configfile option: %s\n", dest_name);
}
}
fclose(handle);
if (wmii_path == NULL)
exit(EXIT_FAILURE);
return result;
}

View File

@ -1,27 +0,0 @@
#include <stdbool.h>
#ifndef _CONFIG_H
#define _CONFIG_H
enum { ORDER_RUN, ORDER_WLAN, ORDER_ETH, ORDER_BATTERY, ORDER_LOAD, ORDER_TIME, MAX_ORDER };
#ifndef _IS_CONFIG_C /* Definitions for everybody */
extern const char *wlan_interface;
extern const char *eth_interface;
extern const char *wmii_path;
extern const char *time_format;
extern const char *battery_path;
extern const char **run_watches;
extern unsigned int num_run_watches;
extern const char *wmii_normcolors;
extern const char order[MAX_ORDER][2];
extern unsigned int interval;
extern bool use_colors;
extern bool get_ethspeed;
#endif
char *glob_path(const char *path);
int load_configuration(const char *configfile);
#endif

View File

@ -65,11 +65,23 @@
#define _IS_WMIISTATUS_C
#include "wmiistatus.h"
#undef _IS_WMIISTATUS_C
#include "config.h"
/* socket file descriptor for general purposes */
static int general_socket;
static const char *wlan_interface;
static const char *eth_interface;
static char *wmii_path;
static const char *time_format;
static const char *battery_path;
static bool use_colors;
static bool get_ethspeed;
static const char *wmii_normcolors = "#222222 #333333";
static char order[MAX_ORDER][2];
static const char **run_watches;
static unsigned int num_run_watches;
static unsigned int interval = 1;
/*
* This function just concats two strings in place, it should only be used
* for concatting order to the name of a file or concatting color codes.
@ -287,6 +299,31 @@ static char *get_battery_info() {
return part;
}
/*
* Return the IP address for the given interface or "no IP" if the
* interface is up and running but hasn't got an IP address yet
*
*/
static const char *get_ip_address(const char *interface) {
static char part[512];
struct ifreq ifr;
struct sockaddr_in addr;
socklen_t len = sizeof(struct sockaddr_in);
memset(part, 0, sizeof(part));
(void)strcpy(ifr.ifr_name, interface);
ifr.ifr_addr.sa_family = AF_INET;
if (ioctl(general_socket, SIOCGIFADDR, &ifr) < 0)
return NULL;
memcpy(&addr, &ifr.ifr_addr, len);
(void)inet_ntop(AF_INET, &addr.sin_addr.s_addr, part, len);
if (strlen(part) == 0)
(void)snprintf(part, sizeof(part), "no IP");
return part;
}
/*
* Just parses /proc/net/wireless looking for lines beginning with
* wlan_interface, extracting the quality of the link and adding the
@ -328,31 +365,6 @@ static char *get_wireless_info() {
return part;
}
/*
* Return the IP address for the given interface or "no IP" if the
* interface is up and running but hasn't got an IP address yet
*
*/
static const char *get_ip_address(const char *interface) {
static char part[512];
struct ifreq ifr;
struct sockaddr_in addr;
socklen_t len = sizeof(struct sockaddr_in);
memset(part, 0, sizeof(part));
(void)strcpy(ifr.ifr_name, interface);
ifr.ifr_addr.sa_family = AF_INET;
if (ioctl(general_socket, SIOCGIFADDR, &ifr) < 0)
return NULL;
memcpy(&addr, &ifr.ifr_addr, len);
(void)inet_ntop(AF_INET, &addr.sin_addr.s_addr, part, len);
if (strlen(part) == 0)
(void)snprintf(part, sizeof(part), "no IP");
return part;
}
/*
* Combines ethernet IP addresses and speed (if requested) for displaying
*
@ -426,6 +438,116 @@ static bool process_runs(const char *path) {
#endif
}
/*
* Reads the configuration from the given file
*
*/
static int load_configuration(const char *configfile) {
#define OPT(x) else if (strcasecmp(dest_name, x) == 0)
/* check if the file exists */
struct stat buf;
if (stat(configfile, &buf) < 0)
return -1;
int result = 0;
FILE *handle = fopen(configfile, "r");
if (handle == NULL)
die("Could not open configfile\n");
char dest_name[512], dest_value[512], whole_buffer[1026];
struct stat stbuf;
while (!feof(handle)) {
char *ret;
if ((ret = fgets(whole_buffer, 1024, handle)) == whole_buffer) {
/* sscanf implicitly strips whitespace */
if (sscanf(whole_buffer, "%s %[^\n]", dest_name, dest_value) < 1)
continue;
} else if (ret != NULL)
die("Could not read line in configuration file\n");
/* skip comments and empty lines */
if (dest_name[0] == '#' || strlen(dest_name) < 3)
continue;
OPT("wlan")
wlan_interface = strdup(dest_value);
OPT("eth")
eth_interface = strdup(dest_value);
OPT("time_format")
time_format = strdup(dest_value);
OPT("battery_path")
battery_path = strdup(dest_value);
OPT("color")
use_colors = true;
OPT("get_ethspeed")
get_ethspeed = true;
OPT("normcolors")
wmii_normcolors = strdup(dest_value);
OPT("interval")
interval = atoi(dest_value);
OPT("wmii_path")
{
static glob_t globbuf;
if (glob(dest_value, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
die("glob() failed\n");
wmii_path = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : dest_value);
globfree(&globbuf);
if ((stat(wmii_path, &stbuf)) == -1) {
fprintf(stderr, "Warning: wmii_path contains an invalid path\n");
free(wmii_path);
wmii_path = strdup(dest_value);
}
if (wmii_path[strlen(wmii_path)-1] != '/')
die("wmii_path is not terminated by /\n");
}
OPT("run_watch")
{
char *name = strdup(dest_value);
char *path = name;
while (*path != ' ')
path++;
*(path++) = '\0';
num_run_watches += 2;
run_watches = realloc(run_watches, sizeof(char*) * num_run_watches);
run_watches[num_run_watches-2] = name;
run_watches[num_run_watches-1] = path;
}
OPT("order")
{
#define SET_ORDER(opt, idx) { if (strcasecmp(token, opt) == 0) sprintf(order[idx], "%d", c++); }
char *walk, *token;
int c = 0;
walk = token = dest_value;
while (*walk != '\0') {
while ((*walk != ',') && (*walk != '\0'))
walk++;
*(walk++) = '\0';
SET_ORDER("run", ORDER_RUN);
SET_ORDER("wlan", ORDER_WLAN);
SET_ORDER("eth", ORDER_ETH);
SET_ORDER("battery", ORDER_BATTERY);
SET_ORDER("load", ORDER_LOAD);
SET_ORDER("time", ORDER_TIME);
token = walk;
while (isspace((int)(*token)))
token++;
}
}
else
{
result = -2;
die("Unknown configfile option: %s\n", dest_name);
}
}
fclose(handle);
if (wmii_path == NULL)
exit(EXIT_FAILURE);
return result;
}
int main(int argc, char *argv[]) {
char part[512],
pathbuf[512];

View File

@ -1,18 +1,4 @@
#define BEGINS_WITH(haystack, needle) (strncmp(haystack, needle, strlen(needle)) == 0)
typedef enum { CS_DISCHARGING, CS_CHARGING, CS_FULL } charging_status_t;
#ifdef _IS_WMIISTATUS_C
static char *concat(const char *str1, const char *str2);
static void cleanup_rbar_dir(void);
static void write_to_statusbar(const char *name, const char *message);
static void write_error_to_statusbar(const char *message);
static char *skip_character(char *input, char character, int amount);
static char *get_battery_info(void);
static char *get_wireless_info(void);
static const char *get_ip_address(const char *interface);
static char *get_eth_info(void);
static bool process_runs(const char *path);
#endif
void die(const char *fmt, ...);
enum { ORDER_RUN, ORDER_WLAN, ORDER_ETH, ORDER_BATTERY, ORDER_LOAD, ORDER_TIME, MAX_ORDER };