Geant4-11
Public Member Functions | Protected Member Functions | Protected Attributes
G4UItcsh Class Reference

#include <G4UItcsh.hh>

Inheritance diagram for G4UItcsh:
G4VUIshell

Public Member Functions

 G4UItcsh (const G4String &prompt="%s> ", G4int maxhist=100)
 
virtual G4String GetCommandLineString (const char *msg=0)
 
virtual void ListCommand (const G4String &input, const G4String &candidate="") const
 
virtual void ResetTerminal ()
 
void SetCurrentDirectory (const G4String &ccd)
 
void SetLsColor (TermColorIndex dirColor, TermColorIndex cmdColor)
 
void SetNColumn (G4int ncol)
 
void SetPrompt (const G4String &prompt)
 
virtual void ShowCurrentDirectory () const
 
 ~G4UItcsh ()
 

Protected Member Functions

void BackspaceCharacter ()
 
void BackwardCursor ()
 
void ClearAfterCursor ()
 
void ClearLine ()
 
void ClearScreen ()
 
void CompleteCommand ()
 
void DeleteCharacter ()
 
void ForwardCursor ()
 
G4String GetAbsCommandDirPath (const G4String &apath) const
 
G4String GetCommandPathTail (const G4String &apath) const
 
G4UIcommandTreeGetCommandTree (const G4String &dir) const
 
G4String GetFirstMatchedString (const G4String &str1, const G4String &str2) const
 
void InitializeCommandLine ()
 
void InsertCharacter (char cc)
 
G4bool IsCursorLast () const
 
void ListMatchedCommand ()
 
virtual void MakePrompt (const char *msg=0)
 
void MoveCursorEnd ()
 
void MoveCursorTop ()
 
void NextCommand ()
 
void PreviousCommand ()
 
G4String ReadLine ()
 
G4String RestoreHistory (G4int index)
 
void RestoreTerm ()
 
void SetTermToInputMode ()
 
void StoreHistory (G4String aCommand)
 

Protected Attributes

G4String clearString
 
TermColorIndex commandColor
 
std::vector< G4StringcommandHistory
 
G4String commandLine
 
G4String commandLineBuf
 
G4String currentCommandDir
 
G4int currentHistoryNo
 
G4int cursorPosition
 
TermColorIndex directoryColor
 
G4bool lsColorFlag
 
G4int maxHistory
 
G4int nColumn
 
G4String promptSetting
 
G4String promptString
 
G4int relativeHistoryIndex
 
termios tios
 

Detailed Description

Definition at line 70 of file G4UItcsh.hh.

Constructor & Destructor Documentation

◆ G4UItcsh()

G4UItcsh::G4UItcsh ( const G4String prompt = "%s> ",
G4int  maxhist = 100 
)

Definition at line 65 of file G4UItcsh.cc.

66 : G4VUIshell(prompt),
68 commandHistory(maxhist), maxHistory(maxhist),
71{
72 // get current terminal mode
73 tcgetattr(0, &tios);
74
75 // read a shell history file
76 const char* path = std::getenv("HOME");
77 if( path == NULL ) return;
78
79 G4String homedir= path;
81
82 std::ifstream histfile;
83 enum { BUFSIZE= 1024 }; char linebuf[BUFSIZE];
84
85 histfile.open(fname, std::ios::in);
86 while (histfile.good()) {
87 if(histfile.eof()) break;
88
89 histfile.getline(linebuf, BUFSIZE);
90 G4String aline= G4StrUtil::strip_copy(linebuf);
91 if(aline.size() != 0) StoreHistory(linebuf);
92 }
93 histfile.close();
94}
static const G4String historyFileName
Definition: G4UItcsh.cc:62
G4int cursorPosition
Definition: G4UItcsh.hh:75
G4int currentHistoryNo
Definition: G4UItcsh.hh:107
termios tios
Definition: G4UItcsh.hh:115
G4String commandLine
Definition: G4UItcsh.hh:74
std::vector< G4String > commandHistory
Definition: G4UItcsh.hh:105
G4int maxHistory
Definition: G4UItcsh.hh:106
void StoreHistory(G4String aCommand)
Definition: G4UItcsh.cc:711
G4int relativeHistoryIndex
Definition: G4UItcsh.hh:108
G4VUIshell(const G4String &prompt="> ")
Definition: G4VUIshell.cc:45
#define BUFSIZE
Definition: liblist.c:40
G4String strip_copy(G4String str, char c=' ')
Return copy of string with leading and trailing characters removed.
string fname
Definition: test.py:308

References BUFSIZE, test::fname, historyFileName, StoreHistory(), G4StrUtil::strip_copy(), and tios.

◆ ~G4UItcsh()

G4UItcsh::~G4UItcsh ( )

Definition at line 97 of file G4UItcsh.cc.

99{
100 // store a shell history
101 const char* path = std::getenv("HOME");
102 if( path == NULL ) return;
103
104 G4String homedir= path;
105 G4String fname= homedir + historyFileName;
106
107 std::ofstream histfile;
108 histfile.open(fname, std::ios::out);
109
110 G4int n0hist= 1;
112
113 for (G4int i=n0hist; i<= currentHistoryNo; i++) {
114 histfile << RestoreHistory(i) << G4endl;
115 }
116
117 histfile.close();
118}
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
G4String RestoreHistory(G4int index)
Definition: G4UItcsh.cc:722

References currentHistoryNo, test::fname, G4endl, historyFileName, maxHistory, and RestoreHistory().

Member Function Documentation

◆ BackspaceCharacter()

