~ubuntu-branches/debian/jessie/debfoster/jessie

« back to all changes in this revision

Viewing changes to src/conffile.c

  • Committer: Bazaar Package Importer
  • Author(s): Ivo Timmermans
  • Date: 2002-01-17 23:08:39 UTC
  • Revision ID: james.westby@ubuntu.com-20020117230839-s22xl5hew1z4s3r5
Tags: upstream-2.5
ImportĀ upstreamĀ versionĀ 2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <unistd.h>
 
4
#include <errno.h>
 
5
#include <string.h>
 
6
 
 
7
#include "AVLTree.h"
 
8
#include "error.h"
 
9
#include "conffile.h"
 
10
#include "status.h"
 
11
#include "config.h"
 
12
#include "intl.h"
 
13
 
 
14
#define READBUF 4096
 
15
 
 
16
/* Various files */
 
17
#define KEEPERS VAR"/lib/debfoster/keepers"
 
18
#define STATUSFILE VAR"/lib/dpkg/status"
 
19
#define AVAILABLEFILE VAR"/lib/dpkg/available"
 
20
#define CONFIG_FILE ETC"/debfoster.conf"
 
21
 
 
22
/* Only simple space-separated arguments can be given */
 
23
#define INSTALLCOMMAND "apt-get install"
 
24
#define REMOVECOMMAND "apt-get --purge remove"
 
25
#define INFOCOMMAND "dpkg -s"
 
26
 
 
27
/* Default policies for packages */
 
28
#define MAXPRIORITY "standard"
 
29
 
 
30
static AVLTree *options = NULL;
 
31
 
 
32
static void set_string(const char **holder, const char *val) {
 
33
        if(*holder) free((char *)*holder);
 
34
        *holder = xstrdup(val);
 
35
}
 
36
 
 
37
#define StringConfigVal(s) const char *s = NULL; \
 
38
        static void set_##s(const char *str) { set_string(&s, str); }
 
39
 
 
40
static void set_bool(int *holder, const char *val) {
 
41
        if(!strcasecmp(val, "y") || !strcasecmp(val, "yes")
 
42
        || !strcasecmp(val, "t") || !strcasecmp(val, "true")
 
43
        || !strcasecmp(val, "1") || !strcasecmp(val, "on"))
 
44
                *holder = 1;
 
45
        else if(!strcasecmp(val, "n") || !strcasecmp(val, "no")
 
46
             || !strcasecmp(val, "f") || !strcasecmp(val, "false")
 
47
             || !strcasecmp(val, "0") || !strcasecmp(val, "off"))
 
48
                *holder = 0;
 
49
        else error_exit(ERROR_CONFIG, _("Not a truth value: \"%s\"\n"), val);
 
50
}
 
51
 
 
52
#define BoolConfigVal(b) int b = 0; \
 
53
        static void set_##b(const char *str) { set_bool(&b, str); }
 
54
 
 
55
StringConfigVal(KeeperFile)
 
56
StringConfigVal(DpkgStatus)
 
57
StringConfigVal(DpkgAvailable)
 
58
StringConfigVal(InstallCmd)
 
59
StringConfigVal(RemoveCmd)
 
60
StringConfigVal(InfoCmd)
 
61
StringConfigVal(MaxPriority)
 
62
StringConfigVal(KeepSections)
 
63
StringConfigVal(NokeepSections)
 
64
StringConfigVal(GuessDepends)
 
65
 
 
66
BoolConfigVal(UseHold)
 
67
BoolConfigVal(UseEssential)
 
68
BoolConfigVal(UsePreDepends)
 
69
BoolConfigVal(UseRecommends)
 
70
BoolConfigVal(UseSuggests)
 
71
BoolConfigVal(UseTasks)
 
72
 
 
73
BoolConfigVal(NegativeKeepers)
 
74
 
 
75
BoolConfigVal(Quiet)
 
76
BoolConfigVal(Force)
 
77
BoolConfigVal(Verbose)
 
78
 
 
79
static int optcmp(const option_t a, const option_t b) {
 
80
        return strcasecmp(a->key, b->key);
 
81
}
 
