]> git.saurik.com Git - apt.git/blob - cmdline/apt.cc
4dc8266327260254fe9af548fc915438f10f4944
[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 "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 " install - install packages\n"
81 " upgrade - upgrade the systems packages\n"
82 "\n"
83 " edit-sources - edit the source information file\n"
84 );
85
86 return true;
87 }
88
89 // figure out what kind of upgrade the user wants
90 bool DoAptUpgrade(CommandLine &CmdL)
91 {
92 if (_config->FindB("Apt::Cmd::Dist-Upgrade"))
93 return DoDistUpgrade(CmdL);
94 else
95 return DoUpgradeWithAllowNewPackages(CmdL);
96 }
97
98 int main(int argc, const char *argv[]) /*{{{*/
99 {
100 CommandLine::Dispatch Cmds[] = {{"list",&List},
101 {"search", &FullTextSearch},
102 {"show", &APT::Cmd::ShowPackage},
103 // needs root
104 {"install",&DoInstall},
105 {"remove", &DoInstall},
106 {"update",&DoUpdate},
107 {"upgrade",&DoAptUpgrade},
108 // misc
109 {"edit-sources",&EditSources},
110 // helper
111 {"moo",&DoMoo},
112 {"help",&ShowHelp},
113 {0,0}};
114
115 std::vector<CommandLine::Args> Args = getCommandArgs("apt", CommandLine::GetCommand(Cmds, argc, argv));
116
117 if(!isatty(1))
118 {
119 std::cerr << std::endl
120 << "WARNING WARNING "
121 << argv[0]
122 << " is *NOT* intended for scripts "
123 << "use at your own peril^Wrisk"
124 << std::endl
125 << std::endl;
126 }
127
128 InitOutput();
129
130 // Set up gettext support
131 setlocale(LC_ALL,"");
132 textdomain(PACKAGE);
133
134 if(pkgInitConfig(*_config) == false)
135 {
136 _error->DumpErrors();
137 return 100;
138 }
139
140 // FIXME: move into a new libprivate/private-install.cc:Install()
141 _config->Set("DPkgPM::Progress", "1");
142 _config->Set("Apt::Color", "1");
143
144 // Parse the command line and initialize the package library
145 CommandLine CmdL(Args.data(), _config);
146 if (CmdL.Parse(argc, argv) == false ||
147 pkgInitSystem(*_config, _system) == false)
148 {
149 _error->DumpErrors();
150 return 100;
151 }
152
153 // See if the help should be shown
154 if (_config->FindB("help") == true ||
155 _config->FindB("version") == true ||
156 CmdL.FileSize() == 0)
157 {
158 ShowHelp(CmdL);
159 return 0;
160 }
161
162 // see if we are in simulate mode
163 CheckSimulateMode(CmdL);
164
165 // parse args
166 CmdL.DispatchArg(Cmds);
167
168 // Print any errors or warnings found during parsing
169 bool const Errors = _error->PendingError();
170 if (_config->FindI("quiet",0) > 0)
171 _error->DumpErrors();
172 else
173 _error->DumpErrors(GlobalError::DEBUG);
174 return Errors == true ? 100 : 0;
175 }
176 /*}}}*/