]> granicus.if.org Git - neomutt/commitdiff
reorganise startup to return through main()
authorRichard Russon <rich@flatcap.org>
Tue, 20 Feb 2018 02:54:23 +0000 (02:54 +0000)
committerRichard Russon <rich@flatcap.org>
Thu, 15 Mar 2018 18:38:04 +0000 (18:38 +0000)
The single exit from main() makes it easier to clean up curses and
logging.

curs_lib.c
init.c
main.c
mutt.h
muttlib.c

index 92b7c1450202035ade23c92cae266a096699d901..f5b9dd96a38106f723fd8ee5f6b08ff0b2b661d4 100644 (file)
@@ -150,10 +150,8 @@ struct Event mutt_getch(void)
   if (ch == ERR)
   {
     if (!isatty(0))
-    {
-      endwin();
-      exit(1);
-    }
+      mutt_exit(1);
+
     return timeout;
   }
 
@@ -377,8 +375,7 @@ void mutt_query_exit(void)
     timeout(-1); /* restore blocking operation */
   if (mutt_yesorno(_("Exit NeoMutt?"), MUTT_YES) == MUTT_YES)
   {
-    endwin();
-    exit(1);
+    mutt_exit(1);
   }
   mutt_clear_error();
   mutt_curs_set(-1);
diff --git a/init.c b/init.c
index 1865d4f9566a93b131dd0c7c995d77922072b6b9..374be99640c410a566e3567743ae72e25ab9a3d8 100644 (file)
--- a/init.c
+++ b/init.c
@@ -25,7 +25,6 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <limits.h>
-#include <pwd.h>
 #include <regex.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -3664,13 +3663,13 @@ int mutt_query_variables(struct ListHead *queries)
     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);
@@ -3701,19 +3700,19 @@ int mutt_dump_variables(int hide_sensitive)
 
     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);
@@ -3735,7 +3734,7 @@ static int execute_commands(struct ListHead *p)
   {
     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);
 
@@ -3784,9 +3783,8 @@ static char *find_cfg(const char *home, const char *xdg_cfg_home)
   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];
@@ -3810,46 +3808,6 @@ void mutt_init(int skip_sys_rc, struct ListHead *commands)
   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)
   {
@@ -3876,9 +3834,8 @@ void mutt_init(int skip_sys_rc, struct ListHead *commands)
    */
   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 */
@@ -4063,10 +4020,8 @@ void mutt_init(int skip_sys_rc, struct ListHead *commands)
       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;
       }
     }
   }
@@ -4117,8 +4072,6 @@ void mutt_init(int skip_sys_rc, struct ListHead *commands)
   {
     if (np->data)
     {
-      if (!OPT_NO_CURSES)
-        endwin();
       if (source_rc(np->data, &err) != 0)
       {
         fputs(err.data, stderr);
@@ -4133,8 +4086,8 @@ void mutt_init(int skip_sys_rc, struct ListHead *commands)
 
   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);
@@ -4158,6 +4111,7 @@ void mutt_init(int skip_sys_rc, struct ListHead *commands)
 #endif
 
   FREE(&err.data);
+  return 0;
 }
 
 int mutt_get_hook_type(const char *name)
diff --git a/main.c b/main.c
index 711eff1fcf7356232cec43fd7b7c90ffc2a07540..7a57cb86b637aab684db90da7e25d0e0386657b5 100644 (file)
--- a/main.c
+++ b/main.c
@@ -28,6 +28,7 @@
 #include <errno.h>
 #include <limits.h>
 #include <locale.h>
+#include <pwd.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -120,12 +121,10 @@ static void usage(void)
          "  -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 */
 
@@ -143,8 +142,8 @@ static void start_curses(void)
 #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();
@@ -161,6 +160,7 @@ static void start_curses(void)
 #endif
   init_extended_keys();
   mutt_reflow_windows();
+  return 0;
 }
 
 #define MUTT_IGNORE (1 << 0)  /* -z */
@@ -172,6 +172,59 @@ static void start_curses(void)
 #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
@@ -206,13 +259,14 @@ int main(int argc, char **argv, char **env)
   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, "");
@@ -295,8 +349,8 @@ int main(int argc, char **argv, char **env)
         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;
@@ -373,6 +427,7 @@ int main(int argc, char **argv, char **env)
           break;
         default:
           usage();
