ソースを参照

Don't exit on error in option_processing

Still it exits when -v or -h is given. They are now guarded by
standalone variable and only do so when it is true.
Tatsuhiro Tsujikawa 12 年 前
コミット
aee621b3d4
3 ファイル変更52 行追加36 行削除
  1. 13 5
      src/Context.cc
  2. 6 1
      src/aria2api.cc
  3. 33 30
      src/option_processing.cc

+ 13 - 5
src/Context.cc

@@ -175,17 +175,25 @@ void showFiles
 } // namespace
 #endif // ENABLE_BITTORRENT || ENABLE_METALINK
 
-extern void option_processing(Option& option, bool standalone,
-                              std::vector<std::string>& uris,
-                              int argc, char** argv, const KeyVals& options);
+extern error_code::Value option_processing(Option& option, bool standalone,
+                                           std::vector<std::string>& uris,
+                                           int argc, char** argv,
+                                           const KeyVals& options);
 
 Context::Context(bool standalone,
                  int argc, char** argv, const KeyVals& options)
 {
   std::vector<std::string> args;
   SharedHandle<Option> op(new Option());
-  option_processing(*op.get(), standalone, args, argc, argv, options);
-
+  error_code::Value rv;
+  rv = option_processing(*op.get(), standalone, args, argc, argv, options);
+  if(rv != error_code::FINISHED) {
+    if(standalone) {
+      exit(rv);
+    } else {
+      throw DL_ABORT_EX("Option processing failed");
+    }
+  }
   SimpleRandomizer::init();
 #ifdef ENABLE_BITTORRENT
   bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX));

+ 6 - 1
src/aria2api.cc

@@ -85,7 +85,12 @@ int libraryDeinit()
 Session* sessionNew(const KeyVals& options)
 {
   int rv;
-  Session* session = new Session(options);
+  Session* session;
+  try {
+    session = new Session(options);
+  } catch(RecoverableException& e) {
+    return 0;
+  }
   if(session->context->reqinfo) {
     rv = session->context->reqinfo->prepare();
     if(rv != 0) {

+ 33 - 30
src/option_processing.cc

@@ -190,9 +190,10 @@ void optionNativeToUtf8(Option& op)
 } // namespace
 #endif // __MINGW32__
 
-void option_processing(Option& op, bool standalone,
-                       std::vector<std::string>& uris,
-                       int argc, char** argv, const KeyVals& options)
+error_code::Value option_processing(Option& op, bool standalone,
+                                    std::vector<std::string>& uris,
+                                    int argc, char** argv,
+                                    const KeyVals& options)
 {
   const SharedHandle<OptionParser>& oparser = OptionParser::getInstance();
   try {
@@ -206,27 +207,28 @@ void option_processing(Option& op, bool standalone,
       oparser->parse(op, cmdstream);
       noConf = op.getAsBool(PREF_NO_CONF);
       ucfname = op.get(PREF_CONF_PATH);
-
-      if(op.defined(PREF_VERSION)) {
-        showVersion();
-        exit(error_code::FINISHED);
-      }
-      if(op.defined(PREF_HELP)) {
-        std::string keyword;
-        if(op.get(PREF_HELP).empty()) {
-          keyword = strHelpTag(TAG_BASIC);
-        } else {
-          keyword = op.get(PREF_HELP);
-          if(util::startsWith(keyword, "--")) {
-            keyword.erase(keyword.begin(), keyword.begin()+2);
-          }
-          std::string::size_type eqpos = keyword.find("=");
-          if(eqpos != std::string::npos) {
-            keyword.erase(keyword.begin()+eqpos, keyword.end());
+      if(standalone) {
+        if(op.defined(PREF_VERSION)) {
+          showVersion();
+          exit(error_code::FINISHED);
+        }
+        if(op.defined(PREF_HELP)) {
+          std::string keyword;
+          if(op.get(PREF_HELP).empty()) {
+            keyword = strHelpTag(TAG_BASIC);
+          } else {
+            keyword = op.get(PREF_HELP);
+            if(util::startsWith(keyword, "--")) {
+              keyword.erase(keyword.begin(), keyword.begin()+2);
+            }
+            std::string::size_type eqpos = keyword.find("=");
+            if(eqpos != std::string::npos) {
+              keyword.erase(keyword.begin()+eqpos, keyword.end());
+            }
           }
+          showUsage(keyword, oparser, global::cout());
+          exit(error_code::FINISHED);
         }
-        showUsage(keyword, oparser, global::cout());
-        exit(error_code::FINISHED);
       }
     }
     SharedHandle<Option> confOption(new Option());
@@ -254,18 +256,18 @@ void option_processing(Option& op, bool standalone,
             global::cerr()->printf(_("Usage:"));
             global::cerr()->printf("\n%s\n", h->getDescription());
           }
-          exit(e.getErrorCode());
+          return e.getErrorCode();
         } catch(Exception& e) {
           global::cerr()->printf(_("Parse error in %s"), cfname.c_str());
           global::cerr()->printf("\n%s", e.stackTrace().c_str());
-          exit(e.getErrorCode());
+          return e.getErrorCode();
         }
       } else if(!ucfname.empty()) {
         global::cerr()->printf(_("Configuration file %s is not found."),
                                cfname.c_str());
         global::cerr()->printf("\n");
         showUsage(strHelpTag(TAG_HELP), oparser, global::cerr());
-        exit(error_code::UNKNOWN_ERROR);
+        return error_code::UNKNOWN_ERROR;
       }
     }
     // Override configuration with environment variables.
@@ -298,15 +300,15 @@ void option_processing(Option& op, bool standalone,
       global::cerr()->printf("\n");
       write(global::cerr(), *h);
     }
-    exit(e.getErrorCode());
+    return e.getErrorCode();
   } catch(UnknownOptionException& e) {
     showUsage("", oparser, global::cerr());
     showCandidates(e.getUnknownOption(), oparser);
-    exit(e.getErrorCode());
+    return e.getErrorCode();
   } catch(Exception& e) {
     global::cerr()->printf("%s", e.stackTrace().c_str());
     showUsage("", oparser, global::cerr());
-    exit(e.getErrorCode());
+    return e.getErrorCode();
   }
   if(standalone &&
      !op.getAsBool(PREF_ENABLE_RPC) &&
@@ -321,15 +323,16 @@ void option_processing(Option& op, bool standalone,
       global::cerr()->printf(MSG_URI_REQUIRED);
       global::cerr()->printf("\n");
       showUsage("", oparser, global::cerr());
-      exit(error_code::UNKNOWN_ERROR);
+      return error_code::UNKNOWN_ERROR;
     }
   }
   if(op.getAsBool(PREF_DAEMON)) {
     if(daemon(0, 0) < 0) {
       perror(MSG_DAEMON_FAILED);
-      exit(error_code::UNKNOWN_ERROR);
+      return error_code::UNKNOWN_ERROR;
     }
   }
+  return error_code::FINISHED;
 }
 
 } // namespace aria2