aboutsummaryrefslogtreecommitdiff
path: root/src/shared.cc
blob: bfd7498de16d8273b2f296ea53a948c89386878d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#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;
    nixDataDir = NIX_DATA_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 (Error & e) {
        cerr << format("error: %1%\n") % e.msg();
        return 1;
    } catch (exception & e) {
        cerr << format("error: %1%\n") % e.what();
        return 1;
    }

    return 0;
}