summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2008-12-04 10:37:52 +0100
committerDavid Sommerseth <dazo@users.sourceforge.net>2008-12-04 10:37:52 +0100
commit6880a718670b2090bfcc5e82f2e9a9e7278ce9c9 (patch)
treeea0e87b009cb8fc6ef7aeb98d6561924b3ac46b8
parented64bb902b148702a09b6a701ff6721b2cdfff61 (diff)
downloadeurephia-6880a718670b2090bfcc5e82f2e9a9e7278ce9c9.tar.gz
eurephia-6880a718670b2090bfcc5e82f2e9a9e7278ce9c9.tar.xz
eurephia-6880a718670b2090bfcc5e82f2e9a9e7278ce9c9.zip
Wrote a replacement getopt for eurephiadm
This is because default getopt(...) is not flexible enough for eurephiadm. Example: eurephiadm -l test.log config -s var1 "value 1" eurephiadm config -s var2 value2 By using getopt in the eurephiadm.c, the -s argument will be parsed there as well, and it is no good solution how to just send arguments after the 'command' further to another argument parser. The new implementation is also not as feature rich as GNU getopt, but it is feature rich enough for the current needs.
-rw-r--r--eurephiadm/CMakeLists.txt1
-rw-r--r--eurephiadm/argparser.c48
-rw-r--r--eurephiadm/argparser.h34
-rw-r--r--eurephiadm/eurephiadm.c35
4 files changed, 100 insertions, 18 deletions
diff --git a/eurephiadm/CMakeLists.txt b/eurephiadm/CMakeLists.txt
index 2571692..fde4513 100644
--- a/eurephiadm/CMakeLists.txt
+++ b/eurephiadm/CMakeLists.txt
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 2.6)
SET(efw_ipt_SRC
eurephiadm.c
+ argparser.c
get_console_input.c
client_config.c
client_context.c
diff --git a/eurephiadm/argparser.c b/eurephiadm/argparser.c
new file mode 100644
index 0000000..1453b0e
--- /dev/null
+++ b/eurephiadm/argparser.c
@@ -0,0 +1,48 @@
+/* argparser.c -- Simple argument parser - inspired by getopt, but simpler
+ *
+ * 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 <string.h>
+
+#include "argparser.h"
+
+
+char *optarg = NULL;
+
+int eurephia_getopt(int curarg, int argc, char **argv, e_options *argopts) {
+ int i = 0;
+ char *arg = argv[curarg];
+
+ for( i = 0; argopts[i].longarg != NULL; i++ ) {
+ if( (strcmp(argopts[i].longarg, arg) == 0)
+ || (strcmp(argopts[i].shortarg, arg) == 0) )
+ {
+ if( (argopts[i].param > 0) && (argc > curarg+1) ) {
+ optarg = argv[curarg+1];
+ } else if( (argopts[i].param > 0) ) {
+ fprintf(stderr, "eurephiadm: ERROR :: Missing argument to '%s'\n", arg);
+ return -1;
+ }
+ return (int) *(&(*argopts[i].shortarg)+1);
+ }
+ }
+ fprintf(stderr, "eurephiadm: ERROR :: Unknown argument '%s'\n", arg);
+ return -1;
+}
diff --git a/eurephiadm/argparser.h b/eurephiadm/argparser.h
new file mode 100644
index 0000000..56ef01e
--- /dev/null
+++ b/eurephiadm/argparser.h
@@ -0,0 +1,34 @@
+/* argparser.c -- Simple argument parser - inspired by getopt, but simpler
+ *
+ * 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 EUREPHIA__ARGPARSE_H
+#define EUREPHIA__ARGPARSE_H
+
+typedef struct {
+ char *longarg;
+ char *shortarg;
+ int param;
+} e_options;
+
+extern char *optarg;
+
+int eurephia_getopt(int curarg, int argc, char **argv, e_options *argopts);
+
+#endif
diff --git a/eurephiadm/eurephiadm.c b/eurephiadm/eurephiadm.c
index 85de55a..f4ad873 100644
--- a/eurephiadm/eurephiadm.c
+++ b/eurephiadm/eurephiadm.c
@@ -23,8 +23,6 @@
#include <string.h>
#include <assert.h>
#include <libgen.h>
-#define _GNU_SOURCE
-#include <getopt.h>
#include <eurephia_nullsafe.h>
#include <eurephia_context.h>
@@ -34,6 +32,7 @@
#include <eurephiadb_session_common.h>
#include <eurephiadb.h>
+#include "argparser.h"
#include "client_context.h"
#include "client_config.h"
#include "client_session.h"
@@ -214,6 +213,15 @@ int main(int argc, char **argv) {
int rc = 0, i = 0, found = 0, cmdargc = 0, argi = 0;
eurephiadm_functions *call_fnc;
+ // Global arguments we accept
+ static e_options argopts[] = {
+ {"--version", "-V", 0},
+ {"--help", "-h", 0},
+ {"--log", "-l", 1},
+ {"--log-level", "-L", 1},
+ {NULL, NULL, 0}
+ };
+
if( argc < 2 ) {
cmd_Help(NULL, NULL, NULL, argc, argv);
return 1;
@@ -221,23 +229,11 @@ 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 ) {
+ for( argi = 1; argi < argc; argi++ ) {
+ if( *argv[argi] != '-' ) {
break;
}
-
+ i = eurephia_getopt(argi, argc, argv, argopts);
switch( i ) {
case 'V':
print_version(argv[0]);
@@ -260,8 +256,11 @@ int main(int argc, char **argv) {
loglevel = atoi_nullsafe(optarg);
argi++;
break;
+
+ default:
+ return 1;
+ break;
}
- argi++;
}
if( argi >= argc ) {