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