]> git.saurik.com Git - apt.git/blob - cmdline/apt-helper.cc
WIP start randomizing
[apt.git] / cmdline / apt-helper.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* #####################################################################
4 apt-helper - cmdline helpers
5 ##################################################################### */
6 /*}}}*/
7 // Include Files /*{{{*/
8 #include <config.h>
9
10 #include <apt-pkg/configuration.h>
11 #include <apt-pkg/cmndline.h>
12 #include <apt-pkg/error.h>
13 #include <apt-pkg/init.h>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/pkgsystem.h>
16 #include <apt-pkg/fileutl.h>
17 #include <apt-pkg/acquire.h>
18 #include <apt-pkg/acquire-item.h>
19
20 #include <apt-private/acqprogress.h>
21 #include <apt-private/private-output.h>
22 #include <apt-private/private-download.h>
23 #include <apt-private/private-cmndline.h>
24 #include <apt-pkg/srvrec.h>
25
26 #include <iostream>
27 #include <string>
28 #include <vector>
29
30 #include <apti18n.h>
31 /*}}}*/
32
33 static bool DoDownloadFile(CommandLine &CmdL)
34 {
35 if (CmdL.FileSize() <= 2)
36 return _error->Error(_("Must specify at least one pair url/filename"));
37
38
39 pkgAcquire Fetcher;
40 AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
41 Fetcher.Setup(&Stat);
42 std::string download_uri = CmdL.FileList[1];
43 std::string targetfile = CmdL.FileList[2];
44 std::string hash;
45 if (CmdL.FileSize() > 3)
46 hash = CmdL.FileList[3];
47 new pkgAcqFile(&Fetcher, download_uri, hash, 0, "desc", "short-desc",
48 "dest-dir-ignored", targetfile);
49 Fetcher.Run();
50 bool Failed = false;
51 if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true ||
52 FileExists(targetfile) == false)
53 return _error->Error(_("Download Failed"));
54 return true;
55 }
56
57 static bool DoSrvLookup(CommandLine &CmdL)
58 {
59 if (CmdL.FileSize() < 1)
60 return _error->Error(_("Must specifc at least one srv record"));
61
62 std::vector<SrvRec> srv_records;
63 for(int i=1; CmdL.FileList[i] != NULL; i++)
64 {
65 if(GetSrvRecords(CmdL.FileList[i], srv_records) == false)
66 _error->Warning(_("GetSrvRec failed for %s"), CmdL.FileList[i]);
67 for (std::vector<SrvRec>::const_iterator I = srv_records.begin();
68 I != srv_records.end(); ++I)
69 {
70 c1out << (*I).target.c_str() << " "
71 << (*I).priority << " "
72 << (*I).weight << " "
73 << (*I).port << " "
74 << std::endl;
75 }
76 }
77 return true;
78 }
79
80 static bool ShowHelp(CommandLine &)
81 {
82 ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
83 COMMON_ARCH,__DATE__,__TIME__);
84
85 if (_config->FindB("version") == true)
86 return true;
87
88 std::cout <<
89 _("Usage: apt-helper [options] command\n"
90 " apt-helper [options] download-file uri target-path\n"
91 "\n"
92 "apt-helper is a internal helper for apt\n"
93 "\n"
94 "Commands:\n"
95 " download-file - download the given uri to the target-path\n"
96 "\n"
97 " This APT helper has Super Meep Powers.\n");
98 return true;
99 }
100
101
102 int main(int argc,const char *argv[]) /*{{{*/
103 {
104 CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
105 {"download-file", &DoDownloadFile},
106 {"srv-lookup", &DoSrvLookup},
107 {0,0}};
108
109 std::vector<CommandLine::Args> Args = getCommandArgs(
110 "apt-download", CommandLine::GetCommand(Cmds, argc, argv));
111
112 // Set up gettext support
113 setlocale(LC_ALL,"");
114 textdomain(PACKAGE);
115
116 // Parse the command line and initialize the package library
117 CommandLine CmdL(Args.data(),_config);
118 if (pkgInitConfig(*_config) == false ||
119 CmdL.Parse(argc,argv) == false ||
120 pkgInitSystem(*_config,_system) == false)
121 {
122 if (_config->FindB("version") == true)
123 ShowHelp(CmdL);
124 _error->DumpErrors();
125 return 100;
126 }
127
128 // See if the help should be shown
129 if (_config->FindB("help") == true ||
130 _config->FindB("version") == true ||
131 CmdL.FileSize() == 0)
132 {
133 ShowHelp(CmdL);
134 return 0;
135 }
136
137 InitOutput();
138
139 // Match the operation
140 CmdL.DispatchArg(Cmds);
141
142 // Print any errors or warnings found during parsing
143 bool const Errors = _error->PendingError();
144 if (_config->FindI("quiet",0) > 0)
145 _error->DumpErrors();
146 else
147 _error->DumpErrors(GlobalError::DEBUG);
148 return Errors == true ? 100 : 0;
149 }
150 /*}}}*/