27652e081a221c508caa5aa5f8a5d1b4eb7f2526
[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                 if (!(msgs = (char **) realloc (msgs,
163                                                 (size_t) (maxmsgs * sizeof(*msgs)))))
164                     adios (NULL, "unable to reallocate msgs storage");
165             }
166             msgs[nummsgs++] = cp;
167         }
168     }
169
170     /*
171      * If we haven't specified -add, -delete, or -list,
172      * then use -add if a sequence was specified, else
173      * use -list.
174      */
175     if (!addsw && !deletesw && !listsw) {
176         if (seqp)
177             addsw++;
178         else
179             listsw++;
180     }
181
182     if (!context_find ("path"))
183         free (path ("./", TFOLDER));
184     if (!nummsgs)
185         msgs[nummsgs++] = listsw ? "all" :"cur";
186     if (!folder)
187         folder = getfolder (1);
188     maildir = m_maildir (folder);
189
190     if (chdir (maildir) == NOTOK)
191         adios (maildir, "unable to change directory to");
192
193     /* read folder and create message structure */
194     if (!(mp = folder_read (folder)))
195         adios (NULL, "unable to read folder %s", folder);
196
197     /* print some general debugging info */
198     if (debugsw)
199         print_debug(mp);
200
201     /* check for empty folder */
202     if (mp->nummsg == 0)
203         adios (NULL, "no messages in %s", folder);
204
205     /* parse all the message ranges/sequences and set SELECTED */
206     for (msgnum = 0; msgnum < nummsgs; msgnum++)
207         if (!m_convert (mp, msgs[msgnum]))
208             done (1);
209
210     if (publicsw == 1 && is_readonly(mp))
211         adios (NULL, "folder %s is read-only, so -public not allowed", folder);
212
213     /*
214      * Make sure at least one sequence has been
215      * specified if we are adding or deleting.
216      */
217     if (seqp == 0 && (addsw || deletesw))
218         adios (NULL, "-%s requires at least one -sequence argument",
219                addsw ? "add" : "delete");
220     seqs[seqp] = NULL;
221
222     /* Adding messages to sequences */
223     if (addsw) {
224         for (seqp = 0; seqs[seqp]; seqp++)
225             if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
226                 done (1);
227     }
228
229     /* Deleting messages from sequences */
230     if (deletesw) {
231         for (seqp = 0; seqs[seqp]; seqp++)
232             if (!seq_delsel (mp, seqs[seqp], publicsw, zerosw))
233                 done (1);
234     }
235
236     /* Listing messages in sequences */
237     if (listsw) {
238         if (seqp) {
239             /* print the sequences given */
240             for (seqp = 0; seqs[seqp]; seqp++)
241                 seq_print (mp, seqs[seqp]);
242         } else {
243             /* else print them all */
244             seq_printall (mp);
245         }
246
247         /* print debugging info about SELECTED messages */
248         if (debugsw)
249             seq_printdebug (mp);
250     }
251
252     seq_save (mp);                      /* synchronize message sequences */
253     context_replace (pfolder, folder);  /* update current folder         */
254     context_save ();                    /* save the context file         */
255     folder_free (mp);                   /* free folder/message structure */
256     return done (0);
257 }
258
259
260 /*
261  * Print general debugging info
262  */
263 static void
264 print_debug (struct msgs *mp)
265 {
266     char buf[100];
267
268     printf ("invo_name     = %s\n", invo_name);
269     printf ("mypath        = %s\n", mypath);
270     printf ("defpath       = %s\n", defpath);
271     printf ("ctxpath       = %s\n", ctxpath);
272     printf ("context flags = %s\n", snprintb (buf, sizeof(buf),
273                 (unsigned) ctxflags, DBITS));
274     printf ("foldpath      = %s\n", mp->foldpath);
275     printf ("folder flags  = %s\n\n", snprintb(buf, sizeof(buf),
276                 (unsigned) mp->msgflags, FBITS));
277     printf ("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
278         mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
279     printf ("lowsel=%d hghsel=%d numsel=%d\n",
280         mp->lowsel, mp->hghsel, mp->numsel);
281     printf ("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
282 }
283
284
285 /*
286  * Print debugging info about all the SELECTED
287  * messages and the sequences they are in.
288  */
289 static void
290 seq_printdebug (struct msgs *mp)
291 {
292     int msgnum;
293     char buf[100];
294
295     printf ("\n");
296     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
297         if (is_selected (mp, msgnum))
298             printf ("%*d: %s\n", DMAXFOLDER, msgnum,
299                 snprintb (buf, sizeof(buf),
300                 (unsigned) mp->msgstats[msgnum - mp->lowoff], seq_bits (mp)));
301     }
302 }