On Sat, 2008-08-23 at 05:16 -0400, Alfred M. Szmidt wrote: 
> +const char args_doc[] = "f1 ... fn directory";
> 
> Upper case for the arguments.
Fixed.

> 
>    +#ifdef KERBEROS
>    +  {"kerberos", 'K', NULL, 0, "Turns off all Kerberos authentication", 
> GRP+1},
>    +  {"realm", 'k', "REALM", 0, "Obtain tickets for the remote host in REALM"
>    +    "instead of the remote host's realm", GRP+1},
>    +#ifdef CRYPT
>    +  {"encrypt", 'x', NULL, 0, "Encrypt all data using DES", GRP+1},
>    +#endif
>    +#endif
> 
> As before, thse switches should just warn that they are not supported.

Fixed.

As Sergey commented, I packed those two flag variables into a simple
structure. However, I'm not sure if this was our best bet. :-)


        * rcp/rcp.c: Include <argp.h> and <libinetutils/libinetutils.h>.
        Remove include for <getopt.h>.
        (ctrl_flag): New data structure.
        (ARGP_PROGRAM_DATA): Call macro.
        (argp, args_doc, doc, argp_options): New variables.
        (parse_opt): New function.
        (help, usage): Functions removed.
        (short_options, long_options): Variables removed.
        (main): Added new variable INDEX. Use argp to parse program
options.


Index: rcp/rcp.c
===================================================================
RCS file: /sources/inetutils/inetutils/rcp/rcp.c,v
retrieving revision 1.29
diff -u -p -r1.29 rcp.c
--- rcp/rcp.c	3 Sep 2007 20:14:19 -0000	1.29
+++ rcp/rcp.c	25 Aug 2008 17:48:13 -0000
@@ -65,10 +65,11 @@
 #include <string.h>
 #include <string.h>
 #include <unistd.h>
-#include <getopt.h>
+#include <argp.h>
 #ifndef HAVE_UTIMES
 # include <utime.h>		/* If we don't have utimes(), use utime(). */
 #endif
+#include <libinetutils/libinetutils.h>
 
 #include "extern.h"
 
@@ -85,39 +86,13 @@ extern char *krb_realmofhost ();
 
 # ifdef CRYPT
 int doencrypt = 0;
-#  define OPTIONS	"dfKk:prtxV"
-# else
-#  define OPTIONS	"dfKk:prtV"
 # endif
-#else
-# define OPTIONS "dfprtV"
 #endif /* KERBEROS  */
 
 #if !defined (S_ISTXT) && defined (S_ISVTX)
 # define S_ISTXT S_ISVTX
 #endif
 
-static const char *short_options = OPTIONS;
-static struct option long_options[] = {
-  {"recursive", required_argument, 0, 'r'},
-  {"preserve", no_argument, 0, 'p'},
-#ifdef KERBEROS
-  {"kerberos", no_argument, 0, 'K'},
-  {"realm", required_argument, 0, 'k'},
-# ifdef CRYPT
-  {"encrypt", no_argument, 0, 'x'},
-# endif
-#endif
-  {"help", no_argument, 0, 'h'},
-  {"version", no_argument, 0, 'V'},
-  /* Server option.  */
-  {"directory", required_argument, 0, 'd'},
-  {"from", required_argument, 0, 'f'},
-  {"to", required_argument, 0, 't'},
-  {0, 0, 0, 0}
-};
-
-
 struct passwd *pwd;
 u_short port;
 uid_t userid;
@@ -137,84 +112,123 @@ void sink (int, char *[]);
 void source (int, char *[]);
 void tolocal (int, char *[]);
 void toremote (char *, int, char *[]);
-void usage (void);
-void help (void);
 
 char *program_name;
 
-int
-main (int argc, char *argv[])
+struct _ctrl_flag
 {
-  struct servent *sp;
-  int ch, fflag, tflag;
-  char *targ;
-  const char *shell;
+  int fflag;
+  int tflag;
+};
+typedef struct _ctrl_flag ctrl_flag;
 
