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