diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2010-12-07 13:45:34 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2010-12-07 13:45:34 +0200 |
commit | ac08a8575ae8ea3b85180bd05464a48768f398ee (patch) | |
tree | 2b62fe9bd48cef51152a7c85f84123e3b6d590c5 /comsat/comsat.c | |
parent | c2b4805128f2619235396726a116331606729e62 (diff) | |
download | mailutils-ac08a8575ae8ea3b85180bd05464a48768f398ee.tar.gz mailutils-ac08a8575ae8ea3b85180bd05464a48768f398ee.tar.bz2 |
comsat: Use mu streams instead of stdio
* comsat/action.c (action_beep, echo_string)
(action_echo, action_exec): Take mu_stream_t as first argument.
(open_rc): Take mu_stream_t as tty, return mu_stream_t as well.
Install the linecon filter.
(run_user_action): Take mu_stream_t as first arg. Pass it to all
invoked actions.
* comsat/comsat.c (get_newline_str): Remove.
(need_crlf): New function.
(notify_user): Use mu_stream_t instead of FILE.
Install a 7bit filter and CRLF filter (if the device requires it).
* comsat/comsat.h: Include mailutils/filter.h.
(run_user_action): Change signature.
Diffstat (limited to 'comsat/comsat.c')
-rw-r--r-- | comsat/comsat.c | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/comsat/comsat.c b/comsat/comsat.c index 8a22a3f45..faf18bef6 100644 --- a/comsat/comsat.c +++ b/comsat/comsat.c @@ -358,19 +358,22 @@ comsat_connection (int fd, struct sockaddr *sa, int salen, return 0; } -static const char * -get_newline_str (FILE *fp) +static int +need_crlf (mu_stream_t str) { #if defined(OPOST) && defined(ONLCR) + mu_transport_t trans[2]; struct termios tbuf; - tcgetattr (fileno (fp), &tbuf); - if ((tbuf.c_oflag & OPOST) && (tbuf.c_oflag & ONLCR)) - return "\n"; + if (mu_stream_ioctl (str, MU_IOCTL_TRANSPORT, MU_IOCTL_OP_GET, trans)) + return 1; /* suppose we do need it */ + if (tcgetattr ((int)trans[0], &tbuf) == 0 && + (tbuf.c_oflag & OPOST) && (tbuf.c_oflag & ONLCR)) + return 0; else - return "\r\n"; + return 1; #else - return "\r\n"; /* Just in case */ + return 1; /* Just in case */ #endif } @@ -380,21 +383,43 @@ static void notify_user (const char *user, const char *device, const char *path, mu_message_qid_t qid) { - FILE *fp; - const char *cr; + mu_stream_t out, dev; mu_mailbox_t mbox = NULL; mu_message_t msg; int status; if (change_user (user)) return; - if ((fp = fopen (device, "w")) == NULL) + status = mu_file_stream_create (&dev, device, MU_STREAM_WRITE); + if (status) { - mu_error (_("cannot open device %s: %s"), device, mu_strerror (errno)); + mu_error (_("cannot open device %s: %s"), device, mu_strerror (status)); return; } - - cr = get_newline_str (fp); + mu_stream_set_buffer (dev, mu_buffer_line, 0); + + status = mu_filter_create (&out, dev, "7bit", MU_FILTER_ENCODE, + MU_STREAM_WRITE); + mu_stream_unref (dev); + if (status) + { + mu_error (_("cannot create 7bit filter: %s"), mu_strerror (status)); + return; + } + + if (need_crlf (out)) + { + mu_stream_t str; + status = mu_filter_create (&str, out, "CRLF", MU_FILTER_ENCODE, + MU_STREAM_WRITE); + mu_stream_unref (out); + if (status) + { + mu_error (_("cannot create crlf filter: %s"), mu_strerror (status)); + return; + } + out = str; + } if (!path) { @@ -403,8 +428,9 @@ notify_user (const char *user, const char *device, const char *path, return; } - if ((status = mu_mailbox_create (&mbox, path)) != 0 - || (status = mu_mailbox_open (mbox, MU_STREAM_READ|MU_STREAM_QACCESS)) != 0) + if ((status = mu_mailbox_create (&mbox, path)) != 0 || + (status = mu_mailbox_open (mbox, + MU_STREAM_READ|MU_STREAM_QACCESS)) != 0) { mu_error (_("cannot open mailbox %s: %s"), path, mu_strerror (status)); @@ -419,8 +445,8 @@ notify_user (const char *user, const char *device, const char *path, return; /* FIXME: Notify the user, anyway */ } - run_user_action (fp, cr, msg); - fclose (fp); + run_user_action (out, msg); + mu_stream_destroy (&out); } /* Search utmp for the local user */ |