]>
diplodocus.org Git - nmh/blob - uip/anno.c
3 * anno.c -- annotate messages
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.
11 * Four new options have been added: delete, list, number, and draft.
12 * Message header fields are used by the new MIME attachment code in
13 * the send command. Adding features to generalize the anno command
14 * seemed to be a better approach than the creation of a new command
15 * whose features would overlap with those of the anno command.
17 * The -draft option causes anno to operate on the current draft file
18 * instead of on a message sequence.
20 * The -delete option deletes header elements that match the -component
21 * field name. If -delete is used without the -text option, the first
22 * header field whose field name matches the component name is deleted.
23 * If the -delete is used with the -text option, and the -text argument
24 * begins with a /, the first header field whose field name matches the
25 * component name and whose field body matches the text is deleted. If
26 * the -text argument does not begin with a /, then the text is assumed
27 * to be the last component of a path name, and the first header field
28 * whose field name matches the component name and a field body whose
29 * last path name component matches the text is deleted. If the -delete
30 * option is used with the new -number option described below, the nth
31 * header field whose field name matches the component name is deleted.
32 * No header fields are deleted if none of the above conditions are met.
34 * The -list option outputs the field bodies from each header field whose
35 * field name matches the component name, one per line. If no -text
36 * option is specified, only the last path name component of each field
37 * body is output. The entire field body is output if the -text option
38 * is used; the contents of the -text argument are ignored. If the -list
39 * option is used in conjuction with the new -number option described
40 * below, each line is numbered starting with 1. A tab separates the
41 * number from the field body.
43 * The -number option works with both the -delete and -list options as
44 * described above. The -number option takes an optional argument. A
45 * value of 1 is assumed if this argument is absent.
51 * We allocate space for messages (msgs array)
52 * this number of elements at a time.
57 static struct swit switches
[] = {
59 { "component field", 0 },
86 #define NOPRESERVESW 14
94 static void make_comp (char **);
98 main (int argc
, char **argv
)
100 int inplace
= 1, datesw
= 1;
101 int nummsgs
, maxmsgs
, msgnum
;
102 char *cp
, *maildir
, *comp
= NULL
;
103 char *text
= NULL
, *folder
= NULL
, buf
[BUFSIZ
];
104 char **argp
, **arguments
, **msgs
;
106 int append
= 0; /* append annotations instead of default prepend */
107 int delete = -2; /* delete header element if set */
108 char *draft
= (char *)0; /* draft file name */
109 int isdf
= 0; /* return needed for m_draft() */
110 int list
= 0; /* list header elements if set */
111 int number
= 0; /* delete specific number of like elements if set */
114 setlocale(LC_ALL
, "");
116 invo_name
= r1bindex (argv
[0], '/');
118 /* read user profile/context */
121 arguments
= getarguments (invo_name
, argc
, argv
, 1);
125 * Allocate the initial space to record message
130 if (!(msgs
= (char **) malloc ((size_t) (maxmsgs
* sizeof(*msgs
)))))
131 adios (NULL
, "unable to allocate storage");
133 while ((cp
= *argp
++)) {
135 switch (smatch (++cp
, switches
)) {
137 ambigsw (cp
, switches
);
140 adios (NULL
, "-%s unknown", cp
);
143 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
145 print_help (buf
, switches
, 1);
148 print_version(invo_name
);
153 adios (NULL
, "only one component at a time!");
154 if (!(comp
= *argp
++) || *comp
== '-')
155 adios (NULL
, "missing argument to %s", argp
[-2]);
174 adios (NULL
, "only one body at a time!");
175 if (!(text
= *argp
++) || *text
== '-')
176 adios (NULL
, "missing argument to %s", argp
[-2]);
179 case DELETESW
: /* delete annotations */
183 case DRFTSW
: /* draft message specified */
187 case LISTSW
: /* produce a listing */
191 case NUMBERSW
: /* number listing or delete by number */
193 adios (NULL
, "only one number at a time!");
195 if (argp
- arguments
== argc
- 1 || **argp
== '-')
199 if (strcmp(*argp
, "all") == 0)
202 else if (!(number
= atoi(*argp
)))
203 adios (NULL
, "missing argument to %s", argp
[-2]);
211 case APPENDSW
: /* append annotations instead of default prepend */
215 case PRESERVESW
: /* preserve access and modification times on annotated message */
219 case NOPRESERVESW
: /* don't preserve access and modification times on annotated message (default) */
224 if (*cp
== '+' || *cp
== '@') {
226 adios (NULL
, "only one folder at a time!");
228 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
231 * Check if we need to allocate more space
232 * for message name/ranges.
234 if (nummsgs
>= maxmsgs
) {
236 if (!(msgs
= (char **) realloc (msgs
,
237 (size_t) (maxmsgs
* sizeof(*msgs
)))))
238 adios (NULL
, "unable to reallocate msgs storage");
240 msgs
[nummsgs
++] = cp
;
245 * We're dealing with the draft message instead of message numbers.
246 * Get the name of the draft and deal with it just as we do with
247 * message numbers below.
250 if (draft
!= (char *)0) {
252 adios(NULL
, "can only have message numbers or -draft.");
254 draft
= getcpy(m_draft(folder
, (char *)0, 1, &isdf
));
259 annolist(draft
, comp
, text
, number
);
261 annotate (draft
, comp
, text
, inplace
, datesw
, delete, append
);
267 if (strcmp(invo_name
, "fanno") == 0) /* ugh! */
271 if (!context_find ("path"))
272 free (path ("./", TFOLDER
));
274 msgs
[nummsgs
++] = "cur";
276 folder
= getfolder (1);
277 maildir
= m_maildir (folder
);
279 if (chdir (maildir
) == NOTOK
)
280 adios (maildir
, "unable to change directory to");
282 /* read folder and create message structure */
283 if (!(mp
= folder_read (folder
)))
284 adios (NULL
, "unable to read folder %s", folder
);
286 /* check for empty folder */
288 adios (NULL
, "no messages in %s", folder
);
290 /* parse all the message ranges/sequences and set SELECTED */
291 for (msgnum
= 0; msgnum
< nummsgs
; msgnum
++)
292 if (!m_convert (mp
, msgs
[msgnum
]))
297 /* annotate all the SELECTED messages */
298 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
299 if (is_selected(mp
, msgnum
)) {
301 annolist(m_name(msgnum
), comp
, text
, number
);
303 annotate (m_name (msgnum
), comp
, text
, inplace
, datesw
, delete, append
);
307 context_replace (pfolder
, folder
); /* update current folder */
308 seq_setcur (mp
, mp
->lowsel
); /* update current message */
309 seq_save (mp
); /* synchronize message sequences */
310 folder_free (mp
); /* free folder/message structure */
311 context_save (); /* save the context file */
316 make_comp (char **ap
)
322 printf ("Enter component name: ");
325 if (fgets (buffer
, sizeof buffer
, stdin
) == NULL
)
327 *ap
= trimcpy (buffer
);
330 if ((cp
= *ap
+ strlen (*ap
) - 1) > *ap
&& *cp
== ':')
332 if (strlen (*ap
) == 0)
333 adios (NULL
, "null component name");
335 adios (NULL
, "invalid component name %s", *ap
);
336 if (strlen (*ap
) >= NAMESZ
)
337 adios (NULL
, "too large component name %s", *ap
);
339 for (cp
= *ap
; *cp
; cp
++)
340 if (!isalnum (*cp
) && *cp
!= '-')
341 adios (NULL
, "invalid component name %s", *ap
);