The Pedigree Project  0.1
DebuggerIO.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 "pedigree/kernel/debugger/DebuggerIO.h"
21 #include "pedigree/kernel/debugger/DebuggerCommand.h"
22 #include "pedigree/kernel/utilities/utility.h"
23 
25 {
26  // Are we ready to recieve another command?
27  if (m_bReady)
28  {
29  // Yes, output a prompt...
30  writeCli("(db) ", DebuggerIO::LightGrey, DebuggerIO::Black);
31  // ...and zero the command string.
32  m_pCommand[0] = '\0';
33  m_bReady = false;
34  }
35 
36  char ch = 0;
37  // Spin in a loop until we get a printable character. getChar returns 0 if a
38  // non-printing character is recieved.
39  while (!(ch = getChar()))
40  ;
41 
42  // Was this a newline?
43  if (ch == '\n' || ch == '\r')
44  {
45  // Command finished, we're ready for the next.
46  m_bReady = true;
47  writeCli("\n", DebuggerIO::White, DebuggerIO::Black);
48  }
49  else
50  {
51  // We've got a character, try and append it to our command string.
52  // But first, was it a backspace?
53  if (ch == 0x08)
54  {
55  // Try and erase one letter of the command string.
56  if (StringLength(m_pCommand))
57  {
58  m_pCommand[StringLength(m_pCommand) - 1] = '\0';
59 
60  writeCli(ch, DebuggerIO::White, DebuggerIO::Black);
61  }
62  }
63  // Tab?
64  else if (ch == 0x09)
65  {
66  if (pAutoComplete)
67  {
68  // Get the full autocomplete string.
69  const char *pACString = pAutoComplete->getString();
70  // HACK:: Here we hack like complete bitches. Just find the last
71  // space in the string, and memcpy the full autocomplete string
72  // in.
73  ssize_t i;
74  for (i = StringLength(m_pCommand); i >= 0; i--)
75  if (m_pCommand[i] == ' ')
76  break;
77 
78  // We also haxxor the cursor, by writing loads of backspaces,
79  // then rewriting the whole string.
80  size_t nBackspaces = StringLength(m_pCommand) - i;
81  for (size_t j = 0; j < nBackspaces - 1; j++)
82  putChar(
83  '\x08' /* backspace */, DebuggerIO::White,
84  DebuggerIO::Black);
85 
86  writeCli(pACString, DebuggerIO::White, DebuggerIO::Black);
87 
88  // Memcpy the full autocomplete string in.
89  MemoryCopy(
90  &m_pCommand[i + 1], pACString, StringLength(pACString) + 1);
91  }
92  }
93  else
94  {
95  // Normal, printing character.
96  size_t len = StringLength(m_pCommand);
97  if (len < COMMAND_MAX - 1)
98  {
99  // Add it to the command string, and null terminate.
100  m_pCommand[len] = ch;
101  m_pCommand[len + 1] = '\0';
102  // And echo it back to the screen, too.
103  writeCli(ch, DebuggerIO::White, DebuggerIO::Black);
104  }
105  }
106  }
107 
108  // Now do a strncpy to the target string.
109  str = static_cast<const char *>(m_pCommand);
110 
111  return m_bReady;
112 }
113 
115  const char *str, DebuggerIO::Colour foreColour,
116  DebuggerIO::Colour backColour)
117 {
118  // We want to disable refreshes during writing, so the screen only gets
119  // updated once.
120  bool bRefreshWasEnabled = false;
122  {
123  bRefreshWasEnabled = true;
124  m_bRefreshesEnabled = false;
125  }
126 
127  // For every character, call writeCli.
128  while (*str)
129  writeCli(*str++, foreColour, backColour);
130 
131  // If refreshes were enabled to start with, reenable them, and force a
132  // refresh.
133  if (bRefreshWasEnabled)
134  {
135  m_bRefreshesEnabled = true;
136  forceRefresh();
137  }
138 }
139 
141  char c, DebuggerIO::Colour foreColour, DebuggerIO::Colour backColour)
142 {
143  // Write the character to the current cursor position.
144  putChar(c, foreColour, backColour);
145 
146  // Scroll if required.
147  scroll();
148 
149  // Move the cursor.
150  moveCursor();
151 
153  forceRefresh();
154 }
bool m_bReady
Definition: DebuggerIO.h:182
virtual void writeCli(const char *str, Colour foreColour, Colour backColour)
Definition: DebuggerIO.cc:114
virtual bool readCli(HugeStaticString &str, DebuggerCommand *pAutoComplete)
Definition: DebuggerIO.cc:24
char m_pCommand[COMMAND_MAX]
Definition: DebuggerIO.h:187
virtual char getChar()=0
virtual void moveCursor()=0
virtual void scroll()=0
virtual const NormalStaticString getString()=0
bool m_bRefreshesEnabled
Definition: DebuggerIO.h:192