summaryrefslogtreecommitdiff
path: root/cmdline/apt-extracttemplates.cc
diff options
context:
space:
mode:
authorArch Librarian <arch@canonical.com>2004-09-20 16:56:38 +0000
committerArch Librarian <arch@canonical.com>2004-09-20 16:56:38 +0000
commit3fc8f6852335ef8d50f40882769d9df6728dd73d (patch)
treebbce2e57dca092670994b95587ddf9acd39a3147 /cmdline/apt-extracttemplates.cc
parent0f2fa32291a66b7a154b5926a6dc8f38476bdd94 (diff)
apt-extracttemplates stuff from debconf
Author: tausq Date: 2001-02-23 04:28:49 GMT apt-extracttemplates stuff from debconf
Diffstat (limited to 'cmdline/apt-extracttemplates.cc')
-rw-r--r--cmdline/apt-extracttemplates.cc142
1 files changed, 142 insertions, 0 deletions
diff --git a/cmdline/apt-extracttemplates.cc b/cmdline/apt-extracttemplates.cc
new file mode 100644
index 000000000..b7af03005
--- /dev/null
+++ b/cmdline/apt-extracttemplates.cc
@@ -0,0 +1,142 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#define _GNU_SOURCE
+#include <getopt.h>
+#include <wait.h>
+#include <fstream.h>
+
+#include <apt-pkg/init.h>
+#if APT_PKG_MAJOR >= 3
+#define APT_COMPATIBILITY 986
+#include <apt-pkg/debversion.h>
+#endif
+#include <apt-pkg/pkgcache.h>
+#include <apt-pkg/configuration.h>
+#include <apt-pkg/progress.h>
+#include <apt-pkg/sourcelist.h>
+#include <apt-pkg/pkgcachegen.h>
+#include <apt-pkg/version.h>
+#include "debfile.h"
+
+#define TMPDIR "/var/lib/debconf/"
+#define STR(x) (x ? x : "")
+//#define TMPDIR "tmp/"
+
+void help(void)
+{
+ fprintf(stderr, "apt-extracttemplates deb [deb]\n");
+ exit(0);
+}
+
+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.close();
+ return strdup(fn);
+}
+
+void writeconfig(const DebFile &file)
+{
+ 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");
+ return;
+ }
+ printf("%s %s %s %s\n",
+ STR(file.Package), // Package
+ STR(file.Version), // Version
+ templatefile, // Template
+ configscript // Config
+ );
+}
+
+void init(MMap *&Map, pkgCache *&Cache)
+{
+ // Initialize the apt cache
+ if (pkgInitConfig(*_config) == false || pkgInitSystem(*_config, _system) == false)
+ {
+ fprintf(stderr, "Cannot initialize apt cache\n");
+ return;
+ }
+ pkgSourceList List;
+ List.ReadMainList();
+ OpProgress Prog;
+ pkgMakeStatusCache(List,Prog,&Map,true);
+ Cache = new pkgCache(Map);
+}
+
+int main(int argc, char **argv, char **env)
+{
+ int idx = 0;
+ char **debs = 0;
+ int numdebs = 0;
+ MMap *Map = 0;
+ const char *debconfver = NULL;
+
+ init(Map, DebFile::Cache);
+ if (Map == 0 || DebFile::Cache == 0)
+ {
+ fprintf(stderr, "Cannot initialize APT cache\n");
+ return 1;
+ }
+
+ debconfver = DebFile::GetInstalledVer("debconf");
+
+ if (debconfver == NULL)
+ {
+ fprintf(stderr, "Cannot get debconf version. Is debconf installed?\n");
+ return 1;
+ }
+
+ 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;
+ }
+
+ for (idx = 0; idx < numdebs; idx++)
+ {
+ DebFile file(debs[idx]);
+ if (file.Go() == false)
+ {
+ fprintf(stderr, "Cannot read %s\n", debs[idx]);
+ continue;
+ }
+ if (file.Template != 0 && file.ParseInfo() == true)
+ {
+ if (file.DepVer != 0 && *file.DepVer != 0 &&
+ pkgCheckDep(file.DepVer,
+ debconfver, file.DepOp) == false)
+ continue;
+ if (file.PreDepVer != 0 && *file.PreDepVer != 0 &&
+ pkgCheckDep(file.PreDepVer,
+ debconfver, file.PreDepOp) == false)
+ continue;
+
+ writeconfig(file);
+ }
+ }
+
+
+ delete Map;
+ delete DebFile::Cache;
+
+ return 0;
+}