-  program_name = argv[0];
+ARGP_PROGRAM_DATA ("rcp", "2008", "FIXME unknown");
 
-  fflag = tflag = 0;
-  while ((ch = getopt_long (argc, argv, short_options, long_options, 0))
-	 != EOF)
-    switch (ch)
-      {				/* User-visible flags. */
-      case 'K':
+const char args_doc[] = "F1 ... FN DIRECTORY";
+const char doc[] = "Remote file copy.";
+
+static struct argp_option argp_options[] = {
+#define GRP 0
+  {"recursive", 'r', NULL, 0, "If any of the source files are directories,"
+    "copies each subtree rooted at that name; in this case the destination"
+    "must be a directory", GRP+1},
+  {"preserve", 'p', NULL, 0, "Attempt to preserve (duplicate) in its copies"
+    "the modification times and modes of the source files", GRP+1},
+  {"kerberos", 'K', NULL, 0, "Turns off all Kerberos authentication", GRP+1},
+  {"realm", 'k', "REALM", 0, "Obtain tickets for the remote host in REALM"
+    "instead of the remote host's realm", GRP+1},
+  {"encrypt", 'x', NULL, 0, "Encrypt all data using DES", GRP+1},
+  {"directory", 'd', NULL, OPTION_HIDDEN, NULL, GRP+1},
+  {"from", 'f', NULL, OPTION_HIDDEN, NULL, GRP+1},
+  {"to", 't', NULL, OPTION_HIDDEN, NULL, GRP+1},
+#undef GRP
+  {NULL}
+};
+
+static error_t
+parse_opt (int key, char *arg, struct argp_state *state)
+{
+  ctrl_flag *flags = (ctrl_flag*) (state->input);
+
+  switch (key)
+  {				/* User-visible flags. */
+    case 'K':
 #ifdef KERBEROS
-	use_kerberos = 0;
+      use_kerberos = 0;
+#else
+      argp_error (state, "'-%c' is currently not supported on this machine.",
+	  key);
 #endif
-	break;
+      break;
 
+    case 'k':
 #ifdef	KERBEROS
-      case 'k':
-	dest_realm = dest_realm_buf;
-	strncpy (dest_realm_buf, optarg, REALM_SZ);
-	break;
+      dest_realm = dest_realm_buf;
+      strncpy (dest_realm_buf, arg, REALM_SZ);
+#else
+      argp_error (state, "'-%c' is currently not supported on this machine.",
+	  key);
+#endif
+      break;
 
-# ifdef CRYPT
-      case 'x':
-	doencrypt = 1;
-	/* des_set_key(cred.session, schedule); */
-	break;
-# endif
+#if defined(KERBEROS) && defined(CRYPT)
+    case 'x':
+      doencrypt = 1;
+      /* des_set_key(cred.session, schedule); */
+#else
+      argp_error (state, "'-%c' is currently not supported on this machine.",
+	  key);
 #endif
+      break;
+
+    case 'p':
+      pflag = 1;
+      break;
+
+    case 'r':
+      iamrecursive = 1;
+      break;
+
+      /* Server options. */
+    case 'd':
+      targetshouldbedirectory = 1;
+      break;
+
+    case 'f':		/* "from" */
+      iamremote = 1;
+      flags->fflag = 1;
+      break;
+
+    case 't':		/* "to" */
+      iamremote = 1;
+      flags->tflag = 1;
+      break;
+		
+    default:
+      return ARGP_ERR_UNKNOWN;
+  }
+
+  return 0;
+}
 
-      case 'p':
-	pflag = 1;
-	break;
-
-      case 'r':
-	iamrecursive = 1;
-	break;
-
-	/* Server options. */
-      case 'd':
-	targetshouldbedirectory = 1;
-	break;
-
-      case 'f':		/* "from" */
-	iamremote = 1;
-	fflag = 1;
-	break;
-
-      case 't':		/* "to" */
-	iamremote = 1;
-	tflag = 1;
-	break;
-
-      case 'V':
-	printf ("rcp (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
-	exit (0);
-	break;
-
-      case 'h':
-	help ();
-	break;
-
-      case '?':
-      default:
-	usage ();
-      }
-  argc -= optind;
-  argv += optind;
+static struct argp argp = {argp_options, parse_opt, args_doc, doc};
+
+int
+main (int argc, char *argv[])
+{
+  struct servent *sp;
+  ctrl_flag flags = {0};
+  char *targ;
+  const char *shell;
+  int index = 0;
+
+  program_name = argv[0];
+
+  /* Parse command line */
+  argp_parse (&argp, argc, argv, 0, &index, &flags);
+ 
+  argc -= index;
+  argv += index;
 
 #ifdef KERBEROS
   if (use_kerberos)
@@ -245,7 +259,7 @@ main (int argc, char *argv[])
 
   rem = STDIN_FILENO;		/* XXX */
 
-  if (fflag)
+  if (flags.fflag)
     {				/* Follow "protocol", send data. */
       response ();
       setuid (userid);
@@ -253,7 +267,7 @@ main (int argc, char *argv[])
       exit (errs);
     }
 
-  if (tflag)
+  if (flags.tflag)
     {				/* Receive data. */
       setuid (userid);
       sink (argc, argv);
@@ -261,7 +275,10 @@ main (int argc, char *argv[])
     }
 
   if (argc < 2)
-    usage ();
+  {
+    argp_help (&argp, stderr, ARGP_HELP_USAGE, "rcp");
+    exit (argp_err_exit_status);
+  }
   if (argc > 2)
     targetshouldbedirectory = 1;
 
@@ -1026,58 +1043,6 @@ response ()
     }
 }
 
