aboutsummaryrefslogtreecommitdiff
path: root/lib/optcache.c
blob: 15b011b5e92f486c54b8cfe8fb2071f29b4a4bc7 (plain)
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
/* This file is part of Mailfromd.
   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Sergey
   Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <errno.h>
#include "libmf.h"
#include "mailutils/alloc.h"


struct cache_list_elt {
	struct cache_list_elt *next;
	struct mf_option_cache *opt;
	size_t size;
};

static struct cache_list_elt *head, *tail;

static struct mf_option_cache *
optcache_dup(struct mf_option_cache *tab, size_t size)
{
	struct mf_option_cache *newtab = mu_calloc(size, sizeof(*tab));
	size_t i;

	for (i = 0; i < size; i++) {
		newtab[i] = tab[i];
		newtab[i].name = mu_strdup(newtab[i].name);
		newtab[i].isset = 0;
	}
	return newtab;
}

void
mf_optcache_add(struct mf_option_cache *tab, size_t size, int flags)
{
	struct cache_list_elt *elt;

	elt = mu_alloc(sizeof(*elt));
	elt->next = NULL;
	if (tail)
		tail->next = elt;
	else
		head = elt;
	tail = elt;

	/* If the array is null-terminated, compute its size */
	if (flags & MF_OCF_NULL)
		for (size = 0; tab[size].name; size++)
			;

	/* Unless it is static, make a copy */
	tab = optcache_dup(tab, size);

	elt->opt = tab;
	elt->size = size;
}

static struct mf_option_cache *
find_option(char const *name)
{
	struct cache_list_elt *elt;

	for (elt = head; elt; elt = elt->next) {
		size_t i;
		struct mf_option_cache *opt;
		
		for (i = 0, opt = elt->opt; i < elt->size; i++, opt++) {
			if (strcmp(opt->name, name) == 0)
				return opt;
		}
	}
	return NULL;
}

int
mf_optcache_set_option(char const *name, char const *value)
{
	int rc;
	struct mf_option_cache *p = find_option(name);
	if (!p) {
		errno = ENOENT;
		return 1;
	}
	rc = p->handler(name, &p->value, value);
	if (rc == 0)
		p->isset = 1;
	return rc;
}

void
mf_optcache_flush()
{
	struct cache_list_elt *elt;

	for (elt = head; elt; elt = elt->next) {
		size_t i;
		struct mf_option_cache *opt;
		
		for (i = 0, opt = elt->opt; i < elt->size; i++, opt++)
			if (opt->isset && opt->set)
				opt->set(&opt->value);
	}
}


int
mf_option_string(char const *opt, union mf_option_value *val, char const *arg)
{
	free(val->ov_string);
	val->ov_string = strdup(arg);
	return 0;
}

int
mf_option_boolean(char const *opt, union mf_option_value *val, char const *arg)
{
	int b;

	if (strcmp (arg, "yes") == 0
	    || strcmp (arg, "true") == 0
	    || strcmp (arg, "t") == 0)
		b = 1;
	else if (strcmp (arg, "no") == 0
		 || strcmp (arg, "false") == 0
		 || strcmp (arg, "nil") == 0)
		b = 0;
	else {
		char *p;
		
		b = strtoul(arg, &p, 10);
		if (*p) {
			errno = EINVAL;
			return 1;
		}
	}
	
	val->ov_bool = b;
	return 0;
}

int
mf_option_timeout(char const *opt, union mf_option_value *val, char const *arg)
{
	time_t interval;
	const char *endp;
	
	if (parse_time_interval(arg, &interval, &endp)) {
		mu_error(_("%s: unrecognized time format (near `%s')"),
			 opt, endp);
		return 1;
	}
	val->ov_time = interval;
	return 0;
}

int
mf_option_size(char const *opt, union mf_option_value *val, char const *arg)
{
	char *p;
	unsigned long n = strtoul(arg, &p, 10);
	if (*p)
		return 1;
	val->ov_size = n;
	return 0;
}
	

Return to:

Send suggestions and report system problems to the System administrator.