From f915f773495e9675a6cd514742666c8c12f005e6 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 28 Mar 2003 09:53:22 +0000 Subject: [PATCH] * Allow arguments to be passed to programs in `nix run'. --- src/nix.cc | 52 ++++++++++++++++++++++--------------------- test/build/pan-run.sh | 2 +- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/nix.cc b/src/nix.cc index 4ff49eabd..c24cb1fd7 100644 --- a/src/nix.cc +++ b/src/nix.cc @@ -393,7 +393,7 @@ string getPkg(string hash) } -void runPkg(string hash) +void runPkg(string hash, const vector & args) { string src; string path; @@ -424,8 +424,17 @@ void runPkg(string hash) putenv((char *) s->c_str()); } + /* Create the list of arguments. */ + const char * args2[env.size() + 2]; + int i = 0; + args2[i++] = runner.c_str(); + for (vector::const_iterator it = args.begin(); + it != args.end(); it++, i++) + args2[i] = it->c_str(); + args2[i] = 0; + /* Execute the runner. This should not return. */ - execl(runner.c_str(), runner.c_str(), 0); + execv(runner.c_str(), (char * *) args2); cout << strerror(errno) << endl; @@ -657,8 +666,8 @@ void run(vector args) string path = getPkg(args[0]); cout << path << endl; } else if (cmd == "run") { - if (args.size() != 1) throw argcError; - runPkg(args[0]); + if (args.size() < 1) throw argcError; + runPkg(args[0], vector(args.begin() + 1, args.end())); } else if (cmd == "ensure") { if (args.size() != 1) throw argcError; ensurePkg(args[0]); @@ -708,8 +717,8 @@ Subcommands: listinst Prints a list of installed packages. - run HASH - Run the descriptor referenced by HASH. + run HASH ARGS... + Run the descriptor referenced by HASH with the given arguments. ensure HASH Like getpkg, but if HASH refers to a run descriptor, fetch only @@ -730,8 +739,6 @@ Subcommands: void main2(int argc, char * * argv) { - int c; - umask(0022); if (getenv(PKGINFO_ENVVAR)) @@ -740,28 +747,23 @@ void main2(int argc, char * * argv) if (getenv(PKGHOME_ENVVAR)) pkgHome = getenv(PKGHOME_ENVVAR); - opterr = 0; - - while ((c = getopt(argc, argv, "hd:")) != EOF) { - - switch (c) { - - case 'h': + /* Parse the global flags. */ + while (argc) { + string arg(*argv); + cout << arg << endl; + if (arg == "-h" || arg == "--help") { printUsage(); return; - - case 'd': + } else if (arg == "-d") { dbfile = optarg; - break; - - default: - throw UsageError("invalid option `" + string(1, optopt) + "'"); - break; - } + } else if (arg[0] == '-') { + throw UsageError("invalid option `" + arg + "'"); + } else break; + argv++, argc--; } + /* Put the remainder in a vector and pass it to run2(). */ vector args; - argc--, argv++; while (argc--) args.push_back(*argv++); run(args); } @@ -769,7 +771,7 @@ void main2(int argc, char * * argv) int main(int argc, char * * argv) { - prog = argv[0]; + prog = *argv++, argc--; try { try { diff --git a/test/build/pan-run.sh b/test/build/pan-run.sh index 923a23292..c933a3803 100755 --- a/test/build/pan-run.sh +++ b/test/build/pan-run.sh @@ -4,4 +4,4 @@ export LD_LIBRARY_PATH=$glib/lib:$atk/lib:$pango/lib:$gtk/lib:$gnet/lib:$pspell/ ldd $pan/bin/pan -$pan/bin/pan \ No newline at end of file +$pan/bin/pan $* \ No newline at end of file