#include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include "../mailbox/include/stream0.h" #define C(X) {int e; if((e = X) != 0) { printf("%s failed: %s\n", #X, strerror(e)); exit(1); } } static int m_header_fill (header_t header, char *buffer, size_t buflen, off_t off, size_t * pnread) { message_t msg = header_get_owner (header); stream_t stream = 0; size_t nread = 0; int status = 0; C( message_get_stream(msg, &stream) ) if (buffer == NULL || buflen == 0) { if (pnread) *pnread = nread; return 0; } /* Position the file pointer and the buffer. */ status = stream_readline (stream, buffer, buflen, off, &nread); /* Detect the end of the headers. */ if( buffer && buffer[0] == '\n' && buffer[1] == '\0' ) nread = 0; if (pnread) *pnread = nread; return status; } /* static int mh_body_read (stream_t is, char *buffer, size_t buflen, off_t off, size_t *pnread) { body_t body = stream_get_owner (is); message_t msg = body_get_owner (body); struct _mh_message *mhm = message_get_owner (msg); mh_pool_open (mhm); return mh_readstream (mhm, buffer, buflen, off, pnread, 0, mhm->body_start, mhm->body_end); } */ int main (int argc, char **argv) { // mailer_t mailer = 0; message_t msg = 0; stream_t in = 0; stream_t out = 0; size_t count = 0; off_t offset = 0; char buf[BUFSIZ]; C( message_create (&msg, NULL) ) C( stdio_stream_create(&in) ) C( stream_open(in, (char*) stdin, 0, MU_STREAM_READ) ) C( message_set_stream(msg, in, 0) ) /* ripped from _mh_attach_message */ /* Set the header. */ { header_t header = NULL; C( header_create (&header, NULL, 0, msg) ) header_set_fill (header, m_header_fill, msg); // header_set_size (header, mh_header_size, msg); // header_set_lines (header, mh_header_lines, msg); message_set_header (msg, header, 0); } #if 0 /* Prepare the body. */ { body_t body = NULL; stream_t stream = NULL; if ((status = body_create (&body, msg)) != 0 || (status = stream_create (&stream, mailbox->flags | MU_STREAM_SEEKABLE, body)) != 0) { body_destroy (&body, msg); stream_destroy (&stream, body); message_destroy (&msg, mhm); return status; } stream_set_read (stream, mh_body_read, body); stream_set_readline (stream, mh_body_readline, body); stream_set_size (stream, mh_stream_size, body); body_set_stream (body, stream, msg); body_set_size (body, mh_body_size, msg); body_set_lines (body, mh_body_lines, msg); message_set_body (msg, body, mhm); } #endif /* try and print the message */ C( message_set_stream(msg, 0, 0) ) C( message_get_stream (msg, &out) ) do { offset += count; count = sizeof(buf); C( stream_read(out, buf, count, offset, &count) ) write(1, buf, count); } while(count != 0); return 0; }