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