1 | // **************************************************************************
|
---|
2 | /** @namespace ReadlineColor
|
---|
3 |
|
---|
4 | @brief A fewer helper functions to apply color attributes and redirect the output
|
---|
5 |
|
---|
6 | */
|
---|
7 | // **************************************************************************
|
---|
8 | #include "ReadlineColor.h"
|
---|
9 |
|
---|
10 | #include <boost/version.hpp>
|
---|
11 | #include <boost/filesystem.hpp>
|
---|
12 |
|
---|
13 | #include "Time.h"
|
---|
14 | #include "Readline.h"
|
---|
15 | #include "WindowLog.h"
|
---|
16 |
|
---|
17 | using namespace std;
|
---|
18 |
|
---|
19 | // --------------------------------------------------------------------------
|
---|
20 | //
|
---|
21 | //! @returns
|
---|
22 | //! always true
|
---|
23 | //
|
---|
24 | bool ReadlineColor::PrintBootMsg(ostream &out, const string &name, bool interactive)
|
---|
25 | {
|
---|
26 | #if BOOST_VERSION < 104600
|
---|
27 | const string n = boost::filesystem::path(name).stem();
|
---|
28 | #else
|
---|
29 | const string n = boost::filesystem::path(name).stem().string();
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | out << kBlue << kBold << kUnderline << "\n Master Control Program (compiled "__DATE__" " __TIME__ << ") " << endl;
|
---|
33 | out << kBlue <<
|
---|
34 | "\n"
|
---|
35 | " ENCOM MX 16-923 USER # 0" << int(Time().Mjd()) << Time::fmt(" %H:%M:%S") << Time() << Time::reset << " INFORMATION\n"
|
---|
36 | "\n"
|
---|
37 | " TELESCOPE CONTROL PROGRAM: " << n << "\n"
|
---|
38 | " ANNEXED BY FACT COLLABORATION\n"
|
---|
39 | " ORIGINAL PROGRAM WRITTEN BY T.BRETZ\n"
|
---|
40 | " THIS INFOMATION " << kUnderline << "PRIORITY ONE"
|
---|
41 | << endl;
|
---|
42 | out << kBlue << " END OF LINE\n" << endl;
|
---|
43 |
|
---|
44 | if (!interactive)
|
---|
45 | return true;
|
---|
46 |
|
---|
47 | out << "Enter 'h' for help." << endl;
|
---|
48 | out << endl;
|
---|
49 |
|
---|
50 | return true;
|
---|
51 | }
|
---|
52 |
|
---|
53 | // --------------------------------------------------------------------------
|
---|
54 | //
|
---|
55 | //! @returns
|
---|
56 | //! always true
|
---|
57 | //
|
---|
58 | bool ReadlineColor::PrintCommands(ostream &out)
|
---|
59 | {
|
---|
60 | out << endl;
|
---|
61 | out << " " << kUnderline << " Commands:" << endl;
|
---|
62 | out << " No application specific commands defined." << endl;
|
---|
63 | out << endl;
|
---|
64 |
|
---|
65 | return true;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // --------------------------------------------------------------------------
|
---|
69 | //
|
---|
70 | //! Displays the available ncurses attributes, like color.
|
---|
71 | //!
|
---|
72 | //! @returns
|
---|
73 | //! always true
|
---|
74 | //
|
---|
75 | bool ReadlineColor::PrintAttributes(ostream &out)
|
---|
76 | {
|
---|
77 | out << endl;
|
---|
78 | out << " Attributes:" << endl;
|
---|
79 | out << " " << kReset << "kReset" << endl;
|
---|
80 | out << " " << kNormal << "kNormal" << endl;
|
---|
81 | out << " " << kHighlight << "kHighlight" << endl;
|
---|
82 | out << " " << kReverse << "kReverse" << endl;
|
---|
83 | out << " " << kUnderline << "kUnderline" << endl;
|
---|
84 | out << " " << kBlink << "kBlink" << endl;
|
---|
85 | out << " " << kDim << "kDim" << endl;
|
---|
86 | out << " " << kBold << "kBold" << endl;
|
---|
87 | out << " " << kProtect << "kProtect" << endl;
|
---|
88 | out << " " << kInvisible << "kInvisible" << endl;
|
---|
89 | out << " " << kAltCharset << "kAltCharset" << kReset << " (kAltCharset)" << endl;
|
---|
90 | out << endl;
|
---|
91 | out << " Colors:" << endl;
|
---|
92 | out << " " << kDefault << "kDefault " << kBold << "+ kBold" << endl;
|
---|
93 | out << " " << kRed << "kRed " << kBold << "+ kBold" << endl;
|
---|
94 | out << " " << kGreen << "kGreen " << kBold << "+ kBold" << endl;
|
---|
95 | out << " " << kYellow << "kYellow " << kBold << "+ kBold" << endl;
|
---|
96 | out << " " << kBlue << "kBlue " << kBold << "+ kBold" << endl;
|
---|
97 | out << " " << kMagenta << "kMagenta " << kBold << "+ kBold" << endl;
|
---|
98 | out << " " << kCyan << "kCyan " << kBold << "+ kBold" << endl;
|
---|
99 | out << " " << kWhite << "kWhite " << kBold << "+ kBold" << endl;
|
---|
100 | out << " " << endl;
|
---|
101 |
|
---|
102 | return true;
|
---|
103 | }
|
---|
104 |
|
---|
105 | // --------------------------------------------------------------------------
|
---|
106 | //
|
---|
107 | //! Displays the keybindings available due to the Shell class
|
---|
108 | //!
|
---|
109 | //! @returns
|
---|
110 | //! always true
|
---|
111 | //!
|
---|
112 | //! @todo
|
---|
113 | //! Update output
|
---|
114 | //
|
---|
115 | bool ReadlineColor::PrintKeyBindings(ostream &out)
|
---|
116 | {
|
---|
117 | out << endl;
|
---|
118 | out << " " << kUnderline << "Key bindings:" << endl << endl;;
|
---|
119 | out << " Default key-bindings are identical with your bash." << endl;
|
---|
120 | out << endl;
|
---|
121 | out << kBold << " Page-up " << kReset << "Search backward in history" << endl;
|
---|
122 | out << kBold << " Page-dn " << kReset << "Search forward in history" << endl;
|
---|
123 | out << kBold << " Ctrl-left " << kReset << "One word backward" << endl;
|
---|
124 | out << kBold << " Ctrl-right " << kReset << "One word forward" << endl;
|
---|
125 | out << kBold << " Ctrl-d " << kReset << "Quit" << endl;
|
---|
126 | out << kBold << " Ctrl-y " << kReset << "Delete line" << endl;
|
---|
127 | out << kBold << " Alt-end/Ctrl-k " << kReset << "Delete until the end of the line" << endl;
|
---|
128 | out << endl;
|
---|
129 |
|
---|
130 | return true;
|
---|
131 | }
|
---|
132 |
|
---|
133 | // --------------------------------------------------------------------------
|
---|
134 | //
|
---|
135 | //! Print a general help text which also includes the commands pre-defined
|
---|
136 | //! by the Shell class.
|
---|
137 | //!
|
---|
138 | //! @returns
|
---|
139 | //! always true
|
---|
140 | //!
|
---|
141 | //! @todo
|
---|
142 | //! Get it up-to-date
|
---|
143 | //
|
---|
144 | bool ReadlineColor::PrintGeneralHelp(ostream &out, const string &name)
|
---|
145 | {
|
---|
146 | out << endl;
|
---|
147 | out << " " << kUnderline << "General help:" << endl << endl;
|
---|
148 | out << " The command history is automatically loaded and saves to" << endl;
|
---|
149 | out << " and from " << name << endl;
|
---|
150 | out << endl;
|
---|
151 | out << kBold << " h,help " << kReset << "Print this help message\n";
|
---|
152 | out << kBold << " clear " << kReset << "Clear history buffer\n";
|
---|
153 | out << kBold << " lh,history " << kReset << "Dump the history buffer to the screen\n";
|
---|
154 | out << kBold << " v,variable " << kReset << "Dump readline variables\n";
|
---|
155 | out << kBold << " f,function " << kReset << "Dump readline functions\n";
|
---|
156 | out << kBold << " m,funmap " << kReset << "Dump readline funmap\n";
|
---|
157 | out << kBold << " c,command " << kReset << "Dump available commands\n";
|
---|
158 | out << kBold << " k,keylist " << kReset << "Dump key bindings\n";
|
---|
159 | out << kBold << " a,attrs " << kReset << "Dump available stream attributes\n";
|
---|
160 | out << kBold << " .! command " << kReset << "Execute a shell command\n";
|
---|
161 | out << kBold << " .w n " << kReset << "Sleep n milliseconds\n";
|
---|
162 | out << kBold << " .x filename " << kReset << "Execute a script of commands (+optional arguments)\n";
|
---|
163 | out << kBold << " .x file:N " << kReset << "Execute a script of commands, start at label N\n";
|
---|
164 | out << kBold << " .j N " << kReset << "Forward jump to label N\n";
|
---|
165 | out << kBold << " : N " << kReset << "Defines a label\n";
|
---|
166 | out << kBold << " # comment " << kReset << "Ignored\n";
|
---|
167 | out << kBold << " .q,quit " << kReset << "Quit" << endl;
|
---|
168 | out << endl;
|
---|
169 |
|
---|
170 | return true;
|
---|
171 | }
|
---|
172 |
|
---|
173 | // --------------------------------------------------------------------------
|
---|
174 | //
|
---|
175 | //! Execute a shell command through a pipe. Write stdout to rl_outstream
|
---|
176 | //!
|
---|
177 | //! @param cmd
|
---|
178 | //! Command to be executed
|
---|
179 | //!
|
---|
180 | //! @returns
|
---|
181 | //! always true
|
---|
182 | //
|
---|
183 | bool ReadlineColor::ExecuteShellCommand(ostream &out, const string &cmd)
|
---|
184 | {
|
---|
185 | FILE *pipe = popen(cmd.c_str(), "r");
|
---|
186 | if (!pipe)
|
---|
187 | {
|
---|
188 | out << kRed << "ERROR - Could not create pipe '" << cmd << "': " << strerror(errno) << " [" << errno << "]" << endl;
|
---|
189 | return true;
|
---|
190 | }
|
---|
191 |
|
---|
192 | while (1)
|
---|
193 | {
|
---|
194 | char buf[1024];
|
---|
195 |
|
---|
196 | const size_t sz = fread(buf, 1, 1024, pipe);
|
---|
197 | out.write(buf, sz);
|
---|
198 |
|
---|
199 | if (feof(pipe) || ferror(pipe))
|
---|
200 | break;
|
---|
201 | }
|
---|
202 |
|
---|
203 | out << endl;
|
---|
204 |
|
---|
205 | if (ferror(pipe))
|
---|
206 | out << kRed << "ERROR - Reading from pipe '" << cmd << "': " << strerror(errno) << " [" << errno << "]" << endl;
|
---|
207 |
|
---|
208 | pclose(pipe);
|
---|
209 |
|
---|
210 | return true;
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | bool ReadlineColor::Process(ostream &out, const string &str)
|
---|
215 | {
|
---|
216 | // ----------- Readline -----------
|
---|
217 |
|
---|
218 | if (str.substr(0, 2)==".!")
|
---|
219 | return ExecuteShellCommand(out, str.substr(2));
|
---|
220 |
|
---|
221 | if (str=="lh" || str=="history")
|
---|
222 | {
|
---|
223 | out << endl << kBold << "History:" << endl;
|
---|
224 | return Readline::RedirectionWrapper(out, Readline::DumpHistory);
|
---|
225 | }
|
---|
226 |
|
---|
227 | if (str=="v" || str=="variable")
|
---|
228 | {
|
---|
229 | out << endl << kBold << "Variables:" << endl;
|
---|
230 | return Readline::RedirectionWrapper(out, Readline::DumpVariables);
|
---|
231 | }
|
---|
232 |
|
---|
233 | if (str=="f" || str=="function")
|
---|
234 | {
|
---|
235 | out << endl << kBold << "Functions:" << endl;
|
---|
236 | return Readline::RedirectionWrapper(out, Readline::DumpFunctions);
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (str=="m" || str=="funmap")
|
---|
240 | {
|
---|
241 | out << endl << kBold << "Funmap:" << endl;
|
---|
242 | return Readline::RedirectionWrapper(out, Readline::DumpFunmap);
|
---|
243 | }
|
---|
244 |
|
---|
245 | // ------------ ReadlineWindow -------------
|
---|
246 |
|
---|
247 | if (str=="a" || str=="attrs")
|
---|
248 | return PrintAttributes(out);
|
---|
249 |
|
---|
250 | return false;
|
---|
251 | }
|
---|