2 ** annosbr.c -- prepend annotation to messages
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
20 static int annosbr(int, char *, char *, char *, int, int, int);
24 annotate(char *file, char *comp, char *text, int datesw,
25 int delete, int append, int preserve)
31 /* open and lock the file to be annotated */
32 if ((fd = lkopen(file, O_RDWR, 0)) == NOTOK) {
38 admonish(file, "unable to lock and open");
44 if (stat(file, &s) == -1) {
45 advise("can't get access and modification times for %s", file);
49 b.actime = s.st_atime;
50 b.modtime = s.st_mtime;
52 i = annosbr(fd, file, comp, text, datesw, delete, append);
54 if (preserve && utime(file, &b) == -1)
55 advise("can't set access and modification times for %s", file);
62 ** Produce a listing of all header fields (annotations) whose field
63 ** name matches comp. Number the listing if number is set. Treate the
64 ** field bodies as path names and just output the last component unless
65 ** text is non-NULL. We don't care what text is set to.
69 annolist(char *file, char *comp, char *text, int number)
71 int c; /* current character */
72 int count; /* header field (annotation) counter */
73 char *cp; /* miscellaneous character pointer */
74 char *field; /* buffer for header field */
75 int field_size; /* size of field buffer */
76 FILE *fp; /* file pointer made from locked file descriptor */
77 int length; /* length of field name */
78 int n; /* number of bytes written */
79 char *sp; /* another miscellaneous character pointer */
81 if ((fp = fopen(file, "r")) == (FILE *)0)
82 adios(file, "unable to open");
85 ** Allocate a buffer to hold the header components as they're read in.
86 ** This buffer might need to be quite large, so we grow it as needed.
88 field = (char *)mh_xmalloc(field_size = 256);
91 ** Get the length of the field name since we use it often.
93 length = strlen(comp);
99 ** Get a line from the input file, growing the field buffer
100 ** as needed. We do this so that we can fit an entire line
101 ** in the buffer making it easy to do a string comparison
102 ** on both the field name and the field body which might be
106 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
107 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
113 if (++n >= field_size - 1) {
114 field = (char *) mh_xrealloc((void *)field, field_size += 256);
120 /* NUL-terminate the field */
123 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
124 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
128 printf("%d\t", ++count);
130 if (text == NULL && (sp = strrchr(cp, '/')) != NULL)
136 } while (*field != '\0' && *field != '-');
146 annosbr(int fd, char *file, char *comp, char *text, int datesw, int delete,
154 int c; /* current character */
155 int count; /* header field (annotation) counter */
156 char *field = NULL; /* buffer for header field */
157 int field_size = 0; /* size of field buffer */
158 FILE *fp = NULL; /* file pointer made from locked file descriptor */
159 int length; /* length of field name */
160 int n; /* number of bytes written */
162 mode = fstat(fd, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot();
164 strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp), sizeof(tmpfil));
168 ** We're going to need to copy some of the message file to the
169 ** temporary file while examining the contents. Convert the
170 ** message file descriptor to a file pointer since it's a lot
171 ** easier and more efficient to use stdio for this. Also allocate
172 ** a buffer to hold the header components as they're read in.
173 ** This buffer is grown as needed later.
176 if (delete >= -1 || append != 0) {
177 if ((fp = fdopen(fd, "r")) == (FILE *)0)
178 adios(NULL, "unable to fdopen file.");
180 field = (char *)mh_xmalloc(field_size = 256);
184 ** We're trying to delete a header field (annotation )if the
185 ** delete flag is not -2 or less. A value greater than zero
186 ** means that we're deleting the nth header field that matches
187 ** the field (component) name. A value of zero means that
188 ** we're deleting the first field in which both the field name
189 ** matches the component name and the field body matches the text.
190 ** The text is matched in its entirety if it begins with a slash;
191 ** otherwise the text is matched against whatever portion of the
192 ** field body follows the last slash. This allows matching of
193 ** both absolute and relative path names. This is because this
194 ** functionality was added to support attachments. It might be
195 ** worth having a separate flag to indicate path name matching
196 ** to make it more general. A value of -1 means to delete all
201 /* Get the length of the field name since we use it often. */
202 length = strlen(comp);
205 ** Initialize the field counter. This is only used if
206 ** we're deleting by number.
211 ** Copy lines from the input file to the temporary file
212 ** until we either find the one that we're looking
213 ** for (which we don't copy) or we reach the end of
214 ** the headers. Both a blank line and a line beginning
215 ** with a - terminate the headers so that we can handle
216 ** both drafts and RFC-2822 format messages.
221 ** Get a line from the input file, growing the
222 ** field buffer as needed. We do this so that
223 ** we can fit an entire line in the buffer making
224 ** it easy to do a string comparison on both the
225 ** field name and the field body which might be
229 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
230 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
236 if (++n >= field_size - 1) {
237 field = (char *) mh_xrealloc((void *)field, field_size *= 2);
243 /* NUL-terminate the field */
247 ** Check for a match on the field name. We delete
248 ** the line by not copying it to the temporary
251 ** o The delete flag is 0, meaning that we're
252 ** going to delete the first matching
253 ** field, and the text is NULL meaning that
254 ** we don't care about the field body.
256 ** o The delete flag is 0, meaning that we're
257 ** going to delete the first matching
258 ** field, and the text begins with a / meaning
259 ** that we're looking for a full path name,
260 ** and the text matches the field body.
262 ** o The delete flag is 0, meaning that we're
263 ** going to delete the first matching
264 ** field, the text does not begin with a /
265 ** meaning that we're looking for the last
266 ** path name component, and the last path
267 ** name component matches the text.
269 ** o The delete flag is positive meaning that
270 ** we're going to delete the nth field
271 ** with a matching field name, and this is
272 ** the nth matching field name.
274 ** o The delete flag is -1 meaning that we're
275 ** going to delete all fields with a
276 ** matching field name.
279 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
284 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
288 if (strcmp(cp, text) == 0)
292 if ((sp = strrchr(cp, '/')) != NULL)
295 if (strcmp(cp, text) == 0)
300 else if (delete == -1)
303 else if (++count == delete)
308 ** This line wasn't a match so copy it to the
312 if ((n = fputs(field, tmp)) == EOF || (c == '\n' && fputc('\n', tmp) == EOF))
313 adios(NULL, "unable to write temporary file.");
315 } while (*field != '\0' && *field != '-');
318 ** Get rid of the field buffer because we're done with it.
325 ** Find the end of the headers before adding the
326 ** annotations if we're appending instead of the default
327 ** prepending. A special check for no headers is needed
333 ** Copy lines from the input file to the temporary
334 ** file until we reach the end of the headers.
337 if ((c = getc(fp)) == '\n')
343 while ((c = getc(fp)) != EOF) {
347 ungetc(c = getc(fp), fp);
349 if (c == '\n' || c == '-')
357 fprintf(tmp, "%s: %s\n", comp, dtimenow(0));
360 while (*cp == ' ' || *cp == '\t')
363 while (*cp && *cp++ != '\n')
366 fprintf(tmp, "%s: %*.*s", comp, (int)(cp - sp), (int)(cp - sp), sp);
368 if (cp[-1] != '\n' && cp != text)
376 ** We've been messing with the input file position. Move the
377 ** input file descriptor to the current place in the file
378 ** because the stock data copying routine uses the descriptor,
382 if (append || delete >= -1) {
383 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1)
384 adios(NULL, "can't seek.");
387 cpydata(fd, fileno(tmp), file, tmpfil);
390 if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK)
391 adios(tmpfil, "unable to open for re-reading");
393 lseek(fd, (off_t) 0, SEEK_SET);
396 ** We're making the file shorter if we're deleting a
397 ** header field so the file has to be truncated or it
398 ** will contain garbage.
401 if (delete >= -1 && ftruncate(fd, 0) == -1)
402 adios(tmpfil, "unable to truncate.");
404 cpydata(tmpfd, fd, tmpfil, file);
409 ** Close the delete file so that we don't run out of file pointers if
410 ** we're doing piles of files. Note that this will make the close() in
411 ** lkclose() fail, but that failure is ignored so it's not a problem.