Skip to content

Commit 03868b0

Browse files
committed
Add first docs
1 parent e49a539 commit 03868b0

File tree

10 files changed

+111
-40
lines changed

10 files changed

+111
-40
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ It targets communication with embedded systems from remote terminal to quickly s
77

88
## Features
99

10-
* Lightweight commands shell for embedded systems
10+
* Lightweight commands shell for embedded systems
1111
* Platform independent and very easy to port
1212
* Development of library under Win32 platform
1313
* Written in C language (C99)
1414
* No dynamic allocation, maximum number of commands assigned at compile time
15+
* Highly configurable
16+
* Simple help-text with `cmd -v` option
1517
* User friendly MIT license
1618

1719
## Contribute

dev/VisualStudio/lwshell_dev.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
</Link>
144144
</ItemDefinitionGroup>
145145
<ItemGroup>
146+
<ClCompile Include="..\..\examples\example_minimal.c" />
146147
<ClCompile Include="..\..\lwshell\src\lwshell\lwshell.c" />
147148
<ClCompile Include="main.c" />
148149
</ItemGroup>

dev/VisualStudio/lwshell_dev.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919
<ClCompile Include="..\..\lwshell\src\lwshell\lwshell.c">
2020
<Filter>Source Files\LWSHELL</Filter>
2121
</ClCompile>
22+
<ClCompile Include="..\..\examples\example_minimal.c">
23+
<Filter>Source Files</Filter>
24+
</ClCompile>
2225
</ItemGroup>
2326
</Project>

dev/VisualStudio/main.c

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <string.h>
44
#include <stdint.h>
55

6+
void example_minimal(void);
7+
68
int32_t
79
addint_cmd(int32_t argc, char** argv) {
810
long long i1, i2;
@@ -68,17 +70,13 @@ shell_output(const char* str, lwshell_t* lw) {
6870
}
6971
}
7072

