2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
39 * Implements gmx::ShellCompletionWriter.
41 * \author Teemu Murtola <teemu.murtola@gmail.com>
42 * \ingroup module_commandline
46 #include "gromacs/commandline/shellcompletions.h"
52 #include <boost/scoped_ptr.hpp>
54 #include "gromacs/commandline/cmdlinehelpcontext.h"
55 #include "gromacs/commandline/pargs.h"
56 #include "gromacs/options/basicoptions.h"
57 #include "gromacs/options/filenameoption.h"
58 #include "gromacs/options/optionsvisitor.h"
59 #include "gromacs/fileio/filenm.h"
60 #include "gromacs/utility/arrayref.h"
61 #include "gromacs/utility/exceptions.h"
62 #include "gromacs/utility/file.h"
63 #include "gromacs/utility/gmxassert.h"
64 #include "gromacs/utility/stringutil.h"
72 class OptionsListWriter : public OptionsVisitor
75 const std::string &optionList() const { return optionList_; }
77 virtual void visitSubSection(const Options §ion)
79 OptionsIterator iterator(section);
80 iterator.acceptSubSections(this);
81 iterator.acceptOptions(this);
83 virtual void visitOption(const OptionInfo &option)
85 if (option.isHidden())
89 if (!optionList_.empty())
91 optionList_.append("\\n");
93 optionList_.append("-");
94 const BooleanOptionInfo *booleanOption
95 = option.toType<BooleanOptionInfo>();
96 if (booleanOption != NULL && booleanOption->defaultValue())
98 optionList_.append("no");
100 optionList_.append(option.name());
104 std::string optionList_;
107 class OptionCompletionWriter : public OptionsVisitor
110 explicit OptionCompletionWriter(File *out) : out_(*out) {}
112 virtual void visitSubSection(const Options §ion)
114 OptionsIterator iterator(section);
115 iterator.acceptSubSections(this);
116 iterator.acceptOptions(this);
118 virtual void visitOption(const OptionInfo &option);
121 void writeOptionCompletion(const OptionInfo &option,
122 const std::string &completion);
127 void OptionCompletionWriter::visitOption(const OptionInfo &option)
129 if (option.isHidden())
133 const FileNameOptionInfo *fileOption = option.toType<FileNameOptionInfo>();
134 if (fileOption != NULL)
136 if (fileOption->isDirectoryOption())
138 writeOptionCompletion(option, "compgen -S ' ' -d $c");
141 const FileNameOptionInfo::ExtensionList &extensionList = fileOption->extensions();
142 if (extensionList.empty())
146 std::string completion("compgen -S ' ' -X '!*");
147 std::string extensions(joinStrings(extensionList, "|"));
148 if (extensionList.size() > 1)
150 extensions = "@(" + extensions + ")";
152 completion.append(extensions);
153 // TODO: Don't duplicate this from filenm.c/futil.c.
154 completion.append("?(.gz|.Z)' -f -- $c ; compgen -S '/' -d $c");
155 writeOptionCompletion(option, completion);
158 const StringOptionInfo *stringOption = option.toType<StringOptionInfo>();
159 if (stringOption != NULL && stringOption->isEnumerated())
161 std::string completion("compgen -S ' ' -W $'");
162 completion.append(joinStrings(stringOption->allowedValues(), "\\n"));
163 completion.append("' -- $c");
164 writeOptionCompletion(option, completion);
169 void OptionCompletionWriter::writeOptionCompletion(
170 const OptionInfo &option, const std::string &completion)
172 std::string result(formatString("-%s) ", option.name().c_str()));
173 if (option.maxValueCount() >= 0)
175 result.append(formatString("(( $n <= %d )) && ", option.maxValueCount()));
177 result.append("COMPREPLY=( $(");
178 result.append(completion);
179 result.append("));;");
180 out_.writeLine(result);
185 class ShellCompletionWriter::Impl
188 Impl(const std::string &binaryName, ShellCompletionFormat /*format*/)
189 : binaryName_(binaryName)
193 std::string completionFunctionName(const char *moduleName) const
195 // TODO: Consider if some characters need to be escaped.
196 return formatString("_%s_%s_compl", binaryName_.c_str(), moduleName);
199 std::string binaryName_;
200 boost::scoped_ptr<File> file_;
203 ShellCompletionWriter::ShellCompletionWriter(const std::string &binaryName,
204 ShellCompletionFormat format)
205 : impl_(new Impl(binaryName, format))
209 ShellCompletionWriter::~ShellCompletionWriter()
213 File *ShellCompletionWriter::outputFile()
215 return impl_->file_.get();
218 void ShellCompletionWriter::startCompletions()
220 impl_->file_.reset(new File(impl_->binaryName_ + "-completion.bash", "w"));
221 impl_->file_->writeLine("shopt -s extglob");
224 void ShellCompletionWriter::writeModuleCompletions(
225 const char *moduleName,
226 const Options &options)
228 File &out = *impl_->file_;
229 out.writeLine(formatString("%s() {", impl_->completionFunctionName(moduleName).c_str()));
230 out.writeLine("local IFS=$'\\n'");
231 out.writeLine("local c=${COMP_WORDS[COMP_CWORD]}");
232 out.writeLine("local n");
233 out.writeLine("for ((n=1;n<COMP_CWORD;++n)) ; do [[ \"${COMP_WORDS[COMP_CWORD-n]}\" == -* ]] && break ; done");
234 out.writeLine("local p=${COMP_WORDS[COMP_CWORD-n]}");
235 out.writeLine("COMPREPLY=()");
237 OptionsListWriter listWriter;
238 listWriter.visitSubSection(options);
239 out.writeLine(formatString("if (( $COMP_CWORD <= 1 )) || [[ $c == -* ]]; then COMPREPLY=( $(compgen -S ' ' -W $'%s' -- $c)); return 0; fi", listWriter.optionList().c_str()));
241 out.writeLine("case \"$p\" in");
242 OptionCompletionWriter optionWriter(&out);
243 optionWriter.visitSubSection(options);
244 out.writeLine("esac }");
247 void ShellCompletionWriter::writeWrapperCompletions(
248 const ModuleNameList &modules, const Options &options)
250 impl_->file_->writeLine("_" + impl_->binaryName_ + "_compl() {");
251 impl_->file_->writeLine("local i c m");
252 impl_->file_->writeLine("local IFS=$'\\n'\n");
253 impl_->file_->writeLine("COMPREPLY=()");
254 impl_->file_->writeLine("unset COMP_WORDS[0]");
255 impl_->file_->writeLine("for ((i=1;i<COMP_CWORD;++i)) ; do");
256 impl_->file_->writeLine("[[ \"${COMP_WORDS[i]}\" != -* ]] && break");
257 impl_->file_->writeLine("unset COMP_WORDS[i]");
258 impl_->file_->writeLine("done");
259 impl_->file_->writeLine("if (( i == COMP_CWORD )); then");
260 impl_->file_->writeLine("c=${COMP_WORDS[COMP_CWORD]}");
261 OptionsListWriter lister;
262 lister.visitSubSection(options);
263 std::string completions(lister.optionList());
264 for (ModuleNameList::const_iterator i = modules.begin();
265 i != modules.end(); ++i)
267 completions.append("\\n");
268 completions.append(*i);
270 impl_->file_->writeLine("COMPREPLY=( $(compgen -S ' ' -W $'" + completions + "' -- $c) )");
271 impl_->file_->writeLine("return 0");
272 impl_->file_->writeLine("fi");
273 impl_->file_->writeLine("m=${COMP_WORDS[i]}");
274 impl_->file_->writeLine("COMP_WORDS=( \"${COMP_WORDS[@]}\" )");
275 impl_->file_->writeLine("COMP_CWORD=$((COMP_CWORD-i))");
276 impl_->file_->writeLine("case \"$m\" in");
277 for (ModuleNameList::const_iterator i = modules.begin();
278 i != modules.end(); ++i)
280 const char *const name = i->c_str();
281 impl_->file_->writeLine(formatString("%s) %s ;;", name,
282 impl_->completionFunctionName(name).c_str()));
284 impl_->file_->writeLine("esac }");
287 void ShellCompletionWriter::finishCompletions()
289 impl_->file_->close();
290 impl_->file_.reset();