Tokenization

I have a program that reads the input from keyboard using readline()
. its like a simple shell that reads the input string and acts on the commands being passed.
the strtok() function looks simple but how do i implement it. Assuming the string from input i get is called "reply".

thanks in advance

strtok - C++ Reference

My understanding is you provide the string and a list of delimiters (commas, spaces etc)

char *reply_ptr=reply;
char *token=NULL;

do
{
     token=strtok(reply_ptr,";. \r\n");

     reply_ptr=NULL;

     if (token) { .... }

} while (token);

ok so this is what i have so far:

int
main()
{
const char* prompt = "shell> ";

int bailout = 0;
while \(!bailout\) \{

	char* reply=readline\(prompt\);
       /* token code here */
char *result=NULL;

result=strtok(reply,"; . \r\n");
while (result !=NULL)
reply=NULL;

/* switch case statements here for token[0] */

and i'm guessing that result holds the value of the current token.. right?

does this make any sense...?

My understanding is the token is a string, not a number, switch() takes a number.

Personally I would not do it like that.... :slight_smile:

I would have a loop what reads a logical line and splits it up into basically an argc/argv type array. Then check argv[0] and match it in a table, if it's in the table, the table will have a function pointer to a handler, if not then just fork/exec and the argc/argv is already setup.

When you want to add macro expansion etc, just plonk that in the logical read line.