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