extern char *optarg;
extern int optind;
int double_dash = argc, nargc = 1;
+ int exit_code = 1;
+ const char *exit_endwin_msg = NULL;
/* sanity check against stupid administrators */
if (alias_queries)
{
- int rv = 0;
ADDRESS *a;
+
+ exit_code = 0;
for (; optind < argc; optind++)
alias_queries = mutt_add_list (alias_queries, argv[optind]);
for (; alias_queries; alias_queries = alias_queries->next)
{
if ((a = mutt_lookup_alias (alias_queries->data)))
- {
+ {
/* output in machine-readable form */
mutt_addrlist_to_intl (a, NULL);
mutt_write_address_list (a, stdout, 0, 0);
}
else
{
- rv = 1;
+ exit_code = 1;
printf ("%s\n", alias_queries->data);
}
}
- return rv;
+ goto cleanup_and_exit;
}
if (!option (OPTNOCURSES))
if (!option (OPTNOCURSES))
mutt_flushinp ();
ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
- mutt_free_windows ();
- mutt_endwin (NULL);
}
else if (subject || msg || sendflags || draftFile || includeFile || attach ||
optind < argc)
if (url_parse_mailto (msg->env, &bodytext, argv[i]) < 0)
{
if (!option (OPTNOCURSES))
+ {
mutt_endwin (NULL);
+ set_option (OPTNOCURSES);
+ }
fputs (_("Failed to parse mailto: link\n"), stderr);
- exit (1);
+ goto cleanup_and_exit;
}
}
else
if (!draftFile && option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc)
{
if (!option (OPTNOCURSES))
+ {
mutt_endwin (NULL);
+ set_option (OPTNOCURSES);
+ }
fputs (_("No recipients specified.\n"), stderr);
- exit (1);
+ goto cleanup_and_exit;
}
if (subject)
if (edit_infile)
{
fputs (_("Cannot use -E flag with stdin\n"), stderr);
- exit (1);
+ goto cleanup_and_exit;
}
fin = stdin;
}
if ((fin = fopen (expanded_infile, "r")) == NULL)
{
if (!option (OPTNOCURSES))
+ {
mutt_endwin (NULL);
+ set_option (OPTNOCURSES);
+ }
perror (expanded_infile);
- exit (1);
+ goto cleanup_and_exit;
}
}
}
if ((fout = safe_fopen (tempfile, "w")) == NULL)
{
if (!option (OPTNOCURSES))
+ {
mutt_endwin (NULL);
+ set_option (OPTNOCURSES);
+ }
perror (tempfile);
safe_fclose (&fin);
FREE (&tempfile);
- exit (1);
+ goto cleanup_and_exit;
}
if (fin)
{
if ((fin = fopen (tempfile, "r")) == NULL)
{
if (!option (OPTNOCURSES))
+ {
mutt_endwin (NULL);
+ set_option (OPTNOCURSES);
+ }
perror (tempfile);
FREE (&tempfile);
- exit (1);
+ goto cleanup_and_exit;
}
}
/* If editing the infile, keep it around afterwards so
if (fstat (fileno (fin), &st))
{
perror (draftFile);
- exit (1);
+ goto cleanup_and_exit;
}
context_hdr->content->length = st.st_size;
if (!a)
{
if (!option (OPTNOCURSES))
+ {
mutt_endwin (NULL);
+ set_option (OPTNOCURSES);
+ }
fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
mutt_free_list (&attach);
- exit (1);
+ goto cleanup_and_exit;
}
t = t->next;
}
if (truncate (expanded_infile, 0) == -1)
{
if (!option (OPTNOCURSES))
+ {
mutt_endwin (NULL);
+ set_option (OPTNOCURSES);
+ }
perror (expanded_infile);
- exit (1);
+ goto cleanup_and_exit;
}
if ((fout = safe_fopen (expanded_infile, "a")) == NULL)
{
if (!option (OPTNOCURSES))
+ {
mutt_endwin (NULL);
+ set_option (OPTNOCURSES);
+ }
perror (expanded_infile);
- exit (1);
+ goto cleanup_and_exit;
}
/* If the message was sent or postponed, these will already
fputc ('\n', fout);
if ((mutt_write_mime_body (msg->content, fout) == -1))
{
- if (!option (OPTNOCURSES))
- mutt_endwin (NULL);
safe_fclose (&fout);
- exit (1);
+ goto cleanup_and_exit;
}
safe_fclose (&fout);
}
FREE (&tempfile);
}
- mutt_free_windows ();
- if (!option (OPTNOCURSES))
- mutt_endwin (NULL);
-
if (rv)
- exit(1);
+ goto cleanup_and_exit;
}
else
{
#endif
if (!mutt_buffy_check (0))
{
- mutt_endwin _("No mailbox with new mail.");
- exit (1);
+ exit_endwin_msg = _("No mailbox with new mail.");
+ goto cleanup_and_exit;
}
folder[0] = 0;
mutt_buffy (folder, sizeof (folder));
}
else if (flags & MUTT_SELECT)
{
- if (!Incoming) {
- mutt_endwin _("No incoming mailboxes defined.");
- exit (1);
+ if (!Incoming)
+ {
+ exit_endwin_msg = _("No incoming mailboxes defined.");
+ goto cleanup_and_exit;
}
folder[0] = 0;
mutt_select_file (folder, sizeof (folder), MUTT_SEL_FOLDER | MUTT_SEL_BUFFY);
if (!folder[0])
{
- mutt_endwin (NULL);
- exit (0);
+ exit_code = 0;
+ goto cleanup_and_exit;
}
}
switch (mx_check_empty (folder))
{
case -1:
- mutt_endwin (strerror (errno));
- exit (1);
+ exit_endwin_msg = strerror (errno);
+ goto cleanup_and_exit;
case 1:
- mutt_endwin _("Mailbox is empty.");
- exit (1);
+ exit_endwin_msg = _("Mailbox is empty.");
+ goto cleanup_and_exit;
}
}
if (Context)
FREE (&Context);
}
+
+ exit_endwin_msg = Errorbuf;
+ }
+
+ exit_code = 0;
+
+cleanup_and_exit:
#ifdef USE_IMAP
- imap_logout_all ();
+ imap_logout_all ();
#endif
#ifdef USE_SASL
- mutt_sasl_done ();
+ mutt_sasl_done ();
#endif
- mutt_free_opts ();
- mutt_free_windows ();
- mutt_endwin (Errorbuf);
- }
-
- exit (0);
+ mutt_free_opts ();
+ mutt_free_windows ();
+ if (!option (OPTNOCURSES))
+ mutt_endwin (exit_endwin_msg);
+ exit (exit_code);
}