457fbcb54fa83826c51c48590e1f5bb6cd7b7426
[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->rwmode=CAPTIVE_OPTION_RWMODE_BLIND;
35         options->media=CAPTIVE_OPTION_MEDIA_DISK;
36         options->debug_messages=FALSE;
37         options->load_module=NULL;
38 }
39
40
41 void captive_options_copy(struct captive_options *dest,const struct captive_options *src)
42 {
43         g_return_if_fail(dest!=NULL);
44         g_return_if_fail(src!=NULL);
45         g_return_if_fail(dest!=src);
46
47         memcpy(dest,src,sizeof(*dest));
48
49         dest->filesystem=g_strdup(src->filesystem);
50         g_io_channel_ref(dest->image_iochannel);
51 }
52
53
54 void captive_options_free(struct captive_options *options)
55 {
56         g_return_if_fail(options!=NULL);
57
58         g_free(options->filesystem);
59         g_io_channel_unref(options->image_iochannel);
60 }
61
62
63 static gchar *captive_popt_optarg;
64
65
66 static void arg_filesystem(void)
67 {
68         g_free(captive_options->filesystem);
69         captive_options->filesystem=g_strdup(captive_popt_optarg);
70 }
71
72 static void arg_load_module(void)
73 {
74         captive_options->load_module=g_list_append(captive_options->load_module,g_strdup(captive_popt_optarg));
75 }
76
77 static void arg_ro(void)
78 {
79         captive_options->rwmode=CAPTIVE_OPTION_RWMODE_RO;
80 }
81 static void arg_blind(void)
82 {
83         captive_options->rwmode=CAPTIVE_OPTION_RWMODE_BLIND;
84 }
85 static void arg_rw(void)
86 {
87         captive_options->rwmode=CAPTIVE_OPTION_RWMODE_RW;
88 }
89
90 static void arg_cdrom(void)
91 {
92         captive_options->media=CAPTIVE_OPTION_MEDIA_CDROM;
93 }
94 static void arg_disk(void)
95 {
96         captive_options->media=CAPTIVE_OPTION_MEDIA_DISK;
97 }
98
99 static void arg_debug_messages(void)
100 {
101         captive_options->debug_messages=TRUE;
102 }
103
104
105 static void captive_popt_callback
106                 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data);
107
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) \
113                 { \
114                         longName: (longname), \
115                         shortName: 0, \
116                         argInfo: (argInfoP), \
117                         arg: (void *)argP, \
118                         val: 0, \
119                         descrip: (descripP), \
120                         argDescrip: (argDescripP), \
121                 }
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       )
126
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")),
135
136 #undef CAPTIVE_POPT_NONE
137 #undef CAPTIVE_POPT_STRING
138 #undef CAPTIVE_POPT
139                 POPT_TABLEEND
140                 };
141
142 static const struct poptOption captive_popt_standalone[]={
143                 CAPTIVE_POPT_INCLUDE,
144                 POPT_AUTOHELP
145                 POPT_TABLEEND
146                 };
147
148
149 static void (*const popt_func_table[])(void)={
150                 arg_filesystem,
151                 arg_load_module,
152                 arg_ro,
153                 arg_blind,
154                 arg_rw,
155                 arg_cdrom,
156                 arg_disk,
157                 arg_debug_messages,
158                 };
159
160
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)
164 {
165 gint funci;
166
167         g_return_if_fail(reason==POPT_CALLBACK_REASON_OPTION);
168
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 */
176 }
177
178
179 gboolean captive_options_parse(struct captive_options *options,const gchar *captive_args)
180 {
181 int errint;
182 int captive_args_argc;
183 const char **captive_args_argv=NULL;
184 poptContext context;
185 gboolean r=FALSE;
186
187         g_return_val_if_fail(options!=NULL,FALSE);
188         g_return_val_if_fail(captive_args!=NULL,FALSE);
189
190         g_assert(captive_options==NULL);
191         captive_options=options;
192
193         errint=poptParseArgvString(captive_args,&captive_args_argc,&captive_args_argv);
194         if (errint!=0) {
195                 g_assert_not_reached(); /* argument parsing args_error */
196                 goto args_err;
197                 }
198         context=poptGetContext(
199                         PACKAGE,        /* name */
200                         captive_args_argc,captive_args_argv,    /* argc,argv */
201                         captive_popt_standalone,        /* options */
202                         POPT_CONTEXT_KEEP_FIRST);
203         if (context==NULL) {
204                 g_assert_not_reached(); /* argument recognization args_error */
205                 goto args_err_argv;
206                 }
207         errint=poptReadDefaultConfig(context,
208                         TRUE);  /* useEnv */
209         if (errint!=0) {
210                 g_assert_not_reached(); /* argument recognization args_error */
211                 goto args_err_context;
212                 }
213         errint=poptGetNextOpt(context);
214         if (errint!=-1) {
215                 g_assert_not_reached(); /* some non-callbacked argument reached */
216                 goto args_err_context;
217                 }
218         if (poptPeekArg(context)) {
219                 g_assert_not_reached(); /* some non-"--"-prefixed argument reached */
220                 goto args_err_context;
221                 }
222         r=TRUE; /* success */
223 args_err_context:
224         poptFreeContext(context);
225 args_err_argv:
226         free(captive_args_argv);        /* may be NULL here */
227 args_err:
228         if (!r) {
229                 puts("FIXME: HELP HERE");
230                 captive_options=NULL;
231                 g_return_val_if_reached(r);
232                 }
233
234         g_assert(captive_options!=NULL);
235         captive_options=NULL;
236
237         return TRUE;
238 }