X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=uip%2Fanno.c;h=33d2b49dec03d53c781946c15098b9f92a219862;hp=e8742385fa2475c1f704de538145c116e3627c3f;hb=6e9577f324bef90765a5edc02044eb111ec48072;hpb=6c0952c1d53bc73b1beefc46ef9ee08cd5213ac3 diff --git a/uip/anno.c b/uip/anno.c index e874238..33d2b49 100644 --- a/uip/anno.c +++ b/uip/anno.c @@ -17,6 +17,11 @@ #include #include #include +#include +#include +#include +#include +#include static enum { MODE_ADD, MODE_DEL, MODE_LIST } mode = MODE_ADD; @@ -48,6 +53,8 @@ static struct swit switches[] = { { NULL, 0 } }; +char *version=VERSION; + /* ** static prototypes */ @@ -86,19 +93,19 @@ main(int argc, char **argv) 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; @@ -110,23 +117,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; @@ -134,7 +141,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; @@ -142,11 +149,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++; @@ -174,12 +181,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)); + folder = mh_xstrdup(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); @@ -187,13 +194,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) { @@ -202,7 +209,7 @@ main(int argc, char **argv) else annotate(file, comp, text, datesw, number, append, preserve); - done(0); + exit(EX_OK); } if (!msgs.size) @@ -212,20 +219,22 @@ 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])) - 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++) { @@ -243,8 +252,7 @@ main(int argc, char **argv) seq_save(mp); folder_free(mp); context_save(); - done(0); - return 1; + return 0; } static void @@ -258,7 +266,7 @@ make_comp(unsigned char **ap) fflush(stdout); if (!fgets(buffer, sizeof buffer, stdin)) { - done(1); + exit(EX_IOERR); } *ap = trimcpy(buffer); } @@ -266,15 +274,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); } @@ -295,11 +303,11 @@ 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. */ - field = (char *)mh_xmalloc(field_size = 256); + field = mh_xcalloc(field_size = 256, sizeof(char)); make_comp(&comp); length = strlen(comp); /* Convenience copy. */ @@ -319,8 +327,7 @@ annolist(char *file, unsigned char *comp, int number) break; } if (++n >= field_size - 1) { - field = (char *)mh_xrealloc(field, - field_size += 256); + field = mh_xrealloc(field, field_size += 256); cp = field + n - 1; } } @@ -337,7 +344,7 @@ annolist(char *file, unsigned char *comp, int number) } while (*field && *field != '-'); - free(field); + mh_free0(&field); fclose(fp); return; @@ -394,7 +401,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); @@ -403,7 +410,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); @@ -451,9 +458,9 @@ 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); + field = mh_xcalloc(field_size, sizeof(char)); /* ** Copy lines from the input file to the temporary file @@ -481,8 +488,7 @@ dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number) } if (++n >= field_size - 1) { - field = (char *) mh_xrealloc(field, - field_size *= 2); + field = mh_xrealloc(field, field_size *= 2); cp = field + n - 1; } } @@ -533,12 +539,12 @@ 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 != '-'); - free(field); + mh_free0(&field); fflush(tmp); fflush(fp); /* The underlying fd will be closed by lkclose() */ @@ -550,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."); } } @@ -574,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') { @@ -632,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."); } } }