]> diplodocus.org Git - nmh/blob - uip/annosbr.c
6cbf32a94ef39d010079140ef9e9b5533b41b267
[nmh] / uip / annosbr.c
1 /* annosbr.c -- prepend annotation to 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
8 #include "h/mh.h"
9 #include "sbr/m_gmprot.h"
10 #include "sbr/cpydata.h"
11 #include "sbr/m_backup.h"
12 #include "sbr/error.h"
13 #include "h/tws.h"
14 #include "h/utils.h"
15 #include "sbr/lock_file.h"
16 #include "sbr/m_mktemp.h"
17 #include <fcntl.h>
18 #include <utime.h>
19
20
21 /*
22 * static prototypes
23 */
24 static int annosbr (int, char *, char *, char *, bool, bool, int, bool);
25
26 /*
27 * This "local" global and the annopreserve() function are a hack that allows additional
28 * functionality to be added to anno without piling on yet another annotate() argument.
29 */
30
31 static int preserve_actime_and_modtime = 0; /* set to preserve access and modification times on annotated message */
32
33 int
34 annotate (char *file, char *comp, char *text, bool inplace, bool datesw, int delete, bool append)
35 {
36 int i, fd;
37 struct utimbuf b;
38 struct stat s;
39 int failed_to_lock = 0;
40
41 /* open and lock the file to be annotated */
42 if ((fd = lkopendata (file, O_RDWR, 0, &failed_to_lock)) == NOTOK) {
43 switch (errno) {
44 case ENOENT:
45 break;
46
47 default:
48 if (failed_to_lock) {
49 admonish (file, "unable to lock");
50 } else {
51 admonish (file, "unable to open");
52 }
53 break;
54 }
55 return 1;
56 }
57
58 if (stat(file, &s) == -1) {
59 inform("can't get access and modification times for %s", file);
60 preserve_actime_and_modtime = 0;
61 }
62
63 b.actime = s.st_atime;
64 b.modtime = s.st_mtime;
65
66 i = annosbr (fd, file, comp, text, inplace, datesw, delete, append);
67
68 if (preserve_actime_and_modtime && utime(file, &b) == -1)
69 inform("can't set access and modification times for %s", file);
70
71 lkclosedata (fd, file);
72 return i;
73 }
74
75 /*
76 * Produce a listing of all header fields (annotations) whose field name matches
77 * comp. Number the listing if number is set. Treat the field bodies as path
78 * names and just output the last component unless text is non-NULL. We don't
79 * care what text is set to.
80 */
81
82 void
83 annolist(char *file, char *comp, char *text, int number)
84 {
85 int c; /* current character */
86 int count; /* header field (annotation) counter */
87 char *cp; /* miscellaneous character pointer */
88 char *field; /* buffer for header field */
89 int field_size; /* size of field buffer */
90 FILE *fp; /* file pointer made from locked file descriptor */
91 int length; /* length of field name */
92 int n; /* number of bytes written */
93 char *sp; /* another miscellaneous character pointer */
94
95 if ((fp = fopen(file, "r")) == NULL)
96 adios(file, "unable to open");
97
98 /*
99 * Allocate a buffer to hold the header components as they're read in.
100 * This buffer might need to be quite large, so we grow it as needed.
101 */
102
103 field = mh_xmalloc(field_size = 256);
104
105 /*
106 * Get the length of the field name since we use it often.
107 */
108
109 length = strlen(comp);
110
111 count = 0;
112
113 do {
114 /*
115 * Get a line from the input file, growing the field buffer as needed. We do this
116 * so that we can fit an entire line in the buffer making it easy to do a string
117 * comparison on both the field name and the field body which might be a long path
118 * name.
119 */
120
121 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
122 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
123 (void)ungetc(c, fp);
124 break;
125 }
126
127 if (++n >= field_size - 1) {
128 field = mh_xrealloc((void *)field, field_size += 256);
129
130 cp = field + n - 1;
131 }
132 }
133
134 /*
135 * NUL-terminate the field..
136 */
137
138 *cp = '\0';
139
140 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
141 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
142 ;
143
144 if (number)
145 (void)printf("%d\t", ++count);
146
147 if (text == NULL && (sp = strrchr(cp, '/')))
148 cp = sp + 1;
149
150 puts(cp);
151 }
152
153 } while (*field != '\0' && *field != '-');
154
155 /*
156 * Clean up.
157 */
158
159 free(field);
160
161 (void)fclose(fp);
162 }
163
164 /*
165 * Set the preserve-times flag. This hack eliminates the need for an additional argument to annotate().
166 */
167
168 void
169 annopreserve(int preserve)
170 {
171 preserve_actime_and_modtime = preserve;
172 }
173
174 static int
175 annosbr (int fd, char *file, char *comp, char *text, bool inplace, bool datesw, int delete, bool append)
176 {
177 int mode, tmpfd;
178 char *cp, *sp;
179 char buffer[BUFSIZ], tmpfil[BUFSIZ];
180 struct stat st;
181 FILE *tmp;
182 int c; /* current character */
183 int count; /* header field (annotation) counter */
184 char *field = NULL; /* buffer for header field */
185 int field_size = 0; /* size of field buffer */
186 FILE *fp = NULL; /* file pointer made from locked file descriptor */
187 int length; /* length of field name */
188 int n; /* number of bytes written */
189
190 mode = fstat (fd, &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot ();
191
192 if ((cp = m_mktemp2(file, "annotate", NULL, &tmp)) == NULL) {
193 die("unable to create temporary file");
194 }
195 strncpy (tmpfil, cp, sizeof(tmpfil));
196 chmod (tmpfil, mode);
197
198 /*
199 * We're going to need to copy some of the message file to the temporary
200 * file while examining the contents. Convert the message file descriptor
201 * to a file pointer since it's a lot easier and more efficient to use
202 * stdio for this. Also allocate a buffer to hold the header components
203 * as they're read in. This buffer is grown as needed later.
204 */
205
206 if (delete >= -1 || append) {
207 if ((fp = fdopen(fd, "r")) == NULL)
208 die("unable to fdopen file.");
209
210 field = mh_xmalloc(field_size = 256);
211 }
212
213 /*
214 * We're trying to delete a header field (annotation )if the delete flag is
215 * not -2 or less. A value greater than zero means that we're deleting the
216 * nth header field that matches the field (component) name. A value of
217 * zero means that we're deleting the first field in which both the field
218 * name matches the component name and the field body matches the text.
219 * The text is matched in its entirety if it begins with a slash; otherwise
220 * the text is matched against whatever portion of the field body follows
221 * the last slash. This allows matching of both absolute and relative path
222 * names. This is because this functionality was added to support attachments.
223 * It might be worth having a separate flag to indicate path name matching to
224 * make it more general. A value of -1 means to delete all matching fields.
225 */
226
227 if (delete >= -1) {
228 /*
229 * Get the length of the field name since we use it often.
230 */
231
232 length = strlen(comp);
233
234 /*
235 * Initialize the field counter. This is only used if we're deleting by
236 * number.
237 */
238
239 count = 0;
240
241 /*
242 * Copy lines from the input file to the temporary file until we either find the one
243 * that we're looking for (which we don't copy) or we reach the end of the headers.
244 * Both a blank line and a line beginning with a - terminate the headers so that we
245 * can handle both drafts and RFC-2822 format messages.
246 */
247
248 do {
249 /*
250 * Get a line from the input file, growing the field buffer as needed. We do this
251 * so that we can fit an entire line in the buffer making it easy to do a string
252 * comparison on both the field name and the field body which might be a long path
253 * name.
254 */
255
256 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
257 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
258 (void)ungetc(c, fp);
259 c = '\n';
260 break;
261 }
262
263 if (++n >= field_size - 1) {
264 field = mh_xrealloc((void *)field, field_size *= 2);
265
266 cp = field + n - 1;
267 }
268 }
269
270 /*
271 * NUL-terminate the field..
272 */
273
274 *cp = '\0';
275
276 /*
277 * Check for a match on the field name. We delete the line by not copying it to the
278 * temporary file if
279 *
280 * o The delete flag is 0, meaning that we're going to delete the first matching
281 * field, and the text is NULL meaning that we don't care about the field body.
282 *
283 * o The delete flag is 0, meaning that we're going to delete the first matching
284 * field, and the text begins with a / meaning that we're looking for a full
285 * path name, and the text matches the field body.
286 *
287 * o The delete flag is 0, meaning that we're going to delete the first matching
288 * field, the text does not begin with a / meaning that we're looking for the
289 * last path name component, and the last path name component matches the text.
290 *
291 * o The delete flag is positive meaning that we're going to delete the nth field
292 * with a matching field name, and this is the nth matching field name.
293 *
294 * o The delete flag is -1 meaning that we're going to delete all fields with a
295 * matching field name.
296 */
297
298 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
299 if (delete == 0) {
300 if (text == NULL)
301 break;
302
303 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
304 ;
305
306 if (*text == '/') {
307 if (strcmp(cp, text) == 0)
308 break;
309 }
310 else {
311 if ((sp = strrchr(cp, '/')) != NULL)
312 cp = sp + 1;
313
314 if (strcmp(cp, text) == 0)
315 break;
316 }
317 }
318
319 else if (delete == -1)
320 continue;
321
322 else if (++count == delete)
323 break;
324 }
325
326 /*
327 * This line wasn't a match so copy it to the temporary file.
328 */
329
330 if ((n = fputs(field, tmp)) == EOF || (c == '\n' && fputc('\n', tmp) == EOF))
331 die("unable to write temporary file.");
332
333 } while (*field != '\0' && *field != '-');
334
335 /*
336 * Get rid of the field buffer because we're done with it.
337 */
338
339 free(field);
340 }
341
342 else {
343 /*
344 * Find the end of the headers before adding the annotations if we're
345 * appending instead of the default prepending. A special check for
346 * no headers is needed if appending.
347 */
348
349 if (append) {
350 /*
351 * Copy lines from the input file to the temporary file until we
352 * reach the end of the headers.
353 */
354
355 if ((c = getc(fp)) == '\n')
356 rewind(fp);
357
358 else {
359 (void)putc(c, tmp);
360
361 while ((c = getc(fp)) != EOF) {
362 (void)putc(c, tmp);
363
364 if (c == '\n') {
365 (void)ungetc(c = getc(fp), fp);
366
367 if (c == '\n' || c == '-')
368 break;
369 }
370 }
371 }
372 }
373
374 if (datesw)
375 fprintf (tmp, "%s: %s\n", comp, dtimenow (0));
376 if ((cp = text)) {
377 do {
378 while (*cp == ' ' || *cp == '\t')
379 cp++;
380 sp = cp;
381 while (*cp && *cp++ != '\n')
382 continue;
383 if (cp - sp)
384 fprintf (tmp, "%s: %*.*s", comp, (int)(cp - sp), (int)(cp - sp), sp);
385 } while (*cp);
386 if (cp[-1] != '\n' && cp != text)
387 putc ('\n', tmp);
388 }
389 }
390
391 fflush (tmp);
392
393 /*
394 * We've been messing with the input file position. Move the input file
395 * descriptor to the current place in the file because the stock data
396 * copying routine uses the descriptor, not the pointer.
397 */
398
399 if (fp && lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1)
400 die("can't seek.");
401
402 cpydata (fd, fileno (tmp), file, tmpfil);
403 fclose (tmp);
404
405 if (inplace) {
406 if ((tmpfd = open (tmpfil, O_RDONLY)) == NOTOK)
407 adios (tmpfil, "unable to open for re-reading");
408
409 lseek(fd, 0, SEEK_SET);
410
411 /*
412 * We're making the file shorter if we're deleting a header field
413 * so the file has to be truncated or it will contain garbage.
414 */
415
416 if (delete >= -1 && ftruncate(fd, 0) == -1)
417 adios(tmpfil, "unable to truncate.");
418
419 cpydata (tmpfd, fd, tmpfil, file);
420 close (tmpfd);
421 (void) m_unlink (tmpfil);
422 } else {
423 strncpy (buffer, m_backup (file), sizeof(buffer));
424 if (rename (file, buffer) == NOTOK) {
425 switch (errno) {
426 case ENOENT: /* unlinked early - no annotations */
427 (void) m_unlink (tmpfil);
428 break;
429
430 default:
431 admonish (buffer, "unable to rename %s to", file);
432 break;
433 }
434 return 1;
435 }
436 if (rename (tmpfil, file) == NOTOK) {
437 admonish (file, "unable to rename %s to", tmpfil);
438 return 1;
439 }
440 }
441
442 /*
443 * Close the delete file so that we don't run out of file pointers if
444 * we're doing piles of files. Note that this will make the close() in
445 * lkclose() fail, but that failure is ignored so it's not a problem.
446 */
447
448 if (fp)
449 (void)fclose(fp);
450
451 return 0;
452 }