summaryrefslogtreecommitdiffstats
path: root/eurephiadm/eurephiadm.c
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2008-12-02 21:10:07 +0100
committerDavid Sommerseth <dazo@users.sourceforge.net>2008-12-02 21:10:07 +0100
commitd162402079fac34d615bfbce449150a33b321101 (patch)
tree7cc5e96cb893d7df428f20f8adf52578d2a7e3c6 /eurephiadm/eurephiadm.c
parent10d775f610b8e96df2d496a86e2a477da4d85cc6 (diff)
downloadeurephia-d162402079fac34d615bfbce449150a33b321101.tar.gz
eurephia-d162402079fac34d615bfbce449150a33b321101.tar.xz
eurephia-d162402079fac34d615bfbce449150a33b321101.zip
Added proper argument handling. Allowing log settings to be overridden by command line
Diffstat (limited to 'eurephiadm/eurephiadm.c')
-rw-r--r--eurephiadm/eurephiadm.c95
1 files changed, 83 insertions, 12 deletions
diff --git a/eurephiadm/eurephiadm.c b/eurephiadm/eurephiadm.c
index bbd21f2..39fcab3 100644
--- a/eurephiadm/eurephiadm.c
+++ b/eurephiadm/eurephiadm.c
@@ -23,6 +23,8 @@
#include <string.h>
#include <assert.h>
#include <libgen.h>
+#define _GNU_SOURCE
+#include <getopt.h>
#include <eurephia_nullsafe.h>
#include <eurephia_context.h>
@@ -56,6 +58,7 @@ int cmd_Help(eurephiaCTX *, eurephiaSESSION *, eurephiaVALUES *cfg, int argc, ch
int cmd_Logout(eurephiaCTX *, eurephiaSESSION *, eurephiaVALUES *cfg, int argc, char **argv);
int cmd_ShowCfg(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.
@@ -67,17 +70,34 @@ static const eurephiadm_functions cmdline_functions[] = {
{NULL, 0, NULL, NULL, NULL}
};
+char *print_version(char *fprg) {
+ char *prg = basename(fprg);
+
+ fprintf(stdout, "%s (v%s) - eurephia administration utility\n"
+ "Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net>\n",
+ prg, EUREPHIADMVERSION);
+ return prg;
+}
+
+
int cmd_Help(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
eurephiadm_functions *func = NULL;
int i;
- char *prg = basename(argv[0]);
+ char *prg = NULL;
- fprintf(stdout, "%s (v%s) - eurephia administration utility\n"
- "Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net>\n\n",
- prg, EUREPHIADMVERSION);
+ prg = print_version(argv[0]);
+
+ fprintf(stdout, "\n Usage: %s [global options] <command> [command options]\n\n", prg);
- fprintf(stdout, " Usage: %s [options] <command> [command options]\n\n", prg);
fprintf(stdout," %-20.20s %-20.20s %-30.30s\n",
+ "-V | --version", "", "Print version of eurephiadm");
+ fprintf(stdout," %-20.20s %-20.20s %-30.30s\n",
+ "-l | --log", "<file name>", "Log to file");
+ fprintf(stdout," %-20.20s %-20.20s %-30.30s\n",
+ "-L | --log-level", "<log level>", "Set log verbosity");
+
+
+ fprintf(stdout,"\n %-20.20s %-20.20s %-30.30s\n",
"Command","Arguments","Description");
fprintf(stdout, " ------------------------------------------------------------------------\n");
@@ -180,12 +200,14 @@ eurephiaSESSION *do_login(eurephiaCTX *ctx) {
int main(int argc, char **argv) {
+ FILE *logfile = NULL;
+ int loglevel = 0;
eurephiaCTX *ctx = NULL;
eurephiaSESSION *session = NULL;
eurephiaVALUES *cfg = NULL;
char *sesskey_file = NULL;
- char *dbparams = NULL;
- int rc = 0, i, found = 0;
+ char *dbparams = NULL, *cmdargv[MAX_ARGUMENTS];
+ int rc = 0, i = 0, found = 0, cmdargc = 0, argi = 0;
eurephiadm_functions *call_fnc;
if( argc < 2 ) {
@@ -194,15 +216,64 @@ int main(int argc, char **argv) {
}
// Parse argument line
+ argi = 1;
+ while( 1 ) {
+ int optidx = 0;
+
+ /* global arguments */
+ static struct option argopts[] = {
+ {"version", 0, 0, 'V'},
+ {"help", 0, 0, 'h'},
+ {"log", 1, 0, 'l'},
+ {"log-level", 1, 0, 'L'},
+ {0,0,0,0}
+ };
+ i = getopt_long(argc, argv, "Vhl:L:", argopts, &optidx);
+ if( i == -1 ) {
+ break;
+ }
+
+ switch( i ) {
+ case 'V':
+ print_version(argv[0]);
+ return 0;
+
+ case 'h':
+ cmd_Help(NULL, NULL, NULL, argc, argv);
+ return 0;
+
+ case 'l':
+ if( (logfile = fopen(optarg, "wb")) == NULL ) {
+ fprintf(stderr, "%s: ERROR :: Could not open log file: %s\n",
+ basename(argv[0]), optarg);
+ return 0;
+ }
+ argi++;
+ break;
+ case 'L':
+ loglevel = atoi_nullsafe(optarg);
+ argi++;
+ break;
+ }
+ argi++;
+ }
+ if( argi >= argc ) {
+ fprintf(stderr, "%s: ERROR :: No command given\n", basename(argv[0]));
+ return 1;
+ } else {
+ for( i = argi; ((i < argc) && (i < MAX_ARGUMENTS)); i++ ) {
+ cmdargv[cmdargc++] = argv[i];
+ }
+ }
// 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];
- if( strcmp(call_fnc->command, argv[1]) == 0 ) {
+ if( strcmp(call_fnc->command, cmdargv[0]) == 0 ) {
found = 1;
break;
}
@@ -210,7 +281,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]);
+ fprintf(stderr, "%s: No such command '%s'\n", basename(argv[0]), argv[argi]);
return 5;
}
@@ -223,7 +294,7 @@ 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 ) {
- rc = call_fnc->function(NULL, NULL, cfg, argc, argv);
+ rc = call_fnc->function(NULL, NULL, cfg, cmdargc, cmdargv);
eFree_values(NULL, cfg);
return rc;
}
@@ -235,7 +306,7 @@ int main(int argc, char **argv) {
//
// Create a eurephia context and load database driver
- ctx = eurephiaCTX_init(NULL, 0, cfg);
+ ctx = eurephiaCTX_init(logfile, loglevel, cfg);
if( ctx == NULL ) {
fprintf(stderr, "Could not initialise a eurephia context.\n");
return 3;
@@ -283,7 +354,7 @@ int main(int argc, char **argv) {
};
// Execute the requested command
- rc = call_fnc->function(ctx, session, cfg, argc, argv);
+ rc = call_fnc->function(ctx, session, cfg, cmdargc, cmdargv);
// Remove session info from memory
eDBfree_session(ctx, session);