diff options
author | Sam Roberts <sroberts@uniserve.com> | 2002-03-19 04:41:42 +0000 |
---|---|---|
committer | Sam Roberts <sroberts@uniserve.com> | 2002-03-19 04:41:42 +0000 |
commit | a8d00fd7f100b4a2111e18b773e2a5734cb94319 (patch) | |
tree | 4513a0008d1987db041c0289e3a7c652acbfca87 /lib | |
parent | 5f9936f7ddb3e52eee5c05014b1342d9cd411f24 (diff) | |
download | mailutils-a8d00fd7f100b4a2111e18b773e2a5734cb94319.tar.gz mailutils-a8d00fd7f100b4a2111e18b773e2a5734cb94319.tar.bz2 |
Added ~/.<progname>rc to be last config file.
Fixed segv for NULL capa and added header file blurb.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mu_argp.c | 80 | ||||
-rw-r--r-- | lib/mu_argp.h | 22 |
2 files changed, 75 insertions, 27 deletions
diff --git a/lib/mu_argp.c b/lib/mu_argp.c index 6dc64a779..26f85b370 100644 --- a/lib/mu_argp.c +++ b/lib/mu_argp.c @@ -362,7 +362,7 @@ mu_daemon_argp_parser (int key, char *arg, struct argp_state *state) #endif #ifndef MU_USER_CONFIG_FILE -# define MU_USER_CONFIG_FILE ".mailutils" +# define MU_USER_CONFIG_FILE "~/.mailutils" #endif static int @@ -375,6 +375,12 @@ member (const char *array[], const char *text, size_t len) 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) @@ -385,10 +391,20 @@ read_rc (const char *progname, const char *name, const char *capa[], size_t n = 0; int x_argc = *argc; char **x_argv = *argv; + char* rcfile = mu_tilde_expansion (name, "/", NULL); - fp = fopen (name, "r"); + if (!rcfile) + { + fprintf (stderr, "%s: not enough memory\n", progname); + exit (1); + } + + fp = fopen (rcfile, "r"); if (!fp) + { + free(rcfile); return; + } while (getline (&buf, &n, fp) > 0) { @@ -435,12 +451,19 @@ read_rc (const char *progname, const char *name, const char *capa[], } len = 0; + if(progname) + { for (p = kwp; *p && !isspace (*p); p++) len++; - - if ((kwp[0] == ':' - && member (capa, kwp+1, len-1)) - || strncmp (progname, kwp, len) == 0) + } + 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; @@ -471,6 +494,7 @@ read_rc (const char *progname, const char *name, const char *capa[], } } fclose (fp); + free(rcfile); *argc = x_argc; *argv = x_argv; @@ -485,8 +509,7 @@ mu_create_argcv (const char *capa[], int x_argc; char **x_argv; int i; - struct passwd *pw; - + progname = strrchr (argv[0], '/'); if (progname) progname++; @@ -505,28 +528,30 @@ mu_create_argcv (const char *capa[], x_argv[x_argc] = argv[x_argc]; x_argc++; + /* Add global config file. */ read_rc (progname, MU_CONFIG_FILE, capa, &x_argc, &x_argv); - pw = getpwuid (getuid ()); - if (pw) - { - struct stat sb; - if (stat (pw->pw_dir, &sb) == 0 && S_ISDIR (sb.st_mode)) - { - /* note: sizeof return value counts terminating zero */ - char *name = xmalloc (strlen (pw->pw_dir) + 1 - + sizeof (MU_USER_CONFIG_FILE)); - sprintf (name, "%s/%s", pw->pw_dir, MU_USER_CONFIG_FILE); - read_rc (progname, name, capa, &x_argc, &x_argv); - free (name); - } - } + /* Add per-user config file. */ + read_rc (progname, MU_USER_CONFIG_FILE, capa, &x_argc, &x_argv); + + /* Add per-program (and per-user) config file. */ + { + char* progrc = malloc (strlen (progname) + 3 /* ~/ */ + 3 /* rc */ + 1); + if (!progrc) + { + fprintf (stderr, "%s: not enough memory\n", progname); + exit (1); + } + sprintf (progrc, "~/.%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; @@ -562,9 +587,9 @@ mu_build_argp (const struct argp *template, const char *capa[]) struct argp *argp; /* Count the capabilities */ - for (n = 0; capa[n]; n++) + for (n = 0; capa && capa[n]; n++) ; - if (template->children) + if (template && template->children) for (; template->children[n].argp; n++) ; @@ -576,11 +601,11 @@ mu_build_argp (const struct argp *template, const char *capa[]) } n = 0; - if (template->children) + if (template && template->children) for (; template->children[n].argp; n++) ap[n] = template->children[n]; - for (; capa[n]; n++) + for (; capa && capa[n]; n++) { struct argp_child *tmp = find_argp_child (capa[n]); if (!tmp) @@ -621,3 +646,4 @@ mu_argp_parse(const struct argp *argp, free ((void*) argp); return ret; } + diff --git a/lib/mu_argp.h b/lib/mu_argp.h index be8790e89..43ffaac87 100644 --- a/lib/mu_argp.h +++ b/lib/mu_argp.h @@ -1,3 +1,23 @@ +/* GNU mailutils - a suite of utilities for electronic mail + Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. + + 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 2, 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, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ + +#ifndef MU_ARGP_H +#define MU_ARGP_H 1 + #include <mailutils/mailbox.h> #include <argp.h> @@ -27,3 +47,5 @@ extern error_t mu_argp_parse __P((const struct argp *argp, int *arg_index, void *input)); +#endif + |