Reformated comments and long lines
[mmh] / uip / mark.c
1 /*
2 ** mark.c -- add message(s) to sequences in given folder
3 **        -- delete messages (s) from sequences in given folder
4 **        -- list sequences in given folder
5 **
6 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
7 ** COPYRIGHT file in the root directory of the nmh distribution for
8 ** complete copyright information.
9 */
10
11 #include <h/mh.h>
12 #include <h/utils.h>
13
14 static struct swit switches[] = {
15 #define ADDSW  0
16         { "add", 0 },
17 #define DELSW  1
18         { "delete", 0 },
19 #define LSTSW  2
20         { "list", 0 },
21 #define SEQSW  3
22         { "sequence name", 0 },
23 #define PUBLSW  4
24         { "public", 0 },
25 #define NPUBLSW  5
26         { "nopublic", 0 },
27 #define ZEROSW  6
28         { "zero", 0 },
29 #define NZEROSW  7
30         { "nozero", 0 },
31 #define VERSIONSW  8
32         { "version", 0 },
33 #define HELPSW  9
34         { "help", 0 },
35 #define DEBUGSW  10
36         { "debug", -5 },
37         { NULL, 0 }
38 };
39
40 /*
41 ** static prototypes
42 */
43 static void print_debug (struct msgs *);
44 static void seq_printdebug (struct msgs *);
45
46
47 int
48 main (int argc, char **argv)
49 {
50         int addsw = 0, deletesw = 0, debugsw = 0;
51         int listsw = 0, publicsw = -1, zerosw = 0;
52         int seqp = 0, msgnum;
53         char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
54         char **argp, **arguments;
55         char *seqs[NUMATTRS + 1];
56         struct msgs_array msgs = { 0, 0, NULL };
57         struct msgs *mp;
58
59 #ifdef LOCALE
60         setlocale(LC_ALL, "");
61 #endif
62         invo_name = r1bindex (argv[0], '/');
63
64         /* read user profile/context */
65         context_read();
66
67         arguments = getarguments (invo_name, argc, argv, 1);
68         argp = arguments;
69
70         /*
71         ** Parse arguments
72         */
73         while ((cp = *argp++)) {
74                 if (*cp == '-') {
75                         switch (smatch (++cp, switches)) {
76                         case AMBIGSW:
77                                 ambigsw (cp, switches);
78                                 done (1);
79                         case UNKWNSW:
80                                 adios (NULL, "-%s unknown\n", cp);
81
82                         case HELPSW:
83                                 snprintf (buf, sizeof(buf),
84                                         "%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],
275                                 seq_bits (mp)));
276         }
277 }