MIT license. Compile with -DMAIN to build the demonstrator.
******************************************************************************/
+
+#define _XOPEN_SOURCE 600
+#define __BSD_VISIBLE 1
+
+#include "config.h"
+#include "fetchmail.h"
+
#include <stdio.h>
#include <ctype.h>
#include <string.h>
+#include <strings.h>
#include <stdlib.h>
-#include "fetchmail.h"
+#include "sdump.h"
#ifndef MAIN
-#include "i18n.h"
+#include "gettext.h"
#else
#include <unistd.h>
static int verbose;
-char *program_name = "rfc822";
+const char *program_name = "rfc822";
#endif /* MAIN */
#ifndef TRUE
#define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
-unsigned char *reply_hack(
- unsigned char *buf /* header to be hacked */,
- const unsigned char *host /* server hostname */,
+#define BEFORE_EOL(s) (strcspn((s), "\r\n"))
+
+char *reply_hack(
+ char *buf /* header to be hacked */,
+ const char *host /* server hostname */,
size_t *length)
/* hack message headers so replies will work properly */
{
- unsigned char *from, *cp, last_nws = '\0', *parens_from = NULL;
+ char *from, *cp, last_nws = '\0', *parens_from = NULL;
int parendepth, state, has_bare_name_part, has_host_part;
#ifndef MAIN
int addresscount = 1;
}
#ifndef MAIN
- if (outlevel >= O_DEBUG)
- report_build(stdout, GT_("About to rewrite %s"), buf);
+ if (outlevel >= O_DEBUG) {
+ report_build(stdout, GT_("About to rewrite %s...\n"), (cp = sdump(buf, BEFORE_EOL(buf))));
+ xfree(cp);
+ }
/* make room to hack the address; buf must be malloced */
for (cp = buf; *cp; cp++)
- if (*cp == ',' || isspace(*cp))
+ if (*cp == ',' || isspace((unsigned char)*cp))
addresscount++;
- buf = (unsigned char *)xrealloc(buf, strlen(buf) + addresscount * (strlen(host) + 1) + 1);
+ buf = (char *)xrealloc(buf, strlen(buf) + addresscount * (strlen(host) + 1) + 1);
#endif /* MAIN */
/*
#ifdef MAIN
if (verbose)
{
- printf("state %d: %s", state, (char *)buf);
- printf("%*s^\n", from - buf + 10, " ");
+ printf("state %d: %s", state, buf);
+ printf("%*s^\n", (int)(from - buf + 10), " ");
}
#endif /* MAIN */
if (state != 2)
break;
case 1: /* we've seen the colon, we're looking for addresses */
- if (!isspace(*from))
+ if (!isspace((unsigned char)*from))
last_nws = *from;
if (*from == '<')
state = 3;
&& last_nws != ';')
{
int hostlen;
- unsigned char *p;
+ char *p;
p = from;
if (parens_from)
from = parens_from;
- while (isspace(*from) || (*from == ','))
+ while (isspace((unsigned char)*from) || (*from == ','))
--from;
from++;
hostlen = strlen(host);
{
parens_from = from;
}
- else if (!isspace(*from))
+ else if (!isspace((unsigned char)*from))
has_bare_name_part = TRUE;
break;
}
#ifndef MAIN
- if (outlevel >= O_DEBUG)
- report_complete(stdout, GT_("Rewritten version is %s\n"), buf);
+ if (outlevel >= O_DEBUG) {
+ report_complete(stdout, GT_("...rewritten version is %s.\n"),
+ (cp = sdump(buf, BEFORE_EOL(buf))));
+ xfree(cp)
+ }
+
#endif /* MAIN */
*length = strlen(buf);
return(buf);
}
-unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to continue previous hdr */)
+char *nxtaddr(const char *hdr /* header to be parsed, NUL to continue previous hdr */)
/* parse addresses in succession out of a specified RFC822 header */
{
- static unsigned char address[BUFSIZ];
- static int tp;
- static const unsigned char *hp;
+ static char address[BUFSIZ];
+ static size_t tp;
+ static const char *hp;
static int state, oldstate;
#ifdef MAIN
- static const unsigned char *orighdr;
+ static const char *orighdr;
#endif /* MAIN */
int parendepth = 0;
tp = 0;
}
+ if (!hp) return NULL;
+
for (; *hp; hp++)
{
#ifdef MAIN
if (verbose)
{
- printf("state %d: %s", state, (char *)orighdr);
- printf("%*s^\n", hp - orighdr + 10, " ");
+ printf("state %d: %s", state, orighdr);
+ printf("%*s^\n", (int)(hp - orighdr + 10), " ");
}
#endif /* MAIN */
state = ENDIT_ALL;
if (tp)
{
- while (tp >= 0 && isspace(address[tp--]))
- continue;
- address[++tp] = '\0';
+ while (tp > 0 && isspace((unsigned char)address[tp - 1]))
+ tp--;
+ address[tp] = '\0';
tp = 0;
return (address);
}
- return((unsigned char *)NULL);
+ return(NULL);
}
else if (*hp == '\\') /* handle RFC822 escaping */
{
state = INSIDE_BRACKETS;
tp = 0;
}
- else if (*hp != ',' && !isspace(*hp))
+ else if (*hp != ',' && !isspace((unsigned char)*hp))
{
--hp;
state = BARE_ADDRESS;
state = INSIDE_DQUOTE;
address[NEXTTP()] = *hp;
}
- else if (!isspace(*hp)) /* just take it, ignoring whitespace */
+ else if (!isspace((unsigned char)*hp)) /* just take it, ignoring whitespace */
address[NEXTTP()] = *hp;
break;
}
#ifdef MAIN
-static void parsebuf(unsigned char *longbuf, int reply)
+static void parsebuf(char *longbuf, int reply)
{
- unsigned char *cp;
- size_t dummy;
+ char *cp;
+ size_t dummy;
if (reply)
{
printf("Rewritten buffer: %s", (char *)longbuf);
}
else
- if ((cp = nxtaddr(longbuf)) != (unsigned char *)NULL)
+ if ((cp = nxtaddr(longbuf)) != (char *)NULL)
do {
printf("\t-> \"%s\"\n", (char *)cp);
} while
- ((cp = nxtaddr((unsigned char *)NULL)) != (unsigned char *)NULL);
+ ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
}
int main(int argc, char *argv[])
{
- unsigned char buf[BUFSIZ], longbuf[BUFSIZ];
- int ch, reply;
+ char buf[BUFSIZ], longbuf[BUFSIZ];
+ int ch, reply;
verbose = reply = FALSE;
while ((ch = getopt(argc, argv, "rv")) != EOF)
break;
}
+ longbuf[0] = '\0';
+
while (fgets(buf, sizeof(buf)-1, stdin))
{
if (buf[0] == ' ' || buf[0] == '\t')