Use common read-eval loop
Showing
1 changed file
with
39 additions
and
19 deletions
... | @@ -17,6 +17,25 @@ | ... | @@ -17,6 +17,25 @@ |
17 | 17 | ||
18 | #include "mail.h" | 18 | #include "mail.h" |
19 | 19 | ||
20 | |||
21 | static char * | ||
22 | source_readline(void *closure, int cont) | ||
23 | { | ||
24 | FILE *fp = closure; | ||
25 | size_t s = 0; | ||
26 | char *buf = NULL; | ||
27 | |||
28 | (void)cont; /*unused*/ | ||
29 | if (getline (&buf, &s, fp) >= 0) | ||
30 | { | ||
31 | int len = strlen (buf); | ||
32 | if (buf[len-1] == '\n') | ||
33 | buf[len-1] = '\0'; | ||
34 | return buf; | ||
35 | } | ||
36 | return NULL; | ||
37 | } | ||
38 | |||
20 | /* | 39 | /* |
21 | * so[urce] file | 40 | * so[urce] file |
22 | */ | 41 | */ |
... | @@ -24,25 +43,26 @@ | ... | @@ -24,25 +43,26 @@ |
24 | int | 43 | int |
25 | mail_source (int argc, char **argv) | 44 | mail_source (int argc, char **argv) |
26 | { | 45 | { |
27 | if (argc == 2) | 46 | FILE *fp; |
47 | int save_term; | ||
48 | |||
49 | if (argc != 2) | ||
50 | { | ||
51 | fprintf(ofile, "source requires an argument\n"); | ||
52 | return 1; | ||
53 | } | ||
54 | |||
55 | fp = fopen (argv[1], "r"); | ||
56 | if (!fp) | ||
28 | { | 57 | { |
29 | FILE *rc = fopen (argv[1], "r"); | 58 | fprintf(ofile, "can't open `%s': %s\n", argv[1], strerror(errno)); |
30 | if (rc != NULL) | 59 | return 1; |
31 | { | ||
32 | char *buf = NULL; | ||
33 | size_t s = 0; | ||
34 | while (getline (&buf, &s, rc) >= 0) | ||
35 | { | ||
36 | int len = strlen (buf); | ||
37 | if (buf[len-1] == '\n') | ||
38 | buf[len-1] = '\0'; | ||
39 | util_do_command("%s", buf); | ||
40 | free (buf); | ||
41 | buf = NULL; | ||
42 | } | ||
43 | fclose (rc); | ||
44 | return 0; | ||
45 | } | ||
46 | } | 60 | } |
47 | return 1; | 61 | |
62 | save_term = mail_is_terminal(); | ||
63 | mail_set_is_terminal(0); | ||
64 | mail_mainloop(source_readline, fp, 0); | ||
65 | mail_set_is_terminal(save_term); | ||
66 | fclose (fp); | ||
67 | return 0; | ||
48 | } | 68 | } | ... | ... |
-
Please register or sign in to post a comment