inspector: Rewrite virt-inspector in C.
[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   enum { drv_a, drv_d, drv_N } type;
81   union {
82     struct {
83       char *filename;       /* disk filename */
84       const char *format;   /* format (NULL == autodetect) */
85     } a;
86     struct {
87       char *guest;          /* guest name */
88     } d;
89     struct {
90       char *filename;       /* disk filename (testX.img) */
91       void *data;           /* prepared type */
92       void (*data_free)(void*); /* function to free 'data' */
93       char *device;         /* device inside the appliance */
94     } N;
95   };
96 };
97
98 struct mp {
99   struct mp *next;
100   char *device;
101   char *mountpoint;
102 };
103
104 /* in inspect.c */
105 extern void inspect_mount (void);
106 extern void print_inspect_prompt (void);
107 /* (low-level inspection functions, used by virt-inspector only) */
108 extern void inspect_do_decrypt (void);
109 extern void inspect_mount_root (const char *root);
110
111 /* in key.c */
112 extern char *read_key (const char *param);
113
114 /* in options.c */
115 extern char add_drives (struct drv *drv, char next_drive);
116 extern void mount_mps (struct mp *mp);
117 extern void free_drives (struct drv *drv);
118 extern void free_mps (struct mp *mp);
119
120 /* in virt.c */
121 extern int add_libvirt_drives (const char *guest);
122
123 #define OPTION_a                                \
124   if (access (optarg, R_OK) != 0) {             \
125     perror (optarg);                            \
126     exit (EXIT_FAILURE);                        \
127   }                                             \
128   drv = malloc (sizeof (struct drv));           \
129   if (!drv) {                                   \
130     perror ("malloc");                          \
131     exit (EXIT_FAILURE);                        \
132   }                                             \
133   drv->type = drv_a;                            \
134   drv->a.filename = optarg;                     \
135   drv->a.format = format;                       \
136   drv->next = drvs;                             \
137   drvs = drv
138
139 #define OPTION_c                                \
140   libvirt_uri = optarg
141
142 #define OPTION_d                                \
143   drv = malloc (sizeof (struct drv));           \
144   if (!drv) {                                   \
145     perror ("malloc");                          \
146     exit (EXIT_FAILURE);                        \
147   }                                             \
148   drv->type = drv_d;                            \
149   drv->d.guest = optarg;                        \
150   drv->next = drvs;                             \
151   drvs = drv
152
153 #define OPTION_i                                \
154   inspector = 1
155
156 #define OPTION_m                                \
157   mp = malloc (sizeof (struct mp));             \
158   if (!mp) {                                    \
159     perror ("malloc");                          \
160     exit (EXIT_FAILURE);                        \
161   }                                             \
162   p = strchr (optarg, ':');                     \
163   if (p) {                                      \
164     *p = '\0';                                  \
165     mp->mountpoint = p+1;                       \
166   } else                                        \
167     mp->mountpoint = bad_cast ("/");            \
168   mp->device = optarg;                          \
169   mp->next = mps;                               \
170   mps = mp
171
172 #define OPTION_n                                \
173   guestfs_set_autosync (g, 0)
174
175 #define OPTION_r                                \
176   read_only = 1
177
178 #define OPTION_v                                \
179   verbose++;                                    \
180   guestfs_set_verbose (g, verbose)
181
182 #define OPTION_V                                                        \
183   {                                                                     \
184     struct guestfs_version *v = guestfs_version (g);                    \
185     printf ("%s %"PRIi64".%"PRIi64".%"PRIi64"%s\n",                     \
186             program_name,                                               \
187             v->major, v->minor, v->release, v->extra);                  \
188     exit (EXIT_SUCCESS);                                                \
189   }
190
191 #define OPTION_w                                                        \
192   if (read_only) {                                                      \
193     fprintf (stderr, _("%s: cannot mix --ro and --rw options\n"),       \
194              program_name);                                             \
195     exit (EXIT_FAILURE);                                                \
196   }
197
198 #define OPTION_x                                \
199   guestfs_set_trace (g, 1)
200
201 #endif /* OPTIONS_H */