]>
git.saurik.com Git - apt.git/blob - cmdline/apt-helper.cc
1 // -*- mode: cpp; mode: fold -*-
3 /* #####################################################################
4 apt-helper - cmdline helpers
5 ##################################################################### */
7 // Include Files /*{{{*/
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>
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>
33 static bool DoDownloadFile(CommandLine
&CmdL
)
35 if (CmdL
.FileSize() <= 2)
36 return _error
->Error(_("Must specify at least one pair url/filename"));
40 AcqTextStatus
Stat(ScreenWidth
, _config
->FindI("quiet",0));
42 std::string download_uri
= CmdL
.FileList
[1];
43 std::string targetfile
= CmdL
.FileList
[2];
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
);
51 if (AcquireRun(Fetcher
, 0, &Failed
, NULL
) == false || Failed
== true ||
52 FileExists(targetfile
) == false)
53 return _error
->Error(_("Download Failed"));
57 static bool DoSrvLookup(CommandLine
&CmdL
)
59 if (CmdL
.FileSize() < 1)
60 return _error
->Error(_("Must specifc at least one srv record"));
62 std::vector
<SrvRec
> srv_records
;
63 for(int i
=1; CmdL
.FileList
[i
] != NULL
; i
++)
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
)
70 c1out
<< (*I
).target
.c_str() << " "
71 << (*I
).priority
<< " "
80 static bool ShowHelp(CommandLine
&)
82 ioprintf(std::cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
83 COMMON_ARCH
,__DATE__
,__TIME__
);
85 if (_config
->FindB("version") == true)
89 _("Usage: apt-helper [options] command\n"
90 " apt-helper [options] download-file uri target-path\n"
92 "apt-helper is a internal helper for apt\n"
95 " download-file - download the given uri to the target-path\n"
97 " This APT helper has Super Meep Powers.\n");
102 int main(int argc
,const char *argv
[]) /*{{{*/
104 CommandLine::Dispatch Cmds
[] = {{"help",&ShowHelp
},
105 {"download-file", &DoDownloadFile
},
106 {"srv-lookup", &DoSrvLookup
},
109 std::vector
<CommandLine::Args
> Args
= getCommandArgs(
110 "apt-download", CommandLine::GetCommand(Cmds
, argc
, argv
));
112 // Set up gettext support
113 setlocale(LC_ALL
,"");
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)
122 if (_config
->FindB("version") == true)
124 _error
->DumpErrors();
128 // See if the help should be shown
129 if (_config
->FindB("help") == true ||
130 _config
->FindB("version") == true ||
131 CmdL
.FileSize() == 0)
139 // Match the operation
140 CmdL
.DispatchArg(Cmds
);
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();
147 _error
->DumpErrors(GlobalError::DEBUG
);
148 return Errors
== true ? 100 : 0;