+          goto main_ok;
       }
     }
   }
@@ -383,16 +438,18 @@ int main(int argc, char **argv, char **env)
   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))
@@ -431,14 +488,19 @@ int main(int argc, char **argv, char **env)
    * 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.  */
@@ -451,15 +513,19 @@ int main(int argc, char **argv, char **env)
   {
     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]));
@@ -476,11 +542,11 @@ int main(int argc, char **argv, char **env)
       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)
@@ -520,7 +586,9 @@ int main(int argc, char **argv, char **env)
   }
 
   if (batch_mode)
-    exit(0);
+  {
+    goto main_ok;
+  }
 
   if (sendflags & SENDPOSTPONED)
   {
@@ -554,9 +622,8 @@ int main(int argc, char **argv, char **env)
       {
         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
@@ -565,9 +632,8 @@ int main(int argc, char **argv, char **env)
 
     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)
@@ -592,8 +658,8 @@ int main(int argc, char **argv, char **env)
         {
           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;
         }
@@ -604,9 +670,8 @@ int main(int argc, char **argv, char **env)
           fin = fopen(expanded_infile, "r");
           if (!fin)
           {
-            mutt_endwin();
-            perror(expanded_infile);
-            exit(1);
+            mutt_perror(expanded_infile);
+            goto main_curses;
           }
         }
       }
@@ -624,11 +689,10 @@ int main(int argc, char **argv, char **env)
         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)
         {
@@ -643,10 +707,9 @@ int main(int argc, char **argv, char **env)
         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
@@ -675,8 +738,8 @@ int main(int argc, char **argv, char **env)
         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;
 
@@ -741,10 +804,9 @@ int main(int argc, char **argv, char **env)
           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);
@@ -760,16 +822,14 @@ int main(int argc, char **argv, char **env)
       {
         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
@@ -790,9 +850,8 @@ int main(int argc, char **argv, char **env)
         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);
       }
@@ -808,10 +867,9 @@ int main(int argc, char **argv, char **env)
     }
 
     mutt_free_windows();
-    mutt_endwin();
 
     if (rv != 0)
-      exit(1);
+      goto main_curses;
   }
   else
   {
@@ -819,9 +877,8 @@ int main(int argc, char **argv, char **env)
     {
       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));
@@ -834,26 +891,20 @@ int main(int argc, char **argv, char **env)
         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;
       }
     }
 
@@ -885,13 +936,11 @@ int main(int argc, char **argv, char **env)
       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;
       }
     }
 
@@ -921,5 +970,13 @@ int main(int argc, char **argv, char **env)
     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;
 }
diff --git a/mutt.h b/mutt.h
index 0f6c760d6240b8d5b8928d90616318d98aac5524..db911b678d018cd7d40b90a5d065993d0946cf41 100644 (file)
--- a/mutt.h
+++ b/mutt.h
@@ -259,7 +259,7 @@ enum QuadOptionResponse
 
 bool mutt_matches_ignore(const char *s);
 
-void mutt_init(int skip_sys_rc, struct ListHead *commands);
+int mutt_init(int skip_sys_rc, struct ListHead *commands);
 
 /* flag to mutt_pattern_comp() */
 #define MUTT_FULL_MSG (1 << 0) /* enable body and header matching */
index eab2f929201080e0bffbdcc60a34bca223ad198e..17fe3a3cd17f58bb2515333127a255d7efbf5e10 100644 (file)
--- a/muttlib.c
+++ b/muttlib.c
@@ -407,7 +407,7 @@ static void mutt_randbuf(void *out, size_t len)
   if (len > 1048576)
   {
     mutt_error(_("mutt_randbuf len=%zu"), len);
-    exit(1);
+    mutt_exit(1);
   }
 /* XXX switch to HAVE_GETRANDOM and getrandom() in about 2017 */
 #if defined(SYS_getrandom) && defined(__linux__)
@@ -427,14 +427,14 @@ static void mutt_randbuf(void *out, size_t len)
     if (!frandom)
     {
       mutt_error(_("open /dev/urandom: %s"), strerror(errno));
-      exit(1);
+      mutt_exit(1);
     }
     setbuf(frandom, NULL);
   }
   if (fread(out, 1, len, frandom) != len)
   {
     mutt_error(_("read /dev/urandom: %s"), strerror(errno));
-    exit(1);
+    mutt_exit(1);
   }
 }