diff --git a/server/commands/client_commands.c b/server/commands/client_commands.c index b766b67..4a0bd25 100644 --- a/server/commands/client_commands.c +++ b/server/commands/client_commands.c @@ -1,5 +1,8 @@ +/* \file client_commands.c + * Defines handlers for general client commands. + */ + /* - * client_commands.c * This file is part of LCDd, the lcdproc server. * * This file is released under the GNU General Public License. Refer to the @@ -14,9 +17,6 @@ * * The client's available function set is defined here, as is the syntax * for each command. - * - * This particular file defines actions concerning clients. - * */ #include @@ -38,15 +38,13 @@ * Debugging only.. prints out a list of arguments it receives */ int -test_func_func (Client * c, int argc, char **argv) +test_func_func(Client *c, int argc, char **argv) { int i; - char str[256]; for (i = 0; i < argc; i++) { - snprintf (str, sizeof(str), "%s: %i -> %s\n", __FUNCTION__, i, argv[i]); - report (RPT_INFO, "%s", str); - sock_send_string (c->sock, str); + report(RPT_INFO, "%s: %i -> %s", __FUNCTION__, i, argv[i]); + sock_printf(c->sock, "%s: %i -> %s\n", __FUNCTION__, i, argv[i]); } return 0; } @@ -59,10 +57,8 @@ test_func_func (Client * c, int argc, char **argv) * Usage: hello */ int -hello_func (Client * c, int argc, char **argv) +hello_func(Client *c, int argc, char **argv) { - char str[256]; - /* TODO: Give *real* info about the server/lcd...*/ if (argc > 1) { @@ -71,14 +67,12 @@ hello_func (Client * c, int argc, char **argv) debug(RPT_INFO, "Hello!"); - memset(str, '\0', sizeof(str)); - snprintf(str, sizeof(str), "connect LCDproc %s protocol %s lcd wid %i hgt %i cellwid %i cellhgt %i\n", + sock_printf(c->sock, "connect LCDproc %s protocol %s lcd wid %i hgt %i cellwid %i cellhgt %i\n", VERSION, PROTOCOL_VERSION, display_props->width, display_props->height, display_props->cellwidth, display_props->cellheight); - sock_send_string (c->sock, str); - + /* make note that client has sent hello */ c->ack = 1; return 0; @@ -92,7 +86,7 @@ hello_func (Client * c, int argc, char **argv) * Usage: bye */ int -bye_func (Client * c, int argc, char **argv) +bye_func(Client *c, int argc, char **argv) { if (c != NULL) { debug(RPT_INFO, "Bye, %s!", (c->name != NULL) ? c->name : "unknown client"); @@ -108,12 +102,10 @@ bye_func (Client * c, int argc, char **argv) * Usage: client_set -name */ int -client_set_func (Client * c, int argc, char **argv) +client_set_func(Client *c, int argc, char **argv) { int i; - char str[256]; - memset(str, '\0', sizeof(str)); if (!c->ack) return 1; @@ -124,40 +116,35 @@ client_set_func (Client * c, int argc, char **argv) i = 1; do { - char *p; + char *p = argv[i]; - /* This bit of code means that "-name" is the same as "name"...*/ - p = argv[i]; + /* ignore leading '-' in options: we allow both forms */ if (*p == '-') p++; - /* Handle the "name" parameter*/ - if (strcmp (p, "name") == 0) { + /* Handle the "name" option */ + if (strcmp(p, "name") == 0) { i++; if (argv[i] == '\0') { sock_printf_error(c->sock, "internal error: no parameter #%d\n", i); continue; } - if (strlen(argv[i]) > sizeof(str) -1) { - sock_send_error(c->sock, "name too long\n"); - } else { - strncpy(str, argv[i], sizeof(str) - 1); + debug(RPT_DEBUG, "client_set: name=\"%s\"", argv[i]); - debug(RPT_DEBUG, "client_set: name=\"%s\"", argv[i]); + /* set the name...*/ + if (c->name != NULL) + free(c->name); - /* set the name...*/ - if (c->name) - free (c->name); - - if ((c->name = strdup (str)) == NULL) { - sock_send_error(c->sock, "error allocating memory!\n"); - } else { - sock_send_string(c->sock, "success\n"); - i++; /* bypass argument (name string)*/ - } + if ((c->name = strdup(argv[i])) == NULL) { + sock_send_error(c->sock, "error allocating memory!\n"); } - } else { + else { + sock_send_string(c->sock, "success\n"); + i++; /* bypass argument (name string)*/ + } + } + else { sock_printf_error(c->sock, "invalid parameter (%s)\n", p); } } while (++i < argc); @@ -171,9 +158,8 @@ client_set_func (Client * c, int argc, char **argv) * * Usage: client_add_key [-exclusively|-shared] {}+ */ -#define BUFLEN 80 int -client_add_key_func (Client * c, int argc, char **argv) +client_add_key_func(Client *c, int argc, char **argv) { int exclusively = 0; int argnr; @@ -187,20 +173,20 @@ client_add_key_func (Client * c, int argc, char **argv) } argnr = 1; - if( argv[argnr][0] == '-' ) { - if( strcmp( argv[argnr], "-shared") == 0 ) { + if (argv[argnr][0] == '-') { + if (strcmp( argv[argnr], "-shared") == 0) { exclusively = 0; } - else if( strcmp( argv[argnr], "-exclusively") == 0 ) { + else if(strcmp(argv[argnr], "-exclusively") == 0) { exclusively = 1; } else { sock_printf_error(c->sock, "Invalid option: %s\n", argv[argnr]); } - argnr ++; + argnr++; } - for ( ; argnr < argc; argnr++ ) { - if( input_reserve_key( argv[argnr], exclusively, c ) < 0 ) { + for ( ; argnr < argc; argnr++) { + if (input_reserve_key(argv[argnr], exclusively, c) < 0) { sock_printf_error(c->sock, "Could not reserve key \"%s\"\n", argv[argnr]); } } @@ -216,7 +202,7 @@ client_add_key_func (Client * c, int argc, char **argv) * Usage: client_del_key {}+ */ int -client_del_key_func (Client * c, int argc, char **argv) +client_del_key_func(Client *c, int argc, char **argv) { int argnr; @@ -228,8 +214,8 @@ client_del_key_func (Client * c, int argc, char **argv) return 0; } - for( argnr=1; argnr < argc; argnr++) { - input_release_key( argv[argnr], c ); + for (argnr = 1; argnr < argc; argnr++) { + input_release_key(argv[argnr], c); } sock_send_string(c->sock, "success\n"); @@ -242,7 +228,7 @@ client_del_key_func (Client * c, int argc, char **argv) * Usage: backlight {on|off|toggle|blink|flash} */ int -backlight_func (Client * c, int argc, char **argv) +backlight_func(Client *c, int argc, char **argv) { if (!c->ack) return 1; @@ -252,27 +238,27 @@ backlight_func (Client * c, int argc, char **argv) return 0; } - debug (RPT_DEBUG, "backlight(%s)", argv[1]); + debug(RPT_DEBUG, "backlight(%s)", argv[1]); //backlight = (backlight && 1); /* only preserves ON/OFF bit*/ if (strcmp ("on", argv[1]) == 0) { c->backlight = BACKLIGHT_ON; - - } else if (strcmp ("off", argv[1]) == 0) { + } + else if (strcmp ("off", argv[1]) == 0) { c->backlight = BACKLIGHT_OFF; - - } else if (strcmp ("toggle", argv[1]) == 0) { + } + else if (strcmp ("toggle", argv[1]) == 0) { if (c->backlight == BACKLIGHT_ON) c->backlight = BACKLIGHT_OFF; else if (c->backlight == BACKLIGHT_OFF) c->backlight = BACKLIGHT_ON; - - } else if (strcmp ("blink", argv[1]) == 0) { + } + else if (strcmp ("blink", argv[1]) == 0) { c->backlight |= BACKLIGHT_BLINK; - - } else if (strcmp ("flash", argv[1]) == 0) { + } + else if (strcmp ("flash", argv[1]) == 0) { c->backlight |= BACKLIGHT_FLASH; } @@ -288,19 +274,16 @@ backlight_func (Client * c, int argc, char **argv) * Usage: info */ int -info_func (Client * c, int argc, char **argv) +info_func(Client *c, int argc, char **argv) { - char str[1024]; + if (!c->ack) + return 1; if (argc > 1) { sock_send_error(c->sock, "Extra arguments ignored...\n"); } - memset(str, '\0', sizeof(str)); - snprintf (str, sizeof(str)-1, "%s\n", drivers_get_info()); - str[sizeof(str)-1] = '\0'; - - sock_send_string (c->sock, str); + sock_printf(c->sock, "%s\n", drivers_get_info()); return 0; } diff --git a/server/commands/client_commands.h b/server/commands/client_commands.h index dd8d742..181a51a 100644 --- a/server/commands/client_commands.h +++ b/server/commands/client_commands.h @@ -1,5 +1,8 @@ +/* \file client_commands.h + * Declares handlers for general client commands. + */ + /* - * client_commands.h * This file is part of LCDd, the lcdproc server. * * This file is released under the GNU General Public License. Refer to the @@ -13,12 +16,12 @@ #ifndef COMMANDS_CLIENT_H #define COMMANDS_CLIENT_H -int hello_func (Client * c, int argc, char **argv); -int bye_func (Client * c, int argc, char **argv); -int client_set_func (Client * c, int argc, char **argv); -int client_add_key_func (Client * c, int argc, char **argv); -int client_del_key_func (Client * c, int argc, char **argv); -int backlight_func (Client * c, int argc, char **argv); +int hello_func(Client *c, int argc, char **argv); +int bye_func(Client *c, int argc, char **argv); +int client_set_func(Client *c, int argc, char **argv); +int client_add_key_func(Client *c, int argc, char **argv); +int client_del_key_func(Client *c, int argc, char **argv); +int backlight_func(Client *c, int argc, char **argv); #endif diff --git a/server/commands/command_list.c b/server/commands/command_list.c index 1d3f97e..b20424d 100644 --- a/server/commands/command_list.c +++ b/server/commands/command_list.c @@ -1,5 +1,8 @@ +/* \file command_list.c + * Defines the dispatcher for handlers dealing with the client commands. + */ + /* - * commands/command_list.c * This file is part of LCDd, the lcdproc server. * * This file is released under the GNU General Public License. Refer to the @@ -14,7 +17,6 @@ * * The client's available function set is defined here, as is the syntax * for each command. <-- TODO ! - * */ #include "command_list.h" diff --git a/server/commands/command_list.h b/server/commands/command_list.h index 599b1ee..4bd2be7 100644 --- a/server/commands/command_list.h +++ b/server/commands/command_list.h @@ -1,5 +1,8 @@ +/* \file command_list.h + * Declares client command dispatcher function. + */ + /* - * commands/command_list.h * This file is part of LCDd, the lcdproc server. * * This file is released under the GNU General Public License. Refer to the @@ -19,7 +22,7 @@ point to a function to call, defined below. */ -typedef int (*CommandFunc) (Client * c, int argc, char **argv); +typedef int (*CommandFunc) (Client *c, int argc, char **argv); typedef struct client_function { char *keyword; diff --git a/server/commands/menu_commands.c b/server/commands/menu_commands.c index 025ed34..53778f9 100644 --- a/server/commands/menu_commands.c +++ b/server/commands/menu_commands.c @@ -1,5 +1,8 @@ +/* \file menu_commands.c + * Defines handlers for client commands concerning menus. + */ + /* - * menu_commands.c * This file is part of LCDd, the lcdproc server. * * This file is released under the GNU General Public License. Refer to the @@ -16,9 +19,6 @@ * * The client's available function set is defined here, as is the syntax * for each command. - * - * This particular file defines actions concerning client supplied menus. - * */ #include @@ -51,12 +51,11 @@ static char *argv2string(int argc, char **argv) char *rtn = NULL; int len; int i; - for (i = len = 0; i < argc; ++i) + for (i = len = 0; i < argc; i++) len += strlen(argv[i]) + 1; rtn = malloc(len + 1); rtn[0] = '\0'; - for (i = len = 0; i < argc; ++i) - { + for (i = len = 0; i < argc; i++) { strcat(rtn, argv[i]); strcat(rtn, " "); } @@ -87,27 +86,27 @@ static char *argv2string(int argc, char **argv) * - ip */ int -menu_add_item_func (Client * c, int argc, char **argv) +menu_add_item_func(Client *c, int argc, char **argv) { - char * menu_id; - char * item_id; - char * text = NULL; - Menu * menu = NULL; - MenuItem * item; + char *menu_id; + char *item_id; + char *text = NULL; + Menu *menu = NULL; + MenuItem *item; MenuItemType itemtype; - char** argv_set = NULL; + char **argv_set = NULL; - debug (RPT_DEBUG, "%s( Client [%d], %s, %s )", + debug(RPT_DEBUG, "%s(Client [%d], %s, %s)", __FUNCTION__, c->sock, argv[1], argv[2]); if (!c->ack) return 1; - if (!c->name) { + if (c->name == NULL) { sock_send_error(c->sock, "You need to give your client a name first\n"); return 0; } - if ((argc < 4 )) { + if (argc < 4) { sock_send_error(c->sock, "Usage: menu_add_item []\n"); return 0; } @@ -116,40 +115,37 @@ menu_add_item_func (Client * c, int argc, char **argv) item_id = argv[2]; /* Does the client have a menu already ? */ - if (!c->menu) { + if (c->menu == NULL) { /* We need to create it */ - report( RPT_INFO, "Client [%d] is using the menu", c->sock ); - c->menu = menu_create ("_client_menu_", menu_commands_handler, c->name, c); - menu_add_item (main_menu, c->menu); + report(RPT_INFO, "Client [%d] is using the menu", c->sock); + c->menu = menu_create("_client_menu_", menu_commands_handler, c->name, c); + menu_add_item(main_menu, c->menu); } - if ( menu_id[0] == 0 ) { - /* No menu specified = client's main menu */ - menu = c->menu; - } else { - /* A specified menu */ - menu = menu_find_item (c->menu, menu_id, true); - } - if (!menu) { + /* use either the given menu or the client's main menu if none was specified */ + menu = (menu_id[0] != '\0') + ? menu_find_item(c->menu, menu_id, true) + : c->menu; + if (menu == NULL) { sock_send_error(c->sock, "Cannot find menu id\n"); return 0; } - item = menu_find_item (c->menu, item_id, true); - if (item) { + item = menu_find_item(c->menu, item_id, true); + if (item != NULL) { sock_send_error(c->sock, "Item id already in use\n"); return 0; } /* Find menuitem type */ - itemtype = menuitem_typename_to_type (argv[3]); + itemtype = menuitem_typename_to_type(argv[3]); if (itemtype == -1) { sock_send_error(c->sock, "Invalid menuitem type\n"); return 0; } /* Is a text given (options don't count)? */ - if (argc >= 5 && argv[4][0] != '-') { + if ((argc >= 5) && (argv[4][0] != '-')) { text = argv[4]; } else { @@ -159,59 +155,58 @@ menu_add_item_func (Client * c, int argc, char **argv) /* Create the menuitem */ switch (itemtype) { case MENUITEM_MENU: - item = menu_create (item_id, menu_commands_handler, text, c); + item = menu_create(item_id, menu_commands_handler, text, c); break; case MENUITEM_ACTION: - item = menuitem_create_action (item_id, menu_commands_handler, text, c, + item = menuitem_create_action(item_id, menu_commands_handler, text, c, MENURESULT_NONE); break; case MENUITEM_CHECKBOX: - item = menuitem_create_checkbox (item_id, menu_commands_handler, text, c, + item = menuitem_create_checkbox(item_id, menu_commands_handler, text, c, false, false); break; case MENUITEM_RING: - item = menuitem_create_ring (item_id, menu_commands_handler, text, c, + item = menuitem_create_ring(item_id, menu_commands_handler, text, c, "", 0); break; case MENUITEM_SLIDER: - item = menuitem_create_slider (item_id, menu_commands_handler, text, c, + item = menuitem_create_slider(item_id, menu_commands_handler, text, c, "", "", 0, 100, 1, 25); break; case MENUITEM_NUMERIC: - item = menuitem_create_numeric (item_id, menu_commands_handler, text, c, + item = menuitem_create_numeric(item_id, menu_commands_handler, text, c, 0, 100, 0); break; case MENUITEM_ALPHA: - item = menuitem_create_alpha (item_id, menu_commands_handler, text, c, + item = menuitem_create_alpha(item_id, menu_commands_handler, text, c, 0, 0, 10, true, false, true, "-./", ""); break; case MENUITEM_IP: - item = menuitem_create_ip (item_id, menu_commands_handler, text, c, + item = menuitem_create_ip(item_id, menu_commands_handler, text, c, 0, "192.168.1.245"); break; default: assert(!"unexpected menuitem type"); } - menu_add_item (menu, item); - menuscreen_inform_item_modified (menu); + menu_add_item(menu, item); + menuscreen_inform_item_modified(menu); sock_send_string(c->sock, "success\n"); /* are there any options (starting with '-')? * - create a temporary argv for menu_set_item() call */ - if (argc > 5 || argv[4][0] == '-') { + if ((argc > 5) || (argv[4][0] == '-')) { // menu_add_item [] // menu_set_item {