Commit d17ec29c d17ec29ca42139df7588ee4f85bbeb8bd20c93e8 by Sergey Poznyakoff

Use stringrefs, add some guidelines for further editing.

* comsat/action.c
* imap4d/fetch.c
* imap4d/search.c
* mh/burst.c
* mh/comp.c
* mh/forw.c
* mh/mh_ctx.c
* mh/mh_format.c
* mh/mh_list.c
* mh/mhn.c
* mh/pick.y
* pop3d/retr.c
* pop3d/top.c
* readmsg/msglist.c
* readmsg/readmsg.c
1 parent 3437509f
......@@ -155,7 +155,7 @@ expand_escape (char **pp, mu_message_t msg, struct obstack *stk)
if (lncount == 0)
lncount = maxlines;
if (mu_message_get_body (msg, &body) == 0
&& mu_body_get_stream (body, &stream) == 0)
&& mu_body_get_streamref (body, &stream) == 0)
{
size_t nread;
char *buf = malloc (size+1);
......@@ -179,6 +179,7 @@ expand_escape (char **pp, mu_message_t msg, struct obstack *stk)
}
obstack_grow (stk, buf, size);
}
mu_stream_destroy (&stream);
free (buf);
}
*pp = p;
......
......@@ -886,6 +886,7 @@ _frt_body (struct fetch_function_closure *ffc,
mu_message_t msg;
mu_stream_t stream = NULL;
size_t size = 0, lines = 0;
int rc;
set_seen (ffc, frt);
if (ffc->name)
......@@ -898,10 +899,12 @@ _frt_body (struct fetch_function_closure *ffc,
util_send (" \"\"");
return RESP_OK;
}
mu_message_get_stream (msg, &stream);
mu_message_get_streamref (msg, &stream);
mu_message_size (msg, &size);
mu_message_lines (msg, &lines);
return fetch_io (stream, ffc->start, ffc->size, size + lines);
rc = fetch_io (stream, ffc->start, ffc->size, size + lines);
mu_stream_destroy (&stream);
return rc;
}
static int
......@@ -912,6 +915,7 @@ _frt_body_text (struct fetch_function_closure *ffc,
mu_body_t body = NULL;
mu_stream_t stream = NULL;
size_t size = 0, lines = 0;
int rc;
set_seen (ffc, frt);
if (ffc->name)
......@@ -928,8 +932,10 @@ _frt_body_text (struct fetch_function_closure *ffc,
mu_message_get_body (msg, &body);
mu_body_size (body, &size);
mu_body_lines (body, &lines);
mu_body_get_stream (body, &stream);
return fetch_io (stream, ffc->start, ffc->size, size + lines);
mu_body_get_streamref (body, &stream);
rc = fetch_io (stream, ffc->start, ffc->size, size + lines);
mu_stream_destroy (&stream);
return rc;
}
static int
......@@ -953,6 +959,7 @@ _frt_header (struct fetch_function_closure *ffc,
mu_header_t header = NULL;
mu_stream_t stream = NULL;
size_t size = 0, lines = 0;
int rc;
set_seen (ffc, frt);
if (ffc->name)
......@@ -969,8 +976,10 @@ _frt_header (struct fetch_function_closure *ffc,
mu_message_get_header (msg, &header);
mu_header_size (header, &size);
mu_header_lines (header, &lines);
mu_header_get_stream (header, &stream);
return fetch_io (stream, ffc->start, ffc->size, size + lines);
mu_header_get_streamref (header, &stream);
rc = fetch_io (stream, ffc->start, ffc->size, size + lines);
mu_stream_destroy (&stream);
return rc;
}
static int
......
......@@ -842,7 +842,7 @@ _scan_body (struct parsebuf *pb, char *text)
mu_message_get_body (pb->msg, &body);
mu_body_size (body, &size);
mu_body_lines (body, &lines);
mu_body_get_stream (body, &stream);
mu_body_get_streamref (body, &stream);
rc = 0;
while (rc == 0
&& mu_stream_read (stream, buffer, sizeof(buffer)-1, &n) == 0
......@@ -851,6 +851,7 @@ _scan_body (struct parsebuf *pb, char *text)
buffer[n] = 0;
rc = util_strcasestr (buffer, text) != NULL;
}
mu_stream_destroy (&stream);
return rc;
}
......
......@@ -286,8 +286,7 @@ burst_digest (mu_message_t msg)
exit (1);
}
mu_message_get_stream (msg, &is);
mu_stream_seek (is, 0, MU_SEEK_SET, NULL);
mu_message_get_streamref (msg, &is);
while (mu_stream_read (is, buf, bufsize, &n) == 0
&& n > 0)
{
......@@ -339,6 +338,7 @@ burst_digest (mu_message_t msg)
flush_stream (&os, buf + start, i - start);
}
mu_stream_destroy (&is);
free (buf);
if (os)
......@@ -390,8 +390,10 @@ burst_or_copy (mu_message_t msg, int recursive, int copy)
mu_body_t body;
mu_message_get_body (msg, &body);
mu_body_get_stream (body, &str);
mu_body_get_streamref (body, &str);
/* FIXME: Check if str is actually destroyed.
See mailbox/message_stream.c
*/
msg = mh_stream_to_message (str);
}
free (value);
......@@ -487,8 +489,6 @@ msg_copy (size_t num, const char *file)
mu_attribute_t attr = NULL;
mu_stream_t istream, ostream;
int rc;
size_t n;
char buf[512];
if ((rc = mu_file_stream_create (&ostream,
file,
......@@ -501,13 +501,16 @@ msg_copy (size_t num, const char *file)
}
mu_mailbox_get_message (tmpbox, num, &msg);
mu_message_get_stream (msg, &istream);
mu_stream_seek (istream, 0, MU_SEEK_SET, NULL);
while (rc == 0
&& mu_stream_read (istream, buf, sizeof buf, &n) == 0
&& n > 0)
mu_message_get_streamref (msg, &istream);
/* FIXME: Implement RFC 934 FSA? */
rc = mu_stream_write (ostream, buf, n, NULL);
rc = mu_stream_copy (ostream, istream, 0);
if (rc)
{
mu_error (_("copy stream error: %s"), mu_strerror (rc));
exit (1);
}
mu_stream_destroy (&istream);
mu_stream_close (ostream);
mu_stream_destroy (&ostream);
......
......@@ -160,21 +160,9 @@ copy_message (mu_mailbox_t mbox, size_t n, const char *file)
mu_stream_t in;
mu_stream_t out;
int rc;
size_t size;
char *buffer;
size_t bufsize, rdsize;
mu_mailbox_get_message (mbox, n, &msg);
mu_message_size (msg, &size);
for (bufsize = size; bufsize > 0 && (buffer = malloc (bufsize)) == 0;
bufsize /= 2)
;
if (!bufsize)
mh_err_memory (1);
mu_message_get_stream (msg, &in);
mu_message_get_streamref (msg, &in);
if ((rc = mu_file_stream_create (&out,
file, MU_STREAM_RDWR|MU_STREAM_CREAT)) != 0
......@@ -182,27 +170,20 @@ copy_message (mu_mailbox_t mbox, size_t n, const char *file)
{
mu_error (_("cannot open output file \"%s\": %s"),
file, mu_strerror (rc));
free (buffer);
return 1;
}
mu_stream_seek (in, 0, MU_SEEK_SET, NULL);
while (size > 0
&& (rc = mu_stream_read (in, buffer, bufsize, &rdsize)) == 0
&& rdsize > 0)
{
if ((rc = mu_stream_write (out, buffer, rdsize, NULL)) != 0)
{
mu_error (_("error writing to \"%s\": %s"),
file, mu_strerror (rc));
break;
}
size -= rdsize;
mu_stream_destroy (&in);
return rc;
}
rc = mu_stream_copy (out, in, 0);
mu_stream_destroy (&in);
mu_stream_close (out);
mu_stream_destroy (&out);
if (rc)
{
mu_error (_("error copying to \"%s\": %s"),
file, mu_strerror (rc));
}
return rc;
}
......
......@@ -249,10 +249,9 @@ msg_copy (mu_message_t msg, mu_stream_t ostream)
char buf[512];
enum rfc934_state state = S1;
rc = mu_message_get_stream (msg, &istream);
rc = mu_message_get_streamref (msg, &istream);
if (rc)
return rc;
mu_stream_seek (istream, 0, SEEK_SET, NULL);
while (rc == 0
&& mu_stream_read (istream, buf, sizeof buf, &n) == 0
&& n > 0)
......@@ -286,6 +285,7 @@ msg_copy (mu_message_t msg, mu_stream_t ostream)
if (i > start)
rc = mu_stream_write (ostream, buf + start, i - start, NULL);
}
mu_stream_destroy (&istream);
return rc;
}
......
......@@ -146,16 +146,15 @@ mh_context_write (mh_context_t *ctx)
return MU_ERR_FAILURE;
}
mu_header_get_stream (ctx->header, &stream);
mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
/* FIXME: Use mu_stream+copy */
mu_header_get_streamref (ctx->header, &stream);
while (mu_stream_read (stream, buffer, sizeof buffer - 1, &n) == 0
&& n != 0)
{
buffer[n] = '\0';
fprintf (fp, "%s", buffer);
}
mu_stream_destroy (&stream);
fclose (fp);
return 0;
}
......
......@@ -609,8 +609,10 @@ mh_format (mh_format_t *fmt, mu_message_t msg, size_t msgno,
strobj_free (&mach.arg_str);
mu_message_get_body (mach.message, &body);
mu_body_size (body, &size);
mu_body_get_stream (body, &stream);
if (size == 0 || !stream)
if (size == 0)
break;
mu_body_get_streamref (body, &stream);
if (!stream)
break;
if (size > rest)
size = rest;
......@@ -619,7 +621,6 @@ mh_format (mh_format_t *fmt, mu_message_t msg, size_t msgno,
mach.arg_str.size = size;
str_off = 0;
mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
while (!mu_stream_read (stream, mach.arg_str.ptr + str_off,
mach.arg_str.size - str_off, &nread)
&& nread != 0
......@@ -629,6 +630,7 @@ mh_format (mh_format_t *fmt, mu_message_t msg, size_t msgno,
if (nread)
str_off += nread;
}
mu_stream_destroy (&stream);
mach.arg_str.ptr[str_off] = 0;
}
break;
......
......@@ -687,7 +687,7 @@ eval_body (struct eval_env *env)
env->prefix = env->svar[S_COMPONENT];
mu_message_get_body (env->msg, &body);
mu_body_get_stream (body, &input);
mu_body_get_streamref (body, &input);
if (env->bvar[B_DECODE])
{
......@@ -707,15 +707,13 @@ eval_body (struct eval_env *env)
}
}
mu_stream_seek (input, 0, SEEK_SET, NULL);
while (mu_stream_readline (input, buf, sizeof buf, &n) == 0
&& n > 0)
{
buf[n] = 0;
print (env, buf, 0);
}
if (dstr)
mu_stream_destroy (&dstr);
mu_stream_destroy (&input);
return 0;
}
......
......@@ -962,6 +962,7 @@ _free_env (char **env)
free (env);
}
/* FIXME: Use mimehdr.c functions instead */
int
get_extbody_params (mu_message_t msg, char **content, char **descr)
{
......@@ -972,8 +973,7 @@ get_extbody_params (mu_message_t msg, char **content, char **descr)
size_t n;
mu_message_get_body (msg, &body);
mu_body_get_stream (body, &stream);
mu_stream_seek (stream, 0, SEEK_SET, NULL);
mu_body_get_streamref (body, &stream);
while (rc == 0
&& mu_stream_readline (stream, buf, sizeof buf, &n) == 0
......@@ -1006,6 +1006,7 @@ get_extbody_params (mu_message_t msg, char **content, char **descr)
*content = strdup (p);
}
}
mu_stream_destroy (&stream);
return 0;
}
......@@ -1113,7 +1114,7 @@ mhn_message_size (mu_message_t msg, size_t *psize)
{
mu_stream_t dstr = NULL, bstr = NULL;
if (mu_body_get_stream (body, &bstr) == 0)
if (mu_body_get_streamref (body, &bstr) == 0)
{
mu_header_t hdr;
char *encoding;
......@@ -1132,7 +1133,6 @@ mhn_message_size (mu_message_t msg, size_t *psize)
char buf[512];
size_t n;
mu_stream_seek (dstr, 0, MU_SEEK_SET, NULL);
while (mu_stream_read (dstr, buf, sizeof buf, &n) == 0
&& n > 0)
size += n;
......@@ -1141,6 +1141,7 @@ mhn_message_size (mu_message_t msg, size_t *psize)
*psize = size;
return 0;
}
mu_stream_destroy (&bstr);
}
}
......@@ -1246,20 +1247,6 @@ mhn_list ()
static mu_list_t mhl_format;
void
cat_message (mu_stream_t out, mu_stream_t in)
{
int rc = 0;
char buf[512];
size_t n;
mu_stream_seek (in, 0, SEEK_SET, NULL);
while (rc == 0
&& mu_stream_read (in, buf, sizeof buf, &n) == 0
&& n > 0)
rc = mu_stream_write (out, buf, n, NULL);
}
int
show_internal (mu_message_t msg, msg_part_t part, char *encoding, mu_stream_t out)
{
......@@ -1274,15 +1261,14 @@ show_internal (mu_message_t msg, msg_part_t part, char *encoding, mu_stream_t ou
mu_strerror (rc));
return 0;
}
mu_body_get_stream (body, &bstr);
mu_body_get_streamref (body, &bstr);
rc = mu_filter_create (&dstr, bstr, encoding,
MU_FILTER_DECODE, MU_STREAM_READ | MU_STREAM_NO_CLOSE);
if (rc == 0)
bstr = dstr;
cat_message (out, bstr);
if (dstr)
mu_stream_destroy (&dstr);
return 0;
rc = mu_stream_copy (out, bstr, 0);
mu_stream_destroy (&bstr);
return rc;
}
int
......@@ -1965,14 +1951,15 @@ copy_header_to_stream (mu_message_t msg, mu_stream_t stream)
size_t bufsize = 0, n = 0;
mu_message_get_header (msg, &hdr);
mu_header_get_stream (hdr, &in);
mu_stream_seek (in, 0, SEEK_SET, NULL);
mu_header_get_streamref (hdr, &in);
/* FIXME: Use mu_stream_copy */
while (mu_stream_getline (in, &buf, &bufsize, &n) == 0 && n > 0)
{
if (n == 1 && buf[0] == '\n')
break;
mu_stream_write (stream, buf, n, NULL);
}
mu_stream_destroy (&in);
free (buf);
}
......@@ -2010,29 +1997,29 @@ finish_text_msg (struct compose_env *env, mu_message_t *msg, int ascii)
mu_message_create (&newmsg, NULL);
mu_message_get_header (newmsg, &hdr);
mu_header_get_stream (hdr, &output);
copy_header (*msg, hdr);
mu_header_set_value (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING,
"quoted-printable", 0);
mu_message_get_body (newmsg, &body);
mu_body_get_stream (body, &output);
mu_stream_seek (output, 0, SEEK_SET, NULL);
mu_body_get_streamref (body, &output);
mu_message_get_body (*msg, &body);
mu_body_get_stream (body, &input);
mu_body_get_streamref (body, &input);
rc = mu_filter_create (&fstr, input, "quoted-printable",
MU_FILTER_ENCODE,
MU_STREAM_READ | MU_STREAM_NO_CLOSE);
if (rc == 0)
{
cat_message (output, fstr);
mu_stream_copy (output, fstr, 0);
mu_stream_destroy (&fstr);
mu_message_unref (*msg);
*msg = newmsg;
}
else
mu_message_destroy (&newmsg, NULL);
mu_stream_destroy (&input);
mu_stream_destroy (&output);
}
finish_msg (env, msg);
}
......@@ -2076,18 +2063,19 @@ edit_extern (char *cmd, struct compose_env *env, mu_message_t *msg, int level)
return 1;
mu_message_get_body (*msg, &body);
mu_body_get_stream (body, &out);
mu_stream_seek (out, 0, SEEK_SET, NULL);
mu_body_get_streamref (body, &out);
if (!id)
id = mh_create_message_id (env->subpart);
mu_header_set_value (hdr2, MU_HEADER_CONTENT_ID, id, 1);
free (id);
mu_header_get_stream (hdr2, &in);
mu_stream_seek (in, 0, SEEK_SET, NULL);
cat_message (out, in);
mu_header_get_streamref (hdr2, &in);
mu_stream_copy (out, in, 0);
mu_stream_destroy (&in);
mu_stream_close (out);
mu_stream_destroy (&out);
mu_header_destroy (&hdr2);
finish_msg (env, msg);
......@@ -2312,11 +2300,11 @@ edit_mime (char *cmd, struct compose_env *env, mu_message_t *msg, int level)
}
mu_message_get_body (*msg, &body);
mu_body_get_stream (body, &out);
cat_message (out, fstr);
mu_body_get_streamref (body, &out);
mu_stream_copy (out, fstr, 0);
mu_stream_close (out);
mu_stream_destroy (&out);
mu_stream_destroy (&fstr);
finish_msg (env, msg);
return rc;
......@@ -2339,7 +2327,7 @@ mhn_edit (struct compose_env *env, int level)
char *buf = NULL;
size_t bufsize = 0, n;
mu_body_t body;
mu_stream_t output;
mu_stream_t output = NULL;
mu_message_t msg = NULL;
size_t line_count = 0;
int ascii_buf;
......@@ -2351,12 +2339,15 @@ mhn_edit (struct compose_env *env, int level)
if (!msg)
{
mu_header_t hdr;
/* Destroy old stream */
mu_stream_destroy (&output);
/* Create new message */
mu_message_create (&msg, NULL);
mu_message_get_header (msg, &hdr);
mu_message_get_body (msg, &body);
mu_body_get_stream (body, &output);
mu_stream_seek (output, 0, SEEK_SET, NULL);
mu_body_get_streamref (body, &output);
line_count = 0;
ascii_buf = 1; /* Suppose it is ascii */
env->subpart++;
......@@ -2391,6 +2382,8 @@ mhn_edit (struct compose_env *env, int level)
free (b2);
mu_stream_close (output);
mu_stream_destroy (&output);
if (line_count)
/* Close and append the previous part */
finish_text_msg (env, &msg, ascii_buf);
......@@ -2473,6 +2466,7 @@ mhn_edit (struct compose_env *env, int level)
else
mu_message_unref (msg);
}
mu_stream_destroy (&output);
return status;
}
......@@ -2621,14 +2615,14 @@ mhn_compose ()
mu_mime_create (&mime, NULL, 0);
mu_message_get_body (message, &body);
mu_body_get_stream (body, &stream);
mu_stream_seek (stream, 0, SEEK_SET, NULL);
mu_body_get_streamref (body, &stream);
env.mime = mime;
env.input = stream;
env.subpart = 0;
env.line = 0;
rc = mhn_edit (&env, 0);
mu_stream_destroy (&stream);
if (rc)
return rc;
......@@ -2671,8 +2665,9 @@ mhn_compose ()
mhn_header (message, msg);
copy_header_to_stream (message, stream);
mu_message_get_stream (msg, &in);
cat_message (stream, in);
mu_message_get_streamref (msg, &in);
mu_stream_copy (stream, in, 0);
mu_stream_destroy (&in);
mu_stream_destroy (&stream);
/* Preserve the backup copy and replace the draft */
......
......@@ -324,8 +324,7 @@ match_message (mu_message_t msg, regex_t *regex)
char buf[128];
size_t n;
mu_message_get_stream (msg, &str);
mu_stream_seek (str, 0, SEEK_SET, NULL);
mu_message_get_streamref (msg, &str);
while (mu_stream_readline (str, buf, sizeof buf, &n) == 0
&& n > 0)
{
......@@ -333,6 +332,7 @@ match_message (mu_message_t msg, regex_t *regex)
if (regexec (regex, buf, 0, NULL, 0) == 0)
return 1;
}
mu_stream_destroy (&str);
return 0;
}
......
......@@ -44,6 +44,7 @@ pop3d_retr (char *arg)
if (pop3d_is_deleted (attr))
return ERR_MESG_DELE;
/* FIXME: Use crlf filter + mu_stream_copy instead of the loop below */
mu_message_get_stream (msg, &stream);
pop3d_outf ("+OK\r\n");
......
......@@ -59,6 +59,7 @@ pop3d_top (char *arg)
/* Header. */
mu_message_get_header (msg, &hdr);
/* FIXME: Use crlf filter + mu_stream_copy instead of the below loop */
mu_header_get_stream (hdr, &stream);
n = 0;
while (mu_stream_readline (stream, buf, sizeof(buf), &n) == 0
......@@ -80,6 +81,7 @@ pop3d_top (char *arg)
int prev_nl = 1;
mu_message_get_body (msg, &body);
/* FIXME: Use the crlf filter + mu_stream_copy */
mu_body_get_stream (body, &stream);
n = 0;
while (mu_stream_readline (stream, buf, sizeof(buf), &n) == 0
......
......@@ -124,8 +124,7 @@ msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv,
mu_stream_t stream = NULL;
mu_mailbox_get_message (mbox, j, &msg);
mu_message_get_stream (msg, &stream);
mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
mu_message_get_streamref (msg, &stream);
while (mu_stream_readline (stream, buf, sizeof buf, &len) == 0
&& len > 0)
{
......
......@@ -211,14 +211,15 @@ print_header (mu_message_t message, int unix_header, int weedc, char **weedv)
size_t len = 0;
char buf[128];
mu_header_get_stream (header, &stream);
mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
/* FIXME: Use mu_stream_copy */
mu_header_get_streamref (header, &stream);
while (mu_stream_read (stream, buf, sizeof (buf) - 1, &len) == 0
&& len != 0)
{
buf[len] = '\0';
printf ("%s", buf);
}
mu_stream_destroy (&stream);
}
else
{
......@@ -264,7 +265,9 @@ print_body (mu_message_t message)
mu_stream_t stream = NULL;
size_t len = 0;
mu_message_get_body (message, &body);
mu_body_get_stream (body, &stream);
/* FIXME: Use mu_stream_copy */
mu_body_get_streamref (body, &stream);
while (mu_stream_read (stream, buf, sizeof (buf) - 1, &len) == 0
&& len != 0)
......@@ -272,6 +275,7 @@ print_body (mu_message_t message)
buf[len] = '\0';
printf ("%s", buf);
}
mu_stream_destroy (&stream);
}
int
......