void G4UItcsh::BackspaceCharacter ( )
protected

Definition at line 218 of file G4UItcsh.cc.

220{
221 if(cursorPosition==1) return;
222
223 // display...
224 if(IsCursorLast()) {
225 G4cout << AsciiBS << ' ' << AsciiBS << std::flush;
226 } else {
227 G4cout << AsciiBS;
228 size_t i;
229 for(i=cursorPosition-2; i< commandLine.length()-1 ;i++){
230 G4cout << commandLine[i+1];
231 }
232 G4cout << ' ';
233 for(i=cursorPosition-2; i< commandLine.length() ;i++){
234 G4cout << AsciiBS;
235 }
236 G4cout << std::flush;
237 }
238
239 // command line string...
240 commandLine.erase(cursorPosition-2, 1);
241
243}
static const char AsciiBS
Definition: G4UItcsh.cc:55
G4GLOB_DLL std::ostream G4cout
G4bool IsCursorLast() const
Definition: G4UItcsh.hh:133

References AsciiBS, commandLine, cursorPosition, G4cout, and IsCursorLast().

Referenced by ReadLine().

◆ BackwardCursor()

void G4UItcsh::BackwardCursor ( )
protected

Definition at line 324 of file G4UItcsh.cc.

326{
327 if(cursorPosition==1) return;
328
330 G4cout << AsciiBS << std::flush;
331}

References AsciiBS, cursorPosition, and G4cout.

Referenced by ReadLine().

◆ ClearAfterCursor()

void G4UItcsh::ClearAfterCursor ( )
protected

Definition at line 283 of file G4UItcsh.cc.

285{
286 if(IsCursorLast()) return;
287
288 // display...
289 G4int i;
290 for(i=cursorPosition; i<=G4int(commandLine.length()); i++) G4cout << ' ';
291 for(i=commandLine.length(); i>=cursorPosition; i--) G4cout << AsciiBS;
292 G4cout << std::flush;
293
294 // command line string...
296 commandLine.length()-cursorPosition+1);
297}

References AsciiBS, commandLine, cursorPosition, G4cout, and IsCursorLast().

Referenced by ReadLine().

◆ ClearLine()

void G4UItcsh::ClearLine ( )
protected

Definition at line 267 of file G4UItcsh.cc.

269{
270 // display...
271 G4int i;
272 for(i= cursorPosition; i>=2; i--) G4cout << AsciiBS;
273 for(i=1; i<=G4int(commandLine.length()); i++) G4cout << ' ';
274 for(i=1; i<=G4int(commandLine.length()); i++) G4cout << AsciiBS;
275 G4cout << std::flush;
276
277 // command line string...
278 commandLine.erase();
280}

References AsciiBS, commandLine, cursorPosition, and G4cout.

Referenced by NextCommand(), and PreviousCommand().

◆ ClearScreen()

void G4UItcsh::ClearScreen ( )
protected

Definition at line 300 of file G4UItcsh.cc.

302{
303 if(! clearString.empty() ) {
305
306 G4cout << promptString << commandLine << std::flush;
307 // reset cursur position
308 for(G4int i=commandLine.length()+1; i>=cursorPosition+1; i--)
309 G4cout << AsciiBS << std::flush;
310 }
311}
G4String clearString
Definition: G4UItcsh.hh:116
G4String promptString
Definition: G4VUIshell.hh:61

References AsciiBS, clearString, commandLine, cursorPosition, G4cout, and G4VUIshell::promptString.

◆ CompleteCommand()

void G4UItcsh::CompleteCommand ( )
protected

Definition at line 436 of file G4UItcsh.cc.

