/*****************************************************************************
**
** parse_cl.c
**
** Sat Mar  7 11:57:10 1998
** mike@cathmike (Mike Borella)
**
** Main command line parsing routines
**
** Automatically created by genparse v0.2.2
**
** See http://www.xnet.com/~cathmike/MSB/Software/ for details
**
*****************************************************************************/


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
#include "getopt.h"
#endif

#include "parse_cl.h"

/*----------------------------------------------------------------------------
 *
 * usage()
 *
 *----------------------------------------------------------------------------
 */

void usage(char *exc)
{
  printf("usage: %s [ -ciplnt ]\n", exc);
  printf("  [ -c ] ");
  printf("[ --count ] ");
  printf("Exit after receiving 'count' packets");
  printf("\n");
  printf("  [ -i ] ");
  printf("[ --interface ] ");
  printf("Listen on interface 'interface'");
  printf("\n");
  printf("  [ -p ] ");
  printf("Dump packet payloads");
  printf("\n");
  printf("  [ -l ] ");
  printf("Don't print link-layer headers");
  printf("\n");
  printf("  [ -n ] ");
  printf("Don't print network-layer headers");
  printf("\n");
  printf("  [ -t ] ");
  printf("Don't print transport-layer headers");
  printf("\n");

  exit(1);
}

/*----------------------------------------------------------------------------
 *
 * free_args()
 *
 *----------------------------------------------------------------------------
 */

void free_args(struct arg_t *my_arg)
{
  if (my_arg->i != NULL) free(my_arg->i);
  free(my_arg);
}

/*----------------------------------------------------------------------------
 *
 * parse_cl()
 *
 *----------------------------------------------------------------------------
 */

struct arg_t *parse_cl(int argc, char *argv[])
{
  extern char *optarg;
  extern int optind;
  int option_index = 0;
  char c;
  struct arg_t *my_arg;
  int errflg = 0;

  static struct option long_options[] =
  {
    {"count", 1, 0, 'c'},
    {"interface", 1, 0, 'i'},
    {"", 1, 0, 'p'},
    {"", 1, 0, 'l'},
    {"", 1, 0, 'n'},
    {"", 1, 0, 't'},
    {0, 0, 0, 0}
  };


  my_arg = (struct arg_t *) malloc (sizeof(struct arg_t));

  my_arg->i = NULL;
  my_arg->p = 0;
  my_arg->l = 0;
  my_arg->n = 0;
  my_arg->t = 0;

  while ((c = getopt_long(argc, argv, "c:i:plnt", long_options, &option_index)) != EOF)
    {
      switch(c)
        {
        case 'c':
          my_arg->c = atoi(optarg);
          if (my_arg->c < 1)
            {
              fprintf(stderr, "parameter range error: c must be >= 1\n");
              errflg ++;
            }
          break;

        case 'i':
          my_arg->i = strdup(optarg);
          break;

        case 'p':
          my_arg->p ++;
          break;

        case 'l':
          my_arg->l ++;
          break;

        case 'n':
          my_arg->n ++;
          break;

        case 't':
          my_arg->t ++;
          break;

        default:
          usage(argv[0]);

        }
    } /* while */

    if (errflg)
      usage(argv[0]);

    if (!(my_arg))
      usage(argv[0]);

  my_arg->optind = optind;
  return my_arg;
}
