Page 1 of 1

Parsing Bootload Command Lines

Posted: Sat May 08, 2010 3:53 am
by TomMan
Hi,

I am currently writing a simple kernel, but I need a way of parsing the boot loader commands that I set with GRUB. I have got to the stage of having a char* with all the commands on one line: but I need to split them up into the individual commands. I tried writing my own strtok function but couldn't get very far, so if someone could help I would be very grateful.

Re: Parsing Bootload Command Lines

Posted: Sat May 08, 2010 5:42 am
by gerryg400
If you want to spend more time having fun writing your OS and less time writing code that already exists, my advice is to get an open source c library and use it as much as possible.

Newlib is good, I use it in my kernel. The wiki has instructions on building it. You could also try PDCLIB which is smaller and perhaps more easily understood.

Of course some people enjoy writing their own c lib. It depends on your goal.

- gerryg400

Re: Parsing Bootload Command Lines

Posted: Sat May 08, 2010 5:43 am
by qw
This is how ReactOS does it: __getmainargs().

Re: Parsing Bootload Command Lines

Posted: Sat May 08, 2010 5:49 am
by NickJohnson
If you don't care about modifying that command line string, you can parse it pretty easily with a single for loop. The idea is to have an array of char* that end up pointing to each token in the string, and to change all whitespace characters to null characters to null terminate the tokens. Start with the first token as the entire string; just go along the string, and when you encounter whitespace, replace it with a null, and add a pointer to the next character to the token array. You will end up with an argv[]-style token list. strtok is a better way to do this, but if you can't implement it, this algorithm will work in a pinch.

Re: Parsing Bootload Command Lines

Posted: Sat May 08, 2010 5:53 am
by Gigasoft
Seriously, if you're up to writing an OS, shouldn't you at least know how to split up a string first?