about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2003-07-04T15·42+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2003-07-04T15·42+0000
commitf826e432aa442e569faaf3cb04d83bfa28bcf260 (patch)
tree94e09e1fc7399949d8a5fb94f7d0abd995665d9e
parent01b34fe5843df9888737699ee9f9fe2f161a1fa3 (diff)
* Refactoring: move initialisation and argument parsing into a shared
  file.

-rw-r--r--src/Makefile.am2
-rw-r--r--src/eval.cc2
-rw-r--r--src/nix.cc56
-rw-r--r--src/shared.cc63
-rw-r--r--src/shared.hh15
5 files changed, 86 insertions, 52 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 573e84eb8a46..a3cd46ca2723 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -3,7 +3,7 @@ check_PROGRAMS = test
 
 AM_CXXFLAGS = -DSYSTEM=\"@host@\" -Wall -I..
 
-nix_SOURCES = nix.cc
+nix_SOURCES = nix.cc shared.cc
 nix_LDADD = libnix.a -ldb_cxx-4 -lATerm
 
 TESTS = test
diff --git a/src/eval.cc b/src/eval.cc
index be8b70a039be..4f4d419f5e3b 100644
--- a/src/eval.cc
+++ b/src/eval.cc
@@ -106,7 +106,7 @@ static void runProgram(const string & program, Environment env)
             
         } catch (exception & e) {
             cerr << format("build error: %1%\n") % e.what();
-         }
+        }
         _exit(1);
 
     }
diff --git a/src/nix.cc b/src/nix.cc
index 9c9936f4b44a..f2d00394f9d7 100644
--- a/src/nix.cc
+++ b/src/nix.cc
@@ -1,11 +1,10 @@
 #include <iostream>
 
-#include "config.h"
-
 #include "globals.hh"
 #include "values.hh"
 #include "eval.hh"
 #include "archive.hh"
+#include "shared.hh"
 
 
 typedef void (* Operation) (Strings opFlags, Strings opArgs);
@@ -224,39 +223,14 @@ static void opInit(Strings opFlags, Strings opArgs)
 }
 
 
-/* Initialize, process arguments, and dispatch to the right
-   operation. */
-static void run(int argc, char * * argv)
+/* Scan the arguments; find the operation, set global flags, put all
+   other flags in a list, and put all other arguments in another
+   list. */
+void run(Strings args)
 {
-    /* Setup Nix paths. */
-    nixStore = NIX_STORE_DIR;
-    nixLogDir = NIX_LOG_DIR;
-    nixDB = (string) NIX_STATE_DIR + "/nixstate.db";
-
-    /* Put the arguments in a vector. */
-    Strings args;
-    while (argc--) args.push_back(*argv++);
-    args.erase(args.begin());
-    
-    /* Expand compound dash options (i.e., `-qlf' -> `-q -l -f'). */
-    for (Strings::iterator it = args.begin();
-         it != args.end(); )
-    {
-        string arg = *it;
-        if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-') {
-            for (unsigned int i = 1; i < arg.length(); i++)
-                args.insert(it, (string) "-" + arg[i]);
-            it = args.erase(it);
-        } else it++;
-    }
-
     Strings opFlags, opArgs;
     Operation op = 0;
 
-    /* Scan the arguments; find the operation, set global flags, put
-       all other flags in a list, and put all other arguments in
-       another list. */
-
     for (Strings::iterator it = args.begin();
          it != args.end(); it++)
     {
@@ -291,22 +265,4 @@ static void run(int argc, char * * argv)
 }
 
 
-int main(int argc, char * * argv)
-{
-    /* ATerm setup. */
-    ATerm bottomOfStack;
-    ATinit(argc, argv, &bottomOfStack);
-
-    try {
-        run(argc, argv);
-    } catch (UsageError & e) {
-        cerr << "error: " << e.what() << endl
-             << "Try `nix --help' for more information.\n";
-        return 1;
-    } catch (exception & e) {
-        cerr << "error: " << e.what() << endl;
-        return 1;
-    }
-
-    return 0;
-}
+string programId = "nix";
diff --git a/src/shared.cc b/src/shared.cc
new file mode 100644
index 000000000000..6d157766e4af
--- /dev/null
+++ b/src/shared.cc
@@ -0,0 +1,63 @@
+#include <iostream>
+
+extern "C" {
+#include <aterm2.h>
+}
+
+#include "globals.hh"
+#include "shared.hh"
+
+#include "config.h"
+
+
+/* Initialize and reorder arguments, then call the actual argument
+   processor. */
+static void initAndRun(int argc, char * * argv)
+{
+    /* Setup Nix paths. */
+    nixStore = NIX_STORE_DIR;
+    nixLogDir = NIX_LOG_DIR;
+    nixDB = (string) NIX_STATE_DIR + "/nixstate.db";
+
+    /* Put the arguments in a vector. */
+    Strings args;
+    while (argc--) args.push_back(*argv++);
+    args.erase(args.begin());
+    
+    /* Expand compound dash options (i.e., `-qlf' -> `-q -l -f'). */
+    for (Strings::iterator it = args.begin();
+         it != args.end(); )
+    {
+        string arg = *it;
+        if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-') {
+            for (unsigned int i = 1; i < arg.length(); i++)
+                args.insert(it, (string) "-" + arg[i]);
+            it = args.erase(it);
+        } else it++;
+    }
+
+    run(args);
+}
+
+
+int main(int argc, char * * argv)
+{
+    /* ATerm setup. */
+    ATerm bottomOfStack;
+    ATinit(argc, argv, &bottomOfStack);
+
+    try {
+        initAndRun(argc, argv);
+    } catch (UsageError & e) {
+        cerr << format(
+            "error: %1%\n"
+            "Try `%2% --help' for more information.\n")
+            % e.what() % programId;
+        return 1;
+    } catch (exception & e) {
+        cerr << format("error: %1%\n") % e.what();
+        return 1;
+    }
+
+    return 0;
+}
diff --git a/src/shared.hh b/src/shared.hh
new file mode 100644
index 000000000000..8ea637fd21ac
--- /dev/null
+++ b/src/shared.hh
@@ -0,0 +1,15 @@
+#ifndef __SHARED_H
+#define __SHARED_H
+
+#include <string>
+
+#include "util.hh"
+
+
+void run(Strings args);
+
+
+extern string programId;
+
+
+#endif /* !__SHARED_H */