From 234edfd074bd8a356494d5d8df8774cf1b096fd8 Mon Sep 17 00:00:00 2001 From: Arch Librarian Date: Mon, 20 Sep 2004 16:56:45 +0000 Subject: apt-extracttemplates cleanup Author: tausq Date: 2001-02-25 04:53:59 GMT apt-extracttemplates cleanup --- cmdline/apt-extracttemplates.cc | 320 +++++++++++++++++++++++++++++++++------- cmdline/apt-extracttemplates.h | 48 ++++++ cmdline/debfile.cc | 304 -------------------------------------- cmdline/debfile.h | 45 ------ cmdline/makefile | 2 +- doc/apt-extracttemplates.1.sgml | 60 ++++++++ doc/makefile | 3 +- 7 files changed, 378 insertions(+), 404 deletions(-) create mode 100644 cmdline/apt-extracttemplates.h delete mode 100644 cmdline/debfile.cc delete mode 100644 cmdline/debfile.h create mode 100644 doc/apt-extracttemplates.1.sgml diff --git a/cmdline/apt-extracttemplates.cc b/cmdline/apt-extracttemplates.cc index b7af03005..ecba44ade 100644 --- a/cmdline/apt-extracttemplates.cc +++ b/cmdline/apt-extracttemplates.cc @@ -1,12 +1,18 @@ -#include -#include -#include -#include -#define _GNU_SOURCE -#include -#include -#include +// -*- mode: cpp; mode: fold -*- +// Description /*{{{*/ +// $Id: apt-extracttemplates.cc,v 1.2 2001/02/25 04:53:59 tausq Exp $ +/* ###################################################################### + + APT Extract Templates - Program to extract debconf config and template + files + This is a simple program to extract config and template information + from Debian packages. It can be used to speed up the preconfiguration + process for debconf-enabled packages + + ##################################################################### */ + /*}}}*/ +// Include Files /*{{{*/ #include #if APT_PKG_MAJOR >= 3 #define APT_COMPATIBILITY 986 @@ -18,65 +24,274 @@ #include #include #include -#include "debfile.h" +#include +#include +#include +#include +#include +#include -#define TMPDIR "/var/lib/debconf/" -#define STR(x) (x ? x : "") -//#define TMPDIR "tmp/" +#include +#include +#include +#include +#define _GNU_SOURCE +#include +#include +#include + +#include +#include +#include "apt-extracttemplates.h" + +#define TMPDIR "/var/lib/debconf/" -void help(void) +pkgCache *DebFile::Cache = 0; + +// DebFile::DebFile - Construct the DebFile object /*{{{*/ +// --------------------------------------------------------------------- +/* */ +DebFile::DebFile(const char *debfile) + : File(debfile, FileFd::ReadOnly), Control(0), DepOp(0), PreDepOp(0), + Config(0), Template(0), Which(None) { - fprintf(stderr, "apt-extracttemplates deb [deb]\n"); - exit(0); } + /*}}}*/ +// DebFile::~DebFile - Destruct the DebFile object /*{{{*/ +// --------------------------------------------------------------------- +/* */ +DebFile::~DebFile() +{ + delete [] Control; + delete [] Config; + delete [] Template; +} + /*}}}*/ +// DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/ +// --------------------------------------------------------------------- +/* */ +char *DebFile::GetInstalledVer(const string &package) +{ + char *ver = 0; + + pkgCache::PkgIterator Pkg = Cache->FindPkg(package); + if (Pkg.end() == false) + { + pkgCache::VerIterator V = Pkg.CurrentVer(); + if (V.end() == false) + { + ver = strdup(V.VerStr()); + } + } -char *writefile(const char *prefix, const char *data) + return ver; +} + /*}}}*/ +// DebFile::Go - Start extracting a debian package /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool DebFile::Go() +{ + ARArchive AR(File); + const ARArchive::Member *Member = AR.FindMember("control.tar.gz"); + if (Member == 0) + { + fprintf(stderr, _("This is not a valid DEB package.\n")); + return false; + } + + if (File.Seek(Member->Start) == false) + { + return false; + } + + ExtractTar Tar(File, Member->Size); + return Tar.Go(*this); +} + /*}}}*/ +// DebFile::DoItem examine element in package and mark /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool DebFile::DoItem(Item &I, int &Fd) +{ + if (strcmp(I.Name, "control") == 0) + { + delete [] Control; + Control = new char[I.Size+1]; + Control[I.Size] = 0; + Which = IsControl; + ControlLen = I.Size; + // make it call the Process method below. this is so evil + Fd = -2; + } + else if (strcmp(I.Name, "config") == 0) + { + delete [] Config; + Config = new char[I.Size+1]; + Config[I.Size] = 0; + Which = IsConfig; + Fd = -2; + } + else if (strcmp(I.Name, "templates") == 0) + { + delete [] Template; + Template = new char[I.Size+1]; + Template[I.Size] = 0; + Which = IsTemplate; + Fd = -2; + } + else + { + Fd = -1; + } + return true; +} + /*}}}*/ +// DebFile::Process examine element in package and copy /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool DebFile::Process(Item &I, const unsigned char *data, + unsigned long size, unsigned long pos) +{ + switch (Which) + { + case IsControl: + memcpy(Control + pos, data, size); + break; + case IsConfig: + memcpy(Config + pos, data, size); + break; + case IsTemplate: + memcpy(Template + pos, data, size); + break; + default: /* throw it away */ ; + } + return true; +} + /*}}}*/ +// DebFile::ParseInfo - Parse control file for dependency info /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool DebFile::ParseInfo() +{ + if (Control == NULL) return false; + pkgTagSection Section; + Section.Scan(Control, ControlLen); + + Package = Section.FindS("Package"); + Version = GetInstalledVer(Package); + + const char *Start, *Stop; + if (Section.Find("Depends", Start, Stop) == true) + { + while (1) + { + string P, V; + unsigned int Op; + Start = debListParser::ParseDepends(Start, Stop, P, V, Op); + if (Start == 0) return false; + if (P == "debconf") + { + DepVer = V; + DepOp = Op; + break; + } + if (Start == Stop) break; + } + } + + if (Section.Find("Pre-Depends", Start, Stop) == true) + { + while (1) + { + string P, V; + unsigned int Op; + Start = debListParser::ParseDepends(Start, Stop, P, V, Op); + if (Start == 0) return false; + if (P == "debconf") + { + PreDepVer = V; + PreDepOp = Op; + break; + } + if (Start == Stop) break; + } + } + + return true; +} + /*}}}*/ +// ShowHelp - show a short help text /*{{{*/ +// --------------------------------------------------------------------- +/* */ +void ShowHelp(void) +{ + ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION, + COMMON_OS,COMMON_CPU,__DATE__,__TIME__); + + fprintf(stderr, + _("Usage: apt-extracttemplates file1 [file2 ...]\n" + "\n" + "apt-extracttemplates is a tool to extract config and template info\n" + "from debian packages\n")); + exit(0); +} + /*}}}*/ +// WriteFile - write the contents of the passed string to a file /*{{{*/ +// --------------------------------------------------------------------- +/* */ +char *WriteFile(const char *prefix, const char *data) { char fn[512]; static int i; snprintf(fn, sizeof(fn), "%s%s.%u%d", TMPDIR, prefix, getpid(), i++); - if (data == NULL) data = ""; - ofstream ofs(fn); if (!ofs) return NULL; - ofs << data; + ofs << (data ? data : ""); ofs.close(); return strdup(fn); } - -void writeconfig(const DebFile &file) + /*}}}*/ +// WriteConfig - write out the config data from a debian package file /*{{{*/ +// --------------------------------------------------------------------- +/* */ +void WriteConfig(const DebFile &file) { - char *templatefile = writefile("template", file.Template); - char *configscript = writefile("config", file.Config); + char *templatefile = WriteFile("template", file.Template); + char *configscript = WriteFile("config", file.Config); if (templatefile == 0 || configscript == 0) { - fprintf(stderr, "Cannot write config script or templates\n"); + fprintf(stderr, _("Cannot write config script or templates\n")); return; } - printf("%s %s %s %s\n", - STR(file.Package), // Package - STR(file.Version), // Version - templatefile, // Template - configscript // Config - ); -} + cout << file.Package << " " << file.Version << " " + << templatefile << " " << configscript << endl; -void init(MMap *&Map, pkgCache *&Cache) + free(templatefile); + free(configscript); +} + /*}}}*/ +// InitCache - initialize the package cache /*{{{*/ +// --------------------------------------------------------------------- +/* */ +int InitCache(MMap *&Map, pkgCache *&Cache) { // Initialize the apt cache if (pkgInitConfig(*_config) == false || pkgInitSystem(*_config, _system) == false) { - fprintf(stderr, "Cannot initialize apt cache\n"); - return; + _error->DumpErrors(); + return -1; } pkgSourceList List; List.ReadMainList(); OpProgress Prog; pkgMakeStatusCache(List,Prog,&Map,true); Cache = new pkgCache(Map); + return 0; } + /*}}}*/ int main(int argc, char **argv, char **env) { @@ -86,51 +301,50 @@ int main(int argc, char **argv, char **env) MMap *Map = 0; const char *debconfver = NULL; - init(Map, DebFile::Cache); - if (Map == 0 || DebFile::Cache == 0) + // Initialize the package cache + if (InitCache(Map, DebFile::Cache) < 0 || Map == 0 || DebFile::Cache == 0) { - fprintf(stderr, "Cannot initialize APT cache\n"); - return 1; + fprintf(stderr, _("Cannot initialize APT cache\n")); + return 100; } - debconfver = DebFile::GetInstalledVer("debconf"); - - if (debconfver == NULL) + // Find out what version of debconf is currently installed + if ((debconfver = DebFile::GetInstalledVer("debconf")) == NULL) { - fprintf(stderr, "Cannot get debconf version. Is debconf installed?\n"); + fprintf(stderr, _("Cannot get debconf version. Is debconf installed?\n")); return 1; } + // Process each package passsed in numdebs = argc - 1; debs = new char *[numdebs]; memcpy(debs, &argv[1], sizeof(char *) * numdebs); - if (numdebs < 1) - { - fprintf(stderr, "apt-extracttemplates foo.deb [...]\n"); - return 0; - } + if (numdebs < 1) ShowHelp(); for (idx = 0; idx < numdebs; idx++) { DebFile file(debs[idx]); if (file.Go() == false) { - fprintf(stderr, "Cannot read %s\n", debs[idx]); + fprintf(stderr, _("Cannot read %s\n"), debs[idx]); continue; } + // Does the package have templates? if (file.Template != 0 && file.ParseInfo() == true) { - if (file.DepVer != 0 && *file.DepVer != 0 && - pkgCheckDep(file.DepVer, + // Check to make sure debconf dependencies are + // satisfied + if (file.DepVer != "" && + pkgCheckDep(file.DepVer.c_str(), debconfver, file.DepOp) == false) continue; - if (file.PreDepVer != 0 && *file.PreDepVer != 0 && - pkgCheckDep(file.PreDepVer, + if (file.PreDepVer != "" && + pkgCheckDep(file.PreDepVer.c_str(), debconfver, file.PreDepOp) == false) continue; - writeconfig(file); + WriteConfig(file); } } diff --git a/cmdline/apt-extracttemplates.h b/cmdline/apt-extracttemplates.h new file mode 100644 index 000000000..0b581604b --- /dev/null +++ b/cmdline/apt-extracttemplates.h @@ -0,0 +1,48 @@ +// -*- mode: cpp; mode: fold -*- +// Description /*{{{*/ +// $Id: apt-extracttemplates.h,v 1.1 2001/02/25 04:53:59 tausq Exp $ +/* ###################################################################### + + apt-extracttemplate - tool to extract template and config data + + ##################################################################### */ + /*}}}*/ +#ifndef _APTEXTRACTTEMPLATE_H_ +#define _APTEXTRACTTEMPLATE_H_ + +#include +#include +#include + +class DebFile : public pkgDirStream +{ + FileFd File; + unsigned long Size; + char *Control; + unsigned long ControlLen; + +public: + DebFile(const char *FileName); + ~DebFile(); + bool DoItem(Item &I, int &fd); + bool Process(pkgDirStream::Item &I, const unsigned char *data, + unsigned long size, unsigned long pos); + + bool Go(); + bool ParseInfo(); + + static char *GetInstalledVer(const string &package); + + string Package; + string Version; + string DepVer, PreDepVer; + unsigned int DepOp, PreDepOp; + + char *Config; + char *Template; + + static pkgCache *Cache; + enum { None, IsControl, IsConfig, IsTemplate } Which; +}; + +#endif diff --git a/cmdline/debfile.cc b/cmdline/debfile.cc deleted file mode 100644 index 0aa246226..000000000 --- a/cmdline/debfile.cc +++ /dev/null @@ -1,304 +0,0 @@ -#include -#include -#include -#include -#include - -#include "debfile.h" - -pkgCache *DebFile::Cache = 0; - -DebFile::DebFile(const char *debfile) - : File(debfile, FileFd::ReadOnly), Control(0), Package(0), Version(0), DepVer(0), PreDepVer(0), DepOp(0), PreDepOp(0), Config(0), Template(0), Which(None) -{ -} - -DebFile::~DebFile() -{ - delete [] Control; - delete [] Config; - delete [] Template; -} - -char *DebFile::GetInstalledVer(const char *package) -{ - char *ver = 0; - - pkgCache::PkgIterator Pkg = Cache->FindPkg(package); - if (Pkg.end() == false) - { - pkgCache::VerIterator V = Pkg.CurrentVer(); - if (V.end() == false) - { - ver = strdup(V.VerStr()); - } - } - - return ver; -} - -bool DebFile::Go() -{ - ARArchive AR(File); - const ARArchive::Member *Member = AR.FindMember("control.tar.gz"); - if (Member == 0) - { - fprintf(stderr, "This is not a valid DEB archive.\n"); - return false; - } - - if (File.Seek(Member->Start) == false) - { - return false; - } - - ExtractTar Tar(File, Member->Size); - return Tar.Go(*this); -} - -bool DebFile::DoItem(Item &I, int &Fd) -{ - if (strcmp(I.Name, "control") == 0) - { - delete [] Control; - Control = new char[I.Size+1]; - Control[I.Size] = 0; - Which = IsControl; - ControlLen = I.Size; - // make it call the Process method below. this is so evil - Fd = -2; - } - else if (strcmp(I.Name, "config") == 0) - { - delete [] Config; - Config = new char[I.Size+1]; - Config[I.Size] = 0; - Which = IsConfig; - Fd = -2; - } - else if (strcmp(I.Name, "templates") == 0) - { - delete [] Template; - Template = new char[I.Size+1]; - Template[I.Size] = 0; - Which = IsTemplate; - Fd = -2; - } - else - { - Fd = -1; - } - return true; -} - -bool DebFile::Process(Item &I, const unsigned char *data, - unsigned long size, unsigned long pos) -{ - switch (Which) - { - case IsControl: - memcpy(Control + pos, data, size); - break; - case IsConfig: - memcpy(Config + pos, data, size); - break; - case IsTemplate: - memcpy(Template + pos, data, size); - break; - default: /* throw it away */ ; - } - return true; -} - -bool DebFile::ParseInfo() -{ - if (Control == NULL) return false; - pkgTagSection Section; - Section.Scan(Control, ControlLen); - - const char *pkgtmp = Section.FindS("Package").c_str(); - Package = CopyString(pkgtmp, strlen(pkgtmp)); - Version = GetInstalledVer(Package); - - const char *Start, *Stop; - if (Section.Find("Depends", Start, Stop) == true) - { - while (1) - { - char *P = 0, *V = 0; - unsigned int Op; - Start = ParseDepends(Start, Stop, P, V, Op); - if (Start == 0) return false; - if (strcmp(P, "debconf") == 0) - { - DepVer = V; - DepOp = Op; - delete[] P; - break; - } - else - { - delete[] P; - delete[] V; - } - if (Start == Stop) break; - } - } - - if (Section.Find("Pre-Depends", Start, Stop) == true) - { - while (1) - { - char *P = 0, *V = 0; - unsigned int Op; - Start = ParseDepends(Start, Stop, P, V, Op); - if (Start == 0) return false; - if (strcmp(P, "debconf") == 0) - { - PreDepVer = V; - PreDepOp = Op; - delete[] P; - break; - } - else - { - delete[] P; - delete[] V; - } - if (Start == Stop) break; - } - } - - return true; -} - -char *DebFile::CopyString(const char *start, unsigned int len) -{ - char *s = new char[len + 1]; - s[len] = 0; - memcpy(s, start, len); - return s; -} - -const char *DebFile::ParseDepends(const char *Start,const char *Stop, - char *&Package, char *&Ver, - unsigned int &Op) -{ - // Strip off leading space - for (;Start != Stop && isspace(*Start) != 0; Start++); - - // Parse off the package name - const char *I = Start; - for (;I != Stop && isspace(*I) == 0 && *I != '(' && *I != ')' && - *I != ',' && *I != '|'; I++); - - // Malformed, no '(' - if (I != Stop && *I == ')') - return 0; - - if (I == Start) - return 0; - - // Stash the package name - Package = CopyString(Start, I - Start); - - // Skip white space to the '(' - for (;I != Stop && isspace(*I) != 0 ; I++); - - // Parse a version - if (I != Stop && *I == '(') - { - // Skip the '(' - for (I++; I != Stop && isspace(*I) != 0 ; I++); - if (I + 3 >= Stop) - return 0; - - // Determine the operator - switch (*I) - { - case '<': - I++; - if (*I == '=') - { - I++; - Op = pkgCache::Dep::LessEq; - break; - } - - if (*I == '<') - { - I++; - Op = pkgCache::Dep::Less; - break; - } - - // < is the same as <= and << is really Cs < for some reason - Op = pkgCache::Dep::LessEq; - break; - - case '>': - I++; - if (*I == '=') - { - I++; - Op = pkgCache::Dep::GreaterEq; - break; - } - - if (*I == '>') - { - I++; - Op = pkgCache::Dep::Greater; - break; - } - - // > is the same as >= and >> is really Cs > for some reason - Op = pkgCache::Dep::GreaterEq; - break; - - case '=': - Op = pkgCache::Dep::Equals; - I++; - break; - - // HACK around bad package definitions - default: - Op = pkgCache::Dep::Equals; - break; - } - - // Skip whitespace - for (;I != Stop && isspace(*I) != 0; I++); - Start = I; - for (;I != Stop && *I != ')'; I++); - if (I == Stop || Start == I) - return 0; - - // Skip trailing whitespace - const char *End = I; - for (; End > Start && isspace(End[-1]); End--); - - Ver = CopyString(Start, End - Start); - I++; - } - else - { - Ver = CopyString("", 0); - Op = pkgCache::Dep::NoOp; - } - - // Skip whitespace - for (;I != Stop && isspace(*I) != 0; I++); - - if (I != Stop && *I == '|') - Op |= pkgCache::Dep::Or; - - if (I == Stop || *I == ',' || *I == '|') - { - if (I != Stop) - for (I++; I != Stop && isspace(*I) != 0; I++); - return I; - } - - return 0; -} diff --git a/cmdline/debfile.h b/cmdline/debfile.h deleted file mode 100644 index b2db3c80b..000000000 --- a/cmdline/debfile.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef _debfile_H -#define _debfile_H - -#include -#include -#include - -class DebFile : public pkgDirStream -{ - const char *ParseDepends(const char *Start,const char *Stop, - char *&Package, char *&Ver, - unsigned int &Op); - - char *CopyString(const char *start, unsigned int len); - - FileFd File; - unsigned long Size; - char *Control; - unsigned long ControlLen; - -public: - DebFile(const char *FileName); - ~DebFile(); - bool DoItem(Item &I, int &fd); - bool Process(pkgDirStream::Item &I, const unsigned char *data, - unsigned long size, unsigned long pos); - - bool Go(); - bool ParseInfo(); - - static char *GetInstalledVer(const char *package); - - char *Package; - char *Version; - char *DepVer, *PreDepVer; - unsigned int DepOp, PreDepOp; - - char *Config; - char *Template; - - static pkgCache *Cache; - enum { None, IsControl, IsConfig, IsTemplate } Which; -}; - -#endif diff --git a/cmdline/makefile b/cmdline/makefile index 0f19d8e59..0c34c1ff9 100644 --- a/cmdline/makefile +++ b/cmdline/makefile @@ -44,5 +44,5 @@ include $(PROGRAM_H) PROGRAM=apt-extracttemplates SLIBS = -lapt-pkg -lapt-inst LIB_MAKES = apt-pkg/makefile -SOURCE = apt-extracttemplates.cc debfile.cc +SOURCE = apt-extracttemplates.cc include $(PROGRAM_H) diff --git a/doc/apt-extracttemplates.1.sgml b/doc/apt-extracttemplates.1.sgml new file mode 100644 index 000000000..6b062ccc9 --- /dev/null +++ b/doc/apt-extracttemplates.1.sgml @@ -0,0 +1,60 @@ + + +%aptent; + +]> + + + &apt-docinfo; + + + apt-extracttemplates + 1 + + + + + apt-extracttemplates + Utility to extract DebConf config and templates from Debian packages + + + + + + apt-extracttemplate + file + + + + Description</> + <para> + <command/apt-extracttemplates/ will take one or more Debian package files + as input and write out (to a temporary directory) all associated config + scripts and template files. For each passed in package that contains + config scripts and templates, one line of output will be generated + in the format: + <para> + package version template-file config-script + </RefSect1> + + <RefSect1><Title>Options</> + &apt-cmdblurb; + + </RefSect1> + + <RefSect1><Title>See Also</> + <para> + &apt-conf; + </RefSect1> + + <RefSect1><Title>Diagnostics</> + <para> + <command/apt-sortpkgs/ returns zero on normal operation, decimal 100 on error. + </RefSect1> + + &manbugs; + &manauthor; + +</refentry> diff --git a/doc/makefile b/doc/makefile index 45cb7a878..8aeb406fa 100644 --- a/doc/makefile +++ b/doc/makefile @@ -12,7 +12,8 @@ include $(DEBIANDOC_H) # Man pages SOURCE = apt-cache.8 apt-get.8 apt-cdrom.8 apt.conf.5 sources.list.5 \ - apt-config.8 apt-sortpkgs.1 apt-ftparchive.1 apt_preferences.5 + apt-config.8 apt-sortpkgs.1 apt-ftparchive.1 apt_preferences.5 \ + apt-extracttemplates.1 INCLUDES = apt.ent include $(SGML_MANPAGE_H) -- cgit v1.2.3