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