438{
439 // inputting string
441
442 // target token is last token
443 auto jhead= input.rfind(' ');
444 if(jhead != G4String::npos) {
445 input.erase(0, jhead);
446 G4StrUtil::lstrip(input);
447 }
448
449 // tail string
450 size_t thead = input.find_last_of('/');
451 G4String strtail = input;
452 if (thead != G4String::npos) strtail = input.substr(thead+1, input.size()-thead-1);
453
454 // command tree of "user specified directory"
456 G4String vcmd;
457
458 G4int len= input.length();
459 if(!input.empty()) {
460 G4int indx= -1;
461 for(G4int i=len-1; i>=0; i--) {
462 if(input[i]=='/') {
463 indx= i;
464 break;
465 }
466 }
467 // get abs. path
468 if(indx != -1) vpath= GetAbsCommandDirPath(input.substr(0,indx+1));
469 if(!(indx==0 && len==1)) vcmd= input.substr(indx+1,len-indx-1); // care for "/"
470 }
471
472 G4UIcommandTree* atree= GetCommandTree(vpath); // get command tree
473 if(atree == NULL) return;
474
475 // list matched directories/commands
476 G4String stream, strtmp;
477 G4String inputpath= vpath+vcmd;
478 G4int nMatch= 0;
479
480 int Ndir= atree-> GetTreeEntry();
481 int Ncmd= atree-> GetCommandEntry();
482
483 // directory ...
484 for(G4int idir=1; idir<=Ndir; idir++) {
485 G4String fpdir= atree-> GetTree(idir)-> GetPathName();
486 // matching test
487 if( fpdir.find(inputpath, 0) == 0) {
488 if(nMatch==0) {
489 stream= GetCommandPathTail(fpdir);
490 } else {
491 strtmp= GetCommandPathTail(fpdir);
492 stream= GetFirstMatchedString(stream, strtmp);
493 }
494 nMatch++;
495 }
496 }
497
498 // command ...
499 for(G4int icmd=1; icmd<=Ncmd; icmd++){
500 G4String fpcmd= atree-> GetPathName() +
501 atree-> GetCommand(icmd) -> GetCommandName();
502 // matching test
503 if( fpcmd.find(inputpath, 0) ==0) {
504 if(nMatch==0) {
505 stream= GetCommandPathTail(fpcmd) + " ";
506 } else {
507 strtmp= GetCommandPathTail(fpcmd) + " ";
508 stream= GetFirstMatchedString(stream, strtmp);
509 }
510 nMatch++;
511 }
512 }
513
514 // display...
515 input= commandLine;
516 // target token is last token
517 jhead= input.rfind(' ');
518 if(jhead == G4String::npos) jhead=0;
519 else jhead++;
520
521 G4int jt = jhead;
522
523 G4String dspstr;
524 G4int i;
525 for(i=jt; i<=G4int(input.length())-1; i++) dspstr+= AsciiBS;
526 for(i=jt; i<=G4int(input.length())-1; i++) dspstr+= ' ';
527 for(i=jt; i<=G4int(input.length())-1; i++) dspstr+= AsciiBS;
528
529 dspstr+= (vpath + stream);
530 if (nMatch == 0) dspstr+= strtail;
531 G4cout << dspstr << std::flush;
532
533 // command line string
534 input.erase(jt);
535 input+= (vpath + stream);
536 if (nMatch==0) input+= strtail;
537
538 commandLine= input;
539 cursorPosition= commandLine.length()+1;
540}
G4String GetFirstMatchedString(const G4String &str1, const G4String &str2) const
Definition: G4UItcsh.cc:686
G4UIcommandTree * GetCommandTree(const G4String &dir) const
Definition: G4VUIshell.cc:117
G4String currentCommandDir
Definition: G4VUIshell.hh:72
G4String GetCommandPathTail(const G4String &apath) const
Definition: G4VUIshell.cc:191
G4String GetAbsCommandDirPath(const G4String &apath) const
Definition: G4VUIshell.cc:145
void lstrip(G4String &str, char c=' ')
Remove leading characters from string.
G4String lstrip_copy(G4String str, char c=' ')
Return copy of string with leading characters removed.

References AsciiBS, commandLine, G4VUIshell::currentCommandDir, cursorPosition, G4cout, G4VUIshell::GetAbsCommandDirPath(), G4VUIshell::GetCommandPathTail(), G4VUIshell::GetCommandTree(), GetFirstMatchedString(), G4StrUtil::lstrip(), and G4StrUtil::lstrip_copy().

Referenced by ReadLine().

◆ DeleteCharacter()

void G4UItcsh::DeleteCharacter ( )
protected

Definition at line 246 of file G4UItcsh.cc.

248{
249 if(IsCursorLast()) return;
250
251 // display...
252 size_t i;
253 for(i=cursorPosition-1; i< commandLine.length()-1 ;i++){
254 G4cout << commandLine[i+1];
255 }
256 G4cout << ' ';
257 for(i=cursorPosition-1; i< commandLine.length() ;i++){
258 G4cout << AsciiBS;
259 }
260 G4cout << std::flush;
261
262 // command lin string...
263 commandLine.erase(cursorPosition-1, 1);
264}

References AsciiBS, commandLine, cursorPosition, G4cout, and IsCursorLast().

Referenced by ReadLine().

◆ ForwardCursor()

void G4UItcsh::ForwardCursor ( )
protected

Definition at line 314 of file G4UItcsh.cc.

316{
317 if(IsCursorLast()) return;
318
319 G4cout << commandLine[(size_t)(cursorPosition-1)] << std::flush;
321}

References commandLine, cursorPosition, G4cout, and IsCursorLast().

Referenced by ReadLine().

◆ GetAbsCommandDirPath()

G4String G4VUIshell::GetAbsCommandDirPath ( const G4String apath) const
protectedinherited

Definition at line 145 of file G4VUIshell.cc.

147{
148 if(apath.empty()) return apath; // null string
149
150 // if "apath" does not start with "/",
151 // then it is treared as relative path
152 G4String bpath= apath;
153 if(apath[(size_t)0] != '/') bpath= currentCommandDir + apath;
154
155 // parsing...
156 G4String absPath= "/";
157 for(G4int indx=1; indx<=G4int(bpath.length())-1; ) {
158 G4int jslash= bpath.find("/", indx); // search index begin with "/"
159 if(indx == jslash) { // skip first '///'
160 indx++;
161 continue;
162 }
163 if(jslash != G4int(G4String::npos)) {
164 if(bpath.substr(indx,jslash-indx) == ".."){ // directory up
165 if(absPath == "/") {
166 indx = jslash+1;
167 continue;
168 }
169 if(absPath.length() >= 2) {
170 absPath.erase(absPath.length()-1); // remove last "/"
171 auto jpre= absPath.rfind('/');
172 if(jpre != G4String::npos) absPath.erase(jpre+1);
173 }
174 } else if(bpath.substr(indx,jslash-indx) == "."){ // nothing to do
175 } else { // add
176 if( !(jslash==indx && bpath[indx]=='/') ) // truncate "////"
177 absPath+= bpath.substr(indx, jslash-indx+1);
178 // better to be check directory existence. (it costs!)
179 }
180 indx= jslash+1;
181 } else { // directory ONLY (ignore non-"/" terminated string)
182 break;
183 }
184 }
185
186 return absPath;
187}

Referenced by CompleteCommand(), and ListMatchedCommand().

◆ GetCommandLineString()

