summaryrefslogtreecommitdiff
path: root/libmu_argp/compat.c
blob: efacd5a78e91dc00233fbb81640ef617f18edff3 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2007, 2009 Free Software Foundation, Inc.

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

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include "mailutils/cctype.h"
#include "mailutils/libargp.h"
#include "mailutils/argcv.h"
#include "mailutils/mutil.h"
#ifdef WITH_TLS
# include "mailutils/tls.h"
#endif
#ifdef WITH_GSASL
# include "mailutils/gsasl.h"
#endif
#include "mailutils/sieve.h"

#ifndef MU_COMPAT_CONFIG_FILE
# define MU_COMPAT_CONFIG_FILE SYSCONFDIR "/mailutils.rc"
#endif

#ifndef MU_COMPAT_USER_CONFIG_FILE
# define MU_COMPAT_USER_CONFIG_FILE "~/.mailutils"
#endif

static int
member (const char *array[], const char *text, size_t len)
{
  int i;
  for (i = 0; array[i]; i++)
    if (strncmp (array[i], text, len) == 0)
      return 1;
  return 0;
}

/* Appends applicable options found in file NAME to argv. If progname
   is NULL, all the options found are assumed to apply. Otherwise they
   apply only if the line starts with ":something", and something is
   found in the CAPA array, or the line starts with PROGNAME.
*/
void
read_rc (const char *progname, const char *name, const char *capa[],
	 int *argc, char ***argv)
{
  FILE *fp;
  char *linebuf = NULL;
  char *buf = NULL;
  size_t n = 0;
  int x_argc = *argc;
  char **x_argv = *argv;
  char* rcfile = mu_tilde_expansion (name, "/", NULL);

  if (!rcfile)
    return;
  
  fp = fopen (rcfile, "r");
  if (!fp)
    {
      free(rcfile);
      return;
    }
  
  while (getline (&buf, &n, fp) > 0)
    {
      char *kwp, *p;
      int len;
      
      for (kwp = buf; *kwp && mu_isspace (*kwp); kwp++)
	;

      if (*kwp == '#' || *kwp == 0)
	continue;

      len = strlen (kwp);
      if (kwp[len-1] == '\n')
	kwp[--len] = 0;

      if (kwp[len-1] == '\\' || linebuf)
	{
	  int cont;
	  
	  if (kwp[len-1] == '\\')
	    {
	      kwp[--len] = 0;
	      cont = 1;
	    }
	  else
	    cont = 0;
	  
	  if (!linebuf)
	    linebuf = calloc (len + 1, 1);
	  else
	    linebuf = realloc (linebuf, strlen (linebuf) + len + 1);
	  
	  if (!linebuf)
	    {
	      fprintf (stderr, _("%s: not enough memory\n"), progname);
	      exit (1);
	    }
	  
	  strcpy (linebuf + strlen (linebuf), kwp);
	  if (cont)
	    continue;
	  kwp = linebuf;
	}

      len = 0;
      if (progname)
	{
	  for (p = kwp; *p && !mu_isspace (*p); p++)
	    len++;
	}
      else
	p = kwp; /* Use the whole line. */

      if (progname == NULL
	  || (kwp[0] == ':' && member (capa, kwp+1, len-1))
	  || strncmp (progname, kwp, len) == 0
	  )
	{
	  int i, n_argc = 0;
	  char **n_argv;
              
	  if (mu_argcv_get (p, "", NULL, &n_argc, &n_argv))
	    {
	      mu_argcv_free (n_argc, n_argv);
	      if (linebuf)
		free (linebuf);
	      linebuf = NULL;
	      continue;
	    }
	  x_argv = realloc (x_argv,
			    (x_argc + n_argc) * sizeof (x_argv[0]));
	  if (!x_argv)
	    {
	      fprintf (stderr, _("%s: not enough memory\n"), progname);
	      exit (1);
	    }
	  
	  for (i = 0; i < n_argc; i++)
	    x_argv[x_argc++] = mu_tilde_expansion (n_argv[i], "/", NULL);
	  
	  free (n_argv);
	}
      if (linebuf)
	free (linebuf);
      linebuf = NULL;
    }
  fclose (fp);
  free(rcfile);

  *argc = x_argc;
  *argv = x_argv;
}


