| [476] | 1 | //
|
|---|
| 2 | // ********************************************************************
|
|---|
| 3 | // * License and Disclaimer *
|
|---|
| 4 | // * *
|
|---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of *
|
|---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and *
|
|---|
| 7 | // * conditions of the Geant4 Software License, included in the file *
|
|---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These *
|
|---|
| 9 | // * include a list of copyright holders. *
|
|---|
| 10 | // * *
|
|---|
| 11 | // * Neither the authors of this software system, nor their employing *
|
|---|
| 12 | // * institutes,nor the agencies providing financial support for this *
|
|---|
| 13 | // * work make any representation or warranty, express or implied, *
|
|---|
| 14 | // * regarding this software system or assume any liability for its *
|
|---|
| 15 | // * use. Please see the license in the file LICENSE and URL above *
|
|---|
| 16 | // * for the full disclaimer and the limitation of liability. *
|
|---|
| 17 | // * *
|
|---|
| 18 | // * This code implementation is the result of the scientific and *
|
|---|
| 19 | // * technical work of the GEANT4 collaboration. *
|
|---|
| 20 | // * By using, copying, modifying or distributing the software (or *
|
|---|
| 21 | // * any work based on the software) you agree to acknowledge its *
|
|---|
| 22 | // * use in resulting scientific publications, and indicate your *
|
|---|
| 23 | // * acceptance of all terms of the Geant4 Software license. *
|
|---|
| 24 | // ********************************************************************
|
|---|
| 25 | //
|
|---|
| 26 | //
|
|---|
| [593] | 27 | // $Id: G4UItcsh.hh,v 1.8 2007/06/14 05:44:58 kmura Exp $
|
|---|
| [1337] | 28 | // GEANT4 tag $Name: geant4-09-04-beta-01 $
|
|---|
| [476] | 29 | //
|
|---|
| 30 |
|
|---|
| 31 | #ifndef G4UItcsh_h
|
|---|
| 32 | #define G4UItcsh_h 1
|
|---|
| 33 |
|
|---|
| 34 | #ifndef WIN32
|
|---|
| 35 |
|
|---|
| 36 | #include <termios.h>
|
|---|
| 37 | #include <vector>
|
|---|
| 38 | #include "G4VUIshell.hh"
|
|---|
| 39 | #include "G4UIcommand.hh"
|
|---|
| 40 | #include "G4UIcommandTree.hh"
|
|---|
| 41 |
|
|---|
| [593] | 42 | // ====================================================================
|
|---|
| [476] | 43 | // Description:
|
|---|
| 44 | // This class gives tcsh-like shell.
|
|---|
| 45 | //
|
|---|
| 46 | // If your terminal supports color code, colored strings are available
|
|---|
| 47 | // in ListCommand(). For activating color support,
|
|---|
| 48 | // e.g.
|
|---|
| 49 | // tcsh-> SetLsColor(GREEN, CYAN); // (dir, command) color
|
|---|
| 50 | //
|
|---|
| 51 | // [key binding]
|
|---|
| 52 | // ^A ... move cursor to the top
|
|---|
| 53 | // ^B ... backward cursor ([LEFT])
|
|---|
| 54 | // ^D ... delete/exit/show matched list
|
|---|
| 55 | // ^E ... move cursor to the end
|
|---|
| 56 | // ^F ... forward cursor ([RIGHT])
|
|---|
| 57 | // ^K ... clear after the cursor
|
|---|
| 58 | // ^L ... clear screen (not implemented)
|
|---|
| 59 | // ^N ... next command ([DOWN])
|
|---|
| 60 | // ^P ... previous command ([UP])
|
|---|
| 61 | // TAB... command completion
|
|---|
| 62 | // DEL... backspace
|
|---|
| 63 | // BS ... backspace
|
|---|
| 64 | //
|
|---|
| 65 | // [prompt string substitution]
|
|---|
| 66 | // %s ... current application status
|
|---|
| 67 | // %/ ... current working directory
|
|---|
| 68 | // %h ... history# (different from G4 history#)
|
|---|
| 69 | //
|
|---|
| [593] | 70 | // ====================================================================
|
|---|
| [476] | 71 |
|
|---|
| 72 | class G4UItcsh : public G4VUIshell {
|
|---|
| 73 | protected:
|
|---|
| 74 | virtual void MakePrompt(const char* msg=0);
|
|---|
| 75 |
|
|---|
| 76 | G4String commandLine; // command line string;
|
|---|
| 77 | G4int cursorPosition; // cursor position
|
|---|
| 78 | G4String commandLineBuf; // temp. command line;
|
|---|
| 79 | G4bool IsCursorLast() const;
|
|---|
| 80 | // Is cursor position at the last of command line ?
|
|---|
| 81 |
|
|---|
| 82 | void InitializeCommandLine();
|
|---|
| 83 | G4String ReadLine();
|
|---|
| 84 | void InsertCharacter(char cc); // insert character
|
|---|
| 85 | void BackspaceCharacter(); // backspace character
|
|---|
| 86 | void DeleteCharacter(); // delete character
|
|---|
| 87 | void ClearLine(); // clear command line
|
|---|
| 88 | void ClearAfterCursor(); // clear after the cursor
|
|---|
| 89 | void ClearScreen(); // clear screen
|
|---|
| 90 |
|
|---|
| 91 | void ForwardCursor(); // move cursor forward
|
|---|
| 92 | void BackwardCursor(); // move cursor backward
|
|---|
| 93 | void MoveCursorTop(); // move cursor to the top
|
|---|
| 94 | void MoveCursorEnd(); // move cursor to the end
|
|---|
| 95 |
|
|---|
| 96 | void NextCommand(); // next command
|
|---|
| 97 | void PreviousCommand(); // previous command
|
|---|
| 98 |
|
|---|
| 99 | void ListMatchedCommand(); // list matched commands
|
|---|
| 100 | void CompleteCommand(); // complete command
|
|---|
| 101 |
|
|---|
| 102 | // utilities...
|
|---|
| 103 | G4String GetFirstMatchedString(const G4String& str1,
|
|---|
| 104 | const G4String& str2) const;
|
|---|
| 105 |
|
|---|
| 106 | // history functionality (history# is managed in itself)
|
|---|
| 107 | std::vector<G4String> commandHistory;
|
|---|
| 108 | G4int maxHistory; // max# of histories stored
|
|---|
| 109 | G4int currentHistoryNo; // global
|
|---|
| 110 | G4int relativeHistoryIndex; // local index relative to current history#
|
|---|
| 111 |
|
|---|
| 112 | void StoreHistory(G4String aCommand);
|
|---|
| 113 | G4String RestoreHistory(G4int index); // index is global history#
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 | // (re)set termios
|
|---|
| 117 | termios tios; // terminal mode (prestatus)
|
|---|
| 118 | G4String clearString; // "clear code (^L)"
|
|---|
| 119 | void SetTermToInputMode();
|
|---|
| 120 | void RestoreTerm();
|
|---|
| 121 |
|
|---|
| 122 | public:
|
|---|
| 123 | G4UItcsh(const G4String& prompt="%s> ", G4int maxhist=100);
|
|---|
| 124 | ~G4UItcsh();
|
|---|
| 125 |
|
|---|
| 126 | void SetLsColor(TermColorIndex dirColor, TermColorIndex cmdColor);
|
|---|
| 127 | virtual G4String GetCommandLine(const char* msg=0);
|
|---|
| [593] | 128 |
|
|---|
| 129 | virtual void ResetTerminal();
|
|---|
| [476] | 130 | };
|
|---|
| 131 |
|
|---|
| [593] | 132 | // ====================================================================
|
|---|
| 133 | // inline functions
|
|---|
| 134 | // ====================================================================
|
|---|
| [476] | 135 | inline G4bool G4UItcsh::IsCursorLast() const
|
|---|
| 136 | {
|
|---|
| 137 | if(cursorPosition == G4int(commandLine.length()+1)) return TRUE;
|
|---|
| 138 | else return FALSE;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | inline void G4UItcsh::SetLsColor(TermColorIndex dirColor,
|
|---|
| 142 | TermColorIndex cmdColor)
|
|---|
| 143 | {
|
|---|
| 144 | lsColorFlag= TRUE;
|
|---|
| 145 | directoryColor= dirColor;
|
|---|
| 146 | commandColor= cmdColor;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | #endif
|
|---|
| 150 | #endif
|
|---|
| 151 |
|
|---|