43static const char AsciiCtrA =
'\001';
44static const char AsciiCtrB =
'\002';
45static const char AsciiCtrC =
'\003';
46static const char AsciiCtrD =
'\004';
47static const char AsciiCtrE =
'\005';
48static const char AsciiCtrF =
'\006';
49static const char AsciiCtrK =
'\013';
50static const char AsciiCtrL =
'\014';
51static const char AsciiCtrN =
'\016';
52static const char AsciiCtrP =
'\020';
53static const char AsciiCtrQ =
'\021';
54static const char AsciiCtrS =
'\023';
55static const char AsciiCtrZ =
'\032';
56static const char AsciiTAB =
'\011';
57static const char AsciiBS =
'\010';
58static const char AsciiDEL =
'\177';
59static const char AsciiESC =
'\033';
61static const int AsciiPrintableMin = 32;
64static const G4String historyFileName =
"/.g4_hist";
71 commandHistory(maxhist),
74 relativeHistoryIndex(0)
81 const char* path = std::getenv(
"HOME");
82 if (path ==
nullptr)
return;
85 G4String fname = homedir + historyFileName;
87 std::ifstream histfile;
92 char linebuf[BUFSIZE];
94 histfile.open(fname, std::ios::in);
95 while (histfile.good()) {
96 if (histfile.eof())
break;
98 histfile.getline(linebuf, BUFSIZE);
99 G4String aline = G4StrUtil::strip_copy(linebuf);
110 const char* path = std::getenv(
"HOME");
111 if (path ==
nullptr)
return;
114 G4String fname = homedir + historyFileName;
116 std::ofstream histfile;
117 histfile.open(fname, std::ios::out);
146 if (msg !=
nullptr) {
162 std::ostringstream os;
202 if (cc < AsciiPrintableMin || (isprint(cc) == 0))
return;
231 G4cout << AsciiBS <<
' ' << AsciiBS << std::flush;
319 G4cout << AsciiBS << std::flush;
340 G4cout << AsciiBS << std::flush;
413 auto jhead = input.rfind(
' ');
414 if (jhead != G4String::npos) {
415 input.erase(0, jhead);
416 G4StrUtil::lstrip(input);
423 if (! input.empty()) {
424 auto len = (
G4int)input.length();
426 for (
G4int i = len - 1; i >= 0; --i) {
427 if (input[i] ==
'/') {
434 if (indx != 0 || len != 1) vcmd = input.substr(indx + 1, len - indx - 1);
452 auto jhead = input.rfind(
' ');
453 if (jhead != G4String::npos) {
454 input.erase(0, jhead);
455 G4StrUtil::lstrip(input);
459 std::size_t thead = input.find_last_of(
'/');
461 if (thead != G4String::npos) strtail = input.substr(thead + 1, input.size() - thead - 1);
467 auto len = (
G4int)input.length();
468 if (! input.empty()) {
470 for (
G4int i = len - 1; i >= 0; --i) {
471 if (input[i] ==
'/') {
478 if (indx != 0 || len != 1) vcmd = input.substr(indx + 1, len - indx - 1);
482 if (atree ==
nullptr)
return;
493 for (
G4int idir = 1; idir <= Ndir; idir++) {
496 if (fpdir.find(inputpath, 0) == 0) {
509 for (
G4int icmd = 1; icmd <= Ncmd; icmd++) {
512 if (fpcmd.find(inputpath, 0) == 0) {
527 jhead = input.rfind(
' ');
528 if (jhead == G4String::npos)
533 std::size_t jt = jhead;
537 for (i = jt; i <= input.length() - 1; ++i)
539 for (i = jt; i <= input.length() - 1; ++i)
541 for (i = jt; i <= input.length() - 1; ++i)
544 dspstr += (vpath + stream);
545 if (nMatch == 0) dspstr += strtail;
546 G4cout << dspstr << std::flush;
550 input += (vpath + stream);
551 if (nMatch == 0) input += strtail;
627 if (cc == AsciiESC) {
629 if (cc ==
'[' || cc ==
'O') {
658 }
while (cc !=
'\n');
676 while ((newCommand.length() > 0) && (newCommand[
G4int(newCommand.length() - 1)] ==
'_')) {
677 newCommand.erase(newCommand.length() - 1);
682 newCommand.append(newLine);
686 G4bool isMeaningfull =
false;
687 for (
G4int i = 0; i < (
G4int)newCommand.length(); ++i) {
688 if (newCommand[i] !=
' ') {
689 isMeaningfull =
true;
693 if (! newCommand.empty() && isMeaningfull)
StoreHistory(newCommand);
706 std::size_t nlen1 = str1.length();
707 std::size_t nlen2 = str2.length();
709 std::size_t nmin = nlen1 < nlen2 ? nlen1 : nlen2;
713 if (str1[i] == str2[i]) {
714 strMatched += str1[i];
757 termios tiosbuf =
tios;
759 tiosbuf.c_iflag &= ~(BRKINT | ISTRIP);
760 tiosbuf.c_iflag |= (IGNBRK | IGNPAR);
761 tiosbuf.c_lflag &= ~(ICANON | IEXTEN | ECHO);
762 tiosbuf.c_cc[VMIN] = 1;
763 tiosbuf.c_cc[VTIME] = 0;
765 tcsetattr(0, TCSAFLUSH, &tiosbuf);
772 tcsetattr(0, TCSAFLUSH, &
tios);
G4GLOB_DLL std::ostream G4cout
const G4ApplicationState & GetCurrentState() const
G4String GetStateString(const G4ApplicationState &aState) const
static G4StateManager * GetStateManager()
G4int GetCommandEntry() const
G4UIcommand * GetCommand(G4int i)
const G4String & GetPathName() const
G4int GetTreeEntry() const
G4UIcommandTree * GetTree(G4int i)
const G4String & GetCommandName() const
void InsertCharacter(char cc)
G4String GetFirstMatchedString(const G4String &str1, const G4String &str2) const
G4String GetCommandLineString(const char *msg=nullptr) override
G4String RestoreHistory(G4int index)
void ResetTerminal() override
void SetTermToInputMode()
void ListMatchedCommand()
G4bool IsCursorLast() const
void InitializeCommandLine()
G4UItcsh(const G4String &prompt="%s> ", G4int maxhist=100)
void MakePrompt(const char *msg=nullptr) override
std::vector< G4String > commandHistory
void StoreHistory(G4String aCommand)
void BackspaceCharacter()
G4int relativeHistoryIndex
virtual void ListCommand(const G4String &input, const G4String &candidate="") const
G4UIcommandTree * GetCommandTree(const G4String &dir) const
G4String currentCommandDir
G4String GetCommandPathTail(const G4String &apath) const
G4String GetAbsCommandDirPath(const G4String &apath) const