Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: add getopt option parser #1804

Merged
merged 1 commit into from
Jun 1, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions deps/getopt/getopt.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
'targets': [
{
'target_name': 'getopt',
'type': 'static_library',
'sources': [
'getopt.h',
'getopt_long.c'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably drop getopt.c if you build getopt_long.c with -DREPLACE_GETOPT.

],
'defines': [
'REPLACE_GETOPT'
]
}
]
}
100 changes: 100 additions & 0 deletions deps/getopt/getopt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* $NetBSD: getopt.h,v 1.4 2000/07/07 10:43:54 ad Exp $ */
/* $FreeBSD$ */

/*-
* Copyright (c) 2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Dieter Baron and Thomas Klausner.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _GETOPT_H_
#define _GETOPT_H_

#ifndef __BEGIN_DECLS
#ifdef __cplusplus
#define __BEGIN_DECLS extern "C" {
#define __END_DECLS }
#else
#define __BEGIN_DECLS
#define __END_DECLS
#endif /* __cplusplus */
#endif /* __BEGIN_DECLS */

#ifdef WIN32
#ifdef _MSC_VER
/* ignore MSVC++ warnings that are annoying and hard to remove:
4702 unreachable code
(there is an unreachable assert(0) in case somehow it is reached)
*/
#pragma warning( disable : 4702 )
#endif

#endif /* WIN32 */

/*
* GNU-like getopt_long()/getopt_long_only() with 4.4BSD optreset extension.
* getopt() is declared here too for GNU programs.
*/
#define no_argument 0
#define required_argument 1
#define optional_argument 2

struct option {
/* name of long option */
const char *name;
/*
* one of no_argument, required_argument, and optional_argument:
* whether option takes an argument
*/
int has_arg;
/* if not NULL, set *flag to val when option found */
int *flag;
/* if flag not NULL, value to set *flag to; else return value */
int val;

/* add description for usage */
const char *desc;
};

__BEGIN_DECLS
extern int getopt_long(int, char * const *, const char *,
const struct option *, int *);
extern int getopt_long_only(int, char * const *, const char *,
const struct option *, int *);
#ifndef _GETOPT_DECLARED
#define _GETOPT_DECLARED
int getopt(int, char * const [], const char *);

extern char *optarg; /* getopt(3) external variables */
extern int optind, opterr, optopt;
#endif
#ifndef _OPTRESET_DECLARED
#define _OPTRESET_DECLARED
extern int optreset; /* getopt(3) external variable */
#endif
__END_DECLS