G4String G4UItcsh::GetCommandLineString ( const char *  msg = 0)
virtual

Implements G4VUIshell.

Definition at line 646 of file G4UItcsh.cc.

648{
650
651 MakePrompt(msg); // update
653
654 G4cout << promptString << std::flush;
655
656 G4String newCommand= ReadLine(); // read line...
657 // multi-line
658 while( (newCommand.length() > 0) &&
659 ( newCommand[newCommand.length()-1] == '_') ) {
660 newCommand.erase(newCommand.length()-1);
661 G4cout << G4endl;
662 promptString= "? ";
663 G4cout << promptString << std::flush;
664 G4String newLine= ReadLine();
665 newCommand.append(newLine);
666 }
667
668 // update history...
669 G4bool isMeaningfull= FALSE; // check NULL command
670 for (size_t i=0; i<newCommand.length(); i++) {
671 if(newCommand[i] != ' ') {
672 isMeaningfull= TRUE;
673 break;
674 }
675 }
676 if( !newCommand.empty() && isMeaningfull) StoreHistory(newCommand);
677
678 // reset terminal
679 RestoreTerm();
680
681 G4cout << G4endl;
682 return newCommand;
683}
bool G4bool
Definition: G4Types.hh:86
#define TRUE
Definition: Globals.hh:27
#define FALSE
Definition: Globals.hh:23
virtual void MakePrompt(const char *msg=0)
Definition: G4UItcsh.cc:121
void RestoreTerm()
Definition: G4UItcsh.cc:753
G4String ReadLine()
Definition: G4UItcsh.cc:546
void SetTermToInputMode()
Definition: G4UItcsh.cc:737

References FALSE, G4cout, G4endl, MakePrompt(), G4VUIshell::promptString, ReadLine(), relativeHistoryIndex, RestoreTerm(), SetTermToInputMode(), StoreHistory(), and TRUE.

◆ GetCommandPathTail()

G4String G4VUIshell::GetCommandPathTail ( const G4String apath) const
protectedinherited

Definition at line 191 of file G4VUIshell.cc.

193{ // xxx/xxx/zzz -> zzz, trancate /// -> /
194 if(apath.empty()) return apath;
195
196 G4int lstr= apath.length();
197
198 // for trancating "/"
199 G4bool Qsla= FALSE;
200 if(apath[(size_t)(lstr-1)]=='/') Qsla= TRUE;
201
202 // searching last '/' from tail
203 G4int indx= -1;
204 for(G4int i=lstr-1; i>=0; i--) {
205 if(Qsla && apath[(size_t)i]!='/') Qsla= FALSE; // break "/" flag!!
206 if(apath[(size_t)i]=='/' && !Qsla) {
207 indx= i;
208 break;
209 }
210 }
211
212 if(indx==-1) return apath; // not found
213
214 if(indx==0 && lstr==1) { // "/"
215 G4String nullStr;
216 return nullStr;
217 } else {
218 //G4String newPath= apath(indx+1,lstr-indx-1);
219 G4String newPath= apath;
220 newPath= newPath.substr(indx+1,lstr-indx-1);
221 return newPath;
222 }
223}

References FALSE, and TRUE.

Referenced by CompleteCommand().

◆ GetCommandTree()

G4UIcommandTree * G4VUIshell::GetCommandTree ( const G4String dir) const
protectedinherited

Definition at line 117 of file G4VUIshell.cc.

119{
121
122 G4UIcommandTree* cmdTree= UI-> GetTree(); // root tree
123
125
126 // parsing absolute path ...
127 if(absPath.length()==0) return NULL;
128 if(absPath[absPath.length()-1] != '/') return NULL; // error??
129 if(absPath=="/") return cmdTree;
130
131 for(G4int indx=1; indx<G4int(absPath.length())-1; ) {
132 G4int jslash= absPath.find("/", indx); // search index begin with "/"
133 if(jslash != G4int(G4String::npos)) {
134 if(cmdTree != NULL)
135 cmdTree= cmdTree-> GetTree(G4String(absPath.substr(0,jslash+1)));
136 }
137 indx= jslash+1;
138 }
139
140 if(cmdTree == NULL) return NULL;
141 else return cmdTree;
142}
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77

References G4UImanager::GetUIpointer(), and G4StrUtil::strip_copy().

Referenced by CompleteCommand().

◆ GetFirstMatchedString()

G4String G4UItcsh::GetFirstMatchedString ( const G4String str1,
const G4String str2 
) const
protected

Definition at line 686 of file G4UItcsh.cc.

689{
690 int nlen1= str1.length();
691 int nlen2= str2.length();
692
693 int nmin = nlen1<nlen2 ? nlen1 : nlen2;
694
695 G4String strMatched;
696 for(size_t i=0; G4int(i)<nmin; i++){
697 if(str1[i]==str2[i]) {
698 strMatched+= str1[i];
699 } else {
700 break;
701 }
702 }
703
704 return strMatched;
705}

Referenced by CompleteCommand().

◆ InitializeCommandLine()

void G4UItcsh::InitializeCommandLine ( )
protected

Definition at line 186 of file G4UItcsh.cc.

188{
189 commandLine= "";
191}

References commandLine, and cursorPosition.

Referenced by ReadLine().

◆ InsertCharacter()

void G4UItcsh::InsertCharacter ( char  cc)
protected

Definition at line 194 of file G4UItcsh.cc.

