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) {
37 admonish(file, "unable to lock and open");
43 if (stat(file, &s) == -1) {
44 advise("can't get access and modification times for %s", file);
48 b.actime = s.st_atime;
49 b.modtime = s.st_mtime;
51 i = annosbr(fd, file, comp, text, datesw, delete, append);
53 if (preserve && utime(file, &b) == -1) {
54 advise("can't set access and modification times for %s", file);
61 ** Produce a listing of all header fields (annotations) whose field
62 ** name matches comp. Number the listing if number is set. Treat the
63 ** field bodies as path names and just output the last component unless
64 ** text is non-NULL. We don't care what text is set to.
67 annolist(char *file, char *comp, char *text, int number)
70 int count; /* header field (annotation) counter */
76 int n; /* number of bytes written */
79 if ((fp = fopen(file, "r")) == NULL) {
80 adios(file, "unable to open");
83 /* We'll grow this buffer as needed. */
84 field = (char *)mh_xmalloc(field_size = 256);
86 length = strlen(comp); /* Convenience copy. */
91 ** Get a line from the input file, growing the field buffer
92 ** as needed. We do this so that we can fit an entire line
93 ** in the buffer making it easy to do a string comparison
94 ** on both the field name and the field body which might be
97 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
98 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
103 if (++n >= field_size - 1) {
104 field = (char *)mh_xrealloc(field,
111 if (strncasecmp(field, comp, length)==0 &&
112 field[length] == ':') {
113 for (cp = field + length + 1;
114 *cp == ' ' || *cp == '\t'; cp++) {
118 printf("%d\t", ++count);
120 if (!text && (sp = strrchr(cp, '/'))) {
126 } while (*field && *field != '-');
135 annosbr(int fd, char *file, char *comp, char *text, int datesw, int delete,
144 int count; /* header field (annotation) counter */
149 int n; /* number of bytes written */
151 mode = fstat(fd, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot();
153 strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp),
158 ** We're going to need to copy some of the message file to the
159 ** temporary file while examining the contents. Convert the
160 ** message file descriptor to a file pointer since it's a lot
161 ** easier and more efficient to use stdio for this. Also allocate
162 ** a buffer to hold the header components as they're read in.
163 ** This buffer is grown as needed later.
165 if (delete >= -1 || append != 0) {
166 if ((fp = fdopen(fd, "r")) == NULL) {
167 adios(NULL, "unable to fdopen file.");
169 field = (char *)mh_xmalloc(field_size = 256);
173 ** We're trying to delete a header field (annotation) if the
174 ** delete flag is greater -2. A value greater than zero
175 ** means that we're deleting the nth header field that matches
176 ** the field (component) name. A value of zero means that
177 ** we're deleting the first field in which both the field name
178 ** matches the component name and the field body matches the text.
179 ** The text is matched in its entirety if it begins with a slash;
180 ** otherwise the text is matched against whatever portion of the
181 ** field body follows the last slash. This allows matching of
182 ** both absolute and relative path names. This is because this
183 ** functionality was added to support attachments. It might be
184 ** worth having a separate flag to indicate path name matching
185 ** to make it more general. A value of -1 means to delete all
189 length = strlen(comp); /* convenience copy */
190 count = 0; /* Only used if we're deleting by number. */
193 ** Copy lines from the input file to the temporary file
194 ** until we either find the one that we're looking
195 ** for (which we don't copy) or we reach the end of
196 ** the headers. Both a blank line and a line beginning
197 ** with a - terminate the headers so that we can handle
198 ** both drafts and RFC-2822 format messages.
202 ** Get a line from the input file, growing the
203 ** field buffer as needed. We do this so that
204 ** we can fit an entire line in the buffer making
205 ** it easy to do a string comparison on both the
206 ** field name and the field body which might be
209 for (n=0, cp=field; (c=getc(fp)) != EOF; *cp++ = c) {
210 if (c == '\n' && (c = getc(fp)) != ' ' &&
217 if (++n >= field_size - 1) {
218 field = (char *) mh_xrealloc(field,
226 ** Check for a match on the field name. We delete
227 ** the line by not copying it to the temporary
230 ** o The delete flag is 0, meaning that we're
231 ** going to delete the first matching
232 ** field, and the text is NULL meaning that
233 ** we don't care about the field body.
235 ** o The delete flag is 0, meaning that we're
236 ** going to delete the first matching
237 ** field, and the text begins with a / meaning
238 ** that we're looking for a full path name,
239 ** and the text matches the field body.
241 ** o The delete flag is 0, meaning that we're
242 ** going to delete the first matching
243 ** field, the text does not begin with a /
244 ** meaning that we're looking for the last
245 ** path name component, and the last path
246 ** name component matches the text.
248 ** o The delete flag is positive meaning that
249 ** we're going to delete the nth field
250 ** with a matching field name, and this is
251 ** the nth matching field name.
253 ** o The delete flag is -1 meaning that we're
254 ** going to delete all fields with a
255 ** matching field name.
257 if (strncasecmp(field, comp, length)==0 &&
258 field[length] == ':') {
263 for (cp=field+length+1;
264 *cp==' ' || *cp=='\t';
269 strcmp(cp, text)==0) {
272 if ((sp = strrchr(cp, '/'))) {
275 if (strcmp(cp, text)==0) {
279 } else if (delete == -1) {
281 } else if (++count == delete) {
287 ** This line wasn't a match so copy it to the
290 if ((n = fputs(field, tmp)) == EOF ||
291 (c=='\n' && fputc('\n', tmp)==EOF)) {
292 adios(NULL, "unable to write temporary file.");
294 } while (*field && *field != '-');
299 ** Find the end of the headers before adding the
300 ** annotations if we're appending instead of the default
301 ** prepending. A special check for no headers is needed
306 ** Copy lines from the input file to the temporary
307 ** file until we reach the end of the headers.
309 if ((c = getc(fp)) == '\n') {
313 while ((c = getc(fp)) != EOF) {
316 ungetc(c = getc(fp), fp);
317 if (c == '\n' || c == '-') {
326 fprintf(tmp, "%s: %s\n", comp, dtimenow(0));
330 while (*cp == ' ' || *cp == '\t') {
334 while (*cp && *cp++ != '\n') {
338 fprintf(tmp, "%s: %*.*s", comp,
343 if (cp[-1] != '\n' && cp != text) {
351 ** We've been messing with the input file position. Move the
352 ** input file descriptor to the current place in the file
353 ** because the stock data copying routine uses the descriptor,
356 if (append || delete >= -1) {
357 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
358 adios(NULL, "can't seek.");
362 cpydata(fd, fileno(tmp), file, tmpfil);
365 if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
366 adios(tmpfil, "unable to open for re-reading");
368 lseek(fd, (off_t) 0, SEEK_SET);
371 ** We're making the file shorter if we're deleting a header field
372 ** so the file has to be truncated or it will contain garbage.
374 if (delete >= -1 && ftruncate(fd, 0) == -1) {
375 adios(tmpfil, "unable to truncate.");
377 cpydata(tmpfd, fd, tmpfil, file);
382 ** Close the delete file so that we don't run out of file pointers if
383 ** we're doing piles of files. Note that this will make the close() in
384 ** lkclose() fail, but that failure is ignored so it's not a problem.