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
+109 -114
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,41 +73,43 @@ 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);
int process_configfile( char * configfile ); int process_configfile(char * configfile);
int split( char * str, char delim, char * parts[], int maxparts ); int split(char * str, char delim, char * parts[], int maxparts);
int connect_and_setup (); int connect_and_setup ();
int process_response( char * str ); 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_END(e) { if( e<0 ) { report( RPT_CRIT,"Critical error, abort"); exit(e); }}
int main( int argc, char **argv ) #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; int error = 0;
CHAIN( error, process_command_line( argc, argv )); CHAIN(error, process_command_line(argc, argv));
if( configfile == NULL ) if (configfile == NULL)
configfile = DEFAULT_CONFIGFILE; configfile = DEFAULT_CONFIGFILE;
CHAIN( error, process_configfile( configfile )); CHAIN(error, process_configfile(configfile));
if( report_dest == UNSET_INT || report_level == UNSET_INT ) { if (report_dest == UNSET_INT || report_level == UNSET_INT) {
report_dest = RPT_DEST_STDERR; report_dest = RPT_DEST_STDERR;
report_level = RPT_ERR; report_level = RPT_ERR;
} }
set_reporting( progname, report_level, report_dest ); set_reporting(progname, report_level, report_dest);
CHAIN_END( error ); CHAIN_END(error);
CHAIN( error, connect_and_setup() ); CHAIN(error, connect_and_setup());
CHAIN_END( error ); CHAIN_END(error);
if(!foreground_mode) { if(!foreground_mode) {
if (daemon(1,1)!=0) { if (daemon(1,1) != 0) {
report(RPT_ERR, "Error: daemonize failed"); report(RPT_ERR, "Error: daemonize failed");
} }
} }
@@ -117,72 +119,69 @@ int main( int argc, char **argv )
return 0; return 0;
} }
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) {
switch( c ) { char *end;
int temp_int;
switch(c) {
case 'c': case 'c':
configfile = strdup( optarg ); configfile = strdup(optarg);
break; break;
case 'O': case 'O':
config_read_string( progname, optarg ); config_read_string(progname, optarg);
break; break;
case 'a': case 'a':
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;
case 'h': case 'h':
fprintf( stderr, "%s", help_text ); fprintf(stderr, "%s", help_text);
exit( 0 ); exit(0);
case ':': case ':':
report( RPT_ERR, "Missing option argument for %c", optopt ); report(RPT_ERR, "Missing option argument for %c", optopt);
error = -1; error = -1;
break; break;
case '?': case '?':
report( RPT_ERR, "Unknown option: %c", optopt ); default:
report(RPT_ERR, "Unknown option: %c", optopt);
error = -1; error = -1;
break; break;
} }
@@ -190,38 +189,36 @@ int process_command_line( int argc, char **argv )
return error; return error;
} }
int process_configfile( char * configfile ) int process_configfile(char * configfile)
{ {
if( strcmp( configfile, UNSET_STR ) == 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_WARNING, "Could not read config file: %s", configfile ); report(RPT_WARNING, "Could not read config file: %s", configfile);
} }
if( strcmp( address, UNSET_STR ) == 0 ) { if (strcmp(address, UNSET_STR) == 0) {
address = strdup( config_get_string( progname, "Address", 0, "localhost" )); address = strdup(config_get_string(progname, "Address", 0, "localhost"));
} }
if( port == UNSET_INT ) { if (port == UNSET_INT) {
port = config_get_int( progname, "Port", 0, 13666 ); port = config_get_int(progname, "Port", 0, 13666);
} }
if( report_level == UNSET_INT ) { if (report_level == UNSET_INT) {
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, 0);
foreground_mode = config_get_bool( progname, "Foreground", 0, 1 );
} }
main_menu = menu_read( "", progname ); main_menu = menu_read("", progname);
if( main_menu->num_menucmds == 0 && main_menu->num_submenus == 0 ) { if (main_menu->num_menucmds == 0 && main_menu->num_submenus == 0) {
main_menu->menucmd_name[0] = "echo test"; main_menu->menucmd_name[0] = "echo test";
main_menu->menucmd_exec[0] = "echo \"This is a test using the echo command.\""; main_menu->menucmd_exec[0] = "echo \"This is a test using the echo command.\"";
main_menu->num_menucmds ++; main_menu->num_menucmds ++;
@@ -231,107 +228,105 @@ 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);
if (sock <= 0) {
sock = sock_connect( address, port );
if( sock <= 0 ) {
return -1; return -1;
} }
/* 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;
} }
return 0; return 0;
} }
int process_response( char * str ) int process_response(char * str)
{ {
char *argv[10]; char *argv[10];
int argc; int argc;
char * str2 = strdup( str ); /* get_args modifies str2 */ char *str2 = strdup(str); /* get_args modifies str2 */
report( RPT_DEBUG, "Server said: \"%s\"", str ); report(RPT_DEBUG, "Server said: \"%s\"", str);
/* Check what the server just said to us... */ /* Check what the server just said to us... */
argc = get_args( argv, str2, 10 ); argc = get_args(argv, str2, 10);
if( argc < 1 ) { if (argc < 1) {
free( str2 ); free(str2);
return 0; return 0;
} }
if( strcmp( argv[0], "menuevent" ) == 0 ) { if (strcmp(argv[0], "menuevent") == 0) {
/* Ah, this is what we were waiting for ! */ /* Ah, this is what we were waiting for ! */
if( argc < 2 ) { if (argc < 2) {
report( RPT_WARNING, "Server gave invalid response" ); report(RPT_WARNING, "Server gave invalid response");
free( str2 ); free(str2);
return -1; return -1;
} }
if( strcmp( argv[1], "select" ) == 0 ) { if (strcmp(argv[1], "select") == 0) {
char * exec; char *exec;
if( argc < 3 ) {
report( RPT_WARNING, "Server gave invalid response" ); if (argc < 3) {
free( str2 ); report(RPT_WARNING, "Server gave invalid response");
free(str2);
return -1; return -1;
} }
/* Find the id */ /* Find the id */
exec = menu_find_cmd_of_id( main_menu, argv[2] ); exec = menu_find_cmd_of_id(main_menu, argv[2]);
if( !exec ) { if (!exec) {
report( RPT_WARNING, "Could not find the item id given by the server" ); report(RPT_WARNING, "Could not find the item id given by the server");
free( str2 ); free(str2);
return -1; return -1;
} }
/* The id has been found */ /* The id has been found */
exec_command( exec ); exec_command(exec);
} }
else { else {
; /* Ignore other menuevents */ ; /* Ignore other menuevents */
} }
} }
else if( strcmp( argv[0], "huh?" ) == 0 ) { else if (strcmp(argv[0], "huh?") == 0) {
/* Report errors */ /* Report errors */
report( RPT_WARNING, "Server said: \"%s\"", str ); report(RPT_WARNING, "Server said: \"%s\"", str);
} }
else { else {
; /* Ignore all other responses */ ; /* Ignore all other responses */
} }
free( str2 ); free(str2);
return 0; return 0;
} }
int exec_command( char * command ) int exec_command(char * command)
{ {
char *argv[4]; char *argv[4];
report( RPT_NOTICE, "Executing: %s", command ); report(RPT_NOTICE, "Executing: %s", command);
argv[0] = getenv( "SHELL" ); argv[0] = getenv("SHELL");
argv[1] = "-c"; argv[1] = "-c";
argv[2] = command; argv[2] = command;
argv[3] = NULL; argv[3] = NULL;
if( !argv[0] ) { if (!argv[0]) {
report( RPT_ERR, "SHELL environment variable not set." ); report(RPT_ERR, "SHELL environment variable not set.");
return -1; return -1;
} }
switch( fork() ) { switch (fork()) {
case 0: case 0:
/* We're the child. Execute the command. */ /* We're the child. Execute the command. */
execv( argv[0], argv ); execv(argv[0], argv);
exit(0); exit(0);
break; break;
case -1: case -1:
report( RPT_ERR, "Could not fork" ); report(RPT_ERR, "Could not fork");
return -1; return -1;
default: default:
/* We're the parent */ /* We're the parent */
@@ -348,14 +343,14 @@ int main_loop ()
/* Continuously check if we get a menu event... */ /* Continuously check if we get a menu event... */
while(( num_bytes = sock_recv_string( sock, buf, sizeof(buf)-1)) >= 0 ) { while ((num_bytes = sock_recv_string(sock, buf, sizeof(buf)-1)) >= 0) {
if( num_bytes == 0 ) { if (num_bytes == 0) {
usleep( 100000 ); usleep(100000);
/* Send an empty line every 3 seconds to make sure the server still exists */ /* Send an empty line every 3 seconds to make sure the server still exists */
if( w++ >= 30 ) { if (w++ >= 30) {
w = 0; w = 0;
if( sock_send_string( sock, "\n" ) < 0 ) { if (sock_send_string(sock, "\n") < 0) {
break; /* Out of while loop */ break; /* Out of while loop */
} }
} }
@@ -365,6 +360,6 @@ int main_loop ()
} }
} }
report( RPT_ERR, "Server disconnected (or connection error)" ); report(RPT_ERR, "Server disconnected (or connection error)");
return 0; return 0;
} }
+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
+70 -70
View File
@@ -20,9 +20,9 @@
#include "menu.h" #include "menu.h"
Menu * menu_find_submenu( Menu * menu, char * submenu_id ); Menu * menu_find_submenu(Menu * menu, char * submenu_id);
Menu * menu_read( char * menu_id, char * progname ) Menu * menu_read(char * menu_id, char * progname)
{ {
char buf[100]; char buf[100];
char * str; char * str;
@@ -31,107 +31,107 @@ Menu * menu_read( char * menu_id, char * progname )
Menu * menu; Menu * menu;
report( RPT_DEBUG, "Reading menu: [%s]", menu_id ); report(RPT_DEBUG, "Reading menu: [%s]", menu_id);
menu = malloc( sizeof(Menu) ); menu = malloc(sizeof(Menu));
menu->num_menucmds = 0; menu->num_menucmds = 0;
menu->num_submenus = 0; menu->num_submenus = 0;
/* 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] ? "_" : "");
buf[sizeof(buf)-1] = 0; buf[sizeof(buf)-1] = 0;
str = config_get_string( progname, buf, menu->num_menucmds, NULL ); str = config_get_string(progname, buf, menu->num_menucmds, NULL);
if( !str ) { if (!str) {
; /* No more menucommands */ ; /* No more menucommands */
} else { } else {
char * str2 = strdup( str ); char * str2 = strdup(str);
if( split( str2, ',', parts, 2 ) == 2 ) { if (split(str2, ',', parts, 2) == 2) {
menu->menucmd_name[menu->num_menucmds] = parts[0]; menu->menucmd_name[menu->num_menucmds] = parts[0];
while( parts[1][0] == ' ' ) parts[1]++; /* Skip spaces */ while (parts[1][0] == ' ') parts[1]++; /* Skip spaces */
menu->menucmd_exec[menu->num_menucmds] = parts[1]; menu->menucmd_exec[menu->num_menucmds] = parts[1];
report( RPT_DEBUG, "Found command: [%s] [%s]", parts[0], parts[1] ); report(RPT_DEBUG, "Found command: [%s] [%s]", parts[0], parts[1]);
} else { } else {
report( RPT_ERR, "Cannot read MenuCommand: \"%s\"", str ); report(RPT_ERR, "Cannot read MenuCommand: \"%s\"", str);
return NULL; return NULL;
} }
menu->num_menucmds ++; menu->num_menucmds++;
} }
} }
/* Read the submenus */ /* Read the submenus */
str = ""; str = "";
while( str && menu->num_submenus < MAX_NUM_SUBMENUS ) { while (str && menu->num_submenus < MAX_NUM_SUBMENUS) {
snprintf( buf, sizeof(buf)-1, "%s%sSubmenu", snprintf(buf, sizeof(buf)-1, "%s%sSubmenu",
menu_id, menu_id[0]?"_":"" ); menu_id, menu_id[0]?"_":"");
buf[sizeof(buf)-1] = 0; buf[sizeof(buf)-1] = 0;
str = config_get_string( progname, buf, menu->num_submenus, NULL ); str = config_get_string(progname, buf, menu->num_submenus, NULL);
if( !str ) { if (!str) {
; /* No more submenus */ ; /* No more submenus */
} else { } else {
char * str2 = strdup( str ); char * str2 = strdup(str);
if( split( str2, ',', parts, 2 ) == 2 ) { if (split(str2, ',', parts, 2) == 2) {
menu->submenu_name[menu->num_submenus] = parts[0]; menu->submenu_name[menu->num_submenus] = parts[0];
while( parts[1][0] == ' ' ) parts[1]++; /* Skip spaces */ while (parts[1][0] == ' ') parts[1]++; /* Skip spaces */
menu->submenu_id[menu->num_submenus] = parts[1]; menu->submenu_id[menu->num_submenus] = parts[1];
report( RPT_DEBUG, "Found submenu: [%s] [%s]", parts[0], parts[1] ); report(RPT_DEBUG, "Found submenu: [%s] [%s]", parts[0], parts[1]);
} else { } else {
report( RPT_ERR, "Cannot read Submenu: \"%s\"", str ); report(RPT_ERR, "Cannot read Submenu: \"%s\"", str);
return NULL; return NULL;
} }
menu->num_submenus ++; menu->num_submenus++;
} }
} }
/* And do the same for all submenus */ /* And do the same for all submenus */
for( i = 0; i < menu->num_submenus; i++ ) { for (i = 0; i < menu->num_submenus; i++) {
menu->submenu[i] = menu_read (menu->submenu_id[i], progname ); menu->submenu[i] = menu_read (menu->submenu_id[i], progname);
} }
return menu; return menu;
} }
int menu_send_to_LCDd( Menu * menu, char * id, int sock ) int menu_send_to_LCDd(Menu * menu, char * id, int sock)
{ {
int i; int i;
char buf[100]; char buf[100];
for( i = 0; i < menu->num_menucmds; i++ ) { for (i = 0; i < menu->num_menucmds; i++) {
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;
} }
for( i = 0; i < menu->num_submenus; i++ ) { for (i = 0; i < menu->num_submenus; i++) {
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;
} }
/* And do the same for all submenus */ /* And do the same for all submenus */
for( i = 0; i < menu->num_submenus; i++ ) { for (i = 0; i < menu->num_submenus; i++) {
if( menu_send_to_LCDd( menu->submenu[i], menu->submenu_id[i], sock ) < 0 ) if (menu_send_to_LCDd(menu->submenu[i], menu->submenu_id[i], sock) < 0)
return -1; return -1;
} }
return 0; return 0;
} }
char * menu_find_cmd_of_id( Menu * menu, char * id ) char * menu_find_cmd_of_id(Menu * menu, char * id)
{ {
char * p; char * p;
char * submenu_id; char * submenu_id;
@@ -139,18 +139,18 @@ char * menu_find_cmd_of_id( Menu * menu, char * id )
int i; int i;
/* Find the submenu_id part of the id */ /* Find the submenu_id part of the id */
submenu_id = strdup( id ); submenu_id = strdup(id);
p = strrchr( submenu_id, '_' ); p = strrchr(submenu_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) {
report( RPT_WARNING, "Server reported an unknown id: %s", id ); report(RPT_WARNING, "Server reported an unknown id: %s", id);
free( submenu_id ); free(submenu_id);
return NULL; return NULL;
} }
} }
@@ -159,38 +159,38 @@ 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);
return menu->menucmd_exec[i]; return menu->menucmd_exec[i];
} else { } else {
report( RPT_WARNING, "Server reported an unknown id: %s", id ); report(RPT_WARNING, "Server reported an unknown id: %s", id);
free( submenu_id ); free(submenu_id);
return NULL; return NULL;
} }
} }
Menu * menu_find_submenu( Menu * menu, char * submenu_id ) Menu * menu_find_submenu(Menu * menu, char * submenu_id)
{ {
int i; int i;
Menu * found; Menu * found;
/* Search for the submenu in this menu and all submenus */ /* Search for the submenu in this menu and all submenus */
for( i = 0; i < menu->num_submenus; i++ ) { for (i = 0; i < menu->num_submenus; i++) {
if( strcmp( menu->submenu_id[i], submenu_id ) == 0 ) if (strcmp(menu->submenu_id[i], submenu_id) == 0)
return menu->submenu[i]; return menu->submenu[i];
found = menu_find_submenu( menu->submenu[i], submenu_id ); found = menu_find_submenu(menu->submenu[i], submenu_id);
if( found ) if (found)
return found; return found;
} }
return NULL; return NULL;
} }
int split( char * str, char delim, char * parts[], int maxparts ) int split(char * str, char delim, char * parts[], int maxparts)
/* Splits a string into parts, to which pointers will be returned in &parts. /* Splits a string into parts, to which pointers will be returned in &parts.
* The return value is the number of parts. * The return value is the number of parts.
* maxparts is the maximum number of parts returned. If more parts exist * maxparts is the maximum number of parts returned. If more parts exist
@@ -204,18 +204,18 @@ 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 */
part_nr ++; part_nr++;
} }
/* and the last part... */ /* and the last part... */
parts[part_nr] = p1; parts[part_nr] = p1;
part_nr ++; part_nr++;
return part_nr; return part_nr;
} }
+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)
+20 -23
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.