1 : #include <iostream>
2 :
3 : #include "hash.hh"
4 : #include "shared.hh"
5 : #include "help.txt.hh"
6 :
7 :
8 : using namespace nix;
9 :
10 :
11 1 : void printHelp()
12 : {
13 1 : std::cout << string((char *) helpText, sizeof helpText);
14 1 : }
15 :
16 :
17 59 : void run(Strings args)
18 : {
19 59 : HashType ht = htMD5;
20 59 : bool flat = false;
21 59 : bool base32 = false;
22 59 : bool truncate = false;
23 59 : enum { opHash, opTo32, opTo16 } op = opHash;
24 :
25 59 : Strings ss;
26 :
27 322 : for (Strings::iterator i = args.begin();
28 : i != args.end(); i++)
29 : {
30 204 : if (*i == "--flat") flat = true;
31 154 : else if (*i == "--base32") base32 = true;
32 118 : else if (*i == "--truncate") truncate = true;
33 118 : else if (*i == "--type") {
34 55 : ++i;
35 55 : if (i == args.end()) throw UsageError("`--type' requires an argument");
36 55 : ht = parseHashType(*i);
37 55 : if (ht == htUnknown)
38 0 : throw UsageError(format("unknown hash type `%1%'") % *i);
39 : }
40 63 : else if (*i == "--to-base16") op = opTo16;
41 61 : else if (*i == "--to-base32") op = opTo32;
42 59 : else ss.push_back(*i);
43 : }
44 :
45 59 : if (op == opHash) {
46 110 : for (Strings::iterator i = ss.begin(); i != ss.end(); ++i) {
47 55 : Hash h = flat ? hashFile(ht, *i) : hashPath(ht, *i);
48 55 : if (truncate && h.hashSize > 20) h = compressHash(h, 20);
49 : std::cout << format("%1%\n") %
50 55 : (base32 ? printHash32(h) : printHash(h));
51 : }
52 : }
53 :
54 : else {
55 8 : for (Strings::iterator i = ss.begin(); i != ss.end(); ++i) {
56 4 : Hash h = op == opTo16 ? parseHash32(ht, *i) : parseHash(ht, *i);
57 : std::cout << format("%1%\n") %
58 4 : (op == opTo16 ? printHash(h) : printHash32(h));
59 : }
60 59 : }
61 59 : }
62 :
63 0 :
64 180 : string programId = "nix-hash";
65 60 :
|