cleanup in main.c:

- use config_get_tristate()
- a few more error checks
- use symbolic constants EXIT_SUCCESS, EXIT_FAILURE
This commit is contained in:
marschap
2007-05-12 16:14:30 +00:00
parent 3e84564e6a
commit b801ff6f5a
3 changed files with 47 additions and 66 deletions
+42 -65
View File
@@ -81,12 +81,14 @@
#define DEFAULT_DRIVER_PATH "" /* not needed */
#define MAX_DRIVERS 8
#define DEFAULT_FOREGROUND_MODE 0
#define DEFAULT_ROTATE_SERVER_SCREEN 1
#define DEFAULT_ROTATE_SERVER_SCREEN SERVERSCREEN_ON
#define DEFAULT_REPORTDEST RPT_DEST_STDERR
#define DEFAULT_REPORTLEVEL RPT_WARNING
#define DEFAULT_SCREEN_DURATION 32
#define DEFAULT_HEARTBEAT HEARTBEAT_ON
#define DEFAULT_BACKLIGHT BACKLIGHT_OPEN
#define DEFAULT_HEARTBEAT HEARTBEAT_OPEN
#define DEFAULT_TITLESPEED TITLESPEED_MAX
/* All variables are set to 'unset' values*/
#define UNSET_INT -1
@@ -162,7 +164,7 @@ static void output_help_screen(void);
static void output_GPL_notice(void);
#define CHAIN(e,f) { if (e>=0) { e=(f); }}
#define CHAIN_END(e,msg) { if (e<0) { report(RPT_CRIT,(msg)); exit(e); }}
#define CHAIN_END(e,msg) { if (e<0) { report(RPT_CRIT,(msg)); exit(EXIT_FAILURE); }}
int
@@ -307,14 +309,14 @@ process_command_line(int argc, char **argv)
break;
case 'c':
strncpy(configfile, optarg, sizeof(configfile));
configfile[sizeof(configfile)-1] = 0; /* Terminate string */
configfile[sizeof(configfile)-1] = '\0'; /* Terminate string */
break;
case 'd':
/* Add to a list of drivers to be initialized later...*/
if (num_drivers < MAX_DRIVERS) {
drivernames[num_drivers] = strdup(optarg);
if (drivernames[num_drivers] != NULL) {
num_drivers ++;
num_drivers++;
}
else {
report(RPT_ERR, "alloc error storing driver name: %s", optarg);
@@ -330,14 +332,14 @@ process_command_line(int argc, char **argv)
break;
case 'a':
strncpy(bind_addr, optarg, sizeof(bind_addr));
bind_addr[sizeof(bind_addr)-1] = 0; /* Terminate string */
bind_addr[sizeof(bind_addr)-1] = '\0'; /* Terminate string */
break;
case 'p':
bind_port = atoi(optarg);
break;
case 'u':
strncpy(user, optarg, sizeof(user));
user[sizeof(user)-1] = 0; /* Terminate string */
user[sizeof(user)-1] = '\0'; /* Terminate string */
break;
case 'w':
default_duration = (int) (atof(optarg) * 1e6 / TIME_UNIT);
@@ -406,16 +408,16 @@ process_configfile(char *configfile)
}
if (bind_port == UNSET_INT)
bind_port = config_get_int("server", "port", 0, UNSET_INT);
bind_port = config_get_int("Server", "Port", 0, UNSET_INT);
if (strcmp(bind_addr, UNSET_STR) == 0)
strncpy(bind_addr, config_get_string("server", "bind", 0, UNSET_STR), sizeof(bind_addr));
strncpy(bind_addr, config_get_string("Server", "Bind", 0, UNSET_STR), sizeof(bind_addr));
if (strcmp(user, UNSET_STR) == 0)
strncpy(user, config_get_string("server", "user", 0, UNSET_STR), sizeof(user));
strncpy(user, config_get_string("Server", "User", 0, UNSET_STR), sizeof(user));
if (default_duration == UNSET_INT) {
default_duration = (config_get_float("server", "waittime", 0, 0) * 1e6 / TIME_UNIT);
default_duration = (config_get_float("Server", "WaitTime", 0, 0) * 1e6 / TIME_UNIT);
if (default_duration == 0)
default_duration = UNSET_INT;
else if (default_duration * TIME_UNIT < 2e6) {
@@ -425,56 +427,26 @@ process_configfile(char *configfile)
}
if (foreground_mode == UNSET_INT) {
int fg = config_get_bool("server", "foreground", 0, UNSET_INT);
int fg = config_get_bool("Server", "Foreground", 0, UNSET_INT);
if (fg != UNSET_INT)
foreground_mode = fg;
}
if (rotate_server_screen == UNSET_INT) {
rotate_server_screen = config_get_bool("server", "serverscreen", 0, UNSET_INT);
rotate_server_screen = config_get_tristate("Server", "ServerScreen", 0, "never", UNSET_INT);
}
if (backlight == UNSET_INT) {
const char *s = config_get_string("server", "Backlight", 0, NULL);
if (s != NULL) {
if ((strcasecmp(s, "on") == 0) || (strcasecmp(s, "yes") == 0)) {
backlight = BACKLIGHT_ON;
}
else if (strcasecmp(s, "off") == 0) {
backlight = BACKLIGHT_OFF;
}
if ((strcasecmp(s, "off") == 0) || (strcasecmp(s, "no") == 0)) {
backlight = BACKLIGHT_OPEN;
}
else {
report(RPT_WARNING, "Backlight state should be on, off or open");
}
}
backlight = config_get_tristate("Server", "Backlight", 0, "open", UNSET_INT);
}
if (heartbeat == UNSET_INT) {
const char *s = config_get_string("server", "Heartbeat", 0, NULL);
if (s != NULL) {
if ((strcasecmp(s, "on") == 0) || (strcasecmp(s, "yes") == 0)) {
heartbeat = HEARTBEAT_ON;
}
else if (strcasecmp(s, "off") == 0) {
heartbeat = HEARTBEAT_OFF;
}
if ((strcasecmp(s, "off") == 0) || (strcasecmp(s, "no") == 0)) {
heartbeat = HEARTBEAT_OPEN;
}
else {
report(RPT_WARNING, "Heartbeat state should be on, off or open");
}
}
heartbeat = config_get_tristate("Server", "Heartbeat", 0, "open", UNSET_INT);
}
if (titlespeed == UNSET_INT) {
int speed = config_get_int("server", "TitleSpeed", 0, TITLESPEED_DEFAULT);
int speed = config_get_int("Server", "TitleSpeed", 0, DEFAULT_TITLESPEED);
/* set titlespeed */
titlespeed = (speed <= TITLESPEED_NO)
@@ -483,31 +455,33 @@ process_configfile(char *configfile)
}
if (report_dest == UNSET_INT) {
int rs = config_get_bool("server", "reportToSyslog", 0, UNSET_INT);
int rs = config_get_bool("Server", "ReportToSyslog", 0, UNSET_INT);
if (rs != UNSET_INT)
report_dest = (rs) ? RPT_DEST_SYSLOG : RPT_DEST_STDERR;
}
if (report_level == UNSET_INT) {
report_level = config_get_int("server", "reportLevel", 0, UNSET_INT);
report_level = config_get_int("Server", "ReportLevel", 0, UNSET_INT);
}
/* Read drivers*/
/* Read drivers */
/* If drivers have been specified on the command line, then do not
* use the driver list from the config file.
*/
if (num_drivers == 0) {
/* read the drivernames*/
/* loop over all the Driver= directives to read the driver names */
while (1) {
const char *s = config_get_string("server", "driver", num_drivers, NULL);
const char *s = config_get_string("Server", "Driver", num_drivers, NULL);
if (s == NULL)
break;
if (s[0] != 0) {
drivernames[num_drivers] = malloc(strlen(s)+1);
strcpy(drivernames[num_drivers], s);
if (s[0] != '\0') {
drivernames[num_drivers] = strdup(s);
if (drivernames[num_drivers] == NULL) {
report(RPT_ERR, "alloc error storing driver name: %s", s);
exit(EXIT_FAILURE);
}
num_drivers++;
}
}
@@ -522,7 +496,7 @@ set_default_settings(void)
{
debug(RPT_DEBUG, "%s()", __FUNCTION__);
/* Set defaults into unfilled variables....*/
/* Set defaults into unfilled variables... */
if (bind_port == UNSET_INT)
bind_port = DEFAULT_BIND_PORT;
@@ -539,11 +513,11 @@ set_default_settings(void)
if (default_duration == UNSET_INT)
default_duration = DEFAULT_SCREEN_DURATION;
if (backlight == UNSET_INT)
backlight = BACKLIGHT_OPEN;
backlight = DEFAULT_BACKLIGHT;
if (heartbeat == UNSET_INT)
heartbeat = HEARTBEAT_OPEN;
heartbeat = DEFAULT_HEARTBEAT;
if (titlespeed == UNSET_INT)
titlespeed = TITLESPEED_DEFAULT;
titlespeed = DEFAULT_TITLESPEED;
if (report_dest == UNSET_INT)
report_dest = DEFAULT_REPORTDEST;
@@ -551,10 +525,13 @@ set_default_settings(void)
report_level = DEFAULT_REPORTLEVEL;
/* Use default driver*/
/* Use default driver */
if (num_drivers == 0) {
drivernames[0] = malloc(strlen(DEFAULT_DRIVER)+1);
strcpy(drivernames[0], DEFAULT_DRIVER);
drivernames[0] = strdup(DEFAULT_DRIVER);
if (drivernames[0] == NULL) {
report(RPT_ERR, "alloc error storing driver name: %s", DEFAULT_DRIVER);
exit(EXIT_FAILURE);
}
num_drivers = 1;
}
}
@@ -613,7 +590,7 @@ child_ok_func(int signal)
debug(RPT_INFO, "%s(signal=%d)", __FUNCTION__, signal);
/* Exit now ! because of bug? in wait() */
_exit(0); /* Parent exits normally. */
_exit(EXIT_SUCCESS); /* Parent exits normally. */
}
@@ -669,7 +646,7 @@ daemonize(void)
/* Child is still running and has signalled it's OK.
* This means the parent can now rest in peace. */
debug(RPT_INFO, "Got OK signal from child.");
exit(0); /* Parent exits normally. */
exit(EXIT_SUCCESS); /* Parent exits normally. */
}
/* At this point we are always the child. */
/* Reset signal handler */
@@ -950,7 +927,7 @@ exit_program(int val)
sock_shutdown(); /* shutdown the sockets server */
report(RPT_INFO, "Exiting.");
_exit(0);
_exit(EXIT_SUCCESS);
}
-1
View File
@@ -33,7 +33,6 @@
#define TITLESPEED_NO 0 /* needs to be (TITLESPEED_MIN - 1) */
#define TITLESPEED_MIN 1
#define TITLESPEED_MAX 10
#define TITLESPEED_DEFAULT 10 /* = no delay */
extern int heartbeat;
extern int backlight;
+5
View File
@@ -14,6 +14,11 @@
#include "screen.h"
/* server screen rotation states */
#define SERVERSCREEN_OFF 0 /* show server screen in rotation */
#define SERVERSCREEN_ON 1 /* show server sreen only when there is no other screen */
#define SERVERSCREEN_NEVER 2 /* don't rotate, and only show a blank screen [not implemented] */
extern Screen *server_screen;
extern int rotate_server_screen;