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->rwmode=CAPTIVE_OPTION_RWMODE_BLIND;
35 options->media=CAPTIVE_OPTION_MEDIA_DISK;
36 options->debug_messages=FALSE;
37 options->load_module=NULL;
41 void captive_options_copy(struct captive_options *dest,const struct captive_options *src)
43 g_return_if_fail(dest!=NULL);
44 g_return_if_fail(src!=NULL);
45 g_return_if_fail(dest!=src);
47 memcpy(dest,src,sizeof(*dest));
49 dest->filesystem=g_strdup(src->filesystem);
50 g_io_channel_ref(dest->image_iochannel);
54 void captive_options_free(struct captive_options *options)
56 g_return_if_fail(options!=NULL);
58 g_free(options->filesystem);
59 g_io_channel_unref(options->image_iochannel);
63 static gchar *captive_popt_optarg;
66 static void arg_filesystem(void)
68 g_free(captive_options->filesystem);
69 captive_options->filesystem=g_strdup(captive_popt_optarg);
72 static void arg_load_module(void)
74 captive_options->load_module=g_list_append(captive_options->load_module,g_strdup(captive_popt_optarg));
77 static void arg_ro(void)
79 captive_options->rwmode=CAPTIVE_OPTION_RWMODE_RO;
81 static void arg_blind(void)
83 captive_options->rwmode=CAPTIVE_OPTION_RWMODE_BLIND;
85 static void arg_rw(void)
87 captive_options->rwmode=CAPTIVE_OPTION_RWMODE_RW;
90 static void arg_cdrom(void)
92 captive_options->media=CAPTIVE_OPTION_MEDIA_CDROM;
94 static void arg_disk(void)
96 captive_options->media=CAPTIVE_OPTION_MEDIA_DISK;
99 static void arg_debug_messages(void)
101 captive_options->debug_messages=TRUE;
105 static void captive_popt_callback
106 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data);
108 const struct poptOption captive_popt[]={
109 { argInfo:POPT_ARG_INTL_DOMAIN, arg:(void *)PACKAGE },
110 { argInfo:POPT_ARG_CALLBACK, arg:(void *)captive_popt_callback },
111 #define POPT_OFFSET 2
112 #define CAPTIVE_POPT(longname,argInfoP,argP,descripP,argDescripP) \
114 longName: (longname), \
116 argInfo: (argInfoP), \
119 descrip: (descripP), \
120 argDescrip: (argDescripP), \
122 #define CAPTIVE_POPT_STRING(longname,descripP,argDescripP) \
123 CAPTIVE_POPT(longname,POPT_ARG_STRING,&captive_popt_optarg,descripP,argDescripP)
124 #define CAPTIVE_POPT_NONE(longname,descripP) \
125 CAPTIVE_POPT(longname,POPT_ARG_NONE ,NULL ,descripP,NULL )
127 CAPTIVE_POPT_STRING("filesystem" ,N_("Path to .sys or .so filesystem module file"),N_("pathname")),
128 CAPTIVE_POPT_STRING("load-module" ,N_("Path to any W32 module to load w/o initialization"),N_("pathname")),
129 CAPTIVE_POPT_NONE( "ro" ,N_("Read/write mode: Any write access will be forbidden")),
130 CAPTIVE_POPT_NONE( "blind" ,N_("Read/write mode: All writes are just simulated in memory (default)")),
131 CAPTIVE_POPT_NONE( "rw" ,N_("Read/write mode: Write directly to the image file/device")),
132 CAPTIVE_POPT_NONE( "cdrom" ,N_("Media type: CD-ROM")),
133 CAPTIVE_POPT_NONE( "disk" ,N_("Media type: Disk (default)")),
134 CAPTIVE_POPT_NONE( "debug-messages",N_("Turn on debugging messages")),
136 #undef CAPTIVE_POPT_NONE
137 #undef CAPTIVE_POPT_STRING
142 static const struct poptOption captive_popt_standalone[]={
143 CAPTIVE_POPT_INCLUDE,
149 static void (*const popt_func_table[])(void)={
161 /* poptCallbackType captive_popt_callback */
162 static void captive_popt_callback
163 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data)
167 g_return_if_fail(reason==POPT_CALLBACK_REASON_OPTION);
169 funci=(opt-(captive_popt+POPT_OFFSET));
170 g_return_if_fail(funci>=0);
171 g_return_if_fail(funci<(gint)G_N_ELEMENTS(popt_func_table));
172 if (popt_func_table[funci])
173 (*popt_func_table[funci])();
174 free(captive_popt_optarg);
175 captive_popt_optarg=NULL; /* sanity, shouldn't be needed */
179 gboolean captive_options_parse(struct captive_options *options,const gchar *captive_args)
182 int captive_args_argc;
183 const char **captive_args_argv=NULL;
187 g_return_val_if_fail(options!=NULL,FALSE);
188 g_return_val_if_fail(captive_args!=NULL,FALSE);
190 g_assert(captive_options==NULL);
191 captive_options=options;
193 errint=poptParseArgvString(captive_args,&captive_args_argc,&captive_args_argv);
195 g_assert_not_reached(); /* argument parsing args_error */
198 context=poptGetContext(
200 captive_args_argc,captive_args_argv, /* argc,argv */
201 captive_popt_standalone, /* options */
202 POPT_CONTEXT_KEEP_FIRST);
204 g_assert_not_reached(); /* argument recognization args_error */
207 errint=poptReadDefaultConfig(context,
210 g_assert_not_reached(); /* argument recognization args_error */
211 goto args_err_context;
213 errint=poptGetNextOpt(context);
215 g_assert_not_reached(); /* some non-callbacked argument reached */
216 goto args_err_context;
218 if (poptPeekArg(context)) {
219 g_assert_not_reached(); /* some non-"--"-prefixed argument reached */
220 goto args_err_context;
222 r=TRUE; /* success */
224 poptFreeContext(context);
226 free(captive_args_argv); /* may be NULL here */
229 puts("FIXME: HELP HERE");
230 captive_options=NULL;
231 g_return_val_if_reached(r);
234 g_assert(captive_options!=NULL);
235 captive_options=NULL;