#endif /* !_GETOPT_H_ */
634 changes: 634 additions & 0 deletions deps/getopt/getopt_long.c

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion node.gyp
Original file line number Diff line number Diff line change
@@ -86,6 +86,7 @@
'dependencies': [
'node_js2c#host',
'deps/cares/cares.gyp:cares',
'deps/getopt/getopt.gyp:getopt',
'deps/v8/tools/gyp/v8.gyp:v8',
'deps/v8/tools/gyp/v8.gyp:v8_libplatform'
],
@@ -94,6 +95,7 @@
'src',
'tools/msvs/genfiles',
'deps/uv/src/ares',
'deps/getopt',
'<(SHARED_INTERMEDIATE_DIR)', # for node_natives.h
'deps/v8' # include/v8_platform.h
],
@@ -114,6 +116,7 @@
'src/node_http_parser.cc',
'src/node_javascript.cc',
'src/node_main.cc',
'src/node_options.cc',
'src/node_os.cc',
'src/node_v8.cc',
'src/node_stat_watcher.cc',
@@ -150,6 +153,7 @@
'src/node_http_parser.h',
'src/node_internals.h',
'src/node_javascript.h',
'src/node_options.h',
'src/node_root_certs.h',
'src/node_version.h',
'src/node_watchdog.h',
@@ -630,7 +634,7 @@
{
'target_name': 'cctest',
'type': 'executable',
'dependencies': [
'dependencies': [
'deps/gtest/gtest.gyp:gtest',
'deps/v8/tools/gyp/v8.gyp:v8',
'deps/v8/tools/gyp/v8.gyp:v8_libplatform'
314 changes: 39 additions & 275 deletions src/node.cc

Large diffs are not rendered by default.

305 changes: 305 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
#include "node_options.h"
#include "node_version.h"
#include "node_internals.h"
#include "getopt.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

namespace node {

enum {
OPT_NO_DEPRECATION = 1000,
OPT_THROW_DEPRECATION,
OPT_TRACE_DEPRECATION,
OPT_TRACE_SYNC_IO,
OPT_V8_OPTIONS,
OPT_ABORT_UNCAUGHT,
OPT_EXPOSE_INTERNALS,
OPT_DEBUG,
OPT_DEBUG_BRK,
OPT_DEBUG_PORT,
#if defined(NODE_HAVE_I18N_SUPPORT)
OPT_ICU_DATA_DIR
#endif
};

static struct option longopts[] = {
{ "help", no_argument, nullptr, 'h', "show help and usage" },
{ "version", no_argument, nullptr, 'v', "print io.js version" },
{ "eval", optional_argument, nullptr, 'e', "evaluate script" },
{ "print", optional_argument, nullptr, 'p',
"evaluate script and print result" },
{ "interactive", no_argument, nullptr, 'i',
"always enter the REPL even if stdin "
"does not appear to be a terminal" },
{ "require", required_argument, nullptr, 'r', "module to preload" },
{ "no-deprecation", no_argument, nullptr, OPT_NO_DEPRECATION,
"silence deprecation warnings" },
{ "throw-deprecation", no_argument, nullptr, OPT_THROW_DEPRECATION,
"throw an exception anytime a deprecated function is used" },
{ "trace-deprecation", no_argument, nullptr, OPT_TRACE_DEPRECATION,
"show stack traces on deprecations" },
{ "trace-sync-io", no_argument, nullptr, OPT_TRACE_SYNC_IO,
"show stake trace when use of sync IO "
"is detected after the first tick" },
{ "v8-options", no_argument, nullptr, OPT_V8_OPTIONS,
"print v8 command line options" },
{ "v8_options", no_argument, nullptr, OPT_V8_OPTIONS,
"print v8 command line options" },
{ "abort-on-uncaught-exception", no_argument, nullptr, OPT_ABORT_UNCAUGHT,
"abort on uncaught exception" },
{ "abort_on_uncaught_exception", no_argument, nullptr, OPT_ABORT_UNCAUGHT,
"abort on uncaught exception" },
{ "expose-internals", no_argument, nullptr, OPT_EXPOSE_INTERNALS,
"expose internal modules" },
{ "expose_internals", no_argument, nullptr, OPT_EXPOSE_INTERNALS,
"expose internal modules" },
{ "debug", optional_argument, nullptr, OPT_DEBUG, "enable debug mode" },
{ "debug-brk", optional_argument, nullptr, OPT_DEBUG_BRK,
"break before starting" },
{ "debug-port", required_argument, nullptr, OPT_DEBUG_PORT,
"specify debug port (defaults to 5858)" },
#if defined(NODE_HAVE_I18N_SUPPORT)
{ "icu-data-dir", required_argument, nullptr, OPT_ICU_DATA_DIR },
#endif
{ nullptr, 0, nullptr, 0, "" }
};

void NodeOptions::PrintHelp() {
printf("Usage: iojs [options] [ -e script | script.js ] [arguments] \n"
" iojs debug script.js [arguments] \n"
"\n"
"Options:\n");
for (size_t i = 0; i < ARRAY_SIZE(longopts); i++) {
if (longopts[i].name == nullptr)
continue;
if (longopts[i].val < 1000) {
printf("\t-%c, --%-30s %-50s\n",
longopts[i].val,
longopts[i].name,
longopts[i].desc);
} else {
printf("\t --%-30s %-50s\n", longopts[i].name, longopts[i].desc);
}
}

printf("\n");

printf("Environment variables:\n"
#ifdef _WIN32
"\t NODE_PATH ';'-separated list of directories\n"
#else
"\t NODE_PATH ':'-separated list of directories\n"
#endif
"\t prefixed to the module search path.\n"
"\t NODE_DISABLE_COLORS Set to 1 to disable colors in the REPL\n"
#if defined(NODE_HAVE_I18N_SUPPORT)
"\t NODE_ICU_DATA Data path for ICU (Intl object) data\n"
#if !defined(NODE_HAVE_SMALL_ICU)
"\t (will extend linked-in data)\n"
#endif
#endif
"\n"
"Documentation can be found at https://iojs.org/\n");
}

void NodeOptions::ParseArgs(int* argc,
const char** argv,
int* exec_argc,
const char*** exec_argv,
int* v8_argc,
const char*** v8_argv) {
const unsigned int nargs = static_cast<unsigned int>(*argc);
const char** new_exec_argv = new const char*[nargs];
const char** new_v8_argv = new const char*[nargs];
const char** new_argv = new const char*[nargs];
const char** local_preload_modules = new const char*[nargs];

// we are mutating the strings vector but not the strings themselves
char** largv = const_cast<char**>(argv);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment here explaining we're mutating the strings vector but not the strings themselves?

for (unsigned int i = 0; i < nargs; ++i) {
new_exec_argv[i] = nullptr;
new_v8_argv[i] = nullptr;
new_argv[i] = nullptr;
local_preload_modules[i] = nullptr;
}

const char* port = nullptr;

// exec_argv starts with the first option, the other two start with argv[0].
unsigned int new_exec_argc = 0;
unsigned int new_v8_argc = 1;
unsigned int new_argc = 1;
new_v8_argv[0] = argv[0];
new_argv[0] = argv[0];
int rc = 0;
unsigned int index = 1;
bool is_eval = false;
bool is_print = false;
const char optstring[] = ":hve:p::ir:d::b::x:";
while ((rc = getopt_long(*argc, largv, optstring, longopts, NULL)) != -1 &&
argv[index][0] == '-') {
unsigned int args_consumed = 1;
const char* const arg = argv[index];
switch (rc) {
case 'h':
PrintHelp();
exit(0);
break;
case 'v':
printf("%s\n", NODE_VERSION);
exit(0);
break;
case 'e':
case 'p':
{
if (!is_eval)
is_eval = (rc == 'e');

if (!is_print)
is_print = (rc == 'p');
const char* name = is_eval ? "eval" : "print";
print_eval = print_eval || is_print;
if (is_eval == true) {
eval_string = argv[index + 1];
args_consumed += 1;
if (eval_string == nullptr) {
fprintf(stderr, "%s: %s requires an argument\n", argv[0], name);
exit(9);
}
} else if ((index + 1 < nargs) &&
argv[index + 1] != nullptr &&
argv[index + 1][0] != '-') {
eval_string = argv[index + 1];
args_consumed += 1;
if (strncmp(eval_string, "\\-", 2) == 0) {
// Starts with "\\-": escaped expression, drop the backslash.
eval_string += 1;
}
}
break;
}
case 'i':
force_repl = true;
break;
case 'r':
{
const char* module = argv[index + 1];
args_consumed += 1;
if (module == nullptr) {
fprintf(stderr, "%s: %s requires an argument\n", argv[0], arg);
exit(9);
}
local_preload_modules[preload_module_count++] = module;
break;
}
case OPT_NO_DEPRECATION:
no_deprecation = true;
break;
case OPT_THROW_DEPRECATION:
throw_deprecation = true;
break;
case OPT_TRACE_DEPRECATION:
trace_deprecation = true;
break;
case OPT_TRACE_SYNC_IO:
trace_sync_io = true;
break;
case OPT_V8_OPTIONS:
new_v8_argv[new_v8_argc] = "--help";
new_v8_argc += 1;
break;
case OPT_ABORT_UNCAUGHT:
abort_on_uncaught_exception = true;
break;
case OPT_EXPOSE_INTERNALS:
// pass through
break;
case OPT_DEBUG:
{
use_debug_agent = true;
if (optarg != nullptr) {
port = const_cast<const char*>(optarg);
}
break;
}
case OPT_DEBUG_BRK:
{
use_debug_agent = true;
debug_wait_connect = true;
if (optarg != nullptr) {
port = const_cast<const char*>(optarg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not very sound. The string that optarg points to may get clobbered by later updates. It would be best to process it immediately.

(Also, you don't need the const_cast.)

EDIT: Looking at it again, I guess it's alright because argv isn't clobbered.

}
break;
}
case OPT_DEBUG_PORT:
{
port = optarg;
break;
}
#if defined(NODE_HAVE_I18N_SUPPORT)
case OPT_ICU_DATA_DIR:
{
if (optarg != nullptr) {
icu_data_dir = const_cast<const char*>(optarg);
}
break;
}
#endif
case '?':
{
if (arg[0] == '-') {
// V8 option. Pass through as-is.
new_v8_argv[new_v8_argc] = arg;
new_v8_argc += 1;
}
break;
}
}

memcpy(new_exec_argv + new_exec_argc,
largv + index,
args_consumed * sizeof(*largv));

new_exec_argc += args_consumed;
index += args_consumed;
}

if (port != nullptr) {
debug_port = atoi(port);
if (debug_port < 1024 || debug_port > 65535) {
fprintf(stderr, "Debug port must be in range 1024 to 65535.\n");
PrintHelp();
exit(12);
}
}

// Copy remaining arguments.
const unsigned int args_left = nargs - index;
memcpy(new_argv + new_argc, argv + index, args_left * sizeof(*argv));
new_argc += args_left;

*exec_argc = new_exec_argc;
*exec_argv = new_exec_argv;
*v8_argc = new_v8_argc;
*v8_argv = new_v8_argv;

// Copy new_argv over argv and update argc.
memcpy(argv, new_argv, new_argc * sizeof(*argv));
delete[] new_argv;
*argc = static_cast<int>(new_argc);

// Copy the preload_modules from the local array to an appropriately sized
// global array.
if (preload_module_count > 0) {
CHECK(!preload_modules);
preload_modules = new const char*[preload_module_count];
memcpy(preload_modules,
local_preload_modules,
preload_module_count * sizeof(*preload_modules));
}
delete[] local_preload_modules;
}

} // namespace node
62 changes: 62 additions & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#ifndef SRC_NODE_OPTIONS_H_
#define SRC_NODE_OPTIONS_H_

#include "node_version.h"
#include "util.h"
#include "getopt.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

namespace node {

class NodeOptions {
public:
bool print_eval = false;
bool force_repl = false;
bool trace_deprecation = false;
bool throw_deprecation = false;
bool abort_on_uncaught_exception = false;
bool trace_sync_io = false;
const char* eval_string = nullptr;
unsigned int preload_module_count = 0;
const char** preload_modules = nullptr;
bool use_debug_agent = false;
bool debug_wait_connect = false;
int debug_port = 5858;
bool no_deprecation = false;
#if defined(NODE_HAVE_I18N_SUPPORT)
// Path to ICU data (for i18n / Intl)
const char* icu_data_dir = nullptr;
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nicer to turn these into fields and then have a global node_options or cli_options variable. Either that or make it impossible to instantiate by adding a NodeOptions() = delete.


NodeOptions() = default;

// Print help to stdout
void PrintHelp();

// Parse command line arguments.
//
// argv is modified in place. exec_argv and v8_argv are out arguments that
// ParseArgs() allocates memory for and stores a pointer to the output
// vector in. The caller should free them with delete[].
//
// On exit:
//
// * argv contains the arguments with node and V8 options filtered out.
// * exec_argv contains both node and V8 options and nothing else.
// * v8_argv contains argv[0] plus any V8 options
void ParseArgs(int* argc,
const char** argv,
int* exec_argc,
const char*** exec_argv,
int* v8_argc,
const char*** v8_argv);

private:
DISALLOW_COPY_AND_ASSIGN(NodeOptions);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the following here?

private:
  DISALLOW_COPY_AND_ASSIGN(NodeOptions);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, adding that fails to build without having the constructor and destructor (even if they are noops)

../src/node.cc:123:13: error: no matching constructor for initialization of 'node::NodeOptions'
NodeOptions node_options;
            ^
../src/node_options.h:56:28: note: candidate constructor not viable: requires 1 argument, but 0 were provided
  DISALLOW_COPY_AND_ASSIGN(NodeOptions);
                           ^
../src/util.h:18:3: note: expanded from macro 'DISALLOW_COPY_AND_ASSIGN'
  TypeName(const TypeName&) = delete;                                         \
  ^
../src/node_options.h:56:28: note: candidate constructor not viable: requires 1 argument, but 0 were provided
../src/util.h:19:3: note: expanded from macro 'DISALLOW_COPY_AND_ASSIGN'
  TypeName(TypeName&&) = delete
  ^
1 error generated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a NodeOptions() = default; in the public part should fix that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that fixed it


} // namespace node

#endif // SRC_NODE_OPTIONS_H_
20 changes: 20 additions & 0 deletions test/parallel/test-cli-debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const execFile = require('child_process').execFile;
const EOL = require('os').EOL;
var depmod = require.resolve('../fixtures/printA.js');
var node = process.execPath;

var debug = ['--debug', depmod];
var debugPort = ['--debug=5859', depmod];

function handle(port) {
return function(er, stdout, stderr) {
assert.equal(er, null);
assert.equal(stderr, `Debugger listening on port ${port}${EOL}`);
};
}

execFile(node, debug, handle(5858));
execFile(node, debugPort, handle(5859));
10 changes: 10 additions & 0 deletions test/parallel/test-cli-help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
const assert = require('assert');
const execFile = require('child_process').execFile;

// test --help
execFile(process.execPath, ['--help'], function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(/Usage/.test(stdout), true);
});
16 changes: 16 additions & 0 deletions test/parallel/test-cli-v8-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const execFile = require('child_process').execFile;

// test --v8-options and --v8_options
(function runTest(flags) {
var flag = flags.pop();
execFile(process.execPath, [flag], function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout.indexOf('Usage') > -1, true);
if (flags.length > 0)
setImmediate(runTest, flags);
});
})(['--v8-options', '--v8_options']);
11 changes: 11 additions & 0 deletions test/parallel/test-cli-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
const assert = require('assert');
const execFile = require('child_process').execFile;

// test --version
execFile(process.execPath, ['--version'], function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
// just in case the version ever has anything appended to it (nightlies?)
assert.equal(/v([\d]+).([\d]+).([\d]+)(.*)/.test(stdout), true);
});
11 changes: 11 additions & 0 deletions test/sequential/test-deprecation-flags.js
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ var node = process.execPath;
var normal = [depmod];
var noDep = ['--no-deprecation', depmod];
var traceDep = ['--trace-deprecation', depmod];
var throwDep = ['--throw-deprecation', depmod];

execFile(node, normal, function(er, stdout, stderr) {
console.error('normal: show deprecation warning');
@@ -36,3 +37,13 @@ execFile(node, traceDep, function(er, stdout, stderr) {
assert.equal(stack.pop(), '\'This is deprecated\'');
console.log('trace ok');
});

execFile(node, throwDep, function(er, stdout, stderr) {
console.error('--throw-deprecation: show stack');
assert.equal(/Error: Command failed/.test(er), true);
assert.equal(stdout, '');
var stack = stderr.trim().split('\n');
assert.equal(/util.js:([\d]+)/.test(stack[0]), true);
assert.equal(/throw new Error\(msg\)/.test(stack[1]), true);
console.log('throw ok');
});