summaryrefslogtreecommitdiffstats
path: root/fish/config.c
blob: 3e004cc699ea591db255740154db5aa1ad0ec582 (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
/* libguestfs - guestfish and guestmount shared option parsing
 * Copyright (C) 2011 Red Hat 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 of the License, 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>

#ifdef HAVE_LIBCONFIG
#include <libconfig.h>
#endif

#include "guestfs.h"

#include "options.h"

#ifdef HAVE_LIBCONFIG

static const char *home_filename = /* $HOME/ */ ".libguestfs-tools.rc";
static const char *etc_filename = "/etc/libguestfs-tools.conf";

/* Note that parse_config is called very early, before command line
 * parsing and before the verbose flag has been set.
 */

void
parse_config (void)
{
  const char *home;
  size_t len;
  char *path;
  FILE *fp;
  config_t conf;

  config_init (&conf);

  /* Try $HOME first. */
  home = getenv ("HOME");
  if (home != NULL) {
    len = strlen (home) + 1 + strlen (home_filename) + 1;
    path = malloc (len);
    if (path == NULL) {
      perror ("malloc");
      exit (EXIT_FAILURE);
    }
    snprintf (path, len, "%s/%s", home, home_filename);

    fp = fopen (path, "r");
    if (fp != NULL) {
      /*
      if (verbose)
        fprintf (stderr, "%s: reading configuration from %s\n",
                 program_name, path);
      */

      if (config_read (&conf, fp) == CONFIG_FALSE) {
        fprintf (stderr,
                 _("%s: %s: line %d: error parsing configuration file: %s\n"),
                 program_name, path, config_error_line (&conf),
                 config_error_text (&conf));
        exit (EXIT_FAILURE);
      }

      if (fclose (fp) == -1) {
        perror (path);
        exit (EXIT_FAILURE);
      }

      /* Notes:
       *
       * (1) It's not obvious from the documentation, that config_read
       * completely resets the 'conf' structure.  This means we cannot
       * call config_read twice on the two possible configuration
       * files, but instead have to copy out settings into our
       * variables between calls.
       *
       * (2) If the next call fails then 'read_only' variable is not
       * updated.  Failure could happen just because the setting is
       * missing from the configuration file, so we ignore it here.
       */
      config_lookup_bool (&conf, "read_only", &read_only);
    }

    free (path);
  }

  fp = fopen (etc_filename, "r");
  if (fp != NULL) {
    /*
    if (verbose)
      fprintf (stderr, "%s: reading configuration from %s\n",
               program_name, etc_filename);
    */

    if (config_read (&conf, fp) == CONFIG_FALSE) {
      fprintf (stderr,
               _("%s: %s: line %d: error parsing configuration file: %s\n"),
               program_name, etc_filename, config_error_line (&conf),
               config_error_text (&conf));
      exit (EXIT_FAILURE);
    }

    if (fclose (fp) == -1) {
      perror (etc_filename);
      exit (EXIT_FAILURE);
    }

    config_lookup_bool (&conf, "read_only", &read_only);
  }

  config_destroy (&conf);
}

#else /* !HAVE_LIBCONFIG */

void
parse_config (void)
{
  /*
  if (verbose)
    fprintf (stderr,
             _("%s: compiled without libconfig, guestfish configuration file ignored\n"),
             program_name);
  */
}

#endif /* !HAVE_LIBCONFIG */