From 1382559dbe353ffb8361753c3d9d69d8c8c48a86 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 23 Oct 2020 20:42:24 -0400 Subject: [PATCH] adapt behavior of lammps-shell when passing filename without -in/-i flag To better handle the case of file association. We now split the path into a directory and basename string and change the current working directory to that directory and read the file with the basename. This simplifies the .desktop file and makes the LAMMPS shell behave as expected on Windows, too. --- tools/lammps-shell/lammps-shell.cpp | 10 ++++++---- tools/lammps-shell/lammps-shell.desktop | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/tools/lammps-shell/lammps-shell.cpp b/tools/lammps-shell/lammps-shell.cpp index b2c8137aba..03edcdc58a 100644 --- a/tools/lammps-shell/lammps-shell.cpp +++ b/tools/lammps-shell/lammps-shell.cpp @@ -719,10 +719,12 @@ int main(int argc, char **argv) // handle the special case where the first argument is not a flag but a file // this happens for example when using file type associations on Windows. // in this case we save the pointer and remove it from argv. - char *input_file = nullptr; + // we also get the directory name and switch to that folder + std::string input_file; if ((argc > 1) && (argv[1][0] != '-')) { --argc; - input_file = argv[1]; + input_file = utils::path_basename(argv[1]); + chdir(utils::path_dirname(input_file).c_str()); for (int i = 1; i < argc; ++i) argv[i] = argv[i+1]; } @@ -733,8 +735,8 @@ int main(int argc, char **argv) init_commands(); // pre-load an input file that was provided on the command line - if (input_file) { - lammps_file(lmp, input_file); + if (!input_file.empty()) { + lammps_file(lmp, input_file.c_str()); } else { for (int i = 0; i < argc; ++i) { if ((strcmp(argv[i], "-in") == 0) || (strcmp(argv[i], "-i") == 0)) { diff --git a/tools/lammps-shell/lammps-shell.desktop b/tools/lammps-shell/lammps-shell.desktop index 2804ca18bb..67675c8f22 100644 --- a/tools/lammps-shell/lammps-shell.desktop +++ b/tools/lammps-shell/lammps-shell.desktop @@ -2,7 +2,7 @@ Version=1.0 Type=Application Categories=Science;Engineering; -Exec=/bin/sh -c "echo -e -n \"\033]0;The LAMMPS Shell\007\"; f=%f; test -n \"$f\" && d=`dirname \$f` && cd \$d; LC_ALL=C lammps-shell \$f" +Exec=/bin/sh -c "echo -e -n \"\033]0;The LAMMPS Shell\007\"; LC_ALL=C lammps-shell %f" Name=The LAMMPS Shell Terminal=true GenericName=MD Simulator