]> diplodocus.org Git - nmh/blob - uip/anno.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / anno.c
1 /* anno.c -- annotate messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 *
7 * Four new options have been added: delete, list, number, and draft.
8 * Message header fields are used by the new MIME attachment code in
9 * the send command. Adding features to generalize the anno command
10 * seemed to be a better approach than the creation of a new command
11 * whose features would overlap with those of the anno command.
12 *
13 * The -draft option causes anno to operate on the current draft file
14 * instead of on a message sequence.
15 *
16 * The -delete option deletes header elements that match the -component
17 * field name. If -delete is used without the -text option, the first
18 * header field whose field name matches the component name is deleted.
19 * If the -delete is used with the -text option, and the -text argument
20 * begins with a /, the first header field whose field name matches the
21 * component name and whose field body matches the text is deleted. If
22 * the -text argument does not begin with a /, then the text is assumed
23 * to be the last component of a path name, and the first header field
24 * whose field name matches the component name and a field body whose
25 * last path name component matches the text is deleted. If the -delete
26 * option is used with the new -number option described below, the nth
27 * header field whose field name matches the component name is deleted.
28 * No header fields are deleted if none of the above conditions are met.
29 *
30 * The -list option outputs the field bodies from each header field whose
31 * field name matches the component name, one per line. If no -text
32 * option is specified, only the last path name component of each field
33 * body is output. The entire field body is output if the -text option
34 * is used; the contents of the -text argument are ignored. If the -list
35 * option is used in conjunction with the new -number option described
36 * below, each line is numbered starting with 1. A tab separates the
37 * number from the field body.
38 *
39 * The -number option works with both the -delete and -list options as
40 * described above. The -number option takes an optional argument. A
41 * value of 1 is assumed if this argument is absent.
42 */
43
44 #include <h/mh.h>
45 #include <h/utils.h>
46 #include "h/done.h"
47 #include "sbr/m_maildir.h"
48
49 #define ANNO_SWITCHES \
50 X("component field", 0, COMPSW) \
51 X("inplace", 0, INPLSW) \
52 X("noinplace", 0, NINPLSW) \
53 X("date", 0, DATESW) \
54 X("nodate", 0, NDATESW) \
55 X("text body", 0, TEXTSW) \
56 X("version", 0, VERSIONSW) \
57 X("help", 0, HELPSW) \
58 X("draft", 2, DRFTSW) \
59 X("list", 1, LISTSW) \
60 X("delete", 2, DELETESW) \
61 X("number", 2, NUMBERSW) \
62 X("append", 1, APPENDSW) \
63 X("preserve", 1, PRESERVESW) \
64 X("nopreserve", 3, NOPRESERVESW) \
65
66 #define X(sw, minchars, id) id,
67 DEFINE_SWITCH_ENUM(ANNO);
68 #undef X
69
70 #define X(sw, minchars, id) { sw, minchars, id },
71 DEFINE_SWITCH_ARRAY(ANNO, switches);
72 #undef X
73
74 /*
75 * static prototypes
76 */
77 static void make_comp (char **);
78
79
80 int
81 main (int argc, char **argv)
82 {
83 bool inplace, datesw;
84 int msgnum;
85 char *cp, *maildir;
86 char *comp = NULL, *text = NULL, *folder = NULL, buf[BUFSIZ];
87 char **argp, **arguments;
88 struct msgs_array msgs = { 0, 0, NULL };
89 struct msgs *mp;
90 bool append; /* append annotations instead of default prepend */
91 int delete = -2; /* delete header element if set */
92 char *draft = NULL; /* draft file name */
93 int isdf = 0; /* return needed for m_draft() */
94 bool list; /* list header elements if set */
95 int number = 0; /* delete specific number of like elements if set */
96
97 if (nmh_init(argv[0], true, true)) { return 1; }
98
99 arguments = getarguments (invo_name, argc, argv, 1);
100 argp = arguments;
101
102 append = list = false;
103 inplace = datesw = true;
104 while ((cp = *argp++)) {
105 if (*cp == '-') {
106 switch (smatch (++cp, switches)) {
107 case AMBIGSW:
108 ambigsw (cp, switches);
109 done (1);
110 case UNKWNSW:
111 die("-%s unknown", cp);
112
113 case HELPSW:
114 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
115 invo_name);
116 print_help (buf, switches, 1);
117 done (0);
118 case VERSIONSW:
119 print_version(invo_name);
120 done (0);
121
122 case COMPSW:
123 if (comp)
124 die("only one component at a time!");
125 if (!(comp = *argp++) || *comp == '-')
126 die("missing argument to %s", argp[-2]);
127 continue;
128
129 case DATESW:
130 datesw = true;
131 continue;
132 case NDATESW:
133 datesw = false;
134 continue;
135
136 case INPLSW:
137 inplace = true;
138 continue;
139 case NINPLSW:
140 inplace = false;
141 continue;
142
143 case TEXTSW:
144 if (text)
145 die("only one body at a time!");
146 if (!(text = *argp++) || *text == '-')
147 die("missing argument to %s", argp[-2]);
148 continue;
149
150 case DELETESW: /* delete annotations */
151 delete = 0;
152 continue;
153
154 case DRFTSW: /* draft message specified */
155 draft = "";
156 continue;
157
158 case LISTSW: /* produce a listing */
159 list = true;
160 continue;
161
162 case NUMBERSW: /* number listing or delete by number */
163 if (number != 0)
164 die("only one number at a time!");
165
166 if (argp - arguments == argc - 1 || **argp == '-')
167 number = 1;
168
169 else {
170 if (strcmp(*argp, "all") == 0)
171 number = -1;
172
173 else if (!(number = atoi(*argp)))
174 die("missing argument to %s", argp[-1]);
175
176 argp++;
177 }
178
179 delete = number;
180 continue;
181
182 case APPENDSW: /* append annotations instead of default prepend */
183 append = true;
184 continue;
185
186 case PRESERVESW: /* preserve access and modification times on annotated message */
187 annopreserve(1);
188 continue;
189
190 case NOPRESERVESW: /* don't preserve access and modification times on annotated message (default) */
191 annopreserve(0);
192 continue;
193 }
194 }
195 if (*cp == '+' || *cp == '@') {
196 if (folder)
197 die("only one folder at a time!");
198 folder = pluspath (cp);
199 } else
200 app_msgarg(&msgs, cp);
201 }
202
203 /*
204 * We're dealing with the draft message instead of message numbers.
205 * Get the name of the draft and deal with it just as we do with
206 * message numbers below.
207 */
208
209 if (draft != NULL) {
210 if (msgs.size != 0)
211 die("can only have message numbers or -draft.");
212
213 draft = mh_xstrdup(m_draft(folder, NULL, 1, &isdf));
214
215 make_comp(&comp);
216
217 if (list)
218 annolist(draft, comp, text, number);
219 else
220 annotate (draft, comp, text, inplace, datesw, delete, append);
221
222 done(0);
223 return 1;
224 }
225
226 if (!context_find ("path"))
227 free (path ("./", TFOLDER));
228 if (!msgs.size)
229 app_msgarg(&msgs, "cur");
230 if (!folder)
231 folder = getfolder (1);
232 maildir = m_maildir (folder);
233
234 if (chdir (maildir) == NOTOK)
235 adios (maildir, "unable to change directory to");
236
237 /* read folder and create message structure */
238 if (!(mp = folder_read (folder, 1)))
239 die("unable to read folder %s", folder);
240
241 /* check for empty folder */
242 if (mp->nummsg == 0)
243 die("no messages in %s", folder);
244
245 /* parse all the message ranges/sequences and set SELECTED */
246 for (msgnum = 0; msgnum < msgs.size; msgnum++)
247 if (!m_convert (mp, msgs.msgs[msgnum]))
248 done (1);
249
250 make_comp (&comp);
251
252 /* annotate all the SELECTED messages */
253 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
254 if (is_selected(mp, msgnum)) {
255 if (list)
256 annolist(m_name(msgnum), comp, text, number);
257 else
258 annotate (m_name (msgnum), comp, text, inplace, datesw, delete, append);
259 }
260 }
261
262 context_replace (pfolder, folder); /* update current folder */
263 seq_setcur (mp, mp->lowsel); /* update current message */
264 seq_save (mp); /* synchronize message sequences */
265 folder_free (mp); /* free folder/message structure */
266 context_save (); /* save the context file */
267 done (0);
268 return 1;
269 }
270
271 static void
272 make_comp (char **ap)
273 {
274 char *cp, buffer[BUFSIZ];
275
276 if (*ap == NULL) {
277 fputs("Enter component name: ", stdout);
278 fflush (stdout);
279
280 if (fgets (buffer, sizeof buffer, stdin) == NULL)
281 done (1);
282 *ap = trimcpy (buffer);
283 }
284
285 if ((cp = *ap + strlen (*ap) - 1) > *ap && *cp == ':')
286 *cp = 0;
287 if (!**ap)
288 die("null component name");
289 if (**ap == '-')
290 die("invalid component name %s", *ap);
291 if (strlen (*ap) >= NAMESZ)
292 die("too large component name %s", *ap);
293
294 for (cp = *ap; *cp; cp++)
295 if (!isalnum ((unsigned char) *cp) && *cp != '-')
296 die("invalid component name %s", *ap);
297 }