Page 1 of 1

Best approach to implement command line

Posted: Mon Nov 30, 2020 6:31 pm
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!

Re: Best approach to implement command line

Posted: Mon Nov 30, 2020 6:45 pm
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.

Re: Best approach to implement command line

Posted: Mon Nov 30, 2020 7:12 pm
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

Re: Best approach to implement command line

Posted: Mon Nov 30, 2020 7:58 pm
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?

Re: Best approach to implement command line

Posted: Tue Dec 01, 2020 11:47 am
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 :)