File: options.c

package info (click to toggle)
aggregate 1.0.2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 304 kB
  • ctags: 117
  • sloc: ansic: 1,036; sh: 335; perl: 132; makefile: 83
file content (177 lines) | stat: -rw-r--r-- 5,154 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**********************************************************************
 * options.c                                                 March 2001
 * Horms                                             horms@verge.net.au
 *
 * Read in command line options
 * Code based on man getopt(3), later translated to popt
 *
 * aggregate
 * CIDR network aggregation and filtering
 * Copyright (C) 1999-2002  Horms
 * 
 * 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
 *
 **********************************************************************/

#include "options.h"
#include "cidr_net.h"
#include "log.h"
#include "config.h"
#include <popt.h>
#include <stdlib.h>

options_t opt;


/**********************************************************************
 * options
 * Read in command line options
 * pre: argc: number or elements in argv
 *      argv: array of strings with command line-options
 *      flag: see options.h for flag values
 *            ignores errors otherwise
 * post: global opt is seeded with values according to argc and argv
 **********************************************************************/

int options(int argc, char **argv){
  int c=0;
  char *optarg;
  poptContext context;

  static struct poptOption options[] =
  {
    {"help",           'h', POPT_ARG_NONE,   NULL, 'h'},
    {"maximum-prefix", 'M', POPT_ARG_STRING, NULL, 'M'},
    {"minimim-prefix", 'm', POPT_ARG_STRING, NULL, 'm'},
    {"output_format",  'o', POPT_ARG_STRING, NULL, 'o'},
    {"input_format",   'i', POPT_ARG_STRING, NULL, 'i'},
    {NULL,             0,   0,               NULL, 0  }
  };

  if(argc==0 || argv==NULL) return(0);

  opt.maximum_prefix=DEFAULT_MAXIMUM_PREFIX;
  opt.minimum_prefix=DEFAULT_MINIMUM_PREFIX;
  opt.output_format=DEFAULT_OUTPUT_FORMAT;
  opt.input_format=DEFAULT_INPUT_FORMAT;
  
  context=poptGetContext("aggregate", argc, (char **)argv, options, 0);

  while ((c=poptGetNextOpt(context)) >= 0){
    optarg=poptGetOptArg(context);
    switch (c){
      case 'h':
	usage(0); 
	break;
      case 'M':
        opt.maximum_prefix=(prefix_t)atoi(optarg);
        break;
      case 'm':
        opt.minimum_prefix=(prefix_t)atoi(optarg);
        break;
      case 'o':
        if(strcasecmp("prefix", optarg)==0){
	  opt.output_format=CIDR_NET_PREFIX;
	}
	else if(strcasecmp("netmask", optarg)==0){
	  opt.output_format=CIDR_NET_NETMASK;
	}
	else if(strcasecmp("range", optarg)==0){
	  opt.output_format=CIDR_ADDRESS_RANGE;
	}
	else {
	  AGGREGATE_LOG(LOG_DEBUG, "Invalid output format: %s", optarg);
          usage(-1);
	}
        break;
      case 'i':
        if(strcasecmp("prefix", optarg)==0){
	  opt.input_format=CIDR_NET_PREFIX;
	}
	else if(strcasecmp("netmask", optarg)==0){
	  opt.input_format=CIDR_NET_NETMASK;
	}
	else if(strcasecmp("range", optarg)==0){
	  opt.input_format=CIDR_ADDRESS_RANGE;
	}
	else {
	  AGGREGATE_LOG(LOG_DEBUG, "Invalid input format: %s", optarg);
          usage(-1);
	}
        break;
    }
  }

  if (c < -1) {
    AGGREGATE_LOG(
      LOG_DEBUG,
      "options: %s: %s\n",
      poptBadOption(context, POPT_BADOPTION_NOALIAS),
      poptStrerror(c)
    );
    usage(-1);
  }
  
  poptFreeContext(context);

  return(0);
}


/**********************************************************************
 * usage
 * Display usage information
 * Printed to stdout if exit_status=0, stderr otherwise
 **********************************************************************/

void usage(int exit_status){
  FILE *stream;

  stream=(exit_status)?stderr:stdout;
  
  fprintf(
    stream, 
    "aggregate version %s Copyright Horms\n"
    "\n"
    "aggregate is tool for aggregating CIDR networks\n"
    "\n"
    "Usage: aggregate [options]\n"
    "\n"
    "Options:\n"
    " -h|--help:\n"
    "    Display this message\n"
    " -i|--input-format:\n"
    "    Format for input. One of: prefix, netmask, range.\n"
    "    (default \"prefix\")\n" 
    " -M|--maximum-prefix:\n"
    "    Supress netorks whose prefix is greater than this value.\n"
    "    Valid values 0-32. -1 for no maximum prefix.\n"
    "    (default %d)\n"
    " -m|--minimum-prefix:\n"
    "    Supress netorks whose prefix is less than this value.\n"
    "    Valid values 0-32. -1 for no minimum prefix.\n"
    "    (default %d)\n"
    " -o|--output-format:\n"
    "    Format for output. One of: prefix, netmask, range.\n"
    "    (default \"prefix\")\n",
    VERSION,
    DEFAULT_MAXIMUM_PREFIX,
    DEFAULT_MINIMUM_PREFIX
  );

  fflush(stream);
  exit(exit_status);
}