#include <mplib1/match_tok.h> int match_string ( const char *line_ptr, const char *match_ptr, char ** next_tok );This function returns a non-zero value if the buffer pointed to by line_ptr contains the string pointed to by match_ptr. It returns zero otherwise. If the match_ptr string starts with a ^ then the string comparision will be against match_ptr+1 and line_ptr.
If the next_tok parameter is non-NULL then value is set to either the next non-space character following a match, or to the line_ptr value.
struct match_token { int m_val; const char *m_ptr; }; int match_a_string( const char *line_ptr, struct match_token *match_ptr, char ** next_tok );This function takes an input line pointed to by the line_ptr parameter and array of match_token structures pointed to by match_ptr which is terminated by an entry with a NULL string pointer, and an optional pointer to a character pointer.
The routine effectively performs a series of match_token calls against each entry in the match_ptr array until it matches and then returns the corresponding m_val. If there is no match then the value returned is that of the terminating match token.
This function can be used to build a simple command parser as illustrated by the following code.
struct match_token my_tokens[] = { { 1, "^help" },. { 2, "^quit" }, { 3, "^details" }, { 0, NULL } }; void use_line( char *line_ptr ) { char *next_cp; switch( match_a_string( line_ptr, my_tokens, &next_cp ) ) { 1 : printf("Help not available\n"); break; 2 : printf("Thank you and goodbye\n"); exit(0); 3 : /* call detail function and pass remainder of line for parameters */ print_details( next_cp ); break; 0 : printf("Unrecognised command \"%s\"\n", line_ptr ); break; } return; }