projects
/
mmh
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
This is mmh-0.3
[mmh]
/
uip
/
anno.c
diff --git
a/uip/anno.c
b/uip/anno.c
index
2647d97
..
9e287be
100644
(file)
--- a/
uip/anno.c
+++ b/
uip/anno.c
@@
-17,6
+17,11
@@
#include <fcntl.h>
#include <errno.h>
#include <utime.h>
#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;
static enum { MODE_ADD, MODE_DEL, MODE_LIST } mode = MODE_ADD;
@@
-86,19
+91,19
@@
main(int argc, char **argv)
switch (smatch(++cp, switches)) {
case AMBIGSW:
ambigsw(cp, switches);
switch (smatch(++cp, switches)) {
case AMBIGSW:
ambigsw(cp, switches);
- done(1);
+ exit(EX_USAGE);
case UNKWNSW:
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);
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);
case VERSIONSW:
print_version(invo_name);
- done(1);
+ exit(argc == 2 ? EX_OK : EX_USAGE);
case DELETESW: /* delete annotations */
mode = MODE_DEL;
case DELETESW: /* delete annotations */
mode = MODE_DEL;
@@
-110,23
+115,23
@@
main(int argc, char **argv)
case COMPSW:
if (comp)
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 == '-')
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)
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 == '-')
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) {
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;
}
if (mode == MODE_LIST) {
number = 1;
@@
-134,7
+139,7
@@
main(int argc, char **argv)
}
/* MODE_DEL */
if (number) {
}
/* 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;
}
if (*argp && strcmp(*argp, "all")==0) {
number = -1;
@@
-142,11
+147,11
@@
main(int argc, char **argv)
continue;
}
if (!*argp || !(number = atoi(*argp))) {
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) {
argp[-1]);
}
if (number < 0) {
- adios(NULL, "invalid number (%d).",
+ adios(EX_USAGE, NULL, "invalid number (%d).",
number);
}
argp++;
number);
}
argp++;
@@
-174,12
+179,12
@@
main(int argc, char **argv)
}
if (*cp == '+' || *cp == '@') {
if (folder)
}
if (*cp == '+' || *cp == '@') {
if (folder)
- adios(NULL, "only one folder at a time!");
+ adios(EX_USAGE, NULL, "only one folder at a time!");
else
else
- folder = getcpy(expandfol(cp));
+ folder = mh_xstrdup(expandfol(cp));
} else if (*cp == '/' || *cp == '.') {
if (file)
} 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);
file = cp;
} else {
app_msgarg(&msgs, cp);
@@
-187,13
+192,13
@@
main(int argc, char **argv)
}
if (file && (folder || msgs.size)) {
}
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) {
}
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) {
}
if (number && text) {
- adios(NULL, "Don't combine -number with -text.");
+ adios(EX_USAGE, NULL, "Don't combine -number with -text.");
}
if (file) {
}
if (file) {
@@
-202,7
+207,7
@@
main(int argc, char **argv)
else
annotate(file, comp, text, datesw, number,
append, preserve);
else
annotate(file, comp, text, datesw, number,
append, preserve);
- done(0);
+ exit(EX_OK);
}
if (!msgs.size)
}
if (!msgs.size)
@@
-212,20
+217,22
@@
main(int argc, char **argv)
maildir = toabsdir(folder);
if (chdir(maildir) == NOTOK)
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)))
/* 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)
/* 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 */
/* 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++) {
/* annotate all the SELECTED messages */
for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
@@
-243,8
+250,7
@@
main(int argc, char **argv)
seq_save(mp);
folder_free(mp);
context_save();
seq_save(mp);
folder_free(mp);
context_save();
- done(0);
- return 1;
+ return 0;
}
static void
}
static void
@@
-258,7
+264,7
@@
make_comp(unsigned char **ap)
fflush(stdout);
if (!fgets(buffer, sizeof buffer, stdin)) {
fflush(stdout);
if (!fgets(buffer, sizeof buffer, stdin)) {
- done(1);
+ exit(EX_IOERR);
}
*ap = trimcpy(buffer);
}
}
*ap = trimcpy(buffer);
}
@@
-266,15
+272,15
@@
make_comp(unsigned char **ap)
if ((cp = *ap + strlen(*ap) - 1) > *ap && *cp == ':')
*cp = '\0';
if (strlen(*ap) == 0)
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 == '-')
if (**ap == '-')
- adios(NULL, "invalid component name %s", *ap);
+ adios(EX_CONFIG, NULL, "invalid component name %s", *ap);
if (strlen(*ap) >= NAMESZ)
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 != '-')
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
+301,11
@@
annolist(char *file, unsigned char *comp, int number)
int n; /* number of bytes written */
if ((fp = fopen(file, "r")) == NULL) {
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. */
}
/* 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. */
make_comp(&comp);
length = strlen(comp); /* Convenience copy. */
@@
-319,8
+325,7
@@
annolist(char *file, unsigned char *comp, int number)
break;
}
if (++n >= field_size - 1) {
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;
}
}
cp = field + n - 1;
}
}
@@
-328,10
+333,7
@@
annolist(char *file, unsigned char *comp, int number)
if (strncasecmp(field, comp, length)==0 &&
field[length] == ':') {
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++);
}
if (number) {
printf("%d\t", count++);
}
@@
-340,7
+342,7
@@
annolist(char *file, unsigned char *comp, int number)
} while (*field && *field != '-');
} while (*field && *field != '-');
- free(field);
+ mh_free0(&field);
fclose(fp);
return;
fclose(fp);
return;
@@
-397,7
+399,7
@@
annotate(char *file, unsigned char *comp, char *text, int datesw,
fclose(tmp);
if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
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);
}
lseek(fd, (off_t) 0, SEEK_SET);
@@
-406,7
+408,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) {
** 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);
}
cpydata(tmpfd, fd, tmpfil, file);
close(tmpfd);
@@
-454,9
+456,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) {
** 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
/*
** Copy lines from the input file to the temporary file
@@
-484,8
+486,7
@@
dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
}
if (++n >= field_size - 1) {
}
if (++n >= field_size - 1) {
- field = (char *) mh_xrealloc(field,
- field_size *= 2);
+ field = mh_xrealloc(field, field_size *= 2);
cp = field + n - 1;
}
}
cp = field + n - 1;
}
}
@@
-536,12
+537,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)) {
/* 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 != '-');
}
} while (*field && *field != '-');
- free(field);
+ mh_free0(&field);
fflush(tmp);
fflush(fp); /* The underlying fd will be closed by lkclose() */
fflush(tmp);
fflush(fp); /* The underlying fd will be closed by lkclose() */
@@
-553,7
+554,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) {
** 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.");
}
}
}
}
@@
-577,7
+578,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) {
** 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') {
}
/* Find the end of the headers. */
if ((c = getc(fp)) == '\n') {
@@
-635,7
+636,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) {
*/
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.");
}
}
}
}
}
}