Replace done with exit at uip
[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", 2 },
27 #define ZEROSW  6
28         { "zero", 0 },
29 #define NZEROSW  7
30         { "nozero", 2 },
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, msgnum;
52         unsigned int seqp = 0;
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         setlocale(LC_ALL, "");
60         invo_name = mhbasename(argv[0]);
61
62         /* read user profile/context */
63         context_read();
64
65         arguments = getarguments(invo_name, argc, argv, 1);
66         argp = arguments;
67
68         /*
69         ** Parse arguments
70         */
71         while ((cp = *argp++)) {
72                 if (*cp == '-') {
73                         switch (smatch(++cp, switches)) {
74                         case AMBIGSW:
75                                 ambigsw(cp, switches);
76                                 /*sysexits.h EX_USAGE*/
77                                 exit(1);
78                         case UNKWNSW:
79                                 adios(NULL, "-%s unknown\n", cp);
80
81                         case HELPSW:
82                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
83                                 print_help(buf, switches, 1);
84                                 exit(0);
85                         case VERSIONSW:
86                                 print_version(invo_name);
87                                 exit(0);
88
89                         case ADDSW:
90                                 addsw++;
91                                 deletesw = listsw = 0;
92                                 continue;
93                         case DELSW:
94                                 deletesw++;
95                                 addsw = listsw = 0;
96                                 continue;
97                         case LSTSW:
98                                 listsw++;
99                                 addsw = deletesw = 0;
100                                 continue;
101
102                         case SEQSW:
103                                 if (!(cp = *argp++) || *cp == '-')
104                                         adios(NULL, "missing argument to %s",
105                                                         argp[-2]);
106
107                                 /* check if too many sequences specified */
108                                 if (seqp >= NUMATTRS)
109                                         adios(NULL, "too many sequences (more than %d) specified", NUMATTRS);
110                                 seqs[seqp++] = cp;
111                                 continue;
112
113                         case PUBLSW:
114                                 publicsw = 1;
115                                 continue;
116                         case NPUBLSW:
117                                 publicsw = 0;
118                                 continue;
119
120                         case DEBUGSW:
121                                 debugsw++;
122                                 continue;
123
124                         case ZEROSW:
125                                 zerosw++;
126                                 continue;
127                         case NZEROSW:
128                                 zerosw = 0;
129                                 continue;
130                         }
131                 }
132                 if (*cp == '+' || *cp == '@') {
133                         if (folder)
134                                 adios(NULL, "only one folder at a time!");
135                         else
136                                 folder = getcpy(expandfol(cp));
137                 } else
138                         app_msgarg(&msgs, cp);
139         }
140
141         /*
142         ** If we haven't specified -add, -delete, or -list,
143         ** then use -add if a sequence was specified, else
144         ** use -list.
145         */
146         if (!addsw && !deletesw && !listsw) {
147                 if (seqp)
148                         addsw++;
149                 else
150                         listsw++;
151         }
152
153         if (!msgs.size)
154                 app_msgarg(&msgs, listsw ? seq_all : seq_cur);
155         if (!folder)
156                 folder = getcurfol();
157         maildir = toabsdir(folder);
158
159         if (chdir(maildir) == NOTOK)
160                 adios(maildir, "unable to change directory to");
161
162         /* read folder and create message structure */
163         if (!(mp = folder_read(folder)))
164                 adios(NULL, "unable to read folder %s", folder);
165
166         /* print some general debugging info */
167         if (debugsw)
168                 print_debug(mp);
169
170         /* check for empty folder */
171         if (mp->nummsg == 0)
172                 adios(NULL, "no messages in %s", folder);
173
174         /* parse all the message ranges/sequences and set SELECTED */
175         for (msgnum = 0; msgnum < msgs.size; msgnum++) {
176                 if (!m_convert(mp, msgs.msgs[msgnum])) {
177                         /*sysexits.h EX_USAGE*/
178                         exit(1);
179                 }
180         }
181
182         if (publicsw == 1 && is_readonly(mp))
183                 adios(NULL, "folder %s is read-only, so -public not allowed",
184                                 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                                 /*TODO find best exitcode*/
200                                 exit(1);
201                         }
202                 }
203         }
204
205         /* Deleting messages from sequences */
206         if (deletesw) {
207                 for (seqp = 0; seqs[seqp]; seqp++)
208                         if (!seq_delsel(mp, seqs[seqp], publicsw, zerosw))
209                                 /*TODO find best exitcode*/
210                                 exit(1);
211         }
212
213         /* Listing messages in sequences */
214         if (listsw) {
215                 if (seqp) {
216                         /* print the sequences given */
217                         for (seqp = 0; seqs[seqp]; seqp++)
218                                 seq_print(mp, seqs[seqp]);
219                 } else {
220                         /* else print them all */
221                         seq_printall(mp);
222                 }
223
224                 /* print debugging info about SELECTED messages */
225                 if (debugsw)
226                         seq_printdebug(mp);
227         }
228
229         seq_save(mp);  /* synchronize message sequences */
230         context_replace(curfolder, folder);  /* update current folder */
231         context_save();  /* save the context file */
232         folder_free(mp);  /* free folder/message structure */
233         return 0;
234 }
235
236
237 /*
238 ** Print general debugging info
239 */
240 static void
241 print_debug(struct msgs *mp)
242 {
243         char buf[100];
244
245         printf("invo_name = %s\n", invo_name);
246         printf("mypath = %s\n", mypath);
247         printf("defpath = %s\n", defpath);
248         printf("ctxpath = %s\n", ctxpath);
249         printf("context flags = %s\n", snprintb(buf, sizeof(buf),
250                         (unsigned) ctxflags, DBITS));
251         printf("foldpath = %s\n", mp->foldpath);
252         printf("folder flags  = %s\n\n", snprintb(buf, sizeof(buf),
253                         (unsigned) mp->msgflags, FBITS));
254         printf("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
255                         mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
256         printf("lowsel=%d hghsel=%d numsel=%d\n",
257                         mp->lowsel, mp->hghsel, mp->numsel);
258         printf("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
259 }
260
261
262 /*
263 ** Print debugging info about all the SELECTED
264 ** messages and the sequences they are in.
265 */
266 static void
267 seq_printdebug(struct msgs *mp)
268 {
269         int msgnum;
270         char buf[100];
271
272         printf("\n");
273         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
274                 if (is_selected(mp, msgnum))
275                         printf("%*d: %s\n", DMAXFOLDER, msgnum, snprintb(buf, sizeof(buf), (unsigned) mp->msgstats[msgnum - mp->lowoff], seq_bits(mp)));
276         }
277 }