]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Uefi/InteractiveIO/CanonRead.c
StdLib: Fix some build problems and obscure bugs.
[mirror_edk2.git] / StdLib / LibC / Uefi / InteractiveIO / CanonRead.c
1 /** @file
2 Canonical Interactive Input Function.
3
4 The functions assume that isatty() is TRUE at the time they are called.
5
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.
11
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.
14 **/
15 #include <Uefi.h>
16
17 #include <LibConfig.h>
18
19 #include <errno.h>
20 #include <sys/syslimits.h>
21 #include <sys/termios.h>
22 #include <Device/IIO.h>
23 #include <MainData.h>
24 #include "IIOutilities.h"
25 #include "IIOechoCtrl.h"
26
27 /** Read a line from the input file in canonical mode.
28 Perform echoing and input processing as directed by the termios flags.
29
30 @param[in] filp A pointer to a file descriptor structure.
31
32 @return The number of characters in the input buffer, or -1 if there
33 was an error.
34 **/
35 ssize_t
36 IIO_CanonRead (
37 struct __filedes *filp
38 )
39 {
40 cIIO *This;
41 cFIFO *InBuf;
42 struct termios *Termio;
43 struct __filedes *fpOut;
44 size_t NumRead;
45 wint_t InChar;
46 tcflag_t IFlag;
47 tcflag_t LFlag;
48 BOOLEAN EchoIsOK;
49 BOOLEAN Activate;
50 BOOLEAN FirstRead;
51 int OutMode;
52 UINTN MaxColumn;
53 UINTN MaxRow;
54
55 NumRead = MAX_INPUT; // Workaround "potentially uninitialized" warning
56 EchoIsOK = FALSE;
57 FirstRead = TRUE;
58 This = filp->devdata;
59 Termio = &This->Termio;
60 InBuf = This->InBuf;
61
62 // Get a copy of the flags we are going to use
63 IFlag = Termio->c_iflag;
64 LFlag = Termio->c_lflag;
65
66 /* Determine what the current screen size is. Also validates the output device. */
67 OutMode = IIO_GetOutputSize(STDOUT_FILENO, &MaxColumn, &MaxRow);
68 if(OutMode >= 0) {
69 /* Set the maximum screen dimensions. */
70 This->MaxColumn = MaxColumn;
71 This->MaxRow = MaxRow;
72
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.
76 */
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
81 }
82 }
83
84 // For now, we only echo to stdout.
85 fpOut = &gMD->fdarray[STDOUT_FILENO];
86
87 // Input and process characters until BufferSize is exhausted.
88 do {
89 InChar = IIO_GetInChar(filp, FirstRead);
90 FirstRead = FALSE;
91 Activate = TRUE;
92 if(InChar == CHAR_CARRIAGE_RETURN) {
93 if((IFlag & IGNCR) != 0) {
94 continue; // Restart the do loop, discarding the CR
95 }
96 else if((IFlag & ICRNL) != 0) {
97 InChar = L'\n';
98 }
99 }
100 else if(InChar == CHAR_LINEFEED) {
101 if((IFlag & INLCR) != 0) {
102 InChar = L'\r';
103 }
104 }
105 else if(CCEQ(Termio->c_cc[VINTR], InChar)) {
106 if((LFlag & ISIG) != 0) {
107 // Raise Signal
108 // Flush Input Buffer
109 // Return to caller
110 InChar = IIO_ECHO_DISCARD;
111 errno = EINTR;
112 }
113 else {
114 Activate = FALSE;
115 }
116 }
117 else if(CCEQ(Termio->c_cc[VQUIT], InChar)) {
118 if((LFlag & ISIG) != 0) {
119 // Raise Signal
120 // Flush Input Buffer
121 // Return to caller
122 InChar = IIO_ECHO_DISCARD;
123 errno = EINTR;
124 }
125 else {
126 Activate = FALSE;
127 }
128 }
129 else if(CCEQ(Termio->c_cc[VEOF], InChar)) {
130 InChar = WEOF;
131 }
132 else if(CCEQ(Termio->c_cc[VEOL], InChar)) {
133 EchoIsOK = FALSE; // Buffer, but don't echo this character
134 }
135 else if(CCEQ(Termio->c_cc[VERASE], InChar)) {
136 InChar = IIO_ECHO_ERASE;
137 Activate = FALSE;
138 }
139 else if(CCEQ(Termio->c_cc[VKILL], InChar)) {
140 InChar = IIO_ECHO_KILL;
141 Activate = FALSE;
142 }
143 else {
144 if((InChar < TtySpecKeyMin) || (InChar >= TtyFunKeyMax)) {
145 Activate = FALSE;
146 }
147 }
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.
154 **/
155 (void)This->Echo(fpOut, (wchar_t)InChar, EchoIsOK);
156 NumRead = InBuf->Count(InBuf, AsElements);
157 } while((NumRead < MAX_INPUT) &&
158 (Activate == FALSE));
159
160 return (ssize_t)NumRead;
161 }