File: debconf-communicate.c

package info (click to toggle)
cdebconf 0.138lenny3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,184 kB
  • ctags: 1,371
  • sloc: ansic: 14,158; sh: 469; makefile: 429; sql: 51; perl: 13
file content (139 lines) | stat: -rw-r--r-- 3,151 bytes parent folder | download | duplicates (3)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 * @file debconf-communicate.c
 * @brief Configuration module interface
 */
#include "confmodule.h"
#include "configuration.h"
#include "frontend.h"
#include "database.h"

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

static struct configuration *config = NULL;
static struct frontend *frontend = NULL;
static struct confmodule *confmodule = NULL;
static struct question_db *questions = NULL;
static struct template_db *templates = NULL;

static struct option options[] = {
    { "frontend", 1, 0, 'f' },
    { 0, 0, 0, 0 }
};

static int save()
{
        return confmodule->save(confmodule);
}

static void cleanup()
{
	if (frontend != NULL)
		frontend_delete(frontend);
	if (questions != NULL)
		question_db_delete(questions);
	if (templates != NULL)
		template_db_delete(templates);
	if (config != NULL)
		config_delete(config);
}

void sighandler(int sig)
{
	save();
	if (sig != SIGUSR1) {
		cleanup();
		exit(1);
	}
}

void help(const char *exename)
{
    fprintf(stderr, "%s [-ffrontend] [package]\n", exename);
    exit(-1);
}

void parsecmdline(struct configuration *config, int argc, char **argv)
{
    int c;

    while ((c = getopt_long(argc, argv, "f:", options, NULL)) > 0)
    {
        switch (c)
        {
            case 'f':
                config->set(config, "_cmdline::frontend", optarg);
                break;
            default:
                break;
        }
    }

    if (optind > argc)
    {
        help(argv[0]);
    }
}

int main(int argc, char **argv)
{
    int code = 127;
    char buf[1024], *ret;

	signal(SIGINT, sighandler);
	signal(SIGTERM, sighandler);
	signal(SIGUSR1, sighandler);
	setlocale (LC_ALL, "");

	config = config_new();
	if (!config) {
	  DIE("Cannot read new config");
	}
	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 ((templates = template_db_new(config, NULL)) == 0)
        DIE("Cannot initialize DebConf template database");
	if ((questions = question_db_new(config, templates, NULL)) == 0)
		DIE("Cannot initialize DebConf configuration database");
	if ((frontend = frontend_new(config, templates, questions)) == 0)
		DIE("Cannot initialize DebConf frontend");

	/* load templates and config */
	templates->methods.load(templates);
    questions->methods.load(questions);

	/* startup the confmodule; run the config script and talk to it */
	confmodule = confmodule_new(config, templates, questions, frontend);

    if (optind < argc)
        confmodule->owner = argv[optind];
    else
        confmodule->owner = "unknown";

    /* start feeding user commands to debconf ... */
    while (fgets(buf, sizeof(buf), stdin))
    {
        ret = confmodule->process_command(confmodule, buf);

        /* extract the first number as the return code */
        code = atoi(ret);

        write(1, ret, strlen(ret));
        write(1, "\n", 1);
        free(ret);
    }

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

	return code;
}