From 15f9987c328fd9f200af85d0e8d2b0aad991a82f Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 6 Oct 2020 18:39:22 -0400 Subject: [PATCH] add custom commands "cd" and "pwd" to the LAMMPS shell --- doc/src/Tools.rst | 7 +++++++ tools/lammps-shell/README | 12 +++++++----- tools/lammps-shell/lammps-shell.cpp | 13 +++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/doc/src/Tools.rst b/doc/src/Tools.rst index 483c77038e..f29103e7af 100644 --- a/doc/src/Tools.rst +++ b/doc/src/Tools.rst @@ -498,9 +498,16 @@ regular LAMMPS commands: help (or ?) print a brief help message history display the current command history list clear_history wipe out the current command history list + pwd print current working directory + cd change current working directory (same as pwd if no directory) \| execute as a shell command and return to the command prompt exit exit the LAMMPS shell cleanly (unlike the "quit" command) +Please note that some known shell operations are implemented in the +LAMMPS :doc:`shell command ` in a platform neutral fashion, +while using the '\|' character will always pass the following text +to the operating system's shell command. + Compilation =========== diff --git a/tools/lammps-shell/README b/tools/lammps-shell/README index efa19236ce..f72305536d 100644 --- a/tools/lammps-shell/README +++ b/tools/lammps-shell/README @@ -84,11 +84,13 @@ Additional commands The followind commands are added to the LAMMPS shell on top of the regular LAMMPS commands: -- help (or ?) print a brief help message -- history display the current command history list -- clear_history wipe out the current command history list -- | execute as a shell command and return to the command prompt -- exit exit the LAMMPS shell cleanly (unlike the "quit" command) +- help (or ?) print a brief help message +- history display the current command history list +- clear_history wipe out the current command history list +- pwd print current working directory +- cd change current working directory (same as pwd if no directory) +- | execute as a shell command and return to the command prompt +- exit exit the LAMMPS shell cleanly (unlike the "quit" command) Compilation =========== diff --git a/tools/lammps-shell/lammps-shell.cpp b/tools/lammps-shell/lammps-shell.cpp index c1f8182535..f286137e53 100644 --- a/tools/lammps-shell/lammps-shell.cpp +++ b/tools/lammps-shell/lammps-shell.cpp @@ -25,6 +25,7 @@ #include #include #define isatty(x) _isatty(x) +#define getcwd(buf, len) _getcwd(buf, len) #endif #if !defined(_WIN32) @@ -460,6 +461,8 @@ static void init_commands() // store LAMMPS shell specific command names commands.push_back("help"); commands.push_back("exit"); + commands.push_back("pwd"); + commands.push_back("cd"); commands.push_back("history"); commands.push_back("clear_history"); @@ -549,6 +552,16 @@ static int shell_cmd(const std::string &cmd) } else if (words[0] == "exit") { free(text); return shell_end(); + } else if ((words[0] == "pwd") || ((words[0] == "cd") && (words.size() == 1))) { + if (getcwd(buf, buflen)) std::cout << buf << "\n"; + free(text); + return 0; + } else if (words[0] == "cd") { + std::string shellcmd = "shell "; + shellcmd += text; + lammps_command(lmp, shellcmd.c_str()); + free(text); + return 0; } else if (words[0] == "history") { free(text); HIST_ENTRY **list = history_list();