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