allow default config file to be absent, honor command line arguments
This commit is contained in:
+3
-12
@@ -51,13 +51,13 @@ char *program_name; /* the name the program was run with */
|
|||||||
int port = 13666; /* default port */
|
int port = 13666; /* default port */
|
||||||
char server[256] = "localhost"; /* default server */
|
char server[256] = "localhost"; /* default server */
|
||||||
int daemon_mode = 0; /* by default, no daemon mode */
|
int daemon_mode = 0; /* by default, no daemon mode */
|
||||||
|
int sock = 0; /* socket handler */
|
||||||
#endif //NETLCDCLIENT
|
#endif //NETLCDCLIENT
|
||||||
|
|
||||||
int iface_count = 0; /* number of interfaces */
|
int iface_count = 0; /* number of interfaces */
|
||||||
|
|
||||||
char unit_label[10] = "B"; /* default unit label is Bytes */
|
char unit_label[10] = "B"; /* default unit label is Bytes */
|
||||||
int transfer_screen = 0; /* by default, transfer screen is not shown */
|
int transfer_screen = 0; /* by default, transfer screen is not shown */
|
||||||
int sock = 0; /* socket handler */
|
|
||||||
|
|
||||||
/* command line parameters, used by getopt_long() */
|
/* command line parameters, used by getopt_long() */
|
||||||
static struct option const long_options[] =
|
static struct option const long_options[] =
|
||||||
@@ -326,7 +326,7 @@ usage (int status)
|
|||||||
|
|
||||||
/* reads and parses configuration file */
|
/* reads and parses configuration file */
|
||||||
static int
|
static int
|
||||||
iface_process_configfile(char *configfile)
|
iface_process_configfile()
|
||||||
{
|
{
|
||||||
char *unit;
|
char *unit;
|
||||||
|
|
||||||
@@ -334,15 +334,6 @@ iface_process_configfile(char *configfile)
|
|||||||
|
|
||||||
/* Read config settings */
|
/* Read config settings */
|
||||||
|
|
||||||
#if 0 /* this should be unnecessary */
|
|
||||||
if ( strcmp(configfile, UNSET_STR) == 0 || config_read_file(configfile) != 0 ) {
|
|
||||||
report(RPT_CRIT, "Could not read config file: %s", configfile);
|
|
||||||
return -1;
|
|
||||||
printf("Could not read config file: %s", configfile);
|
|
||||||
//report( RPT_WARNING, "Could not read config file: %s", configfile );
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
for (iface_count = 0; iface_count < MAX_INTERFACES; iface_count++) {
|
for (iface_count = 0; iface_count < MAX_INTERFACES; iface_count++) {
|
||||||
char iface_label[12];
|
char iface_label[12];
|
||||||
|
|
||||||
@@ -410,7 +401,7 @@ iface_screen(int rep, int display, int *flags_ptr)
|
|||||||
*flags_ptr |= INITIALIZED;
|
*flags_ptr |= INITIALIZED;
|
||||||
|
|
||||||
/* get configuration options */
|
/* get configuration options */
|
||||||
iface_process_configfile(configfile);
|
iface_process_configfile();
|
||||||
|
|
||||||
/* set initial speed screen with widgets */
|
/* set initial speed screen with widgets */
|
||||||
initialize_speed_screen();
|
initialize_speed_screen();
|
||||||
|
|||||||
+108
-93
@@ -10,6 +10,7 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <sys/utsname.h>
|
#include <sys/utsname.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
|
||||||
@@ -37,7 +38,7 @@
|
|||||||
// TODO: Commenting... Everything!
|
// TODO: Commenting... Everything!
|
||||||
|
|
||||||
int Quit = 0;
|
int Quit = 0;
|
||||||
int sock;
|
int sock = -1;
|
||||||
|
|
||||||
char *version = VERSION;
|
char *version = VERSION;
|
||||||
char *build_date = __DATE__;
|
char *build_date = __DATE__;
|
||||||
@@ -56,7 +57,6 @@ static struct utsname unamebuf;
|
|||||||
static void HelpScreen(int exit_state);
|
static void HelpScreen(int exit_state);
|
||||||
static void exit_program(int val);
|
static void exit_program(int val);
|
||||||
static void main_loop();
|
static void main_loop();
|
||||||
static int process_command_line(int argc, char **argv);
|
|
||||||
static int process_configfile(char *cfgfile);
|
static int process_configfile(char *cfgfile);
|
||||||
|
|
||||||
|
|
||||||
@@ -104,13 +104,13 @@ mode sequence[] =
|
|||||||
|
|
||||||
/* All variables are set to 'unset' values*/
|
/* All variables are set to 'unset' values*/
|
||||||
static int islow = -1;
|
static int islow = -1;
|
||||||
char * progname = "lcdproc";
|
char *progname = "lcdproc";
|
||||||
char * server = NULL;
|
char *server = NULL;
|
||||||
int port = LCDPORT;
|
int port = LCDPORT;
|
||||||
int foreground = UNSET_INT;
|
int foreground = UNSET_INT;
|
||||||
static int report_level = UNSET_INT;
|
static int report_level = UNSET_INT;
|
||||||
static int report_dest = UNSET_INT;
|
static int report_dest = UNSET_INT;
|
||||||
char configfile[256]; /* a lot of space in the executable. */
|
char *configfile = NULL;
|
||||||
|
|
||||||
|
|
||||||
const char *get_hostname()
|
const char *get_hostname()
|
||||||
@@ -118,16 +118,19 @@ const char *get_hostname()
|
|||||||
return(unamebuf.nodename);
|
return(unamebuf.nodename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const char *get_sysname()
|
const char *get_sysname()
|
||||||
{
|
{
|
||||||
return(unamebuf.sysname);
|
return(unamebuf.sysname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const char *get_sysrelease()
|
const char *get_sysrelease()
|
||||||
{
|
{
|
||||||
return(unamebuf.release);
|
return(unamebuf.release);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int set_mode(int shortname, char *longname, int state)
|
int set_mode(int shortname, char *longname, int state)
|
||||||
{
|
{
|
||||||
int k;
|
int k;
|
||||||
@@ -140,8 +143,10 @@ int set_mode(int shortname, char *longname, int state)
|
|||||||
if (!state) {
|
if (!state) {
|
||||||
/* clean both the active and inititialized bits since we delete the screen */
|
/* clean both the active and inititialized bits since we delete the screen */
|
||||||
sequence[k].flags &= (~ACTIVE & ~INITIALIZED);
|
sequence[k].flags &= (~ACTIVE & ~INITIALIZED);
|
||||||
sprintf (buffer, "screen_del %c\n", shortname );
|
/* delte the screen if we are connected */
|
||||||
sock_send_string (sock, buffer);
|
if (sock >= 0) {
|
||||||
|
sock_printf(sock, "screen_del %c\n", sequence[k].which);
|
||||||
|
}
|
||||||
} else
|
} else
|
||||||
sequence[k].flags |= ACTIVE;
|
sequence[k].flags |= ACTIVE;
|
||||||
return 1; //found
|
return 1; //found
|
||||||
@@ -150,19 +155,23 @@ int set_mode(int shortname, char *longname, int state)
|
|||||||
return 0; //not found
|
return 0; //not found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void clear_modes()
|
void clear_modes()
|
||||||
{
|
{
|
||||||
int k;
|
int k;
|
||||||
|
|
||||||
/* ignore already selected modes */
|
/* ignore already selected modes */
|
||||||
for (k = 0; sequence[k].which != 0; k++) {
|
for (k = 0; sequence[k].which != 0; k++) {
|
||||||
sequence[k].flags &= (~ACTIVE);
|
sequence[k].flags &= (~ACTIVE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int error = 0;
|
int cfgresult;
|
||||||
|
int c;
|
||||||
|
|
||||||
/* get uname information */
|
/* get uname information */
|
||||||
if (uname(&unamebuf) == -1) {
|
if (uname(&unamebuf) == -1) {
|
||||||
@@ -176,26 +185,90 @@ main(int argc, char **argv)
|
|||||||
signal(SIGHUP, exit_program); // kill -HUP
|
signal(SIGHUP, exit_program); // kill -HUP
|
||||||
signal(SIGKILL, exit_program); // kill -9 [cannot be trapped; but ...]
|
signal(SIGKILL, exit_program); // kill -9 [cannot be trapped; but ...]
|
||||||
|
|
||||||
/* Set default config file, command line may overwrite */
|
/* No error output from getopt */
|
||||||
strncpy(configfile, DEFAULT_CONFIGFILE, sizeof(configfile));
|
opterr = 0;
|
||||||
|
|
||||||
/* Read command line*/
|
/* get options from command line */
|
||||||
CHAIN( error, process_command_line(argc, argv) );
|
while ((c = getopt( argc, argv, "s:p:e:c:fhv")) > 0) {
|
||||||
|
switch (c) {
|
||||||
|
// c is for config file
|
||||||
|
case 'c':
|
||||||
|
configfile = optarg;
|
||||||
|
break;
|
||||||
|
// s is for server
|
||||||
|
case 's':
|
||||||
|
if (server == NULL)
|
||||||
|
server = optarg;
|
||||||
|
else
|
||||||
|
fprintf(stderr, "Ignoring additional server: %s\n", optarg);
|
||||||
|
break;
|
||||||
|
// p is for port
|
||||||
|
case 'p':
|
||||||
|
port = atoi(optarg);
|
||||||
|
if ((port < 1) && (port > 0xFFFF)) {
|
||||||
|
fprintf(stderr, "Warning: Port %d outside of legal range\n", port);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'e':
|
||||||
|
islow = atoi(optarg);
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
foreground = TRUE;
|
||||||
|
break;
|
||||||
|
case 'h':
|
||||||
|
HelpScreen(EXIT_SUCCESS);
|
||||||
|
break;
|
||||||
|
case 'v':
|
||||||
|
fprintf(stderr, "LCDproc %s\n", version);
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
break;
|
||||||
|
// otherwise... Get help!
|
||||||
|
case '?': // unknown option or missing argument
|
||||||
|
default:
|
||||||
|
HelpScreen(EXIT_FAILURE);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Read config file*/
|
/* Read config file*/
|
||||||
if (strcmp(configfile, UNSET_STR) != 0)
|
cfgresult = process_configfile(configfile);
|
||||||
CHAIN( error, process_configfile(configfile) );
|
if (cfgresult < 0) {
|
||||||
|
fprintf(stderr, "Error reading config file");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
/* Set default options */
|
/* Set default reporting options */
|
||||||
if (report_dest == UNSET_INT )
|
if (report_dest == UNSET_INT)
|
||||||
report_dest = DEFAULT_REPORTDEST;
|
report_dest = DEFAULT_REPORTDEST;
|
||||||
if( report_level == UNSET_INT )
|
if( report_level == UNSET_INT)
|
||||||
report_level = DEFAULT_REPORTLEVEL;
|
report_level = DEFAULT_REPORTLEVEL;
|
||||||
|
|
||||||
/* Set reporting settings */
|
/* Set reporting settings */
|
||||||
set_reporting("lcdproc", report_level, report_dest);
|
set_reporting("lcdproc", report_level, report_dest);
|
||||||
|
|
||||||
CHAIN_END( error, "Error in config file or command line\n" );
|
/* parse non-option arguments: modes to add/delete */
|
||||||
|
if (argc > max(optind, 1)) {
|
||||||
|
int i;
|
||||||
|
|
||||||
|
// if no config file was read, ignore hard coded default modes
|
||||||
|
if (cfgresult == 0)
|
||||||
|
clear_modes();
|
||||||
|
|
||||||
|
// turn additional options on or off (using ! as prefix)
|
||||||
|
for (i = max(optind, 1); i < argc; i++) {
|
||||||
|
int state = (*argv[i] == '!') ? 0 : 1;
|
||||||
|
char *name = (state) ? argv[i] : argv[i]+1;
|
||||||
|
int shortname = (strlen(name) == 1) ? name[0] : '\0';
|
||||||
|
fprintf(stderr, "%s%s\n", (state) ? "" : "!", name);
|
||||||
|
int found = set_mode(shortname, name, state);
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
fprintf(stderr, "Invalid Mode: %c\n", name);
|
||||||
|
return(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (server == NULL)
|
if (server == NULL)
|
||||||
server = DEFAULT_SERVER;
|
server = DEFAULT_SERVER;
|
||||||
@@ -238,90 +311,32 @@ main(int argc, char **argv)
|
|||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parses arguments given on command line */
|
|
||||||
static int
|
|
||||||
process_command_line(int argc, char **argv)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int c;
|
|
||||||
|
|
||||||
/* No error output from getopt */
|
/* reads and parses configuration filei
|
||||||
opterr = 0;
|
* returns: 1 if configfile was read,
|
||||||
|
* 0 if default configfile doesn't exist
|
||||||
/* get options */
|
* <0 on error
|
||||||
while ((c = getopt( argc, argv, "s:p:e:c:fhv")) > 0) {
|
*/
|
||||||
switch (c) {
|
|
||||||
// c is for config file
|
|
||||||
case 'c':
|
|
||||||
strncpy(configfile, optarg, sizeof(configfile));
|
|
||||||
configfile[sizeof(configfile)-1] = 0; /* Terminate string */
|
|
||||||
break;
|
|
||||||
// s is for server
|
|
||||||
case 's':
|
|
||||||
if (server == NULL)
|
|
||||||
server = optarg;
|
|
||||||
else
|
|
||||||
fprintf(stderr, "Ignoring additional server: %s\n", optarg);
|
|
||||||
break;
|
|
||||||
// p is for port
|
|
||||||
case 'p':
|
|
||||||
port = atoi(optarg);
|
|
||||||
if ((port < 1) && (port > 0xFFFF)) {
|
|
||||||
fprintf(stderr, "Warning: Port %d outside of legal range\n", port);
|
|
||||||
return(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'e':
|
|
||||||
islow = atoi(optarg);
|
|
||||||
break;
|
|
||||||
case 'f':
|
|
||||||
foreground = TRUE;
|
|
||||||
break;
|
|
||||||
case 'h':
|
|
||||||
HelpScreen(EXIT_SUCCESS);
|
|
||||||
break;
|
|
||||||
case 'v':
|
|
||||||
fprintf(stderr, "LCDproc %s\n", version);
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
break;
|
|
||||||
// otherwise... Get help!
|
|
||||||
case '?': // unknown option or missing argument
|
|
||||||
default:
|
|
||||||
HelpScreen(EXIT_FAILURE);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* parse arguments */
|
|
||||||
if (argc > optind) //user specified some modes, so clear all defaults
|
|
||||||
clear_modes();
|
|
||||||
|
|
||||||
for (i = max(optind, 1); i < argc; i++) {
|
|
||||||
int shortname = (strlen(argv[i]) == 1) ? toupper(argv[i][0]) : '\0';
|
|
||||||
int found = set_mode(shortname, argv[i], 1);
|
|
||||||
|
|
||||||
if (!found) {
|
|
||||||
fprintf(stderr, "Invalid Mode: %c\n", argv[i][0]);
|
|
||||||
return(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* reads and parses configuration file */
|
|
||||||
static int
|
static int
|
||||||
process_configfile(char *configfile)
|
process_configfile(char *configfile)
|
||||||
{
|
{
|
||||||
int k;
|
int k;
|
||||||
|
|
||||||
debug(RPT_DEBUG, "%s(%s)", __FUNCTION__, configfile);
|
debug(RPT_DEBUG, "%s(%s)", __FUNCTION__, (configfile) ? configfile : "<null>");
|
||||||
|
|
||||||
/* Read server settings*/
|
/* Read config settings*/
|
||||||
|
|
||||||
|
if (configfile == NULL) {
|
||||||
|
struct stat statbuf;
|
||||||
|
|
||||||
|
// if default config file does not exist, do not consider this an error
|
||||||
|
if ((lstat(DEFAULT_CONFIGFILE, &statbuf) == -1) && (errno = ENOENT))
|
||||||
|
return 0;
|
||||||
|
|
||||||
if (strcmp(configfile, UNSET_STR) == 0) {
|
|
||||||
configfile = DEFAULT_CONFIGFILE;
|
configfile = DEFAULT_CONFIGFILE;
|
||||||
}
|
}
|
||||||
if (config_read_file(configfile) != 0 ) {
|
|
||||||
|
if (config_read_file(configfile) != 0) {
|
||||||
report(RPT_CRIT, "Could not read config file: %s", configfile);
|
report(RPT_CRIT, "Could not read config file: %s", configfile);
|
||||||
return -1;
|
return -1;
|
||||||
//report(RPT_WARNING, "Could not read config file: %s", configfile);
|
//report(RPT_WARNING, "Could not read config file: %s", configfile);
|
||||||
@@ -365,7 +380,7 @@ process_configfile(char *configfile)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|||||||
@@ -61,8 +61,6 @@ const char *get_hostname();
|
|||||||
const char *get_sysname();
|
const char *get_sysname();
|
||||||
const char *get_sysrelease();
|
const char *get_sysrelease();
|
||||||
|
|
||||||
extern char configfile[];
|
|
||||||
|
|
||||||
#ifndef min
|
#ifndef min
|
||||||
# define min(a,b) (((a) < (b)) ? (a) : (b))
|
# define min(a,b) (((a) < (b)) ? (a) : (b))
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user