Move #include from h/mh.h to source files
[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 #include <unistd.h>
14 #include <locale.h>
15
16 static struct swit switches[] = {
17 #define ADDSW  0
18         { "add", 0 },
19 #define DELSW  1
20         { "delete", 0 },
21 #define LSTSW  2
22         { "list", 0 },
23 #define SEQSW  3
24         { "sequence name", 0 },
25 #define PUBLSW  4
26         { "public", 0 },
27 #define NPUBLSW  5
28         { "nopublic", 2 },
29 #define ZEROSW  6
30         { "zero", 0 },
31 #define NZEROSW  7
32         { "nozero", 2 },
33 #define VERSIONSW  8
34         { "Version", 0 },
35 #define HELPSW  9
36         { "help", 0 },
37 #define DEBUGSW  10
38         { "debug", -5 },
39         { NULL, 0 }
40 };
41
42 /*
43 ** static prototypes
44 */
45 static void print_debug(struct msgs *);
46 static void seq_printdebug(struct msgs *);
47
48
49 int
50 main(int argc, char **argv)
51 {
52         int addsw = 0, deletesw = 0, debugsw = 0;
53         int listsw = 0, publicsw = -1, zerosw = 0, msgnum;
54         unsigned int seqp = 0;
55         char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
56         char **argp, **arguments;
57         char *seqs[NUMATTRS + 1];
58         struct msgs_array msgs = { 0, 0, NULL };
59         struct msgs *mp;
60
61         setlocale(LC_ALL, "");
62         invo_name = mhbasename(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                                 /*sysexits.h EX_USAGE*/
79                                 exit(1);
80                         case UNKWNSW:
81                                 adios(NULL, "-%s unknown\n", cp);
82
83                         case HELPSW:
84                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
85                                 print_help(buf, switches, 1);
86                                 exit(0);
87                         case VERSIONSW:
88                                 print_version(invo_name);
89                                 exit(0);
90
91                         case ADDSW:
92                                 addsw++;
93                                 deletesw = listsw = 0;
94                                 continue;
95                         case DELSW:
96                                 deletesw++;
97                                 addsw = listsw = 0;
98                                 continue;
99                         case LSTSW:
100                                 listsw++;
101                                 addsw = deletesw = 0;
102                                 continue;
103
104                         case SEQSW:
105                                 if (!(cp = *argp++) || *cp == '-')
106                                         adios(NULL, "missing argument to %s",
107                                                         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 = getcpy(expandfol(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 (!msgs.size)
156                 app_msgarg(&msgs, listsw ? seq_all : seq_cur);
157         if (!folder)
158                 folder = getcurfol();
159         maildir = toabsdir(folder);
160
161         if (chdir(maildir) == NOTOK)
162                 adios(maildir, "unable to change directory to");
163
164         /* read folder and create message structure */
165         if (!(mp = folder_read(folder)))
166                 adios(NULL, "unable to read folder %s", folder);
167
168         /* print some general debugging info */
169         if (debugsw)
170                 print_debug(mp);
171
172         /* check for empty folder */
173         if (mp->nummsg == 0)
174                 adios(NULL, "no messages in %s", folder);
175
176         /* parse all the message ranges/sequences and set SELECTED */
177         for (msgnum = 0; msgnum < msgs.size; msgnum++) {
178                 if (!m_convert(mp, msgs.msgs[msgnum])) {
179                         /*sysexits.h EX_USAGE*/
180                         exit(1);
181                 }
182         }
183
184         if (publicsw == 1 && is_readonly(mp))
185                 adios(NULL, "folder %s is read-only, so -public not allowed",
186                                 folder);
187
188         /*
189         ** Make sure at least one sequence has been
190         ** specified if we are adding or deleting.
191         */
192         if (seqp == 0 && (addsw || deletesw))
193                 adios(NULL, "-%s requires at least one -sequence argument",
194                                 addsw ? "add" : "delete");
195         seqs[seqp] = NULL;
196
197         /* Adding messages to sequences */
198         if (addsw) {
199                 for (seqp = 0; seqs[seqp]; seqp++) {
200                         if (!seq_addsel(mp, seqs[seqp], publicsw, zerosw)) {
201                                 /*TODO find best exitcode*/
202                                 exit(1);
203                         }
204                 }
205         }
206
207         /* Deleting messages from sequences */
208         if (deletesw) {
209                 for (seqp = 0; seqs[seqp]; seqp++)
210                         if (!seq_delsel(mp, seqs[seqp], publicsw, zerosw))
211                                 /*TODO find best exitcode*/
212                                 exit(1);
213         }
214
215         /* Listing messages in sequences */
216         if (listsw) {
217                 if (seqp) {
218                         /* print the sequences given */
219                         for (seqp = 0; seqs[seqp]; seqp++)
220                                 seq_print(mp, seqs[seqp]);
221                 } else {
222                         /* else print them all */
223                         seq_printall(mp);
224                 }
225
226                 /* print debugging info about SELECTED messages */
227                 if (debugsw)
228                         seq_printdebug(mp);
229         }
230
231         seq_save(mp);  /* synchronize message sequences */
232         context_replace(curfolder, folder);  /* update current folder */
233         context_save();  /* save the context file */
234         folder_free(mp);  /* free folder/message structure */
235         return 0;
236 }
237
238
239 /*
240 ** Print general debugging info
241 */
242 static void
243 print_debug(struct msgs *mp)
244 {
245         char buf[100];
246
247         printf("invo_name = %s\n", invo_name);
248         printf("mypath = %s\n", mypath);
249         printf("defpath = %s\n", defpath);
250         printf("ctxpath = %s\n", ctxpath);
251         printf("context flags = %s\n", snprintb(buf, sizeof(buf),
252                         (unsigned) ctxflags, DBITS));
253         printf("foldpath = %s\n", mp->foldpath);
254         printf("folder flags  = %s\n\n", snprintb(buf, sizeof(buf),
255                         (unsigned) mp->msgflags, FBITS));
256         printf("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
257                         mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
258         printf("lowsel=%d hghsel=%d numsel=%d\n",
259                         mp->lowsel, mp->hghsel, mp->numsel);
260         printf("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
261 }
262
263
264 /*
265 ** Print debugging info about all the SELECTED
266 ** messages and the sequences they are in.
267 */
268 static void
269 seq_printdebug(struct msgs *mp)
270 {
271         int msgnum;
272         char buf[100];
273
274         printf("\n");
275         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
276                 if (is_selected(mp, msgnum))
277                         printf("%*d: %s\n", DMAXFOLDER, msgnum, snprintb(buf, sizeof(buf), (unsigned) mp->msgstats[msgnum - mp->lowoff], seq_bits(mp)));
278         }
279 }