diff options
Diffstat (limited to 'comsat/action.c')
-rw-r--r-- | comsat/action.c | 156 |
1 files changed, 83 insertions, 73 deletions
diff --git a/comsat/action.c b/comsat/action.c index c9703f28e..b6799c481 100644 --- a/comsat/action.c +++ b/comsat/action.c | |||
@@ -173,98 +173,114 @@ expand_line (const char *str, mu_message_t msg) | |||
173 | } | 173 | } |
174 | 174 | ||
175 | const char *default_action = | 175 | const char *default_action = |
176 | #include "biff.rc.h" | 176 | #include "biff.rc.h" |
177 | ; | 177 | ; |
178 | 178 | ||
179 | /* Examine the tty to determine which filters to apply when printing | ||
180 | to it. On entry, STR is the opened stream, FLT points to an array | ||
181 | of char* with at least 3 slots, and NFLT to an integer number. | ||
182 | On success, populates FLT with the necessary filter chain, and stores | ||
183 | to *NFLT the number of used slots. On error, issues error message and | ||
184 | returns -1. | ||
185 | FLT and NFLT can be used as input to mu_filter_chain_create. | ||
186 | */ | ||
179 | static int | 187 | static int |
180 | need_crlf (mu_stream_t str) | 188 | study_tty (mu_stream_t str, char *flt[], int *nflt) |
181 | { | 189 | { |
182 | #if defined(OPOST) && defined(ONLCR) | ||
183 | mu_transport_t trans[2]; | 190 | mu_transport_t trans[2]; |
184 | struct termios tbuf; | 191 | int fd; |
192 | struct stat st; | ||
193 | int rc; | ||
185 | 194 | ||
186 | if (mu_stream_ioctl (str, MU_IOCTL_TRANSPORT, MU_IOCTL_OP_GET, trans)) | 195 | rc = mu_stream_ioctl (str, MU_IOCTL_TRANSPORT, MU_IOCTL_OP_GET, trans); |
187 | return 1; /* suppose we do need it */ | 196 | if (rc) |
188 | if (tcgetattr ((int) (intptr_t) trans[0], &tbuf) == 0 && | 197 | { |
189 | (tbuf.c_oflag & OPOST) && (tbuf.c_oflag & ONLCR)) | 198 | mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_ioctl", NULL, rc); |
190 | return 0; | 199 | return rc; |
191 | else | 200 | } |
192 | return 1; | 201 | |
202 | *nflt = 0; | ||
203 | fd = (int) (intptr_t) trans[0]; | ||
204 | if (fstat (fd, &st) == 0) | ||
205 | { | ||
206 | switch (st.st_mode & S_IFMT) | ||
207 | { | ||
208 | case S_IFREG: | ||
209 | return 0; | ||
210 | |||
211 | case S_IFCHR: | ||
212 | flt[(*nflt)++] = "7BIT"; | ||
213 | #if defined(OPOST) && defined(ONLCR) | ||
214 | { | ||
215 | struct termios tbuf; | ||
216 | |||
217 | if (!(tcgetattr (fd, &tbuf) == 0 | ||
218 | && (tbuf.c_oflag & OPOST) && (tbuf.c_oflag & ONLCR))) | ||
219 | { | ||
220 | flt[(*nflt)++] = "+"; | ||
221 | flt[(*nflt)++] = "CRLF"; | ||
222 | } | ||
223 | } | ||
193 | #else | 224 | #else |
194 | return 1; /* Just in case */ | 225 | /* Just in case */ |
226 | flt[(*nflt)++] = "+"; | ||
227 | flt[(*nflt)++] = "CRLF"; | ||
195 | #endif | 228 | #endif |
229 | break; | ||
230 | |||
231 | case S_IFSOCK: | ||
232 | return 0; | ||
233 | |||
234 | default: | ||
235 | /* FIXME: Perhaps an error? */ | ||
236 | return 0; | ||
237 | } | ||
238 | } | ||
239 | |||
240 | return 0; | ||
196 | } | 241 | } |
197 | 242 | ||
198 | static mu_stream_t | 243 | static mu_stream_t |
199 | _open_tty (const char *device, int argc, char **argv) | 244 | _open_tty (const char *device, int argc, char **argv) |
200 | { | 245 | { |
201 | mu_stream_t dev, base_dev, prev_stream; | 246 | mu_stream_t dev; |
202 | int status; | 247 | int status; |
248 | char *dfl_argv[4]; | ||
203 | 249 | ||
204 | status = mu_file_stream_create (&dev, device, MU_STREAM_WRITE); | 250 | status = mu_file_stream_create (&dev, device, MU_STREAM_APPEND|MU_STREAM_CREAT); |
205 | if (status) | 251 | if (status) |
206 | { | 252 | { |
207 | mu_error (_("cannot open device %s: %s"), | 253 | mu_error (_("cannot open device %s: %s"), |
208 | device, mu_strerror (status)); | 254 | device, mu_strerror (status)); |
209 | return NULL; | 255 | return NULL; |
210 | } | 256 | } |
211 | mu_stream_set_buffer (dev, mu_buffer_line, 0); | 257 | mu_stream_set_buffer (dev, mu_buffer_line, 0); |
212 | 258 | ||
213 | prev_stream = base_dev = dev; | 259 | if (argc == 0) |
214 | while (argc) | ||
215 | { | 260 | { |
216 | int i; | 261 | status = study_tty (dev, dfl_argv, &argc); |
217 | int mode; | 262 | if (status) |
218 | int qmark; | 263 | return NULL; |
219 | char *fltname; | 264 | argv = dfl_argv; |
220 | 265 | } | |
221 | fltname = argv[0]; | 266 | |
222 | if (fltname[0] == '?') | 267 | if (argc) |
223 | { | 268 | { |
224 | qmark = 1; | 269 | mu_stream_t str; |
225 | fltname++; | 270 | status = mu_filter_chain_create (&str, dev, |
226 | } | 271 | MU_FILTER_ENCODE, MU_STREAM_WRITE, |
227 | else | 272 | argc, argv); |
228 | qmark = 0; | 273 | mu_stream_unref (dev); |
229 | 274 | if (status) | |
230 | if (fltname[0] == '~') | ||
231 | { | ||
232 | mode = MU_FILTER_DECODE; | ||
233 | fltname++; | ||
234 | } | ||
235 | else | ||
236 | { | ||
237 | mode = MU_FILTER_ENCODE; | ||
238 | } | ||
239 | |||
240 | for (i = 1; i < argc; i++) | ||
241 | if (strcmp (argv[i], "+") == 0) | ||
242 | break; | ||
243 | |||
244 | if (qmark == 0 || need_crlf (base_dev)) | ||
245 | { | ||
246 | status = mu_filter_create_args (&dev, prev_stream, fltname, | ||
247 | i, (const char **)argv, | ||
248 | mode, MU_STREAM_WRITE); | ||
249 | mu_stream_unref (prev_stream); | ||
250 | if (status) | ||
251 | { | ||
252 | mu_error (_("cannot open filter stream: %s"), | ||
253 | mu_strerror (status)); | ||
254 | return NULL; | ||
255 | } | ||
256 | prev_stream = dev; | ||
257 | } | ||
258 | argc -= i; | ||
259 | argv += i; | ||
260 | if (argc) | ||
261 | { | 275 | { |
262 | argc--; | 276 | mu_diag_funcall (MU_DIAG_ERROR, "mu_filter_chain_create", device, |
263 | argv++; | 277 | status); |
278 | return NULL; | ||
264 | } | 279 | } |
280 | dev = str; | ||
265 | } | 281 | } |
266 | return dev; | 282 | return dev; |
267 | } | 283 | } |
268 | 284 | ||
269 | mu_stream_t | 285 | mu_stream_t |
270 | open_tty (const char *device, int argc, char **argv) | 286 | open_tty (const char *device, int argc, char **argv) |
@@ -272,27 +288,21 @@ open_tty (const char *device, int argc, char **argv) | |||
272 | mu_stream_t dev; | 288 | mu_stream_t dev; |
273 | 289 | ||
274 | if (!device || !*device || strcmp (device, "null") == 0) | 290 | if (!device || !*device || strcmp (device, "null") == 0) |
275 | { | 291 | { |
276 | int rc = mu_nullstream_create (&dev, MU_STREAM_WRITE); | 292 | int rc = mu_nullstream_create (&dev, MU_STREAM_WRITE); |
277 | if (rc) | 293 | if (rc) |
278 | mu_error (_("cannot open null stream: %s"), mu_strerror (rc)); | 294 | { |
295 | mu_error (_("cannot open null stream: %s"), mu_strerror (rc)); | ||
296 | dev = NULL; | ||
297 | } | ||
279 | } | 298 | } |
280 | else | 299 | else |
281 | dev = _open_tty (device, argc, argv); | 300 | dev = _open_tty (device, argc, argv); |
282 | return dev; | 301 | return dev; |
283 | } | 302 | } |
284 | |||
285 | static mu_stream_t | ||
286 | open_default_tty (const char *device) | ||
287 | { | ||
288 | static char *default_filters[] = { "7bit", "+", "?CRLF", NULL }; | ||
289 | return open_tty (device, MU_ARRAY_SIZE (default_filters) - 1, | ||
290 | default_filters); | ||
291 | } | ||
292 | |||
293 | 303 | ||
294 | struct biffrc_environ | 304 | struct biffrc_environ |
295 | { | 305 | { |
296 | mu_stream_t tty; | 306 | mu_stream_t tty; |
297 | mu_stream_t logstr; | 307 | mu_stream_t logstr; |
298 | mu_message_t msg; | 308 | mu_message_t msg; |
@@ -608,13 +618,13 @@ void | |||
608 | run_user_action (const char *device, mu_message_t msg) | 618 | run_user_action (const char *device, mu_message_t msg) |
609 | { | 619 | { |
610 | int rc, mode; | 620 | int rc, mode; |
611 | mu_stream_t stream; | 621 | mu_stream_t stream; |
612 | struct biffrc_environ env; | 622 | struct biffrc_environ env; |
613 | 623 | ||
614 | env.tty = open_default_tty (device); | 624 | env.tty = open_tty (device, 0, NULL); |
615 | if (!env.tty) | 625 | if (!env.tty) |
616 | return; | 626 | return; |
617 | env.msg = msg; | 627 | env.msg = msg; |
618 | 628 | ||
619 | env.errbuf = NULL; | 629 | env.errbuf = NULL; |
620 | env.errsize = 0; | 630 | env.errsize = 0; |