2003-12-22 16:40:46 +00:00
|
|
|
|
2003-07-04 15:42:03 +00:00
|
|
|
#include <iostream>
|
2003-07-24 08:53:43 +00:00
|
|
|
#include <cctype>
|
2003-07-04 15:42:03 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <aterm2.h>
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "globals.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
2004-01-15 20:23:55 +00:00
|
|
|
void sigintHandler(int signo)
|
|
|
|
{
|
|
|
|
_isInterrupted = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-22 20:53:49 +00:00
|
|
|
void setLogType(string lt)
|
|
|
|
{
|
|
|
|
if (lt == "pretty") logType = ltPretty;
|
|
|
|
else if (lt == "escapes") logType = ltEscapes;
|
|
|
|
else if (lt == "flat") logType = ltFlat;
|
|
|
|
else throw UsageError("unknown log type");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-04 15:42:03 +00:00
|
|
|
/* Initialize and reorder arguments, then call the actual argument
|
|
|
|
processor. */
|
|
|
|
static void initAndRun(int argc, char * * argv)
|
|
|
|
{
|
2004-02-14 21:44:18 +00:00
|
|
|
char * root = getenv("NIX_ROOT");
|
|
|
|
|
|
|
|
if (root) {
|
|
|
|
if (chroot(root) != 0)
|
|
|
|
throw SysError(format("changing root to `%1%'") % root);
|
|
|
|
}
|
|
|
|
|
2003-07-04 15:42:03 +00:00
|
|
|
/* Setup Nix paths. */
|
|
|
|
nixStore = NIX_STORE_DIR;
|
2003-07-10 13:41:28 +00:00
|
|
|
nixDataDir = NIX_DATA_DIR;
|
2003-07-04 15:42:03 +00:00
|
|
|
nixLogDir = NIX_LOG_DIR;
|
2003-11-19 17:27:16 +00:00
|
|
|
nixStateDir = (string) NIX_STATE_DIR;
|
2003-07-31 13:47:13 +00:00
|
|
|
nixDBPath = (string) NIX_STATE_DIR + "/db";
|
2003-07-04 15:42:03 +00:00
|
|
|
|
2004-01-15 20:23:55 +00:00
|
|
|
/* Catch SIGINT. */
|
|
|
|
struct sigaction act, oact;
|
|
|
|
act.sa_handler = sigintHandler;
|
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
if (sigaction(SIGINT, &act, &oact))
|
|
|
|
throw SysError("installing handler for SIGINT");
|
|
|
|
|
2004-03-22 20:53:49 +00:00
|
|
|
/* Process the NIX_LOG_TYPE environment variable. */
|
|
|
|
char * lt = getenv("NIX_LOG_TYPE");
|
|
|
|
if (lt) setLogType(lt);
|
|
|
|
|
2003-07-04 15:42:03 +00:00
|
|
|
/* Put the arguments in a vector. */
|
2003-12-01 15:55:05 +00:00
|
|
|
Strings args, remaining;
|
2003-07-04 15:42:03 +00:00
|
|
|
while (argc--) args.push_back(*argv++);
|
|
|
|
args.erase(args.begin());
|
|
|
|
|
2003-11-03 18:21:53 +00:00
|
|
|
/* Expand compound dash options (i.e., `-qlf' -> `-q -l -f'), and
|
|
|
|
ignore options for the ATerm library. */
|
2003-12-01 15:55:05 +00:00
|
|
|
for (Strings::iterator i = args.begin(); i != args.end(); ++i) {
|
|
|
|
string arg = *i;
|
|
|
|
if (string(arg, 0, 4) == "-at-") ;
|
2003-11-03 18:21:53 +00:00
|
|
|
else if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-') {
|
2003-12-01 15:55:05 +00:00
|
|
|
for (unsigned int j = 1; j < arg.length(); j++)
|
|
|
|
if (isalpha(arg[j]))
|
|
|
|
remaining.push_back((string) "-" + arg[j]);
|
2003-07-24 08:53:43 +00:00
|
|
|
else {
|
2003-12-01 15:55:05 +00:00
|
|
|
remaining.push_back(string(arg, j));
|
2003-07-24 08:53:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-12-01 15:55:05 +00:00
|
|
|
} else remaining.push_back(arg);
|
2003-07-04 15:42:03 +00:00
|
|
|
}
|
2003-12-01 15:55:05 +00:00
|
|
|
args = remaining;
|
|
|
|
remaining.clear();
|
2003-07-04 15:42:03 +00:00
|
|
|
|
2003-12-01 15:55:05 +00:00
|
|
|
/* Process default options. */
|
|
|
|
for (Strings::iterator i = args.begin(); i != args.end(); ++i) {
|
|
|
|
string arg = *i;
|
|
|
|
if (arg == "--verbose" || arg == "-v")
|
|
|
|
verbosity = (Verbosity) ((int) verbosity + 1);
|
2004-03-22 20:53:49 +00:00
|
|
|
else if (arg == "--log-type") {
|
|
|
|
++i;
|
|
|
|
if (i == args.end()) throw UsageError("`--log-type' requires an argument");
|
|
|
|
setLogType(*i);
|
|
|
|
} else if (arg == "--build-output" || arg == "-B")
|
2004-01-13 16:35:43 +00:00
|
|
|
buildVerbosity = lvlError; /* lowest */
|
2003-12-01 15:55:05 +00:00
|
|
|
else if (arg == "--help") {
|
|
|
|
printHelp();
|
|
|
|
return;
|
2004-02-02 10:51:54 +00:00
|
|
|
} else if (arg == "--version") {
|
|
|
|
cout << format("%1% (Nix) %2%") % programId % NIX_VERSION << endl;
|
|
|
|
return;
|
2003-12-01 15:55:05 +00:00
|
|
|
} else if (arg == "--keep-failed" || arg == "-K")
|
|
|
|
keepFailed = true;
|
|
|
|
else remaining.push_back(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
run(remaining);
|
2003-07-04 15:42:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-16 16:29:57 +00:00
|
|
|
static char buf[1024];
|
|
|
|
|
2003-07-04 15:42:03 +00:00
|
|
|
int main(int argc, char * * argv)
|
|
|
|
{
|
|
|
|
/* ATerm setup. */
|
|
|
|
ATerm bottomOfStack;
|
|
|
|
ATinit(argc, argv, &bottomOfStack);
|
|
|
|
|
2003-10-16 11:55:37 +00:00
|
|
|
/* Turn on buffering for cerr. */
|
2003-12-22 16:40:46 +00:00
|
|
|
#if HAVE_PUBSETBUF
|
2003-10-16 11:55:37 +00:00
|
|
|
cerr.rdbuf()->pubsetbuf(buf, sizeof(buf));
|
2003-12-22 16:40:46 +00:00
|
|
|
#endif
|
2003-10-16 11:55:37 +00:00
|
|
|
|
2003-07-04 15:42:03 +00:00
|
|
|
try {
|
|
|
|
initAndRun(argc, argv);
|
|
|
|
} catch (UsageError & e) {
|
2003-11-09 10:35:45 +00:00
|
|
|
printMsg(lvlError,
|
2003-07-24 08:53:43 +00:00
|
|
|
format(
|
|
|
|
"error: %1%\n"
|
|
|
|
"Try `%2% --help' for more information.")
|
|
|
|
% e.what() % programId);
|
2003-07-04 15:42:03 +00:00
|
|
|
return 1;
|
2003-07-07 09:25:26 +00:00
|
|
|
} catch (Error & e) {
|
2003-11-09 10:35:45 +00:00
|
|
|
printMsg(lvlError, format("error: %1%") % e.msg());
|
2003-07-07 09:25:26 +00:00
|
|
|
return 1;
|
2003-07-04 15:42:03 +00:00
|
|
|
} catch (exception & e) {
|
2003-11-09 10:35:45 +00:00
|
|
|
printMsg(lvlError, format("error: %1%") % e.what());
|
2003-07-04 15:42:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|