On Sat, 2008-08-23 at 05:03 -0400, Alfred M. Szmidt wrote:
> +static struct argp_option argp_options[] = {
> +#define GRP 0
> + {"debug", 'D', "LEVEL", OPTION_ARG_OPTIONAL, "Set debugging level",
> GRP+1},
> + {"authmode", 'a', "AUTHMODE", 0, "Specify what mode to use for "
> + "authentication", GRP+1},
> + {"exec-login", 'E', "STRING", 0, "Set program to be executed instead "
> + "of /bin/login", GRP+1},
> + {"no-hostinfo", 'h', NULL, 0, "Do not print host information before
> login "
> + "has been completed", GRP+1},
> + {"linemode", 'l', "MODE", OPTION_ARG_OPTIONAL, "Set line mode", GRP+1},
> + {"no-keepalive", 'n', NULL, 0, "Disable TCP keep-alives", GRP+1},
> + {"reverse-lookup", 'U', NULL, 0, "Refuse connections from addresses
> that "
> + "cannot be mapped back into a symbolic name", GRP+1},
> +#ifdef AUTHENTICATION
> + {"disable-auth-type", 'X', "AUTHTYPE", 0, "Disable the use of given "
> + "authentication option", GRP+1},
> +#endif
>
> This is a bad idea, the option should exist even if maybe it is not
> supported. If it is not supported, then we should get a error message
> saying so.
>
Fixed.
My first thought was printing a warning and continuing to run, but I
believe it doesn't make any sense to say that we won't do what you want,
but doing it our way!. Now it prints an error message in argp's way and
aborts.
* telnetd/telnetd.c: Include <argp.h>. Remove include for
<getopt.h>.
(ARGP_PROGRAM_DATA): Call macro.
(argp, args_doc, doc, argp_options): New variables.
(parse_opt): New function.
(telnetd_version, telnetd_license, telnetd_help): Functions
removed.
(short_options, long_options): Variables removed.
(parse_debug_level): Fixed message formatting.
(main): Use argp to parse program
options.
Updated patch is attached.
Index: telnetd/telnetd.c
===================================================================
RCS file: /sources/inetutils/inetutils/telnetd/telnetd.c,v
retrieving revision 1.38
diff -u -p -r1.38 telnetd.c
--- telnetd/telnetd.c 29 Jun 2007 16:29:29 -0000 1.38
+++ telnetd/telnetd.c 24 Aug 2008 08:16:21 -0000
@@ -19,35 +19,12 @@
#include "telnetd.h"
-#include <getopt.h>
-
#ifdef HAVE_SYS_UTSNAME_H
# include <sys/utsname.h>
#endif
#include <libinetutils.h>
+#include <argp.h>
-static char short_options[] = "a:D::d:E:HhLl::nS:Uu:VX:";
-
-static struct option long_options[] = {
- /* Help options */
- {"version", no_argument, NULL, 'V'},
- {"license", no_argument, NULL, 'L'},
- {"help", no_argument, NULL, 'H'},
- /* Common options */
- {"authmode", required_argument, NULL, 'a'},
- {"debug", optional_argument, NULL, 'D'},
- {"exec-login", required_argument, NULL, 'E'},
- {"no-hostinfo", no_argument, NULL, 'h'},
- {"linemode", optional_argument, NULL, 'l'},
- {"no-keepalive", no_argument, NULL, 'n'},
- {"reverse-lookup", no_argument, NULL, 'U'},
- {"disable-auth-type", required_argument, NULL, 'X'},
- {NULL, 0, NULL, 0}
-};
-
-static void telnetd_version (void);
-static void telnetd_license (void);
-static void telnetd_help (void);
static void parse_authmode (char *str);
static void parse_linemode (char *str);
static void parse_debug_level (char *str);
@@ -110,72 +87,91 @@ int SYNCHing; /* we are in TELNET SYNC
struct telnetd_clocks clocks;
char *program_name;
-int
-main (int argc, char **argv)
-{
- int c;
- program_name = argv[0];
- while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
- != EOF)
- {
- switch (c)
- {
- case 'V':
- telnetd_version ();
- exit (0);
-
- case 'L':
- telnetd_license ();
- exit (0);
+ARGP_PROGRAM_DATA ("telnetd", "2008", "FIXME unknown");
- case 'H':
- telnetd_help ();
- exit (0);
+const char args_doc[] = "";
+const char doc[] = "TELNET protocol server.";
- case 'a':
- parse_authmode (optarg);
- break;
-
- case 'D':
- parse_debug_level (optarg);
- break;
-
- case 'E':
- login_invocation = optarg;
- break;
-
- case 'h':
- hostinfo = 0;
- break;
-
- case 'l':
- parse_linemode (optarg);
- break;
-
- case 'n':
- keepalive = 0;
- break;
+static struct argp_option argp_options[] = {
+#define GRP 0
+ {"debug", 'D', "LEVEL", OPTION_ARG_OPTIONAL, "Set debugging level", GRP+1},
+ {"authmode", 'a', "AUTHMODE", 0, "Specify what mode to use for "
+ "authentication", GRP+1},
+ {"exec-login", 'E', "STRING", 0, "Set program to be executed instead "
+ "of /bin/login", GRP+1},
+ {"no-hostinfo", 'h', NULL, 0, "Do not print host information before login "
+ "has been completed", GRP+1},
+ {"linemode", 'l', "MODE", OPTION_ARG_OPTIONAL, "Set line mode", GRP+1},
+ {"no-keepalive", 'n', NULL, 0, "Disable TCP keep-alives", GRP+1},
+ {"reverse-lookup", 'U', NULL, 0, "Refuse connections from addresses that "
+ "cannot be mapped back into a symbolic name", GRP+1},
+ {"disable-auth-type", 'X', "AUTHTYPE", 0, "Disable the use of given "
+ "authentication option", GRP+1},
+#undef GRP
+ {NULL}
+};
- case 'U':
- reverse_lookup = 1;
- break;
+static error_t
+parse_opt (int key, char *arg, struct argp_state *state)
+{
+ switch (key)
+ {
+ case 'a':
+ parse_authmode (arg);
+ break;
+
+ case 'D':
+ parse_debug_level (arg);
+ break;
+
+ case 'E':
+ login_invocation = arg;
+ break;
+
+ case 'h':
+ hostinfo = 0;
+ break;
+
+ case 'l':
+ parse_linemode (arg);
+ break;
+
+ case 'n':
+ keepalive = 0;
+ break;
+
+ case 'U':
+ reverse_lookup = 1;
+ break;
+
+ case 'X':
+#ifdef AUTHENTICATION
+ auth_disable_name (arg);
+#else
+ argp_error (state, "'-%c' is currently not supported on this machine.", key);
+#endif
+ break;
+
+ case ARGP_KEY_ARG:
+ argp_error (state, "junk arguments in the command line.");
+ /* Not reached */
+ break;
+
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+
+ return 0;
+}
-#ifdef AUTHENTICATION
- case 'X':
- auth_disable_name (optarg);
- break;
-#endif
- default:
- fprintf (stderr, "telnetd: unknown command line option: %c\n", c);
- exit (1);
- }
- }
+static struct argp argp = {argp_options, parse_opt, args_doc, doc};
- if (argc != optind)
- {
- fprintf (stderr, "telnetd: junk arguments in the command line\n");
- exit (1);
- }
+int
+main (int argc, char **argv)
+{
+ program_name = argv[0];
+ /* Parse command line */
+ argp_parse (&argp, argc, argv, 0, NULL, NULL);
openlog ("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
telnetd_setup (0);
@@ -265,7 +261,7 @@ parse_debug_level (char *str)
}
if (i == debug_max_mode)
- fprintf (stderr, "telnetd: unknown debug mode: %s", tok);
+ fprintf (stderr, "telnetd: unknown debug mode: %s\n", tok);
}
}
@@ -687,62 +683,6 @@ print_hostinfo ()
free (str);
}
-void
-telnetd_version ()
-{
- printf ("telnetd - %s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
- printf ("Copyright (C) 1998,2001,2002 Free Software Foundation, Inc.\n");
- printf ("%s comes with ABSOLUTELY NO WARRANTY.\n", PACKAGE_NAME);
- printf ("You may redistribute copies of %s\n", PACKAGE_NAME);
- printf ("under the terms of the GNU General Public License.\n");
- printf ("For more information about these matters, ");
- printf ("see the files named COPYING.\n");
-}
-
-void
-telnetd_license ()
-{
- 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 3, 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 ("%s", license_text);
-}
-
-void
-telnetd_help ()
-{
- printf ("\
-Usage: telnetd [OPTION]\n\
-\n\
-Options are:\n\
- -a, --authmode AUTHMODE specify what mode to use for authentication\n\
- -D, --debug[=LEVEL] set debugging level\n\
- -E, --exec-login STRING set program to be executed instead of /bin/login\n\
- -h, --no-hostinfo do not print host information before login has\n\
- been completed\n\
- -l, --linemode[=MODE] set line mode\n\
- -n, --no-keepalive disable TCP keep-alives\n\
- -U, --reverse-lookup refuse connections from addresses that\n\
- cannot be mapped back into a symbolic name\n\
- -X, --disable-auth-type AUTHTYPE\n\
- disable the use of given authentication option\n\
-Informational options:\n\
- -V, --version display this help and exit\n\
- -L, --license display license and exit\n\
- -H. --help output version information and exit\n");
-}
-
int
stop ()
{
_______________________________________________
bug-inetutils mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-inetutils