Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <shell.hpp>
10 :
11 : #include <cmdline.hpp>
12 : #include <kdb.hpp>
13 :
14 : #include <iostream>
15 : #include <sstream>
16 :
17 : using namespace std;
18 : using namespace kdb;
19 :
20 79 : ShellCommand::ShellCommand ()
21 : : supportedCommands (
22 : "kdbGet <name> .. get conf into current keyset\n"
23 : "kdbSet <name> .. set conf from current keyset\n"
24 : "keyClear .. clears the current key\n"
25 : "keySetName <name> .. set name of current key (without bookmarks!)\n"
26 : "keySetMeta <name> <string> .. set meta of current key\n"
27 : "keySetString <string> .. set string of current key\n"
28 : "ksAppendKey .. append current key to current keyset\n"
29 : "ksCut <name> .. cut current keyset\n"
30 237 : "ksOutput .. outputs all keys of current keyset\n")
31 : {
32 79 : }
33 :
34 1 : int ShellCommand::execute (Cmdline const &)
35 : {
36 2 : KeySet current;
37 2 : Key currentKey;
38 :
39 2 : string commandline;
40 4 : string prompt = "> ";
41 :
42 : cout << prompt;
43 74 : while (getline (cin, commandline))
44 : {
45 48 : istringstream is (commandline);
46 48 : string command;
47 :
48 24 : is >> command;
49 24 : if (command == "kdbGet")
50 : {
51 2 : string parent;
52 1 : is >> parent;
53 3 : Key parentKey (parent, KEY_END);
54 3 : cout << "return value: " << kdb.get (current, parentKey) << endl;
55 : }
56 23 : else if (command == "kdbSet")
57 : {
58 2 : string parent;
59 1 : is >> parent;
60 3 : Key parentKey (parent, KEY_END);
61 3 : cout << "return value: " << kdb.set (current, parentKey) << endl;
62 : }
63 22 : else if (command == "keyClear")
64 : {
65 : currentKey.clear ();
66 : }
67 17 : else if (command == "keySetName")
68 : {
69 10 : string name;
70 5 : is >> name;
71 5 : currentKey.setName (name);
72 : }
73 12 : else if (command == "keySetMeta")
74 : {
75 14 : string name;
76 7 : is >> name;
77 14 : string value;
78 7 : is >> value;
79 14 : std::string tmp;
80 7 : getline (is, tmp);
81 7 : value += tmp;
82 14 : currentKey.setMeta (name, value);
83 35 : cout << "Set meta " << name << " to " << value << endl;
84 : }
85 5 : else if (command == "keySetString")
86 : {
87 0 : string value;
88 0 : is >> value;
89 0 : std::string tmp;
90 0 : getline (is, tmp);
91 0 : value += tmp;
92 0 : currentKey.setString (value);
93 : }
94 5 : else if (command == "ksAppendKey")
95 : {
96 20 : current.append (currentKey.dup ());
97 : }
98 0 : else if (command == "ksCut")
99 : {
100 0 : string parent;
101 0 : is >> parent;
102 0 : Key parentKey (parent, KEY_END);
103 :
104 0 : current.cut (parentKey);
105 : }
106 0 : else if (command == "ksOutput")
107 : {
108 : current.rewind ();
109 0 : while (current.next ())
110 : {
111 0 : Key const & c = current.current ();
112 0 : cout << c.getName ();
113 0 : if (c.isString ())
114 : {
115 0 : cout << " string: " << c.getString () << endl;
116 : }
117 : else
118 : {
119 0 : cout << " binary: " << c.getBinary () << " (length: " << c.getBinarySize () << ")" << endl;
120 : }
121 : }
122 : }
123 : else
124 : {
125 : cout << "unknown command!\n"
126 : "supported are:\n"
127 0 : << supportedCommands << endl;
128 : }
129 :
130 24 : cout << prompt;
131 : }
132 :
133 2 : return 0;
134 : }
135 :
136 316 : ShellCommand::~ShellCommand ()
137 : {
138 7322 : }
|