Adding "History" command
based on BDH Shell
Last updated
based on BDH Shell
Last updated
Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University
This tutorial provides a straightforward, step-by-step guide on how to create a basic CLI for the Serial Terminal. Here, you'll find a simple example of CLI creation that will introduce you to the basics of the process. This lab will also walk you through some of the fundamental concepts and techniques involved, ensuring that you understand each stage of the process.
Create New Application
Select Project
You can check the cmd functions in the `usrcmd.c` file.
The CLI (Command Line Interface) provides a way for users to interact with the system through text-based commands. Here is the detail of each function:
cmd_table_t
: This is a struct that includes a command name, a description, and a function that is executed when the command is entered. The CLI (Command Line Interface) provides a way for users to interact with the system through text-based commands. Here is the detail of each function:
cmd_table_t
: This is a struct that includes a command name, a description, and a function that is executed when the command is entered.
cmdlist[]
: This is a list of the custom commands available in the shell. Each command corresponds to a specific function that is executed when the command is entered.
usrcmd_execute()
: This function executes a given command by looking it up in the cmdlist and calling its corresponding function.
usrcmd_ntopt_callback()
: This is the callback function for the command parsing library. It is invoked when a command is entered, and it executes the appropriate function based on the entered command.
Command Functions
: Functions like usrcmd_help(), usrcmd_info(), usrcmd_clear(), etc. are the implementations of the custom commands. They are executed when the corresponding command is entered into the shell.
usrcmd_task()
: This function is where the command line interface starts. It initializes the shell, sets the prompt, and then starts executing commands.
The new commands that we added are usrcmd_history(), usrcmd_reboot(), and usrcmd_kill().
usrcmd_history
Please follow the steps below to add the history command:
Add the following code snippet to define the usrcmd_history function and related variables at the beginning of your usrcmd.c file:
Modify the usrcmd_execute function to store the commands in the history. Replace the existing usrcmd_execute function with the following code snippet:
First, we check if the history_count
is less than the MAX_HISTORY_COUNT
. If so, it means that the history buffer still has space to store new commands.
If there's space, we use strncpy
to copy the command text into the command_history[history_count]
array, with a maximum length of MAX_LINE_LENGTH
. Then, we ensure that the last character in the array is set to the null terminator ('\0'
) to indicate the end of the string. Finally, we increment the history_count
.
If the history_count
is equal to or greater than the MAX_HISTORY_COUNT
, it means that the history buffer is full. In this case, we need to make space for the new command by removing the oldest command.
To remove the oldest command, we use the memmove
function to shift all the commands up by one position. This will overwrite the oldest command and free up space for the new command at the end of the array.
We then use strncpy
again to copy the new command text into the last position of the command_history
array, with a maximum length of * MAX_LINE_LENGTH
. We also ensure that the last character in the array is set to the null terminator ('\0'
**).
Finally, the function calls ntopt_parse
to parse and execute the command.
Add the history command to the cmdlist
array.
Add the following line to the cmdlist array in the usrcmd.c
file:
After making these changes, you should have a working "history" command that prints the list of previously typed commands in the order they were executed. The command history stores up to
MAX_HISTORY_COUNT commands
. When the limit is reached, the oldest command is removed from the history to make room for new commands.