aboutsummaryrefslogtreecommitdiff
path: root/lib/utils.c
blob: c8106d4ab1af28a6e268ac58756e5e0e46fd02ef (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
184
185
186
187
/* This file is part of Mailfromd.
   Copyright (C) 2007-2008, 2010-2011, 2015 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 <libmf.h>
#include <stdlib.h>
#include <mailutils/locker.h>
#include "mailutils/alloc.h"


char **
config_array_to_argv (mu_config_value_t *val)
{
  int i, j;
  int argc;
  char **argv;
  
  argc = val->v.arg.c;
  argv = mu_calloc (argc + 1, sizeof (argv[0]));
  for (i = j = 0; i < argc; i++)
    {
      if (mu_cfg_assert_value_type (&val->v.arg.v[i], MU_CFG_STRING) == 0)
	argv[j++] = mu_strdup (val->v.arg.v[i].v.string);
    }
  argv[j] = NULL;
  return argv;
}

char *
config_array_to_string (mu_config_value_t *val)
{
  size_t len = 0;
  int i;
  char *str, *p;

  for (i = 0; i < val->v.arg.c; i++)
    {
      if (mu_cfg_assert_value_type (&val->v.arg.v[i], MU_CFG_STRING))
	return NULL;
      len += strlen (val->v.arg.v[i].v.string) + 1;
    }

  str = mu_alloc (len);
  p = str;
  for (i = 0; i < val->v.arg.c; i++)
    {
      size_t n = strlen (val->v.arg.v[i].v.string);
      memcpy (p, val->v.arg.v[i].v.string, n);
      p += n;
      *p++ = ' ';
    }
  str[len-1] = 0;
  return str;
}


int
config_cb_timeout (struct timeval *pt, mu_config_value_t *val)
{
  int rc;
  const char *endp;
  time_t t;
  const char *str;
  char *alloc_str = NULL;
  
  switch (val->type)
    {
    case MU_CFG_STRING:
      str = val->v.string;
      break;

    case MU_CFG_ARRAY:
      str = alloc_str = config_array_to_string (val);
      if (!str)
	return 1;
      break;

    case MU_CFG_LIST:
      mu_error (_("unexpected list"));
      return 1;

    default:
      mu_error (_("INTERNAL ERROR at %s:%d: please report"),
		__FILE__, __LINE__);
      abort();
    }      

  rc = parse_time_interval (str, &t, &endp);
  if (rc)
    mu_error (_("unrecognized time format (near `%s')"), endp);
  else
    {
      pt->tv_usec = 0;
      pt->tv_sec = t;
    }
  free (alloc_str);
  return 0;
}

int
config_cb_ignore(void *data, mu_config_value_t *val)
{
	mu_diag_output (MU_DIAG_WARNING,
			_("this statement has no effect in %s"),
			PACKAGE_STRING);
	return 0;
}

int
config_cb_lock_retry_count(void *data, mu_config_value_t *val)
{
	if (mu_cfg_assert_value_type(val, MU_CFG_STRING))
		return 1;
	if (mf_optcache_set_option("lock-retry-count", val->v.string)) {
		mu_error(_("not a number"));
		return 1;
	}
	return 0;
}

int
config_cb_lock_retry_timeout(void *data, mu_config_value_t *val)
{
	return mf_optcache_set_option("lock-retry-timeout", val->v.string);
}

static void
set_lock_retry_count(union mf_option_value *val)
{
	mu_locker_set_default_retry_count(val->ov_size);
}

static void
set_lock_retry_timeout(union mf_option_value *val)
{
	mu_locker_set_default_retry_timeout(val->ov_time);
}

static struct mf_option_cache lock_option_cache[] = {
	{ "lock-retry-count", mf_option_size, set_lock_retry_count },
	{ "lock-retry-timeout", mf_option_timeout, set_lock_retry_timeout },
	{ NULL }
};

void
mf_init_lock_options()
{
	mf_optcache_add(lock_option_cache, 0, MF_OCF_NULL|MF_OCF_STATIC);
}


int
stderr_closed_p()
{
	int fd = dup(0);
	if (fd < 0)
		return 1;
	close(fd);
	return fd <= 2;
}


int
mf_list_compare_string(const void *item, const void *value)
{
	return strcmp(item, value);
}




Return to:

Send suggestions and report system problems to the System administrator.