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 << " .q,quit " << kReset << "Quit" << endl;
|
---|
167 | out << endl;
|
---|
168 |
|
---|
169 | return true;
|
---|
170 | }
|
---|
171 |
|
---|
172 | // --------------------------------------------------------------------------
|
---|
173 | //
|
---|
174 | //! Execute a shell command through a pipe. Write stdout to rl_outstream
|
---|
175 | //!
|
---|
176 | //! @param cmd
|
---|
177 | //! Command to be executed
|
---|
178 | //!
|
---|
179 | //! @returns
|
---|
180 | //! always true
|
---|
181 | //
|
---|
182 | bool ReadlineColor::ExecuteShellCommand(ostream &out, const string &cmd)
|
---|
183 | {
|
---|
184 | FILE *pipe = popen(cmd.c_str(), "r");
|
---|
185 | if (!pipe)
|
---|
186 | {
|
---|
187 | out << kRed << "ERROR - Could not create pipe '" << cmd << "': " << strerror(errno) << " [" << errno << "]" << endl;
|
---|
188 | return true;
|
---|
189 | }
|
---|
190 |
|
---|
191 | while (1)
|
---|
192 | {
|
---|
193 | char buf[1024];
|
---|
194 |
|
---|
195 | const size_t sz = fread(buf, 1, 1024, pipe);
|
---|
196 | out.write(buf, sz);
|
---|
197 |
|
---|
198 | if (feof(pipe) || ferror(pipe))
|
---|
199 | break;
|
---|
200 | }
|
---|
201 |
|
---|
202 | out << endl;
|
---|
203 |
|
---|
204 | if (ferror(pipe))
|
---|
205 | out << kRed << "ERROR - Reading from pipe '" << cmd << "': " << strerror(errno) << " [" << errno << "]" << endl;
|
---|
206 |
|
---|
207 | pclose(pipe);
|
---|
208 |
|
---|
209 | return true;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | bool ReadlineColor::Process(ostream &out, const string &str)
|
---|
214 | {
|
---|
215 | // ----------- Readline -----------
|
---|
216 |
|
---|
217 | if (str.substr(0, 2)==".!")
|
---|
218 | return ExecuteShellCommand(out, str.substr(2));
|
---|
219 |
|
---|
220 | if (str=="lh" || str=="history")
|
---|
221 | {
|
---|
222 | out << endl << kBold << "History:" << endl;
|
---|
223 | return Readline::RedirectionWrapper(out, Readline::DumpHistory);
|
---|
224 | }
|
---|
225 |
|
---|
226 | if (str=="v" || str=="variable")
|
---|
227 | {
|
---|
228 | out << endl << kBold << "Variables:" << endl;
|
---|
229 | return Readline::RedirectionWrapper(out, Readline::DumpVariables);
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (str=="f" || str=="function")
|
---|
233 | {
|
---|
234 | out << endl << kBold << "Functions:" << endl;
|
---|
235 | return Readline::RedirectionWrapper(out, Readline::DumpFunctions);
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (str=="m" || str=="funmap")
|
---|
239 | {
|
---|
240 | out << endl << kBold << "Funmap:" << endl;
|
---|
241 | return Readline::RedirectionWrapper(out, Readline::DumpFunmap);
|
---|
242 | }
|
---|
243 |
|
---|
244 | // ------------ ReadlineWindow -------------
|
---|
245 |
|
---|
246 | if (str=="a" || str=="attrs")
|
---|
247 | return PrintAttributes(out);
|
---|
248 |
|
---|
249 | return false;
|
---|
250 | }
|
---|