e36c57a09775765a766b8dd4a82d2d9725a3ffda
[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
108 /* in key.c */
109 extern char *read_key (const char *param);
110
111 /* in options.c */
112 extern char add_drives (struct drv *drv, char next_drive);
113 extern void mount_mps (struct mp *mp);
114 extern void free_drives (struct drv *drv);
115 extern void free_mps (struct mp *mp);
116
117 /* in virt.c */
118 extern int add_libvirt_drives (const char *guest);
119
120 #define OPTION_a                                \
121   if (access (optarg, R_OK) != 0) {             \
122     perror (optarg);                            \
123     exit (EXIT_FAILURE);                        \
124   }                                             \
125   drv = malloc (sizeof (struct drv));           \
126   if (!drv) {                                   \
127     perror ("malloc");                          \
128     exit (EXIT_FAILURE);                        \
129   }                                             \
130   drv->type = drv_a;                            \
131   drv->a.filename = optarg;                     \
132   drv->a.format = format;                       \
133   drv->next = drvs;                             \
134   drvs = drv
135
136 #define OPTION_c                                \
137   libvirt_uri = optarg
138
139 #define OPTION_d                                \
140   drv = malloc (sizeof (struct drv));           \
141   if (!drv) {                                   \
142     perror ("malloc");                          \
143     exit (EXIT_FAILURE);                        \
144   }                                             \
145   drv->type = drv_d;                            \
146   drv->d.guest = optarg;                        \
147   drv->next = drvs;                             \
148   drvs = drv
149
150 #define OPTION_i                                \
151   inspector = 1
152
153 #define OPTION_m                                \
154   mp = malloc (sizeof (struct mp));             \
155   if (!mp) {                                    \
156     perror ("malloc");                          \
157     exit (EXIT_FAILURE);                        \
158   }                                             \
159   p = strchr (optarg, ':');                     \
160   if (p) {                                      \
161     *p = '\0';                                  \
162     mp->mountpoint = p+1;                       \
163   } else                                        \
164     mp->mountpoint = bad_cast ("/");            \
165   mp->device = optarg;                          \
166   mp->next = mps;                               \
167   mps = mp
168
169 #define OPTION_n                                \
170   guestfs_set_autosync (g, 0)
171
172 #define OPTION_r                                \
173   read_only = 1
174
175 #define OPTION_v                                \
176   verbose++;                                    \
177   guestfs_set_verbose (g, verbose)
178
179 #define OPTION_V                                                        \
180   {                                                                     \
181     struct guestfs_version *v = guestfs_version (g);                    \
182     printf ("%s %"PRIi64".%"PRIi64".%"PRIi64"%s\n",                     \
183             program_name,                                               \
184             v->major, v->minor, v->release, v->extra);                  \
185     exit (EXIT_SUCCESS);                                                \
186   }
187
188 #define OPTION_x                                \
189   guestfs_set_trace (g, 1)
190
191 #endif /* OPTIONS_H */