2 * User options handling code of libcaptive
3 * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
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
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.
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
22 #include "captive/options.h" /* self */
23 #include <glib/gmessages.h>
24 #include "captive/macros.h"
25 #include <glib/gstrfuncs.h>
29 void captive_options_init(struct captive_options *options)
31 g_return_if_fail(options!=NULL);
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;
43 void captive_options_copy(struct captive_options *dest,const struct captive_options *src)
45 GList *load_module_node;
47 g_return_if_fail(dest!=NULL);
48 g_return_if_fail(src!=NULL);
49 g_return_if_fail(dest!=src);
51 memcpy(dest,src,sizeof(*dest));
53 captive_options_module_copy(&dest->filesystem,&src->filesystem);
54 if (dest->image_iochannel)
55 g_io_channel_ref(dest->image_iochannel);
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;
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);
67 if (src->sandbox_server_argv) {
70 for (sp=src->sandbox_server_argv;*sp;sp++) {
71 if (sp>src->sandbox_server_argv)
72 g_assert(*sp>=sp[-1]);
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));
83 if (src->sandbox_server_ior)
84 dest->sandbox_server_ior=g_strdup(src->sandbox_server_ior);
86 if (src->bug_pathname)
87 dest->bug_pathname=g_strdup(src->bug_pathname);
91 void captive_options_free(struct captive_options *options)
93 g_return_if_fail(options!=NULL);
95 captive_options_module_free(&options->filesystem);
96 if (options->image_iochannel)
97 g_io_channel_unref(options->image_iochannel);
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' */
104 g_free(options->sandbox_server_argv);
105 g_free(options->sandbox_server_ior);
106 g_free(options->bug_pathname);
110 static gchar *captive_popt_optarg;
113 static void arg_filesystem(void)
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);
122 static void arg_load_module(void)
124 struct captive_options_module *options_module;
127 captive_new(options_module);
128 errbool=captive_options_module_load(options_module,captive_popt_optarg);
129 g_assert(errbool==TRUE);
131 captive_options->load_module=g_list_append(captive_options->load_module,options_module);
134 static void arg_ro(void)
136 captive_options->rwmode=CAPTIVE_OPTION_RWMODE_RO;
138 static void arg_blind(void)
140 captive_options->rwmode=CAPTIVE_OPTION_RWMODE_BLIND;
142 static void arg_rw(void)
144 captive_options->rwmode=CAPTIVE_OPTION_RWMODE_RW;
147 static void arg_cdrom(void)
149 captive_options->media=CAPTIVE_OPTION_MEDIA_CDROM;
151 static void arg_disk(void)
153 captive_options->media=CAPTIVE_OPTION_MEDIA_DISK;
156 static void arg_debug_messages(void)
158 captive_options->debug_messages=TRUE;
161 static void sandbox_args_clear(void)
163 free(captive_options->sandbox_server_argv);
164 captive_options->sandbox_server_argv=NULL;
166 g_free(captive_options->sandbox_server_ior);
167 captive_options->sandbox_server_ior=NULL;
169 captive_options->sandbox=FALSE;
172 static void arg_sandbox_server(void)
176 sandbox_args_clear();
178 errint=poptParseArgvString(
179 captive_popt_optarg, /* s */
181 (const char ***)&captive_options->sandbox_server_argv); /* argvPtr */
184 captive_options->sandbox=TRUE;
187 static void arg_sandbox_server_ior(void)
189 sandbox_args_clear();
191 captive_options->sandbox_server_ior=g_strdup(captive_popt_optarg);
192 captive_options->sandbox=TRUE;
195 static void arg_no_sandbox(void)
197 sandbox_args_clear();
200 static void arg_bug_pathname(void)
202 g_free(captive_options->bug_pathname);
203 captive_options->bug_pathname=g_strdup(captive_popt_optarg);
207 static void captive_popt_callback
208 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data);
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) \
216 longName: (longname), \
218 argInfo: (argInfoP), \
221 descrip: (descripP), \
222 argDescrip: (argDescripP), \
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 )
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")),
242 #undef CAPTIVE_POPT_NONE
243 #undef CAPTIVE_POPT_STRING
248 static const struct poptOption captive_popt_standalone[]={
249 CAPTIVE_POPT_INCLUDE,
255 static void (*const popt_func_table[])(void)={
265 arg_sandbox_server_ior,
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)
277 g_return_if_fail(reason==POPT_CALLBACK_REASON_OPTION);
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 */
289 gboolean captive_options_parse(struct captive_options *options,const gchar *captive_args)
292 int captive_args_argc;
293 const char **captive_args_argv=NULL;
297 g_return_val_if_fail(options!=NULL,FALSE);
298 g_return_val_if_fail(captive_args!=NULL,FALSE);
300 g_assert(captive_options==NULL);
301 captive_options=options;
303 errint=poptParseArgvString(captive_args,&captive_args_argc,&captive_args_argv);
305 g_assert_not_reached(); /* argument parsing args_error */
308 context=poptGetContext(
310 captive_args_argc,captive_args_argv, /* argc,argv */
311 captive_popt_standalone, /* options */
312 POPT_CONTEXT_KEEP_FIRST);
314 g_assert_not_reached(); /* argument recognization args_error */
317 errint=poptReadDefaultConfig(context,
320 g_assert_not_reached(); /* argument recognization args_error */
321 goto args_err_context;
323 errint=poptGetNextOpt(context);
325 g_assert_not_reached(); /* some non-callbacked argument reached */
326 goto args_err_context;
328 if (poptPeekArg(context)) {
329 g_assert_not_reached(); /* some non-"--"-prefixed argument reached */
330 goto args_err_context;
332 r=TRUE; /* success */
334 poptFreeContext(context);
336 free(captive_args_argv); /* may be NULL here */
339 puts("FIXME: HELP HERE");
340 captive_options=NULL;
341 g_return_val_if_reached(r);
344 g_assert(captive_options!=NULL);
345 captive_options=NULL;