]>
git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
c2b4b1a50e3e82a9a2903b9d3d38529ad8eab7ad
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-extracttemplates.cc,v 1.3 2001/02/25 05:25:29 tausq Exp $
4 /* ######################################################################
6 APT Extract Templates - Program to extract debconf config and template
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
13 ##################################################################### */
15 // Include Files /*{{{*/
16 #include <apt-pkg/init.h>
17 #include <apt-pkg/cmndline.h>
18 #include <apt-pkg/debversion.h>
19 #include <apt-pkg/pkgcache.h>
20 #include <apt-pkg/configuration.h>
21 #include <apt-pkg/progress.h>
22 #include <apt-pkg/sourcelist.h>
23 #include <apt-pkg/pkgcachegen.h>
24 #include <apt-pkg/version.h>
25 #include <apt-pkg/tagfile.h>
26 #include <apt-pkg/extracttar.h>
27 #include <apt-pkg/arfile.h>
28 #include <apt-pkg/deblistparser.h>
29 #include <apt-pkg/error.h>
30 #include <apt-pkg/strutl.h>
41 #include "apt-extracttemplates.h"
43 #define TMPDIR "/var/lib/debconf/"
45 pkgCache
*DebFile::Cache
= 0;
47 // DebFile::DebFile - Construct the DebFile object /*{{{*/
48 // ---------------------------------------------------------------------
50 DebFile::DebFile(const char *debfile
)
51 : File(debfile
, FileFd::ReadOnly
), Control(0), DepOp(0), PreDepOp(0),
52 Config(0), Template(0), Which(None
)
56 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
57 // ---------------------------------------------------------------------
66 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
67 // ---------------------------------------------------------------------
69 char *DebFile::GetInstalledVer(const string
&package
)
73 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(package
);
74 if (Pkg
.end() == false)
76 pkgCache::VerIterator V
= Pkg
.CurrentVer();
79 ver
= strdup(V
.VerStr());
86 // DebFile::Go - Start extracting a debian package /*{{{*/
87 // ---------------------------------------------------------------------
92 const ARArchive::Member
*Member
= AR
.FindMember("control.tar.gz");
95 fprintf(stderr
, _("This is not a valid DEB package.\n"));
99 if (File
.Seek(Member
->Start
) == false)
104 ExtractTar
Tar(File
, Member
->Size
);
105 return Tar
.Go(*this);
108 // DebFile::DoItem examine element in package and mark /*{{{*/
109 // ---------------------------------------------------------------------
111 bool DebFile::DoItem(Item
&I
, int &Fd
)
113 if (strcmp(I
.Name
, "control") == 0)
116 Control
= new char[I
.Size
+1];
120 // make it call the Process method below. this is so evil
123 else if (strcmp(I
.Name
, "config") == 0)
126 Config
= new char[I
.Size
+1];
131 else if (strcmp(I
.Name
, "templates") == 0)
134 Template
= new char[I
.Size
+1];
135 Template
[I
.Size
] = 0;
146 // DebFile::Process examine element in package and copy /*{{{*/
147 // ---------------------------------------------------------------------
149 bool DebFile::Process(Item
&I
, const unsigned char *data
,
150 unsigned long size
, unsigned long pos
)
155 memcpy(Control
+ pos
, data
, size
);
158 memcpy(Config
+ pos
, data
, size
);
161 memcpy(Template
+ pos
, data
, size
);
163 default: /* throw it away */ ;
168 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
169 // ---------------------------------------------------------------------
171 bool DebFile::ParseInfo()
173 if (Control
== NULL
) return false;
174 pkgTagSection Section
;
175 Section
.Scan(Control
, ControlLen
);
177 Package
= Section
.FindS("Package");
178 Version
= GetInstalledVer(Package
);
180 const char *Start
, *Stop
;
181 if (Section
.Find("Depends", Start
, Stop
) == true)
187 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
188 if (Start
== 0) return false;
195 if (Start
== Stop
) break;
199 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
205 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
206 if (Start
== 0) return false;
213 if (Start
== Stop
) break;
220 // ShowHelp - show a short help text /*{{{*/
221 // ---------------------------------------------------------------------
225 ioprintf(cout
,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE
,VERSION
,
226 COMMON_OS
,COMMON_CPU
,__DATE__
,__TIME__
);
228 if (_config
->FindB("version") == true) return;
231 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
233 "apt-extracttemplates is a tool to extract config and template info\n"
234 "from debian packages\n"));
238 // WriteFile - write the contents of the passed string to a file /*{{{*/
239 // ---------------------------------------------------------------------
241 char *WriteFile(const char *prefix
, const char *data
)
245 snprintf(fn
, sizeof(fn
), "%s%s.%u%d", _config
->Find("APT::ExtractTemplates::TempDir", TMPDIR
).c_str(), prefix
, getpid(), i
++);
248 if (!ofs
) return NULL
;
249 ofs
<< (data
? data
: "");
254 // WriteConfig - write out the config data from a debian package file /*{{{*/
255 // ---------------------------------------------------------------------
257 void WriteConfig(const DebFile
&file
)
259 char *templatefile
= WriteFile("template", file
.Template
);
260 char *configscript
= WriteFile("config", file
.Config
);
262 if (templatefile
== 0 || configscript
== 0)
264 fprintf(stderr
, _("Cannot write config script or templates\n"));
267 cout
<< file
.Package
<< " " << file
.Version
<< " "
268 << templatefile
<< " " << configscript
<< endl
;
274 // InitCache - initialize the package cache /*{{{*/
275 // ---------------------------------------------------------------------
277 int InitCache(MMap
*&Map
, pkgCache
*&Cache
)
279 // Initialize the apt cache
280 if (pkgInitConfig(*_config
) == false || pkgInitSystem(*_config
, _system
) == false)
282 _error
->DumpErrors();
288 pkgMakeStatusCache(List
,Prog
,&Map
,true);
289 Cache
= new pkgCache(Map
);
294 int main(int argc
, const char **argv
)
297 const char *debconfver
= NULL
;
299 CommandLine::Args Args
[] = {
300 {'h',"help","help",0},
301 {'v',"version","version",0},
302 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
303 {'c',"config-file",0,CommandLine::ConfigFile
},
304 {'o',"option",0,CommandLine::ArbItem
},
307 // Initialize the package cache
308 if (InitCache(Map
, DebFile::Cache
) < 0 || Map
== 0 || DebFile::Cache
== 0)
310 fprintf(stderr
, _("Cannot initialize APT cache\n"));
314 // Parse the command line
315 CommandLine
CmdL(Args
,_config
);
316 if (CmdL
.Parse(argc
,argv
) == false)
318 fprintf(stderr
, _("Cannot parse commandline options\n"));
322 // See if the help should be shown
323 if (_config
->FindB("help") == true || CmdL
.FileSize() == 0)
329 // Find out what version of debconf is currently installed
330 if ((debconfver
= DebFile::GetInstalledVer("debconf")) == NULL
)
332 fprintf(stderr
, _("Cannot get debconf version. Is debconf installed?\n"));
336 // Process each package passsed in
337 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
339 DebFile
file(CmdL
.FileList
[I
]);
340 if (file
.Go() == false)
342 fprintf(stderr
, _("Cannot read %s\n"), CmdL
.FileList
[I
]);
345 // Does the package have templates?
346 if (file
.Template
!= 0 && file
.ParseInfo() == true)
348 // Check to make sure debconf dependencies are
350 if (file
.DepVer
!= "" &&
351 DebFile::Cache
->VS
->CheckDep(file
.DepVer
.c_str(),
352 file
.DepOp
, debconfver
) == false)
354 if (file
.PreDepVer
!= "" &&
355 DebFile::Cache
->VS
->CheckDep(file
.PreDepVer
.c_str(),
356 file
.PreDepOp
, debconfver
) == false)
365 delete DebFile::Cache
;