fix a few warnings; tighten arg checks; harmonize -f handling: it is a simple flag!

This commit is contained in:
marschap
2006-04-28 17:10:25 +00:00
parent ff0e33b82f
commit ecc9320b54
8 changed files with 218 additions and 225 deletions
+26 -31
View File
@@ -49,7 +49,7 @@ char * help_text =
" -c <file>\tSpecify configuration file ["DEFAULT_CONFIGFILE"]\n"
" -a <address>\tDNS name or IP address of the LCDd server [localhost]\n"
" -p <port>\tport of the LCDd server [13666]\n"
" -f <0|1>\tRun in foreground (1) or background (0, default)\n"
" -f \tRun in foreground\n"
" -r <level>\tSet reporting level (0-5) [2: errors and warnings]\n"
" -s <0|1>\tReport to syslog (1) or stderr (0, default)\n"
" -h\t\tShow this help\n";
@@ -73,7 +73,7 @@ static int report_dest = UNSET_INT;
Menu * main_menu;
/* Other variables */
int sock;
int sock = -1;
/* Function prototypes */
int process_command_line(int argc, char **argv);
@@ -84,9 +84,11 @@ int process_response( char * str );
int exec_command(char * command);
int main_loop ();
#define CHAIN(e,f) { if (e>=0) { e=(f); }}
#define CHAIN_END(e) { if (e<0) { report(RPT_CRIT,"Critical error, abort"); exit(e); }}
int main(int argc, char **argv)
{
int error = 0;
@@ -120,14 +122,15 @@ int main( int argc, char **argv )
int process_command_line(int argc, char **argv)
{
char c;
char * p;
int temp_int;
int error = 0;
/* No error output from getopt */
opterr = 0;
while(( c = getopt( argc, argv, "c:O:a:p:f:r:s:h" )) > 0) {
while ((c = getopt(argc, argv, "c:O:a:p:fr:s:h")) > 0) {
char *end;
int temp_int;
switch(c) {
case 'c':
configfile = strdup(optarg);
@@ -139,38 +142,33 @@ int process_command_line( int argc, char **argv )
address = strdup(optarg);
break;
case 'p':
temp_int = strtol( optarg, &p, 0 );
if( *optarg != 0 && *p == 0 ) {
temp_int = strtol(optarg, &end, 0);
if ((*optarg != '\0') && (*end == '\0') &&
(temp_int > 0) && (temp_int <= 0xFFFF)) {
port = temp_int;
} else {
report( RPT_ERR, "Could not interpret value for -%c", c );
report(RPT_ERR, "Illegal port value %s", optarg);
error = -1;
}
break;
case 'f':
temp_int = strtol( optarg, &p, 0 );
if( *optarg != 0 && *p == 0 ) {
foreground_mode = temp_int;
} else {
report( RPT_ERR, "Could not interpret value for -%c", c );
error = -1;
}
foreground_mode = 1;
break;
case 'r':
temp_int = strtol( optarg, &p, 0 );
if( *optarg != 0 && *p == 0 ) {
temp_int = strtol(optarg, &end, 0);
if ((*optarg != '\0') && (*end == '\0') && (temp_int >= 0)) {
report_level = temp_int;
} else {
report( RPT_ERR, "Could not interpret value for -%c", c );
report(RPT_ERR, "Illegal report level value %s", optarg);
error = -1;
}
break;
case 's':
temp_int = strtol( optarg, &p, 0 );
if( *optarg != 0 && *p == 0 ) {
temp_int = strtol(optarg, &end, 0);
if ((*optarg != '\0') && (*end == '\0') && (temp_int >= 0)) {
report_dest = (temp_int ? RPT_DEST_SYSLOG : RPT_DEST_STDERR);
} else {
report( RPT_ERR, "Could not interpret value for -%c", c );
report(RPT_ERR, "Illegal log destination value %s", optarg);
error = -1;
}
break;
@@ -182,6 +180,7 @@ int process_command_line( int argc, char **argv )
error = -1;
break;
case '?':
default:
report(RPT_ERR, "Unknown option: %c", optopt);
error = -1;
break;
@@ -209,14 +208,12 @@ int process_configfile( char * configfile )
report_level = config_get_int(progname, "ReportLevel", 0, RPT_WARNING);
}
if (report_dest == UNSET_INT) {
if( config_get_bool( progname, "ReportToSyslog", 0, 0 )) {
report_dest = RPT_DEST_SYSLOG;
} else {
report_dest = RPT_DEST_STDERR;
}
report_dest = (config_get_bool(progname, "ReportToSyslog", 0, 0))
? RPT_DEST_SYSLOG
: RPT_DEST_STDERR;
}
if (foreground_mode == UNSET_INT) {
foreground_mode = config_get_bool( progname, "Foreground", 0, 1 );
foreground_mode = config_get_bool(progname, "Foreground", 0, 0);
}
main_menu = menu_read("", progname);
@@ -231,8 +228,6 @@ int process_configfile( char * configfile )
int connect_and_setup ()
{
char buf[200];
report(RPT_INFO, "Connecting to %s:%d", address, port);
sock = sock_connect(address, port);
@@ -242,8 +237,7 @@ int connect_and_setup ()
/* Create our menu */
sock_send_string(sock, "hello\n");
snprintf( buf, sizeof(buf)-1, "client_set -name \"%s\"\n", progname );
sock_send_string( sock, buf );
sock_printf(sock, "client_set -name \"%s\"\n", progname);
if (menu_send_to_LCDd(main_menu, "", sock) < 0) {
return -1;
@@ -277,6 +271,7 @@ int process_response( char * str )
}
if (strcmp(argv[1], "select") == 0) {
char *exec;
if (argc < 3) {
report(RPT_WARNING, "Server gave invalid response");
free(str2);
+1 -1
View File
@@ -14,7 +14,7 @@ ReportLevel=2
# report to to syslog ?
ReportToSyslog=false
# stay in foreground ?
# run in foreground [default: false; legal: true, false]
Foreground=false
# menu commands for the main menu
+8 -8
View File
@@ -39,7 +39,7 @@ Menu * menu_read( char * menu_id, char * progname )
/* Read the commands */
str = "";
while( str && menu->num_menucmds < MAX_NUM_MENUCMDS ) {
while ((str != NULL) && (menu->num_menucmds < MAX_NUM_MENUCMDS)) {
snprintf(buf, sizeof(buf)-1, "%s%sMenuCommand",
menu_id, menu_id[0] ? "_" : "");
@@ -104,13 +104,13 @@ int menu_send_to_LCDd( Menu * menu, char * id, int sock )
snprintf(buf, sizeof(buf)-1,
"menu_add_item \"%s\" \"%s%s%d\" action \"%s\"\n",
id, id, id[0]?"_":"", i, menu->menucmd_name[i]);
buf[sizeof(buf)-1] = 0;
buf[sizeof(buf)-1] = '\0';
if (sock_send_string(sock, buf) < 0)
return -1;
snprintf(buf, sizeof(buf)-1,
"menu_set_item \"%s\" \"%s%s%d\" -menu_result quit\n",
id, id, id[0]?"_":"", i);
buf[sizeof(buf)-1] = 0;
buf[sizeof(buf)-1] = '\0';
if (sock_send_string(sock, buf) < 0)
return -1;
}
@@ -118,7 +118,7 @@ int menu_send_to_LCDd( Menu * menu, char * id, int sock )
snprintf(buf, sizeof(buf)-1,
"menu_add_item \"%s\" \"%s\" menu \"%s\"\n",
id, menu->submenu_id[i], menu->submenu_name[i]);
buf[sizeof(buf)-1] = 0;
buf[sizeof(buf)-1] = '\0';
if (sock_send_string(sock, buf) < 0)
return -1;
}
@@ -145,7 +145,7 @@ char * menu_find_cmd_of_id( Menu * menu, char * id )
/* Do we need to search the submenus ? */
if (p) {
/* There is a menu id prepended to the id */
*p = 0; /* Crop the submenu_id string */
*p = '\0'; /* Crop the submenu_id string */
item_id = p + 1;
menu = menu_find_submenu(menu, submenu_id);
if (!menu) {
@@ -160,7 +160,7 @@ char * menu_find_cmd_of_id( Menu * menu, char * id )
/* Determine the number at the end of the id */
i = strtol(item_id, &p, 10);
if( *item_id != 0 && *p == 0
if (*item_id != '\0' && *p == '\0'
&& i >= 0 && i < menu->num_menucmds) {
/* OK */
free(submenu_id);
@@ -204,10 +204,10 @@ int split( char * str, char delim, char * parts[], int maxparts )
int part_nr = 0;
/* Find the delim char to end the current part */
while( part_nr < maxparts - 1 && (p2 = strchr( p1, delim )) ) {
while ((part_nr < maxparts - 1) && ((p2 = strchr(p1, delim)) != NULL)) {
/* subsequent parts... */
*p2 = 0;
*p2 = '\0';
parts[part_nr] = p1;
p1 = p2 + 1; /* Just after the delim char */
+1 -1
View File
@@ -14,7 +14,7 @@ ReportLevel=2
# report to to syslog ?
ReportToSyslog=false
# Run in foreground (Default: false)
# run in foreground [default: false; legal: true, false]
#Foreground=true
# slow down initial announcement of modes (in 1/100s)
+12 -12
View File
@@ -60,9 +60,6 @@ static void main_loop();
static int process_configfile(char *cfgfile);
#define CHAIN(e,f) { if (e>=0 ) { e=(f); }}
#define CHAIN_END(e,msg) { if (e<0 ) { report( RPT_CRIT,(msg)); exit(e); }}
// 1/8th second is a single time unit...
#define TIME_UNIT 125000
@@ -190,6 +187,8 @@ main(int argc, char **argv)
/* get options from command line */
while ((c = getopt( argc, argv, "s:p:e:c:fhv")) > 0) {
char *end;
switch (c) {
// c is for config file
case 'c':
@@ -197,21 +196,23 @@ main(int argc, char **argv)
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);
port = strtol(optarg, &end, 0);
if ((*optarg == '\0') || (*end != '\0') ||
(port <= 0) || (port >= 0xFFFF)) {
fprintf(stderr, "Illegal port value %s\n", optarg);
exit(EXIT_FAILURE);
}
break;
case 'e':
islow = atoi(optarg);
islow = strtol(optarg, &end, 0);
if ((*optarg == '\0') || (*end != '\0') || (islow < 0)) {
fprintf(stderr, "Illegal delay value %s\n", optarg);
exit(EXIT_FAILURE);
}
break;
case 'f':
foreground = TRUE;
@@ -264,7 +265,7 @@ fprintf(stderr, "%s%s\n", (state) ? "" : "!", name);
int found = set_mode(shortname, name, state);
if (!found) {
fprintf(stderr, "Invalid Mode: %c\n", name);
fprintf(stderr, "Invalid Mode: %s\n", name);
return(EXIT_FAILURE);
}
}
@@ -433,7 +434,6 @@ exit_program(int val)
exit(val);
}
#define LCDPROC_MENUS
#ifdef LCDPROC_MENUS
int
menus_init ()
+1
View File
@@ -120,6 +120,7 @@ int read_connect_string()
report(RPT_ERR, "Received invalid LCDd connect response.");
return -1;
}
return 0;
}
int read_response(char * buf, int maxsize)
+19 -22
View File
@@ -38,7 +38,7 @@ char * help_text =
" -c <file>\tSpecify a configfile to load ["DEFAULT_CONFIGFILE"]\n"
" -a <address>\tDNS name or IP address of the LCDd server [localhost]\n"
" -p <port>\tPort of the LCDd server [13666]\n"
" -f <0|1>\tRun in 1=foreground or 0=background\n"
" -f \tRun in foreground\n"
" -r <level>\tSet reporting level (0-5) [2: errors and warnings]\n"
" -s <0|1>\tReport to 1=syslog or 0=stderr\n"
" -h\t\tShow this help\n";
@@ -95,14 +95,15 @@ int main( int argc, char ** argv )
int process_command_line( int argc, char ** argv )
{
char c;
char * p;
int temp_int;
int error = 0;
/* No error output from getopt */
opterr = 0;
while ((c = getopt(argc, argv, "hc:a:p:f:r:s:")) > 0) {
while ((c = getopt(argc, argv, "hc:a:p:fr:s:")) > 0) {
char *end;
int temp_int;
switch (c) {
case 'h':
fprintf(stderr, "%s", help_text);
@@ -114,38 +115,33 @@ int process_command_line( int argc, char ** argv )
address = strdup(optarg);
break;
case 'p':
temp_int = strtol(optarg, &p, 0);
if ( *optarg != 0 && *p == 0) {
temp_int = strtol(optarg, &end, 0);
if ((*optarg != '\0') && (*end == '\0') &&
(temp_int > 0) && (temp_int <= 0xFFFF)) {
port = temp_int;
} else {
report(RPT_ERR, "Could not interpret value for -%c", c);
report(RPT_ERR, "Illegal port value %s", optarg);
error = -1;
}
break;
case 'f':
temp_int = strtol(optarg, &p, 0);
if (*optarg != 0 && *p == 0) {
foreground_mode = temp_int;
} else {
report(RPT_ERR, "Could not interpret value for -%c", c);
error = -1;
}
foreground_mode = 1;
break;
case 'r':
temp_int = strtol(optarg, &p, 0);
if (*optarg != 0 && *p == 0 ) {
temp_int = strtol(optarg, &end, 0);
if ((*optarg != '\0') && (*end == '\0') && (temp_int >= 0)) {
report_level = temp_int;
} else {
report(RPT_ERR, "Could not interpret value for -%c", c);
report(RPT_ERR, "Illegal report level value %s", optarg);
error = -1;
}
break;
case 's':
temp_int = strtol( optarg, &p, 0 );
if (*optarg != 0 && *p == 0 ) {
temp_int = strtol(optarg, &end, 0);
if ((*optarg != '\0') && (*end == '\0') && (temp_int >= 0)) {
report_dest = (temp_int ? RPT_DEST_SYSLOG : RPT_DEST_STDERR);
} else {
report(RPT_ERR, "Could not interpret value for -%c", c);
report(RPT_ERR, "Illegal log destination value %s", optarg);
error = -1;
}
break;
@@ -154,6 +150,7 @@ int process_command_line( int argc, char ** argv )
error = -1;
break;
case '?':
default:
report(RPT_ERR, "Unknown option: %c", optopt);
error = -1;
break;
@@ -188,7 +185,7 @@ int process_configfile( char * configfile )
}
}
if (foreground_mode == UNSET_INT) {
foreground_mode = config_get_bool(progname, "Foreground", 0, 1);
foreground_mode = config_get_bool(progname, "Foreground", 0, 0);
}
vcs_device = strdup(config_get_string(progname, "vcsDevice", 0, "/dev/vcs"));
vcsa_device = strdup(config_get_string(progname, "vcsaDevice", 0, "/dev/vcsa"));
@@ -235,7 +232,7 @@ int main_loop()
{
int num_bytes;
char buf[80];
short w;
short w = 0;
/* Continuously check if we get a menu event... */
+1 -1
View File
@@ -15,7 +15,7 @@
# Same as with LCDd.
#ReportLevel=4
# If false, it forks to the background.
# run in foreground [default: false; legal: true, false]
#Foreground=false
# Keys to move the visible area around the screen.