728df04a9d594db44d9dac4e38827f490ffe5710
[libguestfs.git] / fish / options.h
1 /* libguestfs - guestfish and guestmount shared option parsing
2  * Copyright (C) 2010 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 verbose;
71 extern int inspector;
72 extern int keys_from_stdin;
73 extern int echo_keys;
74 extern const char *libvirt_uri;
75 extern const char *program_name;
76
77 /* List of drives added via -a, -d or -N options. */
78 struct drv {
79   struct drv *next;
80
81   char *device;    /* Device name inside the appliance (eg. /dev/sda).
82                     * This is filled in when we add the drives in
83                     * add_drives.  Note that guests (-d option) may
84                     * have multiple drives, in which case this is the
85                     * first drive, and nr_drives is the number of
86                     * drives used.
87                     */
88   int nr_drives;   /* number of drives for this guest */
89
90   enum { drv_a, drv_d, drv_N } type;
91   union {
92     struct {
93       char *filename;       /* disk filename */
94       const char *format;   /* format (NULL == autodetect) */
95     } a;
96     struct {
97       char *guest;          /* guest name */
98     } d;
99     struct {
100       char *filename;       /* disk filename (testX.img) */
101       void *data;           /* prepared type */
102       void (*data_free)(void*); /* function to free 'data' */
103     } N;
104   };
105 };
106
107 struct mp {
108   struct mp *next;
109   char *device;
110   char *mountpoint;
111 };
112
113 /* in inspect.c */
114 extern void inspect_mount (void);
115 extern void print_inspect_prompt (void);
116 /* (low-level inspection functions, used by virt-inspector only) */
117 extern void inspect_do_decrypt (void);
118 extern void inspect_mount_root (const char *root);
119
120 /* in key.c */
121 extern char *read_key (const char *param);
122
123 /* in options.c */
124 extern char add_drives (struct drv *drv, char next_drive);
125 extern void mount_mps (struct mp *mp);
126 extern void free_drives (struct drv *drv);
127 extern void free_mps (struct mp *mp);
128
129 /* in virt.c */
130 extern int add_libvirt_drives (const char *guest);
131
132 #define OPTION_a                                \
133   if (access (optarg, R_OK) != 0) {             \
134     perror (optarg);                            \
135     exit (EXIT_FAILURE);                        \
136   }                                             \
137   drv = malloc (sizeof (struct drv));           \
138   if (!drv) {                                   \
139     perror ("malloc");                          \
140     exit (EXIT_FAILURE);                        \
141   }                                             \
142   drv->type = drv_a;                            \
143   drv->device = NULL;                           \
144   drv->nr_drives = -1;                          \
145   drv->a.filename = optarg;                     \
146   drv->a.format = format;                       \
147   drv->next = drvs;                             \
148   drvs = drv
149
150 #define OPTION_c                                \
151   libvirt_uri = optarg
152
153 #define OPTION_d                                \
154   drv = malloc (sizeof (struct drv));           \
155   if (!drv) {                                   \
156     perror ("malloc");                          \
157     exit (EXIT_FAILURE);                        \
158   }                                             \
159   drv->type = drv_d;                            \
160   drv->device = NULL;                           \
161   drv->nr_drives = -1;                          \
162   drv->d.guest = optarg;                        \
163   drv->next = drvs;                             \
164   drvs = drv
165
166 #define OPTION_i                                \
167   inspector = 1
168
169 #define OPTION_m                                \
170   mp = malloc (sizeof (struct mp));             \
171   if (!mp) {                                    \
172     perror ("malloc");                          \
173     exit (EXIT_FAILURE);                        \
174   }                                             \
175   p = strchr (optarg, ':');                     \
176   if (p) {                                      \
177     *p = '\0';                                  \
178     mp->mountpoint = p+1;                       \
179   } else                                        \
180     mp->mountpoint = bad_cast ("/");            \
181   mp->device = optarg;                          \
182   mp->next = mps;                               \
183   mps = mp
184
185 #define OPTION_n                                \
186   guestfs_set_autosync (g, 0)
187
188 #define OPTION_r                                \
189   read_only = 1
190
191 #define OPTION_v                                \
192   verbose++;                                    \
193   guestfs_set_verbose (g, verbose)
194
195 #define OPTION_V                                                        \
196   {                                                                     \
197     struct guestfs_version *v = guestfs_version (g);                    \
198     printf ("%s %"PRIi64".%"PRIi64".%"PRIi64"%s\n",                     \
199             program_name,                                               \
200             v->major, v->minor, v->release, v->extra);                  \
201     exit (EXIT_SUCCESS);                                                \
202   }
203
204 #define OPTION_w                                                        \
205   if (read_only) {                                                      \
206     fprintf (stderr, _("%s: cannot mix --ro and --rw options\n"),       \
207              program_name);                                             \
208     exit (EXIT_FAILURE);                                                \
209   }
210
211 #define OPTION_x                                \
212   guestfs_set_trace (g, 1)
213
214 #endif /* OPTIONS_H */