Why dont you simplify a little bit, your requirements ?
Don't use a full parser, its too much complex, and even unnecesary for your case.
Make a loop, write a message that represents yout "prompt", can be the current path you are.
Wait for a string, "parse" the string, and do something depending on the contents of the string.
The string could "parse" like expecting a line, in which spaces are the separators ("tokenizer"), and the rest of characters are grouped.
Example.
The program outputs (and stays in the same line):
/user/files/
The user writes (in the same line)
list all;
Your program will generate a list, collection or array like
list
all;
or, if ";" is considered a separator like spaces
/user/files/
list
all
Your program could start by expecting one single instruction, without unix-style "pipes", neither windowze-style redirection.
Your program could make a dictionary of instructions, each instruction, may have a list of parameters.
The command design pattern applies to your case:
http://en.wikipedia.org/wiki/Command_pattern
This a "plain c" pseudocode, is not tested or finished,
just an idea of how could be done.
You could also make it more object oriented,
and in the programming language, you like.
Example:
// "global function" pointer type declaration
typedef
void (*ActionProc) ();
struct Command
{
char[512] Identifier;
ActionProc Action;
};
// global var declarations
list<char*> CommandList = new list<char*>();
list<char*> Tokens = new list<char*>();
void Action_ListDirectory()
{
// code to list directory
} // Action_ListDirectory()
void Action_ChangeDirectory()
{
// code to change directory
} // Action_ChangeDirectory()
void Action_CreateDirectory()
{
// code to create new directory
} // Action_CreateDirectory()
void PrepareCommandList()
{
CommandList->Add("ls", &Action_ListDirectory);
CommandList->Add("cd", &Action_ChangeDirectory);
CommandList->Add("mkdir", &Action_CreateDirectory);
// register more commands
} // void PrepareCommandList()
void interpret(char* args, int *ArgIndex)
{
char* Separator = " ";
Tokens = YourSeparateInTokensFunction(args, Separator);
// "LocateCommand" may be case sensitive
int AIndex = LocateCommand(CommandList, args[ArgIndex]);
if (AIndex >= 0)
{
// the command
move to the next parameter
*ArgIndex = (*ArgIndex + 1);
// obtain already registered command
Command = CommandList[AIndex];
// execute action
Command.Action();
}
else
{
puts("some kind of command not found error, or, error syntax");
}
} // void interpret()
void main(...)
{
bool CanContinue = false;
char* Prompt = "c\:>";
char Buffer[512];
// which command line parameter string is been processed
int ArgsIndex = 0;
PrepareCommandList();
do
{
// display "prompt"
puts(Prompt);
// wait for user input
fgets(Buffer, sizeof(Buffer), stdin);
interpret(buffer, &ArgsIndex);
} while (CanContinue);
} // void main()
You didn't mention your programming language. You can also mention any programming language, but preferably "XYZ".