From 7344075ad1bc29fe5192602aa5ad22e11be2e4cf Mon Sep 17 00:00:00 2001 From: prozessorkern Date: Sun, 23 Jun 2019 22:17:15 +0200 Subject: [PATCH] Finished first Version - fixed doxygen comments - changed license to MPLv2 - fixed some functions and removed not used ones --- LICENSE.md | 357 +++++++++++++++++++- api/shellmatta.h | 84 ++--- src/shellmatta.c | 844 +++++++++++++++++++++++++++++++---------------- 3 files changed, 966 insertions(+), 319 deletions(-) diff --git a/LICENSE.md b/LICENSE.md index 30e3150..3759302 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,9 +1,356 @@ -MIT License +Mozilla Public License Version 2.0 +================================== -Copyright (c) 2019 Stefan Strobel +### 1. Definitions -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +**1.1. “Contributor”** + means each individual or legal entity that creates, contributes to + the creation of, or owns Covered Software. -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +**1.2. “Contributor Version”** + means the combination of the Contributions of others (if any) used + by a Contributor and that particular Contributor's Contribution. + +**1.3. “Contribution”** + means Covered Software of a particular Contributor. + +**1.4. “Covered Software”** + means Source Code Form to which the initial Contributor has attached + the notice in Exhibit A, the Executable Form of such Source Code + Form, and Modifications of such Source Code Form, in each case + including portions thereof. + +**1.5. “Incompatible With Secondary Licenses”** + means + +* **(a)** that the initial Contributor has attached the notice described + in Exhibit B to the Covered Software; or +* **(b)** that the Covered Software was made available under the terms of + version 1.1 or earlier of the License, but not also under the + terms of a Secondary License. + +**1.6. “Executable Form”** + means any form of the work other than Source Code Form. + +**1.7. “Larger Work”** + means a work that combines Covered Software with other material, in + a separate file or files, that is not Covered Software. + +**1.8. “License”** + means this document. + +**1.9. “Licensable”** + means having the right to grant, to the maximum extent possible, + whether at the time of the initial grant or subsequently, any and + all of the rights conveyed by this License. + +**1.10. “Modifications”** + means any of the following: + +* **(a)** any file in Source Code Form that results from an addition to, + deletion from, or modification of the contents of Covered + Software; or +* **(b)** any new file in Source Code Form that contains any Covered + Software. + +**1.11. “Patent Claims” of a Contributor** + means any patent claim(s), including without limitation, method, + process, and apparatus claims, in any patent Licensable by such + Contributor that would be infringed, but for the grant of the + License, by the making, using, selling, offering for sale, having + made, import, or transfer of either its Contributions or its + Contributor Version. + +**1.12. “Secondary License”** + means either the GNU General Public License, Version 2.0, the GNU + Lesser General Public License, Version 2.1, the GNU Affero General + Public License, Version 3.0, or any later versions of those + licenses. + +**1.13. “Source Code Form”** + means the form of the work preferred for making modifications. + +**1.14. “You” (or “Your”)** + means an individual or a legal entity exercising rights under this + License. For legal entities, “You” includes any entity that + controls, is controlled by, or is under common control with You. For + purposes of this definition, “control” means **(a)** the power, direct + or indirect, to cause the direction or management of such entity, + whether by contract or otherwise, or **(b)** ownership of more than + fifty percent (50%) of the outstanding shares or beneficial + ownership of such entity. + + +### 2. License Grants and Conditions + +#### 2.1. Grants + +Each Contributor hereby grants You a world-wide, royalty-free, +non-exclusive license: + +* **(a)** under intellectual property rights (other than patent or trademark) + Licensable by such Contributor to use, reproduce, make available, + modify, display, perform, distribute, and otherwise exploit its + Contributions, either on an unmodified basis, with Modifications, or + as part of a Larger Work; and +* **(b)** under Patent Claims of such Contributor to make, use, sell, offer + for sale, have made, import, and otherwise transfer either its + Contributions or its Contributor Version. + +#### 2.2. Effective Date + +The licenses granted in Section 2.1 with respect to any Contribution +become effective for each Contribution on the date the Contributor first +distributes such Contribution. + +#### 2.3. Limitations on Grant Scope + +The licenses granted in this Section 2 are the only rights granted under +this License. No additional rights or licenses will be implied from the +distribution or licensing of Covered Software under this License. +Notwithstanding Section 2.1(b) above, no patent license is granted by a +Contributor: + +* **(a)** for any code that a Contributor has removed from Covered Software; + or +* **(b)** for infringements caused by: **(i)** Your and any other third party's + modifications of Covered Software, or **(ii)** the combination of its + Contributions with other software (except as part of its Contributor + Version); or +* **(c)** under Patent Claims infringed by Covered Software in the absence of + its Contributions. + +This License does not grant any rights in the trademarks, service marks, +or logos of any Contributor (except as may be necessary to comply with +the notice requirements in Section 3.4). + +#### 2.4. Subsequent Licenses + +No Contributor makes additional grants as a result of Your choice to +distribute the Covered Software under a subsequent version of this +License (see Section 10.2) or under the terms of a Secondary License (if +permitted under the terms of Section 3.3). + +#### 2.5. Representation + +Each Contributor represents that the Contributor believes its +Contributions are its original creation(s) or it has sufficient rights +to grant the rights to its Contributions conveyed by this License. + +#### 2.6. Fair Use + +This License is not intended to limit any rights You have under +applicable copyright doctrines of fair use, fair dealing, or other +equivalents. + +#### 2.7. Conditions + +Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted +in Section 2.1. + + +### 3. Responsibilities + +#### 3.1. Distribution of Source Form + +All distribution of Covered Software in Source Code Form, including any +Modifications that You create or to which You contribute, must be under +the terms of this License. You must inform recipients that the Source +Code Form of the Covered Software is governed by the terms of this +License, and how they can obtain a copy of this License. You may not +attempt to alter or restrict the recipients' rights in the Source Code +Form. + +#### 3.2. Distribution of Executable Form + +If You distribute Covered Software in Executable Form then: + +* **(a)** such Covered Software must also be made available in Source Code + Form, as described in Section 3.1, and You must inform recipients of + the Executable Form how they can obtain a copy of such Source Code + Form by reasonable means in a timely manner, at a charge no more + than the cost of distribution to the recipient; and + +* **(b)** You may distribute such Executable Form under the terms of this + License, or sublicense it under different terms, provided that the + license for the Executable Form does not attempt to limit or alter + the recipients' rights in the Source Code Form under this License. + +#### 3.3. Distribution of a Larger Work + +You may create and distribute a Larger Work under terms of Your choice, +provided that You also comply with the requirements of this License for +the Covered Software. If the Larger Work is a combination of Covered +Software with a work governed by one or more Secondary Licenses, and the +Covered Software is not Incompatible With Secondary Licenses, this +License permits You to additionally distribute such Covered Software +under the terms of such Secondary License(s), so that the recipient of +the Larger Work may, at their option, further distribute the Covered +Software under the terms of either this License or such Secondary +License(s). + +#### 3.4. Notices + +You may not remove or alter the substance of any license notices +(including copyright notices, patent notices, disclaimers of warranty, +or limitations of liability) contained within the Source Code Form of +the Covered Software, except that You may alter any license notices to +the extent required to remedy known factual inaccuracies. + +#### 3.5. Application of Additional Terms + +You may choose to offer, and to charge a fee for, warranty, support, +indemnity or liability obligations to one or more recipients of Covered +Software. However, You may do so only on Your own behalf, and not on +behalf of any Contributor. You must make it absolutely clear that any +such warranty, support, indemnity, or liability obligation is offered by +You alone, and You hereby agree to indemnify every Contributor for any +liability incurred by such Contributor as a result of warranty, support, +indemnity or liability terms You offer. You may include additional +disclaimers of warranty and limitations of liability specific to any +jurisdiction. + + +### 4. Inability to Comply Due to Statute or Regulation + +If it is impossible for You to comply with any of the terms of this +License with respect to some or all of the Covered Software due to +statute, judicial order, or regulation then You must: **(a)** comply with +the terms of this License to the maximum extent possible; and **(b)** +describe the limitations and the code they affect. Such description must +be placed in a text file included with all distributions of the Covered +Software under this License. Except to the extent prohibited by statute +or regulation, such description must be sufficiently detailed for a +recipient of ordinary skill to be able to understand it. + + +### 5. Termination + +**5.1.** The rights granted under this License will terminate automatically +if You fail to comply with any of its terms. However, if You become +compliant, then the rights granted under this License from a particular +Contributor are reinstated **(a)** provisionally, unless and until such +Contributor explicitly and finally terminates Your grants, and **(b)** on an +ongoing basis, if such Contributor fails to notify You of the +non-compliance by some reasonable means prior to 60 days after You have +come back into compliance. Moreover, Your grants from a particular +Contributor are reinstated on an ongoing basis if such Contributor +notifies You of the non-compliance by some reasonable means, this is the +first time You have received notice of non-compliance with this License +from such Contributor, and You become compliant prior to 30 days after +Your receipt of the notice. + +**5.2.** If You initiate litigation against any entity by asserting a patent +infringement claim (excluding declaratory judgment actions, +counter-claims, and cross-claims) alleging that a Contributor Version +directly or indirectly infringes any patent, then the rights granted to +You by any and all Contributors for the Covered Software under Section +2.1 of this License shall terminate. + +**5.3.** In the event of termination under Sections 5.1 or 5.2 above, all +end user license agreements (excluding distributors and resellers) which +have been validly granted by You or Your distributors under this License +prior to termination shall survive termination. + + +### 6. Disclaimer of Warranty + +> Covered Software is provided under this License on an “as is” +> basis, without warranty of any kind, either expressed, implied, or +> statutory, including, without limitation, warranties that the +> Covered Software is free of defects, merchantable, fit for a +> particular purpose or non-infringing. The entire risk as to the +> quality and performance of the Covered Software is with You. +> Should any Covered Software prove defective in any respect, You +> (not any Contributor) assume the cost of any necessary servicing, +> repair, or correction. This disclaimer of warranty constitutes an +> essential part of this License. No use of any Covered Software is +> authorized under this License except under this disclaimer. + +### 7. Limitation of Liability + +> Under no circumstances and under no legal theory, whether tort +> (including negligence), contract, or otherwise, shall any +> Contributor, or anyone who distributes Covered Software as +> permitted above, be liable to You for any direct, indirect, +> special, incidental, or consequential damages of any character +> including, without limitation, damages for lost profits, loss of +> goodwill, work stoppage, computer failure or malfunction, or any +> and all other commercial damages or losses, even if such party +> shall have been informed of the possibility of such damages. This +> limitation of liability shall not apply to liability for death or +> personal injury resulting from such party's negligence to the +> extent applicable law prohibits such limitation. Some +> jurisdictions do not allow the exclusion or limitation of +> incidental or consequential damages, so this exclusion and +> limitation may not apply to You. + + +### 8. Litigation + +Any litigation relating to this License may be brought only in the +courts of a jurisdiction where the defendant maintains its principal +place of business and such litigation shall be governed by laws of that +jurisdiction, without reference to its conflict-of-law provisions. +Nothing in this Section shall prevent a party's ability to bring +cross-claims or counter-claims. + + +### 9. Miscellaneous + +This License represents the complete agreement concerning the subject +matter hereof. If any provision of this License is held to be +unenforceable, such provision shall be reformed only to the extent +necessary to make it enforceable. Any law or regulation which provides +that the language of a contract shall be construed against the drafter +shall not be used to construe this License against a Contributor. + + +### 10. Versions of the License + +#### 10.1. New Versions + +Mozilla Foundation is the license steward. Except as provided in Section +10.3, no one other than the license steward has the right to modify or +publish new versions of this License. Each version will be given a +distinguishing version number. + +#### 10.2. Effect of New Versions + +You may distribute the Covered Software under the terms of the version +of the License under which You originally received the Covered Software, +or under the terms of any subsequent version published by the license +steward. + +#### 10.3. Modified Versions + +If you create software not governed by this License, and you want to +create a new license for such software, you may create and use a +modified version of this License if you rename the license and remove +any references to the name of the license steward (except to note that +such modified license differs from this License). + +#### 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses + +If You choose to distribute Source Code Form that is Incompatible With +Secondary Licenses under the terms of this version of the License, the +notice described in Exhibit B of this License must be attached. + +## Exhibit A - Source Code Form License Notice + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + +If it is not possible or desirable to put the notice in a particular +file, then You may include the notice in a location (such as a LICENSE +file in a relevant directory) where a recipient would be likely to look +for such a notice. + +You may add additional accurate notices of copyright ownership. + +## Exhibit B - “Incompatible With Secondary Licenses” Notice + + This Source Code Form is "Incompatible With Secondary Licenses", as + defined by the Mozilla Public License, v. 2.0. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/api/shellmatta.h b/api/shellmatta.h index 92fc88d..f39b28f 100644 --- a/api/shellmatta.h +++ b/api/shellmatta.h @@ -1,35 +1,19 @@ /* - * MIT License - * * Copyright (c) 2019 Stefan Strobel * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH - * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ /** - * @file shellmatta.h - * @brief API definition of the Shellmatta terminal implementation - * @author Stefan Strobel + * @file shellmatta.h + * @brief API definition of the Shellmatta terminal implementation + * @author Stefan Strobel */ /** - * @addtogroup shellmatta + * @addtogroup shellmatta_api * @{ */ @@ -39,6 +23,13 @@ #include #include +/* global defines */ + +/** + * @brief definition of a shellmatta handle + */ +typedef void* shellmatta_handle_t; + /** * @brief definition of shellmatta return codes */ @@ -62,11 +53,19 @@ typedef enum /** * @brief shellmatta command function definition + * @param[in] handle pointer to the instance which is calling the cmd + * @param[in] arguments argument string called to run this command beginning + * with the command itself + * @param[in] length length of the argument string */ -typedef shellmatta_retCode_t (*shellmatta_cmdFct_t)(int argc, char *argv[]); +typedef shellmatta_retCode_t (*shellmatta_cmdFct_t)(const shellmatta_handle_t handle, + const char *arguments, + uint32_t length); /** * @brief shellmatta write function definition + * @param[in] data data to be written to the output + * @param[in] length length of the data to be written */ typedef shellmatta_retCode_t (*shellmatta_write_t)(const char* data, uint32_t length); @@ -88,11 +87,12 @@ typedef struct shellmatta_cmd */ typedef struct { - uint8_t *buffer; /**< input buffer */ + uint32_t magic; /**< magic number to check if initialized */ + char *buffer; /**< input buffer */ uint32_t bufferSize; /**< size of the input buffer */ uint32_t inputCount; /**< offset of the current write operation */ uint32_t cursor; /**< offset where the cursor is at */ - uint8_t *historyBuffer; /**< buffer to store the last commands */ + char *historyBuffer; /**< buffer to store the last commands */ uint32_t historyBufferSize; /**< size of the history buffer */ uint32_t historyStart; /**< index of the oldest stored command */ uint32_t historyEnd; /**< index of the newest stored command */ @@ -100,27 +100,35 @@ typedef struct bool historyReadUp; /**< flag to show the last history dir */ uint32_t tabCounter; /**< counts the tabulator key presses */ uint32_t escapeCounter; /**< counts the characters of an escape seq */ - uint8_t escapeChars[4]; /**< buffer to save the escape characters */ + char escapeChars[4]; /**< buffer to save the escape characters */ bool echoEnabled; /**< if true the input is printed */ bool dirty; /**< dirty flag to show changes */ const char *prompt; /**< prompt is printed after every command */ shellmatta_mode_t mode; /**< mode of the shell */ shellmatta_write_t write; /**< pointer to write function */ shellmatta_cmd_t *cmdList; /**< pointer to the first command */ + bool cmdListIsConst; /**< true if the #cmdList was passed during + initialization */ } shellmatta_instance_t; + shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t *inst, - uint8_t *buffer, - uint32_t bufferSize, - uint8_t *historyBuffer, - uint32_t historyBufferSize, - const char *prompt, - shellmatta_write_t writeFct); -shellmatta_retCode_t shellmatta_addCmd(shellmatta_instance_t *inst, shellmatta_cmd_t *cmd); -void shellmatta_doTask(shellmatta_instance_t *inst, uint32_t time); -void shellmatta_processData(shellmatta_instance_t *inst, char *data, uint32_t size); -void shellmatta_printf(shellmatta_instance_t *inst, const char *fmt, ...); -void shellmatta_getArg(uint32_t cnt, uint8_t *arg); + shellmatta_handle_t *handle, + char *buffer, + uint32_t bufferSize, + char *historyBuffer, + uint32_t historyBufferSize, + const char *prompt, + const shellmatta_cmd_t *cmdList, + shellmatta_write_t writeFct); +shellmatta_retCode_t shellmatta_addCmd( shellmatta_handle_t handle, + shellmatta_cmd_t *cmd); +shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle, + char *data, + uint32_t size); +shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle, + const char *fmt, + ...); #endif diff --git a/src/shellmatta.c b/src/shellmatta.c index 48ca8eb..ca4e595 100644 --- a/src/shellmatta.c +++ b/src/shellmatta.c @@ -1,36 +1,22 @@ /* - * MIT License - * * Copyright (c) 2019 Stefan Strobel * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH - * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ /** - * @file shellmatta.c - * @brief Main implementation of the Shellmatta terminal implementation - * @author Stefan Strobel + * @file shellmatta.c + * @brief Main implementation of the Shellmatta terminal implementation + * @author Stefan Strobel */ /** * @addtogroup shellmatta * @{ + * @addtogroup shellmatta_private + * @{ */ #include "shellmatta.h" @@ -39,12 +25,40 @@ #include #include -#define SHELLMATTA_MIN(a,b) (((a) > (b)) ? (b) : (a)) -#define SHELLMATTA_MAX(a,b) (((a) > (b)) ? (b) : (a)) -static void writeEcho( shellmatta_instance_t *inst, - const uint8_t *data, - uint32_t length) +/* defines */ + +#ifndef SHELLMATTA_OUTPUT_BUFFER_SIZE +#define SHELLMATTA_OUTPUT_BUFFER_SIZE 128u +#endif + +#define SHELLMATTA_MIN(a,b) (((a) > (b)) ? (b) : (a)) +#define SHELLMATTA_MAX(a,b) (((a) < (b)) ? (b) : (a)) +#define SHELLMATTA_PRINT_BUFFER(buffer,cnt,fct) \ + while((cnt) > sizeof((buffer))) \ + { \ + (cnt) -= sizeof((buffer)); \ + (fct)((buffer), sizeof((buffer))); \ + } \ + if((cnt) != 0u) \ + { \ + (fct)((buffer), (cnt)); \ + } + +#define SHELLMATTA_MAGIC 0x5101E110u + +/* static functions */ + +/** + * @brief function to write an echo to the output depending on + * the echo enabled state of the instance + * @param[in] inst pointer to a shellmatta instance + * @param[in] data pointer to the data so send + * @param[in] length length of the data to send (in byte) + */ +static void writeEcho( shellmatta_instance_t *inst, + const char *data, + uint32_t length) { if(true == inst->echoEnabled) { @@ -52,20 +66,27 @@ static void writeEcho( shellmatta_instance_t *inst, } } -static void appendHistoryByte(shellmatta_instance_t *inst, uint8_t byte) +/** + * @brief appends a byte to the history ring stack buffer + * @param[in] inst pointer to a shellmatta instance + * @param[in] byte byte to append to the history buffer + */ +static void appendHistoryByte(shellmatta_instance_t *inst, char byte) { + /** -# calculate the new history buffer index */ inst->historyEnd ++; - if(inst->historyEnd >= inst->historyBufferSize) { inst->historyEnd = 0u; } + /** -# append the byte */ inst->historyBuffer[inst->historyEnd] = byte; + /** -# check if the we overwrite an existing stored command */ if(inst->historyEnd == inst->historyStart) { - /* move the start pointer to the next termination (0) */ + /** -# move the start pointer to the next termination (0) */ do { inst->historyStart ++; @@ -79,12 +100,20 @@ static void appendHistoryByte(shellmatta_instance_t *inst, uint8_t byte) } } -static bool getHistoryByte(shellmatta_instance_t *inst, uint8_t *byte) +/** + * @brief reads a byte from the history buffer and decreases the read index + * @param[in] inst pointer to a shellmatta instance + * @param[out] byte pointer to a char where the read out byte will be stored + * @return + */ +static bool getHistoryByte(shellmatta_instance_t *inst, char *byte) { bool ret = false; + /** -# check if we have reached the end of the buffer already */ if(inst->historyRead != inst->historyStart) { + /** -# read out one byte and decrease the read index */ *byte = inst->historyBuffer[inst->historyRead]; if(0u == inst->historyRead) @@ -100,22 +129,41 @@ static bool getHistoryByte(shellmatta_instance_t *inst, uint8_t *byte) return ret; } +/** + * @brief stores the current command from the instances buffer into the + * history buffer + * @param[in] inst pointer to a shellmatta instance + */ static void storeHistoryCmd(shellmatta_instance_t *inst) { + uint32_t i; + + /** -# check if we have enough room for the command in the history buffer + * and there is a new command to be stored */ if( (inst->historyBufferSize > inst->inputCount) && (0u != inst->inputCount) && (true == inst->dirty)) { + /** -# append the command termination */ appendHistoryByte(inst, 0u); - for(uint32_t i = inst->inputCount; i > 0u; i --) + /** -# append the command byte wise in reverse direction */ + for(i = inst->inputCount; i > 0u; i --) { appendHistoryByte(inst, inst->buffer[i - 1u]); } } + + /** -# remove the dirty flag - everything is nice and saved */ inst->dirty = false; } +/** + * @brief navigates in the history buffer by the given number of commands + * @param[in] inst pointer to a shellmatta instance + * @param[in] cnt direction and count to navigate + * @return + */ static bool navigateHistoryCmd(shellmatta_instance_t *inst, int32_t cnt) { bool ret = true; @@ -188,9 +236,14 @@ static bool navigateHistoryCmd(shellmatta_instance_t *inst, int32_t cnt) return ret; } +/** + * @brief restores the command from the history buffer where the read + * index points on + * @param[in] inst pointer to a shellmatta instance + */ static void restoreHistoryCmd(shellmatta_instance_t *inst) { - uint8_t byte; + char byte; bool ret = true; ret = getHistoryByte(inst, &byte); @@ -207,31 +260,54 @@ static void restoreHistoryCmd(shellmatta_instance_t *inst) inst->dirty = false; } +/** + * @brief resets the history buffer pointers to show to the most recent + * command again + * @param[in] inst pointer to a shellmatta instance + */ static void resetHistoryBuffer(shellmatta_instance_t *inst) { inst->historyRead = inst->historyEnd; inst->historyReadUp = true; } +/** + * @brief tells the terminal to save the current cursor position + * @param[in] inst pointer to shellmatta instance + */ static void saveCursorPos(shellmatta_instance_t *inst) { writeEcho(inst, "\e[s", 3u); } +/** + * @brief tells the terminal to restore the saved cursor position + * @param[in] inst pointer to shellmatta instance + */ static void restoreCursorPos(shellmatta_instance_t *inst) { writeEcho(inst, "\e[u", 3u); } +/** + * @brief tells the terminal to erase the line on the right side of the + * cursor + * @param[in] inst pointer to shellmatta instance + */ static void eraseLine(shellmatta_instance_t *inst) { writeEcho(inst, "\e[K", 3u); } +/** + * @brief moves the cursor back by the given amoung of characters + * @param[in] inst pointer to shellmatta instance + * @param[in] length number of characters to rewind + */ static void rewindCursor(shellmatta_instance_t *inst, uint32_t length) { - uint8_t terminalCmd[16]; - size_t size; + char terminalCmd[16]; + size_t size; length = SHELLMATTA_MIN (length, inst->cursor); if(length > 0u) @@ -242,78 +318,105 @@ static void rewindCursor(shellmatta_instance_t *inst, uint32_t length) } } +/** + * @brief moves the cursor forward by the given amoung of characters + * @param[in] inst pointer to shellmatta instance + * @param[in] length number of characters to move forward + */ static void forwardCursor(shellmatta_instance_t *inst, uint32_t length) { - uint8_t terminalCmd[16]; + char terminalCmd[16]; size_t size; - length = SHELLMATTA_MAX (length, - (inst->inputCount - inst->cursor)); + length = SHELLMATTA_MAX (length, (inst->inputCount - inst->cursor)); if (length > 0u) { size = snprintf(terminalCmd, sizeof(terminalCmd), "\e[%uC", length); writeEcho(inst, terminalCmd, size); - inst->cursor -= length; + inst->cursor += length; } } +/** + * @brief inserts the given amount of characters at the cursor position + * @param[in] inst pointer to shellmatta instance + * @param[in] data pointer to the data to be inserted + * @param[in] length length of the data to be inserted + */ static void insertChars(shellmatta_instance_t *inst, - uint8_t *data, + char *data, uint32_t length) { - /* check if we have to move chars in the buffer */ - if( (inst->cursor != inst->inputCount) - && (SHELLMATTA_MODE_INSERT == inst->mode)) + if(0u != length) { - /* move the existing chars */ - for ( uint32_t i = inst->inputCount; - i > inst->cursor; - i --) + /** -# check if we have to move chars in the buffer */ + if( (inst->cursor != inst->inputCount) + && (SHELLMATTA_MODE_INSERT == inst->mode)) { - inst->buffer[i + length - 1] = inst->buffer[i - 1]; + /** -# move the existing chars */ + for ( uint32_t i = inst->inputCount; + i > inst->cursor; + i --) + { + inst->buffer[i + length - 1] = inst->buffer[i - 1]; + } + + /** -# store and print the new chars */ + memcpy(&(inst->buffer[inst->cursor]), data, length); + writeEcho(inst, data, length); + + /** -# print the other chars and restore the cursor to this position */ + eraseLine(inst); + saveCursorPos(inst); + writeEcho( inst, + &(inst->buffer[inst->cursor + length]), + inst->inputCount - inst->cursor); + restoreCursorPos(inst); + } + /** -# just overwrite/append the chars */ + else + { + memcpy(&(inst->buffer[inst->cursor]), data, length); + writeEcho(inst, data, length); } - /* store and print the new chars */ - memcpy(&(inst->buffer[inst->cursor]), data, length); - writeEcho(inst, data, length); - - /* print the other chars and restore the cursor to this position */ - eraseLine(inst); - saveCursorPos(inst); - writeEcho( inst, - &(inst->buffer[inst->cursor + length]), - inst->inputCount - inst->cursor); - restoreCursorPos(inst); + inst->inputCount += length; + inst->cursor += length; } - /* just overwrite/append the chars */ - else - { - memcpy(&(inst->buffer[inst->cursor]), data, length); - writeEcho(inst, data, length); - } - - inst->inputCount += length; - inst->cursor += length; } +/** + * @brief removes the given amount of characters from the current cursor + * position + * @param[in] inst pointer to a shellmatta instance + * @param[in] length number of characters to remove + * @param[in] backspace remove characters left of the cursor + */ static void removeChars(shellmatta_instance_t *inst, uint32_t length, bool backspace) { - length = SHELLMATTA_MIN (length, inst->cursor); if(0u != length) { + /** -# rewind the cursor in case of backspace */ if(true == backspace) { + length = SHELLMATTA_MIN (length, inst->cursor); rewindCursor(inst, length); } - /* delete the char at the cursor position */ + else + { + length = SHELLMATTA_MIN (length, inst->inputCount - inst->cursor); + } + /** -# delete the char at the cursor position */ for ( uint32_t i = inst->cursor; i < (inst->inputCount - length); i++) { inst->buffer[i] = inst->buffer[i + length]; } + + /** -# print the rest of the line again */ eraseLine(inst); saveCursorPos(inst); writeEcho( inst, @@ -325,6 +428,11 @@ static void removeChars(shellmatta_instance_t *inst, } } +/** + * @brief clears the input buffer and removes the currend command from + * the terminal output + * @param[in] inst pointer to a shellmatta instance + */ static void clearInput(shellmatta_instance_t *inst) { rewindCursor(inst, inst->cursor); @@ -333,6 +441,12 @@ static void clearInput(shellmatta_instance_t *inst) inst->dirty = false; } +/** + * @brief processes the excape character stream of the instance and chacks + * if an arrow key was pressed + * @param[in] inst pointer to a shellmatta instance + * @return #SHELLMATTA_OK if an arrow key was pressed + */ static shellmatta_retCode_t processArrowKeys(shellmatta_instance_t *inst) { shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT; @@ -385,27 +499,37 @@ static shellmatta_retCode_t processArrowKeys(shellmatta_instance_t *inst) return ret; } -void handleEscapeSequence(shellmatta_instance_t *inst, uint8_t *data) +/** + * @brief handles a ANSI escape sequence to be able to react to some + * special keys + * @param[in] inst pointer to a shellmatta instance + * @param[in] data new received character of the escape sequence + */ +static void handleEscapeSequence(shellmatta_instance_t *inst, char data) { switch (inst->escapeCounter) { case 1u: - inst->escapeChars[inst->escapeCounter - 1] = *data; + inst->escapeChars[inst->escapeCounter - 1] = data; inst->escapeCounter ++; break; case 2u: - inst->escapeChars[inst->escapeCounter - 1] = *data; + inst->escapeChars[inst->escapeCounter - 1] = data; + /** -# check if an arrow key was pressed */ if(SHELLMATTA_OK == processArrowKeys(inst)) { inst->escapeCounter = 0u; } - else if ( (0x4fu == inst->escapeChars[0]) - && (0x46u == inst->escapeChars[1])) + /** -# check if end was pressed */ + else if ( (0x4f == inst->escapeChars[0]) + && (0x46 == inst->escapeChars[1])) { forwardCursor(inst, inst->inputCount - inst->cursor); inst->escapeCounter = 0u; } - else if(0u == (0x40u & *data)) + /** -# if the highest bit is not set this is usually not the end of the + * sequence - so we go on */ + else if(0u == (0x40 & data)) { inst->escapeCounter ++; } @@ -415,15 +539,16 @@ void handleEscapeSequence(shellmatta_instance_t *inst, uint8_t *data) } break; case 3u: - inst->escapeChars[inst->escapeCounter - 1] = *data; - if( (0x5bu == inst->escapeChars[0]) - && (0x33u == inst->escapeChars[1]) - && (0x7eu == inst->escapeChars[2])) + /** -# check if delete was pressed */ + inst->escapeChars[inst->escapeCounter - 1] = data; + if( (0x5b == inst->escapeChars[0]) + && (0x33 == inst->escapeChars[1]) + && (0x7e == inst->escapeChars[2])) { removeChars(inst, 1u, false); inst->escapeCounter = 0u; } - /* pos1 */ + /** -# check if pos1 was pressed */ else if ( (0x5bu == inst->escapeChars[0]) && (0x31u == inst->escapeChars[1]) && (0x7eu == inst->escapeChars[2])) @@ -431,7 +556,7 @@ void handleEscapeSequence(shellmatta_instance_t *inst, uint8_t *data) rewindCursor(inst, inst->cursor); inst->escapeCounter = 0u; } - else if(0u == (0x40u & *data)) + else if(0u == (0x40u & data)) { inst->escapeCounter ++; } @@ -446,55 +571,66 @@ void handleEscapeSequence(shellmatta_instance_t *inst, uint8_t *data) } } +/** + * @brief terminates an input and prints the prompt again + * @param[in] inst pointer to a shellmatta instance + */ static void terminateInput(shellmatta_instance_t *inst) { inst->inputCount = 0u; inst->cursor = 0u; - shellmatta_printf(inst, "\n\r%s", inst->prompt); + shellmatta_printf(inst, "\r\n%s", inst->prompt); } +/** + * @brief searches the registered commands for matching ones and prints + * the common part of all matching commands on the output + * if called twice all matching commands are printed + * @param[in] inst pointer to a shellmatta instance + */ static void doAutocomplete(shellmatta_instance_t *inst) { shellmatta_cmd_t *cmd = inst->cmdList; - uint8_t *tempCmd = NULL; + char *tempCmd = NULL; uint32_t minLen = 0u; uint32_t sizeDiff = 0u; bool exactMatch = true; uint32_t printedLen = 0u; + uint32_t tempCursor = 0u; + /** -# increase the tab counter to print all matching commands next time */ inst->tabCounter++; - /* on douple tab show all matching commands */ + /** -# on douple tab show all matching commands */ if (1u < inst->tabCounter) { inst->tabCounter = 0u; + /** -# loop through all registered commands */ while (NULL != cmd) { - /* check if command matches the input */ - if( (strlen(cmd->cmd) > inst->cursor) + /** -# check if command matches the input */ + if( (strlen(cmd->cmd) >= inst->cursor) && (0u == memcmp(cmd->cmd, inst->buffer, inst->cursor))) { - /* add newline on first find */ + /** -# add newline on first find */ if(0u == printedLen) { - saveCursorPos(inst); - inst->write("\n\r", 2u); + inst->write("\r\n", 2u); } inst->write(cmd->cmd, strlen(cmd->cmd)); printedLen += strlen(cmd->cmd); inst->write(" ", 4u); printedLen += 4u; } - /* check if command matches the input */ - if( (strlen(cmd->cmdAlias) > inst->cursor) + /** -# check if command alias matches the input */ + if( (strlen(cmd->cmdAlias) >= inst->cursor) && (0u == memcmp(cmd->cmdAlias, inst->buffer, inst->cursor))) { - /* add newline on first find */ + /** -# add newline on first find */ if(0u == printedLen) { - saveCursorPos(inst); - inst->write("\n\r", 2u); + inst->write("\r\n", 2u); } inst->write(cmd->cmdAlias, strlen(cmd->cmdAlias)); printedLen += strlen(cmd->cmdAlias); @@ -503,32 +639,34 @@ static void doAutocomplete(shellmatta_instance_t *inst) } cmd = cmd->next; } - /* print input again if a commands was found */ + /** -# print input again if a commands was found */ if(printedLen > 0u) { - writeEcho(inst, "\n\r", 2u); + writeEcho(inst, "\r\n", 2u); writeEcho(inst, inst->prompt, strlen(inst->prompt)); writeEcho(inst, inst->buffer, inst->inputCount); - restoreCursorPos(inst); + tempCursor = inst->cursor; + inst->cursor = inst->inputCount; + rewindCursor(inst, inst->inputCount - tempCursor); } } - /* on single tab autocomplete as far as possible */ + /** -# on single tab autocomplete as far as possible */ else if(0u != inst->cursor) { - /* loop through all registered commands */ + /** -# loop through all registered commands */ while (NULL != cmd) { - /* check if command matches the input */ - if( (strlen(cmd->cmd) > inst->cursor) + /** -# check if command matches the input */ + if( (strlen(cmd->cmd) >= inst->cursor) && (0u == memcmp(cmd->cmd, inst->buffer, inst->cursor))) { - /* store first match */ + /** -# store first match */ if(NULL == tempCmd) { tempCmd = cmd->cmd; minLen = strlen(cmd->cmd); } - /* find common part of the matches */ + /** -# find common part of the matching commands */ else { exactMatch = false; @@ -543,17 +681,17 @@ static void doAutocomplete(shellmatta_instance_t *inst) } } - /* check if command Alias matches the input */ - if( (strlen(cmd->cmdAlias) > inst->cursor) + /** -# check if command Alias matches the input */ + if( (strlen(cmd->cmdAlias) >= inst->cursor) && (0u == memcmp(cmd->cmdAlias, inst->buffer, inst->cursor))) { - /* store first match */ + /** -# store first match */ if(NULL == tempCmd) { tempCmd = cmd->cmdAlias; minLen = strlen(cmd->cmdAlias); } - /* find common part of the matches */ + /** -# find common part of the matches */ else { exactMatch = false; @@ -570,16 +708,16 @@ static void doAutocomplete(shellmatta_instance_t *inst) cmd = cmd->next; } - /* autocomplete found command */ + /** -# autocomplete found command */ if(NULL != tempCmd) { - /* calculate the size of the command to be inserted */ + /** -# calculate the size of the command to be inserted */ sizeDiff = minLen - inst->cursor; - /* copy the found part into the buffer and display it */ + /** -# copy the found part into the buffer and display it */ insertChars(inst, &(tempCmd[inst->cursor]), sizeDiff); - /* on exact match there is no need to double Tab to display all */ + /** -# on exact match there is no need to double Tab to display all */ if(true == exactMatch) { inst->tabCounter = 0u; @@ -593,8 +731,82 @@ static void doAutocomplete(shellmatta_instance_t *inst) } /** - * @brief initialize the shellmatta terminal and provide all callbacks + * @brief prints all possible commands with description and usage + * @param[in] handle handle shellmatta instance handle + * @param[in] arguments not used here + * @param[in] length not used here + * @return #SHELLMATTA_OK + * #SHELLMATTA_ERROR (buffer overflow) + */ +static shellmatta_retCode_t helpCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length) +{ + shellmatta_retCode_t ret = SHELLMATTA_OK; + shellmatta_instance_t *inst = (shellmatta_instance_t*) handle; + shellmatta_cmd_t *cmd = inst->cmdList; + size_t maxCmdLen = 0u; + size_t maxCmdAliasLen = 0u; + size_t maxCmdHelpLen = 0u; + size_t cmdLen = 0u; + size_t cmdAliasLen = 0u; + size_t cmdHelpLen = 0u; + uint32_t tabCnt = 0u; + static const char tabBuffer[] = { ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' '}; + + /** -# loop through all commands to determine the lengths of each cmd */ + while(NULL != cmd) + { + maxCmdLen = SHELLMATTA_MAX(maxCmdLen, strlen(cmd->cmd)); + maxCmdAliasLen = SHELLMATTA_MAX(maxCmdAliasLen, strlen(cmd->cmdAlias)); + maxCmdHelpLen = SHELLMATTA_MAX(maxCmdHelpLen, strlen(cmd->helpText)); + cmd = cmd->next; + } + + /** -# loop through all commands and print all possible information */ + cmd = inst->cmdList; + while(NULL != cmd) + { + /** -# determine the length of each field to add padding */ + cmdLen = strlen(cmd->cmd); + cmdAliasLen = strlen(cmd->cmdAlias); + cmdHelpLen = strlen(cmd->helpText); + + shellmatta_printf(handle, "%s ", cmd->cmd); + tabCnt = (maxCmdLen - cmdLen); + SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write); + shellmatta_printf(handle, "%s ", cmd->cmdAlias); + tabCnt = (maxCmdAliasLen - cmdAliasLen); + SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write); + shellmatta_printf(handle, "%s ", cmd->helpText); + tabCnt = (maxCmdHelpLen - cmdHelpLen); + SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write); + shellmatta_printf(handle, "%s\r\n", cmd->usageText); + + cmd = cmd->next; + } + + (void)arguments; + (void)length; + + return ret; +} +shellmatta_cmd_t helpCmd = {"help", "h", "Print this help text", "help", helpCmdFct, NULL}; + +/* interface functions */ + +/** + * @} + * @addtogroup shellmatta_api + * @{ + */ + +/** + * @brief initialize the shellmatta terminal and provide all callbacks * @param[in,out] inst pointer to a shellmatta instance + * @param[out] handle pointer to shellmatta handle - + * has to be used for all furterh api calls * @param[in] buffer pointer to the input buffer to use * @param[in] bufferSize size of the provided input buffer * @param[in] historyBuffer pointer to the history buffer to use @@ -603,222 +815,302 @@ static void doAutocomplete(shellmatta_instance_t *inst) * 0 in case of no history buffer * @param[in] prompt pointer to prompt string - is printed * after each command + * @param[in] cmdList constant command list if no dynamic + * adding of commands is desired * @param[in] writeFct function pointer to output function */ shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t *inst, - uint8_t *buffer, + shellmatta_handle_t *handle, + char *buffer, uint32_t bufferSize, - uint8_t *historyBuffer, + char *historyBuffer, uint32_t historyBufferSize, const char *prompt, + const shellmatta_cmd_t *cmdList, shellmatta_write_t writeFct) { - /** - copy all provided buffers into the shellmatta instance */ - inst->buffer = buffer; - inst->bufferSize = bufferSize; - inst->inputCount = 0u; - inst->cursor = 0u; - inst->cmdList = NULL; - inst->historyBuffer = historyBuffer; - inst->historyBufferSize = historyBufferSize; - inst->historyStart = 0u; - inst->historyEnd = 0u; - inst->historyRead = 0u; - inst->historyReadUp = true; - inst->write = writeFct; - inst->prompt = prompt; - inst->echoEnabled = true; - inst->dirty = false; - inst->tabCounter = 0u; - inst->escapeCounter = 0u; - inst->mode = SHELLMATTA_MODE_INSERT; + /** -# check parameters for plausibility */ + if( (NULL != inst) + && (NULL != handle) + && (NULL != buffer) + && (0u != bufferSize) + && (NULL != prompt) + && (NULL != writeFct) + && ((NULL != historyBuffer) || (0u == historyBufferSize))) + { + /** -# copy all provided buffers into the shellmatta instance */ + inst->buffer = buffer; + inst->bufferSize = bufferSize; + inst->inputCount = 0u; + inst->cursor = 0u; + inst->historyBuffer = historyBuffer; + inst->historyBufferSize = historyBufferSize; + inst->historyStart = 0u; + inst->historyEnd = 0u; + inst->historyRead = 0u; + inst->historyReadUp = true; + inst->write = writeFct; + inst->prompt = prompt; + inst->echoEnabled = true; + inst->dirty = false; + inst->tabCounter = 0u; + inst->escapeCounter = 0u; + inst->mode = SHELLMATTA_MODE_INSERT; + inst->cmdList = &helpCmd; + inst->cmdListIsConst = false; - terminateInput(inst); + if(NULL != cmdList) + { + helpCmd.next = (shellmatta_cmd_t *) cmdList; + inst->cmdListIsConst = true; + } + + inst->magic = SHELLMATTA_MAGIC; + *handle = (shellmatta_handle_t)inst; + + /** -# print the first prompt */ + terminateInput(inst); + } return SHELLMATTA_OK; } /** - * @brief adds a command to the command list alphabetically ordered - * @param[in] inst pointer to a shellmatta instance + * @brief adds a command to the command list alphabetically ordered + * @param[in] handle shellmatta instance handle * @param[in] cmd pointer to the command to add type #shellmatta_cmd_t + * @return errorcode #SHELLMATTA_OK + * #SHELLMATTA_USE_FAULT (param err) + * SHELLMATTA_DUPLICATE */ -shellmatta_retCode_t shellmatta_addCmd(shellmatta_instance_t *inst, shellmatta_cmd_t *cmd) +shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cmd_t *cmd) { - shellmatta_cmd_t *tempCmd = inst->cmdList; - shellmatta_cmd_t **prevCmd = &inst->cmdList; + shellmatta_instance_t *inst = (shellmatta_instance_t*)handle; + shellmatta_cmd_t *tempCmd; + shellmatta_cmd_t **prevCmd; bool cmdPlaced = false; shellmatta_retCode_t ret = SHELLMATTA_OK; int cmdDiff = 0; int aliasDiff = 0; - /* register first command as list entry */ - if (NULL == tempCmd) + /** -# check parameters for plausibility */ + if( (NULL != inst) + && (SHELLMATTA_MAGIC == inst->magic) + && (false == inst->cmdListIsConst)) { - inst->cmdList = cmd; - cmd->next = NULL; + tempCmd = inst->cmdList; + prevCmd = &inst->cmdList; + /** -# register first command as list entry */ + if (NULL == tempCmd) + { + inst->cmdList = cmd; + cmd->next = NULL; + } + /** -# append the new command sorted */ + else + { + while ((false == cmdPlaced) && (SHELLMATTA_OK == ret)) + { + cmdDiff = strcmp(tempCmd->cmd, cmd->cmd); + aliasDiff = strcmp(tempCmd->cmdAlias, cmd->cmdAlias); + /** -# check for a duplicate command */ + if((0u == cmdDiff) || (0u == aliasDiff)) + { + ret = SHELLMATTA_DUPLICATE; + } + else if(0 < cmdDiff) + { + cmd->next = tempCmd; + *prevCmd = cmd; + cmdPlaced = true; + } + else if(NULL == tempCmd->next) + { + tempCmd->next = cmd; + cmd->next = NULL; + cmdPlaced = true; + } + else + { + /* nothing to do */ + } + prevCmd = &tempCmd; + tempCmd = tempCmd->next; + } + } } - /* append the new command sorted */ else { - while ((false == cmdPlaced) && (SHELLMATTA_OK == ret)) + ret = SHELLMATTA_USE_FAULT; + } + + return ret; +} + +/** + * @brief processes the passed amount of data + * @param[in] handle shellmatta instance handle + * @param[in] data pointer to input data to process + * @param[in] size length of input data to process + * @return errorcode #SHELLMATTA_OK + * #SHELLMATTA_USE_FAULT + */ +shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle, + char *data, + uint32_t size) +{ + shellmatta_cmd_t *cmd; + uint8_t cmdExecuted = 0u; + shellmatta_retCode_t ret = SHELLMATTA_OK; + shellmatta_instance_t *inst = (shellmatta_instance_t*)handle; + + /** -# check parameters for plausibility */ + if( (NULL != inst) + && (SHELLMATTA_MAGIC == inst->magic)) + { + + /** -# process byte wise */ + for (uint32_t i = 0u; i < size; i++) { - cmdDiff = strcmp(tempCmd->cmd, cmd->cmd); - aliasDiff = strcmp(tempCmd->cmdAlias, cmd->cmdAlias); - /* check for a duplicate command */ - if((0u == cmdDiff) || (0u == aliasDiff)) + /** -# handle escape sequences */ + if(inst->escapeCounter != 0u) { - ret = SHELLMATTA_DUPLICATE; + handleEscapeSequence(inst, *data); } - else if(0 < cmdDiff) + /** -# handle return as start of processing the command */ + else if ('\r' == *data) { - cmd->next = tempCmd; - *prevCmd = cmd; - cmdPlaced = true; + cmd = inst->cmdList; + inst->buffer[inst->inputCount] = 0u; + + /** -# store the current command and reset the history buffer */ + inst->dirty = true; + storeHistoryCmd(inst); + resetHistoryBuffer(inst); + + /** -# search for a matching command */ + while (NULL != cmd) + { + if ( (0 == strcmp(inst->buffer, cmd->cmd)) + || (0 == strcmp(inst->buffer, cmd->cmdAlias))) + { + inst->write("\r\n", 2u); + + cmdExecuted = 1u; + cmd->cmdFct(inst, inst->buffer, inst->inputCount); + cmd = NULL; + } + else + { + cmd = cmd->next; + } + } + + if ((cmdExecuted == 0u) && (inst->inputCount > 0)) + { + inst->buffer[inst->inputCount] = '\0'; + shellmatta_printf( inst, + "\r\nCommand: %s not found", + inst->buffer); + } + terminateInput(inst); } - else if(NULL == tempCmd->next) + /** -# check for tabulator key - auto complete */ + else if('\t' == *data) { - tempCmd->next = cmd; - cmd->next = NULL; - cmdPlaced = true; + inst->dirty = true; + doAutocomplete(inst); + } + /** -# check for cancel - + * terminate current input and print prompt again */ + else if(3 == *data) + { + inst->dirty = false; + resetHistoryBuffer(inst); + terminateInput(inst); + } + /** -# check for backspace */ + else if('\b' == *data) + { + inst->dirty = true; + removeChars(inst, 1u, true); + } + /** -# check for delete key */ + else if(0x7eu == *data) + { + inst->dirty = true; + removeChars(inst, 1u, false); + } + /** -# check for start of escape sequence */ + else if('\e' == *data) + { + inst->escapeCounter = 1u; } else { - /* nothing to do */ + inst->dirty = true; + insertChars(inst, data, 1); } - prevCmd = &tempCmd; - tempCmd = tempCmd->next; + + /** -# reset tab counter on not a tab */ + if ('\t' != *data) + { + inst->tabCounter = 0u; + } + + data ++; } } + else + { + ret = SHELLMATTA_USE_FAULT; + } return ret; } - -void shellmatta_doTask(shellmatta_instance_t *inst, uint32_t time) -{ - -} - /** - * @brief processes the passed amount of data - * @param[in] inst pointer to a shellmatta instance - * @param[in] data pointer to input data to process - * @param[in] size length of input data to process + * @brief printf like function to print output to the instances output + * @param[in] handle shellmatta instance handle + * @param[in] fmt format string and parameters + * @return errorcode #SHELLMATTA_OK + * #SHELLMATTA_USE_FAULT (no valid instance) + * #SHELLMATTA_ERROR (buffer overflow or format err) */ -void shellmatta_processData(shellmatta_instance_t *inst, - char *data, - uint32_t size) +shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle, + const char *fmt, + ...) { - /* process byte wise */ - for (uint32_t i = 0u; i < size; i++) + char outputBuffer[SHELLMATTA_OUTPUT_BUFFER_SIZE]; + va_list arg; + int length; + shellmatta_retCode_t ret = SHELLMATTA_OK; + shellmatta_instance_t *inst = (shellmatta_instance_t*)handle; + + /** -# check parameters for plausibility */ + if( (NULL != inst) + && (SHELLMATTA_MAGIC == inst->magic)) { - /* handle escape sequences */ - if(inst->escapeCounter != 0u) - { - handleEscapeSequence(inst, data); - } - /* handle return as start of processing the command */ - else if ('\r' == *data) - { - shellmatta_cmd_t *cmd = inst->cmdList; - uint8_t cmdExecuted = 0u; - inst->buffer[inst->inputCount] = 0u; + va_start(arg, fmt); + length = vsnprintf(outputBuffer, SHELLMATTA_OUTPUT_BUFFER_SIZE, fmt, arg); + va_end(arg); - /* store the current command and reset the history buffer */ - inst->dirty = true; - storeHistoryCmd(inst); - resetHistoryBuffer(inst); - - while (NULL != cmd) - { - if ( (0 == strcmp(inst->buffer, cmd->cmd)) - || (0 == strcmp(inst->buffer, cmd->cmdAlias))) - { - inst->write("\n\r", 2u); - - cmdExecuted = 1u; - char *blubb[10]; - blubb[0] = inst->buffer; - cmd->cmdFct(1, blubb); - cmd = NULL; - } - else - { - cmd = cmd->next; - } - } - - if ((cmdExecuted == 0u) && (inst->inputCount > 0)) - { - inst->buffer[inst->inputCount] = '\0'; - shellmatta_printf( inst, - "\n\rCommand: %s not found", - inst->buffer); - } - terminateInput(inst); - } - /* tabulator key - auto complete */ - else if('\t' == *data) + if(length < 0) { - inst->dirty = true; - doAutocomplete(inst); - } - /* cancel - terminate current input and print prompt again */ - else if(3 == *data) - { - inst->dirty = false; - resetHistoryBuffer(inst); - terminateInput(inst); - } - /* backspace */ - else if('\b' == *data) - { - inst->dirty = true; - removeChars(inst, 1u, true); - } - /* check for delete key */ - else if(0x7eu == *data) - { - inst->dirty = true; - removeChars(inst, 1u, false); - } - /* start of escape sequence */ - else if('\e' == *data) - { - inst->escapeCounter = 1u; + ret = SHELLMATTA_ERROR; } else { - inst->dirty = true; - insertChars(inst, data, 1); + inst->write(outputBuffer, length); } - - /* reset tab counter on not a tab */ - if ('\t' != *data) - { - inst->tabCounter = 0u; - } - - data ++; } -} - -void shellmatta_printf(shellmatta_instance_t *inst, const char *fmt, ...) -{ - char outputBuffer[1024u]; - va_list arg; - - va_start(arg, fmt); - int length = vsnprintf(outputBuffer, 1024, fmt, arg); - va_end(arg); - - inst->write(outputBuffer, length); -} - -void shellmatta_getArg(uint32_t cnt, uint8_t *arg) -{ - + else + { + ret = SHELLMATTA_USE_FAULT; + } + return ret; } /** @} */ +/** @} */ +