build_argv

The build_argv function is a mechanism to build an argv vector like that provided to main.

    struct argv_track *build_argv_track( struct argv_track *argvtp, ... );
The function takes pointer to an argv_track structure allocated on a previous call to the function or NULL if you require one to be allocated for you, and then a sequence of string pointers terminated by a NULL.

The function duplicates each string, splits it on whitespace, and if the sub-string begins with a '$' or '%' then it is replaced by its evaluation from the environment and configuration space. This is done by a call to eval_config_default( NULL, sub_string ).

Once all the strings have been split and evaluated the routine then constructs an argv array with a terminating NULL pointer.

The routine then sets the argv and argc elements of the tracking structure and returns a pointer to the track structure.

If the routine is unable to allocate an argv_track structure when required the function returns NULL.

    void free_argv_track( struct argv_track *argvtp );
This function frees up the argv_track structure and any associated memory that was allocated during the string duplication.

Example Use

Given a program like
    static void print_argv( int argc, char *argv[] )
    {
	int t=0;
	while (t<argc)
	{
	    printf("argv[%d]=%s\n",t,argv[t]);
	    t++;
	}
	return;
    }

    int main( int argc, char *argv[] )
    {
	struct argv_track *argvtp;
	printf("Original argv\n");
	print_argv( argc, argv );
	set_config_string( "WIDTH", "50" );
	argvtp = build_argv_track( NULL, "$USER %WIDTH", argv[1], NULL );
	printf("New argv\n");
	print_argv( argvtp->argc, argvtp->argv );
	free_argv_track( argvtp );
	return(0);
    }
Would give behave as follows
    $ argv_test this is a test
    Original argv
    argv[0]=argv_test
    argv[1]=this
    argv[2]=is
    argv[3]=a
    argv[4]=test
    New argv
    argv[0]=eric
    argv[1]=50
    argv[2]=this