From b539f6df0dbdbfe7cbd9704c940734dde92b83a3 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Fri, 13 Jul 2018 20:20:07 +0200 Subject: [PATCH] stop parsing the command line when encountering '--' And also, add a test for this feature (fix #287) --- ChangeLog | 1 + src/simgrid/sg_config.cpp | 16 ++++++++++------ src/xbt/log.c | 10 +++++++--- teshsuite/xbt/CMakeLists.txt | 4 ++-- teshsuite/xbt/cmdline/cmdline.c | 19 +++++++++++++++++++ teshsuite/xbt/cmdline/cmdline.tesh | 5 +++++ 6 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 teshsuite/xbt/cmdline/cmdline.c create mode 100644 teshsuite/xbt/cmdline/cmdline.tesh diff --git a/ChangeLog b/ChangeLog index 988d5f22b2..96abe8a9b6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,6 +11,7 @@ Fixed bugs: - #282: TIT convertor erase traces when using absolute path in the trace list file - #285: segfault when a s4u actor kills itself with msg process tracing activated - #286: Pajé traces are not exposing the number of cores + - #287: Command-line parsing should stop when encountering '--' ---------------------------------------------------------------------------- diff --git a/src/simgrid/sg_config.cpp b/src/simgrid/sg_config.cpp index 415a9c78d1..2367331f6d 100644 --- a/src/simgrid/sg_config.cpp +++ b/src/simgrid/sg_config.cpp @@ -41,18 +41,22 @@ static void sg_config_cmd_line(int *argc, char **argv) int shall_exit = 0; int i; int j; + bool parse_args = true; // Stop parsing the parameters once we found '--' for (j = i = 1; i < *argc; i++) { - if (not strncmp(argv[i], "--cfg=", strlen("--cfg="))) { + if (not strcmp("--", argv[i])) { + parse_args = false; + // Remove that '--' from the arguments + } else if (parse_args && not strncmp(argv[i], "--cfg=", strlen("--cfg="))) { char *opt = strchr(argv[i], '='); opt++; simgrid::config::set_parse(opt); XBT_DEBUG("Did apply '%s' as config setting", opt); - } else if (not strcmp(argv[i], "--version")) { + } else if (parse_args && not strcmp(argv[i], "--version")) { printf("%s\n", SIMGRID_VERSION_STRING); shall_exit = 1; - } else if (not strcmp(argv[i], "--cfg-help") || not strcmp(argv[i], "--help")) { + } else if (parse_args && (not strcmp(argv[i], "--cfg-help") || not strcmp(argv[i], "--help"))) { printf("Description of the configuration accepted by this simulator:\n"); simgrid::config::help(); printf( @@ -70,12 +74,12 @@ static void sg_config_cmd_line(int *argc, char **argv) "\n" ); shall_exit = 1; - } else if (not strcmp(argv[i], "--help-aliases")) { + } else if (parse_args && not strcmp(argv[i], "--help-aliases")) { printf("Here is a list of all deprecated option names, with their replacement.\n"); simgrid::config::show_aliases(); printf("Please consider using the recent names\n"); shall_exit = 1; - } else if (not strcmp(argv[i], "--help-models")) { + } else if (parse_args && not strcmp(argv[i], "--help-models")) { model_help("host", surf_host_model_description); printf("\n"); model_help("CPU", surf_cpu_model_description); @@ -88,7 +92,7 @@ static void sg_config_cmd_line(int *argc, char **argv) surf_optimization_mode_description[k].description); printf("Both network and CPU models have 'Lazy' as default optimization level\n\n"); shall_exit = 1; - } else if (not strcmp(argv[i], "--help-tracing")) { + } else if (parse_args && not strcmp(argv[i], "--help-tracing")) { TRACE_help(); shall_exit = 1; } else { diff --git a/src/xbt/log.c b/src/xbt/log.c index 3b665d9205..fe6d5d8048 100644 --- a/src/xbt/log.c +++ b/src/xbt/log.c @@ -109,17 +109,21 @@ void xbt_log_init(int *argc, char **argv) { unsigned help_requested = 0; /* 1: logs; 2: categories */ int j = 1; + int parse_args = 1; // Stop parsing the parameters once we found '--' /* Set logs and init log submodule */ for (int i = 1; i < *argc; i++) { - if (!strncmp(argv[i], "--log=", strlen("--log="))) { + if (!strcmp("--", argv[i])) { + parse_args = 0; + argv[j++] = argv[i]; // Keep the '--' for sg_config + } else if (parse_args && !strncmp(argv[i], "--log=", strlen("--log="))) { char* opt = strchr(argv[i], '='); opt++; xbt_log_control_set(opt); XBT_DEBUG("Did apply '%s' as log setting", opt); - } else if (!strcmp(argv[i], "--help-logs")) { + } else if (parse_args && !strcmp(argv[i], "--help-logs")) { help_requested |= 1U; - } else if (!strcmp(argv[i], "--help-log-categories")) { + } else if (parse_args && !strcmp(argv[i], "--help-log-categories")) { help_requested |= 2U; } else { argv[j++] = argv[i]; diff --git a/teshsuite/xbt/CMakeLists.txt b/teshsuite/xbt/CMakeLists.txt index bd505c89bb..e59f3459c4 100644 --- a/teshsuite/xbt/CMakeLists.txt +++ b/teshsuite/xbt/CMakeLists.txt @@ -1,4 +1,4 @@ -foreach(x log_large log_usage mallocator parallel_log_crashtest) +foreach(x cmdline log_large log_usage mallocator parallel_log_crashtest) add_executable (${x} ${x}/${x}.c) target_link_libraries(${x} simgrid) set_target_properties(${x} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${x}) @@ -29,7 +29,7 @@ set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/log_usage/log_us ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc/mmalloc_32.tesh PARENT_SCOPE) set(teshsuite_src ${teshsuite_src} ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc/mmalloc_test.cpp PARENT_SCOPE) -foreach(x log_large parallel_log_crashtest parmap_test) #mallocator parmap_bench +foreach(x cmdline log_large parallel_log_crashtest parmap_test) #mallocator parmap_bench ADD_TESH(tesh-xbt-${x} --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/xbt/${x} --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/${x} ${x}.tesh) endforeach() diff --git a/teshsuite/xbt/cmdline/cmdline.c b/teshsuite/xbt/cmdline/cmdline.c new file mode 100644 index 0000000000..bda54bc1fc --- /dev/null +++ b/teshsuite/xbt/cmdline/cmdline.c @@ -0,0 +1,19 @@ +/* Copyright (c) 2018. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "simgrid/engine.h" +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to this test"); + +int main(int argc, char** argv) +{ + simgrid_init(&argc, argv); + + for (int i = 1; i < argc; i++) + XBT_INFO("argv[%d]=%s", i, argv[i]); + + return 0; +} diff --git a/teshsuite/xbt/cmdline/cmdline.tesh b/teshsuite/xbt/cmdline/cmdline.tesh new file mode 100644 index 0000000000..4145b9315c --- /dev/null +++ b/teshsuite/xbt/cmdline/cmdline.tesh @@ -0,0 +1,5 @@ + +p Test that -- correctly stops the command line parsing + +$ ${bindir:=.}/cmdline -- --help +> [0.000000] [test/INFO] argv[1]=--help -- 2.20.1