apt-extracttemplates stuff from debconf
[ntk/apt.git] / cmdline / apt-extracttemplates.cc
CommitLineData
3fc8f685
AL
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <unistd.h>
5#define _GNU_SOURCE
6#include <getopt.h>
7#include <wait.h>
8#include <fstream.h>
9
10#include <apt-pkg/init.h>
11#if APT_PKG_MAJOR >= 3
12#define APT_COMPATIBILITY 986
13#include <apt-pkg/debversion.h>
14#endif
15#include <apt-pkg/pkgcache.h>
16#include <apt-pkg/configuration.h>
17#include <apt-pkg/progress.h>
18#include <apt-pkg/sourcelist.h>
19#include <apt-pkg/pkgcachegen.h>
20#include <apt-pkg/version.h>
21#include "debfile.h"
22
23#define TMPDIR "/var/lib/debconf/"
24#define STR(x) (x ? x : "")
25//#define TMPDIR "tmp/"
26
27void help(void)
28{
29 fprintf(stderr, "apt-extracttemplates deb [deb]\n");
30 exit(0);
31}
32
33char *writefile(const char *prefix, const char *data)
34{
35 char fn[512];
36 static int i;
37 snprintf(fn, sizeof(fn), "%s%s.%u%d", TMPDIR, prefix, getpid(), i++);
38
39 if (data == NULL) data = "";
40
41 ofstream ofs(fn);
42 if (!ofs) return NULL;
43 ofs << data;
44 ofs.close();
45 return strdup(fn);
46}
47
48void writeconfig(const DebFile &file)
49{
50 char *templatefile = writefile("template", file.Template);
51 char *configscript = writefile("config", file.Config);
52
53 if (templatefile == 0 || configscript == 0)
54 {
55 fprintf(stderr, "Cannot write config script or templates\n");
56 return;
57 }
58 printf("%s %s %s %s\n",
59 STR(file.Package), // Package
60 STR(file.Version), // Version
61 templatefile, // Template
62 configscript // Config
63 );
64}
65
66void init(MMap *&Map, pkgCache *&Cache)
67{
68 // Initialize the apt cache
69 if (pkgInitConfig(*_config) == false || pkgInitSystem(*_config, _system) == false)
70 {
71 fprintf(stderr, "Cannot initialize apt cache\n");
72 return;
73 }
74 pkgSourceList List;
75 List.ReadMainList();
76 OpProgress Prog;
77 pkgMakeStatusCache(List,Prog,&Map,true);
78 Cache = new pkgCache(Map);
79}
80
81int main(int argc, char **argv, char **env)
82{
83 int idx = 0;
84 char **debs = 0;
85 int numdebs = 0;
86 MMap *Map = 0;
87 const char *debconfver = NULL;
88
89 init(Map, DebFile::Cache);
90 if (Map == 0 || DebFile::Cache == 0)
91 {
92 fprintf(stderr, "Cannot initialize APT cache\n");
93 return 1;
94 }
95
96 debconfver = DebFile::GetInstalledVer("debconf");
97
98 if (debconfver == NULL)
99 {
100 fprintf(stderr, "Cannot get debconf version. Is debconf installed?\n");
101 return 1;
102 }
103
104 numdebs = argc - 1;
105 debs = new char *[numdebs];
106 memcpy(debs, &argv[1], sizeof(char *) * numdebs);
107
108 if (numdebs < 1)
109 {
110 fprintf(stderr, "apt-extracttemplates foo.deb [...]\n");
111 return 0;
112 }
113
114 for (idx = 0; idx < numdebs; idx++)
115 {
116 DebFile file(debs[idx]);
117 if (file.Go() == false)
118 {
119 fprintf(stderr, "Cannot read %s\n", debs[idx]);
120 continue;
121 }
122 if (file.Template != 0 && file.ParseInfo() == true)
123 {
124 if (file.DepVer != 0 && *file.DepVer != 0 &&
125 pkgCheckDep(file.DepVer,
126 debconfver, file.DepOp) == false)
127 continue;
128 if (file.PreDepVer != 0 && *file.PreDepVer != 0 &&
129 pkgCheckDep(file.PreDepVer,
130 debconfver, file.PreDepOp) == false)
131 continue;
132
133 writeconfig(file);
134 }
135 }
136
137
138 delete Map;
139 delete DebFile::Cache;
140
141 return 0;
142}