summaryrefslogtreecommitdiff
path: root/mh/mh_argp.c
blob: 380937264b8faa8229d50cda0ff9280a1a008ab4 (plain)
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
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.

   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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* Coexistence between GNU long options, traditional UNIX-style short
   options and traditional MH long options. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <mh.h>
#include <string.h>
#include <argcv.h>

struct mh_argp_data
{
  struct mh_option *mh_option;
  int (*handler)();
  void *closure;
};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct mh_argp_data *data = state->input;
  switch (key)
    {
    case ARGP_KEY_INIT:
      while ((key = mh_getopt (state->argc, state->argv, data->mh_option))
	     != EOF
	     && key != '?')
	{
	  data->handler (key, mh_optarg, data->closure);
	}
      state->next = mh_optind;
      break;

    case ARGP_KEY_ARG:
      if (arg[0] == '+')
	{
	  data->handler ('+', arg+1, data->closure);
	  break;
	}
      return ARGP_ERR_UNKNOWN;
	
    default:
      if (data->handler (key, arg, data->closure) == 0)
	break;
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

int
mh_argp_parse (int argc, char **argv,
	       struct argp_option *option,
	       struct mh_option *mh_option,
	       char *argp_doc, char *doc,
	       int (*handler)(), void *closure)
{
  struct argp argp;
  struct mh_argp_data data;
  char *p;

  program_invocation_name = argv[0];
  p = strrchr (argv[0], '/');
  if (p)
    program_invocation_short_name = p+1;
  else
    program_invocation_short_name = program_invocation_name;

  mh_init ();
  
  memset (&argp, 0, sizeof (argp));
  argp.options = option;
  argp.parser = parse_opt;
  argp.args_doc = argp_doc;
  argp.doc = doc;
  data.mh_option = mh_option;
  data.closure = closure;
  data.handler = handler;

  p = mh_profile_value (program_invocation_short_name, NULL);
  if (p)
    {
      int _argc;
      char **_argv;
      int xargc;
      char **xargv;
      int i;
      
      argcv_get (p, "", NULL, &xargc, &xargv);

      _argc = argc + xargc;
      _argv = calloc (_argc+1, sizeof *_argv);
      if (!_argv)
	{
	  mh_error ("not enough memory");
	  abort ();
	}
      for (i = 0; i < argc; i++)
	_argv[i] = argv[i];
      for (; i < _argc; i++)
	_argv[i] = xargv[i-argc];
      _argv[i] = NULL;
      argp_parse (&argp, _argc, _argv, 0, 0, &data);
      free (_argv);
    }
  else
    argp_parse (&argp, argc, argv, 0, 0, &data);
  return 0;
}

void
mh_license (const char *name)
{
  static char license_text[] =
"   This program is free software; you can redistribute it and/or modify\n"
"   it under the terms of the GNU General Public License as published by\n"
"   the Free Software Foundation; either version 2, or (at your option)\n"
"   any later version.\n"
"\n"
"   This program is distributed in the hope that it will be useful,\n"
"   but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
"   GNU General Public License for more details.\n"
"\n"
"   You should have received a copy of the GNU General Public License\n"
"   along with this program; if not, write to the Free Software\n"
"   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n";
  printf ("This is %s\n%s", name, license_text);
  exit (0);
}

Return to:

Send suggestions and report system problems to the System administrator.