| 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 | |
|---|
| 35 | this->lineCount = 0; |
|---|
| 36 | this->bufferArray[0] = '\0'; |
|---|
| 37 | |
|---|
| 38 | this->setMaxBufferSize(100); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | ShellBuffer* ShellBuffer::singletonRef = NULL; |
|---|
| 42 | std::list<std::string> ShellBuffer::buffer; |
|---|
| 43 | char ShellBuffer::bufferArray[SHELL_BUFFER_SIZE] = ""; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * @brief standard deconstructor |
|---|
| 48 | */ |
|---|
| 49 | ShellBuffer::~ShellBuffer () |
|---|
| 50 | { |
|---|
| 51 | ShellBuffer::singletonRef = NULL; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * @brief deletes all the Buffers |
|---|
| 56 | */ |
|---|
| 57 | void ShellBuffer::flush() |
|---|
| 58 | { |
|---|
| 59 | this->buffer.clear(); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * @brief adds a new Line to the List of Buffers |
|---|
| 64 | * @param line the Line as in the first argument in printf |
|---|
| 65 | */ |
|---|
| 66 | void ShellBuffer::addBufferLineStatic(const char* line, ...) |
|---|
| 67 | { |
|---|
| 68 | static OrxThread::Mutex ShellBuffer__bufferMutex; |
|---|
| 69 | |
|---|
| 70 | OrxThread::MutexLock bufferLock(&ShellBuffer__bufferMutex); |
|---|
| 71 | |
|---|
| 72 | va_list arguments; |
|---|
| 73 | va_start(arguments, line); |
|---|
| 74 | vsnprintf(ShellBuffer::bufferArray, SHELL_BUFFER_SIZE, line, arguments); |
|---|
| 75 | va_end(arguments); |
|---|
| 76 | |
|---|
| 77 | #if DEBUG_LEVEL < 3 |
|---|
| 78 | if (ShellBuffer::singletonRef == NULL) |
|---|
| 79 | #endif |
|---|
| 80 | printf(ShellBuffer::bufferArray); |
|---|
| 81 | #if DEBUG_LEVEL < 3 |
|---|
| 82 | else |
|---|
| 83 | #else |
|---|
| 84 | if (ShellBuffer::singletonRef != NULL) |
|---|
| 85 | #endif |
|---|
| 86 | ShellBuffer::singletonRef->addBufferLine(ShellBuffer::bufferArray); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * @brief add a Line to the List of Buffers |
|---|
| 91 | * @param line |
|---|
| 92 | * @param arguments |
|---|
| 93 | * |
|---|
| 94 | * This function Adds one line to the buffer. |
|---|
| 95 | * and displays the line as the First Line of the display-buffer |
|---|
| 96 | */ |
|---|
| 97 | void ShellBuffer::addBufferLine(const char* line) |
|---|
| 98 | { |
|---|
| 99 | std::string inputBuffer = this->keepBuffer + line; |
|---|
| 100 | |
|---|
| 101 | unsigned int lineBegin = 0; |
|---|
| 102 | unsigned int lineEnd = 0; |
|---|
| 103 | // adding all the new Lines |
|---|
| 104 | while (lineEnd < inputBuffer.size()) |
|---|
| 105 | { |
|---|
| 106 | lineBegin = lineEnd; |
|---|
| 107 | lineEnd = inputBuffer.find('\n', (lineBegin == 0) ? 0: ++lineBegin); |
|---|
| 108 | if (likely(lineEnd != std::string::npos )) |
|---|
| 109 | { |
|---|
| 110 | this->lineCount++; |
|---|
| 111 | this->buffer.push_front(inputBuffer.substr(lineBegin, lineEnd - lineBegin)); |
|---|
| 112 | } |
|---|
| 113 | else // No end of Line reached. |
|---|
| 114 | { |
|---|
| 115 | this->keepBuffer = inputBuffer.substr(lineBegin, inputBuffer.size() - lineBegin); |
|---|
| 116 | break; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | if (inputBuffer[lineBegin] == '\n') |
|---|
| 120 | lineBegin++, lineEnd++; |
|---|
| 121 | |
|---|
| 122 | if (this->buffer.size() > this->maxBufferSize) |
|---|
| 123 | this->buffer.pop_back(); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * @brief displays some nice output from the Shell |
|---|
| 129 | */ |
|---|
| 130 | void ShellBuffer::debug() const |
|---|
| 131 | { |
|---|
| 132 | PRINT(3)("Debugging output to console (not this shell)\n"); |
|---|
| 133 | |
|---|
| 134 | std::list<std::string>::const_iterator bufferLine; |
|---|
| 135 | for (bufferLine = --this->buffer.end(); bufferLine != this->buffer.begin(); --bufferLine) |
|---|
| 136 | printf((*bufferLine).c_str()); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | } |
|---|