#include <fcntl.h>
#include <errno.h>
#include <utime.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <sys/stat.h>
+#include <locale.h>
+#include <sysexits.h>
static enum { MODE_ADD, MODE_DEL, MODE_LIST } mode = MODE_ADD;
int append = 0; /* append annotations instead of default prepend */
int number = 0; /* delete specific number of like elements if set */
-#ifdef LOCALE
setlocale(LC_ALL, "");
-#endif
invo_name = mhbasename(argv[0]);
context_read();
switch (smatch(++cp, switches)) {
case AMBIGSW:
ambigsw(cp, switches);
- done(1);
+ exit(EX_USAGE);
case UNKWNSW:
- adios(NULL, "-%s unknown", cp);
+ adios(EX_USAGE, NULL, "-%s unknown", cp);
case HELPSW:
snprintf(buf, sizeof(buf),
"%s [+folder] [msgs] [switches]",
invo_name);
print_help(buf, switches, 1);
- done(1);
+ exit(argc == 2 ? EX_OK : EX_USAGE);
case VERSIONSW:
print_version(invo_name);
- done(1);
+ exit(argc == 2 ? EX_OK : EX_USAGE);
case DELETESW: /* delete annotations */
mode = MODE_DEL;
case COMPSW:
if (comp)
- adios(NULL, "only one component at a time!");
+ adios(EX_USAGE, NULL, "only one component at a time!");
if (!(comp = *argp++) || *comp == '-')
- adios(NULL, "missing argument to %s",
+ adios(EX_USAGE, NULL, "missing argument to %s",
argp[-2]);
continue;
case TEXTSW:
if (text)
- adios(NULL, "only one body at a time!");
+ adios(EX_USAGE, NULL, "only one body at a time!");
if (!(text = *argp++) || *text == '-')
- adios(NULL, "missing argument to %s",
+ adios(EX_USAGE, NULL, "missing argument to %s",
argp[-2]);
continue;
case NUMBERSW: /* number listing or delete by number */
if (mode == MODE_ADD) {
- adios(NULL, "-number switch must appear after -list or -delete, only.");
+ adios(EX_USAGE, NULL, "-number switch must appear after -list or -delete, only.");
}
if (mode == MODE_LIST) {
number = 1;
}
/* MODE_DEL */
if (number) {
- adios(NULL, "only one number at a time!");
+ adios(EX_USAGE, NULL, "only one number at a time!");
}
if (*argp && strcmp(*argp, "all")==0) {
number = -1;
continue;
}
if (!*argp || !(number = atoi(*argp))) {
- adios(NULL, "missing argument to %s",
+ adios(EX_USAGE, NULL, "missing argument to %s",
argp[-1]);
}
if (number < 0) {
- adios(NULL, "invalid number (%d).",
+ adios(EX_USAGE, NULL, "invalid number (%d).",
number);
}
argp++;
}
if (*cp == '+' || *cp == '@') {
if (folder)
- adios(NULL, "only one folder at a time!");
+ adios(EX_USAGE, NULL, "only one folder at a time!");
else
folder = getcpy(expandfol(cp));
} else if (*cp == '/' || *cp == '.') {
if (file)
- adios(NULL, "only one file at a time!");
+ adios(EX_USAGE, NULL, "only one file at a time!");
file = cp;
} else {
app_msgarg(&msgs, cp);
}
if (file && (folder || msgs.size)) {
- adios(NULL, "Don't intermix files and messages.");
+ adios(EX_USAGE, NULL, "Don't intermix files and messages.");
}
if (!datesw && !text) {
- adios(NULL, "-nodate without -text is a no-op.");
+ adios(EX_USAGE, NULL, "-nodate without -text is a no-op.");
}
if (number && text) {
- adios(NULL, "Don't combine -number with -text.");
+ adios(EX_USAGE, NULL, "Don't combine -number with -text.");
}
if (file) {
else
annotate(file, comp, text, datesw, number,
append, preserve);
- done(0);
+ exit(EX_OK);
}
if (!msgs.size)
maildir = toabsdir(folder);
if (chdir(maildir) == NOTOK)
- adios(maildir, "unable to change directory to");
+ adios(EX_OSERR, maildir, "unable to change directory to");
/* read folder and create message structure */
if (!(mp = folder_read(folder)))
- adios(NULL, "unable to read folder %s", folder);
+ adios(EX_IOERR, NULL, "unable to read folder %s", folder);
/* check for empty folder */
if (mp->nummsg == 0)
- adios(NULL, "no messages in %s", folder);
+ adios(EX_DATAERR, NULL, "no messages in %s", folder);
/* parse all the message ranges/sequences and set SELECTED */
- for (msgnum = 0; msgnum < msgs.size; msgnum++)
- if (!m_convert(mp, msgs.msgs[msgnum]))
- done(1);
+ for (msgnum = 0; msgnum < msgs.size; msgnum++) {
+ if (!m_convert(mp, msgs.msgs[msgnum])) {
+ exit(EX_SOFTWARE);
+ }
+ }
/* annotate all the SELECTED messages */
for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
seq_save(mp);
folder_free(mp);
context_save();
- done(0);
- return 1;
+ return 0;
}
static void
fflush(stdout);
if (!fgets(buffer, sizeof buffer, stdin)) {
- done(1);
+ exit(EX_IOERR);
}
*ap = trimcpy(buffer);
}
if ((cp = *ap + strlen(*ap) - 1) > *ap && *cp == ':')
*cp = '\0';
if (strlen(*ap) == 0)
- adios(NULL, "null component name");
+ adios(EX_SOFTWARE, NULL, "null component name");
if (**ap == '-')
- adios(NULL, "invalid component name %s", *ap);
+ adios(EX_CONFIG, NULL, "invalid component name %s", *ap);
if (strlen(*ap) >= NAMESZ)
- adios(NULL, "too large component name %s", *ap);
+ adios(EX_SOFTWARE, NULL, "too large component name %s", *ap);
for (cp = *ap; *cp; cp++)
if (!isalnum(*cp) && *cp != '-')
- adios(NULL, "invalid component name %s", *ap);
+ adios(EX_CONFIG, NULL, "invalid component name %s", *ap);
}
int n; /* number of bytes written */
if ((fp = fopen(file, "r")) == NULL) {
- adios(file, "unable to open");
+ adios(EX_IOERR, file, "unable to open");
}
/* We'll grow this buffer as needed. */
if (strncasecmp(field, comp, length)==0 &&
field[length] == ':') {
- for (cp = field + length + 1;
- *cp == ' ' || *cp == '\t'; cp++) {
- continue;
- }
+ cp = trim(field + length + 1);
if (number) {
printf("%d\t", count++);
}
fclose(tmp);
if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
- adios(tmpfil, "unable to open for re-reading");
+ adios(EX_IOERR, tmpfil, "unable to open for re-reading");
}
lseek(fd, (off_t) 0, SEEK_SET);
** so the file has to be truncated or it will contain garbage.
*/
if (mode == MODE_DEL && ftruncate(fd, 0) == -1) {
- adios(tmpfil, "unable to truncate.");
+ adios(EX_IOERR, tmpfil, "unable to truncate.");
}
cpydata(tmpfd, fd, tmpfil, file);
close(tmpfd);
** This buffer is grown as needed later.
*/
if ((fp = fdopen(fd, "r")) == NULL) {
- adios(NULL, "unable to fdopen file.");
+ adios(EX_IOERR, NULL, "unable to fdopen file.");
}
field = (char *)mh_xmalloc(field_size);
/* Copy it. */
if ((n = fputs(field, tmp)) == EOF ||
(c=='\n' && fputc('\n', tmp)==EOF)) {
- adios(NULL, "unable to write temporary file.");
+ adios(EX_CANTCREAT, NULL, "unable to write temporary file.");
}
} while (*field && *field != '-');
** not the pointer.
*/
if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
- adios(NULL, "can't seek.");
+ adios(EX_IOERR, NULL, "can't seek.");
}
}
** read in. This buffer is grown as needed later.
*/
if ((fp = fdopen(fd, "r")) == NULL) {
- adios(NULL, "unable to fdopen file.");
+ adios(EX_IOERR, NULL, "unable to fdopen file.");
}
/* Find the end of the headers. */
if ((c = getc(fp)) == '\n') {
*/
if (append) {
if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
- adios(NULL, "can't seek.");
+ adios(EX_IOERR, NULL, "can't seek.");
}
}
}