Commit e781ed92 e781ed92ae27da6e5d400bfa3b4655ebfc849a6c by Sergey Poznyakoff

Use mh_iterate(). Accept message specifications from the command line.

1 parent d2572413
Showing 1 changed file with 32 additions and 23 deletions
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
27 27
28 const char *argp_program_version = "scan (" PACKAGE_STRING ")"; 28 const char *argp_program_version = "scan (" PACKAGE_STRING ")";
29 static char doc[] = "GNU MH scan"; 29 static char doc[] = "GNU MH scan";
30 static char args_doc[] = ""; 30 static char args_doc[] = "[+folder] [msgs]";
31 31
32 /* GNU options */ 32 /* GNU options */
33 static struct argp_option options[] = { 33 static struct argp_option options[] = {
...@@ -68,8 +68,10 @@ static int header; ...@@ -68,8 +68,10 @@ static int header;
68 68
69 static mh_format_t format; 69 static mh_format_t format;
70 70
71 void list_message (mailbox_t mbox, size_t msgno, char *bp, size_t width); 71 static mh_msgset_t msgset;
72 int scan (mailbox_t mbox); 72
73 void list_message __P((mailbox_t mbox, message_t msg, size_t num, void *data));
74 int scan __P((mailbox_t mbox));
73 75
74 static int 76 static int
75 opt_handler (int key, char *arg, void *unused) 77 opt_handler (int key, char *arg, void *unused)
...@@ -128,8 +130,11 @@ opt_handler (int key, char *arg, void *unused) ...@@ -128,8 +130,11 @@ opt_handler (int key, char *arg, void *unused)
128 int 130 int
129 main (int argc, char **argv) 131 main (int argc, char **argv)
130 { 132 {
133 int index;
134 mailbox_t mbox;
135
131 mh_argp_parse (argc, argv, options, mh_option, args_doc, doc, 136 mh_argp_parse (argc, argv, options, mh_option, args_doc, doc,
132 opt_handler, NULL, NULL); 137 opt_handler, NULL, &index);
133 138
134 if (mh_format_parse (format_str, &format)) 139 if (mh_format_parse (format_str, &format))
135 { 140 {
...@@ -137,7 +142,13 @@ main (int argc, char **argv) ...@@ -137,7 +142,13 @@ main (int argc, char **argv)
137 exit (1); 142 exit (1);
138 } 143 }
139 144
140 return scan (mh_open_folder (current_folder, 0)); 145 mbox = mh_open_folder (current_folder, 0);
146 mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "all");
147
148 if (reverse)
149 mh_msgset_reverse (&msgset);
150
151 return scan (mbox);
141 } 152 }
142 153
143 #ifdef HAVE_TERMCAP_H 154 #ifdef HAVE_TERMCAP_H
...@@ -181,13 +192,18 @@ clear_screen () ...@@ -181,13 +192,18 @@ clear_screen ()
181 } 192 }
182 } 193 }
183 194
195 struct list_data {
196 char *buffer;
197 size_t width;
198 };
199
184 int 200 int
185 scan (mailbox_t mbox) 201 scan (mailbox_t mbox)
186 { 202 {
187 size_t i, total; 203 struct list_data list_data;
188 char *buffer;
189 204
190 buffer = xmalloc (width); 205 list_data.buffer = xmalloc (width);
206 list_data.width = width;
191 207
192 if (header) 208 if (header)
193 { 209 {
...@@ -200,27 +216,20 @@ scan (mailbox_t mbox) ...@@ -200,27 +216,20 @@ scan (mailbox_t mbox)
200 strftime (datestr, sizeof datestr, "%c", localtime (&t)); 216 strftime (datestr, sizeof datestr, "%c", localtime (&t));
201 printf ("Folder %s %s\n", url_to_string (url), datestr); 217 printf ("Folder %s %s\n", url_to_string (url), datestr);
202 } 218 }
203
204 mailbox_messages_count (mbox, &total);
205 219
206 if (reverse) 220 mh_iterate (mbox, &msgset, list_message, &list_data);
207 for (i = total; i >= 1; i--) 221
208 list_message (mbox, i, buffer, width);
209 else
210 for (i = 1; i <= total; i++)
211 list_message (mbox, i, buffer, width);
212 clear_screen (); 222 clear_screen ();
213 mh_global_save_state (); 223 mh_global_save_state ();
214 return 0; 224 return 0;
215 } 225 }
216 226
217 void 227 void
218 list_message (mailbox_t mbox, size_t msgno, char *buffer, size_t width) 228 list_message (mailbox_t mbox, message_t msg, size_t num, void *data)
219 { 229 {
220 message_t msg; 230 struct list_data *ld = data;
221 231
222 buffer[0] = 0; 232 ld->buffer[0] = 0;
223 mailbox_get_message (mbox, msgno, &msg); 233 mh_format (&format, msg, num, ld->buffer, ld->width);
224 mh_format (&format, msg, msgno, buffer, width); 234 printf ("%s\n", ld->buffer);
225 printf ("%s\n", buffer);
226 } 235 }
......