73+
/* Program entry point */
7174
int
72-
main(int argc, char** argv) {
73-
printf("Program started...\r\n");
74-
printf("ARGC: %d\r\n", argc);
75-
for (size_t i = 0; i < argc; ++i) {
76-
printf("ARGV %d: %s\r\n", i, argv[i]);
77-
}
78-
printf("\r\n");
79-
75+
main(void) {
8076
/* Init library */
8177
lwshell_init();
78+
79+
/* Add optional output function for the purpose of the feedback */
8280
lwshell_set_output_fn(shell_output);
8381

8482
/* Define shell commands */
@@ -88,21 +86,11 @@ main(int argc, char** argv) {
8886
lwshell_register_cmd("subdbl", subdbl_cmd, "Substitute 2 double numbers and prints them");
8987

9088
/* User input to process every character */
91-
printf("Start entering your text and press enter...\r\n");
89+
printf("Start entering your command and press enter...\r\n");
9290
while (1) {
9391
char c = getch();
94-
#if 0
95-
if (c == '\b') {
96-
printf("\b \b");
97-
} else {
98-
printf("%c", c);
99-
}
100-
if (c == '\r') {
101-
printf("\n");
102-
}
103-
#endif
10492

105-
/* Now insert input */
93+
/* Insert input to library */
10694
lwshell_input(&c, 1);
10795
}
10896
return 0;

docs/examples_src/example_minimal.c

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/get-started/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Minimal example code
9797
To verify proper library setup, minimal example has been prepared.
9898
Run it in your main application file to verify its proper execution
9999

100-
.. literalinclude:: ../examples_src/example_minimal.c
100+
.. literalinclude:: ../../examples/example_minimal.c
101101
:language: c
102102
:linenos:
103103
:caption: Absolute minimum example

docs/index.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@ LwSHELL is lightweight dynamic memory manager optimized for embedded systems.
1616
Features
1717
^^^^^^^^
1818

19-
* Written in ANSI C99, compatible with ``size_t`` for size data types
20-
* Platform indenepdent
21-
* Implements simple shell interface targeting embedded systems
19+
* Lightweight commands shell for embedded systems
20+
* Platform independent and very easy to port
21+
22+
* Development of library under Win32 platform
23+
* Written in C language (C99)
24+
* No dynamic allocation, maximum number of commands assigned at compile time
2225
* Highly configurable
23-
* Includes number parsers for integers
24-
* Operating system ready
26+
* Simple help-text with `cmd -v` option
2527
* User friendly MIT license
2628

2729
Requirements
2830
^^^^^^^^^^^^
2931

3032
* C compiler
31-
* Less than ``2kB`` of non-volatile memory
33+
* Less than ``5kB`` of non-volatile memory
3234

3335
Contribute
3436
^^^^^^^^^^

docs/user-manual/how-it-works.rst

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,59 @@
33
How it works
44
============
55

6-
To be added...
6+
This section describes how library works from the basic perspective.
7+
8+
LwSHELL is designed to accept *computer-command-like* input, in format of ``cmdname param1 "param 2 with space"``,
9+
parse it properly and search for function callback that is assigned for specific ``cmdname``.
10+
11+
Library starts processing input line on *line-feed* or *carriage-return* characters.
12+
It splits tokens by space character:
13+
14+
* Tokens must not include ``space`` character or it will be considered as multi-token input
15+
* To use *space* character as token input, encapsulate character in *double-quotes*
16+
17+
Command structure
18+
^^^^^^^^^^^^^^^^^
19+
20+
Every command has assigned dedicated name and must start with it.
21+
Application must take care to input exact command name since commands are case-sensitive, ``mycmd`` is a different command than ``Mycmd``.
22+
23+
Command structure looks like:
24+
25+
* It must start with command name and has at least one (``1``) parameter, eg. ``mycommand``. Command name is counted as first parameter
26+
* It may have additional parameters split with *space* character
27+
* Every input is parsed as string, even if parameter is string
28+
29+
.. tip::
30+
To use space as an input, encapsulate it with *double quotes*, eg. ``mycmd param1 "param 1 has spaces"``
31+
32+
Register command
33+
^^^^^^^^^^^^^^^^
34+
35+
Application must register command(s) to be used by the system.
36+
This can be done using :cpp:func:`lwshell_register_cmd` function which accepts
37+
*command name*, *command function* and optional *command description*
38+
39+
Command description
40+
^^^^^^^^^^^^^^^^^^^
41+
42+
Every command can have assigned its very simple description text, know as *help text*.
43+
Description is later accessible with special command input that has ``2`` parameters in total and second is ``-h``, ``cmdname -h``.
44+
45+
Data output
46+
^^^^^^^^^^^
47+
48+
To properly work with the library, application must input data to process by using :cpp:func:`lwshell_input` function.
49+
Thanks to the library implementation, it is possible to get data feedback and be able to implement OS-like console.
50+
51+
To enable data-output feature, define your output callback function and assign it with :cpp:func:`lwshell_set_output_fn` function.
52+
53+
Data outputs works on:
54+
55+
* Special characters for *carriage return* and *line-feed*
56+
* Special character *backspace* that returns set of characters to implement backspace-like event on your output
57+
* Actual input character printed back for user feedback
58+
* ``cmdname -h`` feature works to print simple help text
759

860
.. toctree::
961
:maxdepth: 2

examples/example_minimal.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <string.h>
2+
#include "lwshell/lwshell.h"
3+
4+
/* Command to get called */
5+
int32_t
6+
mycmd_fn(int32_t argc, char** argv) {
7+
printf("mycmd_fn called. Number of argv: %d\r\n", (int)argc);
8+
for (int32_t i = 0; i < argc; ++i) {
9+
printf("ARG[%d]: %s\r\n", (int)argc, argv[i]);
10+
}
11+
12+
/* Successful execution */
13+
return 0;
14+
}
15+
16+
/* Example code */
17+
void
18+
example_minimal(void) {
19+
const char* input_str = "mycmd param1 \"param 2 with space\"";
20+
21+
/* Init library */
22+
lwshell_init();
23+
24+
/* Define shell commands */
25+
lwshell_register_cmd("mycmd", mycmd_fn, "Adds 2 integer numbers and prints them");
26+
27+
/* User input to process every character */
28+
29+
/* Now insert input */
30+
lwshell_input(input_str, strlen(input_str));
31+
}

lwshell/src/lwshell/lwshell.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,25 @@ typedef struct {
5555
lwshell_cmd_fn fn; /*!< Command function to call on match */
5656
const char* name; /*!< Command name to search for match */
5757
const char* desc; /*!< Command description for help */
58-
#if 0
59-
char cmd_name[LWSHELL_CFG_MAX_CMD_NAME_LEN + 1];
60-
#endif /* 0 */
6158
} lwshell_cmd_t;
6259

6360
/* Array of all commands */
6461
static lwshell_cmd_t cmds[LWSHELL_CFG_MAX_CMDS];
6562
static size_t cmds_cnt;
6663
static lwshell_t shell;
6764

68-
/**
69-
* \brief Get shell from input
70-
*/
65+
/* Get shell instance from input */
7166
#define LWSHELL_GET_LW(lw) ((lw) != NULL ? (lw) : (&shell))
7267

68+
/* Add character to instance */
7369
#define LWSHELL_ADD_CH(lw, ch) do { \
7470
if ((lw)->buff_ptr < (LWSHELL_ARRAYSIZE(lw->buff) - 1)) { \
7571
(lw)->buff[(lw)->buff_ptr] = ch; \
7672
(lw)->buff[++(lw)->buff_ptr] = '\0'; \
7773
} \
7874
} while (0)
7975

76+
/* Reset buffers */
8077
#define LWSHELL_RESET_BUFF(lw) do { \
8178
memset((lw)->buff, 0x00, sizeof((lw)->buff)); \
8279
memset((lw)->argv, 0x00, sizeof((lw)->argv)); \
@@ -187,8 +184,6 @@ prv_parse_input(lwshell_t* lw) {
187184

188185
/**
189186
* \brief Initialize shell interface
190-
* \param[in] out_fn: Output function to print library data.
191-
* Set to `NULL` if not used
192187
* \return \ref lwshellOK on success, member of \ref lwshellr_t otherwise
193188
*/
194189
lwshellr_t

0 commit comments

Comments
 (0)