bug-mailutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-mailutils] pick missing the "--" feature for matching arbitrary


From: Sergey Poznyakoff
Subject: Re: [bug-mailutils] pick missing the "--" feature for matching arbitrary components
Date: Sun, 14 Nov 2010 12:21:06 +0200

Hi Joe,

Some time ago we had a conversation regarding the use of
the compatibility syntax for matching arbitrary components
with pick[1].  While hacking on Mailutils 3, I stumbled
upon a solution which, in my opinion, might make the use
of traditional pick syntax easier.  Namely: the option
`--Component pattern' is recognized as a component matching
request if `Component' contains at least one capital letter,
or is followed by a colon.  So, for example, the following
will work:

   refile `pick +inbox \
         -from           address@hidden \
     -or -to             address@hidden \
     -or --Apparently-to address@hidden \
   ` +BALUG   

Attached is a patch which implements this feature in Mailutils 2.x.
Please let me know what you think about it.

Regards,
Sergey

[1] http://lists.gnu.org/archive/html/bug-mailutils/2010-09/msg00036.html

diff --git a/mh/mh_getopt.c b/mh/mh_getopt.c
index 2a6c949..1f07d2a 100644
--- a/mh/mh_getopt.c
+++ b/mh/mh_getopt.c
@@ -33,6 +33,8 @@ static int mh_optind = 1;
 static char *mh_optarg;
 static char *mh_optptr;
 
+void (*mh_help_hook) ();
+
 int
 mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc)
 {
@@ -144,6 +146,8 @@ mh_help (struct mh_option *mh_opt, const char *doc)
        printf (" %s", p->arg);
       printf ("\n");
     }
+  if (mh_help_hook)
+    mh_help_hook ();
   printf ("  -help\n");
   printf ("  -version\n");
   printf (_("\nPlease use GNU long options instead.\n"
diff --git a/mh/mh_getopt.h b/mh/mh_getopt.h
index b930cf3..bd3f1ff 100644
--- a/mh/mh_getopt.h
+++ b/mh/mh_getopt.h
@@ -184,6 +184,8 @@ enum mh_arg {
   ARG_ZERO
 };
 
+extern void (*mh_help_hook) (void);
+
 void mh_argp_init (const char *vers);
 void mh_argv_preproc (int argc, char **argv, struct mh_argp_data *data);
 int mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char 
*doc);
diff --git a/mh/pick.c b/mh/pick.c
index ecf3f8a..518d4b0 100644
--- a/mh/pick.c
+++ b/mh/pick.c
@@ -29,7 +29,17 @@
 
 const char *program_version = "pick (" PACKAGE_STRING ")";
 static char doc[] = N_("GNU MH pick")"\v"
-N_("Use -help to obtain the list of traditional MH options.");
+N_("Compatibility syntax for picking a matching component is:\n\
+\n\
+  --Component pattern\n\
+\n\
+where Component is the component name, containing at least one capital\n\
+letter or followed by a colon, e.g.:\n\
+\n\
+  --User-Agent Mailutils\n\
+  --user-agent: Mailutils\n\
+\n\
+Use -help to obtain a list of traditional MH options.");
 static char args_doc[] = N_("[messages]");
 
 /* GNU options */
@@ -145,8 +155,6 @@ add_sequence (char *name)
 static error_t
 opt_handler (int key, char *arg, struct argp_state *state)
 {
-  char *s, *p;
-  
   switch (key)
     {
     case ARG_FOLDER: 
@@ -259,32 +267,6 @@ opt_handler (int key, char *arg, struct argp_state *state)
       seq_flags &= ~SEQ_ZERO;
       break;
        
-    case ARGP_KEY_ERROR:
-      s = state->argv[state->next - 1];
-      if (memcmp (s, "--", 2))
-       {
-         argp_error (state, _("invalid option -- %s"), s);
-         exit (1);
-       }
-      p = strchr (s, '=');
-      if (p)
-       *p++ = 0;
-       
-      pick_add_token (&lexlist, T_COMP, s + 2);
-
-      if (!p)
-       {
-         if (state->next == state->argc)
-           {
-             mu_error (_("invalid option -- %s"), s);
-             exit (1);
-           }
-         p = state->argv[state->next++];
-       }
-      
-      pick_add_token (&lexlist, T_STRING, p);
-      break;
-
     case ARG_LICENSE:
       mh_license (argp_program_version);
       break;
@@ -319,6 +301,17 @@ action_add (void *item, void *data)
   return 0;
 }
 
+void
+pick_help_hook (void)
+{
+  printf ("\n");
+  printf (_("To match another component, use:\n\n"));
+  printf (_("  --Component pattern\n\n"));
+  printf (_("Note, that the component name must either be capitalized,\n"
+           "or followed by a colon.\n"));
+  printf ("\n");
+}
+
 /* NOTICE: For the compatibility with the RAND MH we have to support
    the following command line syntax:
 
@@ -344,12 +337,34 @@ main (int argc, char **argv)
   int index;
   mu_mailbox_t mbox;
   mh_msgset_t msgset;
-  int flags;
+  int interactive = mh_interactive_mode_p ();
 
-  flags = mh_interactive_mode_p () ? 0 : ARGP_NO_ERRS;
   MU_APP_INIT_NLS ();
+  for (index = 1; index < argc; index++)
+    {
+      int colon = 0, cpos;
+      if (argv[index][0] == '-' && argv[index][1] == '-' &&
+         !strchr (argv[index], '=') &&
+         (!interactive ||
+          (colon = argv[index][cpos = strlen (argv[index]) - 1] == ':') ||
+          *mu_str_skip_class_comp (argv[index], MU_CTYPE_UPPER)) &&
+         index + 1 < argc && argv[index+1][0] != '-')
+       {
+         if (colon)
+           {
+             cpos -= 2;
+             mu_asprintf (&argv[index], "--component=%*.*s", cpos, cpos,
+                          argv[index] + 2);
+           }
+         else
+           mu_asprintf (&argv[index], "--component=%s", argv[index] + 2);
+         mu_asprintf (&argv[index + 1], "--pattern=%s", argv[index + 1]);
+         index++;
+       }
+    }
+  mh_help_hook = pick_help_hook;
   mh_argp_init (program_version);
-  mh_argp_parse (&argc, &argv, flags, options, mh_option,
+  mh_argp_parse (&argc, &argv, 0, options, mh_option,
                 args_doc, doc, opt_handler, NULL, &index);
   if (pick_parse (lexlist))
     return 1;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]