-void
-usage ()
-{
-#ifdef KERBEROS
-# ifdef CRYPT
-  fprintf (stderr, "%s\n\t%s\n",
-	   "usage: rcp [-Kpx] [-k realm] f1 f2",
-	   "or: rcp [-Kprx] [-k realm] f1 ... fn directory");
-# else
-  fprintf (stderr, "%s\n\t%s\n",
-	   "usage: rcp [-Kp] [-k realm] f1 f2",
-	   "or: rcp [-Kpr] [-k realm] f1 ... fn directory");
-# endif
-#else
-  fprintf (stderr,
-	   "usage: rcp [-p] f1 f2; or: rcp [-pr] f1 ... fn directory\n");
-#endif
-  exit (1);
-}
-
-void
-help ()
-{
-  puts ("rcp - remote file copy.");
-  puts ("usage: rcp [-p] f1 f2; or: rcp [-pr] f1 ... fn directory\n");
-  puts ("\
-  -p, --preserve    attempt to preserve (duplicate) in its copies the\n\
-                    modification times and modes of the source files");
-  puts ("\
-  -r, --recursive   If any of the source files are directories, copies\n\
-                    each subtree rooted at that name; in this case the\n\
-                    destination must be a directory");
-
-#ifdef KERBEROS
-  puts ("\
-  -K, --kerberos    turns off all Kerberos authentication");
-  puts ("\
-  -k, --realm REALM Obtain tickets for the remote host in REALM\n\
-                    instead of the remote host's realm");
-# ifdef CRYPT
-  puts ("\
-  -x, --encrypt     encrypt all data using DES");
-# endif
-#endif
-  puts ("\
-      --help        give this help list");
-  puts ("\
-  -V, --version     print program version");
-  fprintf (stdout, "\nSubmit bug reports to %s.\n", PACKAGE_BUGREPORT);
-  exit (0);
-}
-
 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
 # include <stdarg.h>
 #else
_______________________________________________
bug-inetutils mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-inetutils

Reply via email to