#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <rtos.h>
#include <kconio.h>

#undef word

char *parse_arg( char * source, int word )
{
    int i, len;
    int curword;
    int inspace;
    char *p, *start, ch;
    char *result;

    curword = 0;

    for ( p = source ; (ch = *p) != 0 ; p++ ) {
        if ( isspace( ch ) ) {
            if ( inspace ) {
                // still in space
            } else {
                // start of space
                inspace = 1;
                // was it the end of the special word
                if ( curword == word )
                    break;
            }
        } else {
            if ( inspace ) {
                // start of new word
                curword ++;
                len = 1;
                start = p;
                inspace = 0;
            } else {
                // 2nd and other characters
                len++;
            }
        }
    }

    if ( curword == word ) {
        result = kcalloc( len + 1 , 1 );
        strncpy( result, start, len );
        result[ len ] = 0;
    } else
        result = NULL;

    return( result );
}


