da06bb0a75218f76e0ccc5ccafdb1640adaa97be
[captive.git] / src / libcaptive / client / options.c
1 /* $Id$
2  * User options handling code of libcaptive
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
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
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "captive/options.h"    /* self */
23 #include <glib/gmessages.h>
24 #include "captive/macros.h"
25 #include <glib/gstrfuncs.h>
26 #include <stdlib.h>
27
28
29 void captive_options_init(struct captive_options *options)
30 {
31         g_return_if_fail(options!=NULL);
32
33         CAPTIVE_MEMZERO(options);
34         options->filesystem.type=CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY;
35         options->rwmode=CAPTIVE_OPTION_RWMODE_BLIND;
36         options->media=CAPTIVE_OPTION_MEDIA_DISK;
37         options->debug_messages=FALSE;
38         options->load_module=NULL;
39         options->sandbox=FALSE;
40 }
41
42
43 void captive_options_copy(struct captive_options *dest,const struct captive_options *src)
44 {
45 GList *load_module_node;
46
47         g_return_if_fail(dest!=NULL);
48         g_return_if_fail(src!=NULL);
49         g_return_if_fail(dest!=src);
50
51         memcpy(dest,src,sizeof(*dest));
52
53         captive_options_module_copy(&dest->filesystem,&src->filesystem);
54         if (dest->image_iochannel)
55                 g_io_channel_ref(dest->image_iochannel);
56
57         dest->load_module=NULL;
58         for (load_module_node=src->load_module;load_module_node;load_module_node=load_module_node->next) {
59 struct captive_options_module *options_module_src=load_module_node->data;
60 struct captive_options_module *options_module_dest;
61
62                 captive_new(options_module_dest);
63                 captive_options_module_copy(options_module_dest,options_module_src);
64                 dest->load_module=g_list_append(dest->load_module,options_module_dest);
65                 }
66
67         if (src->sandbox_server_argv) {
68 char **sp;
69
70                 for (sp=src->sandbox_server_argv;*sp;sp++) {
71                         if (sp>src->sandbox_server_argv)
72                                 g_assert(*sp>=sp[-1]);
73                         }
74                 dest->sandbox_server_argv=g_memdup(src->sandbox_server_argv,
75                                 (sp==src->sandbox_server_argv ? (char *)(sp+1) : sp[-1]+strlen(sp[-1])+1) - (char *)src->sandbox_server_argv);
76                 /* Really update the pointers inside the copied array block. :-) */
77                 for (sp=src->sandbox_server_argv;*sp;sp++) {
78                         dest->sandbox_server_argv[sp-src->sandbox_server_argv]=(gpointer)((char *)dest->sandbox_server_argv
79                                                         +(src->sandbox_server_argv[sp-src->sandbox_server_argv]-(char *)src->sandbox_server_argv));
80                         }
81                 }
82
83         if (src->sandbox_server_ior)
84                 dest->sandbox_server_ior=g_strdup(src->sandbox_server_ior);
85
86         if (src->bug_pathname)
87                 dest->bug_pathname=g_strdup(src->bug_pathname);
88 }
89
90
91 void captive_options_free(struct captive_options *options)
92 {
93         g_return_if_fail(options!=NULL);
94
95         captive_options_module_free(&options->filesystem);
96         if (options->image_iochannel)
97                 g_io_channel_unref(options->image_iochannel);
98
99         while (options->load_module) {
100                 captive_options_module_free(options->load_module->data);
101                 options->load_module=g_list_delete_link(options->load_module,options->load_module);     /* also frees 'options->load_module->data' */
102                 }
103
104         g_free(options->sandbox_server_argv);
105         g_free(options->sandbox_server_ior);
106         g_free(options->bug_pathname);
107 }
108
109
110 static gchar *captive_popt_optarg;
111
112
113 static void arg_filesystem(void)
114 {
115 gboolean errbool;
116
117         captive_options_module_free(&captive_options->filesystem);
118         errbool=captive_options_module_load(&captive_options->filesystem,captive_popt_optarg);
119         g_assert(errbool==TRUE);
120 }
121
122 static void arg_load_module(void)
123 {
124 struct captive_options_module *options_module;
125 gboolean errbool;
126
127         captive_new(options_module);
128         errbool=captive_options_module_load(options_module,captive_popt_optarg);
129         g_assert(errbool==TRUE);
130
131         captive_options->load_module=g_list_append(captive_options->load_module,options_module);
132 }
133
134 static void arg_ro(void)
135 {
136         captive_options->rwmode=CAPTIVE_OPTION_RWMODE_RO;
137 }
138 static void arg_blind(void)
139 {
140         captive_options->rwmode=CAPTIVE_OPTION_RWMODE_BLIND;
141 }
142 static void arg_rw(void)
143 {
144         captive_options->rwmode=CAPTIVE_OPTION_RWMODE_RW;
145 }
146
147 static void arg_cdrom(void)
148 {
149         captive_options->media=CAPTIVE_OPTION_MEDIA_CDROM;
150 }
151 static void arg_disk(void)
152 {
153         captive_options->media=CAPTIVE_OPTION_MEDIA_DISK;
154 }
155
156 static void arg_debug_messages(void)
157 {
158         captive_options->debug_messages=TRUE;
159 }
160
161 static void sandbox_args_clear(void)
162 {
163         free(captive_options->sandbox_server_argv);
164         captive_options->sandbox_server_argv=NULL;
165
166         g_free(captive_options->sandbox_server_ior);
167         captive_options->sandbox_server_ior=NULL;
168
169         captive_options->sandbox=FALSE;
170 }
171
172 static void arg_sandbox_server(void)
173 {
174 int errint;
175
176         sandbox_args_clear();
177
178         errint=poptParseArgvString(
179                         captive_popt_optarg,    /* s */
180                         NULL,   /* argcPtr */
181                         (const char ***)&captive_options->sandbox_server_argv); /* argvPtr */
182         g_assert(errint==0);
183
184         captive_options->sandbox=TRUE;
185 }
186
187 static void arg_sandbox_server_ior(void)
188 {
189         sandbox_args_clear();
190
191         captive_options->sandbox_server_ior=g_strdup(captive_popt_optarg);
192         captive_options->sandbox=TRUE;
193 }
194
195 static void arg_no_sandbox(void)
196 {
197         sandbox_args_clear();
198 }
199
200 static void arg_bug_pathname(void)
201 {
202         g_free(captive_options->bug_pathname);
203         captive_options->bug_pathname=g_strdup(captive_popt_optarg);
204 }
205
206
207 static void captive_popt_callback
208                 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data);
209
210 const struct poptOption captive_popt[]={
211                 { argInfo:POPT_ARG_INTL_DOMAIN, arg:(void *)PACKAGE },
212                 { argInfo:POPT_ARG_CALLBACK,    arg:(void *)captive_popt_callback },
213 #define POPT_OFFSET 2
214 #define CAPTIVE_POPT(longname,argInfoP,argP,descripP,argDescripP) \
215                 { \
216                         longName: (longname), \
217                         shortName: 0, \
218                         argInfo: (argInfoP), \
219                         arg: (void *)argP, \
220                         val: 0, \
221                         descrip: (descripP), \
222                         argDescrip: (argDescripP), \
223                 }
224 #define CAPTIVE_POPT_STRING(longname,descripP,argDescripP) \
225                 CAPTIVE_POPT(longname,POPT_ARG_STRING,&captive_popt_optarg,descripP,argDescripP)
226 #define CAPTIVE_POPT_NONE(longname,descripP) \
227                 CAPTIVE_POPT(longname,POPT_ARG_NONE  ,NULL                ,descripP,NULL       )
228
229                 CAPTIVE_POPT_STRING("filesystem"        ,N_("Pathname to .sys or .so filesystem module file"),N_("pathname")),
230                 CAPTIVE_POPT_STRING("load-module"       ,N_("Pathname to any W32 module to load w/o initialization"),N_("pathname")),
231                 CAPTIVE_POPT_NONE(  "ro"                ,N_("Read/write mode: Any write access will be forbidden")),
232                 CAPTIVE_POPT_NONE(  "blind"             ,N_("Read/write mode: All writes are just simulated in memory (default)")),
233                 CAPTIVE_POPT_NONE(  "rw"                ,N_("Read/write mode: Write directly to the image file/device")),
234                 CAPTIVE_POPT_NONE(  "cdrom"             ,N_("Media type: CD-ROM")),
235                 CAPTIVE_POPT_NONE(  "disk"              ,N_("Media type: Disk (default)")),
236                 CAPTIVE_POPT_NONE(  "debug-messages"    ,N_("Turn on debugging messages")),
237                 CAPTIVE_POPT_STRING("sandbox-server"    ,N_("Pathname to 'captive-sandbox-server', turns on sandboxing"),N_("pathname")),
238                 CAPTIVE_POPT_STRING("sandbox-server-ior",N_("CORBA IOR of 'captive-sandbox-server', turns on sandboxing"),N_("IOR")),
239                 CAPTIVE_POPT_NONE(  "no-sandbox"        ,N_("Turn off sandboxing feature")),
240                 CAPTIVE_POPT_STRING("bug-pathname"      ,N_("Pathname to strftime(3) for .captivebug.xml.gz bugreports"),N_("pathname")),
241
242 #undef CAPTIVE_POPT_NONE
243 #undef CAPTIVE_POPT_STRING
244 #undef CAPTIVE_POPT
245                 POPT_TABLEEND
246                 };
247
248 static const struct poptOption captive_popt_standalone[]={
249                 CAPTIVE_POPT_INCLUDE,
250                 POPT_AUTOHELP
251                 POPT_TABLEEND
252                 };
253
254
255 static void (*const popt_func_table[])(void)={
256                 arg_filesystem,
257                 arg_load_module,
258                 arg_ro,
259                 arg_blind,
260                 arg_rw,
261                 arg_cdrom,
262                 arg_disk,
263                 arg_debug_messages,
264                 arg_sandbox_server,
265                 arg_sandbox_server_ior,
266                 arg_no_sandbox,
267                 arg_bug_pathname,
268                 };
269
270
271 /* poptCallbackType captive_popt_callback */
272 static void captive_popt_callback
273                 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data)
274 {
275 gint funci;
276
277         g_return_if_fail(reason==POPT_CALLBACK_REASON_OPTION);
278
279         funci=(opt-(captive_popt+POPT_OFFSET));
280         g_return_if_fail(funci>=0);
281         g_return_if_fail(funci<(gint)G_N_ELEMENTS(popt_func_table));
282         if (popt_func_table[funci])
283                 (*popt_func_table[funci])();
284         free(captive_popt_optarg);
285         captive_popt_optarg=NULL;       /* sanity, shouldn't be needed */
286 }
287
288
289 gboolean captive_options_parse(struct captive_options *options,const gchar *captive_args)
290 {
291 int errint;
292 int captive_args_argc;
293 const char **captive_args_argv=NULL;
294 poptContext context;
295 gboolean r=FALSE;
296
297         g_return_val_if_fail(options!=NULL,FALSE);
298         g_return_val_if_fail(captive_args!=NULL,FALSE);
299
300         g_assert(captive_options==NULL);
301         captive_options=options;
302
303         errint=poptParseArgvString(captive_args,&captive_args_argc,&captive_args_argv);
304         if (errint!=0) {
305                 g_assert_not_reached(); /* argument parsing args_error */
306                 goto args_err;
307                 }
308         context=poptGetContext(
309                         PACKAGE,        /* name */
310                         captive_args_argc,captive_args_argv,    /* argc,argv */
311                         captive_popt_standalone,        /* options */
312                         POPT_CONTEXT_KEEP_FIRST);
313         if (context==NULL) {
314                 g_assert_not_reached(); /* argument recognization args_error */
315                 goto args_err_argv;
316                 }
317         errint=poptReadDefaultConfig(context,
318                         TRUE);  /* useEnv */
319         if (errint!=0) {
320                 g_assert_not_reached(); /* argument recognization args_error */
321                 goto args_err_context;
322                 }
323         errint=poptGetNextOpt(context);
324         if (errint!=-1) {
325                 g_assert_not_reached(); /* some non-callbacked argument reached */
326                 goto args_err_context;
327                 }
328         if (poptPeekArg(context)) {
329                 g_assert_not_reached(); /* some non-"--"-prefixed argument reached */
330                 goto args_err_context;
331                 }
332         r=TRUE; /* success */
333 args_err_context:
334         poptFreeContext(context);
335 args_err_argv:
336         free(captive_args_argv);        /* may be NULL here */
337 args_err:
338         if (!r) {
339                 puts("FIXME: HELP HERE");
340                 captive_options=NULL;
341                 g_return_val_if_reached(r);
342                 }
343
344         g_assert(captive_options!=NULL);
345         captive_options=NULL;
346
347         return TRUE;
348 }