Skip to content

Commit

Permalink
improve argument parsing in couchjs
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
tilgovi committed Oct 2, 2011
1 parent 25673c2 commit b09c269
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 141 deletions.
37 changes: 7 additions & 30 deletions bin/Makefile.am
Expand Up @@ -13,13 +13,13 @@
if WINDOWS
bin_SCRIPTS = couchdb.bat
else
bin_SCRIPTS = couchdb couchjs couch-config
bin_SCRIPTS = couchdb couch-config
endif

noinst_SCRIPTS = couchjs_dev couch-config_dev

if HELP2MAN
dist_man1_MANS = couchdb.1 couchjs.1
dist_man1_MANS = couchdb.1
endif

CLEANFILES = $(bin_SCRIPTS) $(dist_man1_MANS) $(noinst_SCRIPTS)
Expand Down Expand Up @@ -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" \
Expand Down Expand Up @@ -136,11 +121,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
95 changes: 0 additions & 95 deletions bin/couchjs.tpl.in

This file was deleted.

6 changes: 3 additions & 3 deletions configure.ac
Expand Up @@ -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])

Expand Down Expand Up @@ -433,7 +434,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
Expand Down Expand Up @@ -486,7 +487,6 @@ AC_REVISION([LOCAL_VERSION])

AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([bin/couch-config.tpl])
AC_CONFIG_FILES([bin/couchjs.tpl])
AC_CONFIG_FILES([bin/couchdb.tpl])
AC_CONFIG_FILES([bin/couchdb.bat.tpl])
AC_CONFIG_FILES([bin/Makefile])
Expand Down
14 changes: 14 additions & 0 deletions src/couchdb/priv/Makefile.am
Expand Up @@ -45,6 +45,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 \
Expand All @@ -61,6 +62,19 @@ couchjs_LDADD = $(CURL_LIBS) $(JS_LIBS)
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 $< $@

Expand Down
82 changes: 82 additions & 0 deletions src/couchdb/priv/couch_js/help.h
@@ -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
50 changes: 38 additions & 12 deletions src/couchdb/priv/couch_js/util.c
Expand Up @@ -15,6 +15,7 @@

#include <jsapi.h>

#include "help.h"
#include "util.h"
#include "utf8.h"

Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion src/couchdb/priv/couch_js/util.h
Expand Up @@ -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);
Expand Down

0 comments on commit b09c269

Please sign in to comment.