From f826e432aa442e569faaf3cb04d83bfa28bcf260 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 4 Jul 2003 15:42:03 +0000 Subject: * Refactoring: move initialisation and argument parsing into a shared file. --- src/shared.cc | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/shared.cc (limited to 'src/shared.cc') 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 + +extern "C" { +#include +} + +#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; +} -- cgit 1.4.1