Fix readmsg's printing the last message in the mailbox ($).
* readmsg/msglist.c (msglist): Bugfix.
Showing
2 changed files
with
32 additions
and
31 deletions
... | @@ -19,7 +19,6 @@ | ... | @@ -19,7 +19,6 @@ |
19 | 19 | ||
20 | #include "readmsg.h" | 20 | #include "readmsg.h" |
21 | 21 | ||
22 | |||
23 | static int | 22 | static int |
24 | addset (int **set, int *n, unsigned val) | 23 | addset (int **set, int *n, unsigned val) |
25 | { | 24 | { |
... | @@ -59,28 +58,33 @@ is_number (const char *s) | ... | @@ -59,28 +58,33 @@ is_number (const char *s) |
59 | /* | 58 | /* |
60 | According to ELM readmsg(1): | 59 | According to ELM readmsg(1): |
61 | 60 | ||
62 | 1. A lone ``*'' means select all messages in the mailbox. | 61 | 1. A lone ``*'' means select all messages in the mailbox. |
63 | 62 | ||
64 | 2. A list of message numbers may be specified. Values of ``0'' and ``$'' in the | 63 | 2. A list of message numbers may be specified. Values of ``0'' and |
65 | list both mean the last message in the mailbox. For example: | 64 | ``$'' in the list both mean the last message in the mailbox. For |
65 | example: | ||
66 | 66 | ||
67 | readmsg 1 3 0 | 67 | readmsg 1 3 0 |
68 | 68 | ||
69 | extracts three messages from the folder: the first, the third, and the last. | 69 | extracts three messages from the folder: the first, the third, and |
70 | the last. | ||
70 | 71 | ||
71 | 3. Finally, the selection may be some text to match. This will select a mail | 72 | 3. Finally, the selection may be some text to match. This will |
72 | message which exactly matches the specified text. For example, | 73 | select a mail message which exactly matches the specified text. For |
74 | example, | ||
73 | 75 | ||
74 | readmsg staff meeting | 76 | readmsg staff meeting |
75 | 77 | ||
76 | extracts the message which contains the words ``staff meeting.'' Note that it | 78 | extracts the message which contains the words ``staff meeting.'' |
77 | will not match a message containing ``Staff Meeting'' - the matching is case | 79 | Note that it will not match a message containing ``Staff Meeting'' - |
78 | sensitive. Normally only the first message which matches the pattern will be | 80 | the matching is case sensitive. Normally only the first message |
79 | printed. The -a option discussed in a moment changes this. | 81 | which matches the pattern will be printed. The -a option discussed |
82 | in a moment changes this. | ||
80 | */ | 83 | */ |
81 | 84 | ||
82 | int | 85 | int |
83 | msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, int **set, int *n) | 86 | msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, |
87 | int **set, int *n) | ||
84 | { | 88 | { |
85 | int i = 0; | 89 | int i = 0; |
86 | size_t total = 0; | 90 | size_t total = 0; |
... | @@ -89,7 +93,7 @@ msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, int **set, int | ... | @@ -89,7 +93,7 @@ msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, int **set, int |
89 | 93 | ||
90 | for (i = 0; i < argc; i++) | 94 | for (i = 0; i < argc; i++) |
91 | { | 95 | { |
92 | /* 1. A lone ``*'' means select all messages in the mailbox. */ | 96 | /* 1. A lone ``*'' means select all messages in the mailbox. */ |
93 | if (!strcmp (argv[i], "*")) | 97 | if (!strcmp (argv[i], "*")) |
94 | { | 98 | { |
95 | size_t j; | 99 | size_t j; |
... | @@ -98,18 +102,17 @@ msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, int **set, int | ... | @@ -98,18 +102,17 @@ msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, int **set, int |
98 | addset (set, n, j); | 102 | addset (set, n, j); |
99 | j = argc + 1; | 103 | j = argc + 1; |
100 | } | 104 | } |
101 | /* 2. A list of message numbers may be specified. Values of ``0'' and ``$'' in the | 105 | /* 2. A list of message numbers may be specified. Values of |
102 | list both mean the last message in the mailbox. */ | 106 | ``0'' and ``$'' in the list both mean the last message in the |
107 | mailbox. */ | ||
103 | else if (!strcmp (argv[i], "$") || !strcmp (argv[i], "0")) | 108 | else if (!strcmp (argv[i], "$") || !strcmp (argv[i], "0")) |
104 | { | 109 | { |
105 | size_t j; | 110 | addset (set, n, total); |
106 | mu_mailbox_messages_count (mbox, &total); | ||
107 | for (j = 1; j < total; j++) | ||
108 | addset (set, n, j); | ||
109 | } | 111 | } |
110 | /* 3. Finally, the selection may be some text to match. This will select a mail | 112 | /* 3. Finally, the selection may be some text to match. This |
111 | message which exactly matches the specified text. */ | 113 | will select a mail message which exactly matches the |
112 | else if (!is_number(argv[i])) | 114 | specified text. */ |
115 | else if (!is_number (argv[i])) | ||
113 | { | 116 | { |
114 | size_t j; | 117 | size_t j; |
115 | int found = 0; | 118 | int found = 0; |
... | @@ -117,12 +120,14 @@ msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, int **set, int | ... | @@ -117,12 +120,14 @@ msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, int **set, int |
117 | { | 120 | { |
118 | char buf[128]; | 121 | char buf[128]; |
119 | size_t len = 0; | 122 | size_t len = 0; |
120 | off_t offset = 0; | 123 | mu_off_t offset = 0; |
121 | mu_message_t msg = NULL; | 124 | mu_message_t msg = NULL; |
122 | mu_stream_t stream = NULL; | 125 | mu_stream_t stream = NULL; |
126 | |||
123 | mu_mailbox_get_message (mbox, j, &msg); | 127 | mu_mailbox_get_message (mbox, j, &msg); |
124 | mu_message_get_stream (msg, &stream); | 128 | mu_message_get_stream (msg, &stream); |
125 | while (mu_stream_readline (stream, buf, sizeof buf, offset, &len) == 0 && len > 0) | 129 | while (mu_stream_readline (stream, buf, sizeof buf, |
130 | offset, &len) == 0 && len > 0) | ||
126 | { | 131 | { |
127 | if (strstr (buf, argv[i]) != NULL) | 132 | if (strstr (buf, argv[i]) != NULL) |
128 | { | 133 | { |
... | @@ -132,6 +137,7 @@ msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, int **set, int | ... | @@ -132,6 +137,7 @@ msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, int **set, int |
132 | } | 137 | } |
133 | offset += len; | 138 | offset += len; |
134 | } | 139 | } |
140 | mu_stream_destroy (&stream, NULL); | ||
135 | if (found && !show_all) | 141 | if (found && !show_all) |
136 | break; | 142 | break; |
137 | } | 143 | } | ... | ... |
... | @@ -216,7 +216,7 @@ print_header (mu_message_t message, int unix_header, int weedc, char **weedv) | ... | @@ -216,7 +216,7 @@ print_header (mu_message_t message, int unix_header, int weedc, char **weedv) |
216 | while (mu_stream_read (stream, buf, sizeof (buf) - 1, offset, &len) == 0 | 216 | while (mu_stream_read (stream, buf, sizeof (buf) - 1, offset, &len) == 0 |
217 | && len != 0) | 217 | && len != 0) |
218 | { | 218 | { |
219 | buf[len] ='\0'; | 219 | buf[len] = '\0'; |
220 | printf ("%s", buf); | 220 | printf ("%s", buf); |
221 | offset += len; | 221 | offset += len; |
222 | } | 222 | } |
... | @@ -271,17 +271,12 @@ print_body (mu_message_t message) | ... | @@ -271,17 +271,12 @@ print_body (mu_message_t message) |
271 | while (mu_stream_read (stream, buf, sizeof (buf) - 1, offset, &len) == 0 | 271 | while (mu_stream_read (stream, buf, sizeof (buf) - 1, offset, &len) == 0 |
272 | && len != 0) | 272 | && len != 0) |
273 | { | 273 | { |
274 | buf[len] ='\0'; | 274 | buf[len] = '\0'; |
275 | printf ("%s", buf); | 275 | printf ("%s", buf); |
276 | offset += len; | 276 | offset += len; |
277 | } | 277 | } |
278 | } | 278 | } |
279 | 279 | ||
280 | /* This is still work in progress */ | ||
281 | /* FIXME: Parse options: See readmsg(1) part of elm: | ||
282 | readmsg 1 3 0 | ||
283 | extracts three messages from the folder: the first, the third, and | ||
284 | the last. */ | ||
285 | int | 280 | int |
286 | main (int argc, char **argv) | 281 | main (int argc, char **argv) |
287 | { | 282 | { | ... | ... |
-
Please register or sign in to post a comment