aboutsummaryrefslogtreecommitdiff
path: root/lib/appinit.c
blob: 622d85639071c75bad4e406d49d1275355d58e16 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* 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 <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <mailutils/mailutils.h>
#include <mailutils/libargp.h>
#include <mailutils/gocs.h>
#include "libmf.h"

extern struct mu_cfg_tree *mu_argp_tree;

char *mu_app_rcfile;

static char *
get_canonical_name ()
{
  char *name;
  size_t len;
  char *p = strchr (argp_program_version, ' ');
  if (!p)
    return strdup (mu_program_name);
  len = p - argp_program_version;
  name = malloc (len + 1);
  if (!name)
    abort ();
  memcpy (name, argp_program_version, len);
  name[len] = 0;
  return name;
}

int
mfd_parse_config_files (struct mu_cfg_param *param, void *target)
{
  int flags = 0;
  int rc;
  
  if (mu_cfg_parser_verbose)
    flags |= MU_PARSE_CONFIG_VERBOSE;
  if (mu_cfg_parser_verbose > 1)
    flags |= MU_PARSE_CONFIG_DUMP;

  if (mu_load_site_rcfile)
    {
      rc = mu_parse_config (MU_CONFIG_FILE, mu_program_name, param,
			    flags | MU_PARSE_CONFIG_GLOBAL, target);
      if (rc == ENOMEM)
	{
	  mu_error ("%s", mu_strerror (rc));
	  return rc;
	}

      if (mu_app_rcfile)
	{
	  rc = mu_parse_config (mu_app_rcfile, mu_program_name, param,
				flags, target);
	  if (rc == ENOMEM)
	    {
	      mu_error ("%s", mu_strerror (rc));
	      return rc;
	    }
	}
    }
  
  if (mu_load_user_rcfile && mu_program_name)
    {
      size_t size = 3 + strlen (mu_program_name) + 1;
      char *file_name = malloc (size);
      if (file_name)
	{
	  strcpy (file_name, "~/.");
	  strcat (file_name, mu_program_name);

	  rc = mu_parse_config (file_name, mu_program_name, param, flags,
				target);
	  if (rc == ENOMEM)
	    {
	      mu_error ("%s", mu_strerror (rc));
	      return rc;
	    }

	  free (file_name);
	}
    }

  if (mu_load_rcfile)
    {
      rc = mu_parse_config (mu_load_rcfile, mu_program_name, param,
			    flags, target);
      if (rc)
	{
	  mu_error (_("errors parsing file %s: %s"), mu_load_rcfile,
		    mu_strerror (rc));
	  return rc;
	}
    }
	
  return 0;
}

static void
fixup_capa (char **excapa)
{
  char **p, **q;

  for (p = q = excapa; *q; q++)
    {
      if (**q != '.')
	*p++ = *q;
    }
  *p = NULL;
}
  
int
mfd_app_init (struct argp *myargp, const char **capa,
	      struct mu_cfg_param *cfg_param,
	      int argc, char **argv, int flags, int *pindex,
	      void *opt_data, void *cfg_data)
{
  int rc, i;
  struct argp *argp;
  struct argp argpnull = { 0 };
  char **excapa;
  int cfgflags = 0;
  
  mu_set_program_name (argv[0]);
  mu_log_tag = (char*)mu_program_name;
  mu_stdstream_setup ();
  mu_libargp_init ();
  if (capa)
    for (i = 0; capa[i]; i++)
      mu_gocs_register_std (capa[i]); 
  if (!myargp)
    myargp = &argpnull;
  argp = mu_argp_build (myargp, &excapa);

  fixup_capa (excapa);
  
  mu_cfg_tree_create (&mu_argp_tree);
  rc = argp_parse (argp, argc, argv, flags, pindex, opt_data);
  mu_argp_done (argp);
  if (rc)
    return rc;

  /* Reset program name, it may have been changed using the `--program-name'
     option. */
  mu_set_program_name (program_invocation_name);
  
  mu_libcfg_init (excapa);
  free (excapa);

  if (mu_help_config_mode)
    {
      char *comment;
      char *canonical_name = get_canonical_name ();
      mu_stream_t stream;
      mu_stdio_stream_create (&stream, MU_STDOUT_FD, 0);
      asprintf (&comment,
		_("This is a template for %s: the configuration file for %s."),
		mu_app_rcfile, canonical_name);
      mu_cfg_format_docstring (stream, comment, 0);
      free (comment);
      asprintf (&comment, "For more information, use `info %s'.",
		canonical_name);
      mu_cfg_format_docstring (stream, comment, 0);
      free (comment);
      
      mu_format_config_tree (stream, mu_program_name, cfg_param, 0);
      mu_stream_destroy (&stream);
      exit (0);
    }
  else
    mfd_parse_config_files (cfg_param, cfg_data);

  if (mu_cfg_parser_verbose)
    cfgflags |= MU_PARSE_CONFIG_VERBOSE;
  if (mu_cfg_parser_verbose > 1)
    cfgflags |= MU_PARSE_CONFIG_DUMP;
  rc = mu_cfg_tree_reduce (mu_argp_tree, mu_program_name, cfg_param,
			   cfgflags, cfg_data);

  if (mu_rcfile_lint)
    {
      if (rc || mu_cfg_error_count)
	exit (1);
      if (mu_app_cfg_verifier)
	rc = mu_app_cfg_verifier ();
      exit (rc ? 1 : 0);
    }
  
  mu_gocs_flush ();
  mu_cfg_destroy_tree (&mu_argp_tree);

  return !!(rc || mu_cfg_error_count);
}

Return to:

Send suggestions and report system problems to the System administrator.