File: debconf.c

package info (click to toggle)
cdebconf 0.10-7.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 884 kB
  • ctags: 642
  • sloc: ansic: 5,072; makefile: 285; sh: 274; sql: 51
file content (84 lines) | stat: -rw-r--r-- 1,929 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "confmodule.h"
#include "configuration.h"
#include "frontend.h"
#include "database.h"

#include <signal.h>
#include <string.h>

static struct configuration *config = NULL;
static struct frontend *frontend = NULL;
static struct confmodule *confmodule = NULL;
static struct database *db = NULL;

void cleanup()
{
	if (db != NULL)
		db->save(db);
	if (frontend != NULL)
		frontend_delete(frontend);
	if (db != NULL)
		database_delete(db);
	if (config != NULL)
		config_delete(config);
}

void sighandler(int sig)
{
	cleanup();
}

void parsecmdline(struct configuration *config, int argc, char **argv)
{
	if (argc <= 1)
	{
		fprintf(stderr, "%s <config script>\n", argv[0]);
		exit(-1);
	}
}

int main(int argc, char **argv)
{
	signal(SIGINT, sighandler);

	config = config_new();
	parsecmdline(config, argc, argv);

	/* parse the configuration info */
	if (config->read(config, DEBCONFCONFIG) == 0)
		DIE("Error reading configuration information");

	/* initialize database and frontend modules */
	if ((db = database_new(config)) == 0)
		DIE("Cannot initialize DebConf database");
	if ((frontend = frontend_new(config, db)) == 0)
		DIE("Cannot initialize DebConf frontend");

	/* set title */
	{
		char buf[100], pkg[100];
		char *slash = strrchr(argv[1], '/');
		if (slash == NULL) slash = argv[1]; else slash++;
		snprintf(pkg, sizeof(pkg), "%s", slash);
		if (strlen(pkg) >= 7 
			&& strcmp(pkg + strlen(pkg) - 7, ".config") == 0)
		{
			pkg[strlen(pkg) - 7] = '\0';
		}
		snprintf(buf, sizeof(buf), "Configuring %s", pkg);
		frontend->set_title(frontend, buf);
	}

	/* load templates */
	db->load(db);

	/* startup the confmodule; run the config script and talk to it */
	confmodule = confmodule_new(config, db, frontend);
	confmodule->run(confmodule, argc, argv);
	confmodule->communicate(confmodule);

	/* shutting down .... sync the database and shutdown the modules */
	cleanup();

	return confmodule->exitcode;
}