diff options
| author | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-01 23:19:29 +0100 |
|---|---|---|
| committer | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-01 23:19:29 +0100 |
| commit | feef382d26f4c06c1ec67f2be98db6162dfb981d (patch) | |
| tree | f4843ceaf136986086a85da68acf79952eaa76c5 | |
| parent | cfecad2e0e5efc55394b282cbfbb5caa859f5d16 (diff) | |
Prepared to have eurephiadm commands as one file per command
under the commands/ directory. The declaration of each command
is done in the commands.h file.
Implemented changes from commit 525361b6dc382d5086e9e2b0539f3248ee239023
in eurephiadm as well. Reorganised and improved some comments as well.
| -rw-r--r-- | eurephiadm/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | eurephiadm/commands.h | 29 | ||||
| -rw-r--r-- | eurephiadm/commands/list_users.c | 36 | ||||
| -rw-r--r-- | eurephiadm/eurephiadm.c | 72 |
4 files changed, 108 insertions, 30 deletions
diff --git a/eurephiadm/CMakeLists.txt b/eurephiadm/CMakeLists.txt index 00b798f..d08386e 100644 --- a/eurephiadm/CMakeLists.txt +++ b/eurephiadm/CMakeLists.txt @@ -7,6 +7,7 @@ SET(efw_ipt_SRC client_config.c client_context.c client_session.c + commands/list_users.c ../common/eurephia_log.c ../common/eurephia_getsym.c ../common/eurephia_values.c diff --git a/eurephiadm/commands.h b/eurephiadm/commands.h new file mode 100644 index 0000000..e707f10 --- /dev/null +++ b/eurephiadm/commands.h @@ -0,0 +1,29 @@ +/* commands.h -- All eurephiadm commands are declared here + * The function is found in the ./commands dir + * where each command have their own file + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef EUREPHIADM_COMMANDS_H_ +# define EUREPHIADM_COMMANDS_H_ + +int cmd_ListUsers(eurephiaCTX *, eurephiaSESSION *, eurephiaVALUES *cfg, int argc, char **argv); + + +#endif /* !COMMANDS_H_ */ diff --git a/eurephiadm/commands/list_users.c b/eurephiadm/commands/list_users.c new file mode 100644 index 0000000..c5aebb1 --- /dev/null +++ b/eurephiadm/commands/list_users.c @@ -0,0 +1,36 @@ +/* list_users.c -- eurephiadm command + * Retrieves a list over all users and displays them + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <eurephia_nullsafe.h> +#include <eurephia_context.h> +#include <eurephia_log.h> +#include <eurephia_values_struct.h> +#include <eurephiadb_session_struct.h> + +int cmd_ListUsers(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { + fprintf(stdout, "** List users is not implemented yet\n"); + return 1; +} + diff --git a/eurephiadm/eurephiadm.c b/eurephiadm/eurephiadm.c index 06183e8..bbd21f2 100644 --- a/eurephiadm/eurephiadm.c +++ b/eurephiadm/eurephiadm.c @@ -36,10 +36,12 @@ #include "client_config.h" #include "client_session.h" #include "get_console_input.h" +#include "commands.h" #define EUREPHIADMVERSION "1.0" #define MAX_ARGUMENTS 64 + typedef struct { char *command; int need_session; @@ -49,23 +51,23 @@ typedef struct { int (*function)(eurephiaCTX *, eurephiaSESSION *, eurephiaVALUES *, int argc, char **argv); } eurephiadm_functions; +/* eurephiadm commands located in this file */ +int cmd_Help(eurephiaCTX *, eurephiaSESSION *, eurephiaVALUES *cfg, int argc, char **argv); +int cmd_Logout(eurephiaCTX *, eurephiaSESSION *, eurephiaVALUES *cfg, int argc, char **argv); +int cmd_ShowCfg(eurephiaCTX *, eurephiaSESSION *, eurephiaVALUES *cfg, int argc, char **argv); -int cmd_help(eurephiaCTX *, eurephiaSESSION *, eurephiaVALUES *cfg, int argc, char **argv); -int cmd_logout(eurephiaCTX *, eurephiaSESSION *, eurephiaVALUES *cfg, int argc, char **argv); -int cmd_listusers(eurephiaCTX *, eurephiaSESSION *, eurephiaVALUES *cfg, int argc, char **argv); -int cmd_show_cfg(eurephiaCTX *, eurephiaSESSION *, eurephiaVALUES *cfg, int argc, char **argv); - +// Other commands are declared in ./commands.h and the function implemented in +// ./commands/*.c ... where each command should have their own file. static const eurephiadm_functions cmdline_functions[] = { - {"help", 0, NULL, "This help screen", NULL, cmd_help}, - {"logout", 1, NULL, "Logout from an open session", NULL, cmd_logout}, - {"listusers", 1, NULL, "List all registered users", NULL, cmd_listusers}, - {"show-settings", 0, NULL, "List all config variables", NULL, cmd_show_cfg}, + {"help", 0, NULL, "This help screen", NULL, cmd_Help}, + {"logout", 1, NULL, "Logout from an open session", NULL, cmd_Logout}, + {"listusers", 1, NULL, "List all registered users", NULL, cmd_ListUsers}, + {"show-settings", 0, NULL, "List all config variables", NULL, cmd_ShowCfg}, {NULL, 0, NULL, NULL, NULL} }; - -int cmd_help(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { +int cmd_Help(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { eurephiadm_functions *func = NULL; int i; char *prg = basename(argv[0]); @@ -90,15 +92,14 @@ int cmd_help(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int a return 0; } -int cmd_logout(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { - return eDBadminLogout(ctx, sess); -} - -int cmd_listusers(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { - return 1; +int cmd_Logout(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { + int rc = 0; + rc = eDBadminLogout(ctx, sess); + fprintf(stdout, "%s\n", (rc == 0 ? "Logged out succesfully" : "Logout failed.\n")); + return rc; } -int cmd_show_cfg(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { +int cmd_ShowCfg(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { eurephiaVALUES *ptr = NULL; fprintf(stdout, "Confuriation settings:\n"); @@ -183,16 +184,21 @@ int main(int argc, char **argv) { eurephiaSESSION *session = NULL; eurephiaVALUES *cfg = NULL; char *sesskey_file = NULL; - char *dbdriver = NULL, *dbparams = NULL; + char *dbparams = NULL; int rc = 0, i, found = 0; eurephiadm_functions *call_fnc; if( argc < 2 ) { - cmd_help(NULL, NULL, NULL, argc, argv); + cmd_Help(NULL, NULL, NULL, argc, argv); return 1; } // Parse argument line + + + + + // Find the command requested and save a pointer to that command's C function for( i = 0; cmdline_functions[i].command != NULL; i++ ) { call_fnc = (eurephiadm_functions *)&cmdline_functions[i]; @@ -202,6 +208,7 @@ int main(int argc, char **argv) { } } + // Exit with error if command is not found if( found == 0 ) { fprintf(stderr, "%s: No such command '%s'\n", argv[0], argv[1]); return 5; @@ -216,27 +223,31 @@ int main(int argc, char **argv) { // If function do not need a logged in session, go a head process it now and exit if( call_fnc->need_session == 0 ) { - return call_fnc->function(NULL, NULL, cfg, argc, argv); + rc = call_fnc->function(NULL, NULL, cfg, argc, argv); + eFree_values(NULL, cfg); + return rc; } - // Get database and connection ifo - dbdriver = eGet_value(cfg, "database_driver"); - dbparams = eGet_value(cfg, "database_params"); + // + // Load database driver, setup a context and create or reuse an open session + // and then call the command's function + // - // Create an eurephia context and load database driver - ctx = eurephiaCTX_init(stderr, 50, dbdriver); + // Create a eurephia context and load database driver + ctx = eurephiaCTX_init(NULL, 0, cfg); if( ctx == NULL ) { fprintf(stderr, "Could not initialise a eurephia context.\n"); return 3; } // Connect to the database + dbparams = eGet_value(cfg, "database_params"); if( !eurephia_ConnectDB(ctx, dbparams) ) { return 4; } - // Load session file, if it exists. + // Load session file, if it exists. (reuse open session) if( (sesskey_file = read_session_file(ctx)) == NULL ) { // No session file - go straight to login if( strcmp(call_fnc->command, "logout") == 0) { @@ -263,14 +274,15 @@ int main(int argc, char **argv) { } free_nullsafe(sesskey_file); - if(session != NULL ) { - // If we have an open session now, write a session file + // If we have an open session now, call the requested command + if(session != NULL ) { + // Write session info to a file if( !write_session_file(ctx, session) ) { eurephia_log(ctx, LOG_ERROR, 0, "Could not write session file. Login needed for actions"); }; - // Process admin commands here + // Execute the requested command rc = call_fnc->function(ctx, session, cfg, argc, argv); // Remove session info from memory |
