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

Improve command line parsing in ocamlrun #10098

Merged
merged 3 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ Working version
a breaking change if the configuration system was being abused before).
(David Allsopp, review by Xavier Leroy)

* #10098: Improve command-line parsing in ocamlrun: strictly recognise options,
be more informative for `ocamlrun -I` and support `--` for terminating options
parsing.
(David Allsopp, review by Xavier Leroy)

### Code generation and optimizations:

- #9876: do not cache the young_limit GC variable in a processor register.
Expand Down
89 changes: 53 additions & 36 deletions runtime/startup_byt.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,49 +277,66 @@ static char_os * read_section_to_os(int fd, struct exec_trailer *trail,

static int parse_command_line(char_os **argv)
{
int i, j;
int i, j, len, parsed;

for(i = 1; argv[i] != NULL && argv[i][0] == '-'; i++) {
switch(argv[i][1]) {
case 't':
++ caml_trace_level; /* ignored unless DEBUG mode */
break;
case 'v':
if (!strcmp_os (argv[i], T("-version"))){
printf ("%s\n", "The OCaml runtime, version " OCAML_VERSION_STRING);
exit (0);
}else if (!strcmp_os (argv[i], T("-vnum"))){
printf ("%s\n", OCAML_VERSION_STRING);
exit (0);
}else{
len = strlen_os(argv[i]);
parsed = 1;
if (len == 2) {
dra27 marked this conversation as resolved.
Show resolved Hide resolved
/* Single-letter options, e.g. -v */
switch(argv[i][1]) {
case '-':
return i + 1;
break;
case 't':
++ caml_trace_level; /* ignored unless DEBUG mode */
break;
case 'v':
caml_verb_gc = 0x001+0x004+0x008+0x010+0x020;
break;
case 'p':
for (j = 0; caml_names_of_builtin_cprim[j] != NULL; j++)
printf("%s\n", caml_names_of_builtin_cprim[j]);
exit(0);
break;
case 'b':
caml_record_backtrace(Val_true);
break;
case 'I':
if (argv[i + 1] != NULL) {
caml_ext_table_add(&caml_shared_libs_path, argv[i + 1]);
i++;
} else {
error("option '-I' needs an argument.");
}
break;
case 'm':
print_magic = 1;
break;
case 'M':
printf("%s\n", EXEC_MAGIC);
exit(0);
break;
default:
parsed = 0;
}
break;
case 'p':
for (j = 0; caml_names_of_builtin_cprim[j] != NULL; j++)
printf("%s\n", caml_names_of_builtin_cprim[j]);
exit(0);
break;
case 'b':
caml_record_backtrace(Val_true);
break;
case 'I':
if (argv[i + 1] != NULL) {
caml_ext_table_add(&caml_shared_libs_path, argv[i + 1]);
i++;
} else {
/* Named options, e.g. -version */
if (!strcmp_os(argv[i], T("-version"))) {
dra27 marked this conversation as resolved.
Show resolved Hide resolved
printf("%s\n", "The OCaml runtime, version " OCAML_VERSION_STRING);
exit(0);
} else if (!strcmp_os(argv[i], T("-vnum"))) {
printf("%s\n", OCAML_VERSION_STRING);
exit(0);
} else {
parsed = 0;
}
break;
case 'm':
print_magic = 1;
break;
case 'M':
printf ( "%s\n", EXEC_MAGIC);
exit(0);
break;
default:
error("unknown option %s", caml_stat_strdup_of_os(argv[i]));
}

if (!parsed)
error("unknown option %s", caml_stat_strdup_of_os(argv[i]));
}

return i;
}

Expand Down