add basic "edit-sources" command
[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 #include <apt-private/private-utils.h>
58 /*}}}*/
59
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
70 // do syntax check after save (like visudo)
71
72 EditFileInSensibleEditor(sourceslist);
73
74 return true;
75 }
76 /*}}}*/
77
78
79 bool ShowHelp(CommandLine &CmdL)
80 {
81 ioprintf(c1out,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
82 COMMON_ARCH,__DATE__,__TIME__);
83
84 // FIXME: generate from CommandLine
85 c1out <<
86 _("Usage: apt [options] command\n"
87 "\n"
88 "CLI for apt.\n"
89 "Commands: \n"
90 " list - list packages based on package names\n"
91 " search - search in package descriptions\n"
92 " show - show package details\n"
93 "\n"
94 " update - update list of available packages\n"
95 " install - install packages\n"
96 " upgrade - upgrade the systems packages\n"
97 "\n"
98 " edit-sources - edit the source information file\n"
99 );
100
101 return true;
102 }
103
104 int main(int argc, const char *argv[]) /*{{{*/
105 {
106 CommandLine::Dispatch Cmds[] = {{"list",&List},
107 {"search", &FullTextSearch},
108 {"show", &APT::Cmd::ShowPackage},
109 // needs root
110 {"install",&DoInstall},
111 {"remove", &DoInstall},
112 {"update",&DoUpdate},
113 {"upgrade",&DoUpgradeWithAllowNewPackages},
114 // misc
115 {"edit-sources",&EditSources},
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 if(!isatty(1))
124 {
125 std::cerr << std::endl
126 << "WARNING WARNING "
127 << argv[0]
128 << " is *NOT* intended for scripts "
129 << "use at your own peril^Wrisk"
130 << std::endl
131 << std::endl;
132 }
133
134 InitOutput();
135
136 // Set up gettext support
137 setlocale(LC_ALL,"");
138 textdomain(PACKAGE);
139
140 if(pkgInitConfig(*_config) == false)
141 {
142 _error->DumpErrors();
143 return 100;
144 }
145
146 // FIXME: move into a new libprivate/private-install.cc:Install()
147 _config->Set("DPkgPM::Progress", "1");
148 _config->Set("Apt::Color", "1");
149
150 // Parse the command line and initialize the package library
151 CommandLine CmdL(Args.data(), _config);
152 if (CmdL.Parse(argc, argv) == false ||
153 pkgInitSystem(*_config, _system) == false)
154 {
155 _error->DumpErrors();
156 return 100;
157 }
158
159 // See if the help should be shown
160 if (_config->FindB("help") == true ||
161 _config->FindB("version") == true ||
162 CmdL.FileSize() == 0)
163 {
164 ShowHelp(CmdL);
165 return 0;
166 }
167
168 // see if we are in simulate mode
169 CheckSimulateMode(CmdL);
170
171 // parse args
172 CmdL.DispatchArg(Cmds);
173
174 // Print any errors or warnings found during parsing
175 bool const Errors = _error->PendingError();
176 if (_config->FindI("quiet",0) > 0)
177 _error->DumpErrors();
178 else
179 _error->DumpErrors(GlobalError::DEBUG);
180 return Errors == true ? 100 : 0;
181 }
182 /*}}}*/