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 */
|
||||
char server[256] = "localhost"; /* default server */
|
||||
int daemon_mode = 0; /* by default, no daemon mode */
|
||||
int sock = 0; /* socket handler */
|
||||
#endif //NETLCDCLIENT
|
||||
|
||||
int iface_count = 0; /* number of interfaces */
|
||||
|
||||
char unit_label[10] = "B"; /* default unit label is Bytes */
|
||||
int transfer_screen = 0; /* by default, transfer screen is not shown */
|
||||
int sock = 0; /* socket handler */
|
||||
|
||||
/* command line parameters, used by getopt_long() */
|
||||
static struct option const long_options[] =
|
||||
@@ -326,7 +326,7 @@ usage (int status)
|
||||
|
||||
/* reads and parses configuration file */
|
||||
static int
|
||||
iface_process_configfile(char *configfile)
|
||||
iface_process_configfile()
|
||||
{
|
||||
char *unit;
|
||||
|
||||
@@ -334,15 +334,6 @@ iface_process_configfile(char *configfile)
|
||||
|
||||
/* 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++) {
|
||||
char iface_label[12];
|
||||
|
||||
@@ -410,7 +401,7 @@ iface_screen(int rep, int display, int *flags_ptr)
|
||||
*flags_ptr |= INITIALIZED;
|
||||
|
||||
/* get configuration options */
|
||||
iface_process_configfile(configfile);
|
||||
iface_process_configfile();
|
||||
|
||||
/* set initial speed screen with widgets */
|
||||
initialize_speed_screen();
|
||||
|
||||
+108
-93
@@ -10,6 +10,7 @@
|
||||
#include <signal.h>
|
||||
#include <netdb.h>
|
||||
#include <ctype.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
@@ -37,7 +38,7 @@
|
||||
// TODO: Commenting... Everything!
|
||||
|
||||
int Quit = 0;
|
||||
int sock;
|
||||
int sock = -1;
|
||||
|
||||
char *version = VERSION;
|
||||
char *build_date = __DATE__;
|
||||
@@ -56,7 +57,6 @@ static struct utsname unamebuf;
|
||||
static void HelpScreen(int exit_state);
|
||||
static void exit_program(int val);
|
||||
static void main_loop();
|
||||
static int process_command_line(int argc, char **argv);
|
||||
static int process_configfile(char *cfgfile);
|
||||
|
||||
|
||||
@@ -104,13 +104,13 @@ mode sequence[] =
|
||||
|
||||
/* All variables are set to 'unset' values*/
|
||||
static int islow = -1;
|
||||
char * progname = "lcdproc";
|
||||
char * server = NULL;
|
||||
char *progname = "lcdproc";
|
||||
char *server = NULL;
|
||||
int port = LCDPORT;
|
||||
int foreground = UNSET_INT;
|
||||
static int report_level = 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()
|
||||
@@ -118,16 +118,19 @@ const char *get_hostname()
|
||||
return(unamebuf.nodename);
|
||||
}
|
||||
|
||||
|
||||
const char *get_sysname()
|
||||
{
|
||||
return(unamebuf.sysname);
|
||||
}
|
||||
|
||||
|
||||
const char *get_sysrelease()
|
||||
{
|
||||
return(unamebuf.release);
|
||||
}
|
||||
|
||||
|
||||
int set_mode(int shortname, char *longname, int state)
|
||||
{
|
||||
int k;
|
||||
@@ -140,8 +143,10 @@ int set_mode(int shortname, char *longname, int state)
|
||||
if (!state) {
|
||||
/* clean both the active and inititialized bits since we delete the screen */
|
||||
sequence[k].flags &= (~ACTIVE & ~INITIALIZED);
|
||||
sprintf (buffer, "screen_del %c\n", shortname );
|
||||
sock_send_string (sock, buffer);
|
||||
/* delte the screen if we are connected */
|
||||
if (sock >= 0) {
|
||||
sock_printf(sock, "screen_del %c\n", sequence[k].which);
|
||||
}
|
||||
} else
|
||||
sequence[k].flags |= ACTIVE;
|
||||
return 1; //found
|
||||
@@ -150,19 +155,23 @@ int set_mode(int shortname, char *longname, int state)
|
||||
return 0; //not found
|
||||
}
|
||||
|
||||
|
||||
void clear_modes()
|
||||
{
|
||||
int k;
|
||||
|
||||
/* ignore already selected modes */
|
||||
for (k = 0; sequence[k].which != 0; k++) {
|
||||
sequence[k].flags &= (~ACTIVE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int error = 0;
|
||||
int cfgresult;
|
||||
int c;
|
||||
|
||||
/* get uname information */
|
||||
if (uname(&unamebuf) == -1) {
|
||||
@@ -176,26 +185,90 @@ main(int argc, char **argv)
|
||||
signal(SIGHUP, exit_program); // kill -HUP
|
||||
signal(SIGKILL, exit_program); // kill -9 [cannot be trapped; but ...]
|
||||
|
||||
/* Set default config file, command line may overwrite */
|
||||
strncpy(configfile, DEFAULT_CONFIGFILE, sizeof(configfile));
|
||||
/* No error output from getopt */
|
||||
opterr = 0;
|
||||
|
||||
/* Read command line*/
|
||||
CHAIN( error, process_command_line(argc, argv) );
|
||||
/* get options from command line */
|
||||
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*/
|
||||
if (strcmp(configfile, UNSET_STR) != 0)
|
||||
CHAIN( error, process_configfile(configfile) );
|
||||
cfgresult = process_configfile(configfile);
|
||||
if (cfgresult < 0) {
|
||||
fprintf(stderr, "Error reading config file");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Set default options */
|
||||
if (report_dest == UNSET_INT )
|
||||
/* Set default reporting options */
|
||||
if (report_dest == UNSET_INT)
|
||||
report_dest = DEFAULT_REPORTDEST;
|
||||
if( report_level == UNSET_INT )
|
||||
if( report_level == UNSET_INT)
|
||||
report_level = DEFAULT_REPORTLEVEL;
|
||||
|
||||
/* Set reporting settings */
|
||||
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)
|
||||
server = DEFAULT_SERVER;
|
||||
@@ -238,90 +311,32 @@ main(int argc, char **argv)
|
||||
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 */
|
||||
opterr = 0;
|
||||
|
||||
/* get options */
|
||||
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 */
|
||||
/* reads and parses configuration filei
|
||||
* returns: 1 if configfile was read,
|
||||
* 0 if default configfile doesn't exist
|
||||
* <0 on error
|
||||
*/
|
||||
static int
|
||||
process_configfile(char *configfile)
|
||||
{
|
||||
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;
|
||||
}
|
||||
if (config_read_file(configfile) != 0 ) {
|
||||
|
||||
if (config_read_file(configfile) != 0) {
|
||||
report(RPT_CRIT, "Could not read config file: %s", configfile);
|
||||
return -1;
|
||||
//report(RPT_WARNING, "Could not read config file: %s", configfile);
|
||||
@@ -365,7 +380,7 @@ process_configfile(char *configfile)
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -61,8 +61,6 @@ const char *get_hostname();
|
||||
const char *get_sysname();
|
||||
const char *get_sysrelease();
|
||||
|
||||
extern char configfile[];
|
||||
|
||||
#ifndef min
|
||||
# define min(a,b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user