Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

[main] Simplified jslinux command line options #1875

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
112 changes: 91 additions & 21 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#endif
#else
#include "zjs_linux_port.h"
#include <unistd.h>
#include <getopt.h>
#endif // ZJS_LINUX_BUILD
#include "zjs_script.h"
#include "zjs_util.h"
Expand Down Expand Up @@ -81,40 +83,107 @@ static u8_t no_exit = 0;
// if > 0, jslinux will exit after this many milliseconds
static u32_t exit_after = 0;
static struct timespec exit_timer;
static char *js_args = NULL;

static void usage(void)
{
printf("jslinux usage:\n");
printf("\t--jsargs, -j \"<args>\" Pass args to js script via process\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

More normal usage style is shortcut first, and caps for arg names, e.g. something like:

\t-j, --jsargs="ARGS"\t\tPass args...

printf("\t--exit-time, -t <milli> Exit after a certain number of"
"milliseconds\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

need a space between 'of' and 'milliseconds'

printf("\t--noexit, -n Do not exit jslinux when no events"
" are present\n");
printf("\t--unittest, -u Run jslinux unit tests\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use a couple \t's here to line up the descriptions.

printf("\t--debugger, -d Start debugger, waits for debugging"
" connection at startup\n");
printf("\t--help, -h Display this help message\n");
}

static const struct option main_options[] = {
{ "jsargs", required_argument, NULL, 'j' },
{ "exit-time", required_argument, NULL, 't' },
{ "noexit", no_argument, NULL, 'n' },
{ "unittest", no_argument, NULL, 'u' },
{ "debugger", no_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
Copy link
Contributor

Choose a reason for hiding this comment

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

Odd spacing here, maybe just don't try to line things up?

{ }
};

u8_t process_cmd_line(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; ++i) {
if (!strncmp(argv[i], "--unittest", 10)) {
// run unit tests
for (;;) {
int opt;

opt = getopt_long(argc, argv, "j:t:nudh", main_options, NULL);
if (opt < 0)
break;
Copy link
Contributor

Choose a reason for hiding this comment

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

braces for great justice


switch (opt) {
case 't':
exit_after = atoi(optarg);
ZJS_PRINT("jslinux will terminate after %u milliseconds\n",
(unsigned int)exit_after);
clock_gettime(CLOCK_MONOTONIC, &exit_timer);
break;
case 'n':
no_exit = true;
break;
case 'u':
zjs_run_unit_tests();
} else if (!strncmp(argv[i], "--debugger", 10)) {
break;
case 'd':
#ifdef ZJS_DEBUGGER
// run in debugger
start_debug_server = true;
#else
ERR_PRINT("Debugger disabled, rebuild with DEBUGGER=on");
return 0;
#endif
} else if (!strncmp(argv[i], "--noexit", 8)) {
no_exit = 1;
} else if (!strncmp(argv[i], "-t", 2)) {
if (i == argc - 1) {
// no time argument, return error
ERR_PRINT("no time argument given after '-t'\n");
return 0;
} else {
char *str_time = argv[i + 1];
exit_after = atoi(str_time);
ZJS_PRINT("jslinux will terminate after %u milliseconds\n",
(unsigned int)exit_after);
clock_gettime(CLOCK_MONOTONIC, &exit_timer);
}
case 'h':
usage();
exit(0);
case 'j':
js_args = optarg;
break;
default:
usage();
exit(1);
}
}

return 1;
}

void init_process(char *args)
{
if (!args)
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

Braces, or maybe change this to an assert?


char *ptr = args;
char *last = args;
jerry_value_t array = ZJS_UNDEFINED;
ZVAL global_obj = jerry_get_global_object();
ZVAL process = zjs_create_object();

while ((ptr = strchr(last, ' '))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer you do the assignment outside of the condition. So probably make it a forever loop that you break out of.

ZVAL elem = jerry_create_string_sz(last, ptr - last);

array = zjs_push_array(array, elem);
last = ptr + 1;
}

if (args) {
ZVAL elem = jerry_create_string(last);

array = zjs_push_array(array, elem);
}

zjs_set_property(process, "argv", array);
zjs_set_property(global_obj, "process", process);

jerry_release_value(array);
}

#else
#ifndef CONFIG_NET_APP_AUTO_INIT
#ifdef BUILD_MODULE_BLE
Expand Down Expand Up @@ -248,11 +317,11 @@ int main(int argc, char *argv[])
#ifndef ZJS_SNAPSHOT_BUILD
#ifdef ZJS_LINUX_BUILD
if (argc > 1) {
if (process_cmd_line(argc - 1, argv + 1) == 0) {
if (process_cmd_line(argc, argv) == 0) {
ERR_PRINT("command line options error\n");
goto error;
}
if (zjs_read_script(argv[1], &script, &script_len)) {
if (zjs_read_script(argv[argc - 1], &script, &script_len)) {
ERR_PRINT("could not read script file %s\n", argv[1]);
goto error;
}
Expand Down Expand Up @@ -299,6 +368,7 @@ int main(int argc, char *argv[])

#ifdef ZJS_LINUX_BUILD
zjs_free(script);
init_process(js_args);
Copy link
Contributor

@grgustaf grgustaf Apr 23, 2018

Choose a reason for hiding this comment

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

We should probably make this its own module, conditional based on usage w/ analyze, like other things. Otherwise it's just going to be overhead. Particularly when we don't really have a use case. :)

So perhaps split that out here for a separate PR?

#endif

#ifdef ZJS_SNAPSHOT_BUILD
Expand Down