1 2 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
|
/*
* mark.c -- add message(s) to sequences in given folder
* -- delete messages (s) from sequences in given folder
* -- list sequences in given folder
*
* This code is Copyright (c) 2002, by the authors of nmh. See the
* COPYRIGHT file in the root directory of the nmh distribution for
* complete copyright information.
*/
#include <h/mh.h>
#include <h/utils.h>
#define MARK_SWITCHES \
X("add", 0, ADDSW) \
X("delete", 0, DELSW) \
X("list", 0, LSTSW) \
X("sequence name", 0, SEQSW) \
X("public", 0, PUBLSW) \
X("nopublic", 0, NPUBLSW) \
X("zero", 0, ZEROSW) \
X("nozero", 0, NZEROSW) \
X("version", 0, VERSIONSW) \
X("help", 0, HELPSW) \
X("debug", -5, DEBUGSW) \
#define X(sw, minchars, id) id,
DEFINE_SWITCH_ENUM(MARK);
#undef X
#define X(sw, minchars, id) { sw, minchars, id },
DEFINE_SWITCH_ARRAY(MARK, switches);
#undef X
/*
* static prototypes
*/
static void print_debug (struct msgs *);
static void seq_printdebug (struct msgs *);
int
main (int argc, char **argv)
{
int addsw = 0, deletesw = 0, debugsw = 0;
int listsw = 0, publicsw = -1, zerosw = 0, msgnum;
unsigned int seqp = 0;
char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
char **argp, **arguments;
svector_t seqs = svector_create (0);
struct msgs_array msgs = { 0, 0, NULL };
struct msgs *mp;
if (nmh_init(argv[0], 1)) { return 1; }
arguments = getarguments (invo_name, argc, argv, 1);
argp = arguments;
/*
* Parse arguments
*/
while ((cp = *argp++)) {
if (*cp == '-') {
switch (smatch (++cp, switches)) {
case AMBIGSW:
ambigsw (cp, switches);
done (1);
case UNKWNSW:
adios (NULL, "-%s unknown\n", cp);
case HELPSW:
snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
invo_name);
print_help (buf, switches, 1);
done (0);
case VERSIONSW:
print_version(invo_name);
done (0);
case ADDSW:
addsw++;
deletesw = listsw = 0;
continue;
case DELSW:
deletesw++;
addsw = listsw = 0;
continue;
case LSTSW:
listsw++;
addsw = deletesw = 0;
continue;
case SEQSW:
if (!(cp = *argp++) || *cp == '-')
adios (NULL, "missing argument to %s", argp[-2]);
svector_push_back (seqs, cp);
seqp++;
continue;
case PUBLSW:
publicsw = 1;
continue;
case NPUBLSW:
publicsw = 0;
continue;
case DEBUGSW:
debugsw++;
continue;
case ZEROSW:
zerosw++;
continue;
case NZEROSW:
zerosw = 0;
continue;
}
}
if (*cp == '+' || *cp == '@') {
if (folder)
adios (NULL, "only one folder at a time!");
else
folder = pluspath (cp);
} else
app_msgarg(&msgs, cp);
}
/*
* If we haven't specified -add, -delete, or -list,
* then use -add if a sequence was specified, else
* use -list.
*/
if (!addsw && !deletesw && !listsw) {
if (seqp)
addsw++;
else
listsw++;
}
if (!context_find ("path"))
free (path ("./", TFOLDER));
if (!msgs.size)
app_msgarg(&msgs, listsw ? "all" :"cur");
if (!folder)
folder = getfolder (1);
maildir = m_maildir (folder);
if (chdir (maildir) == NOTOK)
adios (maildir, "unable to change directory to");
/* read folder and create message structure */
if (!(mp = folder_read (folder, 1)))
adios (NULL, "unable to read folder %s", folder);
/* print some general debugging info */
if (debugsw)
print_debug(mp);
/* check for empty folder */
if (mp->nummsg == 0)
adios (NULL, "no messages in %s", folder);
/* parse all the message ranges/sequences and set SELECTED */
for (msgnum = 0; msgnum < msgs.size; msgnum++)
if (!m_convert (mp, msgs.msgs[msgnum]))
done (1);
if (publicsw == 1 && is_readonly(mp))
adios (NULL, "folder %s is read-only, so -public not allowed", folder);
/*
* Make sure at least one sequence has been
* specified if we are adding or deleting.
*/
if (seqp == 0 && (addsw || deletesw))
adios (NULL, "-%s requires at least one -sequence argument",
addsw ? "add" : "delete");
/* Adding messages to sequences */
if (addsw) {
for (seqp = 0; seqp < svector_size (seqs); seqp++)
if (!seq_addsel (mp, svector_at (seqs, seqp), publicsw, zerosw))
done (1);
}
/* Deleting messages from sequences */
if (deletesw) {
for (seqp = 0; seqp < svector_size (seqs); seqp++)
if (!seq_delsel (mp, svector_at (seqs, seqp), publicsw, zerosw))
done (1);
}
/* Listing messages in sequences */
if (listsw) {
if (seqp) {
/* print the sequences given */
for (seqp = 0; seqp < svector_size (seqs); seqp++)
seq_print (mp, svector_at (seqs, seqp));
} else {
/* else print them all */
seq_printall (mp);
}
/* print debugging info about SELECTED messages */
if (debugsw)
seq_printdebug (mp);
}
svector_free (seqs);
seq_save (mp); /* synchronize message sequences */
context_replace (pfolder, folder); /* update current folder */
context_save (); /* save the context file */
folder_free (mp); /* free folder/message structure */
done (0);
return 1;
}
/*
* Print general debugging info
*/
static void
print_debug (struct msgs *mp)
{
char buf[100];
printf ("invo_name = %s\n", invo_name);
printf ("mypath = %s\n", mypath);
printf ("defpath = %s\n", defpath);
printf ("ctxpath = %s\n", ctxpath);
printf ("context flags = %s\n", snprintb (buf, sizeof(buf),
(unsigned) ctxflags, DBITS));
printf ("foldpath = %s\n", mp->foldpath);
printf ("folder flags = %s\n\n", snprintb(buf, sizeof(buf),
(unsigned) mp->msgflags, FBITS));
printf ("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
printf ("lowsel=%d hghsel=%d numsel=%d\n",
mp->lowsel, mp->hghsel, mp->numsel);
printf ("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
}
/*
* Print debugging info about all the SELECTED
* messages and the sequences they are in.
* Due limitattions of snprintb(), only a limited
* number of sequences will be printed. See the
* comments in sbr/seq_bits.c.
*/
static void
seq_printdebug (struct msgs *mp)
{
int msgnum;
char buf[BUFSIZ];
printf ("\n");
for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
if (is_selected (mp, msgnum))
printf ("%*d: %s\n", DMAXFOLDER, msgnum,
snprintb (buf, sizeof buf,
(unsigned) *bvector_bits (msgstat (mp, msgnum)),
seq_bits (mp)));
}
}
|