The Pedigree Project  0.1
ConsoleMaster.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "Console.h"
21 #include "pedigree/kernel/processor/types.h"
22 #include "pedigree/kernel/utilities/Buffer.h"
23 #include "pedigree/kernel/utilities/String.h"
24 
25 class Filesystem;
26 
27 ConsoleMasterFile::ConsoleMasterFile(
28  size_t consoleNumber, String consoleName, Filesystem *pFs)
29  : ConsoleFile(consoleNumber, consoleName, pFs), bLocked(false), pLocker(0)
30 {
31 }
32 
34  uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock)
35 {
36  // Check for NL->CRNL conversion which requires special logic.
37  size_t slaveFlags = m_pOther->m_Flags;
38  if (!(slaveFlags & ConsoleManager::OMapNLToCRNL))
39  {
40  // Easy read/write - output line discipline will not need to do any
41  // conversions that involve expansion.
42  uint64_t nBytes =
43  m_Buffer.read(reinterpret_cast<char *>(buffer), size, bCanBlock);
44  if (!nBytes)
45  {
46  return 0;
47  }
48 
49  return outputLineDiscipline(
50  reinterpret_cast<char *>(buffer), nBytes, size, m_pOther->m_Flags);
51  }
52 
53  uint64_t totalBytes = 0;
54  while (totalBytes < size)
55  {
56  // Check for no longer able to read as needed.
57  if ((size / 2) == 0)
58  {
59  break;
60  }
61 
62  // We assume that the worst-case buffer might be read, which contains
63  // 100% newlines that would expand to carriage return + newline.
64  // Eventually we'll reach a point where we can't halve size and then
65  // we just return what's been read so far (assuming there's still
66  // content in the buffer by that stage).
67  // Note: the integer division will floor() which is intentional.
68  uint64_t nBytes = m_Buffer.read(
69  reinterpret_cast<char *>(buffer + totalBytes), size / 2, bCanBlock);
70  if (!nBytes)
71  {
72  break;
73  }
74 
75  // Perform line discipline using the full, unhalved size so we can
76  // expand all available newlines.
77  size_t disciplineSize = outputLineDiscipline(
78  reinterpret_cast<char *>(buffer + totalBytes), nBytes, size,
79  m_pOther->m_Flags);
80  totalBytes += disciplineSize;
81  size -= disciplineSize;
82 
83  // After the first iteration, disallow any further blocking so we read
84  // the remainder of the buffer then terminate quickly.
85  bCanBlock = false;
86  }
87 
88  return totalBytes;
89 }
90 
92  uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock)
93 {
94  if (!m_pOther->m_Buffer.canWrite(bCanBlock))
95  {
96  return 0;
97  }
98 
99  // Pass on to the input discipline, which will write to the slave.
100  inputLineDiscipline(reinterpret_cast<char *>(buffer), size);
101 
102  return size;
103 }
static size_t outputLineDiscipline(char *buf, size_t len, size_t maxSz, size_t flags=0)
Output line discipline.
bool canWrite(bool block)
Definition: Buffer.cc:409
size_t read(T *buffer, size_t count, bool block=true)
Definition: Buffer.cc:236
Definition: String.h:49
ConsoleFile * m_pOther
Other side of the console.
Definition: Console.h:135
void inputLineDiscipline(char *buf, size_t len, size_t flags=~0U, const char *controlChars=0)
Input line discipline.
virtual uint64_t writeBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
virtual uint64_t readBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)