Permalink
Browse files

improve argument parsing in couchjs

This change makes argument parsing in couchjs more robust:
  * Removes the need for couchjs script
  * Adds long options
  * Makes all the options to couchjs available (fix COUCHDB-893)

Conflicts:

	bin/Makefile.am
	bin/couchjs.tpl.in
	configure.ac
  • Loading branch information...
tilgovi committed Oct 2, 2011
1 parent e779492 commit a378a6a13981b962039c8cd11e11caf0759965bd
View
@@ -13,13 +13,13 @@
if WINDOWS
bin_SCRIPTS = couchdb.bat
else
-bin_SCRIPTS = couchdb couchjs
+bin_SCRIPTS = couchdb
endif
noinst_SCRIPTS = couchjs_dev
if HELP2MAN
-dist_man1_MANS = couchdb.1 couchjs.1
+dist_man1_MANS = couchdb.1
endif
CLEANFILES = $(bin_SCRIPTS) $(dist_man1_MANS) $(noinst_SCRIPTS)
@@ -48,27 +48,12 @@ couchdb: couchdb.tpl
$@ < $<
chmod +x $@
-couchjs: couchjs.tpl
- sed -e "s|%locallibbindir%|@locallibbindir@|g" \
- -e "s|%bug_uri%|@bug_uri@|g" \
- -e "s|%package_author_address%|@package_author_address@|g" \
- -e "s|%package_author_name%|@package_author_name@|g" \
- -e "s|%package_name%|@package_name@|g" \
- -e "s|%version%|@version@|g" \
- -e "s|%couchjs_command_name%|$(couchjs_command_name)|g" > \
- $@ < $<
- chmod +x $@
+install-exec-hook:
+ $(LN_S) -f "$(locallibbindir)/$(couchjs_command_name)" \
+ "$(DESTDIR)$(bindir)/$(couchjs_command_name)"
-couchjs_dev: couchjs.tpl
- sed -e "s|%locallibbindir%|$(abs_top_builddir)/src/couchdb/priv|g" \
- -e "s|%bug_uri%|@bug_uri@|g" \
- -e "s|%package_author_address%|@package_author_address@|g" \
- -e "s|%package_author_name%|@package_author_name@|g" \
- -e "s|%package_name%|@package_name@|g" \
- -e "s|%version%|@version@|g" \
- -e "s|%couchjs_command_name%|$(couchjs_command_name)|g" > \
- $@ < $<
- chmod +x $@
+couchjs_dev:
+ $(LN_S) -f "$(abs_top_builddir)/src/couchdb/priv/couchjs" "$@"
couchdb.bat: couchdb.bat.tpl
sed -e "s|%ICU_CONFIG%|$(ICU_CONFIG)|g" \
@@ -88,11 +73,3 @@ couchdb.1: couchdb.tpl.in
$(HELP2MAN_EXECUTABLE) $(HELP2MAN_OPTION) \
--name="Apache CouchDB database server" ./couchdb --output $@; \
fi
-
-couchjs.1: couchjs.tpl.in
- touch $@
- if test -x "$(HELP2MAN_EXECUTABLE)"; then \
- $(MAKE) -f Makefile couchjs; \
- $(HELP2MAN_EXECUTABLE) $(HELP2MAN_OPTION) \
- --name="Apache CouchDB JavaScript interpreter" ./couchjs --output $@; \
- fi
View
@@ -1,92 +0,0 @@
-#! /bin/sh -e
-
-# Licensed under the Apache License, Version 2.0 (the "License"); you may not
-# use this file except in compliance with the License. You may obtain a copy of
-# the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations under
-# the License.
-
-SCRIPT_OK=0
-SCRIPT_ERROR=1
-
-DEFAULT_VERSION=170
-
-basename=`basename $0`
-
-display_version () {
- cat << EOF
-$basename - %package_name% %version%
-
-Licensed under the Apache License, Version 2.0 (the "License"); you may not use
-this file except in compliance with the License. You may obtain a copy of the
-License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software distributed
-under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
-CONDITIONS OF ANY KIND, either express or implied. See the License for the
-specific language governing permissions and limitations under the License.
-EOF
-}
-
-display_help () {
- cat << EOF
-Usage: $basename [FILE]
-
-The $basename command runs the %package_name% JavaScript interpreter.
-
-The exit status is 0 for success or 1 for failure.
-
-Options:
-
- -h display a short help message and exit
- -V display version information and exit
-
-Report bugs at <%bug_uri%>.
-EOF
-}
-
-display_error () {
- if test -n "$1"; then
- echo $1 >&2
- fi
- echo >&2
- echo "Try \`"$basename" -h' for more information." >&2
- exit $SCRIPT_ERROR
-}
-
-run_couchjs () {
- exec %locallibbindir%/%couchjs_command_name% $@
-}
-
-parse_script_option_list () {
- set +e
- options=`getopt hV $@`
- if test ! $? -eq 0; then
- display_error
- fi
- set -e
- eval set -- $options
- while [ $# -gt 0 ]; do
- case "$1" in
- -h) shift; display_help; exit $SCRIPT_OK;;
- -V) shift; display_version; exit $SCRIPT_OK;;
- --) shift; break;;
- *) break;;
- esac
- done
- option_list=`echo $@ | sed 's/--//'`
- if test -z "$option_list"; then
- display_error "You must specify a FILE."
- fi
- run_couchjs $option_list
-}
-
-parse_script_option_list $@
View
@@ -10,7 +10,8 @@ dnl WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
dnl License for the specific language governing permissions and limitations
dnl under the License.
-AC_INIT([LOCAL_PACKAGE_NAME], [LOCAL_VERSION], [], [LOCAL_PACKAGE_TARNAME])
+AC_INIT([LOCAL_PACKAGE_NAME], [LOCAL_VERSION], [LOCAL_BUG_URI],
+ [LOCAL_PACKAGE_TARNAME])
AC_PREREQ([2.59])
@@ -339,7 +340,7 @@ AC_ARG_VAR([HELP2MAN_EXECUTABLE], [path to the `help2man' program])
if test -n "$HELP2MAN_EXECUTABLE"; then
help2man_enabled=true
else
- if test -f "$srcdir/bin/couchdb.1" -a -f "$srcdir/bin/couchjs.1"; then
+ if test -f "$srcdir/bin/couchdb.1" -a -f "$srcdir/src/couchdb/priv/couchjs.1"; then
help2man_enabled=true
else
help2man_enabled=false
@@ -389,7 +390,6 @@ AC_SUBST(abs_top_builddir)
AC_REVISION([LOCAL_VERSION])
AC_CONFIG_FILES([Makefile])
-AC_CONFIG_FILES([bin/couchjs.tpl])
AC_CONFIG_FILES([bin/couchdb.tpl])
AC_CONFIG_FILES([bin/couchdb.bat.tpl])
AC_CONFIG_FILES([bin/Makefile])
@@ -41,6 +41,7 @@ couch_icu_driver_la_LDFLAGS += -no-undefined
endif
COUCHJS_SRCS = \
+ couch_js/help.h \
couch_js/http.c \
couch_js/http.h \
couch_js/main.c \
@@ -58,6 +59,19 @@ couchjs_LDADD = $(CURL_LDFLAGS) @JSLIB@
couchpriv_DATA = stat_descriptions.cfg
couchpriv_PROGRAMS = couchspawnkillable
+if HELP2MAN
+dist_man1_MANS = couchjs.1
+endif
+
+HELP2MAN_OPTION=--no-info --help-option="-h" --version-option="-V"
+
+couchjs.1: couchjs
+ touch $@
+ if test -x "$(HELP2MAN_EXECUTABLE)"; then \
+ $(HELP2MAN_EXECUTABLE) $(HELP2MAN_OPTION) \
+ --name="$(package_name) JavaScript interpreter" ./couchjs --output $@; \
+ fi
+
%.cfg: %.cfg.in
cp $< $@
@@ -0,0 +1,82 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#ifndef COUCHJS_HELP_H
+#define COUCHJS_HELP_H
+
+#include <libgen.h>
+
+#include "config.h"
+
+static const char VERSION_TEMPLATE[] =
+ "%s - %s\n"
+ "\n"
+ "Licensed under the Apache License, Version 2.0 (the \"License\"); you may "
+ "not use\n"
+ "this file except in compliance with the License. You may obtain a copy of"
+ "the\n"
+ "License at\n"
+ "\n"
+ " http://www.apache.org/licenses/LICENSE-2.0\n"
+ "\n"
+ "Unless required by applicable law or agreed to in writing, software "
+ "distributed\n"
+ "under the License is distributed on an \"AS IS\" BASIS, WITHOUT "
+ "WARRANTIES OR\n"
+ "CONDITIONS OF ANY KIND, either express or implied. See the License "
+ "for the\n"
+ "specific language governing permissions and limitations under the "
+ "License.\n";
+
+static const char USAGE_TEMPLATE[] =
+ "Usage: %s [FILE]\n"
+ "\n"
+ "The %s command runs the %s JavaScript interpreter.\n"
+ "\n"
+ "The exit status is 0 for success or 1 for failure.\n"
+ "\n"
+ "Options:\n"
+ "\n"
+ " -h, --help display a short help message and exit\n"
+ " -V, --version display version information and exit\n"
+ " -H, --http install %s cURL bindings (only avaiable\n"
+ " if package was built with cURL available)\n"
+ " --stack-size=SIZE specify that the interpreter should set the\n"
+ " the stack quota for JS contexts to SIZE bytes\n"
+ "\n"
+ "Report bugs at <%s>.\n";
+
+#define BASENAME basename((char*)argv[0])
+
+#define couch_version(basename) \
+ fprintf( \
+ stdout, \
+ VERSION_TEMPLATE, \
+ basename, \
+ PACKAGE_STRING)
+
+#define DISPLAY_VERSION couch_version(BASENAME)
+
+
+#define couch_usage(basename) \
+ fprintf( \
+ stdout, \
+ USAGE_TEMPLATE, \
+ basename, \
+ basename, \
+ PACKAGE_NAME, \
+ basename, \
+ PACKAGE_BUGREPORT)
+
+#define DISPLAY_USAGE couch_usage(BASENAME)
+
+#endif // Included help.h
@@ -15,6 +15,7 @@
#include <jsapi.h>
+#include "help.h"
#include "util.h"
#include "utf8.h"
@@ -79,31 +80,56 @@ couch_parse_args(int argc, const char* argv[])
args->stack_size = 8L * 1024L;
while(i < argc) {
- if(strcmp("--http", argv[i]) == 0) {
+ if(strcmp("-h", argv[i]) == 0 ||
+ strcmp("--help", argv[i]) == 0) {
+ DISPLAY_USAGE;
+ exit(0);
+ } else if(strcmp("-V", argv[i]) == 0 ||
+ strcmp("--version", argv[i]) == 0) {
+ DISPLAY_VERSION;
+ exit(0);
+ } else if(strcmp("-H", argv[i]) == 0 ||
+ strcmp("--http", argv[i]) == 0) {
args->use_http = 1;
- } else if(strcmp("--stack-size", argv[i]) == 0) {
- args->stack_size = atoi(argv[i+1]);
+ } else if(strncmp("--stack-size", argv[i], 12) == 0) {
+ if(argv[i][12] == '\0') {
+ args->stack_size = atoi(argv[++i]);
+ } else if(argv[i][12] == '=') {
+ args->stack_size = atoi(argv[i]+13);
+ } else {
+ DISPLAY_USAGE;
+ exit(2);
+ }
+
if(args->stack_size <= 0) {
fprintf(stderr, "Invalid stack size.\n");
exit(2);
}
+ } else if(strcmp("--", argv[i]) == 0) {
+ i++;
+ break;
} else {
- args->script = slurp_file(args->script, argv[i]);
- if(args->script_name == NULL) {
- if(strcmp(argv[i], "-") == 0) {
- args->script_name = "<stdin>";
- } else {
- args->script_name = argv[i];
- }
+ break;
+ }
+ i++;
+ }
+
+ while(i < argc) {
+ args->script = slurp_file(args->script, argv[i]);
+ if(args->script_name == NULL) {
+ if(strcmp(argv[i], "-") == 0) {
+ args->script_name = "<stdin>";
} else {
- args->script_name = "<multiple_files>";
+ args->script_name = argv[i];
}
+ } else {
+ args->script_name = "<multiple_files>";
}
i++;
}
if(args->script_name == NULL || args->script == NULL) {
- fprintf(stderr, "No script provided.\n");
+ DISPLAY_USAGE;
exit(3);
}
@@ -22,7 +22,6 @@ typedef struct {
char* script;
} couch_args;
-void couch_usage();
couch_args* couch_parse_args(int argc, const char* argv[]);
int couch_fgets(char* buf, int size, FILE* fp);
JSString* couch_readline(JSContext* cx, FILE* fp);

0 comments on commit a378a6a

Please sign in to comment.