-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathCliThread.cpp
921 lines (724 loc) · 22.4 KB
/
CliThread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
//==============================================================================
//
// CliThread.cpp
//
// Copyright (C) 2013-2022 Greg Utas
//
// This file is part of the Robust Services Core (RSC).
//
// RSC is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// RSC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#include "CliThread.h"
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <ios>
#include <istream>
#include <sstream>
#include <utility>
#include "CinThread.h"
#include "CliBuffer.h"
#include "CliCommand.h"
#include "CliRegistry.h"
#include "CliStack.h"
#include "CoutThread.h"
#include "Debug.h"
#include "Duration.h"
#include "Element.h"
#include "FileSystem.h"
#include "FileThread.h"
#include "Formatters.h"
#include "NbCliParms.h"
#include "NbDaemons.h"
#include "NbIncrement.h"
#include "PosixSignal.h"
#include "PosixSignalRegistry.h"
#include "Restart.h"
#include "Singleton.h"
#include "Symbol.h"
#include "SymbolRegistry.h"
#include "TraceBuffer.h"
using std::ostream;
using std::string;
//------------------------------------------------------------------------------
namespace NodeBase
{
// The maximum depth of nesting when >send directs output to another file.
//
constexpr size_t MaxOutputDepth = 8;
// The default prompt for user input.
//
constexpr char CliPrompt = '>';
//------------------------------------------------------------------------------
CliThread::CliThread() :
Thread(OperationsFaction, Singleton<CliDaemon>::Instance()),
command_(nullptr),
result_(0)
{
Debug::ft("CliThread.ctor");
AllocResources();
SetInitialized();
}
//------------------------------------------------------------------------------
CliThread::~CliThread()
{
Debug::ftnt("CliThread.dtor");
auto thread = Singleton<CinThread>::Extant();
if(thread != nullptr) thread->ClearClient(this);
}
//------------------------------------------------------------------------------
c_string CliThread::AbbrName() const
{
return CliDaemonName;
}
//------------------------------------------------------------------------------
void CliThread::AllocResources()
{
Debug::ft("CliThread.AllocResources");
ibuf.reset(new CliBuffer);
obuf.reset(new std::ostringstream);
*obuf << std::boolalpha << std::nouppercase;
stack_.reset(new CliStack);
prompt_.clear();
outFiles_.clear();
}
//------------------------------------------------------------------------------
fixed_string YesNoChars = "yn";
fixed_string YesNoHelp = "Enter y(yes) or n(no): ";
bool CliThread::BoolPrompt(const string& prompt)
{
Debug::ft("CliThread.BoolPrompt");
return (CharPrompt(prompt, YesNoChars, YesNoHelp) == 'y');
}
//------------------------------------------------------------------------------
char CliThread::CharPrompt
(const string& prompt, const string& chars, const string& help, bool upper)
{
Debug::ft("CliThread.CharPrompt");
// If input is being taken from a file rather than the console,
// return the first character.
//
if(chars.empty()) return NUL;
if(ibuf->ReadingFromFile()) return chars.front();
auto first = true;
string text;
// Output the query until the user enters a character in CHARS.
// Echo the user's input to the console transcript file.
//
while(true)
{
ostringstreamPtr stream(new std::ostringstream);
if(first)
*stream << prompt << " [" << chars << "]: ";
else
*stream << help;
Flush();
CoutThread::Spool(stream);
first = false;
if(CinThread::GetLine(text) < 0) return NUL;
text.pop_back();
FileThread::Record(text, true);
if(text.size() == 1)
{
char c = (upper ? text[0] : tolower(text[0]));
if(chars.find(c) != string::npos) return c;
}
}
}
//------------------------------------------------------------------------------
void CliThread::Destroy()
{
Debug::ft("CliThread.Destroy");
Singleton<CliThread>::Destroy();
}
//------------------------------------------------------------------------------
void CliThread::Display(ostream& stream,
const string& prefix, const Flags& options) const
{
Thread::Display(stream, prefix, options);
auto lead1 = prefix + spaces(2);
auto lead2 = prefix + spaces(4);
stream << prefix << "ibuf : " << CRLF;
ibuf->Display(stream, lead1, options);
stream << prefix << "obuf : " << obuf.get() << CRLF;
stream << prefix << "stack : " << CRLF;
stack_->Display(stream, lead1, options);
stream << prefix << "prompt : " << prompt_ << CRLF;
stream << prefix << "command : " << strObj(command_) << CRLF;
stream << prefix << "cookie : " << CRLF;
cookie_.Display(stream, lead1, options);
stream << prefix << "result : " << result_ << CRLF;
stream << prefix << "stream : " << stream_.get() << CRLF;
stream << prefix << "outFiles : " << CRLF;
for(size_t i = 0; i < outFiles_.size(); ++i)
{
stream << lead1 << strIndex(i) << outFiles_.at(i) << CRLF;
}
stream << prefix << "appsData : " << CRLF;
for(auto a = appsData_.cbegin(); a != appsData_.cend(); ++a)
{
stream << lead1 << strIndex(a->first) << CRLF;
a->second->Display(stream, lead2, options);
}
}
//------------------------------------------------------------------------------
word CliThread::DisplayHelp(const string& path, const string& key) const
{
Debug::ft("CliThread.DisplayHelp");
// Open the help file addressed by PATH.
//
auto stream = FileSystem::CreateIstream(path.c_str());
if(stream == nullptr) return -2;
// Find line that contains "? KEY" and display the lines that follow, up
// to the next line that begins with '?'. If a line begins with '?' and
// ends with '*', it is a wildcard that matches KEY if KEY's begins with
// the same characters as those that precede the asterisk.
//
auto found = false;
string line;
while(stream->peek() != EOF)
{
FileSystem::GetLine(*stream, line);
if(line.empty())
{
if(found) *obuf << CRLF;
continue;
}
switch(line.front())
{
case '/':
continue;
case '?':
if(found) return 0;
while(line.back() == SPACE) line.pop_back();
line.erase(0, 2);
if(!line.empty() && line.back() == '*')
{
auto keyStart = key.substr(0, line.size() - 1);
auto lineStart = line.substr(0, line.size() - 1);
if(strCompare(lineStart, keyStart, false) == 0) found = true;
}
else
{
if(strCompare(line, key, false) == 0) found = true;
}
break;
default:
if(found) *obuf << line << CRLF;
}
}
return (found ? 0 : -1);
}
//------------------------------------------------------------------------------
bool CliThread::EndOfInput() const
{
Debug::ft("CliThread.EndOfInput");
if(!ibuf->FindNextNonBlank()) return true;
ibuf->ErrorAtPos(*this, "Error: extra input");
return false;
}
//------------------------------------------------------------------------------
void CliThread::Enter()
{
Debug::ft("CliThread.Enter");
// Put the root increment on the stack and start reading commands.
//
stack_->SetRoot(*Singleton<NbIncrement>::Instance());
while(true) ReadCommands();
}
//------------------------------------------------------------------------------
word CliThread::Execute(const string& input)
{
Debug::ft("CliThread.Execute");
word result = -1;
Flush();
auto rc = ibuf->PutLine(*this, input);
if(rc == StreamOk)
{
auto comm = ParseCommand();
if(comm != nullptr) result = InvokeCommand(*comm);
}
Flush();
return result;
}
//------------------------------------------------------------------------------
ostream* CliThread::FileStream()
{
Debug::ft("CliThread.FileStream");
stream_ = FileThread::CreateStream();
return stream_.get();
}
//------------------------------------------------------------------------------
void CliThread::Flush()
{
Debug::ft("CliThread.Flush");
// Send output to either the console or a separate file.
//
if((obuf != nullptr) && (obuf->tellp() > 0))
{
if(outFiles_.empty())
CoutThread::Spool(obuf);
else
FileThread::Spool(outFiles_.back(), obuf);
Pause();
}
// Create a new output buffer for the next command's results.
//
obuf.reset(new std::ostringstream);
*obuf << std::boolalpha << std::nouppercase;
}
//------------------------------------------------------------------------------
bool CliThread::GenerateReportPreemptably()
{
Debug::ft("CliThread.GenerateReportPreemptably");
// Generate the report preemptably unless tracing is on in the lab
// and the user specifically wants to trace report generation.
//
if(Debug::TraceOn())
{
if(!Element::RunningInLab()) return true;
if(BoolPrompt(StopTracingPrompt))
{
Singleton<TraceBuffer>::Instance()->StopTracing();
return true;
}
if(BoolPrompt(TraceReportPrompt)) return false;
}
return true;
}
//------------------------------------------------------------------------------
CliAppData* CliThread::GetAppData(CliAppData::Id aid) const
{
Debug::ft("CliThread.GetAppData");
auto a = appsData_.find(aid);
if(a != appsData_.cend()) return a->second.get();
return nullptr;
}
//------------------------------------------------------------------------------
void CliThread::Inform(const std::ostringstream& stream)
{
Debug::ft("CliThread.Inform(stream)");
Inform(stream.str());
}
//------------------------------------------------------------------------------
void CliThread::Inform(const string& str)
{
Debug::ft("CliThread.Inform(string)");
*obuf << str;
Flush();
}
//------------------------------------------------------------------------------
word CliThread::IntPrompt(const string& prompt, word min, word max)
{
Debug::ft("CliThread.IntPrompt");
word result = -1;
// If input is being taken from a file rather than the console,
// return 0.
//
if(ibuf->ReadingFromFile()) return 0;
auto first = true;
string text;
// Output the query until the user enters an integer. Echo the
// user's input to the console transcript file.
//
while(true)
{
ostringstreamPtr stream(new std::ostringstream);
if(first)
*stream << prompt;
else
*stream << "Enter " << min << " to " << max << ": ";
Flush();
CoutThread::Spool(stream);
first = false;
if(CinThread::GetLine(text) < 0) return -1;
text.pop_back();
string input(text);
FileThread::Record(input, true);
auto rc = CliBuffer::GetInt(input, result, false);
if(rc != CliParm::Ok) continue;
if((result >= min) && (result <= max)) return result;
}
}
//------------------------------------------------------------------------------
word CliThread::InvokeCommand(const CliCommand& comm)
{
Debug::ft("CliThread.InvokeCommand");
// Initialize the cookie so that it will look for the command's
// first parameter.
//
cookie_.Initialize();
// Execute the command and output the results.
//
Pause();
command_ = &comm;
SetResult(comm.ProcessCommand(*this));
command_ = nullptr;
return result_;
}
//------------------------------------------------------------------------------
word CliThread::InvokeSubcommand(const CliCommand& comm)
{
Debug::ft("CliThread.InvokeSubcommand");
auto prev = command_;
command_ = &comm;
SetResult(comm.ProcessCommand(*this));
command_ = prev;
return result_;
}
//------------------------------------------------------------------------------
void CliThread::Notify(CliAppData::Event event) const
{
Debug::ft("CliThread.Notify");
for(auto a = appsData_.cbegin(); a != appsData_.cend(); ++a)
{
a->second->EventOccurred(event);
}
}
//------------------------------------------------------------------------------
const CliCommand* CliThread::ParseCommand() const
{
Debug::ft("CliThread.ParseCommand");
string token1;
string token2;
string tag;
auto inIncr = true;
// Record the command in any output file. (CliBuffer::GetLine copies
// each input to the console and/or the console transcript file.)
//
if(!outFiles_.empty())
{
auto input = ibuf->GetInput();
FileThread::Spool(outFiles_.back(), input, true);
}
// Get the first token, which must be the name of a command or
// increment.
//
if(ibuf->GetStr(tag, token1) != CliParm::Ok) return nullptr;
if(!tag.empty()) return nullptr;
auto comm = stack_->FindCommand(token1);
if(comm != nullptr)
{
// <command>
//
return comm;
}
auto incr = stack_->FindIncrement(token1);
if(incr == nullptr)
{
incr = Singleton<CliRegistry>::Instance()->FindIncrement(token1);
if(incr == nullptr)
{
// <junk>
//
*obuf << spaces(2) << NoCommandExpl << token1 << CRLF;
return nullptr;
}
inIncr = false;
stack_->Push(*incr);
}
if(ibuf->GetStr(tag, token2) != CliParm::Ok)
{
// <increment>
//
if(inIncr) *obuf << AlreadyInIncrement << token1 << '.' << CRLF;
return nullptr;
}
if(!tag.empty()) return nullptr;
comm = incr->FindCommand(token2);
if(comm == nullptr)
{
// <increment> <junk>
//
*obuf << spaces(2) << NoCommandExpl << token1;
*obuf << CliCommand::CommandSeparator << token2 << CRLF;
}
if(!inIncr) stack_->Pop();
return comm;
}
//------------------------------------------------------------------------------
void CliThread::Patch(sel_t selector, void* arguments)
{
Thread::Patch(selector, arguments);
}
//------------------------------------------------------------------------------
bool CliThread::PopOutputFile(bool all)
{
Debug::ft("CliThread.PopOutputFile");
if(outFiles_.empty()) return false;
SendAckToOutputFile();
while(!outFiles_.empty())
{
outFiles_.pop_back();
if(!all) return true;
}
return true;
}
//------------------------------------------------------------------------------
bool CliThread::PushOutputFile(const string& file)
{
Debug::ft("CliThread.PushOutputFile");
if(outFiles_.size() >= MaxOutputDepth) return false;
SendAckToOutputFile();
outFiles_.push_back(file);
return true;
}
//------------------------------------------------------------------------------
fn_name CliThread_ReadCommands = "CliThread.ReadCommands";
void CliThread::ReadCommands()
{
Debug::ft(CliThread_ReadCommands);
auto skip = false; // suppresses the CLI prompt if set
while(true)
{
// Print the CLI prompt, which contains the name of the
// most recently entered increment (if any).
//
string prompt(stack_->Top()->Name());
prompt.push_back(CliPrompt);
prompt_ = prompt;
if(!skip)
{
CoutThread::Spool(prompt_.c_str());
if(!outFiles_.empty())
{
FileThread::Spool(outFiles_.back(), prompt_, false);
}
}
skip = false;
// Read the user's input and parse it.
//
auto rc = ibuf->GetLine(*this);
if(rc > 0)
{
auto comm = ParseCommand();
if(comm != nullptr) InvokeCommand(*comm);
Flush();
}
else
{
switch(rc)
{
case StreamEmpty:
//
// Redisplay the prompt again only if reading from the console.
//
skip = ibuf->ReadingFromFile();
break;
case StreamBadChar:
//
// CliBuffer has displayed an error string. Just loop around and
// prompt for new input.
//
break;
case StreamEof:
case StreamFailure:
//
// CliBuffer has handled the error. Continue with the next input.
//
skip = true;
break;
case StreamInterrupt:
case StreamRestart:
//
// o StreamInterrupt occurs when we plan to exit during a restart.
// Pausing causes us to receive SIGCLOSE and exit.
// o StreamRestart can occur if InitThread traps during a restart,
// after we have been created. Pausing is also appropriate in
// this case, as another restart should occur momentarily.
//
Pause(msecs_t(1 * ONE_SEC));
break;
case StreamInUse:
default:
Debug::SwLog(CliThread_ReadCommands, "unexpected StreamRc", rc);
}
}
}
}
//------------------------------------------------------------------------------
bool CliThread::Recover()
{
Debug::ft("CliThread.Recover");
auto sig = GetSignal();
auto reg = Singleton<PosixSignalRegistry>::Instance();
if(reg->Attrs(sig).test(PosixSignal::Break))
{
// On a break signal, remain in the current increment(s) but
// abort whatever work was in progress.
//
appsData_.clear();
outFiles_.clear();
stream_.reset();
ibuf->Reset();
}
return true;
}
//------------------------------------------------------------------------------
word CliThread::Report(word rc, const string& expl, col_t indent) const
{
Debug::ft("CliThread.Report");
// If EXPL contains explicit endlines, output each substring
// that ends with an endline individually.
//
size_t size = expl.size();
size_t begin = 0;
while(begin < size)
{
auto end = expl.find(CRLF, begin);
if(end == string::npos)
{
Report1(expl, begin, size - 1, indent);
return rc;
}
if(end == begin)
*obuf << CRLF;
else
Report1(expl, begin, end - 1, indent);
begin = end + 1;
}
return rc;
}
//------------------------------------------------------------------------------
void CliThread::Report1
(const string& expl, size_t begin, size_t end, col_t indent) const
{
Debug::ft("CliThread.Report1");
size_t maxlen = 79 - indent; // maximum line length
while(begin <= end)
{
// If the rest of EXPL fits on one line, output it and return.
//
auto size = end - begin + 1;
if(size <= maxlen)
{
*obuf << spaces(indent) << expl.substr(begin, size) << CRLF;
return;
}
// Starting at the last character that would fit on a line,
// work backwards to find a blank, and insert an endline at
// that point. Then continue with the remaining characters.
//
auto stop = std::min(begin + maxlen - 1, end);
auto blank = expl.rfind(SPACE, stop);
if(blank != string::npos)
{
*obuf << spaces(indent) << expl.substr(begin, blank - begin) << CRLF;
begin = blank + 1;
}
else
{
// There are more characters without intervening blanks than
// fit on one line. Just output the entire substring and let
// it wrap wherever.
//
*obuf << spaces(indent) << expl.substr(begin, size) << CRLF;
return;
}
}
}
//------------------------------------------------------------------------------
void CliThread::SendAckToOutputFile()
{
Debug::ft("CliThread.SendAckToOutputFile");
std::ostringstream ack;
ack << spaces(2) << SuccessExpl << CRLF;
if(outFiles_.empty())
CoutThread::Spool(ack.str().c_str());
else
FileThread::Spool(outFiles_.back(), ack.str());
}
//------------------------------------------------------------------------------
void CliThread::SendToFile(const string& name, bool purge)
{
Debug::ft("CliThread.SendToFile");
if(stream_->tellp() > 0)
{
FileThread::Spool(name, stream_, purge);
}
}
//------------------------------------------------------------------------------
void CliThread::SetAppData(CliAppData* data, CliAppData::Id aid)
{
Debug::ft("CliThread.SetAppData");
if(data == nullptr)
appsData_.erase(aid);
else
appsData_[aid].reset(data);
}
//------------------------------------------------------------------------------
void CliThread::SetResult(word result)
{
Debug::ft("CliThread.SetResult");
result_ = result;
auto reg = Singleton<SymbolRegistry>::Instance();
auto sym = reg->EnsureSymbol("cli.result");
if(sym != nullptr) sym->SetValue(std::to_string(result_), false);
}
//------------------------------------------------------------------------------
void CliThread::Shutdown(RestartLevel level)
{
Debug::ft("CliThread.Shutdown");
// Nullify the resources whose heap will be deleted during a restart.
//
Restart::Release(ibuf);
Restart::Release(stack_);
for(auto a = appsData_.begin(); a != appsData_.end(); ++a)
{
Restart::Release(a->second);
}
Thread::Shutdown(level);
}
//------------------------------------------------------------------------------
void CliThread::Startup(RestartLevel level)
{
Debug::ft("CliThread.Startup");
Thread::Startup(level);
AllocResources();
}
//------------------------------------------------------------------------------
string CliThread::StrPrompt(const string& prompt)
{
Debug::ft("CliThread.StrPrompt");
// If input is being taken from a file rather than the console,
// return an empty string.
//
if(ibuf->ReadingFromFile()) return EMPTY_STR;
auto first = true;
string text;
// Output the query, read the user's input, and echo it to the
// console transcript file before returning it.
//
while(true)
{
ostringstreamPtr stream(new std::ostringstream);
if(first)
*stream << prompt;
else
*stream << "Please enter a non-empty string";
Flush();
CoutThread::Spool(stream);
first = false;
if(CinThread::GetLine(text) < 0) return EMPTY_STR;
text.pop_back();
if(!text.empty())
{
string input(text);
FileThread::Record(input, true);
return input;
}
}
}
}