Use sysexits.h for better exit-codes
[mmh] / uip / anno.c
index 7a65a7f..1c97179 100644 (file)
@@ -21,6 +21,7 @@
 #include <ctype.h>
 #include <sys/stat.h>
 #include <locale.h>
+#include <sysexits.h>
 
 static enum { MODE_ADD, MODE_DEL, MODE_LIST } mode = MODE_ADD;
 
@@ -90,20 +91,19 @@ main(int argc, char **argv)
                        switch (smatch(++cp, switches)) {
                        case AMBIGSW:
                                ambigsw(cp, switches);
-                               /* sysexits.h EX_USAGE */
-                               exit(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);
-                               exit(0);
+                               exit(argc == 2 ? EX_OK : EX_USAGE);
                        case VERSIONSW:
                                print_version(invo_name);
-                               exit(0);
+                               exit(argc == 2 ? EX_OK : EX_USAGE);
 
                        case DELETESW:  /* delete annotations */
                                mode = MODE_DEL;
@@ -115,23 +115,23 @@ main(int argc, char **argv)
 
                        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;
@@ -139,7 +139,7 @@ main(int argc, char **argv)
                                }
                                /* 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;
@@ -147,11 +147,11 @@ main(int argc, char **argv)
                                        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++;
@@ -179,12 +179,12 @@ main(int argc, char **argv)
                }
                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);
@@ -192,13 +192,13 @@ main(int argc, char **argv)
        }
 
        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) {
@@ -207,7 +207,7 @@ main(int argc, char **argv)
                else
                        annotate(file, comp, text, datesw, number,
                                        append, preserve);
-               exit(0);
+               exit(EX_OK);
        }
 
        if (!msgs.size)
@@ -217,21 +217,20 @@ main(int argc, char **argv)
        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])) {
-                       /* sysexits.h EX_USAGE */
-                       exit(1);
+                       exit(EX_SOFTWARE);
                }
        }
 
@@ -265,8 +264,7 @@ make_comp(unsigned char **ap)
                fflush(stdout);
 
                if (!fgets(buffer, sizeof buffer, stdin)) {
-                       /* sysexits.h EX_IOERR */
-                       exit(1);
+                       exit(EX_IOERR);
                }
                *ap = trimcpy(buffer);
        }
@@ -274,15 +272,15 @@ make_comp(unsigned char **ap)
        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);
 }
 
 
@@ -303,7 +301,7 @@ annolist(char *file, unsigned char *comp, int number)
        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. */
@@ -402,7 +400,7 @@ annotate(char *file, unsigned char *comp, char *text, int datesw,
        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);
 
@@ -411,7 +409,7 @@ annotate(char *file, unsigned char *comp, char *text, int datesw,
        **  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);
@@ -459,7 +457,7 @@ dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
        ** 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);
 
@@ -541,7 +539,7 @@ dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
                /* 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 != '-');
@@ -558,7 +556,7 @@ dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
        ** 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.");
        }
 }
 
@@ -582,7 +580,7 @@ doadd(int fd, unsigned char *comp, char *text, FILE *tmp, int datesw,
                ** 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') {
@@ -640,7 +638,7 @@ doadd(int fd, unsigned char *comp, char *text, FILE *tmp, int datesw,
        */
        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.");
                }
        }
 }