//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/io
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

#include <stdio.h>

#include <libh/io.h>

//-----------------------------------------------------------------------------
// program	test_sub_copy
//
// purpose	Test the sub_copy() function by copying stdin to stdout
//		while substituting each occurrence of ${NAME} with the
//		specified environment variable value.
//
// usage	test_sub_copy < inputfile > outputfile
//-----------------------------------------------------------------------------
int
main ()
{
    char *	value		;
    int		rc		;
    char	name	[80]	;

    for (;;) {
	rc = sub_copy( stdin, stdout, name, 80, "${", "}" );
	if ( rc <= 0 ) {
	    break;
	}
	else if ( rc == 1 ) {
	    value = getenv( name );
	    if ( value ) {
		fputs( value, stdout );
	    } else {
		fputs( name, stdout );
	    }
	}
	else if ( rc == 2 ) {
	    fputs( name, stdout );
	}
	else {
	    fputs( "<unknown return code>", stdout );
	}
    }

    return 0;
}

