summaryrefslogtreecommitdiff
path: root/StdLib/LibC/Uefi/InteractiveIO/IIOecho.c
blob: 14369de95b4ed09e5d6e866e5f34bdabcda39d05 (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
/** @file
  Echo characters to an Interactive I/O Output device.

  The functions assume that isatty() is TRUE at the time they are called.
  Since the UEFI console is a WIDE character device, these functions do all
  processing using wide characters.

  It is the responsibility of the caller, or higher level function, to perform
  any necessary translation between wide and narrow characters.

  Copyright (c) 2012, 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  <assert.h>
#include  <errno.h>
#include  <sys/termios.h>
#include  <Device/IIO.h>
#include  "IIOutilities.h"
#include  "IIOechoCtrl.h"

/** Echo one character to an IIO file.

    If character InCh is a special "echo control" character, process it and output
    the resultant character(s), if any.  Otherwise pass the character on to the
    IIO_WriteOne() function which performs generic output processing, if needed.

    @param[in]    filp        Pointer to an open IIO file's file descriptor structure.
    @param[in]    InCh        The wide character to be echoed.
    @param[in]    EchoIsOK    A flag indicating whether echoing is appropriate for this
                              device or not.

    @retval   -1    The filp argument does not refer to an IIO device.
                    Global value errno is set to EINVAL.
    @retval   >=0   The number of characters actually output.

    @sa   IIO_WriteOne
**/
ssize_t
IIO_EchoOne (
  struct __filedes     *filp,
  wchar_t               InCh,
  BOOLEAN               EchoIsOK
  )
{
  cIIO       *This;
  cFIFO      *OutBuf;
  cFIFO      *InBuf;
  UINT8      *AttrBuf;
  ssize_t     NumEcho;
  tcflag_t    LFlags;
  UINT32      AttrDex;
  int         i;

  NumEcho = -1;
  This    = filp->devdata;

  if(This != NULL) {
    LFlags  = This->Termio.c_lflag;
    OutBuf  = This->OutBuf;
    InBuf   = This->InBuf;
    AttrBuf = This->AttrBuf;
    AttrDex = InBuf->GetWDex(InBuf);

    switch(InCh) {
      case IIO_ECHO_DISCARD:
        // Do not buffer or otherwise process
        NumEcho = 0;
        break;

      case IIO_ECHO_ERASE:
        // Delete last character from InBuf
        if(!InBuf->IsEmpty(InBuf)) {
          (void)InBuf->Truncate(InBuf);

          // Erase screen character(s) based on Attrib value
          if(LFlags & ECHO) {
            AttrDex = (UINT32)ModuloDecrement(AttrDex, InBuf->NumElements);
            NumEcho = AttrBuf[AttrDex];
            for(i = 0; i < NumEcho; ++i) {
              (void)IIO_WriteOne(filp, OutBuf, CHAR_BACKSPACE);
            }
            if(LFlags & ECHOE) {
              for(i = 0; i < NumEcho; ++i) {
                (void)IIO_WriteOne(filp, OutBuf, L' ');
              }
              for(i = 0; i < NumEcho; ++i) {
                (void)IIO_WriteOne(filp, OutBuf, CHAR_BACKSPACE);
              }
            }
          }
          else {
            NumEcho = 0;
          }
        }
        break;

      case IIO_ECHO_KILL:
        // Flush contents of InBuf and OutBuf
        InBuf->Flush(InBuf, (size_t)-1);
        OutBuf->Flush(OutBuf, (size_t)-1);

        // Erase characters from screen.
        if(LFlags & ECHOE) {
          NumEcho = IIO_CursorDelta(This, &This->InitialXY, &This->CurrentXY);
          for(i = 0; i < NumEcho; ++i) {
            (void)IIO_WriteOne(filp, OutBuf, L' ');
          }
        }
        break;

      default:
        // Add character to input buffer
        (void)InBuf->Write(InBuf, &InCh, 1);

        NumEcho = 0;  // In case echoing is not enabled or OK
        // If echoing is OK and enabled, "echo" character using IIO_WriteOne
        if( EchoIsOK                &&
            ( (LFlags & ECHO)       ||
              ((LFlags & ECHONL) && (InCh == CHAR_LINEFEED))))
        {
          NumEcho = IIO_WriteOne(filp, OutBuf, InCh);
        }
        AttrBuf[AttrDex] = (UINT8)NumEcho;
        break;
    }
  }
  else {
    errno = EINVAL;
  }
  return NumEcho;
}