about summary refs log blame commit diff
path: root/src/shared.cc
blob: c0f07e955c73b4d9a360efa4734104a4978546df (plain) (tree)
1
2
                   
                 
















                                                                  
                              
                            
                                               












                                                                    





                                                           
















                                       




                                                         
                 
                         
                                                      
                 
                             
                                                       




                 

 
#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);
}


int main(int argc, char * * argv)
{
    /* ATerm setup. */
    ATerm bottomOfStack;
    ATinit(argc, argv, &bottomOfStack);

    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;
}