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 INFORMATION " << 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 << " Home " << kReset << "Beginning of line" << endl;
|
---|
126 | out << kBold << " End " << kReset << "End of line" << endl;
|
---|
127 | out << kBold << " Ctrl-d " << kReset << "Quit" << endl;
|
---|
128 | out << kBold << " Ctrl-y " << kReset << "Delete line" << endl;
|
---|
129 | out << kBold << " Alt-end/Ctrl-k " << kReset << "Delete until the end of the line" << endl;
|
---|
130 | out << endl;
|
---|
131 |
|
---|
132 | return true;
|
---|
133 | }
|
---|
134 |
|
---|
135 | // --------------------------------------------------------------------------
|
---|
136 | //
|
---|
137 | //! Print a general help text which also includes the commands pre-defined
|
---|
138 | //! by the Shell class.
|
---|
139 | //!
|
---|
140 | //! @returns
|
---|
141 | //! always true
|
---|
142 | //!
|
---|
143 | //! @todo
|
---|
144 | //! Get it up-to-date
|
---|
145 | //
|
---|
146 | bool ReadlineColor::PrintGeneralHelp(ostream &out, const string &name)
|
---|
147 | {
|
---|
148 | out << endl;
|
---|
149 | out << " " << kUnderline << "General help:" << endl << endl;
|
---|
150 | out << " The command history is automatically loaded and saves to" << endl;
|
---|
151 | out << " and from " << name << endl;
|
---|
152 | out << endl;
|
---|
153 | out << kBold << " h,help " << kReset << "Print this help message\n";
|
---|
154 | out << kBold << " clear " << kReset << "Clear history buffer\n";
|
---|
155 | out << kBold << " lh,history " << kReset << "Dump the history buffer to the screen\n";
|
---|
156 | out << kBold << " v,variable " << kReset << "Dump readline variables\n";
|
---|
157 | out << kBold << " f,function " << kReset << "Dump readline functions\n";
|
---|
158 | out << kBold << " m,funmap " << kReset << "Dump readline funmap\n";
|
---|
159 | out << kBold << " c,command " << kReset << "Dump available commands\n";
|
---|
160 | out << kBold << " k,keylist " << kReset << "Dump key bindings\n";
|
---|
161 | out << kBold << " a,attrs " << kReset << "Dump available stream attributes\n";
|
---|
162 | out << kBold << " .! command " << kReset << "Execute a shell command\n";
|
---|
163 | out << kBold << " .w n " << kReset << "Sleep n milliseconds\n";
|
---|
164 | out << kBold << " .x filename " << kReset << "Execute a script of commands (+optional arguments)\n";
|
---|
165 | out << kBold << " .x file:N " << kReset << "Execute a script of commands, start at label N\n";
|
---|
166 | out << kBold << " .j N " << kReset << "Forward jump to label N\n";
|
---|
167 | out << kBold << " .lt f0 f1 N " << kReset << "If float f0 lower than float f1, jump to label N\n";
|
---|
168 | out << kBold << " .gt f0 f1 N " << kReset << "If float f0 greater than float f1, jump to label N\n";
|
---|
169 | out << kBold << " .eq i0 i1 N " << kReset << "If int i0 equal int i1, jump to label N\n";
|
---|
170 | out << kBold << " : N " << kReset << "Defines a label (N=number)\n";
|
---|
171 | out << kBold << " # comment " << kReset << "Ignored\n";
|
---|
172 | out << kBold << " .q,quit " << kReset << "Quit" << endl;
|
---|
173 | out << endl;
|
---|
174 |
|
---|
175 | return true;
|
---|
176 | }
|
---|
177 |
|
---|
178 | // --------------------------------------------------------------------------
|
---|
179 | //
|
---|
180 | //! Execute a shell command through a pipe. Write stdout to rl_outstream
|
---|
181 | //!
|
---|
182 | //! @param cmd
|
---|
183 | //! Command to be executed
|
---|
184 | //!
|
---|
185 | //! @returns
|
---|
186 | //! always true
|
---|
187 | //
|
---|
188 | bool ReadlineColor::ExecuteShellCommand(ostream &out, const string &cmd)
|
---|
189 | {
|
---|
190 | FILE *pipe = popen(cmd.c_str(), "r");
|
---|
191 | if (!pipe)
|
---|
192 | {
|
---|
193 | out << kRed << "ERROR - Could not create pipe '" << cmd << "': " << strerror(errno) << " [" << errno << "]" << endl;
|
---|
194 | return true;
|
---|
195 | }
|
---|
196 |
|
---|
197 | while (1)
|
---|
198 | {
|
---|
199 | char buf[1024];
|
---|
200 |
|
---|
201 | const size_t sz = fread(buf, 1, 1024, pipe);
|
---|
202 | out.write(buf, sz);
|
---|
203 |
|
---|
204 | if (feof(pipe) || ferror(pipe))
|
---|
205 | break;
|
---|
206 | }
|
---|
207 |
|
---|
208 | out << endl;
|
---|
209 |
|
---|
210 | if (ferror(pipe))
|
---|
211 | out << kRed << "ERROR - Reading from pipe '" << cmd << "': " << strerror(errno) << " [" << errno << "]" << endl;
|
---|
212 |
|
---|
213 | pclose(pipe);
|
---|
214 |
|
---|
215 | return true;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | bool ReadlineColor::Process(ostream &out, const string &str)
|
---|
220 | {
|
---|
221 | // ----------- Readline -----------
|
---|
222 |
|
---|
223 | if (str.substr(0, 2)==".!")
|
---|
224 | return ExecuteShellCommand(out, str.substr(2));
|
---|
225 |
|
---|
226 | if (str=="lh" || str=="history")
|
---|
227 | {
|
---|
228 | out << endl << kBold << "History:" << endl;
|
---|
229 | return Readline::RedirectionWrapper(out, Readline::DumpHistory);
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (str=="v" || str=="variable")
|
---|
233 | {
|
---|
234 | out << endl << kBold << "Variables:" << endl;
|
---|
235 | return Readline::RedirectionWrapper(out, Readline::DumpVariables);
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (str=="f" || str=="function")
|
---|
239 | {
|
---|
240 | out << endl << kBold << "Functions:" << endl;
|
---|
241 | return Readline::RedirectionWrapper(out, Readline::DumpFunctions);
|
---|
242 | }
|
---|
243 |
|
---|
244 | if (str=="m" || str=="funmap")
|
---|
245 | {
|
---|
246 | out << endl << kBold << "Funmap:" << endl;
|
---|
247 | return Readline::RedirectionWrapper(out, Readline::DumpFunmap);
|
---|
248 | }
|
---|
249 |
|
---|
250 | // ------------ ReadlineWindow -------------
|
---|
251 |
|
---|
252 | if (str=="a" || str=="attrs")
|
---|
253 | return PrintAttributes(out);
|
---|
254 |
|
---|
255 | return false;
|
---|
256 | }
|
---|