Page 1 of 1
Command interpretation in simple cli
Posted: Fri Dec 04, 2020 5:26 pm
by verynewbienoob
Hello again,
This time I am wondering whether creating a structure like:
Code: Select all
struct commands
{
char command = "example"
(*func_ptr[option])();
}
And then making a linked list with all my desired commands is not an overkill ? I would have then to create a for and compare every element of linked list with my command buffer string to execute some command.
On the other hand I am also thinking about simple if if if if...
Thanks for advices.
Re: Command interpretation in simple cli
Posted: Fri Dec 04, 2020 5:34 pm
by austanss
no need for struct, just a simple function that uses the function pointer, that's all
Re: Command interpretation in simple cli
Posted: Fri Dec 04, 2020 6:16 pm
by Octocontrabass
verynewbienoob wrote:Code: Select all
struct commands
{
char command = "example"
(*func_ptr[option])();
}
Your struct looks a bit funny. Is that supposed to be "char *" instead of "char"? What kind of function pointer declaration is that?
verynewbienoob wrote:linked list
Will you be adding or removing commands at runtime? If yes, a binary search tree will have better performance while using a similar amount of memory. If no, a sorted array and a binary search will have better performance while using a similar amount of memory.
Re: Command interpretation in simple cli
Posted: Fri Dec 04, 2020 6:23 pm
by verynewbienoob
Octocontrabass wrote:verynewbienoob wrote:Code: Select all
struct commands
{
char command = "example"
(*func_ptr[option])();
}
Your struct looks a bit funny. Is that supposed to be "char *" instead of "char"? What kind of function pointer declaration is that?
verynewbienoob wrote:linked list
Will you be adding or removing commands at runtime? If yes, a binary search tree will have better performance while using a similar amount of memory. If no, a sorted array and a binary search will have better performance while using a similar amount of memory.
Yes, that is supposed to be pointer to char.
Re: Command interpretation in simple cli
Posted: Fri Dec 04, 2020 7:13 pm
by klange
In my kernel debug shell I
keep an array of names/commands/help text, and when the shell first starts I
load it into a hash map so that commands can be looked up by name. This makes
processing commands straightforward. The hashmap is overkill, but it allows for easy runtime addition of more commands.