Replaced mkinstalldirs with `mkdir -p'.
[mmh] / 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 <errno.h>
14 #include <utime.h>
15
16
17 /*
18 ** static prototypes
19 */
20 static int annosbr(int, char *, char *, char *, int, int, int);
21
22
23 int
24 annotate(char *file, char *comp, char *text, int datesw,
25         int delete, int append, int preserve)
26 {
27         int i, fd;
28         struct utimbuf b;
29         struct stat s;
30
31         /* open and lock the file to be annotated */
32         if ((fd = lkopen(file, O_RDWR, 0)) == NOTOK) {
33                 switch (errno) {
34                 case ENOENT:
35                         break;
36                 default:
37                         admonish(file, "unable to lock and open");
38                         break;
39                 }
40                 return 1;
41         }
42
43         if (stat(file, &s) == -1) {
44                 advise("can't get access and modification times for %s", file);
45                 preserve = 0;
46         }
47
48         b.actime = s.st_atime;
49         b.modtime = s.st_mtime;
50
51         i = annosbr(fd, file, comp, text, datesw, delete, append);
52
53         if (preserve && utime(file, &b) == -1) {
54                 advise("can't set access and modification times for %s", file);
55         }
56         lkclose(fd, file);
57         return i;
58 }
59
60 /*
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.
65 */
66 void
67 annolist(char *file, char *comp, char *text, int number)
68 {
69         int c;
70         int count;  /* header field (annotation) counter */
71         char *cp;
72         char *field;
73         int field_size;
74         FILE *fp;
75         int length;
76         int n;  /* number of bytes written */
77         char *sp;
78
79         if ((fp = fopen(file, "r")) == NULL) {
80                 adios(file, "unable to open");
81         }
82
83         /* We'll grow this buffer as needed. */
84         field = (char *)mh_xmalloc(field_size = 256);
85
86         length = strlen(comp); /* Convenience copy. */
87         count = 0;
88
89         do {
90                 /*
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
95                 ** a long path name.
96                 */
97                 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
98                         if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
99                                 ungetc(c, fp);
100                                 c = '\n';
101                                 break;
102                         }
103                         if (++n >= field_size - 1) {
104                                 field = (char *)mh_xrealloc(field,
105                                                 field_size += 256);
106                                 cp = field + n - 1;
107                         }
108                 }
109                 *cp = '\0';
110
111                 if (strncasecmp(field, comp, length)==0 &&
112                                 field[length] == ':') {
113                         for (cp = field + length + 1;
114                                         *cp == ' ' || *cp == '\t'; cp++) {
115                                 continue;
116                         }
117                         if (number) {
118                                 printf("%d\t", ++count);
119                         }
120                         if (!text && (sp = strrchr(cp, '/'))) {
121                                 cp = sp + 1;
122                         }
123                         printf("%s\n", cp);
124                 }
125
126         } while (*field && *field != '-');
127
128         free(field);
129         fclose(fp);
130
131         return;
132 }
133
134 static int
135 annosbr(int fd, char *file, char *comp, char *text, int datesw, int delete,
136                 int append)
137 {
138         int mode, tmpfd;
139         char *cp, *sp;
140         char tmpfil[BUFSIZ];
141         struct stat st;
142         FILE *tmp;
143         int c;
144         int count;  /* header field (annotation) counter */
145         char *field = NULL;
146         int field_size = 0;
147         FILE *fp = NULL;
148         int length;
149         int n;  /* number of bytes written */
150
151         mode = fstat(fd, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot();
152
153         strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp),
154                         sizeof(tmpfil));
155         chmod(tmpfil, mode);
156
157         /*
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.
164         */
165         if (delete >= -1 || append != 0) {
166                 if ((fp = fdopen(fd, "r")) == NULL) {
167                         adios(NULL, "unable to fdopen file.");
168                 }
169                 field = (char *)mh_xmalloc(field_size = 256);
170         }
171
172         /*
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
186         ** matching fields.
187         */
188         if (delete >= -1) {
189                 length = strlen(comp);  /* convenience copy */
190                 count = 0; /* Only used if we're deleting by number. */
191
192                 /*
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.
199                 */
200                 do {
201                         /*
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
207                         ** a long path name.
208                         */
209                         for (n=0, cp=field; (c=getc(fp)) != EOF; *cp++ = c) {
210                                 if (c == '\n' && (c = getc(fp)) != ' ' &&
211                                                 c != '\t') {
212                                         ungetc(c, fp);
213                                         c = '\n';
214                                         break;
215                                 }
216
217                                 if (++n >= field_size - 1) {
218                                         field = (char *) mh_xrealloc(field,
219                                                         field_size *= 2);
220                                         cp = field + n - 1;
221                                 }
222                         }
223                         *cp = '\0';
224
225                         /*
226                         ** Check for a match on the field name.  We delete
227                         ** the line by not copying it to the temporary
228                         ** file if
229                         **
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.
234                         **
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.
240                         **
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.
247                         **
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.
252                         **
253                         **  o  The delete flag is -1 meaning that we're
254                         **     going to delete all fields with a
255                         **     matching field name.
256                         */
257                         if (strncasecmp(field, comp, length)==0 &&
258                                         field[length] == ':') {
259                                 if (!delete) {
260                                         if (!text) {
261                                                 break;
262                                         }
263                                         for (cp=field+length+1;
264                                                         *cp==' ' || *cp=='\t';
265                                                         cp++) {
266                                                 continue;
267                                         }
268                                         if (*text == '/' &&
269                                                         strcmp(cp, text)==0) {
270                                                 break;
271                                         } else {
272                                                 if ((sp = strrchr(cp, '/'))) {
273                                                         cp = sp + 1;
274                                                 }
275                                                 if (strcmp(cp, text)==0) {
276                                                         break;
277                                                 }
278                                         }
279                                 } else if (delete == -1) {
280                                         continue;
281                                 } else if (++count == delete) {
282                                         break;
283                                 }
284                         }
285
286                         /*
287                         ** This line wasn't a match so copy it to the
288                         ** temporary file.
289                         */
290                         if ((n = fputs(field, tmp)) == EOF ||
291                                         (c=='\n' && fputc('\n', tmp)==EOF)) {
292                                 adios(NULL, "unable to write temporary file.");
293                         }
294                 } while (*field && *field != '-');
295                 free(field);
296
297         } else {
298                 /*
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
302                 **  if appending.
303                 */
304                 if (append) {
305                         /*
306                         ** Copy lines from the input file to the temporary
307                         ** file until we reach the end of the headers.
308                         */
309                         if ((c = getc(fp)) == '\n') {
310                                 rewind(fp);
311                         } else {
312                                 putc(c, tmp);
313                                 while ((c = getc(fp)) != EOF) {
314                                         putc(c, tmp);
315                                         if (c == '\n') {
316                                                 ungetc(c = getc(fp), fp);
317                                                 if (c == '\n' || c == '-') {
318                                                         break;
319                                                 }
320                                         }
321                                 }
322                         }
323                 }
324
325                 if (datesw) {
326                         fprintf(tmp, "%s: %s\n", comp, dtimenow(0));
327                 }
328                 if ((cp = text)) {
329                         do {
330                                 while (*cp == ' ' || *cp == '\t') {
331                                         cp++;
332                                 }
333                                 sp = cp;
334                                 while (*cp && *cp++ != '\n') {
335                                         continue;
336                                 }
337                                 if (cp - sp) {
338                                         fprintf(tmp, "%s: %*.*s", comp,
339                                                 (int)(cp - sp),
340                                                 (int)(cp - sp), sp);
341                                 }
342                         } while (*cp);
343                         if (cp[-1] != '\n' && cp != text) {
344                                 putc('\n', tmp);
345                         }
346                 }
347         }
348         fflush(tmp);
349
350         /*
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,
354         ** not the pointer.
355         */
356         if (append || delete >= -1) {
357                 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
358                         adios(NULL, "can't seek.");
359                 }
360         }
361
362         cpydata(fd, fileno(tmp), file, tmpfil);
363         fclose(tmp);
364
365         if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
366                 adios(tmpfil, "unable to open for re-reading");
367         }
368         lseek(fd, (off_t) 0, SEEK_SET);
369
370         /*
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.
373         */
374         if (delete >= -1 && ftruncate(fd, 0) == -1) {
375                 adios(tmpfil, "unable to truncate.");
376         }
377         cpydata(tmpfd, fd, tmpfil, file);
378         close(tmpfd);
379         unlink(tmpfil);
380
381         /*
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.
385         */
386         if (delete >= -1) {
387                 fclose(fp);
388         }
389         return 0;
390 }