/*
   <p>Test harness for <i>getopt()</i>.</p>

   <p>This is a program for demonstrating the capabilities of
   extended <i>getopt()</i>. Run it with various combinations of
   options and arguments on the command line to see how
   <i>getopt()</i> works.</p>

   <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>
*/

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

#define OPT_STRING "abc~d~e?f?"    /* Meaning:
                                      -a -b take no arguments
                                      -c -d take mandatory arguments
                                      -e -f take optional arguments
                                   */

#define OPT_CHARS  "-+/"           /* options can begin with - + / */

main(int argc, char **argv)
{
    int ch;

    setprog(argv[0]);
    
    opterrfd = fileno(stdout);     /* errors to STDOUT */
    opterr = 1;                    /* set this to 1 for auto errors */
    optbad = '!';                  /* return ! instead of ? for errors */
    optneed = '~';                 /* mandatory arg marked by ~ */
    optmaybe = '?';                /* optional arg marked by ? */
    optstart = OPT_CHARS;          /* set warning character list */
    optcase = 0;
    
    while ((ch = getopt(argc, argv, OPT_STRING)) != EOF)
    {
        if (ch != 0)
        {
            printf("\n\toption index (optind) after getopt(): %5d\n",
                   optind);
            printf("\t\tgetopt() return value:            %c (%d)\n",
                   ch,ch);
            printf("\t\tchar that begins option (optchar):%c\n",
                   optchar);
            printf("\t\tactual char looked at (optopt):   %c\n",
                   optopt);
            printf("\t\toption argument:                  \"%s\"\n\n",
                   !optarg ? "(null)" : optarg);
        }
        else
        {
            printf("\n\targument index                    %5d\n",
                   optind-1);
            printf("\t\targument: \"%s\"\n", 
                   !argv[optind-1] ? "(null)" : argv[optind-1]);
        }
    }
    
    printf("\n\nArgument classification summary:\n\n");
    optind = 1;                    /* rescan non options */
    
    while ((ch = getarg(argc,argv)) != EOF)
    {
        if (ch == 0)
            printf("\tArgument %5d is an option: %s\n",
                   optind-1,argv[optind-1]);
        if (ch)
            printf("\tNon-option argument %5d  : %s\n",
                   optind-1, optarg);
    }
    printf("\n");
        
    return(0);
}


