e0cad0138a838b08efbbffcadadeac34b4177889
[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 inspect.c */
116 extern void inspect_mount (void);
117 extern void print_inspect_prompt (void);
118 /* (low-level inspection functions, used by virt-inspector only) */
119 extern void inspect_do_decrypt (void);
120 extern void inspect_mount_root (const char *root);
121
122 /* in key.c */
123 extern char *read_key (const char *param);
124
125 /* in options.c */
126 extern char add_drives (struct drv *drv, char next_drive);
127 extern void mount_mps (struct mp *mp);
128 extern void free_drives (struct drv *drv);
129 extern void free_mps (struct mp *mp);
130
131 /* in virt.c */
132 extern int add_libvirt_drives (const char *guest);
133
134 #define OPTION_a                                \
135   if (access (optarg, R_OK) != 0) {             \
136     perror (optarg);                            \
137     exit (EXIT_FAILURE);                        \
138   }                                             \
139   drv = malloc (sizeof (struct drv));           \
140   if (!drv) {                                   \
141     perror ("malloc");                          \
142     exit (EXIT_FAILURE);                        \
143   }                                             \
144   drv->type = drv_a;                            \
145   drv->device = NULL;                           \
146   drv->nr_drives = -1;                          \
147   drv->a.filename = optarg;                     \
148   drv->a.format = format;                       \
149   drv->next = drvs;                             \
150   drvs = drv
151
152 #define OPTION_c                                \
153   libvirt_uri = optarg
154
155 #define OPTION_d                                \
156   drv = malloc (sizeof (struct drv));           \
157   if (!drv) {                                   \
158     perror ("malloc");                          \
159     exit (EXIT_FAILURE);                        \
160   }                                             \
161   drv->type = drv_d;                            \
162   drv->device = NULL;                           \
163   drv->nr_drives = -1;                          \
164   drv->d.guest = optarg;                        \
165   drv->next = drvs;                             \
166   drvs = drv
167
168 #define OPTION_i                                \
169   inspector = 1
170
171 #define OPTION_m                                \
172   mp = malloc (sizeof (struct mp));             \
173   if (!mp) {                                    \
174     perror ("malloc");                          \
175     exit (EXIT_FAILURE);                        \
176   }                                             \
177   mp->options = NULL;                           \
178   mp->mountpoint = bad_cast ("/");              \
179   p = strchr (optarg, ':');                     \
180   if (p) {                                      \
181     *p = '\0';                                  \
182     p++;                                        \
183     mp->mountpoint = p;                         \
184     p = strchr (p, ':');                        \
185     if (p) {                                    \
186       *p = '\0';                                \
187       p++;                                      \
188       mp->options = p;                          \
189     }                                           \
190   }                                             \
191   mp->device = optarg;                          \
192   mp->next = mps;                               \
193   mps = mp
194
195 #define OPTION_n                                \
196   guestfs_set_autosync (g, 0)
197
198 #define OPTION_r                                \
199   read_only = 1
200
201 #define OPTION_v                                \
202   verbose++;                                    \
203   guestfs_set_verbose (g, verbose)
204
205 #define OPTION_V                                                        \
206   {                                                                     \
207     struct guestfs_version *v = guestfs_version (g);                    \
208     printf ("%s %"PRIi64".%"PRIi64".%"PRIi64"%s\n",                     \
209             program_name,                                               \
210             v->major, v->minor, v->release, v->extra);                  \
211     exit (EXIT_SUCCESS);                                                \
212   }
213
214 #define OPTION_w                                                        \
215   if (read_only) {                                                      \
216     fprintf (stderr, _("%s: cannot mix --ro and --rw options\n"),       \
217              program_name);                                             \
218     exit (EXIT_FAILURE);                                                \
219   }
220
221 #define OPTION_x                                \
222   guestfs_set_trace (g, 1)
223
224 #endif /* OPTIONS_H */