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 */
78 setlocale(LC_ALL, "");
80 invo_name = mhbasename(argv[0]);
83 arguments = getarguments(invo_name, argc, argv, 1);
86 while ((cp = *argp++)) {
88 switch (smatch(++cp, switches)) {
90 ambigsw(cp, switches);
93 adios(NULL, "-%s unknown", cp);
96 snprintf(buf, sizeof(buf),
97 "%s [+folder] [msgs] [switches]",
99 print_help(buf, switches, 1);
102 print_version(invo_name);
105 case DELETESW: /* delete annotations */
109 case LISTSW: /* produce a listing */
115 adios(NULL, "only one component at a time!");
116 if (!(comp = *argp++) || *comp == '-')
117 adios(NULL, "missing argument to %s",
123 adios(NULL, "only one body at a time!");
124 if (!(text = *argp++) || *text == '-')
125 adios(NULL, "missing argument to %s",
129 case NUMBERSW: /* number listing or delete by number */
130 if (mode == MODE_ADD) {
131 adios(NULL, "-number switch must appear after -list or -delete, only.");
133 if (mode == MODE_LIST) {
139 adios(NULL, "only one number at a time!");
141 if (*argp && strcmp(*argp, "all")==0) {
146 if (!*argp || !(number = atoi(*argp))) {
147 adios(NULL, "missing argument to %s",
151 adios(NULL, "invalid number (%d).",
177 if (*cp == '+' || *cp == '@') {
179 adios(NULL, "only one folder at a time!");
181 folder = getcpy(expandfol(cp));
182 } else if (*cp == '/' || *cp == '.') {
184 adios(NULL, "only one file at a time!");
187 app_msgarg(&msgs, cp);
191 if (file && (folder || msgs.size)) {
192 adios(NULL, "Don't intermix files and messages.");
194 if (!datesw && !text) {
195 adios(NULL, "-nodate without -text is a no-op.");
197 if (number && text) {
198 adios(NULL, "Don't combine -number with -text.");
202 if (mode == MODE_LIST)
203 annolist(file, comp, number);
205 annotate(file, comp, text, datesw, number,
211 app_msgarg(&msgs, seq_cur);
213 folder = getcurfol();
214 maildir = toabsdir(folder);
216 if (chdir(maildir) == NOTOK)
217 adios(maildir, "unable to change directory to");
219 /* read folder and create message structure */
220 if (!(mp = folder_read(folder)))
221 adios(NULL, "unable to read folder %s", folder);
223 /* check for empty folder */
225 adios(NULL, "no messages in %s", folder);
227 /* parse all the message ranges/sequences and set SELECTED */
228 for (msgnum = 0; msgnum < msgs.size; msgnum++)
229 if (!m_convert(mp, msgs.msgs[msgnum]))
232 /* annotate all the SELECTED messages */
233 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
234 if (is_selected(mp, msgnum)) {
235 if (mode == MODE_LIST)
236 annolist(m_name(msgnum), comp, number);
238 annotate(m_name(msgnum), comp, text, datesw,
239 number, append, preserve);
243 context_replace(curfolder, folder);
244 seq_setcur(mp, mp->lowsel);
253 make_comp(unsigned char **ap)
259 printf("Enter component name: ");
262 if (!fgets(buffer, sizeof buffer, stdin)) {
265 *ap = trimcpy(buffer);
268 if ((cp = *ap + strlen(*ap) - 1) > *ap && *cp == ':')
270 if (strlen(*ap) == 0)
271 adios(NULL, "null component name");
273 adios(NULL, "invalid component name %s", *ap);
274 if (strlen(*ap) >= NAMESZ)
275 adios(NULL, "too large component name %s", *ap);
277 for (cp = *ap; *cp; cp++)
278 if (!isalnum(*cp) && *cp != '-')
279 adios(NULL, "invalid component name %s", *ap);
284 ** Produce a listing of all header fields (annotations) whose field
285 ** name matches comp. Number the listing if number is set.
288 annolist(char *file, unsigned char *comp, int number)
291 int count = 1; /* header field (annotation) counter */
297 int n; /* number of bytes written */
299 if ((fp = fopen(file, "r")) == NULL) {
300 adios(file, "unable to open");
303 /* We'll grow this buffer as needed. */
304 field = (char *)mh_xmalloc(field_size = 256);
307 length = strlen(comp); /* Convenience copy. */
311 ** Get a line from the input file, growing the field buffer
312 ** as needed. We do this so that we can fit an entire line
313 ** in the buffer making it easy to do a string comparison
314 ** on both the field name and the field body which might be
317 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
318 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
323 if (++n >= field_size - 1) {
324 field = (char *)mh_xrealloc(field,
331 if (strncasecmp(field, comp, length)==0 &&
332 field[length] == ':') {
333 for (cp = field + length + 1;
334 *cp == ' ' || *cp == '\t'; cp++) {
338 printf("%d\t", count++);
343 } while (*field && *field != '-');
353 annotate(char *file, unsigned char *comp, char *text, int datesw,
354 int number, int append, int preserve)
363 /* open and lock the file to be annotated */
364 if ((fd = lkopen(file, O_RDWR, 0)) == NOTOK) {
369 admonish(file, "unable to lock and open");
375 if (stat(file, &st) == -1) {
376 advise("can't get access and modification times for %s", file);
379 b.actime = st.st_atime;
380 b.modtime = st.st_mtime;
382 perms = fstat(fd, &st) != NOTOK ?
383 (int)(st.st_mode & 0777) : m_gmprot();
385 strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp),
387 chmod(tmpfil, perms);
391 if (mode == MODE_DEL) {
392 dodel(fd, comp, text, tmp, number);
394 if (mode == MODE_ADD) {
395 doadd(fd, comp, text, tmp, datesw, append);
398 cpydata(fd, fileno(tmp), file, tmpfil);
401 if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
402 adios(tmpfil, "unable to open for re-reading");
404 lseek(fd, (off_t) 0, SEEK_SET);
407 ** We're making the file shorter if we're deleting a header field
408 ** so the file has to be truncated or it will contain garbage.
410 if (mode == MODE_DEL && ftruncate(fd, 0) == -1) {
411 adios(tmpfil, "unable to truncate.");
413 cpydata(tmpfd, fd, tmpfil, file);
417 if (preserve && utime(file, &b) == -1) {
418 advise("can't set access and modification times for %s", file);
425 ** We're trying to delete a header field (annotation).
427 ** - If number is greater than zero,
428 ** we're deleting the nth header field that matches
429 ** the field (component) name.
430 ** - If number is zero and text is NULL,
431 ** we're deleting the first field in which the field name
432 ** matches the component name.
433 ** - If number is zero and text is set,
434 ** we're deleting the first field in which both the field name
435 ** matches the component name and the field body matches the text.
436 ** - If number is -1,
437 ** we delete all matching fields.
440 dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
442 int length = strlen(comp); /* convenience copy */
443 int count = 1; /* Number of matching header line. */
447 int field_size = 256;
451 ** We're going to need to copy some of the message file to the
452 ** temporary file while examining the contents. Convert the
453 ** message file descriptor to a file pointer since it's a lot
454 ** easier and more efficient to use stdio for this. Also allocate
455 ** a buffer to hold the header components as they're read in.
456 ** This buffer is grown as needed later.
458 if ((fp = fdopen(fd, "r")) == NULL) {
459 adios(NULL, "unable to fdopen file.");
461 field = (char *)mh_xmalloc(field_size);
464 ** Copy lines from the input file to the temporary file
465 ** until we either find the one that we're looking
466 ** for (which we don't copy) or we reach the end of
467 ** the headers. Both a blank line and a line beginning
468 ** with a - terminate the headers so that we can handle
469 ** both drafts and RFC-2822 format messages.
473 ** Get a line from the input file, growing the
474 ** field buffer as needed. We do this so that
475 ** we can fit an entire line in the buffer making
476 ** it easy to do a string comparison on both the
477 ** field name and the field body which might be
480 for (n=0, cp=field; (c=getc(fp)) != EOF; *cp++ = c) {
481 if (c == '\n' && (c = getc(fp)) != ' ' &&
488 if (++n >= field_size - 1) {
489 field = (char *) mh_xrealloc(field,
496 if (strncasecmp(field, comp, length)==0 &&
497 field[length] == ':') {
499 ** This component matches and thus is a candidate.
500 ** We delete the line by not copying it to the
501 ** temporary file. Thus:
502 ** - Break if we've found the one to delete.
503 ** - Continue if this is one to delete, but
504 ** there'll be further ones.
507 if (!number && !text) {
508 /* this first one is it */
513 /* delete all of them */
515 } else if (number == count++) {
516 /* delete this specific one */
521 /* delete the first matching one */
523 while (*cp==' ' || *cp=='\t') {
524 cp++; /* eat leading whitespace */
526 if (*text == '/' && strcmp(text, cp)==0) {
527 break; /* full path matches */
528 } else if (strcmp(text, mhbasename(cp))==0) {
529 break; /* basename matches */
533 ** Although the compoment name mached, it
534 ** wasn't the right one.
539 if ((n = fputs(field, tmp)) == EOF ||
540 (c=='\n' && fputc('\n', tmp)==EOF)) {
541 adios(NULL, "unable to write temporary file.");
544 } while (*field && *field != '-');
549 fflush(fp); /* The underlying fd will be closed by lkclose() */
552 ** We've been messing with the input file position. Move the
553 ** input file descriptor to the current place in the file
554 ** because the stock data copying routine uses the descriptor,
557 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
558 adios(NULL, "can't seek.");
564 doadd(int fd, unsigned char *comp, char *text, FILE *tmp, int datesw,
573 ** We're going to need to copy some of the message
574 ** file to the temporary file while examining the
575 ** contents. Convert the message file descriptor to
576 ** a file pointer since it's a lot easier and more
577 ** efficient to use stdio for this. Also allocate
578 ** a buffer to hold the header components as they're
579 ** read in. This buffer is grown as needed later.
581 if ((fp = fdopen(fd, "r")) == NULL) {
582 adios(NULL, "unable to fdopen file.");
584 /* Find the end of the headers. */
585 if ((c = getc(fp)) == '\n') {
586 /* Special check for no headers is needed. */
590 ** Copy lines from the input file to the
591 ** temporary file until we reach the end
595 while ((c = getc(fp)) != EOF) {
598 ungetc(c = getc(fp), fp);
599 if (c == '\n' || c == '-') {
608 fprintf(tmp, "%s: %s\n", comp, dtimenow());
611 /* Add body text header */
613 while (*cp == ' ' || *cp == '\t') {
617 while (*cp && *cp++ != '\n') {
621 fprintf(tmp, "%s: %*.*s", comp,
626 if (cp[-1] != '\n' && cp != text) {
633 ** We've been messing with the input file position. Move the
634 ** input file descriptor to the current place in the file
635 ** because the stock data copying routine uses the descriptor,
639 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
640 adios(NULL, "can't seek.");