| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 
 | /* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2003-2025 Free Software Foundation, Inc.
   GNU Mailutils 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 3, or (at your option)
   any later version.
   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
/* MH pick command */
#include <mh.h>
#include <regex.h>
#include <pick.h>
#include <pick-gram.h>
static char prog_doc[] = N_("Search for messages by content");
static char args_doc[] = N_("[--COMPONENT PATTERN]... [MSGLIST]");
static int public_option = 1;
static int zero_option = 0;
static int list = 1;
static int seq_flags = 0; /* Create public sequences;
			     Do not zero the sequence before addition */
static mu_list_t seq_list;  /* List of sequence names to operate upon */
static mu_list_t lexlist;   /* List of input tokens */
static mu_msgset_t picked_message_uids;
static void
add_component_name (struct mu_parseopt *po, struct mu_option *opt,
		    char const *arg)
{
  pick_add_token (&lexlist, T_COMP, arg);
}
static void
add_component_pattern (struct mu_parseopt *po, struct mu_option *opt,
		       char const *arg)
{
  pick_add_token (&lexlist, T_STRING, arg);
}
static void
add_cflags (struct mu_parseopt *po, struct mu_option *opt,
	    char const *arg)
{
  pick_add_token (&lexlist, T_CFLAGS, arg);
}
static void
add_comp_match (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
  pick_add_token (&lexlist, T_COMP, opt->opt_default);
  pick_add_token (&lexlist, T_STRING, arg);
}
static void
add_datefield (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
  pick_add_token (&lexlist, T_DATEFIELD, arg);
}
static void
add_after (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
  pick_add_token (&lexlist, T_AFTER, NULL);
  pick_add_token (&lexlist, T_STRING, arg);
}
static void
add_before (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
  pick_add_token (&lexlist, T_BEFORE, NULL);
  pick_add_token (&lexlist, T_STRING, arg);
}
static void
add_and (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
  pick_add_token (&lexlist, T_AND, NULL);
}
static void
add_or (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
  pick_add_token (&lexlist, T_OR, NULL);  
}
static void
add_not (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
  pick_add_token (&lexlist, T_NOT, NULL);
}
static void
add_lbrace (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
  pick_add_token (&lexlist, T_LBRACE, NULL);
}
static void
add_rbrace (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
  pick_add_token (&lexlist, T_RBRACE, NULL);
}
static void
add_to_sequence (struct mu_parseopt *po, struct mu_option *opt,
		 char const *arg)
{
  if (!seq_list && mu_list_create (&seq_list))
    {
      mu_error (_("cannot create sequence list"));
      exit (1);
    }
  mu_list_append (seq_list, mu_strdup (arg));
  list = 0;
}
static struct mu_option options[] = {
  MU_OPTION_GROUP (N_("Search patterns")),
  { "component", 0, N_("FIELD"), MU_OPTION_DEFAULT,
    N_("search the named header field"),
    mu_c_string, NULL, add_component_name },
  { "pattern",   0, N_("STRING"), MU_OPTION_DEFAULT,
    N_("set pattern to look for"),
    mu_c_string, NULL, add_component_pattern },
  { "search",    0, NULL, MU_OPTION_ALIAS },
  { "cflags",    0,  N_("STRING"), MU_OPTION_DEFAULT,
    N_("flags controlling the type of regular expressions. "
       "STRING must consist of one or more of the following letters: "
       "B=basic, E=extended, I=ignore case, C=case sensitive. "
       "Default is \"EI\". The flags remain in effect until the next "
       "occurrence of --cflags option. The option must occur right "
       "before --pattern or --component option (or its alias)."),
    mu_c_string, NULL, add_cflags },
  { "cc",        0,  N_("STRING"), MU_OPTION_DEFAULT,
    N_("search for Cc matching STRING"),
    mu_c_string, NULL, add_comp_match, "cc" },
  { "date",      0,  N_("STRING"), MU_OPTION_DEFAULT,
    N_("search for Date matching STRING"),
    mu_c_string, NULL, add_comp_match, "date" },
  { "from",      0,  N_("STRING"), MU_OPTION_DEFAULT,
    N_("search for From matching STRING"),
    mu_c_string, NULL, add_comp_match, "from" },
  { "subject",   0,  N_("STRING"), MU_OPTION_DEFAULT,
    N_("search for Subject matching STRING"),
    mu_c_string, NULL, add_comp_match, "subject" },
  { "to",        0,  N_("STRING"), MU_OPTION_DEFAULT,
    N_("search for To matching STRING"),
    mu_c_string, NULL, add_comp_match, "to" },
  MU_OPTION_GROUP (N_("Date constraint operations")),
  { "datefield", 0, N_("STRING"), MU_OPTION_DEFAULT,
    N_("search in the named date header field (default is `Date:')"),
    mu_c_string, NULL, add_datefield },
  { "after",     0, N_("DATE"), MU_OPTION_DEFAULT,
    N_("match messages after the given date"),
    mu_c_string, NULL, add_after },
  { "before",    0, N_("DATE"), MU_OPTION_DEFAULT,
    N_("match messages before the given date"),
    mu_c_string, NULL, add_before },
  MU_OPTION_GROUP (N_("Logical operations and grouping")),
  { "and",       0, NULL, MU_OPTION_DEFAULT,
    N_("logical AND (default)"),
    mu_c_string, NULL, add_and },
  { "or",        0, NULL, MU_OPTION_DEFAULT,
    N_("logical OR"),
    mu_c_string, NULL, add_or },
  { "not",       0, NULL, MU_OPTION_DEFAULT,
    N_("logical NOT"),
    mu_c_string, NULL, add_not },
  { "lbrace",    0, NULL, MU_OPTION_DEFAULT,
    N_("open group"),
    mu_c_string, NULL, add_lbrace },
  { "(",         0, NULL, MU_OPTION_ALIAS },
  { "rbrace",    0, NULL, MU_OPTION_DEFAULT,
    N_("close group"),
    mu_c_string, NULL, add_rbrace },
  { ")",         0, NULL, MU_OPTION_ALIAS },
  MU_OPTION_GROUP (N_("Operations over the selected messages")),
  { "list",      0, NULL, MU_OPTION_DEFAULT,
    N_("list the numbers of the selected messages (default)"),
    mu_c_bool, &list },
  { "sequence",  0, N_("NAME"), MU_OPTION_DEFAULT,
    N_("add matching messages to the given sequence"),
    mu_c_string, NULL, add_to_sequence },
  { "public",    0, NULL,       MU_OPTION_DEFAULT,
    N_("create public sequence"),
    mu_c_bool, &public_option },
  { "zero",      0, NULL,       MU_OPTION_DEFAULT,
    N_("empty the sequence before adding messages"),
    mu_c_bool, &zero_option },
  MU_OPTION_END
  
};
static int
pick_message (size_t num, mu_message_t msg, void *data)
{
  if (pick_eval (msg))
    {
      mh_message_number (msg, &num);
      if (list)
	printf ("%s\n", mu_umaxtostr (0, num));
      if (picked_message_uids)
	mu_msgset_add_range (picked_message_uids, num, num, MU_MSGSET_UID);
    }
  return 0;
}
static int
action_add (void *item, void *data)
{
  mu_mailbox_t mbox = data;
  mh_seq_add (mbox, (char *)item, picked_message_uids, seq_flags);
  return 0;
}
int
main (int argc, char **argv)
{
  int i, status;
  mu_mailbox_t mbox;
  mu_msgset_t msgset;
  int argv_alloc = 0;
  /* Expand eventual --COMPONENT NAME pairs */
  for (i = 1; i < argc;)
    {
      if (strncmp (argv[i], "--", 2) == 0 && argv[i][3] && i + 1 < argc)
	{
	  size_t n = argc + 2;
	  if (argv_alloc)
	    argv = mu_realloc (argv, (n + 1) * sizeof (argv[0]));
	  else
	    {
	      char **p = mu_calloc (n + 1, sizeof p[0]);
	      memcpy (p, argv, (argc + 1) * sizeof argv[0]);
	      argv = p;
	      argv_alloc = 1;
	    }
	  
	  memmove (argv + i + 4, argv + i + 2,
		   (argc - i - 1) * sizeof argv[0]);
	  argv[i + 3] = argv[i + 1];
	  argv[i + 2] = mu_strdup ("-pattern");
	  argv[i + 1] = mu_strdup (argv[i] + 2);
	  argv[i] = mu_strdup ("-component");
	  argc = n;
	  i += 4; 
	}
      else
	i++;
    }
  
  mh_getopt (&argc, &argv, options, MH_GETOPT_DEFAULT_FOLDER,
	     args_doc, prog_doc, NULL);
  if (pick_parse (lexlist))
    return 1;
  seq_flags = (public_option ? 0 : SEQ_PRIVATE) | (zero_option ? SEQ_ZERO : 0);
  
  mbox = mh_open_folder (mh_current_folder (),
			 seq_list ? MU_STREAM_RDWR : MU_STREAM_READ);
  if (seq_list)
    mu_msgset_create (&picked_message_uids, NULL, MU_MSGSET_UID);
  
  mh_msgset_parse (&msgset, mbox, argc, argv, "all");
  status = mu_msgset_foreach_message (msgset, pick_message, NULL);
  if (picked_message_uids)
    mu_list_foreach (seq_list, action_add, mbox);
  mh_global_save_state ();
  mu_mailbox_close (mbox);
  mu_mailbox_destroy (&mbox);
  return status;
}
  
 |