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.
26 static enum { MODE_ADD, MODE_DEL, MODE_LIST } mode = MODE_ADD;
28 static struct swit switches[] = {
30 { "component field", 0 },
51 #define NOPRESERVESW 11
56 char *version=VERSION;
61 static void make_comp(unsigned char **);
62 static int annotate(char *, unsigned char *, char *, int, int, int, int);
63 static void annolist(char *, unsigned char *, int);
64 static void dodel(int, unsigned char *, char *, FILE *, int);
65 static void doadd(int, unsigned char *, char *, FILE *, int, int);
69 main(int argc, char **argv)
75 unsigned char *comp = NULL;
76 char *text = NULL, *folder = NULL, buf[BUFSIZ];
78 char **argp, **arguments;
79 struct msgs_array msgs = { 0, 0, NULL };
81 int append = 0; /* append annotations instead of default prepend */
82 int number = 0; /* delete specific number of like elements if set */
84 setlocale(LC_ALL, "");
85 invo_name = mhbasename(argv[0]);
88 arguments = getarguments(invo_name, argc, argv, 1);
91 while ((cp = *argp++)) {
93 switch (smatch(++cp, switches)) {
95 ambigsw(cp, switches);
98 adios(EX_USAGE, NULL, "-%s unknown", cp);
101 snprintf(buf, sizeof(buf),
102 "%s [+folder] [msgs] [switches]",
104 print_help(buf, switches, 1);
105 exit(argc == 2 ? EX_OK : EX_USAGE);
107 print_version(invo_name);
108 exit(argc == 2 ? EX_OK : EX_USAGE);
110 case DELETESW: /* delete annotations */
114 case LISTSW: /* produce a listing */
120 adios(EX_USAGE, NULL, "only one component at a time!");
121 if (!(comp = *argp++) || *comp == '-')
122 adios(EX_USAGE, NULL, "missing argument to %s",
128 adios(EX_USAGE, NULL, "only one body at a time!");
129 if (!(text = *argp++) || *text == '-')
130 adios(EX_USAGE, NULL, "missing argument to %s",
134 case NUMBERSW: /* number listing or delete by number */
135 if (mode == MODE_ADD) {
136 adios(EX_USAGE, NULL, "-number switch must appear after -list or -delete, only.");
138 if (mode == MODE_LIST) {
144 adios(EX_USAGE, NULL, "only one number at a time!");
146 if (*argp && strcmp(*argp, "all")==0) {
151 if (!*argp || !(number = atoi(*argp))) {
152 adios(EX_USAGE, NULL, "missing argument to %s",
156 adios(EX_USAGE, NULL, "invalid number (%d).",
182 if (*cp == '+' || *cp == '@') {
184 adios(EX_USAGE, NULL, "only one folder at a time!");
186 folder = mh_xstrdup(expandfol(cp));
187 } else if (*cp == '/' || *cp == '.') {
189 adios(EX_USAGE, NULL, "only one file at a time!");
192 app_msgarg(&msgs, cp);
196 if (file && (folder || msgs.size)) {
197 adios(EX_USAGE, NULL, "Don't intermix files and messages.");
199 if (!datesw && !text) {
200 adios(EX_USAGE, NULL, "-nodate without -text is a no-op.");
202 if (number && text) {
203 adios(EX_USAGE, NULL, "Don't combine -number with -text.");
207 if (mode == MODE_LIST)
208 annolist(file, comp, number);
210 annotate(file, comp, text, datesw, number,
216 app_msgarg(&msgs, seq_cur);
218 folder = getcurfol();
219 maildir = toabsdir(folder);
221 if (chdir(maildir) == NOTOK)
222 adios(EX_OSERR, maildir, "unable to change directory to");
224 /* read folder and create message structure */
225 if (!(mp = folder_read(folder)))
226 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
228 /* check for empty folder */
230 adios(EX_DATAERR, NULL, "no messages in %s", folder);
232 /* parse all the message ranges/sequences and set SELECTED */
233 for (msgnum = 0; msgnum < msgs.size; msgnum++) {
234 if (!m_convert(mp, msgs.msgs[msgnum])) {
239 /* annotate all the SELECTED messages */
240 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
241 if (is_selected(mp, msgnum)) {
242 if (mode == MODE_LIST)
243 annolist(m_name(msgnum), comp, number);
245 annotate(m_name(msgnum), comp, text, datesw,
246 number, append, preserve);
250 context_replace(curfolder, folder);
251 seq_setcur(mp, mp->lowsel);
259 make_comp(unsigned char **ap)
265 printf("Enter component name: ");
268 if (!fgets(buffer, sizeof buffer, stdin)) {
271 *ap = trimcpy(buffer);
274 if ((cp = *ap + strlen(*ap) - 1) > *ap && *cp == ':')
276 if (strlen(*ap) == 0)
277 adios(EX_SOFTWARE, NULL, "null component name");
279 adios(EX_CONFIG, NULL, "invalid component name %s", *ap);
280 if (strlen(*ap) >= NAMESZ)
281 adios(EX_SOFTWARE, NULL, "too large component name %s", *ap);
283 for (cp = *ap; *cp; cp++)
284 if (!isalnum(*cp) && *cp != '-')
285 adios(EX_CONFIG, NULL, "invalid component name %s", *ap);
290 ** Produce a listing of all header fields (annotations) whose field
291 ** name matches comp. Number the listing if number is set.
294 annolist(char *file, unsigned char *comp, int number)
297 int count = 1; /* header field (annotation) counter */
303 int n; /* number of bytes written */
305 if ((fp = fopen(file, "r")) == NULL) {
306 adios(EX_IOERR, file, "unable to open");
309 /* We'll grow this buffer as needed. */
310 field = mh_xcalloc(field_size = 256, sizeof(char));
313 length = strlen(comp); /* Convenience copy. */
317 ** Get a line from the input file, growing the field buffer
318 ** as needed. We do this so that we can fit an entire line
319 ** in the buffer making it easy to do a string comparison
320 ** on both the field name and the field body which might be
323 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
324 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
329 if (++n >= field_size - 1) {
330 field = mh_xrealloc(field, field_size += 256);
336 if (strncasecmp(field, comp, length)==0 &&
337 field[length] == ':') {
338 cp = trim(field + length + 1);
340 printf("%d\t", count++);
345 } while (*field && *field != '-');
355 annotate(char *file, unsigned char *comp, char *text, int datesw,
356 int number, int append, int preserve)
365 /* open and lock the file to be annotated */
366 if ((fd = lkopen(file, O_RDWR, 0)) == NOTOK) {
371 admonish(file, "unable to lock and open");
377 if (stat(file, &st) == -1) {
378 advise("can't get access and modification times for %s", file);
381 b.actime = st.st_atime;
382 b.modtime = st.st_mtime;
384 perms = fstat(fd, &st) != NOTOK ?
385 (int)(st.st_mode & 0777) : m_gmprot();
387 strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp),
389 chmod(tmpfil, perms);
393 if (mode == MODE_DEL) {
394 dodel(fd, comp, text, tmp, number);
396 if (mode == MODE_ADD) {
397 doadd(fd, comp, text, tmp, datesw, append);
400 cpydata(fd, fileno(tmp), file, tmpfil);
403 if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
404 adios(EX_IOERR, tmpfil, "unable to open for re-reading");
406 lseek(fd, (off_t) 0, SEEK_SET);
409 ** We're making the file shorter if we're deleting a header field
410 ** so the file has to be truncated or it will contain garbage.
412 if (mode == MODE_DEL && ftruncate(fd, 0) == -1) {
413 adios(EX_IOERR, tmpfil, "unable to truncate.");
415 cpydata(tmpfd, fd, tmpfil, file);
419 if (preserve && utime(file, &b) == -1) {
420 advise("can't set access and modification times for %s", file);
427 ** We're trying to delete a header field (annotation).
429 ** - If number is greater than zero,
430 ** we're deleting the nth header field that matches
431 ** the field (component) name.
432 ** - If number is zero and text is NULL,
433 ** we're deleting the first field in which the field name
434 ** matches the component name.
435 ** - If number is zero and text is set,
436 ** we're deleting the first field in which both the field name
437 ** matches the component name and the field body matches the text.
438 ** - If number is -1,
439 ** we delete all matching fields.
442 dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
444 int length = strlen(comp); /* convenience copy */
445 int count = 1; /* Number of matching header line. */
449 int field_size = 256;
453 ** We're going to need to copy some of the message file to the
454 ** temporary file while examining the contents. Convert the
455 ** message file descriptor to a file pointer since it's a lot
456 ** easier and more efficient to use stdio for this. Also allocate
457 ** a buffer to hold the header components as they're read in.
458 ** This buffer is grown as needed later.
460 if ((fp = fdopen(fd, "r")) == NULL) {
461 adios(EX_IOERR, NULL, "unable to fdopen file.");
463 field = mh_xcalloc(field_size, sizeof(char));
466 ** Copy lines from the input file to the temporary file
467 ** until we either find the one that we're looking
468 ** for (which we don't copy) or we reach the end of
469 ** the headers. Both a blank line and a line beginning
470 ** with a - terminate the headers so that we can handle
471 ** both drafts and RFC-2822 format messages.
475 ** Get a line from the input file, growing the
476 ** field buffer as needed. We do this so that
477 ** we can fit an entire line in the buffer making
478 ** it easy to do a string comparison on both the
479 ** field name and the field body which might be
482 for (n=0, cp=field; (c=getc(fp)) != EOF; *cp++ = c) {
484 ungetc((c = getc(fp)), fp);
485 if (c != ' ' && c != '\t') {
492 if (++n >= field_size - 1) {
493 field = mh_xrealloc(field, field_size *= 2);
499 if (strncasecmp(field, comp, length)==0 &&
500 field[length] == ':') {
502 ** This component matches and thus is a candidate.
503 ** We delete the line by not copying it to the
504 ** temporary file. Thus:
505 ** - Break if we've found the one to delete.
506 ** - Continue if this is one to delete, but
507 ** there'll be further ones.
510 if (!number && !text) {
511 /* this first one is it */
516 /* delete all of them */
518 } else if (number == count++) {
519 /* delete this specific one */
524 /* delete the first matching one */
526 while (*cp==' ' || *cp=='\t') {
527 cp++; /* eat leading whitespace */
529 if (*text == '/' && strcmp(text, cp)==0) {
530 break; /* full path matches */
531 } else if (strcmp(text, mhbasename(cp))==0) {
532 break; /* basename matches */
536 ** Although the compoment name mached, it
537 ** wasn't the right one.
542 if ((n = fputs(field, tmp)) == EOF ||
543 (c=='\n' && fputc('\n', tmp)==EOF)) {
544 adios(EX_CANTCREAT, NULL, "unable to write temporary file.");
547 } while (*field && *field != '-');
552 fflush(fp); /* The underlying fd will be closed by lkclose() */
555 ** We've been messing with the input file position. Move the
556 ** input file descriptor to the current place in the file
557 ** because the stock data copying routine uses the descriptor,
560 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
561 adios(EX_IOERR, NULL, "can't seek.");
567 doadd(int fd, unsigned char *comp, char *text, FILE *tmp, int datesw,
576 ** We're going to need to copy some of the message
577 ** file to the temporary file while examining the
578 ** contents. Convert the message file descriptor to
579 ** a file pointer since it's a lot easier and more
580 ** efficient to use stdio for this. Also allocate
581 ** a buffer to hold the header components as they're
582 ** read in. This buffer is grown as needed later.
584 if ((fp = fdopen(fd, "r")) == NULL) {
585 adios(EX_IOERR, NULL, "unable to fdopen file.");
587 /* Find the end of the headers. */
588 if ((c = getc(fp)) == '\n') {
589 /* Special check for no headers is needed. */
593 ** Copy lines from the input file to the
594 ** temporary file until we reach the end
598 while ((c = getc(fp)) != EOF) {
601 ungetc(c = getc(fp), fp);
602 if (c == '\n' || c == '-') {
611 fprintf(tmp, "%s: %s\n", comp, dtimenow());
614 /* Add body text header */
616 while (*cp == ' ' || *cp == '\t') {
620 while (*cp && *cp++ != '\n') {
624 fprintf(tmp, "%s: %*.*s", comp,
629 if (cp[-1] != '\n' && cp != text) {
636 ** We've been messing with the input file position. Move the
637 ** input file descriptor to the current place in the file
638 ** because the stock data copying routine uses the descriptor,
642 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
643 adios(EX_IOERR, NULL, "can't seek.");