Add -n synonym for --names-only for apt-cache.
[ntk/apt.git] / cmdline / apt-extracttemplates.cc
CommitLineData
234edfd0
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
259ca596 3// $Id: apt-extracttemplates.cc,v 1.12 2002/11/09 22:41:55 doogie Exp $
234edfd0
AL
4/* ######################################################################
5
6 APT Extract Templates - Program to extract debconf config and template
7 files
3fc8f685 8
234edfd0
AL
9 This is a simple program to extract config and template information
10 from Debian packages. It can be used to speed up the preconfiguration
11 process for debconf-enabled packages
12
13 ##################################################################### */
14 /*}}}*/
15// Include Files /*{{{*/
3fc8f685 16#include <apt-pkg/init.h>
5cbf1b49 17#include <apt-pkg/cmndline.h>
3fc8f685
AL
18#include <apt-pkg/pkgcache.h>
19#include <apt-pkg/configuration.h>
20#include <apt-pkg/progress.h>
21#include <apt-pkg/sourcelist.h>
22#include <apt-pkg/pkgcachegen.h>
23#include <apt-pkg/version.h>
234edfd0
AL
24#include <apt-pkg/tagfile.h>
25#include <apt-pkg/extracttar.h>
26#include <apt-pkg/arfile.h>
27#include <apt-pkg/deblistparser.h>
28#include <apt-pkg/error.h>
29#include <apt-pkg/strutl.h>
4decd43c 30#include <apt-pkg/fileutl.h>
3fc8f685 31
234edfd0
AL
32#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35#include <unistd.h>
7e726255 36#include <fstream>
234edfd0 37
233c2b66 38#include <locale.h>
234edfd0
AL
39#include <config.h>
40#include <apti18n.h>
41#include "apt-extracttemplates.h"
7e726255 42 /*}}}*/
234edfd0 43
4decd43c 44#define TMPDIR "/tmp"
3fc8f685 45
234edfd0
AL
46pkgCache *DebFile::Cache = 0;
47
48// DebFile::DebFile - Construct the DebFile object /*{{{*/
49// ---------------------------------------------------------------------
50/* */
51DebFile::DebFile(const char *debfile)
7e726255
AL
52 : File(debfile, FileFd::ReadOnly), Control(0), DepOp(0),
53 PreDepOp(0), Config(0), Template(0), Which(None)
3fc8f685 54{
3fc8f685 55}
234edfd0
AL
56 /*}}}*/
57// DebFile::~DebFile - Destruct the DebFile object /*{{{*/
58// ---------------------------------------------------------------------
59/* */
60DebFile::~DebFile()
61{
62 delete [] Control;
63 delete [] Config;
64 delete [] Template;
65}
66 /*}}}*/
67// DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
68// ---------------------------------------------------------------------
69/* */
7e726255 70string DebFile::GetInstalledVer(const string &package)
234edfd0 71{
234edfd0
AL
72 pkgCache::PkgIterator Pkg = Cache->FindPkg(package);
73 if (Pkg.end() == false)
74 {
75 pkgCache::VerIterator V = Pkg.CurrentVer();
7e726255 76 if (V.end() == false)
234edfd0 77 {
7e726255 78 return V.VerStr();
234edfd0
AL
79 }
80 }
3fc8f685 81
7e726255 82 return string();
234edfd0
AL
83}
84 /*}}}*/
85// DebFile::Go - Start extracting a debian package /*{{{*/
86// ---------------------------------------------------------------------
87/* */
88bool DebFile::Go()
89{
90 ARArchive AR(File);
7e726255
AL
91 if (_error->PendingError() == true)
92 return false;
93
234edfd0
AL
94 const ARArchive::Member *Member = AR.FindMember("control.tar.gz");
95 if (Member == 0)
7e726255 96 return _error->Error(_("%s not a valid DEB package."),File.Name().c_str());
234edfd0
AL
97
98 if (File.Seek(Member->Start) == false)
234edfd0 99 return false;
234edfd0
AL
100 ExtractTar Tar(File, Member->Size);
101 return Tar.Go(*this);
102}
103 /*}}}*/
104// DebFile::DoItem examine element in package and mark /*{{{*/
105// ---------------------------------------------------------------------
106/* */
107bool DebFile::DoItem(Item &I, int &Fd)
108{
109 if (strcmp(I.Name, "control") == 0)
110 {
111 delete [] Control;
112 Control = new char[I.Size+1];
113 Control[I.Size] = 0;
114 Which = IsControl;
115 ControlLen = I.Size;
116 // make it call the Process method below. this is so evil
117 Fd = -2;
118 }
119 else if (strcmp(I.Name, "config") == 0)
120 {
121 delete [] Config;
122 Config = new char[I.Size+1];
123 Config[I.Size] = 0;
124 Which = IsConfig;
125 Fd = -2;
126 }
127 else if (strcmp(I.Name, "templates") == 0)
128 {
129 delete [] Template;
130 Template = new char[I.Size+1];
131 Template[I.Size] = 0;
132 Which = IsTemplate;
133 Fd = -2;
134 }
135 else
136 {
7e726255 137 // Ignore it
234edfd0
AL
138 Fd = -1;
139 }
140 return true;
141}
142 /*}}}*/
143// DebFile::Process examine element in package and copy /*{{{*/
144// ---------------------------------------------------------------------
145/* */
146bool DebFile::Process(Item &I, const unsigned char *data,
147 unsigned long size, unsigned long pos)
148{
149 switch (Which)
150 {
151 case IsControl:
152 memcpy(Control + pos, data, size);
153 break;
154 case IsConfig:
155 memcpy(Config + pos, data, size);
156 break;
157 case IsTemplate:
158 memcpy(Template + pos, data, size);
159 break;
160 default: /* throw it away */ ;
161 }
162 return true;
163}
164 /*}}}*/
165// DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
166// ---------------------------------------------------------------------
167/* */
168bool DebFile::ParseInfo()
169{
170 if (Control == NULL) return false;
7e726255 171
234edfd0
AL
172 pkgTagSection Section;
173 Section.Scan(Control, ControlLen);
174
175 Package = Section.FindS("Package");
176 Version = GetInstalledVer(Package);
177
178 const char *Start, *Stop;
179 if (Section.Find("Depends", Start, Stop) == true)
180 {
181 while (1)
182 {
183 string P, V;
184 unsigned int Op;
185 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
186 if (Start == 0) return false;
187 if (P == "debconf")
188 {
189 DepVer = V;
190 DepOp = Op;
191 break;
192 }
193 if (Start == Stop) break;
194 }
195 }
196
197 if (Section.Find("Pre-Depends", Start, Stop) == true)
198 {
199 while (1)
200 {
201 string P, V;
202 unsigned int Op;
203 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
204 if (Start == 0) return false;
205 if (P == "debconf")
206 {
207 PreDepVer = V;
208 PreDepOp = Op;
209 break;
210 }
211 if (Start == Stop) break;
212 }
213 }
214
215 return true;
216}
217 /*}}}*/
218// ShowHelp - show a short help text /*{{{*/
219// ---------------------------------------------------------------------
220/* */
7e726255 221int ShowHelp(void)
234edfd0
AL
222{
223 ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
224 COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
225
7e726255
AL
226 if (_config->FindB("version") == true)
227 return 0;
5cbf1b49 228
7e726255 229 cout <<
234edfd0
AL
230 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
231 "\n"
232 "apt-extracttemplates is a tool to extract config and template info\n"
7e726255
AL
233 "from debian packages\n"
234 "\n"
235 "Options:\n"
236 " -h This help text\n"
237 " -t Set the temp dir\n"
238 " -c=? Read this configuration file\n"
239 " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n");
240 return 0;
234edfd0
AL
241}
242 /*}}}*/
243// WriteFile - write the contents of the passed string to a file /*{{{*/
244// ---------------------------------------------------------------------
245/* */
259ca596 246string WriteFile(const char *package, const char *prefix, const char *data)
3fc8f685
AL
247{
248 char fn[512];
249 static int i;
259ca596 250 snprintf(fn, sizeof(fn), "%s/%s.%s.%u%d", _config->Find("APT::ExtractTemplates::TempDir", TMPDIR).c_str(), package, prefix, getpid(), i++);
4decd43c
AL
251 FileFd f;
252 if (data == NULL)
253 data = "";
3fc8f685 254
4decd43c 255 if (!f.Open(fn, FileFd::WriteTemp, 0600))
7e726255
AL
256 {
257 _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
258 return string();
259 }
260
4decd43c
AL
261 f.Write(data, strlen(data));
262 f.Close();
7e726255 263 return fn;
3fc8f685 264}
234edfd0
AL
265 /*}}}*/
266// WriteConfig - write out the config data from a debian package file /*{{{*/
267// ---------------------------------------------------------------------
268/* */
269void WriteConfig(const DebFile &file)
3fc8f685 270{
259ca596
AL
271 string templatefile = WriteFile(file.package, "template", file.Template);
272 string configscript = WriteFile(file.package, "config", file.Config);
3fc8f685 273
7e726255 274 if (templatefile.empty() == true || configscript.empty() == true)
3fc8f685 275 return;
234edfd0
AL
276 cout << file.Package << " " << file.Version << " "
277 << templatefile << " " << configscript << endl;
234edfd0
AL
278}
279 /*}}}*/
280// InitCache - initialize the package cache /*{{{*/
281// ---------------------------------------------------------------------
282/* */
7e726255
AL
283bool Go(CommandLine &CmdL)
284{
3fc8f685 285 // Initialize the apt cache
7e726255 286 MMap *Map = 0;
3fc8f685
AL
287 pkgSourceList List;
288 List.ReadMainList();
289 OpProgress Prog;
290 pkgMakeStatusCache(List,Prog,&Map,true);
08945efa
AL
291 if (Map == 0)
292 return false;
7e726255
AL
293 DebFile::Cache = new pkgCache(Map);
294 if (_error->PendingError() == true)
295 return false;
5cbf1b49 296
234edfd0 297 // Find out what version of debconf is currently installed
7e726255
AL
298 string debconfver = DebFile::GetInstalledVer("debconf");
299 if (debconfver.empty() == true)
300 return _error->Error( _("Cannot get debconf version. Is debconf installed?"));
3fc8f685 301
234edfd0 302 // Process each package passsed in
5cbf1b49 303 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
3fc8f685 304 {
7e726255 305 // Will pick up the errors later..
5cbf1b49 306 DebFile file(CmdL.FileList[I]);
7e726255 307 if (file.Go() == false)
08945efa
AL
308 {
309 _error->Error("Prior errors apply to %s",CmdL.FileList[I]);
310 continue;
311 }
312
234edfd0 313 // Does the package have templates?
3fc8f685
AL
314 if (file.Template != 0 && file.ParseInfo() == true)
315 {
234edfd0
AL
316 // Check to make sure debconf dependencies are
317 // satisfied
f034a64a 318 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
234edfd0 319 if (file.DepVer != "" &&
418349f0
AL
320 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
321 file.DepOp,file.DepVer.c_str()
322 ) == false)
3fc8f685 323 continue;
234edfd0 324 if (file.PreDepVer != "" &&
418349f0
AL
325 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
326 file.PreDepOp,file.PreDepVer.c_str()
327 ) == false)
3fc8f685
AL
328 continue;
329
234edfd0 330 WriteConfig(file);
3fc8f685
AL
331 }
332 }
333
334
335 delete Map;
336 delete DebFile::Cache;
7e726255
AL
337
338 return !_error->PendingError();
339}
340 /*}}}*/
341
342int main(int argc, const char **argv)
343{
344 CommandLine::Args Args[] = {
345 {'h',"help","help",0},
346 {'v',"version","version",0},
347 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
348 {'c',"config-file",0,CommandLine::ConfigFile},
349 {'o',"option",0,CommandLine::ArbItem},
350 {0,0,0,0}};
67111687
AL
351
352 // Set up gettext support
353 setlocale(LC_ALL,"");
354 textdomain(PACKAGE);
355
7e726255
AL
356 // Parse the command line and initialize the package library
357 CommandLine CmdL(Args,_config);
358 if (pkgInitConfig(*_config) == false ||
359 CmdL.Parse(argc,argv) == false ||
360 pkgInitSystem(*_config,_system) == false)
361 {
362 _error->DumpErrors();
363 return 100;
364 }
365
366 // See if the help should be shown
367 if (_config->FindB("help") == true ||
368 CmdL.FileSize() == 0)
369 return ShowHelp();
370
371 Go(CmdL);
3fc8f685 372
7e726255
AL
373 // Print any errors or warnings found during operation
374 if (_error->empty() == false)
375 {
376 // This goes to stderr..
377 bool Errors = _error->PendingError();
378 _error->DumpErrors();
379 return Errors == true?100:0;
380 }
381
3fc8f685
AL
382 return 0;
383}