* mf.c -- mail filter subroutines
*
* $Id$
+ *
+ * This code is Copyright (c) 2002, by the authors of nmh. See the
+ * COPYRIGHT file in the root directory of the nmh distribution for
+ * complete copyright information.
*/
#include <h/mf.h>
#include <ctype.h>
#include <stdio.h>
+#include <h/utils.h>
/*
* static prototypes
*/
static char *getcpy (char *);
-static char *add (char *, char *);
static void compress (char *, char *);
static int isat (char *);
static int parse_address (void);
for(;;)
pause();
}
- if ((p = malloc ((size_t) (strlen (s) + 2))))
- strcpy (p, s);
+ p = mh_xmalloc ((size_t) (strlen (s) + 2));
+ strcpy (p, s);
return p;
}
-static char *
-add (char *s1, char *s2)
-{
- register char *p;
-
- if (!s2)
- return getcpy (s1);
-
- if ((p = malloc ((size_t) (strlen (s1) + strlen (s2) + 2))))
- sprintf (p, "%s%s", s2, s1);
- free (s2);
- return p;
-}
-
int
isfrom(char *string)
{
static int
my_lex (char *buffer)
{
+ /* buffer should be at least BUFSIZ bytes long */
int i, gotat = 0;
register char c, *bp;
+/* Add C to the buffer bp. After use of this macro *bp is guaranteed to be within the buffer. */
+#define ADDCHR(C) do { *bp++ = (C); if ((bp - buffer) == (BUFSIZ-1)) goto my_lex_buffull; } while (0)
+
bp = buffer;
*bp = 0;
if (!cp)
return (last_lex = LX_END);
}
- if (c == '(')
- for (*bp++ = c, i = 0;;)
+ if (c == '(') {
+ ADDCHR(c);
+ for (i = 0;;)
switch (c = *cp++) {
case 0:
cp = NULL;
return (last_lex = LX_ERR);
case QUOTE:
- *bp++ = c;
+ ADDCHR(c);
if ((c = *cp++) == 0) {
cp = NULL;
return (last_lex = LX_ERR);
}
- *bp++ = c;
+ ADDCHR(c);
continue;
case '(':
i++;
default:
- *bp++ = c;
+ ADDCHR(c);
continue;
case ')':
- *bp++ = c;
+ ADDCHR(c);
if (--i < 0) {
*bp = 0;
note = note ? add (buffer, add (" ", note))
return my_lex (buffer);
}
}
+ }
- if (c == '"')
- for (*bp++ = c;;)
+ if (c == '"') {
+ ADDCHR(c);
+ for (;;)
switch (c = *cp++) {
case 0:
cp = NULL;
return (last_lex = LX_ERR);
case QUOTE:
- *bp++ = c;
+ ADDCHR(c);
if ((c = *cp++) == 0) {
cp = NULL;
return (last_lex = LX_ERR);
}
default:
- *bp++ = c;
+ ADDCHR(c);
continue;
case '"':
- *bp++ = c;
+ ADDCHR(c);
*bp = 0;
return (last_lex = LX_QSTR);
}
-
- if (c == '[')
- for (*bp++ = c;;)
+ }
+
+ if (c == '[') {
+ ADDCHR(c);
+ for (;;)
switch (c = *cp++) {
case 0:
cp = NULL;
return (last_lex = LX_ERR);
case QUOTE:
- *bp++ = c;
+ ADDCHR(c);
if ((c = *cp++) == 0) {
cp = NULL;
return (last_lex = LX_ERR);
}
default:
- *bp++ = c;
+ ADDCHR(c);
continue;
case ']':
- *bp++ = c;
+ ADDCHR(c);
*bp = 0;
return (last_lex = LX_DLIT);
}
-
- *bp++ = c;
+ }
+
+ ADDCHR(c);
*bp = 0;
for (i = 0; special[i].lx_chr != 0; i++)
if (c == special[i].lx_chr)
goto got_atom;
if (iscntrl (c) || isspace (c))
break;
- *bp++ = c;
+ ADDCHR(c);
}
got_atom: ;
if (c == 0)
last_lex = !gotat || cp == NULL || strchr(cp, '<') != NULL
? LX_ATOM : LX_AT;
return last_lex;
+
+ my_lex_buffull:
+ /* Out of buffer space. *bp is the last byte in the buffer */
+ *bp = 0;
+ return (last_lex = LX_ERR);
}
static char *pp = NULL;
if (pp == NULL)
- if (!(pp = malloc ((size_t) (len = BUFSIZ))))
- return NOTOK;
+ pp = mh_xmalloc ((size_t) (len = BUFSIZ));
for (ep = (cp = pp) + len - 2;;) {
switch (i = getc (in)) {
break;
}
if (cp >= ep) {
- if (!(dp = realloc (pp, (size_t) (len += BUFSIZ)))) {
- free (pp);
- pp = NULL;
- return NOTOK;
- }
- else
- cp += dp - pp, ep = (pp = cp) + len - 2;
+ dp = mh_xrealloc (pp, (size_t) (len += BUFSIZ));
+ cp += dp - pp, ep = (pp = cp) + len - 2;
}
}
}