semf
processor.cpp
Go to the documentation of this file.
1
11#include <algorithm>
12#include <string>
13
14namespace semf::esh
15{
16Processor::Processor(const LinkedList<Command>& commands, Printer& printer, char lineBuffer[], size_t lineBufferSize, char** argvBuffer, size_t argvBufferSize)
17: m_commands(commands),
18 m_printer(printer),
19 m_lineBuffer{lineBuffer},
20 m_lineBufferSize{lineBufferSize},
21 m_argv{argvBuffer},
22 m_argvSize{argvBufferSize}
23{
24 std::fill_n(m_argv, m_argvSize, nullptr);
25}
26
28{
29 if (m_busy)
30 {
31 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::LoadCommand_IsBusy)));
32 return;
33 }
34 if (m_lineBuffer[0] == '\0')
35 {
36 done();
37 return;
38 }
39
40 m_busy = true;
41 parseLine();
42
43 for (const Command& command : m_commands)
44 {
45 if (command.name() == m_argv[0])
46 {
47 m_currentCommand = &command;
48 return;
49 }
50 }
51
52 m_lastExitStatus = -1;
53 printError();
54}
55
57{
58 if (m_currentCommand == nullptr)
59 return;
60
61 m_currentCommand->command(m_argc, m_argv, m_printer, m_lastExitStatus);
62 std::fill_n(m_argv, m_argvSize, nullptr);
63 m_currentCommand = nullptr;
64 onDone();
65}
66
67void Processor::parseLine()
68{
69 int pos = 0;
70 int length = std::char_traits<char>::length(m_lineBuffer);
71 m_argc = 0;
72
73 while (pos <= length)
74 {
75 if (m_lineBuffer[pos] != '\t' && m_lineBuffer[pos] != ' ' && m_lineBuffer[pos] != '\0')
76 m_argv[m_argc++] = &m_lineBuffer[pos];
77
78 for (; m_lineBuffer[pos] != '\t' && m_lineBuffer[pos] != ' ' && m_lineBuffer[pos] != '\0'; pos++)
79 {
80 }
81
82 if (m_lineBuffer[pos] == '\t' || m_lineBuffer[pos] == ' ')
83 m_lineBuffer[pos] = '\0';
84
85 pos++;
86 }
87}
88
89void Processor::printError()
90{
91 m_printer.dataWritten.connect(m_onQuotationMarkPrintedSlot);
92 m_printer.print("\r\n\"");
93}
94
95void Processor::onQuotationMarkPrinted()
96{
97 m_printer.dataWritten.disconnect(m_onQuotationMarkPrintedSlot);
98 m_printer.dataWritten.connect(m_onNamePrintedSlot);
99 m_printer.print(m_argv[0]);
100}
101void Processor::onNamePrinted()
102{
103 m_printer.dataWritten.disconnect(m_onNamePrintedSlot);
104 m_printer.dataWritten.connect(m_onDoneSlot);
105 m_printer.print("\": command not found.\r\n");
106}
107void Processor::onDone()
108{
109 m_printer.dataWritten.disconnect(m_onDoneSlot);
110 m_busy = false;
111 done();
112}
113} // namespace semf::esh
Class for representing errors. Every error should have a unique source code. As a user feel encourage...
Definition: error.h:22
LinkedList is an managed double linked list implementation.
Definition: linkedlist.h:43
Wrapper class for shell command. The user has to create object of this class for adding their functio...
Definition: command.h:28
Class for string related UART communication. Using this class outside of an esh-context can make sens...
Definition: printer.h:25
void print(char character, size_t count)
Prints a single character count times.
Definition: printer.cpp:20
Processor(const LinkedList< Command > &commands, Printer &printer, char lineBuffer[], size_t lineBufferSize, char **argvBuffer, size_t argvBufferSize)
Constructor.
Definition: processor.cpp:16
void loadCommand()
Load a command for execution according to the current command line.
Definition: processor.cpp:27
void execLoadedCommand()
Execute the currenty loaded command. Returns with no error if no command is loaded....
Definition: processor.cpp:56