summaryrefslogtreecommitdiff
path: root/StdLib/LibC/Uefi/InteractiveIO/CanonRead.c
blob: 8c8e076d80b6f6be7efe719ac53a6d2cd6038bf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/** @file
  Canonical Interactive Input Function.

  The functions assume that isatty() is TRUE at the time they are called.

  Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>
  This program and the accompanying materials are licensed and made available
  under the terms and conditions of the BSD License which accompanies this
  distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php.

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include  <Uefi.h>

#include  <LibConfig.h>

#include  <errno.h>
#include  <sys/syslimits.h>
#include  <sys/termios.h>
#include  <Device/IIO.h>
#include  <MainData.h>
#include  "IIOutilities.h"
#include  "IIOechoCtrl.h"

/** Read a line from the input file in canonical mode.
    Perform echoing and input processing as directed by the termios flags.

    @param[in]    filp      A pointer to a file descriptor structure.

    @return     The number of characters in the input buffer, or -1 if there
                was an error.
**/
ssize_t
IIO_CanonRead (
  struct __filedes *filp
  )
{
  cIIO             *This;
  cFIFO            *InBuf;
  struct termios   *Termio;
  struct __filedes *fpOut;
  size_t            NumRead;
  wint_t            InChar;
  tcflag_t          IFlag;
  tcflag_t          LFlag;
  BOOLEAN           EchoIsOK;
  BOOLEAN           Activate;
  BOOLEAN           FirstRead;
  int               OutMode;
  UINTN             MaxColumn;
  UINTN             MaxRow;

  NumRead   = MAX_INPUT;    // Workaround "potentially uninitialized" warning
  EchoIsOK  = FALSE;
  FirstRead = TRUE;
  This      = filp->devdata;
  Termio    = &This->Termio;
  InBuf     = This->InBuf;

  // Get a copy of the flags we are going to use
  IFlag = Termio->c_iflag;
  LFlag = Termio->c_lflag;

  /* Determine what the current screen size is. Also validates the output device. */
  OutMode = IIO_GetOutputSize(STDOUT_FILENO, &MaxColumn, &MaxRow);
  if(OutMode >= 0) {
    /*  Set the maximum screen dimensions. */
    This->MaxColumn = MaxColumn;
    This->MaxRow    = MaxRow;

    /*  Record where the cursor is at the beginning of this Input operation.
        The currently set stdout device is used to determine this.  If there is
        no stdout, or stdout is not an interactive device, nothing is recorded.
    */
    if (IIO_GetCursorPosition(STDOUT_FILENO, &This->InitialXY.Column, &This->InitialXY.Row) >= 0) {
      This->CurrentXY.Column  = This->InitialXY.Column;
      This->CurrentXY.Row     = This->InitialXY.Row;
      EchoIsOK  = TRUE;   // Can only echo to stdout
    }
  }

  // For now, we only echo to stdout.
  fpOut = &gMD->fdarray[STDOUT_FILENO];

  //  Input and process characters until BufferSize is exhausted.
  do {
    InChar = IIO_GetInChar(filp, FirstRead);
    if (InChar == WEOF) {
      NumRead = 0;
      break;
    }
    FirstRead = FALSE;
    Activate  = TRUE;
    if(InChar == CHAR_CARRIAGE_RETURN) {
      if((IFlag & IGNCR) != 0) {
        continue;   // Restart the do loop, discarding the CR
      }
      else if((IFlag & ICRNL) != 0) {
        InChar = L'\n';
      }
    }
    else if(InChar == CHAR_LINEFEED) {
      if((IFlag & INLCR) != 0) {
        InChar = L'\r';
      }
    }
    else if(CCEQ(Termio->c_cc[VINTR], InChar)) {
      if((LFlag & ISIG) != 0) {
        // Raise Signal
        // Flush Input Buffer
        // Return to caller
        InChar = IIO_ECHO_DISCARD;
        errno = EINTR;
      }
      else {
        Activate = FALSE;
      }
    }
    else if(CCEQ(Termio->c_cc[VQUIT], InChar)) {
      if((LFlag & ISIG) != 0) {
        // Raise Signal
        // Flush Input Buffer
        // Return to caller
        InChar = IIO_ECHO_DISCARD;
        errno = EINTR;
      }
      else {
        Activate = FALSE;
      }
    }
    else if(CCEQ(Termio->c_cc[VEOF], InChar)) {
      InChar = WEOF;
      NumRead = 0;
      EchoIsOK = FALSE;   // Buffer, but don't echo this character
    }
    else if(CCEQ(Termio->c_cc[VEOL], InChar)) {
      EchoIsOK = FALSE;   // Buffer, but don't echo this character
    }
    else if(CCEQ(Termio->c_cc[VERASE], InChar)) {
      InChar = IIO_ECHO_ERASE;
      Activate = FALSE;
    }
    else if(CCEQ(Termio->c_cc[VKILL], InChar)) {
      InChar = IIO_ECHO_KILL;
      Activate = FALSE;
    }
    else {
      if((InChar < TtySpecKeyMin) || (InChar >= TtyFunKeyMax)) {
        Activate = FALSE;
      }
    }
    /** The Echo function is responsible for:
          * Adding the character to the input buffer, if appropriate.
          * Removing characters from the input buffer for ERASE and KILL processing.
          * Visually removing characters from the screen if ECHOE is set.
          * Ensuring one can not backspace beyond the beginning of the input text.
          * Sending final echo strings to output.
    **/
    (void)This->Echo(fpOut, (wchar_t)InChar, EchoIsOK);
    NumRead = InBuf->Count(InBuf, AsElements);
  } while((NumRead < MAX_INPUT) &&
          (Activate == FALSE));

  return (ssize_t)NumRead;
}