document list/search
[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
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>
57 /*}}}*/
58
59 bool ShowHelp(CommandLine &CmdL)
60 {
61 ioprintf(c1out,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
62 COMMON_ARCH,__DATE__,__TIME__);
63
64 // FIXME: generate from CommandLine
65 c1out <<
66 _("Usage: apt [options] command\n"
67 "\n"
68 "CLI for apt.\n"
69 "Commands: \n"
70 " list - list packages based on package names\n"
71 " search - search in package descriptions\n"
72 " show - show package details\n"
73 "\n"
74 " update - update list of available packages\n"
75 " upgrade - upgrade the systems packages\n"
76 "\n"
77 " install - install packages\n"
78 " remove - remove packages\n"
79 );
80
81 return true;
82 }
83
84 int main(int argc, const char *argv[]) /*{{{*/
85 {
86 CommandLine::Dispatch Cmds[] = {{"list",&List},
87 {"search", &FullTextSearch},
88 {"show", &APT::Cmd::ShowPackage},
89 // needs root
90 {"install",&DoInstall},
91 {"remove", &DoInstall},
92 {"update",&DoUpdate},
93 {"upgrade",&DoUpgradeWithAllowNewPackages},
94 // helper
95 {"moo",&DoMoo},
96 {"help",&ShowHelp},
97 {0,0}};
98
99 std::vector<CommandLine::Args> Args = getCommandArgs("apt", CommandLine::GetCommand(Cmds, argc, argv));
100
101 if(!isatty(1))
102 {
103 std::cerr << std::endl
104 << "WARNING WARNING "
105 << argv[0]
106 << " is *NOT* intended for scripts "
107 << "use at your own peril^Wrisk"
108 << std::endl
109 << std::endl;
110 }
111
112 InitOutput();
113
114 // Set up gettext support
115 setlocale(LC_ALL,"");
116 textdomain(PACKAGE);
117
118 if(pkgInitConfig(*_config) == false)
119 {
120 _error->DumpErrors();
121 return 100;
122 }
123
124 // FIXME: move into a new libprivate/private-install.cc:Install()
125 _config->Set("DPkgPM::Progress-Fancy", "1");
126 _config->Set("Apt::Color", "1");
127
128 // Parse the command line and initialize the package library
129 CommandLine CmdL(Args.data(), _config);
130 if (CmdL.Parse(argc, argv) == false ||
131 pkgInitSystem(*_config, _system) == false)
132 {
133 _error->DumpErrors();
134 return 100;
135 }
136
137 // See if the help should be shown
138 if (_config->FindB("help") == true ||
139 _config->FindB("version") == true ||
140 CmdL.FileSize() == 0)
141 {
142 ShowHelp(CmdL);
143 return 0;
144 }
145
146 // see if we are in simulate mode
147 CheckSimulateMode(CmdL);
148
149 // parse args
150 CmdL.DispatchArg(Cmds);
151
152 // Print any errors or warnings found during parsing
153 bool const Errors = _error->PendingError();
154 if (_config->FindI("quiet",0) > 0)
155 _error->DumpErrors();
156 else
157 _error->DumpErrors(GlobalError::DEBUG);
158 return Errors == true ? 100 : 0;
159 }
160 /*}}}*/