MCPcopy Create free account
hub / github.com/Icinga/icinga2 / GetBashCompletionSuggestions

Method GetBashCompletionSuggestions

lib/cli/clicommand.cpp:18–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16namespace po = boost::program_options;
17
18std::vector<String> icinga::GetBashCompletionSuggestions(const String& type, const String& word)
19{
20 std::vector<String> result;
21
22#ifndef _WIN32
23 String bashArg = "compgen -A " + Utility::EscapeShellArg(type) + " " + Utility::EscapeShellArg(word);
24 String cmd = "bash -c " + Utility::EscapeShellArg(bashArg);
25
26 FILE *fp = popen(cmd.CStr(), "r");
27
28 char line[4096];
29 while (fgets(line, sizeof(line), fp)) {
30 String wline = line;
31 boost::algorithm::trim_right_if(wline, boost::is_any_of("\r\n"));
32 result.push_back(wline);
33 }
34
35 pclose(fp);
36
37 /* Append a slash if there's only one suggestion and it's a directory */
38 if ((type == "file" || type == "directory") && result.size() == 1) {
39 String path = result[0];
40
41 struct stat statbuf;
42 if (lstat(path.CStr(), &statbuf) >= 0) {
43 if (S_ISDIR(statbuf.st_mode)) {
44 result.clear(),
45 result.push_back(path + "/");
46 }
47 }
48 }
49#endif /* _WIN32 */
50
51 return result;
52}
53
54std::vector<String> icinga::GetFieldCompletionSuggestions(const Type::Ptr& type, const String& word)
55{

Callers

nothing calls this directly

Calls 4

CStrMethod · 0.80
push_backMethod · 0.80
sizeMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected