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