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