Add/update copyright notice in all source code files.
[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
16 /*
17  * We allocate space for messages (msgs array)
18  * this number of elements at a time.
19  */
20 #define MAXMSGS  256
21
22
23 static struct swit switches[] = {
24 #define ADDSW               0
25     { "add", 0 },
26 #define DELSW               1
27     { "delete", 0 },
28 #define LSTSW               2
29     { "list", 0 },
30 #define SEQSW               3
31     { "sequence name", 0 },
32 #define PUBLSW              4
33     { "public", 0 },
34 #define NPUBLSW             5
35     { "nopublic", 0 },
36 #define ZEROSW              6
37     { "zero", 0 },
38 #define NZEROSW             7
39     { "nozero", 0 },
40 #define VERSIONSW           8
41     { "version", 0 },
42 #define HELPSW              9
43     { "help", 0 },
44 #define DEBUGSW            10
45     { "debug", -5 },
46     { NULL, 0 }
47 };
48
49 /*
50  * static prototypes
51  */
52 static void print_debug (struct msgs *);
53 static void seq_printdebug (struct msgs *);
54
55
56 int
57 main (int argc, char **argv)
58 {
59     int addsw = 0, deletesw = 0, debugsw = 0;
60     int listsw = 0, publicsw = -1, zerosw = 0;
61     int seqp = 0, msgnum, nummsgs, maxmsgs;
62     char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
63     char **argp, **arguments;
64     char *seqs[NUMATTRS + 1], **msgs;
65     struct msgs *mp;
66
67 #ifdef LOCALE
68     setlocale(LC_ALL, "");
69 #endif
70     invo_name = r1bindex (argv[0], '/');
71
72     /* read user profile/context */
73     context_read();
74
75     arguments = getarguments (invo_name, argc, argv, 1);
76     argp = arguments;
77
78     /*
79      * Allocate the initial space to record message
80      * names, ranges, and sequences.
81      */
82     nummsgs = 0;
83     maxmsgs = MAXMSGS;
84     if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
85         adios (NULL, "unable to allocate storage");
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 }