]> git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
Added locale.h for LC_ALL
[apt.git] / cmdline / apt-extracttemplates.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: apt-extracttemplates.cc,v 1.9 2002/02/15 03:40:00 jgg Exp $
4 /* ######################################################################
5
6 APT Extract Templates - Program to extract debconf config and template
7 files
8
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 /*{{{*/
16 #include <apt-pkg/init.h>
17 #include <apt-pkg/cmndline.h>
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>
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>
30 #include <apt-pkg/fileutl.h>
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <fstream>
37
38 #include <locale.h>
39 #include <config.h>
40 #include <apti18n.h>
41 #include "apt-extracttemplates.h"
42 /*}}}*/
43
44 #define TMPDIR "/tmp"
45
46 pkgCache *DebFile::Cache = 0;
47
48 // DebFile::DebFile - Construct the DebFile object /*{{{*/
49 // ---------------------------------------------------------------------
50 /* */
51 DebFile::DebFile(const char *debfile)
52 : File(debfile, FileFd::ReadOnly), Control(0), DepOp(0),
53 PreDepOp(0), Config(0), Template(0), Which(None)
54 {
55 }
56 /*}}}*/
57 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
58 // ---------------------------------------------------------------------
59 /* */
60 DebFile::~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 /* */
70 string DebFile::GetInstalledVer(const string &package)
71 {
72 pkgCache::PkgIterator Pkg = Cache->FindPkg(package);
73 if (Pkg.end() == false)
74 {
75 pkgCache::VerIterator V = Pkg.CurrentVer();
76 if (V.end() == false)
77 {
78 return V.VerStr();
79 }
80 }
81
82 return string();
83 }
84 /*}}}*/
85 // DebFile::Go - Start extracting a debian package /*{{{*/
86 // ---------------------------------------------------------------------
87 /* */
88 bool DebFile::Go()
89 {
90 ARArchive AR(File);
91 if (_error->PendingError() == true)
92 return false;
93
94 const ARArchive::Member *Member = AR.FindMember("control.tar.gz");
95 if (Member == 0)
96 return _error->Error(_("%s not a valid DEB package."),File.Name().c_str());
97
98 if (File.Seek(Member->Start) == false)
99 return false;
100 ExtractTar Tar(File, Member->Size);
101 return Tar.Go(*this);
102 }
103 /*}}}*/
104 // DebFile::DoItem examine element in package and mark /*{{{*/
105 // ---------------------------------------------------------------------
106 /* */
107 bool 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 {
137 // Ignore it
138 Fd = -1;
139 }
140 return true;
141 }
142 /*}}}*/
143 // DebFile::Process examine element in package and copy /*{{{*/
144 // ---------------------------------------------------------------------
145 /* */
146 bool 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 /* */
168 bool DebFile::ParseInfo()
169 {
170 if (Control == NULL) return false;
171
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 /* */
221 int ShowHelp(void)
222 {
223 ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
224 COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
225
226 if (_config->FindB("version") == true)
227 return 0;
228
229 cout <<
230 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
231 "\n"
232 "apt-extracttemplates is a tool to extract config and template info\n"
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;
241 }
242 /*}}}*/
243 // WriteFile - write the contents of the passed string to a file /*{{{*/
244 // ---------------------------------------------------------------------
245 /* */
246 string WriteFile(const char *prefix, const char *data)
247 {
248 char fn[512];
249 static int i;
250 snprintf(fn, sizeof(fn), "%s/%s.%u%d", _config->Find("APT::ExtractTemplates::TempDir", TMPDIR).c_str(), prefix, getpid(), i++);
251 FileFd f;
252 if (data == NULL)
253 data = "";
254
255 if (!f.Open(fn, FileFd::WriteTemp, 0600))
256 {
257 _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
258 return string();
259 }
260
261 f.Write(data, strlen(data));
262 f.Close();
263 return fn;
264 }
265 /*}}}*/
266 // WriteConfig - write out the config data from a debian package file /*{{{*/
267 // ---------------------------------------------------------------------
268 /* */
269 void WriteConfig(const DebFile &file)
270 {
271 string templatefile = WriteFile("template", file.Template);
272 string configscript = WriteFile("config", file.Config);
273
274 if (templatefile.empty() == true || configscript.empty() == true)
275 return;
276 cout << file.Package << " " << file.Version << " "
277 << templatefile << " " << configscript << endl;
278 }
279 /*}}}*/
280 // InitCache - initialize the package cache /*{{{*/
281 // ---------------------------------------------------------------------
282 /* */
283 bool Go(CommandLine &CmdL)
284 {
285 // Initialize the apt cache
286 MMap *Map = 0;
287 pkgSourceList List;
288 List.ReadMainList();
289 OpProgress Prog;
290 pkgMakeStatusCache(List,Prog,&Map,true);
291 DebFile::Cache = new pkgCache(Map);
292 if (_error->PendingError() == true)
293 return false;
294
295 // Find out what version of debconf is currently installed
296 string debconfver = DebFile::GetInstalledVer("debconf");
297 if (debconfver.empty() == true)
298 return _error->Error( _("Cannot get debconf version. Is debconf installed?"));
299
300 // Process each package passsed in
301 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
302 {
303 // Will pick up the errors later..
304 DebFile file(CmdL.FileList[I]);
305 if (file.Go() == false)
306 continue;
307
308 // Does the package have templates?
309 if (file.Template != 0 && file.ParseInfo() == true)
310 {
311 // Check to make sure debconf dependencies are
312 // satisfied
313 cout << "Check " << file.DepVer << ',' << debconfver << endl;
314 if (file.DepVer != "" &&
315 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
316 file.DepOp,file.DepVer.c_str()
317 ) == false)
318 continue;
319 if (file.PreDepVer != "" &&
320 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
321 file.PreDepOp,file.PreDepVer.c_str()
322 ) == false)
323 continue;
324
325 WriteConfig(file);
326 }
327 }
328
329
330 delete Map;
331 delete DebFile::Cache;
332
333 return !_error->PendingError();
334 }
335 /*}}}*/
336
337 int main(int argc, const char **argv)
338 {
339 CommandLine::Args Args[] = {
340 {'h',"help","help",0},
341 {'v',"version","version",0},
342 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
343 {'c',"config-file",0,CommandLine::ConfigFile},
344 {'o',"option",0,CommandLine::ArbItem},
345 {0,0,0,0}};
346
347 // Set up gettext support
348 setlocale(LC_ALL,"");
349 textdomain(PACKAGE);
350
351 // Parse the command line and initialize the package library
352 CommandLine CmdL(Args,_config);
353 if (pkgInitConfig(*_config) == false ||
354 CmdL.Parse(argc,argv) == false ||
355 pkgInitSystem(*_config,_system) == false)
356 {
357 _error->DumpErrors();
358 return 100;
359 }
360
361 // See if the help should be shown
362 if (_config->FindB("help") == true ||
363 CmdL.FileSize() == 0)
364 return ShowHelp();
365
366 Go(CmdL);
367
368 // Print any errors or warnings found during operation
369 if (_error->empty() == false)
370 {
371 // This goes to stderr..
372 bool Errors = _error->PendingError();
373 _error->DumpErrors();
374 return Errors == true?100:0;
375 }
376
377 return 0;
378 }