]> diplodocus.org Git - nmh/blob - uip/mark.c
Removed temporary probes added in commit
[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 svector_t seqs = svector_create (0);
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 svector_push_back (seqs, cp);
105 seqp++;
106 continue;
107
108 case PUBLSW:
109 publicsw = 1;
110 continue;
111 case NPUBLSW:
112 publicsw = 0;
113 continue;
114
115 case DEBUGSW:
116 debugsw++;
117 continue;
118
119 case ZEROSW:
120 zerosw++;
121 continue;
122 case NZEROSW:
123 zerosw = 0;
124 continue;
125 }
126 }
127 if (*cp == '+' || *cp == '@') {
128 if (folder)
129 adios (NULL, "only one folder at a time!");
130 else
131 folder = pluspath (cp);
132 } else
133 app_msgarg(&msgs, cp);
134 }
135
136 /*
137 * If we haven't specified -add, -delete, or -list,
138 * then use -add if a sequence was specified, else
139 * use -list.
140 */
141 if (!addsw && !deletesw && !listsw) {
142 if (seqp)
143 addsw++;
144 else
145 listsw++;
146 }
147
148 if (!context_find ("path"))
149 free (path ("./", TFOLDER));
150 if (!msgs.size)
151 app_msgarg(&msgs, listsw ? "all" :"cur");
152 if (!folder)
153 folder = getfolder (1);
154 maildir = m_maildir (folder);
155
156 if (chdir (maildir) == NOTOK)
157 adios (maildir, "unable to change directory to");
158
159 /* read folder and create message structure */
160 if (!(mp = folder_read (folder, 1)))
161 adios (NULL, "unable to read folder %s", folder);
162
163 /* print some general debugging info */
164 if (debugsw)
165 print_debug(mp);
166
167 /* check for empty folder */
168 if (mp->nummsg == 0)
169 adios (NULL, "no messages in %s", folder);
170
171 /* parse all the message ranges/sequences and set SELECTED */
172 for (msgnum = 0; msgnum < msgs.size; msgnum++)
173 if (!m_convert (mp, msgs.msgs[msgnum]))
174 done (1);
175
176 if (publicsw == 1 && is_readonly(mp))
177 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
178
179 /*
180 * Make sure at least one sequence has been
181 * specified if we are adding or deleting.
182 */
183 if (seqp == 0 && (addsw || deletesw))
184 adios (NULL, "-%s requires at least one -sequence argument",
185 addsw ? "add" : "delete");
186
187 /* Adding messages to sequences */
188 if (addsw) {
189 for (seqp = 0; seqp < svector_size (seqs); seqp++)
190 if (!seq_addsel (mp, svector_at (seqs, seqp), publicsw, zerosw))
191 done (1);
192 }
193
194 /* Deleting messages from sequences */
195 if (deletesw) {
196 for (seqp = 0; seqp < svector_size (seqs); seqp++)
197 if (!seq_delsel (mp, svector_at (seqs, seqp), publicsw, zerosw))
198 done (1);
199 }
200
201 /* Listing messages in sequences */
202 if (listsw) {
203 if (seqp) {
204 /* print the sequences given */
205 for (seqp = 0; seqp < svector_size (seqs); seqp++)
206 seq_print (mp, svector_at (seqs, seqp));
207 } else {
208 /* else print them all */
209 seq_printall (mp);
210 }
211
212 /* print debugging info about SELECTED messages */
213 if (debugsw)
214 seq_printdebug (mp);
215 }
216
217 svector_free (seqs);
218 seq_save (mp); /* synchronize message sequences */
219 context_replace (pfolder, folder); /* update current folder */
220 context_save (); /* save the context file */
221 folder_free (mp); /* free folder/message structure */
222 done (0);
223 return 1;
224 }
225
226
227 /*
228 * Print general debugging info
229 */
230 static void
231 print_debug (struct msgs *mp)
232 {
233 char buf[100];
234
235 printf ("invo_name = %s\n", invo_name);
236 printf ("mypath = %s\n", mypath);
237 printf ("defpath = %s\n", defpath);
238 printf ("ctxpath = %s\n", ctxpath);
239 printf ("context flags = %s\n", snprintb (buf, sizeof(buf),
240 (unsigned) ctxflags, DBITS));
241 printf ("foldpath = %s\n", mp->foldpath);
242 printf ("folder flags = %s\n\n", snprintb(buf, sizeof(buf),
243 (unsigned) mp->msgflags, FBITS));
244 printf ("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
245 mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
246 printf ("lowsel=%d hghsel=%d numsel=%d\n",
247 mp->lowsel, mp->hghsel, mp->numsel);
248 printf ("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
249 }
250
251
252 /*
253 * Print debugging info about all the SELECTED
254 * messages and the sequences they are in.
255 * Due limitattions of snprintb(), only a limited
256 * number of sequences will be printed. See the
257 * comments in sbr/seq_bits.c.
258 */
259 static void
260 seq_printdebug (struct msgs *mp)
261 {
262 int msgnum;
263 char buf[BUFSIZ];
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,
269 snprintb (buf, sizeof buf,
270 (unsigned) *bvector_bits (msgstat (mp, msgnum)),
271 seq_bits (mp)));
272 }
273 }