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