196{
197 if( ! (cc >= AsciiPrintableMin && isprint(cc)) ) return;
198
199 // display...
200 G4cout << cc;
201 size_t i;
202 for(i=cursorPosition-1; i<commandLine.length() ;i++)
203 G4cout << commandLine[i];
204 for(i=cursorPosition-1; i<commandLine.length() ;i++)
205 G4cout << AsciiBS;
206 G4cout << std::flush;
207
208 // command line string...
209 if(IsCursorLast()) { // add
210 commandLine+= cc;
211 } else { // insert
212 commandLine.insert(cursorPosition-1, G4String(1,cc));
213 }
215}
static const int AsciiPrintableMin
Definition: G4UItcsh.cc:59

References AsciiBS, AsciiPrintableMin, commandLine, cursorPosition, G4cout, and IsCursorLast().

Referenced by ReadLine().

◆ IsCursorLast()

G4bool G4UItcsh::IsCursorLast ( ) const
inlineprotected

Definition at line 133 of file G4UItcsh.hh.

134{
135 if(cursorPosition == G4int(commandLine.length()+1)) return TRUE;
136 else return FALSE;
137}

References commandLine, cursorPosition, FALSE, and TRUE.

Referenced by BackspaceCharacter(), ClearAfterCursor(), DeleteCharacter(), ForwardCursor(), InsertCharacter(), and ReadLine().

◆ ListCommand()

void G4VUIshell::ListCommand ( const G4String input,
const G4String candidate = "" 
) const
virtualinherited

Definition at line 229 of file G4VUIshell.cc.

232{
233 // specified directpry
234 G4String input = G4StrUtil::strip_copy(dir);
235
236 // command tree of "user specified directory"
238 G4String vcmd;
239
240 G4int len= input.length();
241 if(! input.empty()) {
242 G4int indx= -1;
243 for(G4int i=len-1; i>=0; i--) { // search last '/'
244 if(input[(size_t)i]=='/') {
245 indx= i;
246 break;
247 }
248 }
249 // get abs. path
250 if(indx != -1) vpath= GetAbsCommandDirPath(input.substr(0,indx+1));
251 if(!(indx==0 && len==1)) vcmd= input.substr(indx+1,len-indx-1); // care for "/"
252 }
253
254 // check "vcmd" is directory?
255 G4String inputpath= vpath+vcmd;
256 if(! vcmd.empty()){
257 G4String tmpstr= inputpath + "/";
258 if(GetCommandTree(tmpstr) != NULL) {
259 vpath= tmpstr;
260 vcmd= "";
261 }
262 }
263
264 // check "vpath" directory exists?
265 G4UIcommandTree* atree= GetCommandTree(vpath);
266 if(atree == NULL) {
267 G4cout << "<" << input << ">: No such directory" << G4endl;
268 return;
269 }
270
271 // list matched directories/commands
272 G4String stream;
273 G4bool isMatch= FALSE;
274
275 G4int Ndir= atree-> GetTreeEntry();
276 G4int Ncmd= atree-> GetCommandEntry();
277 if(Ndir==0 && Ncmd==0) return; // no contents
278
279 // directory ...
280 for(G4int idir=1; idir<=Ndir; idir++) {
281 if(idir==1 && lsColorFlag) stream+= TermColorString[directoryColor];
282 G4String fpdir= atree-> GetTree(idir)-> GetPathName();
283 // matching test
284 if(candidate.empty()) { // list all
285 if(vcmd=="" || fpdir==inputpath) {
286 stream+= GetCommandPathTail(fpdir); stream+= " ";
287 isMatch= TRUE;
288 }
289 } else { // list only matched with candidate
290 if( fpdir.find(candidate, 0) == 0) {
291 stream+= GetCommandPathTail(fpdir); stream+= " ";
292 }
293 }
294 }
295
296 // command ...
297 for(G4int icmd=1; icmd<=Ncmd; icmd++){
298 if(icmd==1 && lsColorFlag) stream+= TermColorString[commandColor];
299 G4String fpcmd= atree-> GetPathName() +
300 atree-> GetCommand(icmd) -> GetCommandName();
301 // matching test
302 if(candidate.empty()) { // list all
303 if(vcmd=="" || fpcmd==inputpath) {
304 stream+= GetCommandPathTail(fpcmd); stream+= "* ";
305 isMatch= TRUE;
306 }
307 } else { // list only matched with candidate
308 if( fpcmd.find(candidate, 0) == 0) {
309 stream+= GetCommandPathTail(fpcmd); stream+= "* ";
310 }
311 }
312 }
313
314 // waring : not matched
315 if(!isMatch && candidate.empty())
316 G4cout << "<" << input
317 << ">: No such directory or command" << std::flush;
318
319 // display
320 G4UIArrayString arrayString(stream);
321 arrayString.Show(nColumn);
322}
static const G4String TermColorString[8]
Definition: G4VUIshell.cc:39
TermColorIndex directoryColor
Definition: G4VUIshell.hh:68
G4int nColumn
Definition: G4VUIshell.hh:63
TermColorIndex commandColor
Definition: G4VUIshell.hh:69
G4bool lsColorFlag
Definition: G4VUIshell.hh:67

References FALSE, G4cout, G4endl, anonymous_namespace{G4ZMQServer.cc}::GetCommandTree(), G4UIArrayString::Show(), G4StrUtil::strip_copy(), TermColorString, and TRUE.

Referenced by ListMatchedCommand().

◆ ListMatchedCommand()

void G4UItcsh::ListMatchedCommand ( )
protected

Definition at line 396 of file G4UItcsh.cc.

