diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-10-20T09·20+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-10-20T09·20+0000 |
commit | 53e376d836133a660223198c7bb8308fb912375e (patch) | |
tree | 92d5e5381b9bfafd2a79d3efdec71f14edb798d9 /src/libmain | |
parent | 0eab306466fdb186c692521dd1f2b949e56c54da (diff) |
* Refactored the source tree.
Diffstat (limited to 'src/libmain')
-rw-r--r-- | src/libmain/shared.cc | 79 | ||||
-rw-r--r-- | src/libmain/shared.hh | 15 |
2 files changed, 94 insertions, 0 deletions
diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc new file mode 100644 index 000000000000..80463308a3f0 --- /dev/null +++ b/src/libmain/shared.cc @@ -0,0 +1,79 @@ +#include <iostream> +#include <cctype> + +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; + nixDataDir = NIX_DATA_DIR; + nixLogDir = NIX_LOG_DIR; + nixDBPath = (string) NIX_STATE_DIR + "/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++) + if (isalpha(arg[i])) + args.insert(it, (string) "-" + arg[i]); + else { + args.insert(it, string(arg, i)); + break; + } + it = args.erase(it); + } else it++; + } + + run(args); +} + + +static char buf[1024]; + +int main(int argc, char * * argv) +{ + /* ATerm setup. */ + ATerm bottomOfStack; + ATinit(argc, argv, &bottomOfStack); + + /* Turn on buffering for cerr. */ + cerr.rdbuf()->pubsetbuf(buf, sizeof(buf)); + + try { + initAndRun(argc, argv); + } catch (UsageError & e) { + msg(lvlError, + format( + "error: %1%\n" + "Try `%2% --help' for more information.") + % e.what() % programId); + return 1; + } catch (Error & e) { + msg(lvlError, format("error: %1%") % e.msg()); + return 1; + } catch (exception & e) { + msg(lvlError, format("error: %1%") % e.what()); + return 1; + } + + return 0; +} diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh new file mode 100644 index 000000000000..8ea637fd21ac --- /dev/null +++ b/src/libmain/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 */ |