2 ** anno.c -- annotate 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.
8 ** Three new options have been added: delete, list, and number. Adding
9 ** features to generalize the anno command seemed to be a better approach
10 ** than the creation of a new command whose features would overlap with
11 ** those of the anno command.
21 static enum { MODE_ADD, MODE_DEL, MODE_LIST } mode = MODE_ADD;
23 static struct swit switches[] = {
25 { "component field", 0 },
46 #define NOPRESERVESW 11
54 static void make_comp(unsigned char **);
55 static int annotate(char *, unsigned char *, char *, int, int, int, int);
56 static void annolist(char *, unsigned char *, int);
57 static void dodel(int, unsigned char *, char *, FILE *, int);
58 static void doadd(int, unsigned char *, char *, FILE *, int, int);
62 main(int argc, char **argv)
68 unsigned char *comp = NULL;
69 char *text = NULL, *folder = NULL, buf[BUFSIZ];
71 char **argp, **arguments;
72 struct msgs_array msgs = { 0, 0, NULL };
74 int append = 0; /* append annotations instead of default prepend */
75 int number = 0; /* delete specific number of like elements if set */
77 setlocale(LC_ALL, "");
78 invo_name = mhbasename(argv[0]);
81 arguments = getarguments(invo_name, argc, argv, 1);
84 while ((cp = *argp++)) {
86 switch (smatch(++cp, switches)) {
88 ambigsw(cp, switches);
91 adios(NULL, "-%s unknown", cp);
94 snprintf(buf, sizeof(buf),
95 "%s [+folder] [msgs] [switches]",
97 print_help(buf, switches, 1);
100 print_version(invo_name);
103 case DELETESW: /* delete annotations */
107 case LISTSW: /* produce a listing */
113 adios(NULL, "only one component at a time!");
114 if (!(comp = *argp++) || *comp == '-')
115 adios(NULL, "missing argument to %s",
121 adios(NULL, "only one body at a time!");
122 if (!(text = *argp++) || *text == '-')
123 adios(NULL, "missing argument to %s",
127 case NUMBERSW: /* number listing or delete by number */
128 if (mode == MODE_ADD) {
129 adios(NULL, "-number switch must appear after -list or -delete, only.");
131 if (mode == MODE_LIST) {
137 adios(NULL, "only one number at a time!");
139 if (*argp && strcmp(*argp, "all")==0) {
144 if (!*argp || !(number = atoi(*argp))) {
145 adios(NULL, "missing argument to %s",
149 adios(NULL, "invalid number (%d).",
175 if (*cp == '+' || *cp == '@') {
177 adios(NULL, "only one folder at a time!");
179 folder = getcpy(expandfol(cp));
180 } else if (*cp == '/' || *cp == '.') {
182 adios(NULL, "only one file at a time!");
185 app_msgarg(&msgs, cp);
189 if (file && (folder || msgs.size)) {
190 adios(NULL, "Don't intermix files and messages.");
192 if (!datesw && !text) {
193 adios(NULL, "-nodate without -text is a no-op.");
195 if (number && text) {
196 adios(NULL, "Don't combine -number with -text.");
200 if (mode == MODE_LIST)
201 annolist(file, comp, number);
203 annotate(file, comp, text, datesw, number,
209 app_msgarg(&msgs, seq_cur);
211 folder = getcurfol();
212 maildir = toabsdir(folder);
214 if (chdir(maildir) == NOTOK)
215 adios(maildir, "unable to change directory to");
217 /* read folder and create message structure */
218 if (!(mp = folder_read(folder)))
219 adios(NULL, "unable to read folder %s", folder);
221 /* check for empty folder */
223 adios(NULL, "no messages in %s", folder);
225 /* parse all the message ranges/sequences and set SELECTED */
226 for (msgnum = 0; msgnum < msgs.size; msgnum++)
227 if (!m_convert(mp, msgs.msgs[msgnum]))
230 /* annotate all the SELECTED messages */
231 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
232 if (is_selected(mp, msgnum)) {
233 if (mode == MODE_LIST)
234 annolist(m_name(msgnum), comp, number);
236 annotate(m_name(msgnum), comp, text, datesw,
237 number, append, preserve);
241 context_replace(curfolder, folder);
242 seq_setcur(mp, mp->lowsel);
251 make_comp(unsigned char **ap)
257 printf("Enter component name: ");
260 if (!fgets(buffer, sizeof buffer, stdin)) {
263 *ap = trimcpy(buffer);
266 if ((cp = *ap + strlen(*ap) - 1) > *ap && *cp == ':')
268 if (strlen(*ap) == 0)
269 adios(NULL, "null component name");
271 adios(NULL, "invalid component name %s", *ap);
272 if (strlen(*ap) >= NAMESZ)
273 adios(NULL, "too large component name %s", *ap);
275 for (cp = *ap; *cp; cp++)
276 if (!isalnum(*cp) && *cp != '-')
277 adios(NULL, "invalid component name %s", *ap);
282 ** Produce a listing of all header fields (annotations) whose field
283 ** name matches comp. Number the listing if number is set.
286 annolist(char *file, unsigned char *comp, int number)
289 int count = 1; /* header field (annotation) counter */
295 int n; /* number of bytes written */
297 if ((fp = fopen(file, "r")) == NULL) {
298 adios(file, "unable to open");
301 /* We'll grow this buffer as needed. */
302 field = (char *)mh_xmalloc(field_size = 256);
305 length = strlen(comp); /* Convenience copy. */
309 ** Get a line from the input file, growing the field buffer
310 ** as needed. We do this so that we can fit an entire line
311 ** in the buffer making it easy to do a string comparison
312 ** on both the field name and the field body which might be
315 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
316 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
321 if (++n >= field_size - 1) {
322 field = (char *)mh_xrealloc(field,
329 if (strncasecmp(field, comp, length)==0 &&
330 field[length] == ':') {
331 for (cp = field + length + 1;
332 *cp == ' ' || *cp == '\t'; cp++) {
336 printf("%d\t", count++);
341 } while (*field && *field != '-');
351 annotate(char *file, unsigned char *comp, char *text, int datesw,
352 int number, int append, int preserve)
361 /* open and lock the file to be annotated */
362 if ((fd = lkopen(file, O_RDWR, 0)) == NOTOK) {
367 admonish(file, "unable to lock and open");
373 if (stat(file, &st) == -1) {
374 advise("can't get access and modification times for %s", file);
377 b.actime = st.st_atime;
378 b.modtime = st.st_mtime;
380 perms = fstat(fd, &st) != NOTOK ?
381 (int)(st.st_mode & 0777) : m_gmprot();
383 strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp),
385 chmod(tmpfil, perms);
389 if (mode == MODE_DEL) {
390 dodel(fd, comp, text, tmp, number);
392 if (mode == MODE_ADD) {
393 doadd(fd, comp, text, tmp, datesw, append);
396 cpydata(fd, fileno(tmp), file, tmpfil);
399 if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
400 adios(tmpfil, "unable to open for re-reading");
402 lseek(fd, (off_t) 0, SEEK_SET);
405 ** We're making the file shorter if we're deleting a header field
406 ** so the file has to be truncated or it will contain garbage.
408 if (mode == MODE_DEL && ftruncate(fd, 0) == -1) {
409 adios(tmpfil, "unable to truncate.");
411 cpydata(tmpfd, fd, tmpfil, file);
415 if (preserve && utime(file, &b) == -1) {
416 advise("can't set access and modification times for %s", file);
423 ** We're trying to delete a header field (annotation).
425 ** - If number is greater than zero,
426 ** we're deleting the nth header field that matches
427 ** the field (component) name.
428 ** - If number is zero and text is NULL,
429 ** we're deleting the first field in which the field name
430 ** matches the component name.
431 ** - If number is zero and text is set,
432 ** we're deleting the first field in which both the field name
433 ** matches the component name and the field body matches the text.
434 ** - If number is -1,
435 ** we delete all matching fields.
438 dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
440 int length = strlen(comp); /* convenience copy */
441 int count = 1; /* Number of matching header line. */
445 int field_size = 256;
449 ** We're going to need to copy some of the message file to the
450 ** temporary file while examining the contents. Convert the
451 ** message file descriptor to a file pointer since it's a lot
452 ** easier and more efficient to use stdio for this. Also allocate
453 ** a buffer to hold the header components as they're read in.
454 ** This buffer is grown as needed later.
456 if ((fp = fdopen(fd, "r")) == NULL) {
457 adios(NULL, "unable to fdopen file.");
459 field = (char *)mh_xmalloc(field_size);
462 ** Copy lines from the input file to the temporary file
463 ** until we either find the one that we're looking
464 ** for (which we don't copy) or we reach the end of
465 ** the headers. Both a blank line and a line beginning
466 ** with a - terminate the headers so that we can handle
467 ** both drafts and RFC-2822 format messages.
471 ** Get a line from the input file, growing the
472 ** field buffer as needed. We do this so that
473 ** we can fit an entire line in the buffer making
474 ** it easy to do a string comparison on both the
475 ** field name and the field body which might be
478 for (n=0, cp=field; (c=getc(fp)) != EOF; *cp++ = c) {
479 if (c == '\n' && (c = getc(fp)) != ' ' &&
486 if (++n >= field_size - 1) {
487 field = (char *) mh_xrealloc(field,
494 if (strncasecmp(field, comp, length)==0 &&
495 field[length] == ':') {
497 ** This component matches and thus is a candidate.
498 ** We delete the line by not copying it to the
499 ** temporary file. Thus:
500 ** - Break if we've found the one to delete.
501 ** - Continue if this is one to delete, but
502 ** there'll be further ones.
505 if (!number && !text) {
506 /* this first one is it */
511 /* delete all of them */
513 } else if (number == count++) {
514 /* delete this specific one */
519 /* delete the first matching one */
521 while (*cp==' ' || *cp=='\t') {
522 cp++; /* eat leading whitespace */
524 if (*text == '/' && strcmp(text, cp)==0) {
525 break; /* full path matches */
526 } else if (strcmp(text, mhbasename(cp))==0) {
527 break; /* basename matches */
531 ** Although the compoment name mached, it
532 ** wasn't the right one.
537 if ((n = fputs(field, tmp)) == EOF ||
538 (c=='\n' && fputc('\n', tmp)==EOF)) {
539 adios(NULL, "unable to write temporary file.");
542 } while (*field && *field != '-');
547 fflush(fp); /* The underlying fd will be closed by lkclose() */
550 ** We've been messing with the input file position. Move the
551 ** input file descriptor to the current place in the file
552 ** because the stock data copying routine uses the descriptor,
555 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
556 adios(NULL, "can't seek.");
562 doadd(int fd, unsigned char *comp, char *text, FILE *tmp, int datesw,
571 ** We're going to need to copy some of the message
572 ** file to the temporary file while examining the
573 ** contents. Convert the message file descriptor to
574 ** a file pointer since it's a lot easier and more
575 ** efficient to use stdio for this. Also allocate
576 ** a buffer to hold the header components as they're
577 ** read in. This buffer is grown as needed later.
579 if ((fp = fdopen(fd, "r")) == NULL) {
580 adios(NULL, "unable to fdopen file.");
582 /* Find the end of the headers. */
583 if ((c = getc(fp)) == '\n') {
584 /* Special check for no headers is needed. */
588 ** Copy lines from the input file to the
589 ** temporary file until we reach the end
593 while ((c = getc(fp)) != EOF) {
596 ungetc(c = getc(fp), fp);
597 if (c == '\n' || c == '-') {
606 fprintf(tmp, "%s: %s\n", comp, dtimenow());
609 /* Add body text header */
611 while (*cp == ' ' || *cp == '\t') {
615 while (*cp && *cp++ != '\n') {
619 fprintf(tmp, "%s: %*.*s", comp,
624 if (cp[-1] != '\n' && cp != text) {
631 ** We've been messing with the input file position. Move the
632 ** input file descriptor to the current place in the file
633 ** because the stock data copying routine uses the descriptor,
637 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
638 adios(NULL, "can't seek.");