Commit da9c204e da9c204ef4ff9b89863a15bd3346331acc7b5679 by Sergey Poznyakoff

comsat: minor changes

* libmailutils/mailbox/mailbox.c (mu_mailbox_open): Fix error
checking condition.
* comsat/action.c (act_getline): Remove.
(open_rc): Return mu_stream_t. Use linecon filter.
1 parent 0138b981
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
17 17
18 #include "comsat.h" 18 #include "comsat.h"
19 #include <mailutils/io.h> 19 #include <mailutils/io.h>
20 #include <mailutils/filter.h>
20 #define obstack_chunk_alloc malloc 21 #define obstack_chunk_alloc malloc
21 #define obstack_chunk_free free 22 #define obstack_chunk_free free
22 #include <obstack.h> 23 #include <obstack.h>
...@@ -39,52 +40,6 @@ ...@@ -39,52 +40,6 @@
39 number of characters and lines in the expansion. 40 number of characters and lines in the expansion.
40 When omitted, they default to 400, 5. */ 41 When omitted, they default to 400, 5. */
41 42
42 static unsigned
43 act_getline (FILE *fp, char **sptr, size_t *size)
44 {
45 char buf[256];
46 int cont = 1;
47 size_t used = 0;
48 unsigned lines = 0;
49
50 if (feof (fp))
51 return 0;
52
53 while (cont && fgets (buf, sizeof buf, fp))
54 {
55 int len = strlen (buf);
56 if (buf[len-1] == '\n')
57 {
58 lines++;
59 buf[--len] = 0;
60 if (buf[len-1] == '\\')
61 {
62 buf[--len] = 0;
63 cont = 1;
64 }
65 else
66 cont = 0;
67 }
68 else
69 cont = 1;
70
71 if (len + used + 1 > *size)
72 {
73 *sptr = realloc (*sptr, len + used + 1);
74 if (!*sptr)
75 return 0;
76 *size = len + used + 1;
77 }
78 memcpy (*sptr + used, buf, len);
79 used += len;
80 }
81
82 if (*sptr)
83 (*sptr)[used] = 0;
84
85 return lines;
86 }
87
88 static int 43 static int
89 expand_escape (char **pp, mu_message_t msg, struct obstack *stk) 44 expand_escape (char **pp, mu_message_t msg, struct obstack *stk)
90 { 45 {
...@@ -331,11 +286,13 @@ action_exec (FILE *tty, int argc, char **argv) ...@@ -331,11 +286,13 @@ action_exec (FILE *tty, int argc, char **argv)
331 } 286 }
332 } 287 }
333 288
334 static FILE * 289 static mu_stream_t
335 open_rc (const char *filename, FILE *tty) 290 open_rc (const char *filename, FILE *tty)
336 { 291 {
337 struct stat stb; 292 struct stat stb;
338 struct passwd *pw = getpwnam (username); 293 struct passwd *pw = getpwnam (username);
294 mu_stream_t stream, input;
295 int rc;
339 296
340 /* To be on the safe side, we do not allow root to have his .biffrc */ 297 /* To be on the safe side, we do not allow root to have his .biffrc */
341 if (!allow_biffrc || pw->pw_uid == 0) 298 if (!allow_biffrc || pw->pw_uid == 0)
...@@ -357,21 +314,45 @@ open_rc (const char *filename, FILE *tty) ...@@ -357,21 +314,45 @@ open_rc (const char *filename, FILE *tty)
357 return NULL; 314 return NULL;
358 } 315 }
359 } 316 }
360 return fopen (filename, "r"); 317 rc = mu_file_stream_create (&input, filename, MU_STREAM_READ);
318 if (rc)
319 {
320 if (rc != ENOENT)
321 {
322 fprintf (tty, _("Cannot open .biffrc file: %s\r\n"),
323 mu_strerror (rc));
324 mu_diag_output (MU_DIAG_NOTICE, _("cannot open %s for %s: %s"),
325 filename, username, mu_strerror (rc));
326 }
327 return NULL;
328 }
329 rc = mu_filter_create (&stream, input, "LINECON", MU_FILTER_DECODE,
330 MU_STREAM_READ);
331 mu_stream_unref (input);
332 if (rc)
333 {
334 fprintf (tty, _("Cannot create filter for your .biffrc file: %s\r\n"),
335 mu_strerror (rc));
336 mu_diag_output (MU_DIAG_NOTICE,
337 _("cannot create filter for file %s of %s: %s"),
338 filename, username, mu_strerror (rc));
339 return NULL;
340 }
341 return stream;
361 } 342 }
362 343
363 void 344 void
364 run_user_action (FILE *tty, const char *cr, mu_message_t msg) 345 run_user_action (FILE *tty, const char *cr, mu_message_t msg)
365 { 346 {
366 FILE *fp; 347 mu_stream_t input;
367 int nact = 0; 348 int nact = 0;
368 char *stmt = NULL;
369 size_t size = 0;
370 349
371 fp = open_rc (BIFF_RC, tty); 350 input = open_rc (BIFF_RC, tty);
372 if (fp) 351 if (input)
373 { 352 {
374 unsigned n; 353 char *stmt = NULL;
354 size_t size = 0;
355 size_t n;
375 char *cwd = mu_getcwd (); 356 char *cwd = mu_getcwd ();
376 char *rcname; 357 char *rcname;
377 struct mu_locus locus; 358 struct mu_locus locus;
...@@ -381,19 +362,19 @@ run_user_action (FILE *tty, const char *cr, mu_message_t msg) ...@@ -381,19 +362,19 @@ run_user_action (FILE *tty, const char *cr, mu_message_t msg)
381 if (!rcname) 362 if (!rcname)
382 { 363 {
383 mu_diag_funcall (MU_DIAG_ERROR, "mu_make_file_name", NULL, ENOMEM); 364 mu_diag_funcall (MU_DIAG_ERROR, "mu_make_file_name", NULL, ENOMEM);
384 fclose (fp); 365 locus.mu_file = BIFF_RC;
385 return;
386 } 366 }
387 367 else
388 locus.mu_file = rcname; 368 locus.mu_file = rcname;
389 locus.mu_line = 1; 369 locus.mu_line = 1;
390 locus.mu_col = 0; 370 locus.mu_col = 0;
391 while ((n = act_getline (fp, &stmt, &size))) 371 while (mu_stream_getline (input, &stmt, &size, &n) == 0 && n > 0)
392 { 372 {
393 struct mu_wordsplit ws; 373 struct mu_wordsplit ws;
394 374
395 ws.ws_comment = "#"; 375 ws.ws_comment = "#";
396 if (mu_wordsplit (stmt, &ws, MU_WRDSF_DEFFLAGS | MU_WRDSF_COMMENT) 376 if (mu_wordsplit (stmt, &ws,
377 MU_WRDSF_DEFFLAGS | MU_WRDSF_COMMENT) == 0
397 && ws.ws_wordc) 378 && ws.ws_wordc)
398 { 379 {
399 mu_stream_ioctl (mu_strerr, MU_IOCTL_LOGSTREAM, 380 mu_stream_ioctl (mu_strerr, MU_IOCTL_LOGSTREAM,
...@@ -443,9 +424,9 @@ run_user_action (FILE *tty, const char *cr, mu_message_t msg) ...@@ -443,9 +424,9 @@ run_user_action (FILE *tty, const char *cr, mu_message_t msg)
443 } 424 }
444 } 425 }
445 mu_wordsplit_free (&ws); 426 mu_wordsplit_free (&ws);
446 locus.mu_line += n; 427 locus.mu_line++;
447 } 428 }
448 fclose (fp); 429 mu_stream_destroy (&input);
449 mu_stream_ioctl (mu_strerr, MU_IOCTL_LOGSTREAM, 430 mu_stream_ioctl (mu_strerr, MU_IOCTL_LOGSTREAM,
450 MU_IOCTL_LOGSTREAM_SET_LOCUS, NULL); 431 MU_IOCTL_LOGSTREAM_SET_LOCUS, NULL);
451 free (rcname); 432 free (rcname);
......
...@@ -278,7 +278,7 @@ mu_mailbox_open (mu_mailbox_t mbox, int flag) ...@@ -278,7 +278,7 @@ mu_mailbox_open (mu_mailbox_t mbox, int flag)
278 if (flag & MU_STREAM_QACCESS) 278 if (flag & MU_STREAM_QACCESS)
279 { 279 {
280 /* Quick access mailboxes are read-only */ 280 /* Quick access mailboxes are read-only */
281 if (flag & (MU_STREAM_WRITE | MU_STREAM_RDWR 281 if (flag & (MU_STREAM_WRITE
282 | MU_STREAM_APPEND | MU_STREAM_CREAT)) 282 | MU_STREAM_APPEND | MU_STREAM_CREAT))
283 return EINVAL; /* FIXME: Better error code, please? */ 283 return EINVAL; /* FIXME: Better error code, please? */
284 } 284 }
......