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.
25 static enum { MODE_ADD, MODE_DEL, MODE_LIST } mode = MODE_ADD;
27 static struct swit switches[] = {
29 { "component field", 0 },
50 #define NOPRESERVESW 11
58 static void make_comp(unsigned char **);
59 static int annotate(char *, unsigned char *, char *, int, int, int, int);
60 static void annolist(char *, unsigned char *, int);
61 static void dodel(int, unsigned char *, char *, FILE *, int);
62 static void doadd(int, unsigned char *, char *, FILE *, int, int);
66 main(int argc, char **argv)
72 unsigned char *comp = NULL;
73 char *text = NULL, *folder = NULL, buf[BUFSIZ];
75 char **argp, **arguments;
76 struct msgs_array msgs = { 0, 0, NULL };
78 int append = 0; /* append annotations instead of default prepend */
79 int number = 0; /* delete specific number of like elements if set */
81 setlocale(LC_ALL, "");
82 invo_name = mhbasename(argv[0]);
85 arguments = getarguments(invo_name, argc, argv, 1);
88 while ((cp = *argp++)) {
90 switch (smatch(++cp, switches)) {
92 ambigsw(cp, switches);
93 /* sysexits.h EX_USAGE */
96 adios(NULL, "-%s unknown", cp);
99 snprintf(buf, sizeof(buf),
100 "%s [+folder] [msgs] [switches]",
102 print_help(buf, switches, 1);
105 print_version(invo_name);
108 case DELETESW: /* delete annotations */
112 case LISTSW: /* produce a listing */
118 adios(NULL, "only one component at a time!");
119 if (!(comp = *argp++) || *comp == '-')
120 adios(NULL, "missing argument to %s",
126 adios(NULL, "only one body at a time!");
127 if (!(text = *argp++) || *text == '-')
128 adios(NULL, "missing argument to %s",
132 case NUMBERSW: /* number listing or delete by number */
133 if (mode == MODE_ADD) {
134 adios(NULL, "-number switch must appear after -list or -delete, only.");
136 if (mode == MODE_LIST) {
142 adios(NULL, "only one number at a time!");
144 if (*argp && strcmp(*argp, "all")==0) {
149 if (!*argp || !(number = atoi(*argp))) {
150 adios(NULL, "missing argument to %s",
154 adios(NULL, "invalid number (%d).",
180 if (*cp == '+' || *cp == '@') {
182 adios(NULL, "only one folder at a time!");
184 folder = getcpy(expandfol(cp));
185 } else if (*cp == '/' || *cp == '.') {
187 adios(NULL, "only one file at a time!");
190 app_msgarg(&msgs, cp);
194 if (file && (folder || msgs.size)) {
195 adios(NULL, "Don't intermix files and messages.");
197 if (!datesw && !text) {
198 adios(NULL, "-nodate without -text is a no-op.");
200 if (number && text) {
201 adios(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(maildir, "unable to change directory to");
222 /* read folder and create message structure */
223 if (!(mp = folder_read(folder)))
224 adios(NULL, "unable to read folder %s", folder);
226 /* check for empty folder */
228 adios(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])) {
233 /* sysexits.h EX_USAGE */
238 /* annotate all the SELECTED messages */
239 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
240 if (is_selected(mp, msgnum)) {
241 if (mode == MODE_LIST)
242 annolist(m_name(msgnum), comp, number);
244 annotate(m_name(msgnum), comp, text, datesw,
245 number, append, preserve);
249 context_replace(curfolder, folder);
250 seq_setcur(mp, mp->lowsel);
258 make_comp(unsigned char **ap)
264 printf("Enter component name: ");
267 if (!fgets(buffer, sizeof buffer, stdin)) {
268 /* sysexits.h EX_IOERR */
271 *ap = trimcpy(buffer);
274 if ((cp = *ap + strlen(*ap) - 1) > *ap && *cp == ':')
276 if (strlen(*ap) == 0)
277 adios(NULL, "null component name");
279 adios(NULL, "invalid component name %s", *ap);
280 if (strlen(*ap) >= NAMESZ)
281 adios(NULL, "too large component name %s", *ap);
283 for (cp = *ap; *cp; cp++)
284 if (!isalnum(*cp) && *cp != '-')
285 adios(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(file, "unable to open");
309 /* We'll grow this buffer as needed. */
310 field = (char *)mh_xmalloc(field_size = 256);
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 = (char *)mh_xrealloc(field,
337 if (strncasecmp(field, comp, length)==0 &&
338 field[length] == ':') {
339 cp = trim(field + length + 1);
341 printf("%d\t", count++);
346 } while (*field && *field != '-');
356 annotate(char *file, unsigned char *comp, char *text, int datesw,
357 int number, int append, int preserve)
366 /* open and lock the file to be annotated */
367 if ((fd = lkopen(file, O_RDWR, 0)) == NOTOK) {
372 admonish(file, "unable to lock and open");
378 if (stat(file, &st) == -1) {
379 advise("can't get access and modification times for %s", file);
382 b.actime = st.st_atime;
383 b.modtime = st.st_mtime;
385 perms = fstat(fd, &st) != NOTOK ?
386 (int)(st.st_mode & 0777) : m_gmprot();
388 strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp),
390 chmod(tmpfil, perms);
394 if (mode == MODE_DEL) {
395 dodel(fd, comp, text, tmp, number);
397 if (mode == MODE_ADD) {
398 doadd(fd, comp, text, tmp, datesw, append);
401 cpydata(fd, fileno(tmp), file, tmpfil);
404 if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
405 adios(tmpfil, "unable to open for re-reading");
407 lseek(fd, (off_t) 0, SEEK_SET);
410 ** We're making the file shorter if we're deleting a header field
411 ** so the file has to be truncated or it will contain garbage.
413 if (mode == MODE_DEL && ftruncate(fd, 0) == -1) {
414 adios(tmpfil, "unable to truncate.");
416 cpydata(tmpfd, fd, tmpfil, file);
420 if (preserve && utime(file, &b) == -1) {
421 advise("can't set access and modification times for %s", file);
428 ** We're trying to delete a header field (annotation).
430 ** - If number is greater than zero,
431 ** we're deleting the nth header field that matches
432 ** the field (component) name.
433 ** - If number is zero and text is NULL,
434 ** we're deleting the first field in which the field name
435 ** matches the component name.
436 ** - If number is zero and text is set,
437 ** we're deleting the first field in which both the field name
438 ** matches the component name and the field body matches the text.
439 ** - If number is -1,
440 ** we delete all matching fields.
443 dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
445 int length = strlen(comp); /* convenience copy */
446 int count = 1; /* Number of matching header line. */
450 int field_size = 256;
454 ** We're going to need to copy some of the message file to the
455 ** temporary file while examining the contents. Convert the
456 ** message file descriptor to a file pointer since it's a lot
457 ** easier and more efficient to use stdio for this. Also allocate
458 ** a buffer to hold the header components as they're read in.
459 ** This buffer is grown as needed later.
461 if ((fp = fdopen(fd, "r")) == NULL) {
462 adios(NULL, "unable to fdopen file.");
464 field = (char *)mh_xmalloc(field_size);
467 ** Copy lines from the input file to the temporary file
468 ** until we either find the one that we're looking
469 ** for (which we don't copy) or we reach the end of
470 ** the headers. Both a blank line and a line beginning
471 ** with a - terminate the headers so that we can handle
472 ** both drafts and RFC-2822 format messages.
476 ** Get a line from the input file, growing the
477 ** field buffer as needed. We do this so that
478 ** we can fit an entire line in the buffer making
479 ** it easy to do a string comparison on both the
480 ** field name and the field body which might be
483 for (n=0, cp=field; (c=getc(fp)) != EOF; *cp++ = c) {
484 if (c == '\n' && (c = getc(fp)) != ' ' &&
491 if (++n >= field_size - 1) {
492 field = (char *) mh_xrealloc(field,
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(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(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(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(NULL, "can't seek.");