Browse Source

Use getopt_long() for utilities if getopt.h exists.

git-svn-id: https://klish.googlecode.com/svn/trunk@259 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
f947f5c11b
6 changed files with 61 additions and 10 deletions
  1. 12 4
      bin/clish.cpp
  2. 10 3
      bin/konf.c
  3. 11 3
      bin/konfd.c
  4. 3 0
      config.h.in
  5. 19 0
      configure
  6. 6 0
      configure.ac

+ 12 - 4
bin/clish.cpp

@@ -11,8 +11,10 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <getopt.h>
 #include <unistd.h>
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#endif
 
 #include "clish/shell.h"
 #include "clish/internal.h"
@@ -24,6 +26,7 @@
 /* #define version(v) printf("%s\n", QUOTE(v)) */
 #define version(v) printf("%s\n", v)
 
+/* Hooks */
 static clish_shell_hooks_t my_hooks = {
     NULL, /* don't worry about init callback */
     clish_access_callback,
@@ -52,7 +55,8 @@ int main(int argc, char **argv)
 	const char *viewid = getenv("CLISH_VIEWID");
 
 	static const char *shortopts = "hvs:ledx:w:i:";
-/*	static const struct option longopts[] = {
+#ifdef HAVE_GETOPT_H
+	static const struct option longopts[] = {
 		{"help",	0, NULL, 'h'},
 		{"version",	0, NULL, 'v'},
 		{"socket",	1, NULL, 's'},
@@ -64,13 +68,17 @@ int main(int argc, char **argv)
 		{"viewid",	1, NULL, 'i'},
 		{NULL,		0, NULL, 0}
 	};
-*/
+#endif
+
 	/* Parse command line options */
 	optind = 0;
 	while(1) {
 		int opt;
-/*		opt = getopt_long(argc, argv, shortopts, longopts, NULL); */
+#ifdef HAVE_GETOPT_H
+		opt = getopt_long(argc, argv, shortopts, longopts, NULL);
+#else
 		opt = getopt(argc, argv, shortopts);
+#endif
 		if (-1 == opt)
 			break;
 		switch (opt) {

+ 10 - 3
bin/konf.c

@@ -12,7 +12,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#ifdef HAVE_GETOPT_H
 #include <getopt.h>
+#endif
 
 #include "konf/net.h"
 #include "konf/query.h"
@@ -46,19 +48,24 @@ int main(int argc, char **argv)
 	unsigned i = 0;
 
 	static const char *shortopts = "hvs:";
-/*	static const struct option longopts[] = {
+#ifdef HAVE_GETOPT_H
+	static const struct option longopts[] = {
 		{"help",	0, NULL, 'h'},
 		{"version",	0, NULL, 'v'},
 		{"socket",	1, NULL, 's'},
 		{NULL,		0, NULL, 0}
 	};
-*/
+#endif
+
 	/* Parse command line options */
 	optind = 0;
 	while(1) {
 		int opt;
-/*		opt = getopt_long(argc, argv, shortopts, longopts, NULL); */
+#ifdef HAVE_GETOPT_H
+		opt = getopt_long(argc, argv, shortopts, longopts, NULL);
+#else
 		opt = getopt(argc, argv, shortopts);
+#endif
 		if (-1 == opt)
 			break;
 		switch (opt) {

+ 11 - 3
bin/konfd.c

@@ -21,6 +21,9 @@
 #include <string.h>
 #include <sys/select.h>
 #include <signal.h>
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#endif
 
 #include "clish/private.h"
 #include "konf/tree.h"
@@ -76,19 +79,24 @@ int main(int argc, char **argv)
 
 	/* Command line options */
 	static const char *shortopts = "hvs:";
-/*	static const struct option longopts[] = {
+#ifdef HAVE_GETOPT_H
+	static const struct option longopts[] = {
 		{"help",	0, NULL, 'h'},
 		{"version",	0, NULL, 'v'},
 		{"socket",	1, NULL, 's'},
 		{NULL,		0, NULL, 0}
 	};
-*/
+#endif
+
 	/* Parse command line options */
 	optind = 0;
 	while(1) {
 		int opt;
-/*		opt = getopt_long(argc, argv, shortopts, longopts, NULL); */
+#ifdef HAVE_GETOPT_H
+		opt = getopt_long(argc, argv, shortopts, longopts, NULL);
+#else
 		opt = getopt(argc, argv, shortopts);
+#endif
 		if (-1 == opt)
 			break;
 		switch (opt) {

+ 3 - 0
config.h.in

@@ -6,6 +6,9 @@
 /* Define to 1 if you have the <dlfcn.h> header file. */
 #undef HAVE_DLFCN_H
 
+/* Define to 1 if you have the <getopt.h> header file. */
+#undef HAVE_GETOPT_H
+
 /* Define to 1 if you have the <inttypes.h> header file. */
 #undef HAVE_INTTYPES_H
 

+ 19 - 0
configure

@@ -15353,6 +15353,25 @@ else
 $as_echo "$as_me: WARNING: Skipping BFD library checks because of GPL Licence" >&2;}
 fi
 
+################################
+# Check for getopt_long()
+################################
+for ac_header in getopt.h
+do :
+  ac_fn_c_check_header_mongrel "$LINENO" "getopt.h" "ac_cv_header_getopt_h" "$ac_includes_default"
+if test "x$ac_cv_header_getopt_h" = x""yes; then :
+  cat >>confdefs.h <<_ACEOF
+#define HAVE_GETOPT_H 1
+_ACEOF
+
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: getopt.h not found: only short parameters can be used on command line" >&5
+$as_echo "$as_me: WARNING: getopt.h not found: only short parameters can be used on command line" >&2;}
+fi
+
+done
+
+
 ac_config_files="$ac_config_files Makefile"
 
 cat >confcache <<\_ACEOF

+ 6 - 0
configure.ac

@@ -234,5 +234,11 @@ else
     AC_MSG_WARN([Skipping BFD library checks because of GPL Licence])
 fi
 
+################################
+# Check for getopt_long()
+################################
+AC_CHECK_HEADERS(getopt.h, [],
+    AC_MSG_WARN([getopt.h not found: only short parameters can be used on command line]))
+
 AC_CONFIG_FILES(Makefile)
 AC_OUTPUT