Commit | Line | Data |
---|---|---|
b9179170 MV |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
3 | /* ###################################################################### | |
4 | ||
5 | apt - CLI UI for apt | |
6 | ||
7 | Returns 100 on failure, 0 on success. | |
8 | ||
9 | ##################################################################### */ | |
10 | /*}}}*/ | |
11 | // Include Files /*{{{*/ | |
12 | #include<config.h> | |
13 | ||
14 | #include <cassert> | |
15 | #include <locale.h> | |
16 | #include <iostream> | |
17 | #include <unistd.h> | |
18 | #include <errno.h> | |
19 | #include <regex.h> | |
20 | #include <stdio.h> | |
21 | #include <iomanip> | |
22 | #include <algorithm> | |
23 | ||
24 | ||
25 | #include <apt-pkg/error.h> | |
26 | #include <apt-pkg/cachefile.h> | |
27 | #include <apt-pkg/cacheset.h> | |
28 | #include <apt-pkg/init.h> | |
29 | #include <apt-pkg/progress.h> | |
30 | #include <apt-pkg/sourcelist.h> | |
31 | #include <apt-pkg/cmndline.h> | |
32 | #include <apt-pkg/strutl.h> | |
33 | #include <apt-pkg/fileutl.h> | |
34 | #include <apt-pkg/pkgrecords.h> | |
35 | #include <apt-pkg/srcrecords.h> | |
36 | #include <apt-pkg/version.h> | |
37 | #include <apt-pkg/policy.h> | |
38 | #include <apt-pkg/tagfile.h> | |
39 | #include <apt-pkg/algorithms.h> | |
40 | #include <apt-pkg/sptr.h> | |
41 | #include <apt-pkg/pkgsystem.h> | |
42 | #include <apt-pkg/indexfile.h> | |
43 | #include <apt-pkg/metaindex.h> | |
44 | ||
45 | #include <apti18n.h> | |
46 | ||
47 | #include <apt-private/private-list.h> | |
48 | #include <apt-private/private-search.h> | |
49 | #include <apt-private/private-install.h> | |
50 | #include <apt-private/private-output.h> | |
51 | #include <apt-private/private-update.h> | |
52 | #include <apt-private/private-cmndline.h> | |
53 | #include <apt-private/private-moo.h> | |
54 | #include <apt-private/private-upgrade.h> | |
55 | #include <apt-private/private-show.h> | |
56 | #include <apt-private/private-main.h> | |
cfacba52 | 57 | #include <apt-private/private-utils.h> |
b9179170 MV |
58 | /*}}}*/ |
59 | ||
cfacba52 MV |
60 | // EditSource - EditSourcesList /*{{{*/ |
61 | // --------------------------------------------------------------------- | |
62 | bool EditSources(CommandLine &CmdL) | |
63 | { | |
64 | // FIXME: suport CmdL.FileList to specify sources.list.d files | |
65 | ||
66 | std::string sourceslist = _config->FindFile("Dir::Etc::sourcelist"); | |
67 | ||
68 | // FIXME: take hash before, | |
69 | // when changed display message to apt update | |
d2524a53 MV |
70 | bool res; |
71 | pkgSourceList sl; | |
cfacba52 | 72 | |
d2524a53 MV |
73 | do { |
74 | EditFileInSensibleEditor(sourceslist); | |
75 | _error->PushToStack(); | |
76 | res = sl.Read(sourceslist); | |
77 | if (!res) { | |
78 | std::string outs; | |
79 | strprintf(outs, _("Failed to parse %s. Edit again? "), | |
80 | sourceslist.c_str()); | |
81 | std::cout << outs; | |
82 | res = !YnPrompt(true); | |
83 | } | |
84 | _error->RevertToStack(); | |
85 | } while (res == false); | |
cfacba52 MV |
86 | |
87 | return true; | |
88 | } | |
89 | /*}}}*/ | |
90 | ||
91 | ||
b9179170 MV |
92 | bool ShowHelp(CommandLine &CmdL) |
93 | { | |
94 | ioprintf(c1out,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION, | |
95 | COMMON_ARCH,__DATE__,__TIME__); | |
96 | ||
97 | // FIXME: generate from CommandLine | |
98 | c1out << | |
99 | _("Usage: apt [options] command\n" | |
100 | "\n" | |
101 | "CLI for apt.\n" | |
102 | "Commands: \n" | |
103 | " list - list packages based on package names\n" | |
104 | " search - search in package descriptions\n" | |
105 | " show - show package details\n" | |
106 | "\n" | |
107 | " update - update list of available packages\n" | |
108 | " install - install packages\n" | |
109 | " upgrade - upgrade the systems packages\n" | |
cfacba52 MV |
110 | "\n" |
111 | " edit-sources - edit the source information file\n" | |
b9179170 MV |
112 | ); |
113 | ||
114 | return true; | |
115 | } | |
116 | ||
117 | int main(int argc, const char *argv[]) /*{{{*/ | |
118 | { | |
119 | CommandLine::Dispatch Cmds[] = {{"list",&List}, | |
120 | {"search", &FullTextSearch}, | |
121 | {"show", &APT::Cmd::ShowPackage}, | |
122 | // needs root | |
123 | {"install",&DoInstall}, | |
124 | {"remove", &DoInstall}, | |
125 | {"update",&DoUpdate}, | |
126 | {"upgrade",&DoUpgradeWithAllowNewPackages}, | |
cfacba52 MV |
127 | // misc |
128 | {"edit-sources",&EditSources}, | |
b9179170 MV |
129 | // helper |
130 | {"moo",&DoMoo}, | |
131 | {"help",&ShowHelp}, | |
132 | {0,0}}; | |
133 | ||
134 | std::vector<CommandLine::Args> Args = getCommandArgs("apt", CommandLine::GetCommand(Cmds, argc, argv)); | |
135 | ||
136 | if(!isatty(1)) | |
137 | { | |
138 | std::cerr << std::endl | |
139 | << "WARNING WARNING " | |
140 | << argv[0] | |
141 | << " is *NOT* intended for scripts " | |
142 | << "use at your own peril^Wrisk" | |
143 | << std::endl | |
144 | << std::endl; | |
145 | } | |
146 | ||
147 | InitOutput(); | |
148 | ||
149 | // Set up gettext support | |
150 | setlocale(LC_ALL,""); | |
151 | textdomain(PACKAGE); | |
152 | ||
153 | if(pkgInitConfig(*_config) == false) | |
154 | { | |
155 | _error->DumpErrors(); | |
156 | return 100; | |
157 | } | |
158 | ||
159 | // FIXME: move into a new libprivate/private-install.cc:Install() | |
160 | _config->Set("DPkgPM::Progress", "1"); | |
161 | _config->Set("Apt::Color", "1"); | |
162 | ||
163 | // Parse the command line and initialize the package library | |
164 | CommandLine CmdL(Args.data(), _config); | |
165 | if (CmdL.Parse(argc, argv) == false || | |
166 | pkgInitSystem(*_config, _system) == false) | |
167 | { | |
168 | _error->DumpErrors(); | |
169 | return 100; | |
170 | } | |
171 | ||
172 | // See if the help should be shown | |
173 | if (_config->FindB("help") == true || | |
174 | _config->FindB("version") == true || | |
175 | CmdL.FileSize() == 0) | |
176 | { | |
177 | ShowHelp(CmdL); | |
178 | return 0; | |
179 | } | |
180 | ||
181 | // see if we are in simulate mode | |
182 | CheckSimulateMode(CmdL); | |
183 | ||
184 | // parse args | |
185 | CmdL.DispatchArg(Cmds); | |
186 | ||
187 | // Print any errors or warnings found during parsing | |
188 | bool const Errors = _error->PendingError(); | |
189 | if (_config->FindI("quiet",0) > 0) | |
190 | _error->DumpErrors(); | |
191 | else | |
192 | _error->DumpErrors(GlobalError::DEBUG); | |
193 | return Errors == true ? 100 : 0; | |
194 | } | |
195 | /*}}}*/ |