CLI  2.9
ui_password.cpp
1 /*
2  Copyright (c) 2006-2018, Alexis Royer, http://alexis.royer.free.fr/CLI
3 
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation
10  and/or other materials provided with the distribution.
11  * Neither the name of the CLI library project nor the names of its contributors may be used to endorse or promote products derived from this software
12  without specific prior written permission.
13 
14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 
28 #include "cli/pch.h"
29 
30 #include "cli/ui_password.h"
31 #include "cli/shell.h"
32 #include "command_line_edition.h"
33 
34 
36 
37  CLI_NS_BEGIN(ui)
38 
39  Password::Password(const bool B_DisplayStars, const int I_MinPasswordLength, const int I_MaxPasswordLength)
40  : UI(),
41  m_bDisplayStars(B_DisplayStars), m_iMinPasswordLength(I_MinPasswordLength), m_iMaxPasswordLength(I_MaxPasswordLength),
42  m_cliPassword(* new CmdLineEdition()), m_cliLine(* new CmdLineEdition())
43  {
44  }
45 
46  Password::Password(ExecutionContext& CLI_ParentContext, const bool B_DisplayStars, const int I_MinPasswordLength, const int I_MaxPasswordLength)
47  : UI(CLI_ParentContext),
48  m_bDisplayStars(B_DisplayStars), m_iMinPasswordLength(I_MinPasswordLength), m_iMaxPasswordLength(I_MaxPasswordLength),
49  m_cliPassword(* new CmdLineEdition()), m_cliLine(* new CmdLineEdition())
50  {
51  }
52 
54  {
55  delete & m_cliPassword;
56  delete & m_cliLine;
57  }
58 
59  const tk::String Password::GetPassword(void) const
60  {
61  return m_cliPassword.GetLine();
62  }
63 
64  void Password::Reset(void)
65  {
66  m_cliPassword.Reset();
67  m_cliLine.Reset();
68  }
69 
71  {
72  const OutputDevice& ECHO = GetStream(ECHO_STREAM);
73 
74  m_cliPassword.Reset();
75  m_cliLine.CleanAll(ECHO);
76  }
77 
78  void Password::OnKey(const KEY E_KeyCode)
79  {
80  const OutputDevice& NOECHO = OutputDevice::GetNullDevice();
81  const OutputDevice& ECHO = GetStream(ECHO_STREAM);
82 
83  switch (E_KeyCode)
84  {
85  case NULL_KEY:
86  EndControl(false);
87  break;
88  //case KEY_UP:
89  //case KEY_DOWN:
90  //case PAGE_UP:
91  //case PAGE_DOWN:
92  case KEY_BEGIN:
93  m_cliPassword.Home(NOECHO);
94  if (m_bDisplayStars)
95  {
96  m_cliLine.Home(ECHO);
97  }
98  break;
99  case KEY_END:
100  m_cliPassword.End(NOECHO);
101  if (m_bDisplayStars)
102  {
103  m_cliLine.End(ECHO);
104  }
105  break;
106  case KEY_LEFT:
107  m_cliPassword.MoveCursor(NOECHO, -1);
108  if (! m_cliLine.GetLeft().IsEmpty())
109  {
110  m_cliLine.MoveCursor(ECHO, -1);
111  }
112  else
113  {
114  if (m_bDisplayStars) {
115  Beep();
116  }
117  }
118  break;
119  case KEY_RIGHT:
120  m_cliPassword.MoveCursor(NOECHO, 1);
121  if (! m_cliLine.GetRight().IsEmpty())
122  {
123  m_cliLine.MoveCursor(ECHO, 1);
124  }
125  else
126  {
127  if (m_bDisplayStars)
128  {
129  Beep();
130  }
131  }
132  break;
133  case BACKSPACE:
134  m_cliPassword.Delete(NOECHO, -1);
135  if (! m_cliLine.GetLeft().IsEmpty())
136  {
137  m_cliLine.Delete(ECHO, -1);
138  }
139  else
140  {
141  if (m_bDisplayStars)
142  {
143  Beep();
144  }
145  }
146  break;
147  case DELETE:
148  m_cliPassword.Delete(NOECHO, 1);
149  if (! m_cliLine.GetRight().IsEmpty())
150  {
151  m_cliLine.Delete(ECHO, 1);
152  }
153  else
154  {
155  if (m_bDisplayStars)
156  {
157  Beep();
158  }
159  }
160  break;
161  case ENTER:
162  if (((m_iMinPasswordLength < 0) || (m_cliPassword.GetLine().GetLength() >= (unsigned int) m_iMinPasswordLength))
163  && ((m_iMaxPasswordLength < 0) || (m_cliPassword.GetLine().GetLength() <= (unsigned int) m_iMaxPasswordLength)))
164  {
165  m_cliLine.NextLine(ECHO);
166  EndControl(true);
167  }
168  else
169  {
170  Beep();
171  }
172  break;
173  case BREAK:
174  case ESCAPE:
175  case LOGOUT:
176  EndControl(false);
177  break;
178  case TAB:
179  Beep();
180  break;
181  case CLS:
182  m_cliPassword.Reset();
183  m_cliLine.CleanAll(ECHO);
184  break;
185 
186  case QUESTION:
187  case KEY_a: case KEY_aacute: case KEY_agrave: case KEY_auml: case KEY_acirc:
188  case KEY_b: case KEY_c: case KEY_ccedil: case KEY_d:
189  case KEY_e: case KEY_eacute: case KEY_egrave: case KEY_euml: case KEY_ecirc:
190  case KEY_f: case KEY_g: case KEY_h:
191  case KEY_i: case KEY_iacute: case KEY_igrave: case KEY_iuml: case KEY_icirc:
192  case KEY_j: case KEY_k: case KEY_l: case KEY_m: case KEY_n:
193  case KEY_o: case KEY_oacute: case KEY_ograve: case KEY_ouml: case KEY_ocirc:
194  case KEY_p: case KEY_q: case KEY_r: case KEY_s: case KEY_t:
195  case KEY_u: case KEY_uacute: case KEY_ugrave: case KEY_uuml: case KEY_ucirc:
196  case KEY_v: case KEY_w: case KEY_x: case KEY_y: case KEY_z:
197 
198  case KEY_A: case KEY_B: case KEY_C: case KEY_D: case KEY_E: case KEY_F:
199  case KEY_G: case KEY_H: case KEY_I: case KEY_J: case KEY_K: case KEY_L:
200  case KEY_M: case KEY_N: case KEY_O: case KEY_P: case KEY_Q: case KEY_R:
201  case KEY_S: case KEY_T: case KEY_U: case KEY_V: case KEY_W: case KEY_X:
202  case KEY_Y: case KEY_Z:
203 
204  case KEY_0: case KEY_1: case KEY_2: case KEY_3: case KEY_4: case KEY_5:
205  case KEY_6: case KEY_7: case KEY_8: case KEY_9:
206 
207  case PLUS:
208  case MINUS:
209  case STAR:
210  case SLASH:
211  case LOWER_THAN:
212  case GREATER_THAN:
213  case EQUAL:
214  case PERCENT:
215 
216  case SPACE:
217  case UNDERSCORE:
218  case AROBASE:
219  case SHARP:
220  case AMPERCENT:
221  case DOLLAR:
222  case BACKSLASH:
223  case PIPE:
224  case TILDE:
225  case SQUARE:
226  case EURO:
227  case POUND:
228  case MICRO:
229  case PARAGRAPH:
230 
231  case EXCLAMATION:
232  case COLUMN:
233  case DOT:
234  case COMA:
235  case SEMI_COLUMN:
236  case QUOTE:
237  case DOUBLE_QUOTE:
238 
239  case OPENING_BRACE:
240  case CLOSING_BRACE:
241  case OPENING_CURLY_BRACE:
242  case CLOSING_CURLY_BRACE:
243  case OPENING_BRACKET:
244  case CLOSING_BRACKET:
245  if ((m_iMaxPasswordLength < 0) || (m_cliPassword.GetLine().GetLength() < (unsigned int) m_iMaxPasswordLength))
246  {
247  m_cliPassword.Put(NOECHO, E_KeyCode);
248  if (m_bDisplayStars)
249  {
250  m_cliLine.Put(ECHO, STAR);
251  }
252  }
253  else
254  {
255  if (m_bDisplayStars)
256  {
257  Beep();
258  }
259  }
260  break;
261 
262  case INSERT:
263  m_cliPassword.SetInsertMode(! m_cliPassword.GetInsertMode());
264  m_cliLine.SetInsertMode(! m_cliLine.GetInsertMode());
265  break;
266 
267  default:
268  // Non managed character. Just ignore.
269  break;
270  }
271  }
272 
273  CLI_NS_END(ui)
274 
276 
void NextLine(const OutputDevice &CLI_OutputDevice)
Moves the cursor to the next line.
Right arrow key.
Definition: io_device.h:194
static OutputDevice & GetNullDevice(void)
Null device singleton.
Main namespace of the CLI library.
Shell class definition.
Echo stream.
Definition: exec_context.h:55
Based on utf-8 encoding for &#39;§&#39; (changed in version 2.9)
Definition: io_device.h:170
const tk::String GetLeft(void) const
Left part of the command line accessor.
Based on utf-8 encoding for &#39;à&#39; (changed in version 2.9)
Definition: io_device.h:76
virtual ~Password(void)
Destructor.
Definition: ui_password.cpp:53
Based on utf-8 encoding for &#39;€&#39; (changed in version 2.9)
Definition: io_device.h:167
virtual void OnKey(const KEY E_KeyCode)
Handler called on character input.
Definition: ui_password.cpp:78
Based on utf-8 encoding for &#39;í&#39; (changed in version 2.9)
Definition: io_device.h:92
Based on utf-8 encoding for &#39;ì&#39; (changed in version 2.9)
Definition: io_device.h:93
const bool GetInsertMode(void) const
Insert mode retrieval.
Based on utf-8 encoding for &#39;ç&#39; (changed in version 2.9)
Definition: io_device.h:81
Based on utf-8 encoding for &#39;ü&#39; (changed in version 2.9)
Definition: io_device.h:114
End key.
Definition: io_device.h:201
#define CLI_NS_END(__ns)
End a namespace definition.
Definition: namespace.h:45
void EndControl(const bool B_ExecResult)
Method to call by child classes in order to end the control execution.
Definition: ui.cpp:79
Based on utf-8 encoding for &#39;£&#39; (changed in version 2.9)
Definition: io_device.h:168
Based on utf-8 encoding for &#39;á&#39; (changed in version 2.9)
Definition: io_device.h:75
Based on utf-8 encoding for &#39;ô&#39; (changed in version 2.9)
Definition: io_device.h:105
virtual void ResetToDefault(void)
Handler called when default value is required to be restored.
Definition: ui_password.cpp:70
Password user interface object.
Definition: ui_password.h:47
void MoveCursor(const OutputDevice &CLI_OutputDevice, const int I_Count)
Moves the cursor.
virtual void Reset(void)
Handler called when data reset is required.
Definition: ui_password.cpp:64
const tk::String GetLine(void) const
Current command line.
Password class definition.
Based on utf-8 encoding for &#39;ï&#39; (changed in version 2.9)
Definition: io_device.h:94
void CleanAll(const OutputDevice &CLI_OutputDevice)
Line deletion.
const tk::String GetRight(void) const
Right part of the command line accessor.
Password(const bool B_DisplayStars, const int I_MinPasswordLength, const int I_MaxPasswordLength)
Top execution context constructor.
Definition: ui_password.cpp:39
Execution context.
Definition: exec_context.h:149
CmdLineEdition class definition.
Space.
Definition: io_device.h:57
void Beep(void)
Sends a beep signal.
Based on utf-8 encoding for &#39;ü&#39; (changed in version 2.9)
Definition: io_device.h:86
CLI library default pre-compiled header.
Begin key.
Definition: io_device.h:200
Generic output device.
Definition: io_device.h:256
Based on utf-8 encoding for &#39;ä&#39; (changed in version 2.9)
Definition: io_device.h:77
void SetInsertMode(const bool B_InsertMode)
Insert mode setting.
Clean screen key (changed from 129 to 501 in order to avoid overlap with printable ASCII characters)...
Definition: io_device.h:60
Based on utf-8 encoding for &#39;ò&#39; (changed in version 2.9)
Definition: io_device.h:103
Based on utf-8 encoding for &#39;µ&#39; (changed in version 2.9)
Definition: io_device.h:169
enum _KEY KEY
Input keys.
const tk::String GetPassword(void) const
Password retrieval.
Definition: ui_password.cpp:59
#define CLI_NS_BEGIN(__ns)
Begin a namespace definition.
Definition: namespace.h:38
Based on utf-8 encoding for &#39;û&#39; (changed in version 2.9)
Definition: io_device.h:115
Based on utf-8 encoding for &#39;â&#39; (changed in version 2.9)
Definition: io_device.h:78
Based on utf-8 encoding for &#39;é&#39; (changed in version 2.9)
Definition: io_device.h:84
Escape.
Definition: io_device.h:56
Based on utf-8 encoding for &#39;ù&#39; (changed in version 2.9)
Definition: io_device.h:113
void Put(const OutputDevice &CLI_OutputDevice, const KEY E_Char)
Character addition.
Based on utf-8 encoding for &#39;²&#39; (changed in version 2.9)
Definition: io_device.h:166
Backspace (changed from &#39;\b&#39; to 8 in version 2.7 for ASCII compliance).
Definition: io_device.h:58
Based on utf-8 encoding for &#39;è&#39; (changed in version 2.9)
Definition: io_device.h:85
Left arrow key.
Definition: io_device.h:193
Based on utf-8 encoding for &#39;ú&#39; (changed in version 2.9)
Definition: io_device.h:112
Enter.
Definition: io_device.h:55
void Home(const OutputDevice &CLI_OutputDevice)
Moves the cursor at the beginning of the line.
void Delete(const OutputDevice &CLI_OutputDevice, const int I_Count)
Character deletion.
Command line edition objet.
Break (Ctrl+C).
Definition: io_device.h:53
Tab key.
Definition: io_device.h:62
Logout (Ctrl+D).
Definition: io_device.h:54
Based on utf-8 encoding for &#39;î&#39; (changed in version 2.9)
Definition: io_device.h:95
Based on utf-8 encoding for &#39;û&#39; (changed in version 2.9)
Definition: io_device.h:87
Generic user interface class.
Definition: ui.h:60
void End(const OutputDevice &CLI_OutputDevice)
Moves the cursor at the end of the line.
void Reset(void)
Reset the object.
const OutputDevice & GetStream(const STREAM_TYPE E_StreamType) const
Output stream accessor.
Insert key (changed from 500 to 502 in order to avoid overlap with printable ASCII characters)...
Definition: io_device.h:61
Based on utf-8 encoding for &#39;ö&#39; (changed in version 2.9)
Definition: io_device.h:104
Based on utf-8 encoding for &#39;ó&#39; (changed in version 2.9)
Definition: io_device.h:102
Null key.
Definition: io_device.h:51
Delete key (changed from 128 to 127 in version 2.7 for ASCII compliance).
Definition: io_device.h:59