82
 
 
83
option_t find_option(const char *key) {
 
84
        struct option_t o;
 
85
        AVLNode *c;
 
86
 
 
87
        o.key = key;
 
88
 
 
89
        c = AVLSearch(options, &o);
 
90
 
 
91
        return c?c->item:NULL;
 
92
}
 
93
 
 
94
static void add_option(const char *key, void (*set)(const char *), const char *def) {
 
95
        option_t o;
 
96
        o = xmalloc(sizeof *o);
 
97
        o->key = xstrdup(key);
 
98
        o->set = set;
 
99
        set(def);
 
100
        if(!AVLInsert(options, o))
 
101
                perror_exit(ERROR_CONFIG, key);
 
102
}
 
103
 
 
104
static void free_option(option_t opt) {
 
105
        if(opt) {
 
106
                if(opt->key)
 
107
                        free((char *)opt->key);
 
108
                free(opt);
 
109
        }
 
110
}
 
111
 
 
112
void readconfig(const char *config) {
 
113
        FILE *f;
 
114
        unsigned char buf[READBUF];
 
115
        option_t o;
 
116
        char *key, *value;
 
117
        int lineno = 0;
 
118
 
 
119
        if(!config)
 
120
                config = CONFIG_FILE;
 
121
 
 
122
        if(options)
 
123
                AVLFreeNodes(options);
 
124
        else
 
125
                options = AVLAllocTree((AVLCompare)optcmp, (AVLFreeItem)free_option);
 
126
 
 
127
        /* Seed tree with defaults for all options */
 
128
        add_option("KeeperFile",    set_KeeperFile,    KEEPERS);
 
129
        add_option("DpkgStatus",    set_DpkgStatus,    STATUSFILE);
 
130
        add_option("DpkgAvailable", set_DpkgAvailable, AVAILABLEFILE);
 
131
        add_option("InstallCmd",    set_InstallCmd,    INSTALLCOMMAND);
 
132
        add_option("RemoveCmd",     set_RemoveCmd,     REMOVECOMMAND);
 
133
        add_option("InfoCmd",       set_InfoCmd,       INFOCOMMAND);
 
134
        add_option("MaxPriority",   set_MaxPriority,   MAXPRIORITY);
 
135
        add_option("KeepSections",  set_KeepSections,  "");
 
136
        add_option("NokeepSections",  set_NokeepSections, "");
 
137
        add_option("GuessDepends",  set_GuessDepends, "");
 
138
 
 
139
        add_option("UseHold",       set_UseHold,       "yes");
 
140
        add_option("UseEssential",  set_UseEssential,  "yes");
 
141
        add_option("UsePreDepends", set_UsePreDepends, "yes");
 
142
        add_option("UseRecommends", set_UseRecommends, "yes");
 
143
        add_option("UseSuggests",   set_UseSuggests,   "no");
 
144
        add_option("UseTasks",      set_UseTasks,      "no");
 
145
 
 
146
        add_option("NegativeKeepers", set_NegativeKeepers, "yes");
 
147
 
 
148
        add_option("Quiet",         set_Quiet,         "no");
 
149
        add_option("Force",         set_Force,         "no");
 
150
        add_option("Verbose",       set_Verbose,       "no");
 
151
 
 
152
        /* Read the options but fail if there's no default for it */
 
153
        f = fopen(config, "r");
 
154
        if(f) {
 
155
                while(fgets(buf, READBUF, f)) {
 
156
                        lineno++;
 
157
                        key = chop(buf);
 
158
                        if(*key && *key != '#') {
 
159
                                value = strchr(key, '=');
 
160
                                if(!value)
 
161
                                        error_exit(ERROR_CONFIG, _("Syntax error in %s:%d\n"), CONFIG_FILE, lineno);
 
162
                                *value++ = '\0';
 
163
                                key = chop(key);
 
164
                                if(!(o = find_option(key)))
 
165
                                        error_exit(ERROR_CONFIG, _("Unknown option `%s' at %s:%d\n"), key, CONFIG_FILE, lineno);
 
166
                                o->set(chop(value));
 
167
                        }
 
168
                }
 
169
        }
 
170
}