Commit c0b47540 c0b47540fa1c2ffaebecf742089cc3cad214d6a5 by Alain Magloire

* frm/frm.c : Forget to add --summary option and fix return status.

When being very quiet just freopen(/dev/null, w, stdout).
1 parent 67dee9aa
Showing 1 changed file with 45 additions and 19 deletions
...@@ -42,13 +42,14 @@ static struct option long_options[] = ...@@ -42,13 +42,14 @@ static struct option long_options[] =
42 {"number", no_argument, 0, 'n'}, 42 {"number", no_argument, 0, 'n'},
43 {"Quiet", no_argument, 0, 'Q'}, 43 {"Quiet", no_argument, 0, 'Q'},
44 {"query", no_argument, 0, 'q'}, 44 {"query", no_argument, 0, 'q'},
45 {"summary", no_argument, 0, 'S'},
45 {"status", required_argument, 0, 's'}, 46 {"status", required_argument, 0, 's'},
46 {"align", no_argument, 0, 't'}, 47 {"align", no_argument, 0, 't'},
47 {"version", no_argument, 0, 'v'}, 48 {"version", no_argument, 0, 'v'},
48 {0, 0, 0, 0} 49 {0, 0, 0, 0}
49 }; 50 };
50 51
51 const char *short_options ="hlnQqs:tv"; 52 const char *short_options ="hlnQqSs:tv";
52 53
53 static int show_to; 54 static int show_to;
54 static int show_from = 1; 55 static int show_from = 1;
...@@ -65,8 +66,7 @@ static int have_new_mail; ...@@ -65,8 +66,7 @@ static int have_new_mail;
65 #define IS_OLD 0x010 66 #define IS_OLD 0x010
66 #define IS_NEW 0x100 67 #define IS_NEW 0x100
67 static int select_attribute; 68 static int select_attribute;
68 69 static int selected;
69 static int counter;
70 70
71 71
72 /* Retrieve the Personal Name from the header To: or From: */ 72 /* Retrieve the Personal Name from the header To: or From: */
...@@ -98,6 +98,8 @@ get_personal (header_t hdr, char *field, char *personal, size_t buflen) ...@@ -98,6 +98,8 @@ get_personal (header_t hdr, char *field, char *personal, size_t buflen)
98 static int 98 static int
99 action (observer_t o, size_t type) 99 action (observer_t o, size_t type)
100 { 100 {
101 static int counter;
102
101 switch (type) 103 switch (type)
102 { 104 {
103 case MU_EVT_MESSAGE_ADD: 105 case MU_EVT_MESSAGE_ADD:
...@@ -137,9 +139,8 @@ action (observer_t o, size_t type) ...@@ -137,9 +139,8 @@ action (observer_t o, size_t type)
137 if (attribute_is_recent (attr)) 139 if (attribute_is_recent (attr))
138 have_new_mail = 1; 140 have_new_mail = 1;
139 141
140 if (be_quiet) 142 if (select_attribute)
141 break; 143 selected = 1;
142
143 144
144 if (show_number) 145 if (show_number)
145 printf ("%d: ", counter); 146 printf ("%d: ", counter);
...@@ -192,13 +193,14 @@ usage (const char *argv) ...@@ -192,13 +193,14 @@ usage (const char *argv)
192 printf (" -n, --number display the message numberd\n"); 193 printf (" -n, --number display the message numberd\n");
193 printf (" -Q, --Quiet very quiet\n"); 194 printf (" -Q, --Quiet very quiet\n");
194 printf (" -q, --query print a message if unread mail\n"); 195 printf (" -q, --query print a message if unread mail\n");
196 printf (" -S, --summary print a summary of messages\n");
195 printf (" -s, --status=[nor] select message with the specific \ 197 printf (" -s, --status=[nor] select message with the specific \
196 attribute\n"); 198 attribute\n");
197 printf (" [n]ew, [r]ead, [u]nread.\n"); 199 printf (" [n]ew, [r]ead, [u]nread.\n");
198 printf (" -t, --align Try to align\n"); 200 printf (" -t, --align Try to align\n");
199 printf (" -v, --version display version information and exit\n"); 201 printf (" -v, --version display version information and exit\n");
200 printf ("\nReport bugs to bug-mailutils@gnu.org\n"); 202 printf ("\nReport bugs to bug-mailutils@gnu.org\n");
201 exit (0); 203 exit (3);
202 } 204 }
203 205
204 /* This is a clone of the elm program call "frm". It is a good example on 206 /* This is a clone of the elm program call "frm". It is a good example on
...@@ -212,7 +214,9 @@ int ...@@ -212,7 +214,9 @@ int
212 main(int argc, char **argv) 214 main(int argc, char **argv)
213 { 215 {
214 char *mailbox_name = NULL; 216 char *mailbox_name = NULL;
217 size_t total = 0;
215 int c; 218 int c;
219 int status = 0;
216 220
217 while ((c = getopt_long (argc, argv, short_options, long_options, NULL)) 221 while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
218 != -1) 222 != -1)
...@@ -234,6 +238,11 @@ main(int argc, char **argv) ...@@ -234,6 +238,11 @@ main(int argc, char **argv)
234 case 'Q': 238 case 'Q':
235 /* Very silent. */ 239 /* Very silent. */
236 be_quiet += 2; 240 be_quiet += 2;
241 if (freopen("/dev/null", "w", stdout) == NULL)
242 {
243 perror ("Can not be very quiet");
244 exit (3);
245 }
237 break; 246 break;
238 247
239 case 'q': 248 case 'q':
...@@ -269,7 +278,7 @@ main(int argc, char **argv) ...@@ -269,7 +278,7 @@ main(int argc, char **argv)
269 278
270 case 'v': 279 case 'v':
271 printf ("Mailutils 0.0.0: frm\n"); 280 printf ("Mailutils 0.0.0: frm\n");
272 exit (0); 281 exit (3);
273 break; 282 break;
274 283
275 default: 284 default:
...@@ -302,26 +311,43 @@ main(int argc, char **argv) ...@@ -302,26 +311,43 @@ main(int argc, char **argv)
302 || (status = mailbox_open (mbox, MU_STREAM_READ) != 0)) 311 || (status = mailbox_open (mbox, MU_STREAM_READ) != 0))
303 { 312 {
304 fprintf (stderr, "could not open mailbox\n"); 313 fprintf (stderr, "could not open mailbox\n");
305 exit (1); 314 exit (3);
306 } 315 }
307 316
308 observer_create (&observer, mbox); 317 if (! be_quiet)
309 observer_set_action (observer, action, mbox); 318 {
310 mailbox_get_observable (mbox, &observable); 319 observer_create (&observer, mbox);
311 observable_attach (observable, MU_EVT_MESSAGE_ADD, observer); 320 observer_set_action (observer, action, mbox);
321 mailbox_get_observable (mbox, &observable);
322 observable_attach (observable, MU_EVT_MESSAGE_ADD, observer);
323 }
312 324
313 mailbox_scan (mbox, 1, NULL); 325 mailbox_messages_count (mbox, &total);
314 326
315 observable_detach (observable, observer); 327 if (! be_quiet)
316 observer_destroy (&observer, mbox); 328 {
329 observable_detach (observable, observer);
330 observer_destroy (&observer, mbox);
331 }
317 332
318 mailbox_close(mbox); 333 mailbox_close(mbox);
319 mailbox_destroy(&mbox); 334 mailbox_destroy(&mbox);
320 } 335 }
321 336
322 if (show_summary && be_quiet < 1) 337 if (show_summary)
323 printf ("You have %d messages\n", counter); 338 printf ("You have %d messages\n", total);
324 if (show_query && have_new_mail) 339 if (show_query && have_new_mail)
325 printf ("You have new mail\n"); 340 printf ("You have new mail\n");
326 return 0; 341
342 /* 0 - selected messages discover.
343 1 - have messages.
344 2 - no message.
345 */
346 if (selected)
347 status = 0;
348 else if (total > 0)
349 status = 1;
350 else
351 status = 2;
352 return status;
327 } 353 }
......