]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Uefi/InteractiveIO/CanonRead.c
2 Canonical Interactive Input Function.
4 The functions assume that isatty() is TRUE at the time they are called.
6 Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <LibConfig.h>
20 #include <sys/syslimits.h>
21 #include <sys/termios.h>
22 #include <Device/IIO.h>
24 #include "IIOutilities.h"
25 #include "IIOechoCtrl.h"
27 /** Read a line from the input file in canonical mode.
28 Perform echoing and input processing as directed by the termios flags.
30 @param[in] filp A pointer to a file descriptor structure.
32 @return The number of characters in the input buffer, or -1 if there
37 struct __filedes
*filp
42 struct termios
*Termio
;
43 struct __filedes
*fpOut
;
55 NumRead
= MAX_INPUT
; // Workaround "potentially uninitialized" warning
59 Termio
= &This
->Termio
;
62 // Get a copy of the flags we are going to use
63 IFlag
= Termio
->c_iflag
;
64 LFlag
= Termio
->c_lflag
;
66 /* Determine what the current screen size is. Also validates the output device. */
67 OutMode
= IIO_GetOutputSize(STDOUT_FILENO
, &MaxColumn
, &MaxRow
);
69 /* Set the maximum screen dimensions. */
70 This
->MaxColumn
= MaxColumn
;
71 This
->MaxRow
= MaxRow
;
73 /* Record where the cursor is at the beginning of this Input operation.
74 The currently set stdout device is used to determine this. If there is
75 no stdout, or stdout is not an interactive device, nothing is recorded.
77 if (IIO_GetCursorPosition(STDOUT_FILENO
, &This
->InitialXY
.Column
, &This
->InitialXY
.Row
) >= 0) {
78 This
->CurrentXY
.Column
= This
->InitialXY
.Column
;
79 This
->CurrentXY
.Row
= This
->InitialXY
.Row
;
80 EchoIsOK
= TRUE
; // Can only echo to stdout
84 // For now, we only echo to stdout.
85 fpOut
= &gMD
->fdarray
[STDOUT_FILENO
];
87 // Input and process characters until BufferSize is exhausted.
89 InChar
= IIO_GetInChar(filp
, FirstRead
);
92 if(InChar
== CHAR_CARRIAGE_RETURN
) {
93 if((IFlag
& IGNCR
) != 0) {
94 continue; // Restart the do loop, discarding the CR
96 else if((IFlag
& ICRNL
) != 0) {
100 else if(InChar
== CHAR_LINEFEED
) {
101 if((IFlag
& INLCR
) != 0) {
105 else if(CCEQ(Termio
->c_cc
[VINTR
], InChar
)) {
106 if((LFlag
& ISIG
) != 0) {
108 // Flush Input Buffer
110 InChar
= IIO_ECHO_DISCARD
;
117 else if(CCEQ(Termio
->c_cc
[VQUIT
], InChar
)) {
118 if((LFlag
& ISIG
) != 0) {
120 // Flush Input Buffer
122 InChar
= IIO_ECHO_DISCARD
;
129 else if(CCEQ(Termio
->c_cc
[VEOF
], InChar
)) {
132 else if(CCEQ(Termio
->c_cc
[VEOL
], InChar
)) {
133 EchoIsOK
= FALSE
; // Buffer, but don't echo this character
135 else if(CCEQ(Termio
->c_cc
[VERASE
], InChar
)) {
136 InChar
= IIO_ECHO_ERASE
;
139 else if(CCEQ(Termio
->c_cc
[VKILL
], InChar
)) {
140 InChar
= IIO_ECHO_KILL
;
144 if((InChar
< TtySpecKeyMin
) || (InChar
>= TtyFunKeyMax
)) {
148 /** The Echo function is responsible for:
149 * Adding the character to the input buffer, if appropriate.
150 * Removing characters from the input buffer for ERASE and KILL processing.
151 * Visually removing characters from the screen if ECHOE is set.
152 * Ensuring one can not backspace beyond the beginning of the input text.
153 * Sending final echo strings to output.
155 (void)This
->Echo(fpOut
, (wchar_t)InChar
, EchoIsOK
);
156 NumRead
= InBuf
->Count(InBuf
, AsElements
);
157 } while((NumRead
< MAX_INPUT
) &&
158 (Activate
== FALSE
));
160 return (ssize_t
)NumRead
;