File: options.c

package info (click to toggle)
perdition 2.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,676 kB
  • sloc: ansic: 14,560; sh: 5,274; makefile: 391
file content (125 lines) | stat: -rw-r--r-- 3,273 bytes parent folder | download | duplicates (8)
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
/**********************************************************************
 * options.c                                                   May 2002
 * Horms                                             horms@verge.net.au
 *
 * Parse command line arguments
 * Code based on man getopt(3), later translated to popt.
 * Some code based on man popt(3)
 *
 * perdition
 * Mail retrieval proxy server
 * Copyright (C) 1999-2005  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 **********************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>

#include "options.h"

#ifdef DMALLOC
#include <dmalloc.h>
#endif


/**********************************************************************
 * makebdb_options
 * parse command line arguments
 **********************************************************************/

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

  static struct poptOption options[] =
  {
    {"help",  'h', POPT_ARG_NONE, NULL, 'h', NULL, NULL},
    {"undo",  'u', POPT_ARG_NONE, NULL, 'u', NULL, NULL},
    {NULL,    0,   0,             NULL, 0  , NULL, NULL}
  };


  /*Defaults*/
  memset(&opt, 0, sizeof(opt));

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

  context= poptGetContext("perdition", argc, (const char **)argv, options, 0);

  while ((c=poptGetNextOpt(context)) >= 0){
    optarg=(char *)poptGetOptArg(context);
    switch (c){
      case 'h':
        usage(0);
        break;
      case 'u':
        opt.undo=1;
        break;
      }
  }

  if (c < -1) {
    fprintf(
      stderr, 
      "options: %s: %s\n",
      poptBadOption(context, POPT_BADOPTION_NOALIAS),
      poptStrerror(c)
    );
  }
  
  opt.mapname = (char *)poptGetArg(context);
  if((opt.mapname == NULL) || !(poptPeekArg(context) == NULL)){
    usage(-1);
  }

  return(opt);
}


/**********************************************************************
 * usage
 * Display usage information and exit
 * Prints to stdout if exit_status=0, stderr otherwise
 **********************************************************************/

void usage(int exit_status){
  FILE *stream;

  if(exit_status!=0){
     stream=stderr;
  }
  else{
     stream=stdout;
  }

    
  fprintf(
    stream, 
    "perdition version " VERSION " Copyright Horms\n"
    "\n"
    "Usage: makebdb [options] bdbname\n"
    "   options: -h, --help: print this message\n"
    "            -u, --undo: print content of database file, one entry a line\n"
  );

  exit(exit_status);
}