File: debconf-show.c

package info (click to toggle)
cdebconf 0.182
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,668 kB
  • sloc: ansic: 16,289; sh: 547; makefile: 438; sql: 52; perl: 13
file content (183 lines) | stat: -rw-r--r-- 3,803 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

#include "common.h"
#include "configuration.h"
#include "database.h"
#include "question.h"
#include "template.h"

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

int listowners = 0;
int listdbs = 0;
static struct option options[] = {
    { "db", 1, NULL, 'd' },
    { "listowners", 0, &listowners, 'o' },
    { "listdbs", 0, &listdbs, 'l' },
    { 0, 0, 0, 0 }
};

static void usage(const char *exename)
{
    printf("Usage:\n");
    printf("\t%s packagename [...] [--db=dbname]\n", exename);
    printf("\t%s --listowners [--db=dbname]\n", exename);
    printf("\t%s --listdbs\n", exename);
    exit(0);
}

int main(int argc, char **argv)
{
	struct configuration *config = config_new();
	struct template_db *tdb;
	struct question_db *qdb;
	struct question *q;
	void *iter = 0;
	const char *tdbname = NULL;

    int c;
	const char *dbname = NULL;
	struct questionowner *owners = NULL;

    setlocale(LC_ALL, "");

    while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0)
    {
        switch (c)
        {
            case 'd':
				dbname = optarg;
                break;
            default:
                break;
        }
    }

	config = config_new();

	if (config->read(config, DEBCONFCONFIG) == 0)
		DIE("Error reading configuration information");

	if (listdbs)
	{
		struct configitem *instance, *item;

		instance = config->tree(config,"config::instance");
		if (instance)
		{
			for (item=instance->child; item != NULL; item = item->next)
			{
				printf("%s %s\n", item->tag, item->value);
			}
		}
		
		config_delete(config);
		
		return 0;
	}
	
	if (dbname)
	{
		/* find out which template databases to load; fall back to global
		 * default if not configured otherwise
		 */
	    char *configpath;
		if (asprintf(&configpath, "config::instance::%s::template", dbname) == -1)
		    DIE("Out of memory");
		tdbname = config->get(config, configpath, NULL);
		free(configpath);
	}

	/* initialize database modules */
	if ((tdb = template_db_new(config, tdbname)) == 0)
		DIE("Cannot initialize debconf template database");
	if ((qdb = question_db_new(config, tdb, dbname)) == 0)
		DIE("Cannot initialize debconf database");

	tdb->methods.load(tdb);
	qdb->methods.load(qdb);

	if (listowners)
	{
		while((q = qdb->methods.iterate(qdb, &iter)) != NULL)
		{
			struct questionowner *owner = q->owners;

			while (owner)
			{
				struct questionowner *o = owners;
				while (o != NULL)
				{
					if (0 == strcmp(o->owner, owner->owner))
						break;
					o = o->next;
				}
				if (o == NULL)
				{
					o = malloc(sizeof(struct questionowner));
					o->next = owners;
					o->owner = owner->owner;
					owners = o;
					fprintf(stdout, "%s\n", owner->owner);
				}
				owner = owner->next;
			}
		}
	}
	else
	{
		for (c = optind; c < argc; c++)
		{
			struct questionowner *o = malloc(sizeof(struct questionowner));
			o->next = owners;
			o->owner = argv[c];
			owners = o;
		}

		if (!owners)
		{
			usage(argv[0]);
		}

		while((q = qdb->methods.iterate(qdb, &iter)) != NULL)
		{
			const char seen = (q->flags & DC_QFLAG_SEEN)?'*':' ';
			const char *type = q->template->type?q->template->type:"";
			const char *value = strcmp("password", type)?q->value:"(password omitted)";

			const struct questionowner *owner = q->owners;
			while (owner)
			{
				const struct questionowner *o = owners;
				while (o != NULL)
				{
					if (0 == strcmp(o->owner, owner->owner))
					{
						fprintf(stdout, "%c %s:", seen, q->tag);
						if (value)
							fprintf(stdout, " %s", value);
						fputc('\n', stdout);
						break;
					}
					o = o->next;
				}
				owner = owner->next;
			}
		}
	}

	while (owners)
	{
		struct questionowner *o = owners;
		owners = o->next;
		free(o);
	}

	question_db_delete(qdb);
	template_db_delete(tdb);

	config_delete(config);

	return 0;
}