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
59 static void make_comp(unsigned char **);
60 static int annotate(char *, unsigned char *, char *, int, int, int, int);
61 static void annolist(char *, unsigned char *, int);
62 static void dodel(int, unsigned char *, char *, FILE *, int);
63 static void doadd(int, unsigned char *, char *, FILE *, int, int);
67 main(int argc, char **argv)
73 unsigned char *comp = NULL;
74 char *text = NULL, *folder = NULL, buf[BUFSIZ];
76 char **argp, **arguments;
77 struct msgs_array msgs = { 0, 0, NULL };
79 int append = 0; /* append annotations instead of default prepend */
80 int number = 0; /* delete specific number of like elements if set */
82 setlocale(LC_ALL, "");
83 invo_name = mhbasename(argv[0]);
86 arguments = getarguments(invo_name, argc, argv, 1);
89 while ((cp = *argp++)) {
91 switch (smatch(++cp, switches)) {
93 ambigsw(cp, switches);
96 adios(EX_USAGE, NULL, "-%s unknown", cp);
99 snprintf(buf, sizeof(buf),
100 "%s [+folder] [msgs] [switches]",
102 print_help(buf, switches, 1);
103 exit(argc == 2 ? EX_OK : EX_USAGE);
105 print_version(invo_name);
106 exit(argc == 2 ? EX_OK : EX_USAGE);
108 case DELETESW: /* delete annotations */
112 case LISTSW: /* produce a listing */
118 adios(EX_USAGE, NULL, "only one component at a time!");
119 if (!(comp = *argp++) || *comp == '-')
120 adios(EX_USAGE, NULL, "missing argument to %s",
126 adios(EX_USAGE, NULL, "only one body at a time!");
127 if (!(text = *argp++) || *text == '-')
128 adios(EX_USAGE, NULL, "missing argument to %s",
132 case NUMBERSW: /* number listing or delete by number */
133 if (mode == MODE_ADD) {
134 adios(EX_USAGE, NULL, "-number switch must appear after -list or -delete, only.");
136 if (mode == MODE_LIST) {
142 adios(EX_USAGE, NULL, "only one number at a time!");
144 if (*argp && strcmp(*argp, "all")==0) {
149 if (!*argp || !(number = atoi(*argp))) {
150 adios(EX_USAGE, NULL, "missing argument to %s",
154 adios(EX_USAGE, NULL, "invalid number (%d).",
180 if (*cp == '+' || *cp == '@') {
182 adios(EX_USAGE, NULL, "only one folder at a time!");
184 folder = getcpy(expandfol(cp));
185 } else if (*cp == '/' || *cp == '.') {
187 adios(EX_USAGE, NULL, "only one file at a time!");
190 app_msgarg(&msgs, cp);
194 if (file && (folder || msgs.size)) {
195 adios(EX_USAGE, NULL, "Don't intermix files and messages.");
197 if (!datesw && !text) {
198 adios(EX_USAGE, NULL, "-nodate without -text is a no-op.");
200 if (number && text) {
201 adios(EX_USAGE, NULL, "Don't combine -number with -text.");
205 if (mode == MODE_LIST)
206 annolist(file, comp, number);
208 annotate(file, comp, text, datesw, number,
214 app_msgarg(&msgs, seq_cur);
216 folder = getcurfol();
217 maildir = toabsdir(folder);
219 if (chdir(maildir) == NOTOK)
220 adios(EX_OSERR, maildir, "unable to change directory to");
222 /* read folder and create message structure */
223 if (!(mp = folder_read(folder)))
224 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
226 /* check for empty folder */
228 adios(EX_DATAERR, NULL, "no messages in %s", folder);
230 /* parse all the message ranges/sequences and set SELECTED */
231 for (msgnum = 0; msgnum < msgs.size; msgnum++) {
232 if (!m_convert(mp, msgs.msgs[msgnum])) {
237 /* annotate all the SELECTED messages */
238 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
239 if (is_selected(mp, msgnum)) {
240 if (mode == MODE_LIST)
241 annolist(m_name(msgnum), comp, number);
243 annotate(m_name(msgnum), comp, text, datesw,
244 number, append, preserve);
248 context_replace(curfolder, folder);
249 seq_setcur(mp, mp->lowsel);
257 make_comp(unsigned char **ap)
263 printf("Enter component name: ");
266 if (!fgets(buffer, sizeof buffer, stdin)) {
269 *ap = trimcpy(buffer);
272 if ((cp = *ap + strlen(*ap) - 1) > *ap && *cp == ':')
274 if (strlen(*ap) == 0)
275 adios(EX_SOFTWARE, NULL, "null component name");
277 adios(EX_CONFIG, NULL, "invalid component name %s", *ap);
278 if (strlen(*ap) >= NAMESZ)
279 adios(EX_SOFTWARE, NULL, "too large component name %s", *ap);
281 for (cp = *ap; *cp; cp++)
282 if (!isalnum(*cp) && *cp != '-')
283 adios(EX_CONFIG, NULL, "invalid component name %s", *ap);
288 ** Produce a listing of all header fields (annotations) whose field
289 ** name matches comp. Number the listing if number is set.
292 annolist(char *file, unsigned char *comp, int number)
295 int count = 1; /* header field (annotation) counter */
301 int n; /* number of bytes written */
303 if ((fp = fopen(file, "r")) == NULL) {
304 adios(EX_IOERR, file, "unable to open");
307 /* We'll grow this buffer as needed. */
308 field = (char *)mh_xmalloc(field_size = 256);
311 length = strlen(comp); /* Convenience copy. */
315 ** Get a line from the input file, growing the field buffer
316 ** as needed. We do this so that we can fit an entire line
317 ** in the buffer making it easy to do a string comparison
318 ** on both the field name and the field body which might be
321 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
322 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
327 if (++n >= field_size - 1) {
328 field = (char *)mh_xrealloc(field,
335 if (strncasecmp(field, comp, length)==0 &&
336 field[length] == ':') {
337 cp = trim(field + length + 1);
339 printf("%d\t", count++);
344 } while (*field && *field != '-');
354 annotate(char *file, unsigned char *comp, char *text, int datesw,
355 int number, int append, int preserve)
364 /* open and lock the file to be annotated */
365 if ((fd = lkopen(file, O_RDWR, 0)) == NOTOK) {
370 admonish(file, "unable to lock and open");
376 if (stat(file, &st) == -1) {
377 advise("can't get access and modification times for %s", file);
380 b.actime = st.st_atime;
381 b.modtime = st.st_mtime;
383 perms = fstat(fd, &st) != NOTOK ?
384 (int)(st.st_mode & 0777) : m_gmprot();
386 strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp),
388 chmod(tmpfil, perms);
392 if (mode == MODE_DEL) {
393 dodel(fd, comp, text, tmp, number);
395 if (mode == MODE_ADD) {
396 doadd(fd, comp, text, tmp, datesw, append);
399 cpydata(fd, fileno(tmp), file, tmpfil);
402 if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
403 adios(EX_IOERR, tmpfil, "unable to open for re-reading");
405 lseek(fd, (off_t) 0, SEEK_SET);
408 ** We're making the file shorter if we're deleting a header field
409 ** so the file has to be truncated or it will contain garbage.
411 if (mode == MODE_DEL && ftruncate(fd, 0) == -1) {
412 adios(EX_IOERR, tmpfil, "unable to truncate.");
414 cpydata(tmpfd, fd, tmpfil, file);
418 if (preserve && utime(file, &b) == -1) {
419 advise("can't set access and modification times for %s", file);
426 ** We're trying to delete a header field (annotation).
428 ** - If number is greater than zero,
429 ** we're deleting the nth header field that matches
430 ** the field (component) name.
431 ** - If number is zero and text is NULL,
432 ** we're deleting the first field in which the field name
433 ** matches the component name.
434 ** - If number is zero and text is set,
435 ** we're deleting the first field in which both the field name
436 ** matches the component name and the field body matches the text.
437 ** - If number is -1,
438 ** we delete all matching fields.
441 dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
443 int length = strlen(comp); /* convenience copy */
444 int count = 1; /* Number of matching header line. */
448 int field_size = 256;
452 ** We're going to need to copy some of the message file to the
453 ** temporary file while examining the contents. Convert the
454 ** message file descriptor to a file pointer since it's a lot
455 ** easier and more efficient to use stdio for this. Also allocate
456 ** a buffer to hold the header components as they're read in.
457 ** This buffer is grown as needed later.
459 if ((fp = fdopen(fd, "r")) == NULL) {
460 adios(EX_IOERR, NULL, "unable to fdopen file.");
462 field = (char *)mh_xmalloc(field_size);
465 ** Copy lines from the input file to the temporary file
466 ** until we either find the one that we're looking
467 ** for (which we don't copy) or we reach the end of
468 ** the headers. Both a blank line and a line beginning
469 ** with a - terminate the headers so that we can handle
470 ** both drafts and RFC-2822 format messages.
474 ** Get a line from the input file, growing the
475 ** field buffer as needed. We do this so that
476 ** we can fit an entire line in the buffer making
477 ** it easy to do a string comparison on both the
478 ** field name and the field body which might be
481 for (n=0, cp=field; (c=getc(fp)) != EOF; *cp++ = c) {
482 if (c == '\n' && (c = getc(fp)) != ' ' &&
489 if (++n >= field_size - 1) {
490 field = (char *) mh_xrealloc(field,
497 if (strncasecmp(field, comp, length)==0 &&
498 field[length] == ':') {
500 ** This component matches and thus is a candidate.
501 ** We delete the line by not copying it to the
502 ** temporary file. Thus:
503 ** - Break if we've found the one to delete.
504 ** - Continue if this is one to delete, but
505 ** there'll be further ones.
508 if (!number && !text) {
509 /* this first one is it */
514 /* delete all of them */
516 } else if (number == count++) {
517 /* delete this specific one */
522 /* delete the first matching one */
524 while (*cp==' ' || *cp=='\t') {
525 cp++; /* eat leading whitespace */
527 if (*text == '/' && strcmp(text, cp)==0) {
528 break; /* full path matches */
529 } else if (strcmp(text, mhbasename(cp))==0) {
530 break; /* basename matches */
534 ** Although the compoment name mached, it
535 ** wasn't the right one.
540 if ((n = fputs(field, tmp)) == EOF ||
541 (c=='\n' && fputc('\n', tmp)==EOF)) {
542 adios(EX_CANTCREAT, NULL, "unable to write temporary file.");
545 } while (*field && *field != '-');
550 fflush(fp); /* The underlying fd will be closed by lkclose() */
553 ** We've been messing with the input file position. Move the
554 ** input file descriptor to the current place in the file
555 ** because the stock data copying routine uses the descriptor,
558 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
559 adios(EX_IOERR, NULL, "can't seek.");
565 doadd(int fd, unsigned char *comp, char *text, FILE *tmp, int datesw,
574 ** We're going to need to copy some of the message
575 ** file to the temporary file while examining the
576 ** contents. Convert the message file descriptor to
577 ** a file pointer since it's a lot easier and more
578 ** efficient to use stdio for this. Also allocate
579 ** a buffer to hold the header components as they're
580 ** read in. This buffer is grown as needed later.
582 if ((fp = fdopen(fd, "r")) == NULL) {
583 adios(EX_IOERR, NULL, "unable to fdopen file.");
585 /* Find the end of the headers. */
586 if ((c = getc(fp)) == '\n') {
587 /* Special check for no headers is needed. */
591 ** Copy lines from the input file to the
592 ** temporary file until we reach the end
596 while ((c = getc(fp)) != EOF) {
599 ungetc(c = getc(fp), fp);
600 if (c == '\n' || c == '-') {
609 fprintf(tmp, "%s: %s\n", comp, dtimenow());
612 /* Add body text header */
614 while (*cp == ' ' || *cp == '\t') {
618 while (*cp && *cp++ != '\n') {
622 fprintf(tmp, "%s: %*.*s", comp,
627 if (cp[-1] != '\n' && cp != text) {
634 ** We've been messing with the input file position. Move the
635 ** input file descriptor to the current place in the file
636 ** because the stock data copying routine uses the descriptor,
640 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
641 adios(EX_IOERR, NULL, "can't seek.");