#include <errno.h>
#include <inttypes.h>
#include <limits.h>
-#include <pwd.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
snprintf(command, sizeof(command), "set ?%s\n", np->data);
if (mutt_parse_rc_line(command, &token, &err) == -1)
{
- fprintf(stderr, "%s\n", err.data);
+ mutt_error("%s", err.data);
FREE(&token.data);
FREE(&err.data);
return 1;
}
- printf("%s\n", err.data);
+ mutt_message("%s", err.data);
}
FREE(&token.data);
if (hide_sensitive && IS_SENSITIVE(MuttVars[i]))
{
- printf("%s='***'\n", MuttVars[i].name);
+ mutt_message("%s='***'\n", MuttVars[i].name);
continue;
}
snprintf(command, sizeof(command), "set ?%s\n", MuttVars[i].name);
if (mutt_parse_rc_line(command, &token, &err) == -1)
{
- fprintf(stderr, "%s\n", err.data);
+ mutt_message("%s", err.data);
FREE(&token.data);
FREE(&err.data);
return 1;
}
- printf("%s\n", err.data);
+ mutt_message("%s", err.data);
}
FREE(&token.data);
{
if (mutt_parse_rc_line(np->data, &token, &err) == -1)
{
- fprintf(stderr, _("Error in command line: %s\n"), err.data);
+ mutt_error(_("Error in command line: %s"), err.data);
FREE(&token.data);
FREE(&err.data);
return NULL;
}
-void mutt_init(int skip_sys_rc, struct ListHead *commands)
+int mutt_init(int skip_sys_rc, struct ListHead *commands)
{
- struct passwd *pw = NULL;
struct utsname utsname;
const char *p = NULL;
char buffer[STRING];
snprintf(AttachmentMarker, sizeof(AttachmentMarker), "\033]9;%" PRIu64 "\a",
mutt_rand64());
- /* on one of the systems I use, getcwd() does not return the same prefix
- as is listed in the passwd file */
- p = mutt_str_getenv("HOME");
- if (p)
- HomeDir = mutt_str_strdup(p);
-
- /* Get some information about the user */
- pw = getpwuid(getuid());
- if (pw)
- {
- char rnbuf[STRING];
-
- Username = mutt_str_strdup(pw->pw_name);
- if (!HomeDir)
- HomeDir = mutt_str_strdup(pw->pw_dir);
-
- RealName = mutt_str_strdup(mutt_gecos_name(rnbuf, sizeof(rnbuf), pw));
- Shell = mutt_str_strdup(pw->pw_shell);
- endpwent();
- }
- else
- {
- if (!HomeDir)
- {
- mutt_endwin();
- fputs(_("unable to determine home directory"), stderr);
- exit(1);
- }
- p = mutt_str_getenv("USER");
- if (p)
- Username = mutt_str_strdup(p);
- else
- {
- mutt_endwin();
- fputs(_("unable to determine username"), stderr);
- exit(1);
- }
- Shell = mutt_str_strdup((p = mutt_str_getenv("SHELL")) ? p : "/bin/sh");
- }
-
/* Start up debugging mode if requested from cmdline */
if (debuglevel_cmdline > 0)
{
*/
if ((uname(&utsname)) == -1)
{
- mutt_endwin();
- perror(_("unable to determine nodename via uname()"));
- exit(1);
+ mutt_perror(_("unable to determine nodename via uname()"));
+ return 1;
}
/* some systems report the FQDN instead of just the hostname */
np->data = mutt_str_strdup(buffer);
if (access(np->data, F_OK))
{
- mutt_endwin();
- snprintf(buffer, sizeof(buffer), "%s: %s", np->data, strerror(errno));
- puts(buffer);
- exit(1);
+ mutt_perror(np->data);
+ return 1;
}
}
}
{
if (np->data)
{
- if (!OPT_NO_CURSES)
- endwin();
if (source_rc(np->data, &err) != 0)
{
fputs(err.data, stderr);
if (need_pause && !OPT_NO_CURSES)
{
- if (mutt_any_key_to_continue(NULL) == -1)
- mutt_exit(1);
+ if (mutt_any_key_to_continue(NULL) == 'q')
+ return 1;
}
mutt_file_mkdir(Tmpdir, S_IRWXU);
#endif
FREE(&err.data);
+ return 0;
}
int mutt_get_hook_type(const char *name)
#include <errno.h>
#include <limits.h>
#include <locale.h>
+#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
" -z exit immediately if there are no messages in the mailbox\n"
" -Z open the first folder with new message, exit immediately if none\n"
" -h this help message"));
-
- exit(0);
}
// clang-format on
-static void start_curses(void)
+static int start_curses(void)
{
km_init(); /* must come before mutt_init */
#endif
if (!initscr())
{
- puts(_("Error initializing terminal."));
- exit(1);
+ mutt_error(_("Error initializing terminal."));
+ return 1;
}
/* slang requires the signal handlers to be set after initializing */
mutt_signal_init();
#endif
init_extended_keys();
mutt_reflow_windows();
+ return 0;
}
#define MUTT_IGNORE (1 << 0) /* -z */
#define MUTT_NEWS (1 << 5) /* -g and -G */
#endif
+static int get_user_info(void)
+{
+ const char *p = NULL;
+
+ p = mutt_str_getenv("HOME");
+ if (p)
+ HomeDir = mutt_str_strdup(p);
+
+ /* Get some information about the user */
+ struct passwd *pw = getpwuid(getuid());
+ if (pw)
+ {
+ char rnbuf[STRING];
+
+ Username = mutt_str_strdup(pw->pw_name);
+ if (!HomeDir)
+ HomeDir = mutt_str_strdup(pw->pw_dir);
+
+ RealName = mutt_str_strdup(mutt_gecos_name(rnbuf, sizeof(rnbuf), pw));
+ Shell = mutt_str_strdup(pw->pw_shell);
+ endpwent();
+ }
+
+ if (!Username)
+ {
+ p = mutt_str_getenv("USER");
+ if (p)
+ Username = mutt_str_strdup(p);
+ }
+
+ if (!Username)
+ {
+ mutt_error(_("unable to determine username"));
+ return 1;
+ }
+
+ if (!HomeDir)
+ {
+ mutt_error(_("unable to determine home directory"));
+ return 1;
+ }
+
+ if (!Shell)
+ {
+ p = mutt_str_getenv("SHELL");
+ if (!p)
+ p = "/bin/sh";
+ Shell = mutt_str_strdup(p);
+ }
+
+ return 0;
+}
+
/**
* main - Start NeoMutt
* @param argc Number of command line arguments
extern char *optarg;
extern int optind;
int double_dash = argc, nargc = 1;
+ int rc = 1;
/* sanity check against stupid administrators */
if (getegid() != getgid())
{
fprintf(stderr, "%s: I don't want to run with privileges!\n", argv[0]);
- exit(1);
+ goto main_exit;
}
setlocale(LC_ALL, "");
case 'd':
if (mutt_str_atoi(optarg, &debuglevel_cmdline) < 0 || debuglevel_cmdline <= 0)
{
- fprintf(stderr, _("Error: value '%s' is invalid for -d.\n"), optarg);
- return 1;
+ mutt_error(_("Error: value '%s' is invalid for -d."), optarg);
+ goto main_exit;
}
printf(_("Debugging at level %d.\n"), debuglevel_cmdline);
break;
break;
default:
usage();
+ goto main_ok;
}
}
}
optind = 1;
argc = nargc;
- switch (version)
+ if (version > 0)
{
- case 0:
- break;
- case 1:
+ if (version == 1)
print_version();
- exit(0);
- default:
+ else
print_copyright();
- exit(0);
+ goto main_ok;
+ }
+
+ if (get_user_info() != 0)
+ {
+ goto main_exit;
}
if (!STAILQ_EMPTY(&cc_list) || !STAILQ_EMPTY(&bcc_list))
* before calling the init_pair() function to set the color scheme. */
if (!OPT_NO_CURSES)
{
- start_curses();
+ rc = start_curses();
+ if (rc != 0)
+ goto main_curses;
/* check whether terminal status is supported (must follow curses init) */
TSSupported = mutt_ts_capability();
}
/* set defaults and read init files */
- mutt_init(flags & MUTT_NOSYSRC, &commands);
+ rc = mutt_init(flags & MUTT_NOSYSRC, &commands);
+ if (rc != 0)
+ goto main_curses;
+
mutt_list_free(&commands);
/* Initialize crypto backends. */
{
for (; optind < argc; optind++)
mutt_list_insert_tail(&queries, mutt_str_strdup(argv[optind]));
- return mutt_query_variables(&queries);
+ rc = mutt_query_variables(&queries);
+ goto main_curses;
}
if (dump_variables)
- return mutt_dump_variables(hide_sensitive);
+ {
+ rc = mutt_dump_variables(hide_sensitive);
+ goto main_curses;
+ }
if (!STAILQ_EMPTY(&alias_queries))
{
- int rc = 0;
+ rc = 0;
struct Address *a = NULL;
for (; optind < argc; optind++)
mutt_list_insert_tail(&alias_queries, mutt_str_strdup(argv[optind]));
else
{
rc = 1;
- printf("%s\n", np->data);
+ mutt_message("%s", np->data);
}
}
mutt_list_free(&alias_queries);
- return rc;
+ goto main_curses;
}
if (!OPT_NO_CURSES)
}
if (batch_mode)
- exit(0);
+ {
+ goto main_ok;
+ }
if (sendflags & SENDPOSTPONED)
{
{
if (url_parse_mailto(msg->env, &bodytext, argv[i]) < 0)
{
- mutt_endwin();
- fputs(_("Failed to parse mailto: link\n"), stderr);
- exit(1);
+ mutt_error(_("Failed to parse mailto: link"));
+ goto main_curses;
}
}
else
if (!draft_file && Autoedit && !msg->env->to && !msg->env->cc)
{
- mutt_endwin();
- fputs(_("No recipients specified.\n"), stderr);
- exit(1);
+ mutt_error(_("No recipients specified."));
+ goto main_curses;
}
if (subject)
{
if (edit_infile)
{
- fputs(_("Cannot use -E flag with stdin\n"), stderr);
- exit(1);
+ mutt_error(_("Cannot use -E flag with stdin"));
+ goto main_curses;
}
fin = stdin;
}
fin = fopen(expanded_infile, "r");
if (!fin)
{
- mutt_endwin();
- perror(expanded_infile);
- exit(1);
+ mutt_perror(expanded_infile);
+ goto main_curses;
}
}
}
fout = mutt_file_fopen(tempfile, "w");
if (!fout)
{
- mutt_endwin();
- perror(tempfile);
mutt_file_fclose(&fin);
+ mutt_perror(tempfile);
FREE(&tempfile);
- exit(1);
+ goto main_curses;
}
if (fin)
{
fin = fopen(tempfile, "r");
if (!fin)
{
- mutt_endwin();
- perror(tempfile);
+ mutt_perror(tempfile);
FREE(&tempfile);
- exit(1);
+ goto main_curses;
}
}
/* If editing the infile, keep it around afterwards so
context_hdr->content = mutt_new_body();
if (fstat(fileno(fin), &st) != 0)
{
- perror(draft_file);
- exit(1);
+ mutt_perror(draft_file);
+ goto main_curses;
}
context_hdr->content->length = st.st_size;
msg->content = a = mutt_make_file_attach(np->data);
if (!a)
{
- mutt_endwin();
- fprintf(stderr, _("%s: unable to attach file.\n"), np->data);
+ mutt_error(_("%s: unable to attach file."), np->data);
mutt_list_free(&attach);
- exit(1);
+ goto main_curses;
}
}
mutt_list_free(&attach);
{
if (truncate(expanded_infile, 0) == -1)
{
- mutt_endwin();
- perror(expanded_infile);
- exit(1);
+ mutt_perror(expanded_infile);
+ goto main_curses;
}
fout = mutt_file_fopen(expanded_infile, "a");
if (!fout)
{
- mutt_endwin();
- perror(expanded_infile);
- exit(1);
+ mutt_perror(expanded_infile);
+ goto main_curses;
}
/* If the message was sent or postponed, these will already
fputc('\n', fout);
if ((mutt_write_mime_body(msg->content, fout) == -1))
{
- mutt_endwin();
mutt_file_fclose(&fout);
- exit(1);
+ goto main_curses;
}
mutt_file_fclose(&fout);
}
}
mutt_free_windows();
- mutt_endwin();
if (rv != 0)
- exit(1);
+ goto main_curses;
}
else
{
{
if (!mutt_buffy_check(false))
{
- mutt_endwin();
- puts(_("No mailbox with new mail."));
- exit(1);
+ mutt_message(_("No mailbox with new mail."));
+ goto main_curses;
}
folder[0] = '\0';
mutt_buffy(folder, sizeof(folder));
OPT_NEWS = true;
CurrentNewsSrv = nntp_select_server(NewsServer, false);
if (!CurrentNewsSrv)
- {
- mutt_endwin();
- puts(ErrorBuf);
- exit(1);
- }
+ goto main_curses;
}
else
#endif
if (!Incoming)
{
- mutt_endwin();
- puts(_("No incoming mailboxes defined."));
- exit(1);
+ mutt_error(_("No incoming mailboxes defined."));
+ goto main_curses;
}
folder[0] = '\0';
mutt_select_file(folder, sizeof(folder), MUTT_SEL_FOLDER | MUTT_SEL_BUFFY, NULL, NULL);
if (folder[0] == '\0')
{
- mutt_endwin();
- exit(0);
+ goto main_ok;
}
}
switch (mx_check_empty(folder))
{
case -1:
- mutt_endwin();
- puts(strerror(errno));
- exit(1);
+ mutt_perror(folder);
+ goto main_curses;
case 1:
- mutt_endwin();
- puts(_("Mailbox is empty."));
- exit(1);
+ mutt_error(_("Mailbox is empty."));
+ goto main_curses;
}
}
puts(ErrorBuf);
}
- exit(0);
+main_ok:
+ rc = 0;
+main_curses:
+ mutt_endwin();
+ /* Repeat the last message to the user */
+ if (ErrorBuf[0])
+ puts(ErrorBuf);
+main_exit:
+ return rc;
}