File: debconf-dumpdb.c

package info (click to toggle)
cdebconf 0.227
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,552 kB
  • sloc: ansic: 16,568; sh: 549; makefile: 456; sql: 52; perl: 13
file content (135 lines) | stat: -rw-r--r-- 3,470 bytes parent folder | download | duplicates (5)
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
/**
 * @file debconf-dumpdb.c
 * @brief Dump a database
 *
 * This code was found on debian-boot@lists.debian.org 2003-07-30,
 * posted by Tollef Fog Heen.
 *
 */
#include "confmodule.h"
#include "configuration.h"
#include "frontend.h"
#include "database.h"
#include "template.h"
#include "question.h"

#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <unistd.h>
#include <locale.h>
#include <sys/types.h>
#include <regex.h>

static struct option g_dpc_args[] = {
    { "help", 0, NULL, 'h' },
    { "pattern", 1, NULL, 'p' },
    { 0, 0, 0, 0 }
};

static void usage(const char *exename)
{
    printf("%s [-h|--help] [-p|--pattern pattern] [source-db]\n", exename);
    printf("\tsource-db -  config database to dump\n");
    printf("\t-h, --help - this help message\n");
    printf("\t-p, --pattern pattern - dump only names matching this pattern\n");
    exit(0);
}

int main(int argc, char **argv)
{
    struct configuration *config;
    struct template_db *tdb;
    struct question_db *qdb;
    struct question *q;
    char *configpath;
    const char *tdbname;
    char *dbname = 0;
    char *pattern = 0;
    regex_t pattern_regex;
    void *iter;
    int c;

    setlocale(LC_ALL, "");
    
    while ((c = getopt_long(argc, argv, "hp:", g_dpc_args, NULL)) > 0)
    {
        switch (c)
        {
        case 'h': usage(argv[0]); break;
        case 'p': pattern = optarg; break;
        }
    }

    if (optind + 1 > argc)
        usage(argv[0]);

    dbname = argv[optind];

    if (dbname == NULL)
        usage(argv[0]);

    /* suppress warnings about unknown localized fields */
    setenv("DEBCONF_NO_I18N", "1", 1);

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

    /* find out which template databases to load; fall back to global
     * default if not configured otherwise
     */
    if (asprintf(&configpath, "config::instance::%s::template", dbname) == -1)
        DIE("Out of memory");
    tdbname = config->get(config, configpath, NULL);

    /* 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");

    /* load database */
    tdb->methods.load(tdb);
    qdb->methods.load(qdb);
    
    /* 
     * Iterate through all the questions and print them out
     */

    /* maybe compile pattern regex */
    if (pattern) {
        int err = regcomp(&pattern_regex, pattern, REG_EXTENDED | REG_NOSUB);
        if (err != 0) {
            int errmsgsize = regerror(err, &pattern_regex, NULL, 0);
            char *errmsg = malloc(errmsgsize);
            if (errmsg == NULL)
                DIE("Out of memory");
            regerror(err, &pattern_regex, errmsg, errmsgsize);
            DIE("regcomp: %s", errmsg);
        }
    }

    /* TODO: error checking */
    iter = 0;
    while ((q = qdb->methods.iterate(qdb, &iter)) != NULL)
    {
        if (pattern) {
            if (regexec(&pattern_regex, q->tag, 0, 0, 0) != 0)
                goto nextq;
        }

        printf("%s %s %s\n", q->tag, q->template->type, q->value);
nextq:
        question_deref(q);
    }

    if (pattern)
        regfree(&pattern_regex);

    question_db_delete(qdb);
    template_db_delete(tdb);

    return 0;
}