398{
399 G4cout << G4endl;
400
401 // input string
403 // target token is last token
404 auto jhead= input.rfind(' ');
405 if(jhead != G4String::npos) {
406 input.erase(0, jhead);
407 G4StrUtil::lstrip(input);
408 }
409
410 // command tree of "user specified directory"
412 G4String vcmd = "";
413
414 if( !input.empty() ) {
415 G4int len= input.length();
416 G4int indx=-1;
417 for(G4int i=len-1; i>=0; i--) {
418 if(input[(size_t)i]=='/') {
419 indx= i;
420 break;
421 }
422 }
423 // get abs. path
424 if(indx != -1) vpath= GetAbsCommandDirPath(input.substr(0,indx+1));
425 if(!(indx==0 && len==1)) vcmd= input.substr(indx+1,len-indx-1); // care for "/"
426 }
427
428 // list matched dirs/commands
429 //G4cout << "@@@ vpath=" << vpath <<":vcmd=" << vcmd << G4endl;
430 ListCommand(vpath, vpath+vcmd);
431
432 G4cout << promptString << commandLine << std::flush;
433}
virtual void ListCommand(const G4String &input, const G4String &candidate="") const
Definition: G4VUIshell.cc:229

References commandLine, G4VUIshell::currentCommandDir, G4cout, G4endl, G4VUIshell::GetAbsCommandDirPath(), G4VUIshell::ListCommand(), G4StrUtil::lstrip(), G4StrUtil::lstrip_copy(), and G4VUIshell::promptString.

Referenced by ReadLine().

◆ MakePrompt()

void G4UItcsh::MakePrompt ( const char *  msg = 0)
protectedvirtual

Reimplemented from G4VUIshell.

Definition at line 121 of file G4UItcsh.cc.

123{
124 if(promptSetting.length()<=1) {
126 return;
127 }
128
129 promptString="";
130 size_t i;
131 for(i=0; i<promptSetting.length()-1; i++){
132 if(promptSetting[i]=='%'){
133 switch (promptSetting[i+1]) {
134 case 's': // current application status
135 {
136 G4String stateStr;
137 if(msg)
138 { stateStr = msg; }
139 else
140 {
142 stateStr= statM-> GetStateString(statM->GetCurrentState());
143 }
144 promptString.append(stateStr);
145 i++;
146 }
147 break;
148 case '/': // current working directory
150 i++;
151 break;
152 case 'h': // history#
153 {
154 std::ostringstream os;
155 os << currentHistoryNo;
156 promptString.append(os.str());
157 i++;
158 }
159 break;
160 default:
161 break;
162 }
163 } else {
165 }
166 }
167
168 // append last chaacter
169 if(i == promptSetting.length()-1)
171}
const G4ApplicationState & GetCurrentState() const
static G4StateManager * GetStateManager()
G4String promptSetting
Definition: G4VUIshell.hh:60

References G4VUIshell::currentCommandDir, currentHistoryNo, G4StateManager::GetCurrentState(), G4StateManager::GetStateManager(), G4VUIshell::promptSetting, and G4VUIshell::promptString.

Referenced by GetCommandLineString().

◆ MoveCursorEnd()

void G4UItcsh::MoveCursorEnd ( )
protected

Definition at line 345 of file G4UItcsh.cc.

347{
348 for(size_t i=cursorPosition-1; i<commandLine.length(); i++){
349 G4cout << commandLine[i];
350 }
351 G4cout << std::flush;
352 cursorPosition=commandLine.length()+1;
353}

References commandLine, cursorPosition, and G4cout.

Referenced by ReadLine().

◆ MoveCursorTop()

void G4UItcsh::MoveCursorTop ( )
protected

Definition at line 334 of file G4UItcsh.cc.

336{
337 for(G4int i=cursorPosition; i>1; i--){
338 G4cout << AsciiBS;
339 }
340 G4cout << std::flush;
342}

References AsciiBS, cursorPosition, and G4cout.

Referenced by ReadLine().

◆ NextCommand()

void G4UItcsh::NextCommand ( )
protected

◆ PreviousCommand()

void G4UItcsh::PreviousCommand ( )
protected

Definition at line 356 of file G4UItcsh.cc.

358{
359 G4int nhmax= currentHistoryNo-1 >= maxHistory ?
361
362 // retain current input
364
365 if(relativeHistoryIndex>=-nhmax+1 && relativeHistoryIndex<=0) {
366 ClearLine();
369
370 G4cout << commandLine << std::flush;
371 cursorPosition= commandLine.length()+1;
372 }
373}

References ClearLine(), commandLine, commandLineBuf, currentHistoryNo, cursorPosition, G4cout, maxHistory, relativeHistoryIndex, and RestoreHistory().

Referenced by ReadLine().

◆ ReadLine()

G4String G4UItcsh::ReadLine ( )
protected

Definition at line 546 of file G4UItcsh.cc.

