/*
   <p>Test harness for argparse and getpath().</p> 

                Written by M.C.Gregorie.

                Version history
                    
                1.0    06/07/94    Initial release
                1.1    03/02/02    Ported to Linux

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   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 Lesser 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.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#define VERSION       "1.1"
#define COPYRIGHT     "(c) M.C.Gregorie, 1994"

#include <stdio.h>
#include "environ.h"

#ifdef MSDOS
#include <stdlib.h>
#endif

#ifdef LINUX
#include <stdlib.h>
#include <string.h>
#endif

#define FLAGCNT    3

#define HELP       0        /* -? help */
#define DEBUG      1        /* -d debug mode */
#define FILENAMES  2        /* -z=file of names <names on STDIN> */

#define MAYBEMARK  '%'
#define BADMARK    '!'
#define OK         ' '
#define OPTLIST    "?dz%"

/*
    This function scans the command line for options and does
    all the associated setup.
*/
int readargs(int argc, char *argv[], int flags[], char **files)
{
    int         i, 
                opt;
    static char *nullstr = "";

    /* initialise flags */

    for (i=0 ; i<FLAGCNT ; i++) flags[i] = FALSE;

    /* set up options from the command line */

    optmaybe = MAYBEMARK;
    optbad = BADMARK;
    optind = 1;
    *files = nullstr;
    while ((opt=getopt(argc,argv,OPTLIST)) != EOF)   
    {
        switch (opt)
        {
            case 0:         continue;
            case BADMARK:   return(opt);
            case '?':       flags[HELP] = TRUE;         
                            break;
            case 'd':       flags[DEBUG] = TRUE;
                            break;
            case 'z':       flags[FILENAMES] = TRUE;
                            if (optarg)
                            {
                                *files = (char*)malloc(max(strlen(optarg),1));
                                if (*files == NULL)
                                    error(progname(),
                                          "can't allocate space",
                                          "for -z filename");
                                strcpy(*files,optarg);
                            }
                            break;
            default :       error(progname(),
                                  "impossible happened parsing options",
                                  "");
        }
    }
    return(OK);
}

/*
    Show the punter what the options are
*/
void showhelp()
{
    printf("\ntestargs v%s %s\n\n",VERSION,COPYRIGHT);
    printf("Syntax  : testargs [<opts>] <path...> [<opts>]\n");
    printf("Function: argparse and getpath test harness\n");
    printf("Options :\n");
    printf("     -d           debug mode\n");
    printf("     -z[<=path>]  read path names from stdin or <path> ");
    printf("if given\n");
}

/*
    The main program starts here
    ----------------------------
*/
int main(int argc, char *argv[])
{
    FILE    *pathlist;
    char    *pathsource,
            *inputname;
    int     flags[FLAGCNT],
            pathresult,
            processed;

    setprog(argv[0]);
    
    /*
        Parse and set up command options
    */
    if (readargs(argc,argv,flags,&pathsource) == BADMARK)
        return(1);

    /*
        If help was asked for, give it and quit
    */
    if (flags[HELP]) 
    {
        showhelp();
        return(0);
    }

    /*
        This code sets up for the -z option, which expects a list
        of the files to be processed on stdin or in a named file.
    */
    if (flags[FILENAMES])
    {
    	if (flags[DEBUG])
    	    printf("pathsource is [%s]\n", pathsource);
    	    
        if (strlen(pathsource) == 0)
            pathlist = fdopen(0, "r");
        else
            pathlist = fopen(pathsource,"r");
            
        if (pathlist == NULL) 
            error(argv[0],"Cannot open path list:",pathsource);
    }

    /*
        The following code drives the application. 
    */
    optind = 1;
    processed = 0;

    while (inputname = getpath(argc,argv,pathlist,flags[FILENAMES]))
    {
        if (flags[DEBUG])
            printf("Processing parameter %s\n",inputname);
        processed++;
            
        printf("Parameter %d is %s\n",processed,inputname);
        
    }

    if (!processed)
        error(argv[0],"no file(s) selected","");
    return(0);
}

