Coexistence between GNU long options, traditional UNIX-style short options
and traditional MH long options.
Showing
1 changed file
with
115 additions
and
0 deletions
mh/mh_argp.c
0 → 100644
1 | /* GNU mailutils - a suite of utilities for electronic mail | ||
2 | Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | ||
17 | |||
18 | /* Coexistence between GNU long options, traditional UNIX-style short | ||
19 | options and traditional MH long options. */ | ||
20 | |||
21 | #ifdef HAVE_CONFIG_H | ||
22 | # include <config.h> | ||
23 | #endif | ||
24 | |||
25 | #include <mh_getopt.h> | ||
26 | #include <string.h> | ||
27 | |||
28 | struct mh_argp_data | ||
29 | { | ||
30 | struct mh_option *mh_option; | ||
31 | int (*handler)(); | ||
32 | void *closure; | ||
33 | }; | ||
34 | |||
35 | static error_t | ||
36 | parse_opt (int key, char *arg, struct argp_state *state) | ||
37 | { | ||
38 | struct mh_argp_data *data = state->input; | ||
39 | switch (key) | ||
40 | { | ||
41 | case ARGP_KEY_INIT: | ||
42 | while ((key = mh_getopt (state->argc, state->argv, data->mh_option)) | ||
43 | != EOF | ||
44 | && key != '?') | ||
45 | { | ||
46 | data->handler (key, mh_optarg, data->closure); | ||
47 | } | ||
48 | state->next = mh_optind; | ||
49 | break; | ||
50 | |||
51 | case ARGP_KEY_ARG: | ||
52 | if (arg[0] == '+') | ||
53 | { | ||
54 | data->handler ('f', arg+1, data->closure); | ||
55 | break; | ||
56 | } | ||
57 | return ARGP_ERR_UNKNOWN; | ||
58 | |||
59 | default: | ||
60 | if (data->handler (key, arg, data->closure) == 0) | ||
61 | break; | ||
62 | return ARGP_ERR_UNKNOWN; | ||
63 | } | ||
64 | return 0; | ||
65 | } | ||
66 | |||
67 | int | ||
68 | mh_argp_parse (int argc, char **argv, | ||
69 | struct argp_option *option, | ||
70 | struct mh_option *mh_option, | ||
71 | char *argp_doc, char *doc, | ||
72 | int (*handler)(), void *closure) | ||
73 | { | ||
74 | struct argp argp; | ||
75 | struct mh_argp_data data; | ||
76 | char *p; | ||
77 | |||
78 | program_invocation_name = argv[0]; | ||
79 | p = strrchr (argv[0], '/'); | ||
80 | if (p) | ||
81 | program_invocation_short_name = p+1; | ||
82 | else | ||
83 | program_invocation_short_name = program_invocation_name; | ||
84 | memset (&argp, 0, sizeof (argp)); | ||
85 | argp.options = option; | ||
86 | argp.parser = parse_opt; | ||
87 | argp.args_doc = argp_doc; | ||
88 | argp.doc = doc; | ||
89 | data.mh_option = mh_option; | ||
90 | data.closure = closure; | ||
91 | data.handler = handler; | ||
92 | return argp_parse (&argp, argc, argv, 0, 0, &data); | ||
93 | } | ||
94 | |||
95 | void | ||
96 | mh_license (const char *name) | ||
97 | { | ||
98 | static char license_text[] = | ||
99 | " This program is free software; you can redistribute it and/or modify\n" | ||
100 | " it under the terms of the GNU General Public License as published by\n" | ||
101 | " the Free Software Foundation; either version 2, or (at your option)\n" | ||
102 | " any later version.\n" | ||
103 | "\n" | ||
104 | " This program is distributed in the hope that it will be useful,\n" | ||
105 | " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" | ||
106 | " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" | ||
107 | " GNU General Public License for more details.\n" | ||
108 | "\n" | ||
109 | " You should have received a copy of the GNU General Public License\n" | ||
110 | " along with this program; if not, write to the Free Software\n" | ||
111 | " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"; | ||
112 | printf ("This is %s\n%s", name, license_text); | ||
113 | exit (0); | ||
114 | } | ||
115 |
-
Please register or sign in to post a comment