548{
550
551 char cc;
552 do{ // input loop
553 G4cin.get(cc);
554
555 // treatment for special character
556 switch(cc){
557 case AsciiCtrA: // ... move cursor to the top
559 break;
560 case AsciiCtrB: // ... backward cursor
562 break;
563 case AsciiCtrD: // ... delete/exit/show matched list
564 if(commandLine.length()!=0 && IsCursorLast()) ListMatchedCommand();
565 else if (commandLine.empty()) {
566 return G4String("exit");
567 } else DeleteCharacter();
568 break;
569 case AsciiCtrE: // ... move cursor to the end
571 break;
572 case AsciiCtrF: // ... forward cursor
574 break;
575 case AsciiCtrK: // ... clear after the cursor
577 break;
578 case AsciiCtrL: // ... clear screen
579 // ClearScreen();
580 break;
581 case AsciiCtrN: // ... next command
582 NextCommand();
583 break;
584 case AsciiCtrP: // ... previous command
586 break;
587 case AsciiTAB: // ... command completion
588 if( (!commandLine.empty()) && IsCursorLast()) CompleteCommand();
589 break;
590 case AsciiDEL: // ... backspace
592 break;
593 case AsciiBS: // ... backspace
595 break;
596 case AsciiCtrC: // ... kill prompt
597 break;
598 case AsciiCtrQ: // ... restarts suspeded output
599 break;
600 case AsciiCtrS: // ... suspend output
601 break;
602 case AsciiCtrZ: // ... suspend
603 break;
604 default:
605 break;
606 }
607
608 // treatment for ESC. character
609 if( cc == AsciiESC) { // ESC
610 G4cin.get(cc);
611 if (cc == '[' || cc == 'O') { // care for another termcap, such as konsole
612 G4cin.get(cc);
613 switch(cc) {
614 case 'A': // [UP]
615 cc = 'P' - '@';
616 PreviousCommand(); // ... show previous commad
617 break;
618 case 'B': // [DOWN]
619 cc = 'N' - '@';
620 NextCommand(); // ... show next commad
621 break;
622 case 'C': // [RIGHT]
623 cc = 'F' - '@';
624 ForwardCursor(); // ... forward cursor
625 break;
626 case 'D': // [LEFT]
627 cc = 'B' - '@';
628 BackwardCursor(); // ... backward cursor
629 break;
630 default: // who knows !?
631 cc = 0;
632 break;
633 }
634 }
635 }
636
637 // insert character to command line and display
638 InsertCharacter(cc);
639
640 } while( cc != '\n');
641
642 return commandLine;
643}
static const char AsciiCtrN
Definition: G4UItcsh.cc:49
static const char AsciiCtrS
Definition: G4UItcsh.cc:52
static const char AsciiCtrL
Definition: G4UItcsh.cc:48
static const char AsciiCtrE
Definition: G4UItcsh.cc:45
static const char AsciiTAB
Definition: G4UItcsh.cc:54
static const char AsciiCtrD
Definition: G4UItcsh.cc:44
static const char AsciiCtrK
Definition: G4UItcsh.cc:47
static const char AsciiCtrP
Definition: G4UItcsh.cc:50
static const char AsciiCtrA
Definition: G4UItcsh.cc:41
static const char AsciiDEL
Definition: G4UItcsh.cc:56
static const char AsciiCtrZ
Definition: G4UItcsh.cc:53
static const char AsciiCtrB
Definition: G4UItcsh.cc:42
static const char AsciiCtrC
Definition: G4UItcsh.cc:43
static const char AsciiCtrQ
Definition: G4UItcsh.cc:51
static const char AsciiCtrF
Definition: G4UItcsh.cc:46
static const char AsciiESC
Definition: G4UItcsh.cc:57
#define G4cin
Definition: G4ios.hh:56
void MoveCursorTop()
Definition: G4UItcsh.cc:334
void InsertCharacter(char cc)
Definition: G4UItcsh.cc:194
void PreviousCommand()
Definition: G4UItcsh.cc:356
void CompleteCommand()
Definition: G4UItcsh.cc:436
void NextCommand()
Definition: G4UItcsh.cc:376
void ForwardCursor()
Definition: G4UItcsh.cc:314
void MoveCursorEnd()
Definition: G4UItcsh.cc:345
void DeleteCharacter()
Definition: G4UItcsh.cc:246
void ListMatchedCommand()
Definition: G4UItcsh.cc:396
void InitializeCommandLine()
Definition: G4UItcsh.cc:186
void BackwardCursor()
Definition: G4UItcsh.cc:324
void ClearAfterCursor()
Definition: G4UItcsh.cc:283
void BackspaceCharacter()
Definition: G4UItcsh.cc:218

References AsciiBS, AsciiCtrA, AsciiCtrB, AsciiCtrC, AsciiCtrD, AsciiCtrE, AsciiCtrF, AsciiCtrK, AsciiCtrL, AsciiCtrN, AsciiCtrP, AsciiCtrQ, AsciiCtrS, AsciiCtrZ, AsciiDEL, AsciiESC, AsciiTAB, BackspaceCharacter(), BackwardCursor(), ClearAfterCursor(), commandLine, CompleteCommand(), DeleteCharacter(), ForwardCursor(), G4cin, InitializeCommandLine(), InsertCharacter(), IsCursorLast(), ListMatchedCommand(), MoveCursorEnd(), MoveCursorTop(), NextCommand(), and PreviousCommand().

Referenced by GetCommandLineString().

◆ ResetTerminal()

void G4UItcsh::ResetTerminal ( )
virtual

Reimplemented from G4VUIshell.

Definition at line 175 of file G4UItcsh.cc.

177{
178 RestoreTerm();
179}

References RestoreTerm().

◆ RestoreHistory()

G4String G4UItcsh::RestoreHistory ( G4int  index)
protected

Definition at line 722 of file G4UItcsh.cc.

724{
725 if(histNo>= currentHistoryNo) return "";
726
727 G4int index= histNo%maxHistory;
728 if(index==0) index= maxHistory;
729
730 return commandHistory[index-1]; // 0-offset
731}

References commandHistory, currentHistoryNo, and maxHistory.

Referenced by NextCommand(), PreviousCommand(), and ~G4UItcsh().

◆ RestoreTerm()

