diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2001-09-14 12:14:36 +0000 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2001-09-14 12:14:36 +0000 |
commit | 98a5981ef013545a0b0bf5b1bc8483438f1bdf43 (patch) | |
tree | 9f6a97a864d3bc89556ecda4a3cf96830a41cea3 /mh/mh_getopt.c | |
parent | 59443de195637d0894ae89575419feeac9502564 (diff) | |
download | mailutils-98a5981ef013545a0b0bf5b1bc8483438f1bdf43.tar.gz mailutils-98a5981ef013545a0b0bf5b1bc8483438f1bdf43.tar.bz2 |
MH-style options parsing.
Diffstat (limited to 'mh/mh_getopt.c')
-rw-r--r-- | mh/mh_getopt.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/mh/mh_getopt.c b/mh/mh_getopt.c new file mode 100644 index 000000000..626821232 --- /dev/null +++ b/mh/mh_getopt.c @@ -0,0 +1,114 @@ +/* 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. */ + +/* Parse traditional MH options. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <mh_getopt.h> +#include <getopt.h> + +int mh_optind = 1; +char *mh_optarg; +char *mh_optptr; + +int +mh_getopt (int argc, char **argv, struct mh_option *mh_opt) +{ + struct mh_option *p; + int optlen; + + if (mh_optind >= argc || argv[mh_optind] == NULL) + return EOF; + mh_optptr = argv[mh_optind]; + if (mh_optptr[0] != '-') + return EOF; + + optlen = strlen (mh_optptr+1); + for (p = mh_opt; p->opt; p++) + { + if (p->match_len <= optlen + && strlen (p->opt) >= optlen + && (memcmp (mh_optptr+1, p->opt, optlen) == 0 + || (p->flags == MH_OPT_BOOL + && optlen > 2 + && memcmp (mh_optptr+3, p->opt, optlen-2) == 0))) + break; + } + + if (p->opt) + { + switch (p->flags) + { + case MH_OPT_BOOL: + if (memcmp (mh_optptr+1, "no", 2) == 0) + mh_optarg = "no"; + else + mh_optarg = "yes"; + break; + + case MH_OPT_ARG: + mh_optarg = argv[++mh_optind]; + break; + + default: + mh_optarg = NULL; + } + mh_optind++; + return p->key; + } + else if (!strcmp (mh_optptr+1, "help")) + { + mh_help (mh_opt); + exit (1); + } + return '?'; +} + +void +mh_help (struct mh_option *mh_opt) +{ + struct mh_option *p; + + printf ("Compatibility syntax:\n"); + printf ("%s [+folder] [msgs] [switches]\n", program_invocation_short_name); + printf (" switches are:\n"); + + for (p = mh_opt; p->opt; p++) + { + int len = strlen (p->opt); + + printf (" -"); + if (p->flags == MH_OPT_BOOL) + printf ("[no]"); + if (len > p->match_len) + printf ("(%*.*s)%s", + p->match_len, p->match_len, p->opt, + p->opt + p->match_len); + else + printf ("%s", p->opt); + + if (p->flags == MH_OPT_ARG) + printf (" %s", p->arg); + printf ("\n"); + } + printf (" -help\n"); +} |