Merge remote-tracking branch 'mvo/feature/apt-show-nice' into debian/experimental...
[ntk/apt.git] / cmdline / apt.cc
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 #include <apt-pkg/hashes.h>
45
46 #include <apti18n.h>
47
48 #include <apt-private/private-list.h>
49 #include <apt-private/private-search.h>
50 #include <apt-private/private-install.h>
51 #include <apt-private/private-output.h>
52 #include <apt-private/private-update.h>
53 #include <apt-private/private-cmndline.h>
54 #include <apt-private/private-moo.h>
55 #include <apt-private/private-upgrade.h>
56 #include <apt-private/private-show.h>
57 #include <apt-private/private-main.h>
58 #include <apt-private/private-utils.h>
59 #include <apt-private/private-sources.h>
60 /*}}}*/
61
62
63
64 bool ShowHelp(CommandLine &CmdL)
65 {
66 ioprintf(c1out,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
67 COMMON_ARCH,__DATE__,__TIME__);
68
69 // FIXME: generate from CommandLine
70 c1out <<
71 _("Usage: apt [options] command\n"
72 "\n"
73 "CLI for apt.\n"
74 "Basic commands: \n"
75 " list - list packages based on package names\n"
76 " search - search in package descriptions\n"
77 " show - show package details\n"
78 "\n"
79 " update - update list of available packages\n"
80 "\n"
81 " install - install packages\n"
82 " remove - remove packages\n"
83 "\n"
84 " upgrade - upgrade the system by installing/upgrading packages\n"
85 "full-upgrade - upgrade the system by removing/installing/upgrading packages\n"
86 "\n"
87 " edit-sources - edit the source information file\n"
88 );
89
90 return true;
91 }
92
93 int main(int argc, const char *argv[]) /*{{{*/
94 {
95 CommandLine::Dispatch Cmds[] = {
96 // query
97 {"list",&List},
98 {"search", &FullTextSearch},
99 {"show", &APT::Cmd::ShowPackage},
100
101 // package stuff
102 {"install",&DoInstall},
103 {"remove", &DoInstall},
104 {"purge", &DoInstall},
105
106 // system wide stuff
107 {"update",&DoUpdate},
108 {"upgrade",&DoUpgrade},
109 {"full-upgrade",&DoDistUpgrade},
110 // for compat with muscle memory
111 {"dist-upgrade",&DoDistUpgrade},
112
113 // misc
114 {"edit-sources",&EditSources},
115
116 // helper
117 {"moo",&DoMoo},
118 {"help",&ShowHelp},
119 {0,0}};
120
121 std::vector<CommandLine::Args> Args = getCommandArgs("apt", CommandLine::GetCommand(Cmds, argc, argv));
122
123 InitOutput();
124
125 // Set up gettext support
126 setlocale(LC_ALL,"");
127 textdomain(PACKAGE);
128
129 if(pkgInitConfig(*_config) == false)
130 {
131 _error->DumpErrors();
132 return 100;
133 }
134
135 // some different defaults
136 _config->CndSet("DPkgPM::Progress", "1");
137 _config->CndSet("Apt::Color", "1");
138 _config->CndSet("APT::Get::Upgrade-Allow-New", true);
139
140 // Parse the command line and initialize the package library
141 CommandLine CmdL(Args.data(), _config);
142 if (CmdL.Parse(argc, argv) == false ||
143 pkgInitSystem(*_config, _system) == false)
144 {
145 _error->DumpErrors();
146 return 100;
147 }
148
149 if(!isatty(STDOUT_FILENO) &&
150 _config->FindB("Apt::Cmd::Disable-Script-Warning", false) == false)
151 {
152 std::cerr << std::endl
153 << "WARNING: " << argv[0] << " "
154 << "does not have a stable CLI interface yet. "
155 << "Use with caution in scripts."
156 << std::endl
157 << std::endl;
158 }
159 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
160 _config->Set("quiet","1");
161
162 // See if the help should be shown
163 if (_config->FindB("help") == true ||
164 _config->FindB("version") == true ||
165 CmdL.FileSize() == 0)
166 {
167 ShowHelp(CmdL);
168 return 0;
169 }
170
171 // see if we are in simulate mode
172 CheckSimulateMode(CmdL);
173
174 // parse args
175 CmdL.DispatchArg(Cmds);
176
177 // Print any errors or warnings found during parsing
178 bool const Errors = _error->PendingError();
179 if (_config->FindI("quiet",0) > 0)
180 _error->DumpErrors();
181 else
182 _error->DumpErrors(GlobalError::DEBUG);
183 return Errors == true ? 100 : 0;
184 }
185 /*}}}*/