void G4UItcsh::RestoreTerm ( )
protected

Definition at line 753 of file G4UItcsh.cc.

755{
756 tcsetattr(0, TCSAFLUSH, &tios);
757}

References tios.

Referenced by GetCommandLineString(), and ResetTerminal().

◆ SetCurrentDirectory()

void G4VUIshell::SetCurrentDirectory ( const G4String ccd)
inlineinherited

Definition at line 114 of file G4VUIshell.hh.

115{
117}

References G4VUIshell::currentCommandDir.

◆ SetLsColor()

void G4UItcsh::SetLsColor ( TermColorIndex  dirColor,
TermColorIndex  cmdColor 
)
inlinevirtual

Reimplemented from G4VUIshell.

Definition at line 139 of file G4UItcsh.hh.

141{
143 directoryColor= dirColor;
144 commandColor= cmdColor;
145}

References G4VUIshell::commandColor, G4VUIshell::directoryColor, G4VUIshell::lsColorFlag, and TRUE.

◆ SetNColumn()

void G4VUIshell::SetNColumn ( G4int  ncol)
inlineinherited

Definition at line 104 of file G4VUIshell.hh.

105{
106 nColumn= ncol;
107}

References G4VUIshell::nColumn.

◆ SetPrompt()

void G4VUIshell::SetPrompt ( const G4String prompt)
inlineinherited

Definition at line 109 of file G4VUIshell.hh.

110{
111 promptSetting= prompt;
112}

References G4VUIshell::promptSetting.

◆ SetTermToInputMode()

void G4UItcsh::SetTermToInputMode ( )
protected

Definition at line 737 of file G4UItcsh.cc.

739{
740 termios tiosbuf= tios;
741
742 tiosbuf.c_iflag &= ~(BRKINT | ISTRIP);
743 tiosbuf.c_iflag |= (IGNBRK | IGNPAR);
744 tiosbuf.c_lflag &= ~(ICANON | IEXTEN | ECHO);
745 tiosbuf.c_cc[VMIN] = 1;
746 tiosbuf.c_cc[VTIME] = 0;
747
748 tcsetattr(0, TCSAFLUSH, &tiosbuf);
749}

References tios.

Referenced by GetCommandLineString().

◆ ShowCurrentDirectory()

void G4VUIshell::ShowCurrentDirectory ( ) const
inlinevirtualinherited

Definition at line 123 of file G4VUIshell.hh.

124{
126}

References G4VUIshell::currentCommandDir, G4cout, and G4endl.

◆ StoreHistory()

void G4UItcsh::StoreHistory ( G4String  aCommand)
protected

Definition at line 711 of file G4UItcsh.cc.

713{
715 if(i==0) i=maxHistory;
716
717 commandHistory[i-1]= aCommand; // 0-offset
719}

References commandHistory, currentHistoryNo, and maxHistory.

Referenced by G4UItcsh(), and GetCommandLineString().

Field Documentation

◆ clearString

G4String G4UItcsh::clearString
protected

Definition at line 116 of file G4UItcsh.hh.

Referenced by ClearScreen().

◆ commandColor

TermColorIndex G4VUIshell::commandColor
protectedinherited

Definition at line 69 of file G4VUIshell.hh.

Referenced by SetLsColor().

◆ commandHistory

std::vector<G4String> G4UItcsh::commandHistory
protected

Definition at line 105 of file G4UItcsh.hh.

Referenced by RestoreHistory(), and StoreHistory().

◆ commandLine

G4String G4UItcsh::commandLine
protected

◆ commandLineBuf

G4String G4UItcsh::commandLineBuf
protected

Definition at line 76 of file G4UItcsh.hh.

Referenced by NextCommand(), and PreviousCommand().

◆ currentCommandDir

G4String G4VUIshell::currentCommandDir
protectedinherited

◆ currentHistoryNo

G4int G4UItcsh::currentHistoryNo
protected

◆ cursorPosition

G4int G4UItcsh::cursorPosition
protected

◆ directoryColor

TermColorIndex G4VUIshell::directoryColor
protectedinherited

Definition at line 68 of file G4VUIshell.hh.

Referenced by SetLsColor().

◆ lsColorFlag

G4bool G4VUIshell::lsColorFlag
protectedinherited

Definition at line 67 of file G4VUIshell.hh.

Referenced by SetLsColor().

◆ maxHistory

G4int G4UItcsh::maxHistory
protected

Definition at line 106 of file G4UItcsh.hh.

Referenced by NextCommand(), PreviousCommand(), RestoreHistory(), StoreHistory(), and ~G4UItcsh().

◆ nColumn

G4int G4VUIshell::nColumn
protectedinherited

Definition at line 63 of file G4VUIshell.hh.

Referenced by G4VUIshell::SetNColumn().

◆ promptSetting

G4String G4VUIshell::promptSetting
protectedinherited

Definition at line 60 of file G4VUIshell.hh.

Referenced by MakePrompt(), G4VUIshell::MakePrompt(), and G4VUIshell::SetPrompt().

◆ promptString

G4String G4VUIshell::promptString
protectedinherited

◆ relativeHistoryIndex

G4int G4UItcsh::relativeHistoryIndex
protected

Definition at line 108 of file G4UItcsh.hh.

Referenced by GetCommandLineString(), NextCommand(), and PreviousCommand().

◆ tios

termios G4UItcsh::tios
protected

Definition at line 115 of file G4UItcsh.hh.

Referenced by G4UItcsh(), RestoreTerm(), and SetTermToInputMode().


The documentation for this class was generated from the following files: