From a378a6a13981b962039c8cd11e11caf0759965bd Mon Sep 17 00:00:00 2001 From: Randall Leeds Date: Sun, 2 Oct 2011 04:11:54 -0700 Subject: 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 --- src/couchdb/priv/Makefile.am | 14 +++++++ src/couchdb/priv/couch_js/help.h | 82 ++++++++++++++++++++++++++++++++++++++++ src/couchdb/priv/couch_js/util.c | 50 ++++++++++++++++++------ src/couchdb/priv/couch_js/util.h | 1 - 4 files changed, 134 insertions(+), 13 deletions(-) create mode 100644 src/couchdb/priv/couch_js/help.h (limited to 'src/couchdb/priv') diff --git a/src/couchdb/priv/Makefile.am b/src/couchdb/priv/Makefile.am index 4a32c0a2..130da9f4 100644 --- a/src/couchdb/priv/Makefile.am +++ b/src/couchdb/priv/Makefile.am @@ -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 $< $@ diff --git a/src/couchdb/priv/couch_js/help.h b/src/couchdb/priv/couch_js/help.h new file mode 100644 index 00000000..e7c3af0e --- /dev/null +++ b/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 + +#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 diff --git a/src/couchdb/priv/couch_js/util.c b/src/couchdb/priv/couch_js/util.c index 070d7172..ab1295f8 100644 --- a/src/couchdb/priv/couch_js/util.c +++ b/src/couchdb/priv/couch_js/util.c @@ -15,6 +15,7 @@ #include +#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 = ""; - } 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 = ""; } else { - args->script_name = ""; + args->script_name = argv[i]; } + } else { + args->script_name = ""; } i++; } if(args->script_name == NULL || args->script == NULL) { - fprintf(stderr, "No script provided.\n"); + DISPLAY_USAGE; exit(3); } diff --git a/src/couchdb/priv/couch_js/util.h b/src/couchdb/priv/couch_js/util.h index 6caebfa1..380ff846 100644 --- a/src/couchdb/priv/couch_js/util.h +++ b/src/couchdb/priv/couch_js/util.h @@ -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); -- cgit v1.2.3