void
mu_create_argcv (const char *capa[],
		 int argc, char **argv, int *p_argc, char ***p_argv)
{
  char *progname;
  int x_argc;
  char **x_argv;
  int i;
  int rcdir = 0;

  progname = strrchr (argv[0], '/');
  if (progname)
    progname++;
  else
    progname = argv[0];

  x_argv = malloc (sizeof (x_argv[0]));
  if (!x_argv)
    {
      fprintf (stderr, _("%s: not enough memory\n"), progname);
      exit (1);
    }

  /* Add command name */
  x_argc = 0;
  x_argv[x_argc] = argv[x_argc];
  x_argc++;

  /* Add global config file. */
  read_rc (progname, MU_COMPAT_CONFIG_FILE, capa, &x_argc, &x_argv);

  /* Look for per-user config files in ~/.mailutils/ or in ~/, but
     not both. This allows mailutils' utilities to have their config
     files segregated, if necessary. */

  {
    struct stat s;
    char *rcdirname = mu_tilde_expansion (MU_COMPAT_USER_CONFIG_FILE, "/", NULL);

    if (!rcdirname
	|| (stat(rcdirname, &s) == 0 && S_ISDIR(s.st_mode)))
      rcdir = 1;

    free(rcdirname);
  }

  /* Add per-user config file. */
  if (!rcdir)
    {
      read_rc (progname, MU_COMPAT_USER_CONFIG_FILE, capa, &x_argc, &x_argv);
    }
  else
    {
      char *userrc = NULL;

      userrc = malloc (sizeof (MU_COMPAT_USER_CONFIG_FILE)
		       /* provides an extra slot
			  for null byte as well */
		       + 1 /* slash */
		       + 9 /*mailutils*/); 

      if (!userrc)
	{
	  fprintf (stderr, _("%s: not enough memory\n"), progname);
	  exit (1);
	}
      
      sprintf (userrc, "%s/mailutils", MU_COMPAT_USER_CONFIG_FILE);
      read_rc (progname, userrc, capa, &x_argc, &x_argv);
      
      free (userrc);
    }

  /* Add per-user, per-program config file. */
  {
    char *progrc = NULL;
    int size;
    
    if (rcdir)
      size = sizeof (MU_COMPAT_USER_CONFIG_FILE)
	             + 1
		     + strlen (progname)
		     + 2 /* rc */;
    else
      size = 6 /*~/.mu.*/
	     + strlen (progname)
	     + 3 /* "rc" + null terminator */;

    progrc = malloc (size);

    if (!progrc)
      {
	fprintf (stderr, _("%s: not enough memory\n"), progname);
	exit (1);
      }

    if (rcdir)
      sprintf (progrc, "%s/%src", MU_COMPAT_USER_CONFIG_FILE, progname);
    else
      sprintf (progrc, "~/.mu.%src", progname);

    read_rc (NULL, progrc, capa, &x_argc, &x_argv);
    free (progrc);
  }

  /* Finally, add the command line options */
  x_argv = realloc (x_argv, (x_argc + argc) * sizeof (x_argv[0]));
  for (i = 1; i < argc; i++)
    x_argv[x_argc++] = argv[i];

  x_argv[x_argc] = NULL;

  *p_argc = x_argc;
  *p_argv = x_argv;
}

error_t
mu_argp_parse (const struct argp *myargp, 
	       int *pargc, char **pargv[],  
	       unsigned flags,
	       const char *capa[],
	       int *arg_index,     
	       void *input)
{
  struct argp *argp;
  error_t rc;
  const struct argp argpnull = { 0 };
  int i;
  
  /* Make sure we have program version and bug address initialized */
  mu_argp_init (argp_program_version, argp_program_bug_address);

  mu_set_program_name ((*pargv)[0]);
  mu_libargp_init ();
  for (i = 0; capa[i]; i++)
    {
#ifdef WITH_TLS
      if (strcmp (capa[i], "tls") == 0)
	mu_gocs_register ("tls", mu_tls_module_init);
      else
#endif /* WITH_TLS */
#ifdef WITH_GSASL
      if (strcmp (capa[i], "gsasl") == 0)
	mu_gocs_register ("gsasl", mu_gsasl_module_init);
      else
#endif
      if (strcmp (capa[i], "sieve") == 0)
	mu_gocs_register ("sieve", mu_sieve_module_init);
      else
	mu_gocs_register_std (capa[i]); 
    }
  
  if (!myargp)
    myargp = &argpnull;
  argp = mu_argp_build (myargp, NULL);
  rc = argp_parse (argp, *pargc, *pargv, flags, arg_index, input);
  mu_argp_done (argp);
  if (rc)
    return rc;

  mu_gocs_flush ();

  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.