]> diplodocus.org Git - nmh/blob - uip/mark.c
Document argsplit changes in mh-profile man page.
[nmh] / uip / mark.c
1
2 /*
3 * mark.c -- add message(s) to sequences in given folder
4 * -- delete messages (s) from sequences in given folder
5 * -- list sequences in given folder
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/utils.h>
14
15 #define MARK_SWITCHES \
16 X("add", 0, ADDSW) \
17 X("delete", 0, DELSW) \
18 X("list", 0, LSTSW) \
19 X("sequence name", 0, SEQSW) \
20 X("public", 0, PUBLSW) \
21 X("nopublic", 0, NPUBLSW) \
22 X("zero", 0, ZEROSW) \
23 X("nozero", 0, NZEROSW) \
24 X("version", 0, VERSIONSW) \
25 X("help", 0, HELPSW) \
26 X("debug", -5, DEBUGSW) \
27
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(MARK);
30 #undef X
31
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(MARK, switches);
34 #undef X
35
36 /*
37 * static prototypes
38 */
39 static void print_debug (struct msgs *);
40 static void seq_printdebug (struct msgs *);
41
42
43 int
44 main (int argc, char **argv)
45 {
46 int addsw = 0, deletesw = 0, debugsw = 0;
47 int listsw = 0, publicsw = -1, zerosw = 0, msgnum;
48 unsigned int seqp = 0;
49 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
50 char **argp, **arguments;
51 char *seqs[NUMATTRS + 1];
52 struct msgs_array msgs = { 0, 0, NULL };
53 struct msgs *mp;
54
55 #ifdef LOCALE
56 setlocale(LC_ALL, "");
57 #endif
58 invo_name = r1bindex (argv[0], '/');
59
60 /* read user profile/context */
61 context_read();
62
63 arguments = getarguments (invo_name, argc, argv, 1);
64 argp = arguments;
65
66 /*
67 * Parse arguments
68 */
69 while ((cp = *argp++)) {
70 if (*cp == '-') {
71 switch (smatch (++cp, switches)) {
72 case AMBIGSW:
73 ambigsw (cp, switches);
74 done (1);
75 case UNKWNSW:
76 adios (NULL, "-%s unknown\n", cp);
77
78 case HELPSW:
79 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
80 invo_name);
81 print_help (buf, switches, 1);
82 done (0);
83 case VERSIONSW:
84 print_version(invo_name);
85 done (0);
86
87 case ADDSW:
88 addsw++;
89 deletesw = listsw = 0;
90 continue;
91 case DELSW:
92 deletesw++;
93 addsw = listsw = 0;
94 continue;
95 case LSTSW:
96 listsw++;
97 addsw = deletesw = 0;
98 continue;
99
100 case SEQSW:
101 if (!(cp = *argp++) || *cp == '-')
102 adios (NULL, "missing argument to %s", argp[-2]);
103
104 /* check if too many sequences specified */
105 if (seqp >= NUMATTRS)
106 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
107 seqs[seqp++] = cp;
108 continue;
109
110 case PUBLSW:
111 publicsw = 1;
112 continue;
113 case NPUBLSW:
114 publicsw = 0;
115 continue;
116
117 case DEBUGSW:
118 debugsw++;
119 continue;
120
121 case ZEROSW:
122 zerosw++;
123 continue;
124 case NZEROSW:
125 zerosw = 0;
126 continue;
127 }
128 }
129 if (*cp == '+' || *cp == '@') {
130 if (folder)
131 adios (NULL, "only one folder at a time!");
132 else
133 folder = pluspath (cp);
134 } else
135 app_msgarg(&msgs, cp);
136 }
137
138 /*
139 * If we haven't specified -add, -delete, or -list,
140 * then use -add if a sequence was specified, else
141 * use -list.
142 */
143 if (!addsw && !deletesw && !listsw) {
144 if (seqp)
145 addsw++;
146 else
147 listsw++;
148 }
149
150 if (!context_find ("path"))
151 free (path ("./", TFOLDER));
152 if (!msgs.size)
153 app_msgarg(&msgs, listsw ? "all" :"cur");
154 if (!folder)
155 folder = getfolder (1);
156 maildir = m_maildir (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", folder);
180
181 /*
182 * Make sure at least one sequence has been
183 * specified if we are adding or deleting.
184 */
185 if (seqp == 0 && (addsw || deletesw))
186 adios (NULL, "-%s requires at least one -sequence argument",
187 addsw ? "add" : "delete");
188 seqs[seqp] = NULL;
189
190 /* Adding messages to sequences */
191 if (addsw) {
192 for (seqp = 0; seqs[seqp]; seqp++)
193 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
194 done (1);
195 }
196
197 /* Deleting messages from sequences */
198 if (deletesw) {
199 for (seqp = 0; seqs[seqp]; seqp++)
200 if (!seq_delsel (mp, seqs[seqp], publicsw, zerosw))
201 done (1);
202 }
203
204 /* Listing messages in sequences */
205 if (listsw) {
206 if (seqp) {
207 /* print the sequences given */
208 for (seqp = 0; seqs[seqp]; seqp++)
209 seq_print (mp, seqs[seqp]);
210 } else {
211 /* else print them all */
212 seq_printall (mp);
213 }
214
215 /* print debugging info about SELECTED messages */
216 if (debugsw)
217 seq_printdebug (mp);
218 }
219
220 seq_save (mp); /* synchronize message sequences */
221 context_replace (pfolder, folder); /* update current folder */
222 context_save (); /* save the context file */
223 folder_free (mp); /* free folder/message structure */
224 done (0);
225 return 1;
226 }
227
228
229 /*
230 * Print general debugging info
231 */
232 static void
233 print_debug (struct msgs *mp)
234 {
235 char buf[100];
236
237 printf ("invo_name = %s\n", invo_name);
238 printf ("mypath = %s\n", mypath);
239 printf ("defpath = %s\n", defpath);
240 printf ("ctxpath = %s\n", ctxpath);
241 printf ("context flags = %s\n", snprintb (buf, sizeof(buf),
242 (unsigned) ctxflags, DBITS));
243 printf ("foldpath = %s\n", mp->foldpath);
244 printf ("folder flags = %s\n\n", snprintb(buf, sizeof(buf),
245 (unsigned) mp->msgflags, FBITS));
246 printf ("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
247 mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
248 printf ("lowsel=%d hghsel=%d numsel=%d\n",
249 mp->lowsel, mp->hghsel, mp->numsel);
250 printf ("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
251 }
252
253
254 /*
255 * Print debugging info about all the SELECTED
256 * messages and the sequences they are in.
257 */
258 static void
259 seq_printdebug (struct msgs *mp)
260 {
261 int msgnum;
262 char buf[100];
263
264 printf ("\n");
265 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
266 if (is_selected (mp, msgnum))
267 printf ("%*d: %s\n", DMAXFOLDER, msgnum,
268 snprintb (buf, sizeof(buf),
269 (unsigned) mp->msgstats[msgnum - mp->lowoff], seq_bits (mp)));
270 }
271 }