* man/mhbuild.man: wrapped one appearance of "Content-Disposition"
[mmh] / uip / mark.c
1
2 /*
3  * mark.c -- add message(s) to sequences in given folder
4  *        -- delete messages (s) from sequences in given folder
5  *        -- list sequences in given folder
6  *
7  * $Id$
8  *
9  * This code is Copyright (c) 2002, by the authors of nmh.  See the
10  * COPYRIGHT file in the root directory of the nmh distribution for
11  * complete copyright information.
12  */
13
14 #include <h/mh.h>
15 #include <h/utils.h>
16
17 /*
18  * We allocate space for messages (msgs array)
19  * this number of elements at a time.
20  */
21 #define MAXMSGS  256
22
23
24 static struct swit switches[] = {
25 #define ADDSW               0
26     { "add", 0 },
27 #define DELSW               1
28     { "delete", 0 },
29 #define LSTSW               2
30     { "list", 0 },
31 #define SEQSW               3
32     { "sequence name", 0 },
33 #define PUBLSW              4
34     { "public", 0 },
35 #define NPUBLSW             5
36     { "nopublic", 0 },
37 #define ZEROSW              6
38     { "zero", 0 },
39 #define NZEROSW             7
40     { "nozero", 0 },
41 #define VERSIONSW           8
42     { "version", 0 },
43 #define HELPSW              9
44     { "help", 0 },
45 #define DEBUGSW            10
46     { "debug", -5 },
47     { NULL, 0 }
48 };
49
50 /*
51  * static prototypes
52  */
53 static void print_debug (struct msgs *);
54 static void seq_printdebug (struct msgs *);
55
56
57 int
58 main (int argc, char **argv)
59 {
60     int addsw = 0, deletesw = 0, debugsw = 0;
61     int listsw = 0, publicsw = -1, zerosw = 0;
62     int seqp = 0, msgnum, nummsgs, maxmsgs;
63     char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
64     char **argp, **arguments;
65     char *seqs[NUMATTRS + 1], **msgs;
66     struct msgs *mp;
67
68 #ifdef LOCALE
69     setlocale(LC_ALL, "");
70 #endif
71     invo_name = r1bindex (argv[0], '/');
72
73     /* read user profile/context */
74     context_read();
75
76     arguments = getarguments (invo_name, argc, argv, 1);
77     argp = arguments;
78
79     /*
80      * Allocate the initial space to record message
81      * names, ranges, and sequences.
82      */
83     nummsgs = 0;
84     maxmsgs = MAXMSGS;
85     msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
86
87     /*
88      * Parse arguments
89      */
90     while ((cp = *argp++)) {
91         if (*cp == '-') {
92             switch (smatch (++cp, switches)) {
93             case AMBIGSW: 
94                 ambigsw (cp, switches);
95                 done (1);
96             case UNKWNSW: 
97                 adios (NULL, "-%s unknown\n", cp);
98
99             case HELPSW: 
100                 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
101                           invo_name);
102                 print_help (buf, switches, 1);
103                 done (1);
104             case VERSIONSW:
105                 print_version(invo_name);
106                 done (1);
107
108             case ADDSW: 
109                 addsw++;
110                 deletesw = listsw = 0;
111                 continue;
112             case DELSW: 
113                 deletesw++;
114                 addsw = listsw = 0;
115                 continue;
116             case LSTSW: 
117                 listsw++;
118                 addsw = deletesw = 0;
119                 continue;
120
121             case SEQSW: 
122                 if (!(cp = *argp++) || *cp == '-')
123                     adios (NULL, "missing argument to %s", argp[-2]);
124
125                 /* check if too many sequences specified */
126                 if (seqp >= NUMATTRS)
127                     adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
128                 seqs[seqp++] = cp;
129                 continue;
130
131             case PUBLSW: 
132                 publicsw = 1;
133                 continue;
134             case NPUBLSW: 
135                 publicsw = 0;
136                 continue;
137
138             case DEBUGSW: 
139                 debugsw++;
140                 continue;
141
142             case ZEROSW: 
143                 zerosw++;
144                 continue;
145             case NZEROSW: 
146                 zerosw = 0;
147                 continue;
148             }
149         }
150         if (*cp == '+' || *cp == '@') {
151             if (folder)
152                 adios (NULL, "only one folder at a time!");
153             else
154                 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
155         } else {
156             /*
157              * Check if we need to allocate more space
158              * for message names/ranges/sequences.
159              */
160             if (nummsgs >= maxmsgs) {
161                 maxmsgs += MAXMSGS;
162                 msgs = (char **) mh_xrealloc (msgs,
163                     (size_t) (maxmsgs * sizeof(*msgs)));
164             }
165             msgs[nummsgs++] = cp;
166         }
167     }
168
169     /*
170      * If we haven't specified -add, -delete, or -list,
171      * then use -add if a sequence was specified, else
172      * use -list.
173      */
174     if (!addsw && !deletesw && !listsw) {
175         if (seqp)
176             addsw++;
177         else
178             listsw++;
179     }
180
181     if (!context_find ("path"))
182         free (path ("./", TFOLDER));
183     if (!nummsgs)
184         msgs[nummsgs++] = listsw ? "all" :"cur";
185     if (!folder)
186         folder = getfolder (1);
187     maildir = m_maildir (folder);
188
189     if (chdir (maildir) == NOTOK)
190         adios (maildir, "unable to change directory to");
191
192     /* read folder and create message structure */
193     if (!(mp = folder_read (folder)))
194         adios (NULL, "unable to read folder %s", folder);
195
196     /* print some general debugging info */
197     if (debugsw)
198         print_debug(mp);
199
200     /* check for empty folder */
201     if (mp->nummsg == 0)
202         adios (NULL, "no messages in %s", folder);
203
204     /* parse all the message ranges/sequences and set SELECTED */
205     for (msgnum = 0; msgnum < nummsgs; msgnum++)
206         if (!m_convert (mp, msgs[msgnum]))
207             done (1);
208
209     if (publicsw == 1 && is_readonly(mp))
210         adios (NULL, "folder %s is read-only, so -public not allowed", folder);
211
212     /*
213      * Make sure at least one sequence has been
214      * specified if we are adding or deleting.
215      */
216     if (seqp == 0 && (addsw || deletesw))
217         adios (NULL, "-%s requires at least one -sequence argument",
218                addsw ? "add" : "delete");
219     seqs[seqp] = NULL;
220
221     /* Adding messages to sequences */
222     if (addsw) {
223         for (seqp = 0; seqs[seqp]; seqp++)
224             if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
225                 done (1);
226     }
227
228     /* Deleting messages from sequences */
229     if (deletesw) {
230         for (seqp = 0; seqs[seqp]; seqp++)
231             if (!seq_delsel (mp, seqs[seqp], publicsw, zerosw))
232                 done (1);
233     }
234
235     /* Listing messages in sequences */
236     if (listsw) {
237         if (seqp) {
238             /* print the sequences given */
239             for (seqp = 0; seqs[seqp]; seqp++)
240                 seq_print (mp, seqs[seqp]);
241         } else {
242             /* else print them all */
243             seq_printall (mp);
244         }
245
246         /* print debugging info about SELECTED messages */
247         if (debugsw)
248             seq_printdebug (mp);
249     }
250
251     seq_save (mp);                      /* synchronize message sequences */
252     context_replace (pfolder, folder);  /* update current folder         */
253     context_save ();                    /* save the context file         */
254     folder_free (mp);                   /* free folder/message structure */
255     return done (0);
256 }
257
258
259 /*
260  * Print general debugging info
261  */
262 static void
263 print_debug (struct msgs *mp)
264 {
265     char buf[100];
266
267     printf ("invo_name     = %s\n", invo_name);
268     printf ("mypath        = %s\n", mypath);
269     printf ("defpath       = %s\n", defpath);
270     printf ("ctxpath       = %s\n", ctxpath);
271     printf ("context flags = %s\n", snprintb (buf, sizeof(buf),
272                 (unsigned) ctxflags, DBITS));
273     printf ("foldpath      = %s\n", mp->foldpath);
274     printf ("folder flags  = %s\n\n", snprintb(buf, sizeof(buf),
275                 (unsigned) mp->msgflags, FBITS));
276     printf ("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
277         mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
278     printf ("lowsel=%d hghsel=%d numsel=%d\n",
279         mp->lowsel, mp->hghsel, mp->numsel);
280     printf ("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
281 }
282
283
284 /*
285  * Print debugging info about all the SELECTED
286  * messages and the sequences they are in.
287  */
288 static void
289 seq_printdebug (struct msgs *mp)
290 {
291     int msgnum;
292     char buf[100];
293
294     printf ("\n");
295     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
296         if (is_selected (mp, msgnum))
297             printf ("%*d: %s\n", DMAXFOLDER, msgnum,
298                 snprintb (buf, sizeof(buf),
299                 (unsigned) mp->msgstats[msgnum - mp->lowoff], seq_bits (mp)));
300     }
301 }