1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL |
---|
17 | |
---|
18 | #include "shell_buffer.h" |
---|
19 | |
---|
20 | #include <stdarg.h> |
---|
21 | |
---|
22 | #include "debug.h" |
---|
23 | #include "shell.h" |
---|
24 | #include "lib/util/threading.h" |
---|
25 | |
---|
26 | namespace OrxShell |
---|
27 | { |
---|
28 | /** |
---|
29 | * @brief standard constructor |
---|
30 | */ |
---|
31 | ShellBuffer::ShellBuffer () |
---|
32 | { |
---|
33 | ShellBuffer::singletonRef = this; |
---|
34 | this->shell = NULL; |
---|
35 | |
---|
36 | this->lineCount = 0; |
---|
37 | this->keepBufferArray[0] = '\0'; |
---|
38 | this->bufferArray[0] = '\0'; |
---|
39 | this->keepBuffer = false; |
---|
40 | |
---|
41 | this->setBufferSize(100); |
---|
42 | } |
---|
43 | |
---|
44 | ShellBuffer* ShellBuffer::singletonRef = NULL; |
---|
45 | std::list<std::string> ShellBuffer::buffer; |
---|
46 | |
---|
47 | /** |
---|
48 | * @brief standard deconstructor |
---|
49 | */ |
---|
50 | ShellBuffer::~ShellBuffer () |
---|
51 | { |
---|
52 | if (this->shell != NULL) |
---|
53 | delete this->shell; |
---|
54 | |
---|
55 | ShellBuffer::singletonRef = NULL; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * @brief registers the Shell to the Buffer |
---|
60 | * @param shell the Shell to register. |
---|
61 | */ |
---|
62 | void ShellBuffer::registerShell(Shell* shell) |
---|
63 | { |
---|
64 | if (this->shell == NULL) |
---|
65 | this->shell = shell; |
---|
66 | else |
---|
67 | PRINTF(1)("already registered a Shell to the ShellBuffer\n"); |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * @brief unregisters the Shell from the Buffer |
---|
72 | * @param shell the Shell to unregister. |
---|
73 | */ |
---|
74 | void ShellBuffer::unregisterShell(Shell* shell) |
---|
75 | { |
---|
76 | if (this->shell == shell) |
---|
77 | this->shell = NULL; |
---|
78 | else |
---|
79 | PRINTF(1)("cannot unregister shell, because it is not registered to the ShellBuffer\n"); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * @brief deletes all the Buffers |
---|
84 | */ |
---|
85 | void ShellBuffer::flush() |
---|
86 | { |
---|
87 | this->buffer.clear(); |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * @brief adds a new Line to the List of Buffers |
---|
92 | * @param line the Line as in the first argument in printf |
---|
93 | */ |
---|
94 | bool ShellBuffer::addBufferLineStatic(const char* line, ...) |
---|
95 | { |
---|
96 | va_list arguments; |
---|
97 | va_start(arguments, line); |
---|
98 | |
---|
99 | static OrxThread::Mutex ShellBuffer__bufferMutex; |
---|
100 | |
---|
101 | OrxThread::MutexLock bufferLock(&ShellBuffer__bufferMutex); |
---|
102 | #if DEBUG_LEVEL < 3 |
---|
103 | if (ShellBuffer::singletonRef == NULL) |
---|
104 | #endif |
---|
105 | vprintf(line, arguments); |
---|
106 | #if DEBUG_LEVEL < 3 |
---|
107 | else |
---|
108 | #else |
---|
109 | if (ShellBuffer::singletonRef != NULL) |
---|
110 | #endif |
---|
111 | ShellBuffer::singletonRef->addBufferLine(line, arguments); |
---|
112 | return true; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * @brief add a Line to the List of Buffers |
---|
117 | * @param line |
---|
118 | * @param arguments |
---|
119 | * |
---|
120 | * This function Adds one line to the buffer. |
---|
121 | * and displays the line as the First Line of the display-buffer |
---|
122 | */ |
---|
123 | void ShellBuffer::addBufferLine(const char* line, va_list arguments) |
---|
124 | { |
---|
125 | char* inputEnd; |
---|
126 | char* newLineBegin; |
---|
127 | char* newLineEnd; |
---|
128 | |
---|
129 | // copy the output to the bufferArray |
---|
130 | vsprintf(this->bufferArray, line, arguments); |
---|
131 | |
---|
132 | // check if we have something left in the buffers |
---|
133 | // if so, append the new String to this one. |
---|
134 | if (unlikely(this->keepBuffer)) |
---|
135 | { |
---|
136 | strcat(this->keepBufferArray, this->bufferArray); |
---|
137 | inputEnd = this->keepBufferArray + strlen(this->bufferArray); |
---|
138 | newLineBegin = this->keepBufferArray; |
---|
139 | this->keepBuffer = false; |
---|
140 | } |
---|
141 | else |
---|
142 | { |
---|
143 | inputEnd = this->bufferArray + strlen(this->bufferArray); |
---|
144 | newLineBegin = this->bufferArray; |
---|
145 | } |
---|
146 | |
---|
147 | // adding all the new Lines |
---|
148 | while (newLineBegin < inputEnd) |
---|
149 | { |
---|
150 | newLineEnd = strchr(newLineBegin, '\n'); |
---|
151 | if (likely(newLineEnd != NULL && *newLineEnd == '\n')) |
---|
152 | *newLineEnd = '\0'; |
---|
153 | else |
---|
154 | { |
---|
155 | unsigned int len = strlen(newLineBegin); |
---|
156 | char* copyBuffer = new char[len+1]; |
---|
157 | strcpy(copyBuffer, newLineBegin); |
---|
158 | strncpy(this->keepBufferArray, copyBuffer, len); |
---|
159 | delete[] copyBuffer; |
---|
160 | this->keepBufferArray[len] = '\0'; |
---|
161 | this->keepBuffer = true; |
---|
162 | break; |
---|
163 | } |
---|
164 | |
---|
165 | this->lineCount++; |
---|
166 | this->buffer.push_back(newLineBegin); |
---|
167 | if (likely (this->shell != NULL) && unlikely (this->shell->isActive())) |
---|
168 | this->shell->printToDisplayBuffer(newLineBegin); |
---|
169 | |
---|
170 | if (this->buffer.size() > this->bufferSize) |
---|
171 | this->buffer.pop_front(); |
---|
172 | |
---|
173 | newLineBegin = newLineEnd+1; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * @brief displays some nice output from the Shell |
---|
179 | */ |
---|
180 | void ShellBuffer::debug() const |
---|
181 | { |
---|
182 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
183 | |
---|
184 | std::list<std::string>::const_iterator bufferLine; |
---|
185 | for (bufferLine = this->buffer.begin(); bufferLine != this->buffer.end(); bufferLine++) |
---|
186 | printf((*bufferLine).c_str()); |
---|
187 | } |
---|
188 | |
---|
189 | } |
---|