Move -nice handling to CommandLineModuleManager
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlinemodulemanager.cpp
index 18e44a07f955e0d261f7780a5952063cc0965052..ac5f422d332e10ec9314c46f24854cefd39ab64f 100644 (file)
 #include <string>
 #include <utility>
 
+#include "config.h"
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
 #include "gromacs/legacyheaders/copyrite.h"
 
 #include "gromacs/commandline/cmdlinehelpcontext.h"
@@ -65,9 +71,6 @@
 #include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/stringutil.h"
 
-// For GMX_BINARY_SUFFIX
-#include "config.h"
-
 namespace gmx
 {
 
@@ -117,26 +120,22 @@ class CMainCommandLineModule : public CommandLineModuleInterface
             return shortDescription_;
         }
 
+        virtual void init(CommandLineModuleSettings * /*settings*/)
+        {
+        }
         virtual int run(int argc, char *argv[])
         {
             return mainFunction_(argc, argv);
         }
         virtual void writeHelp(const CommandLineHelpContext &context) const
         {
-            char *argv[2];
-            int   argc = 1;
-            // TODO: The constness should not be cast away.
-            argv[0] = const_cast<char *>(name_);
-            argv[1] = NULL;
-            GlobalCommandLineHelpContext global(context);
-            mainFunction_(argc, argv);
+            writeCommandLineHelpCMain(context, name_, mainFunction_);
         }
 
     private:
         const char             *name_;
         const char             *shortDescription_;
         CMainFunction           mainFunction_;
-
 };
 
 //! \}
@@ -149,7 +148,8 @@ class CMainCommandLineModule : public CommandLineModuleInterface
 
 CommandLineCommonOptionsHolder::CommandLineCommonOptionsHolder()
     : options_(NULL, NULL), bHelp_(false), bHidden_(false),
-      bQuiet_(false), bVersion_(false), bCopyright_(true), debugLevel_(0)
+      bQuiet_(false), bVersion_(false), bCopyright_(true),
+      niceLevel_(19), debugLevel_(0)
 {
     binaryInfoSettings_.copyright(true);
 }
@@ -171,6 +171,8 @@ void CommandLineCommonOptionsHolder::initOptions()
                            .description("Print extended version information and quit"));
     options_.addOption(BooleanOption("copyright").store(&bCopyright_)
                            .description("Print copyright information on startup"));
+    options_.addOption(IntegerOption("nice").store(&niceLevel_)
+                           .description("Set the nicelevel (default depends on command)"));
     options_.addOption(IntegerOption("debug").store(&debugLevel_)
                            .hidden().defaultValueIfSet(1)
                            .description("Write file with debug information, "
@@ -187,6 +189,15 @@ bool CommandLineCommonOptionsHolder::finishOptions()
     return !bVersion_;
 }
 
+void CommandLineCommonOptionsHolder::adjustFromSettings(
+        const CommandLineModuleSettings &settings)
+{
+    if (!options_.isSet("nice"))
+    {
+        niceLevel_ = settings.defaultNiceLevel();
+    }
+}
+
 /********************************************************************
  * CommandLineModuleManager::Impl
  */
@@ -549,7 +560,12 @@ int CommandLineModuleManager::run(int argc, char *argv[])
     {
         return 0;
     }
-    /* Open the debug file */
+
+    CommandLineModuleSettings settings;
+    module->init(&settings);
+    optionsHolder.adjustFromSettings(settings);
+
+    // Open the debug file.
     if (optionsHolder.debugLevel() > 0)
     {
         std::string filename(impl_->programContext_.programName());
@@ -562,6 +578,22 @@ int CommandLineModuleManager::run(int argc, char *argv[])
         fprintf(stderr, "Will write debug log file: %s\n", filename.c_str());
         gmx_init_debug(optionsHolder.debugLevel(), filename.c_str());
     }
+#if defined(HAVE_UNISTD_H) && !defined(GMX_NO_NICE)
+    // Set the nice level unless disabled in the configuration.
+    if (optionsHolder.niceLevel() != 0)
+    {
+        static bool bNiceSet = false; // Only set it once.
+        if (!bNiceSet)
+        {
+            if (nice(optionsHolder.niceLevel()) == -1)
+            {
+                // Do nothing, but use the return value to avoid warnings.
+            }
+            bNiceSet = true;
+        }
+    }
+#endif
+
     int rc = 0;
     if (!(module == impl_->helpModule_ && !bMaster))
     {