Best approach to implement command line

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

Best approach to implement command line

Post by verynewbienoob »

Hello all (for the first time),
So I have implemented working idt,gdt and keyboard queue. I would like to move forward and implement a commands to use like 'clean' 'print' etc.
So I am wondering how to achieve that in proper way.
Waiting for your advices!
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: Best approach to implement command line

Post by austanss »

You have a keyboard queue? Great. You want to print the letters as they come through the queue, and at the same time add the characters to a buffer. When you get the keycode for "enter pressed", print a newline, and parse the input from the buffer. If you don't have a filesystem, you can just create some hardcoded commands. If you do, create executables for these commands and put them in a path of some sort, (/bin on Unix, %PATH% on Windows), then laod the associated executables when you type in a command.

That's my take.
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".
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Best approach to implement command line

Post by bzt »

verynewbienoob wrote:So I am wondering how to achieve that in proper way.
First of all, you'll need a readline library. You can port an existing one, or write one yourself. A very very simple implementation can be found here (about 70 SLoC, replace uart_getc() and printf() with your kernel's functions). This is responsible for getting the user input. Basically it is a line editor, where the user can move the cursor around and can edit and modify the line until they press Enter (it also displays the edited line so that the user can see what they are doing).
Once an Enter is pressed, you should split the line string into a string array (like char **argc). The first element will be the command verb, and the rest its parameters. You could use other command parsing methods of course, but it worth splitting the line in advance.
Finally you dispatch the command verbs (in the first element of the string array) to different functions.

Cheers,
bzt
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Best approach to implement command line

Post by Octocontrabass »

First, you need user mode so your command interpreter can be a standalone program instead of part of your kernel. :wink:

What's best depends on how you want your kernel to work. Do you want your command interpreter to be part of your kernel? Do you want it to be an ordinary program that runs in user mode? Do you have some other idea for how you want to design your OS?
verynewbienoob
Posts: 9
Joined: Mon Nov 30, 2020 6:24 pm
Libera.chat IRC: verynewbienoob

Re: Best approach to implement command line

Post by verynewbienoob »

rizxt wrote:You have a keyboard queue? Great. You want to print the letters as they come through the queue, and at the same time add the characters to a buffer. When you get the keycode for "enter pressed", print a newline, and parse the input from the buffer. If you don't have a filesystem, you can just create some hardcoded commands. If you do, create executables for these commands and put them in a path of some sort, (/bin on Unix, %PATH% on Windows), then laod the associated executables when you type in a command.

That's my take.
I'll go with that way since I've no memory management yet so I can't use readline lib.
Thanks for replies dudes :)
Post Reply