Command interpretation in simple cli

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
verynewbienoob
Posts: 9
Joined: Mon Nov 30, 2020 6:24 pm
Libera.chat IRC: verynewbienoob

Command interpretation in simple cli

Post 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.
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: Command interpretation in simple cli

Post by austanss »

no need for struct, just a simple function that uses the function pointer, that's all
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Command interpretation in simple cli

Post 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.
verynewbienoob
Posts: 9
Joined: Mon Nov 30, 2020 6:24 pm
Libera.chat IRC: verynewbienoob

Re: Command interpretation in simple cli

Post 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.
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: Command interpretation in simple cli

Post 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.
Post Reply