debian: Add build-dep erlang-dev.
[libguestfs.git] / fish / options.h
1 /* libguestfs - guestfish and guestmount shared option parsing
2  * Copyright (C) 2010-2011 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #ifndef OPTIONS_H
20 #define OPTIONS_H
21
22 #ifdef HAVE_GETTEXT
23 #include "gettext.h"
24 #ifndef _
25 #define _(str) dgettext(PACKAGE, (str))
26 #endif
27 #ifndef N_
28 #define N_(str) dgettext(PACKAGE, (str))
29 #endif
30 #else
31 #ifndef _
32 #define _(str) str
33 #endif
34 #ifndef _
35 #define N_(str) str
36 #endif
37 #endif
38
39 #ifndef STREQ
40 #define STREQ(a,b) (strcmp((a),(b)) == 0)
41 #endif
42 #ifndef STRCASEEQ
43 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
44 #endif
45 #ifndef STRNEQ
46 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
47 #endif
48 #ifndef STRCASENEQ
49 #define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
50 #endif
51 #ifndef STREQLEN
52 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
53 #endif
54 #ifndef STRCASEEQLEN
55 #define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
56 #endif
57 #ifndef STRNEQLEN
58 #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
59 #endif
60 #ifndef STRCASENEQLEN
61 #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
62 #endif
63 #ifndef STRPREFIX
64 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
65 #endif
66
67 /* Provided by guestfish or guestmount. */
68 extern guestfs_h *g;
69 extern int read_only;
70 extern int live;
71 extern int verbose;
72 extern int inspector;
73 extern int keys_from_stdin;
74 extern int echo_keys;
75 extern const char *libvirt_uri;
76 extern const char *program_name;
77
78 /* List of drives added via -a, -d or -N options. */
79 struct drv {
80   struct drv *next;
81
82   char *device;    /* Device name inside the appliance (eg. /dev/sda).
83                     * This is filled in when we add the drives in
84                     * add_drives.  Note that guests (-d option) may
85                     * have multiple drives, in which case this is the
86                     * first drive, and nr_drives is the number of
87                     * drives used.
88                     */
89   int nr_drives;   /* number of drives for this guest */
90
91   enum { drv_a, drv_d, drv_N } type;
92   union {
93     struct {
94       char *filename;       /* disk filename */
95       const char *format;   /* format (NULL == autodetect) */
96     } a;
97     struct {
98       char *guest;          /* guest name */
99     } d;
100     struct {
101       char *filename;       /* disk filename (testX.img) */
102       void *data;           /* prepared type */
103       void (*data_free)(void*); /* function to free 'data' */
104     } N;
105   };
106 };
107
108 struct mp {
109   struct mp *next;
110   char *device;
111   char *mountpoint;
112   char *options;
113 };
114
115 /* in config.c */
116 extern void parse_config (void);
117
118 /* in inspect.c */
119 extern void inspect_mount (void);
120 extern void print_inspect_prompt (void);
121 /* (low-level inspection functions, used by virt-inspector only) */
122 extern void inspect_do_decrypt (void);
123 extern void inspect_mount_root (const char *root);
124
125 /* in key.c */
126 extern char *read_key (const char *param);
127
128 /* in options.c */
129 extern char add_drives (struct drv *drv, char next_drive);
130 extern void mount_mps (struct mp *mp);
131 extern void free_drives (struct drv *drv);
132 extern void free_mps (struct mp *mp);
133
134 /* in virt.c */
135 extern int add_libvirt_drives (const char *guest);
136
137 #define OPTION_a                                \
138   if (access (optarg, R_OK) != 0) {             \
139     perror (optarg);                            \
140     exit (EXIT_FAILURE);                        \
141   }                                             \
142   drv = malloc (sizeof (struct drv));           \
143   if (!drv) {                                   \
144     perror ("malloc");                          \
145     exit (EXIT_FAILURE);                        \
146   }                                             \
147   drv->type = drv_a;                            \
148   drv->device = NULL;                           \
149   drv->nr_drives = -1;                          \
150   drv->a.filename = optarg;                     \
151   drv->a.format = format;                       \
152   drv->next = drvs;                             \
153   drvs = drv
154
155 #define OPTION_c                                \
156   libvirt_uri = optarg
157
158 #define OPTION_d                                \
159   drv = malloc (sizeof (struct drv));           \
160   if (!drv) {                                   \
161     perror ("malloc");                          \
162     exit (EXIT_FAILURE);                        \
163   }                                             \
164   drv->type = drv_d;                            \
165   drv->device = NULL;                           \
166   drv->nr_drives = -1;                          \
167   drv->d.guest = optarg;                        \
168   drv->next = drvs;                             \
169   drvs = drv
170
171 #define OPTION_i                                \
172   inspector = 1
173
174 #define OPTION_m                                \
175   mp = malloc (sizeof (struct mp));             \
176   if (!mp) {                                    \
177     perror ("malloc");                          \
178     exit (EXIT_FAILURE);                        \
179   }                                             \
180   mp->options = NULL;                           \
181   mp->mountpoint = bad_cast ("/");              \
182   p = strchr (optarg, ':');                     \
183   if (p) {                                      \
184     *p = '\0';                                  \
185     p++;                                        \
186     mp->mountpoint = p;                         \
187     p = strchr (p, ':');                        \
188     if (p) {                                    \
189       *p = '\0';                                \
190       p++;                                      \
191       mp->options = p;                          \
192     }                                           \
193   }                                             \
194   mp->device = optarg;                          \
195   mp->next = mps;                               \
196   mps = mp
197
198 #define OPTION_n                                \
199   guestfs_set_autosync (g, 0)
200
201 #define OPTION_r                                \
202   read_only = 1
203
204 #define OPTION_v                                \
205   verbose++;                                    \
206   guestfs_set_verbose (g, verbose)
207
208 #define OPTION_V                                                        \
209   {                                                                     \
210     struct guestfs_version *v = guestfs_version (g);                    \
211     printf ("%s %"PRIi64".%"PRIi64".%"PRIi64"%s\n",                     \
212             program_name,                                               \
213             v->major, v->minor, v->release, v->extra);                  \
214     exit (EXIT_SUCCESS);                                                \
215   }
216
217 #define OPTION_w                                                        \
218   if (read_only) {                                                      \
219     fprintf (stderr, _("%s: cannot mix --ro and --rw options\n"),       \
220              program_name);                                             \
221     exit (EXIT_FAILURE);                                                \
222   }
223
224 #define OPTION_x                                \
225   guestfs_set_trace (g, 1)
226
227 #endif /* OPTIONS_H */