Check command-line options for conflicting matchers and actions.

This commit is contained in:
Daniel Spiljar 2023-04-19 21:55:25 +02:00
parent 3040f9c363
commit c19253d080
Signed by: dspiljar
GPG Key ID: A32CE9C59D8003B5
8 changed files with 241 additions and 152 deletions

View File

@ -24,7 +24,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
# include <zlib.h> # include <zlib.h>
#endif /* HAVE_LIBZ */ #endif /* HAVE_LIBZ */
@ -81,21 +80,11 @@ main (int argc, char **argv)
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
set_default_options (); init_options ();
get_runtime_options (&argc, argv, long_options); get_options (&argc, argv, long_options);
if ((config.body == 0) && (config.headers == 0)) check_options ();
{
config.body = 1;
config.headers = 1;
}
if (config.format == MAILDIR && config.action == WRITE)
{
gethostname (config.hostname, HOST_NAME_SIZE);
config.pid = (int) getpid ();
}
runtime.cs = (checksum_t *) xmalloc (sizeof (checksum_t)); runtime.cs = (checksum_t *) xmalloc (sizeof (checksum_t));
runtime.cs->md5 = (char **) xcalloc (1, sizeof (char **)); runtime.cs->md5 = (char **) xcalloc (1, sizeof (char **));
@ -111,7 +100,7 @@ main (int argc, char **argv)
if (config.haveregex) if (config.haveregex)
{ {
#ifdef HAVE_LIBPCRE #ifdef HAVE_LIBPCRE
if (config.perl) if (config.regextype == REGEX_PERL)
pcre_init (); pcre_init ();
else else
#endif /* HAVE_LIBPCRE */ #endif /* HAVE_LIBPCRE */
@ -125,7 +114,7 @@ main (int argc, char **argv)
while (optind < argc) while (optind < argc)
{ {
if (config.action == DELETE) if (config.action == ACTION_DELETE)
{ {
tmpmbox_create (argv[optind]); tmpmbox_create (argv[optind]);
runtime.tmp_mbox = (mbox_t *) mbox_open (config.tmpfilename, "w"); runtime.tmp_mbox = (mbox_t *) mbox_open (config.tmpfilename, "w");
@ -140,7 +129,7 @@ main (int argc, char **argv)
havemailbox = 1; havemailbox = 1;
if (config.action == COUNT) if (config.action == ACTION_COUNT)
{ {
if (singlefile) if (singlefile)
fprintf (stdout, "%i\n", runtime.count); fprintf (stdout, "%i\n", runtime.count);
@ -152,19 +141,20 @@ main (int argc, char **argv)
fprintf (stdout, "%s:%i\n", argv[optind], runtime.count); fprintf (stdout, "%s:%i\n", argv[optind], runtime.count);
} }
} }
if (config.action == DELETE) if (config.action == ACTION_DELETE)
{ {
mbox_close (runtime.tmp_mbox); mbox_close (runtime.tmp_mbox);
rename (config.tmpfilename, argv[optind]); rename (config.tmpfilename, argv[optind]);
} }
++optind; ++optind;
} }
if (!havemailbox) if (!havemailbox)
{ {
config.format = MBOX; config.format = FORMAT_MBOX;
scan_mailbox ("-"); scan_mailbox ("-");
if (config.action == COUNT) if (config.action == ACTION_COUNT)
fprintf (stdout, "%i\n", runtime.count); fprintf (stdout, "%i\n", runtime.count);
} }

View File

@ -1,6 +1,6 @@
/* /*
mboxgrep - scan mailbox for messages matching a regular expression mboxgrep - scan mailbox for messages matching a regular expression
Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006 Daniel Spiljar Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006, 2023 Daniel Spiljar
Mboxgrep is free software; you can redistribute it and/or modify it Mboxgrep is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by under the terms of the GNU General Public License as published by
@ -89,7 +89,7 @@ mbox_open (const char *path, const char *mode)
return NULL; return NULL;
} }
if (config.lock) if (config.lock > LOCK_NONE)
{ {
#ifdef HAVE_FLOCK #ifdef HAVE_FLOCK
int op; int op;
@ -123,27 +123,27 @@ mbox_open (const char *path, const char *mode)
if (mode[0] == 'r') if (mode[0] == 'r')
{ {
if (config.format == MBOX) if (config.format == FORMAT_MBOX)
mp->fp = (FILE *) m_fdopen (fd, "r"); mp->fp = (FILE *) m_fdopen (fd, "r");
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
else if (config.format == ZMBOX) else if (config.format == FORMAT_ZMBOX)
mp->fp = (gzFile *) m_gzdopen (fd, "rb"); mp->fp = (gzFile *) m_gzdopen (fd, "rb");
#endif /* HAVE_LIBZ */ #endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2 #ifdef HAVE_LIBBZ2
else if (config.format == BZ2MBOX) else if (config.format == FORMAT_BZ2MBOX)
mp->fp = (BZFILE *) BZ2_bzdopen (fd, "rb"); mp->fp = (BZFILE *) BZ2_bzdopen (fd, "rb");
#endif /* HAVE_LIBBZ2 */ #endif /* HAVE_LIBBZ2 */
} }
else if (mode[0] == 'w') else if (mode[0] == 'w')
{ {
if (config.format == MBOX) if (config.format == FORMAT_MBOX)
mp->fp = (FILE *) m_fdopen (fd, "w"); mp->fp = (FILE *) m_fdopen (fd, "w");
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
else if (config.format == ZMBOX) else if (config.format == FORMAT_ZMBOX)
mp->fp = (gzFile *) m_gzdopen (fd, "wb"); mp->fp = (gzFile *) m_gzdopen (fd, "wb");
#endif /* HAVE_LIBZ */ #endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2 #ifdef HAVE_LIBBZ2
else if (config.format == BZ2MBOX) else if (config.format == FORMAT_BZ2MBOX)
mp->fp = (BZFILE *) BZ2_bzdopen (fd, "wb"); mp->fp = (BZFILE *) BZ2_bzdopen (fd, "wb");
#endif /* HAVE_LIBBZ2 */ #endif /* HAVE_LIBBZ2 */
} }
@ -163,14 +163,14 @@ mbox_open (const char *path, const char *mode)
if (mode[0] == 'r') if (mode[0] == 'r')
{ {
if (config.format == MBOX) if (config.format == FORMAT_MBOX)
fgets (buffer, BUFSIZ, mp->fp); fgets (buffer, BUFSIZ, mp->fp);
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
else if (config.format == ZMBOX) else if (config.format == FORMAT_ZMBOX)
gzgets (mp->fp, buffer, BUFSIZ); gzgets (mp->fp, buffer, BUFSIZ);
#endif /* HAVE_LIBZ */ #endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2 #ifdef HAVE_LIBBZ2
else if (config.format == BZ2MBOX) else if (config.format == FORMAT_BZ2MBOX)
{ {
char c[1] = "\0"; char c[1] = "\0";
int n = 0; int n = 0;
@ -196,14 +196,14 @@ mbox_open (const char *path, const char *mode)
fprintf (stderr, "%s: %s: Not a mbox folder\n", APPNAME, fprintf (stderr, "%s: %s: Not a mbox folder\n", APPNAME,
path); path);
} }
if (config.format == MBOX) if (config.format == FORMAT_MBOX)
fclose (mp->fp); fclose (mp->fp);
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
else if (config.format == ZMBOX) else if (config.format == FORMAT_ZMBOX)
gzclose (mp->fp); gzclose (mp->fp);
#endif /* HAVE_LIBZ */ #endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2 #ifdef HAVE_LIBBZ2
else if (config.format == BZ2MBOX) else if (config.format == FORMAT_BZ2MBOX)
BZ2_bzclose (mp->fp); BZ2_bzclose (mp->fp);
#endif /* HAVE_LIBBZ2 */ #endif /* HAVE_LIBBZ2 */
return NULL; return NULL;
@ -216,14 +216,14 @@ mbox_open (const char *path, const char *mode)
void void
mbox_close (mbox_t * mp) mbox_close (mbox_t * mp)
{ {
if (config.format == MBOX) if (config.format == FORMAT_MBOX)
fclose (mp->fp); fclose (mp->fp);
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
else if (config.format == ZMBOX) else if (config.format == FORMAT_ZMBOX)
gzclose (mp->fp); gzclose (mp->fp);
#endif /* HAVE_LIBZ */ #endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2 #ifdef HAVE_LIBBZ2
else if (config.format == BZ2MBOX) else if (config.format == FORMAT_BZ2MBOX)
BZ2_bzclose (mp->fp); BZ2_bzclose (mp->fp);
#endif /* HAVE_LIBBZ2 */ #endif /* HAVE_LIBBZ2 */
@ -249,7 +249,7 @@ mbox_read_message (mbox_t * mp)
for (;;) for (;;)
{ {
if (config.format == MBOX) if (config.format == FORMAT_MBOX)
{ {
if (fgets (buffer, BUFSIZ, mp->fp) == NULL) if (fgets (buffer, BUFSIZ, mp->fp) == NULL)
{ {
@ -261,7 +261,7 @@ mbox_read_message (mbox_t * mp)
} }
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
else if (config.format == ZMBOX) else if (config.format == FORMAT_ZMBOX)
{ {
if (gzgets (mp->fp, buffer, BUFSIZ) == NULL) if (gzgets (mp->fp, buffer, BUFSIZ) == NULL)
{ {
@ -274,7 +274,7 @@ mbox_read_message (mbox_t * mp)
#endif /* HAVE_LIBZ */ #endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2 #ifdef HAVE_LIBBZ2
else if (config.format == BZ2MBOX) else if (config.format == FORMAT_BZ2MBOX)
{ {
char c[1] = "\0"; char c[1] = "\0";
int n = 0; int n = 0;
@ -370,10 +370,10 @@ tmpfile_name (const char *path)
void void
mbox_write_message (message_t * msg, mbox_t * mbox) mbox_write_message (message_t * msg, mbox_t * mbox)
{ {
if (config.format == MBOX) if (config.format == FORMAT_MBOX)
fprintf (mbox->fp, "%s\n%s", msg->headers, msg->body); fprintf (mbox->fp, "%s\n%s", msg->headers, msg->body);
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
else if (config.format == ZMBOX) else if (config.format == FORMAT_ZMBOX)
{ {
gzwrite_loop (mbox->fp, msg->headers); gzwrite_loop (mbox->fp, msg->headers);
gzwrite (mbox->fp, "\n", 1); gzwrite (mbox->fp, "\n", 1);
@ -381,7 +381,7 @@ mbox_write_message (message_t * msg, mbox_t * mbox)
} }
#endif /* HAVE_LIBZ */ #endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2 #ifdef HAVE_LIBBZ2
else if (config.format == BZ2MBOX) else if (config.format == FORMAT_BZ2MBOX)
{ {
bzwrite_loop (mbox->fp, msg->headers); bzwrite_loop (mbox->fp, msg->headers);
BZ2_bzwrite (mbox->fp, "\n", 1); BZ2_bzwrite (mbox->fp, "\n", 1);

View File

@ -46,34 +46,46 @@
typedef enum typedef enum
{ {
MBOX, FORMAT_UNDEF,
ZMBOX, FORMAT_MBOX,
MH, FORMAT_ZMBOX,
NNML, FORMAT_MH,
NNMH, FORMAT_NNML,
MAILDIR, FORMAT_NNMH,
BZ2MBOX FORMAT_MAILDIR,
FORMAT_BZ2MBOX
} }
format_t; format_t;
typedef enum typedef enum
{ {
NONE, LOCK_UNDEF,
FCNTL, LOCK_NONE,
FLOCK LOCK_FCNTL,
LOCK_FLOCK
} }
lockmethod_t; lockmethod_t;
typedef enum typedef enum
{ {
DISPLAY, ACTION_UNDEF,
WRITE, ACTION_DISPLAY,
COUNT, ACTION_WRITE,
DELETE, ACTION_COUNT,
PIPE ACTION_DELETE,
ACTION_PIPE
} }
action_t; action_t;
typedef enum
{
REGEX_UNDEF,
REGEX_BASIC,
REGEX_EXTENDED,
REGEX_PERL
}
regextype_t;
typedef struct typedef struct
{ {
FILE *fp; FILE *fp;
@ -93,9 +105,6 @@ checksum_t;
typedef struct typedef struct
{ {
int extended;
int perl;
int body; int body;
int headers; int headers;
int dedup; int dedup;
@ -117,6 +126,7 @@ typedef struct
action_t action; action_t action;
format_t format; format_t format;
lockmethod_t lock; lockmethod_t lock;
regextype_t regextype;
} }
option_t; option_t;

View File

@ -1,6 +1,6 @@
/* -*- C -*- /* -*- C -*-
mboxgrep - scan mailbox for messages matching a regular expression mboxgrep - scan mailbox for messages matching a regular expression
Copyright (C) 2000, 2001, 2002, 2003, 2006 Daniel Spiljar Copyright (C) 2000, 2001, 2002, 2003, 2006, 2023 Daniel Spiljar
Mboxgrep is free software; you can redistribute it and/or modify it Mboxgrep is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by under the terms of the GNU General Public License as published by
@ -129,7 +129,7 @@ mh_read_message (DIR * dp)
fgets (buffer, BUFSIZ, fp); fgets (buffer, BUFSIZ, fp);
/* if (config.format == NNML || config.format == NNMH) */ /* if (config.format == FORMAT_NNML || config.format == FORMAT_NNMH) */
/* { */ /* { */
/* if (0 != strncmp ("X-From-Line: ", buffer, 13)) */ /* if (0 != strncmp ("X-From-Line: ", buffer, 13)) */
/* { */ /* { */

View File

@ -28,62 +28,76 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include "mboxgrep.h" #include "mboxgrep.h"
#include "misc.h"
#include "wrap.h" #include "wrap.h"
#include "getopt.h" #include "getopt.h"
#include "info.h" #include "info.h"
#include "message.h" #include "message.h"
format_t /* Determine the folder format passed to -m. */
folder_format (const char *name)
void
set_folder_format (const char *name)
{ {
format_t f; if (config.format > 0)
{
if (config.merr)
fprintf (stderr, "%s: multiple mailbox types specified\n", APPNAME);
exit (2);
}
if (0 == strncasecmp (name, "mbox", 4)) if (0 == strncasecmp (name, "mbox", 4))
f = MBOX; config.format = FORMAT_MBOX;
else if (0 == strncasecmp (name, "zmbox", 5)) else if (0 == strncasecmp (name, "zmbox", 5))
f = ZMBOX; config.format = FORMAT_ZMBOX;
else if (0 == strncasecmp (name, "gzmbox", 6)) else if (0 == strncasecmp (name, "gzmbox", 6))
f = ZMBOX; config.format = FORMAT_ZMBOX;
else if (0 == strncasecmp (name, "bzmbox", 5)) else if (0 == strncasecmp (name, "bzmbox", 5))
f = BZ2MBOX; config.format = FORMAT_BZ2MBOX;
else if (0 == strncasecmp (name, "bz2mbox", 5)) else if (0 == strncasecmp (name, "bz2mbox", 5))
f = BZ2MBOX; config.format = FORMAT_BZ2MBOX;
else if (0 == strncasecmp (name, "mh", 2)) else if (0 == strncasecmp (name, "mh", 2))
f = MH; config.format = FORMAT_MH;
else if (0 == strncasecmp (name, "nnml", 4)) else if (0 == strncasecmp (name, "nnml", 4))
f = NNML; config.format = FORMAT_NNML;
else if (0 == strncasecmp (name, "nnmh", 4)) else if (0 == strncasecmp (name, "nnmh", 4))
f = NNMH; config.format = FORMAT_NNMH;
else if (0 == strncasecmp (name, "maildir", 7)) else if (0 == strncasecmp (name, "maildir", 7))
f = MAILDIR; config.format = FORMAT_MAILDIR;
else else
{ {
if (config.merr) if (config.merr)
fprintf (stderr, "%s: %s: unknown folder type\n", APPNAME, name); fprintf (stderr, "%s: %s: unknown folder type\n", APPNAME, name);
exit (2); exit (2);
} }
return f;
} }
lockmethod_t /* Determine the file locking method passed to -l. */
lock_method (const char *name)
void
set_lock_method (const char *name)
{ {
lockmethod_t l; if (config.lock > 0)
{
if (config.merr)
fprintf (stderr, "%s: conflicting file locking options specified\n", APPNAME);
exit (2);
}
if (0 == strncasecmp (name, "none", 4)) if (0 == strncasecmp (name, "none", 4))
l = NONE; config.lock = LOCK_NONE;
else if (0 == strncasecmp (name, "off", 3)) else if (0 == strncasecmp (name, "off", 3))
l = NONE; config.lock = LOCK_NONE;
#ifdef HAVE_FCNTL #ifdef HAVE_FCNTL
else if (0 == strncasecmp (name, "fcntl", 5)) else if (0 == strncasecmp (name, "fcntl", 5))
l = FCNTL; config.lock = LOCK_FCNTL;
#endif /* HAVE_FCNTL */ #endif /* HAVE_FCNTL */
#ifdef HAVE_FLOCK #ifdef HAVE_FLOCK
else if (0 == strncasecmp (name, "flock", 5)) else if (0 == strncasecmp (name, "flock", 5))
l = FLOCK; config.lock = LOCK_FLOCK;
#endif /* HAVE_FLOCK */ #endif /* HAVE_FLOCK */
else else
{ {
@ -91,10 +105,10 @@ lock_method (const char *name)
fprintf (stderr, "mboxgrep: %s: unknown file locking method\n", name); fprintf (stderr, "mboxgrep: %s: unknown file locking method\n", name);
exit (2); exit (2);
} }
return l;
} }
/* Dead code */
/* /*
time_t parse_date(char *datestr) time_t parse_date(char *datestr)
{ {
@ -170,26 +184,29 @@ postmark_print (message_t * msg)
fprintf (stdout, "From nobody %s\n", date_str); fprintf (stdout, "From nobody %s\n", date_str);
} }
/* Initialize the option_t struct. */
void void
set_default_options (void) init_options (void)
{ {
config.perl = 0; config.regextype = REGEX_UNDEF;
config.extended = 1;
config.invert = 0; config.invert = 0;
config.headers = 0; config.headers = 0;
config.body = 0; config.body = 0;
config.action = DISPLAY; config.action = ACTION_UNDEF;
config.dedup = 0; config.dedup = 0;
config.recursive = 0; config.recursive = 0;
config.ignorecase = 0; config.ignorecase = 0;
config.format = MBOX; /* default mailbox format */ config.format = FORMAT_UNDEF;
config.lock = FCNTL; /* default file locking method */ config.lock = LOCK_UNDEF; /* default file locking method */
config.merr = 1; /* report errors by default */ config.merr = 1; /* report errors by default */
config.debug = 0; config.debug = 0;
} }
/* Parse command-line arguments and assign values to option_t. */
void void
get_runtime_options (int *argc, char **argv, struct option *long_options) get_options (int *argc, char **argv, struct option *long_options)
{ {
int option_index = 0, c; int option_index = 0, c;
@ -206,35 +223,26 @@ get_runtime_options (int *argc, char **argv, struct option *long_options)
case '?': case '?':
usage (); usage ();
case 'c': case 'c':
config.action = COUNT; set_option_action (ACTION_COUNT, NULL);
break; break;
case 'd': case 'd':
config.action = DELETE; set_option_action (ACTION_DELETE, NULL);
break; break;
case 'e': case 'e':
config.regex_s = xstrdup (optarg); config.regex_s = xstrdup (optarg);
config.haveregex = 1; config.haveregex = 1;
break; break;
case 'o': case 'o':
config.outboxname = xstrdup (optarg); set_option_action (ACTION_WRITE, optarg);
config.action = WRITE;
break; break;
case 'E': case 'E':
config.extended = 1; set_option_regextype (REGEX_EXTENDED);
break; break;
case 'G': case 'G':
config.extended = 0; set_option_regextype (REGEX_BASIC);
break; break;
case 'P': case 'P':
#ifdef HAVE_LIBPCRE set_option_regextype (REGEX_PERL);
config.extended = 0;
config.perl = 1;
#else
fprintf (stderr,
"%s: Support for Perl regular expressions not "
"compiled in\n", APPNAME);
exit (2);
#endif /* HAVE_LIBPCRE */
break; break;
case 'h': case 'h':
help (); help ();
@ -243,14 +251,13 @@ get_runtime_options (int *argc, char **argv, struct option *long_options)
config.ignorecase = 1; config.ignorecase = 1;
break; break;
case 'm': case 'm':
config.format = folder_format (optarg); set_folder_format (optarg);
break; break;
case 'l': case 'l':
config.lock = lock_method (optarg); set_lock_method (optarg);
break; break;
case 'p': case 'p':
config.action = PIPE; set_option_action (ACTION_PIPE, optarg);
config.pipecmd = xstrdup (optarg);
break; break;
case 'V': case 'V':
version (); version ();
@ -289,7 +296,7 @@ get_runtime_options (int *argc, char **argv, struct option *long_options)
config.dedup = 1; config.dedup = 1;
break; break;
case 'l': case 'l':
config.lock = 0; set_lock_method ("none");
break; break;
default: default:
fprintf (stderr, "%s: invalid option -- n%c\n", fprintf (stderr, "%s: invalid option -- n%c\n",
@ -300,3 +307,82 @@ get_runtime_options (int *argc, char **argv, struct option *long_options)
} /* switch */ } /* switch */
} /* while */ } /* while */
} }
/* Check the state of command-line options after parsing them.
* Raise error on conflicting options and set uninitialized ones to default values.
*/
void
check_options (void)
{
gethostname (config.hostname, HOST_NAME_SIZE);
config.pid = (int) getpid ();
if (config.action == ACTION_UNDEF)
{
config.action = ACTION_DISPLAY;
}
if (config.format == FORMAT_UNDEF)
{
config.format = FORMAT_MBOX; /* default mailbox format */
}
if (config.regextype == REGEX_UNDEF)
{
config.regextype = REGEX_EXTENDED; /* default regex type */
}
if ((config.body == 0) && (config.headers == 0))
{
config.body = 1;
config.headers = 1;
}
}
void
set_option_action (action_t action, char *path)
{
if (config.action > 0)
{
if (config.merr)
fprintf (stderr, "%s: conflicting actions specified\n", APPNAME);
exit (2);
}
config.action = action;
if (action == ACTION_WRITE)
{
config.outboxname = xstrdup (path);
}
if (action == ACTION_PIPE)
{
config.pipecmd = xstrdup (optarg);
}
}
void
set_option_regextype (regextype_t regextype)
{
if (config.regextype > 0)
{
if (config.merr)
fprintf (stderr, "%s: conflicting matchers specified\n", APPNAME);
exit (2);
}
#ifndef HAVE_LIBPCRE
if (regextype == REGEX_PERL);
{
fprintf (stderr,
"%s: Support for Perl regular expressions not compiled in\n",
APPNAME);
exit (2);
}
}
#endif /* HAVE_LIBPCRE */
config.regextype = regextype;
}

View File

@ -25,14 +25,17 @@
# include "message.h" # include "message.h"
/* #include <time.h> */ /* #include <time.h> */
format_t folder_format (const char *name); void set_folder_format (const char *name);
lockmethod_t lock_method (const char *name); void set_lock_method (const char *name);
/* time_t parse_date(char *datestr); */ /* time_t parse_date(char *datestr); */
char *parse_return_path (char *rpath); char *parse_return_path (char *rpath);
void *allocate_message (void); void *allocate_message (void);
void postmark_print (message_t * msg); void postmark_print (message_t * msg);
void set_default_options (void); void init_options (void);
void get_runtime_options (int *argc, char **argv, void get_options (int *argc, char **argv,
struct option *long_options); struct option *long_options);
void check_options (void);
void set_option_action (action_t action, char *path);
void set_option_regextype (regextype_t regextype);
#endif /* MISC_H */ #endif /* MISC_H */

View File

@ -77,7 +77,7 @@ regex_init (void)
if (config.ignorecase) if (config.ignorecase)
flag1 = REG_ICASE; flag1 = REG_ICASE;
if (config.extended) if (config.regextype == REGEX_EXTENDED)
flag2 = REG_EXTENDED; flag2 = REG_EXTENDED;
config.posix_pattern = (regex_t *) xmalloc (sizeof (regex_t)); config.posix_pattern = (regex_t *) xmalloc (sizeof (regex_t));

View File

@ -83,7 +83,7 @@ scan_mailbox (char path[])
int delete = 0; int delete = 0;
int isdup = 0; int isdup = 0;
if (config.format == MAILDIR && config.action == WRITE) if (config.format == FORMAT_MAILDIR && config.action == ACTION_WRITE)
{ {
foo = opendir (config.outboxname); /* do NOT change this to m_opendir! */ foo = opendir (config.outboxname); /* do NOT change this to m_opendir! */
if (foo == NULL && errno == ENOENT) if (foo == NULL && errno == ENOENT)
@ -101,24 +101,24 @@ scan_mailbox (char path[])
} }
runtime.count = 0; runtime.count = 0;
if (config.action == DELETE) if (config.action == ACTION_DELETE)
delete = 1; delete = 1;
if ((config.format == MBOX) || (config.format == ZMBOX) if ((config.format == FORMAT_MBOX) || (config.format == FORMAT_ZMBOX)
|| (config.format == BZ2MBOX)) || (config.format == FORMAT_BZ2MBOX))
{ {
mbox = (mbox_t *) mbox_open (path, "r"); mbox = (mbox_t *) mbox_open (path, "r");
if (mbox == NULL) if (mbox == NULL)
return; return;
} }
else if ((config.format == MH) || (config.format == NNMH) else if ((config.format == FORMAT_MH) || (config.format == FORMAT_NNMH)
|| (config.format == NNML)) || (config.format == FORMAT_NNML))
{ {
boxd = mh_open (path); boxd = mh_open (path);
if (boxd == NULL) if (boxd == NULL)
return; return;
} }
else if (config.format == MAILDIR) else if (config.format == FORMAT_MAILDIR)
{ {
maildird = maildir_open (path); maildird = maildir_open (path);
@ -131,13 +131,13 @@ scan_mailbox (char path[])
config.res1 = 1; config.res1 = 1;
config.res2 = 1; config.res2 = 1;
if ((config.format == MBOX) || (config.format == ZMBOX) if ((config.format == FORMAT_MBOX) || (config.format == FORMAT_ZMBOX)
|| (config.format == BZ2MBOX)) || (config.format == FORMAT_BZ2MBOX))
msg = (message_t *) mbox_read_message (mbox); msg = (message_t *) mbox_read_message (mbox);
else if ((config.format == MH) || (config.format == NNMH) else if ((config.format == FORMAT_MH) || (config.format == FORMAT_NNMH)
|| (config.format == NNML)) || (config.format == FORMAT_NNML))
msg = (message_t *) mh_read_message (boxd); msg = (message_t *) mh_read_message (boxd);
else if (config.format == MAILDIR) else if (config.format == FORMAT_MAILDIR)
msg = (message_t *) maildir_read_message (maildird); msg = (message_t *) maildir_read_message (maildird);
if (msg == NULL) if (msg == NULL)
@ -147,7 +147,7 @@ scan_mailbox (char path[])
msg->from = (char *) xstrdup ("nobody"); msg->from = (char *) xstrdup ("nobody");
#ifdef HAVE_LIBPCRE #ifdef HAVE_LIBPCRE
if (config.perl) if (config.regextype == REGEX_PERL)
pcre_match (msg); pcre_match (msg);
else else
#endif /* HAVE_LIBPCRE */ #endif /* HAVE_LIBPCRE */
@ -160,24 +160,24 @@ scan_mailbox (char path[])
((config.invert ^ delete)) && ((config.invert ^ delete)) &&
((config.dedup && !isdup) || !config.dedup)) ((config.dedup && !isdup) || !config.dedup))
{ {
if (config.action == DISPLAY) if (config.action == ACTION_DISPLAY)
{ {
if (config.format != MBOX && config.format != ZMBOX if (config.format != FORMAT_MBOX && config.format != FORMAT_ZMBOX
&& config.format != BZ2MBOX && config.format != FORMAT_BZ2MBOX
&& 0 != strncmp ("From ", msg->headers, 5)) && 0 != strncmp ("From ", msg->headers, 5))
postmark_print (msg); postmark_print (msg);
fprintf (stdout, "%s\n%s", msg->headers, msg->body); fprintf (stdout, "%s\n%s", msg->headers, msg->body);
} }
else if (config.action == WRITE) else if (config.action == ACTION_WRITE)
{ {
if (config.format == MAILDIR) if (config.format == FORMAT_MAILDIR)
maildir_write_message (msg, config.outboxname); maildir_write_message (msg, config.outboxname);
else if (config.format == MH || config.format == NNMH else if (config.format == FORMAT_MH || config.format == FORMAT_NNMH
|| config.format == NNML) || config.format == FORMAT_NNML)
mh_write_message (msg, config.outboxname); mh_write_message (msg, config.outboxname);
else if ((config.format == MBOX) || (config.format == ZMBOX) else if ((config.format == FORMAT_MBOX) || (config.format == FORMAT_ZMBOX)
|| (config.format == BZ2MBOX)) || (config.format == FORMAT_BZ2MBOX))
{ {
out = mbox_open (config.outboxname, "w"); out = mbox_open (config.outboxname, "w");
/* fprintf (out->fp, "%s\n%s", msg->headers, msg->body); */ /* fprintf (out->fp, "%s\n%s", msg->headers, msg->body); */
@ -185,7 +185,7 @@ scan_mailbox (char path[])
mbox_close (out); mbox_close (out);
} }
} }
else if (config.action == PIPE) else if (config.action == ACTION_PIPE)
{ {
outf = popen (config.pipecmd, "w"); outf = popen (config.pipecmd, "w");
if (outf == NULL) if (outf == NULL)
@ -200,19 +200,19 @@ scan_mailbox (char path[])
fprintf (outf, "%s\n%s", msg->headers, msg->body); fprintf (outf, "%s\n%s", msg->headers, msg->body);
pclose (outf); pclose (outf);
} }
else if (config.action == COUNT) else if (config.action == ACTION_COUNT)
runtime.count++; runtime.count++;
else if (config.action == DELETE && else if (config.action == ACTION_DELETE &&
((config.format == MBOX) || (config.format == ZMBOX) ((config.format == FORMAT_MBOX) || (config.format == FORMAT_ZMBOX)
|| (config.format == BZ2MBOX))) || (config.format == FORMAT_BZ2MBOX)))
mbox_write_message (msg, runtime.tmp_mbox); mbox_write_message (msg, runtime.tmp_mbox);
} }
else else
if (((((config.res1 == 0) | (config.res2 == 0)) ^ config.invert) if (((((config.res1 == 0) | (config.res2 == 0)) ^ config.invert)
&& delete) && ((config.format == MH) || (config.format == NNMH) && delete) && ((config.format == FORMAT_MH) || (config.format == FORMAT_NNMH)
|| (config.format == NNML) || (config.format == FORMAT_NNML)
|| (config.format == MAILDIR))) || (config.format == FORMAT_MAILDIR)))
m_unlink (msg->filename); m_unlink (msg->filename);
free (msg->body); free (msg->body);
@ -220,11 +220,11 @@ scan_mailbox (char path[])
free (msg); free (msg);
} /* for */ } /* for */
if ((config.format == MBOX) || (config.format == ZMBOX) if ((config.format == FORMAT_MBOX) || (config.format == FORMAT_ZMBOX)
|| (config.format == BZ2MBOX)) || (config.format == FORMAT_BZ2MBOX))
mbox_close (mbox); mbox_close (mbox);
else if ((config.format == MH) || (config.format == NNMH) else if ((config.format == FORMAT_MH) || (config.format == FORMAT_NNMH)
|| (config.format == NNML)) || (config.format == FORMAT_NNML))
mh_close (boxd); mh_close (boxd);
} }