2bf5bd0918463cf843c1f4ca5a7a7575c2eb6be2
[captive.git] / src / client / cmdline / main.c
1 /* $Id$
2  * client cmdline interface for libcaptive
3  * Copyright (C) 2002-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 <glib/gmessages.h>
23 #include <stdlib.h>
24 #include <glib/giochannel.h>
25 #include <glib/gerror.h>
26 #include <popt.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <locale.h>
30
31 #include <captive/client-vfs.h>
32
33 #include "main.h"       /* self */
34 #include "cmd_shell.h"
35 #include "cmd_cd.h"
36 #include "cmd_lcd.h"
37 #include "cmd_ls.h"
38 #include "cmd_get.h"
39 #include "cmd_put.h"
40 #include "cmd_info.h"
41 #include "cmd_volume.h"
42 #include "cmd_rm.h"
43 #include "cmd_mv.h"
44 #include "cmd_mkdir.h"
45 #include "cmd_rmdir.h"
46 #include "cmd_commit.h"
47 #include "cmd_open.h"
48 #include "cmd_create.h"
49 #include "cmd_close.h"
50 #include "cmd_quit.h"
51 #include "cmd_help.h"
52 #include "utf8.h"
53
54
55 CaptiveVfsObject *cmdline_captive_vfs_object;
56
57
58 GQuark cmdline_main_error_quark(void)
59 {
60 GQuark r=0;
61
62         if (!r)
63                 r=g_quark_from_static_string("cmdline-main");
64
65         return r;
66 }
67
68
69 static const struct poptOption popt_table[]={
70                 CAPTIVE_POPT_INCLUDE,
71                 POPT_AUTOHELP
72                 POPT_TABLEEND
73                 };
74
75 const struct cmdline_command cmdline_command_table[]={
76                 /* First entry is the default if no command name was specified. */
77                 { "shell" ,N_("Interactive commands shell.")                                ,cmd_shell_table ,cmd_shell ,0,0 },
78                 { "cd"    ,N_("Print or change current guest-os directory[1].")             ,cmd_cd_table    ,cmd_cd    ,0,1 },
79                 { "lcd"   ,N_("Print or change current host-os  directory[1].")             ,cmd_lcd_table   ,cmd_lcd   ,0,1 },
80                 { "ls"    ,N_("Directory[1] listing.")                                      ,cmd_ls_table    ,cmd_ls    ,0,1 },
81                 { "get"   ,N_("Copy guest-os file[1] to host-os (opt. file[2]).")           ,cmd_get_table   ,cmd_get   ,1,2 },
82                 { "put"   ,N_("Copy host-os file[1] to guest-os (opt. file[2]).")           ,cmd_put_table   ,cmd_put   ,1,2 },
83                 { "info"  ,N_("Query information about guest-os item[1].")                  ,cmd_info_table  ,cmd_info  ,1,1 },
84                 { "volume",N_("Query information about guest-os volume.")                   ,cmd_volume_table,cmd_volume,0,0 },
85                 { "rm"    ,N_("Remove guest-os file[1].")                                   ,cmd_rm_table    ,cmd_rm    ,1,1 },
86                 { "mv"    ,N_("Move (rename) guest-os item[1] to guest-os item[2].")        ,cmd_mv_table    ,cmd_mv    ,2,2 },
87                 { "mkdir" ,N_("Create guest-os directory[1].")                              ,cmd_mkdir_table ,cmd_mkdir ,1,1 },
88                 { "rmdir" ,N_("Remove guest-os directory[1].")                              ,cmd_rmdir_table ,cmd_rmdir ,1,1 },
89                 { "commit",N_("Write any pending changes and remount the volume.")          ,cmd_commit_table,cmd_commit,0,0 },
90                 { "open"  ,N_("Open as[1] file[2] in mode; see 'open --help'")              ,cmd_open_table  ,cmd_open  ,2,2 },
91                 { "create",N_("Create as[1] file[2] in mode with perm; see 'create --help'"),cmd_create_table,cmd_create,2,2 },
92                 { "close" ,N_("Close handle[1]")                                            ,cmd_close_table ,cmd_close ,1,1 },
93                 { "quit"  ,N_("Quit this program.")                                         ,cmd_quit_table  ,cmd_quit  ,0,0 },
94                 { "help"  ,N_("Show this list of commands or help for command[1].")         ,cmd_help_table  ,cmd_help  ,0,1 },
95                 { NULL },       /* G_N_ELEMENTS() not usable as sizeof() is not visible for 'extern' */
96                 };
97
98
99 static gboolean displayArgs_hit;
100
101 static void displayArgs(poptContext con,enum poptCallbackReason foo,struct poptOption *key,const char *arg,void *data)
102 {
103         displayArgs_hit=TRUE;
104
105         if (key->shortName=='?')
106                 poptPrintHelp(con,stdout,0);
107         else
108                 poptPrintUsage(con,stdout,0);
109 }
110
111 const struct poptOption cmdline_poptHelpOptions[]={
112                 { argInfo:POPT_ARG_INTL_DOMAIN,arg:"popt" },
113                 { NULL   ,'\0',POPT_ARG_CALLBACK,(void *)&displayArgs,'\0',NULL,NULL },
114                 { "help" ,'?' ,0                ,NULL,'?',/* N_ */("Show this help message"),     NULL },
115                 { "usage",'\0',0                ,NULL,'u',/* N_ */("Display brief usage message"),NULL },
116                 POPT_TABLEEND
117                 };
118
119
120 static void invoke_cmd_err(int cmd_argc,const char **cmd_argv,GError **errp)
121 {
122 const struct cmdline_command *commandp;
123 const char *cmd_name=NULL;
124 poptContext cmd_context;
125 int errint;
126 const char **cmdarg_argv;
127 int cmdarg_argc,argci;
128 const char **csp,*cs;
129 const char *emptyargv_NULL=NULL;
130
131         g_return_if_fail(cmd_argc>=0);
132         g_return_if_fail(!errp || !*errp);
133
134         /* poptGetContext() cannot be passed argc==0 even if we lass POPT_CONTEXT_KEEP_FIRST
135          * as it is buggy. Workaround it by keeping the command name as argv[0].
136          */
137         if (!cmd_argc) {
138 const char *stub_shell[]={ cmdline_command_table[0].name,NULL };
139                 
140                 cmd_argc=1;
141                 cmd_argv=stub_shell;
142                 }
143
144         for (argci=0;argci<cmd_argc;argci++) {
145                 if ((cs=CMD_LOCALE_TO_UTF8_ALLOCA(cmd_argv[argci])))
146                         cmd_argv[argci]=cs;
147                 }
148
149         cmd_name=*cmd_argv;
150         for (commandp=cmdline_command_table;commandp->name;commandp++) {
151                 if (!cmd_name   /* NULL cmd_name fallback to the first table entry - "shell" */
152                                 || !strcasecmp(cmd_name,commandp->name))
153                         break;
154                 }
155         if (!commandp->name) {
156                 g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_UNKNOWN_COMMAND,
157                                 _("Unknown command, try 'help': %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(cmd_name));
158                 return;
159                 }
160         displayArgs_hit=FALSE;
161         cmd_context=poptGetContext(
162                         PACKAGE,        /* name */
163                         cmd_argc,cmd_argv,      /* argc,argv */
164                         commandp->table,        /* options */
165                         POPT_CONTEXT_POSIXMEHARDER);    /* flags; !POPT_CONTEXT_KEEP_FIRST */
166         if (cmd_context==NULL) {
167                 g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_INVALID_COMMAND_ARGUMENTS,
168                                 _("Invalid arguments for command: %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(cmd_name));
169                 return;
170                 }
171         errint=poptReadDefaultConfig(cmd_context,
172                         TRUE);  /* useEnv */
173         if (errint!=0) {
174                 g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_READING_COMMAND_CONFIG,
175                                 _("Error '%s' reading default configuration for command: %s"),
176                                 poptStrerror(errint),CMD_LOCALE_FROM_UTF8_ALLOCA(cmd_name));
177                 goto err_free_context;
178                 }
179         errint=poptGetNextOpt(cmd_context);
180         if (errint!=-1) {
181                 g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_EXCEEDING_COMMAND_OPTION,
182                                 _("Exceeding command option for command: %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(cmd_name));
183                 goto err_free_context;
184                 }
185         if (!(cmdarg_argv=poptGetArgs(cmd_context)))
186                 cmdarg_argv=&emptyargv_NULL;
187
188         if (displayArgs_hit)
189                 goto err_free_context;
190         
191         for (csp=cmdarg_argv,cmdarg_argc=0;*csp;csp++)
192                 cmdarg_argc++;
193         if (cmdarg_argc<commandp->argsn_min || cmdarg_argc>commandp->argsn_max) {
194                 g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_INVALID_COMMAND_ARGUMENT_COUNT,
195                                 _("Invalid number of command '%s' arguments: %d; expected from %d to %d incl."),
196                                 CMD_LOCALE_FROM_UTF8_ALLOCA(cmd_name),cmdarg_argc,commandp->argsn_min,commandp->argsn_max);
197                 goto err_free_context;
198                 }
199
200         (*commandp->func)(cmdarg_argv,errp);
201
202 err_free_context:
203         poptFreeContext(cmd_context);
204 }
205
206
207 void err_cleanup(GError **errp)
208 {
209         g_return_if_fail(errp!=NULL);
210
211         if (!*errp)
212                 return;
213         printf("\nERROR: %s\n",(*errp)->message);
214         g_clear_error(errp);
215 }
216
217
218 void invoke_cmd(int cmd_argc,const char **cmd_argv)
219 {
220 GError *gerr=NULL;
221
222         invoke_cmd_err(cmd_argc,cmd_argv,&gerr);
223         err_cleanup(&gerr);
224 }
225
226
227 /* Returns: Success (no error occured). */
228 gboolean errvfsresult_to_gerr(GError **errp,GnomeVFSResult errvfsresult)
229 {
230         g_return_val_if_fail(!errp || !*errp,FALSE);
231
232         if (errvfsresult==GNOME_VFS_OK)
233                 return TRUE;
234
235         g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_GENERIC_ERROR,
236                         _("Generic error: %s"),gnome_vfs_result_to_string(errvfsresult));
237         return FALSE;
238 }
239
240 void main_exit(void) G_GNUC_NORETURN;
241 void main_exit(void)
242 {
243         if (cmdline_captive_vfs_object) {
244                 g_object_unref(cmdline_captive_vfs_object);
245                 cmdline_captive_vfs_object=NULL;
246                 }
247         exit(EXIT_SUCCESS);
248 }
249
250 int main(int argc,char **argv)
251 {
252 poptContext context;
253 int errint;
254 const char **cmd_argv,**csp;
255 int cmd_argc;
256 GError *gerr=NULL;
257 struct captive_options options;
258
259         /* Do not set g_log_set_always_fatal() here as we would not be able
260          * to restart failed children due to communication-failure alarms.
261          */
262
263         /* Prevent output block buffering if redirecting stdout to file. */
264         setvbuf(stdout,(char *)NULL,_IONBF,0);
265         setvbuf(stderr,(char *)NULL,_IONBF,0);
266
267         /* Initialize the i18n stuff */
268         setlocale(LC_ALL,"");
269         bindtextdomain(PACKAGE,LOCALEDIR);
270         textdomain(PACKAGE);
271
272         /* Initialize GObject subsystem of GLib. */
273         g_type_init();
274
275         captive_options_init(&options);
276         captive_options=&options;       /* for parsing by 'CAPTIVE_POPT_INCLUDE' */
277
278         context=poptGetContext(
279                         PACKAGE,        /* name */
280                         argc,(/*en-const*/const char **)argv,   /* argc,argv */
281                         popt_table,     /* options */
282                         POPT_CONTEXT_POSIXMEHARDER);    /* flags; && !POPT_CONTEXT_KEEP_FIRST */
283         if (context==NULL) {
284                 g_error(_("Error parsing command-line arguments"));
285                 return EXIT_FAILURE;
286                 }
287         errint=poptReadDefaultConfig(context,
288                         TRUE);  /* useEnv */
289         if (errint!=0)
290                 g_warning(_("Error reading default popt configuration"));
291         errint=poptGetNextOpt(context);
292         if (errint!=-1) {
293                 g_error(_("Error parsing (dash-prefixed) command-line argument"));
294                 return EXIT_FAILURE;
295                 }
296         cmd_argv=poptGetArgs(context);
297         for (csp=cmd_argv,cmd_argc=0;csp && *csp;csp++)
298                 cmd_argc++;
299
300         captive_options=NULL;   /* already parsed by 'CAPTIVE_POPT_INCLUDE' */
301
302         /* image_iochannel */
303         if (cmd_argc<=0) {
304                 g_error(_("File/device disk image pathname command-line argument required"));
305                 return EXIT_FAILURE;
306                 }
307         g_assert(options.image_iochannel==NULL);
308         if (!(options.image_iochannel=g_io_channel_new_file(
309                         cmd_argv[0],    /* filename */
310                         (options.rwmode==CAPTIVE_OPTION_RWMODE_RW ? "r+" : "r"),        /* mode */
311                         NULL))) {       /* error */
312                 g_error(_("image_iochannel open failed"));
313                 return EXIT_FAILURE;
314                 }
315         cmd_argc--;
316         cmd_argv++;
317
318         if (options.filesystem.type==CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY) {
319                 g_error(_("'--filesystem' option required ('ntfs.sys' pathname suggested)"));
320                 return EXIT_FAILURE;
321                 }
322         if (!options.load_module) {
323                 g_warning(_("'--load-module' option required ('ntoskrnl.exe' pathname suggested)"));
324                 return EXIT_FAILURE;
325                 }
326
327         if (GNOME_VFS_OK!=captive_vfs_new(&cmdline_captive_vfs_object,&options)) {
328                 g_error(_("captive_vfs_new() failed"));
329                 return EXIT_FAILURE;
330                 }
331         captive_options_free(&options);
332
333         cmd_cd_internal("/",&gerr);
334         if (gerr) {
335                 err_cleanup(&gerr);
336                 return EXIT_FAILURE;
337                 }
338
339         invoke_cmd(cmd_argc,cmd_argv);
340
341         /* 'cmd_argv' gets cleared by 'poptFreeContext(context);' below */
342         poptFreeContext(context);
343
344         main_exit();    /* unref 'cmdline_captive_vfs_object' */
345         /* NOTREACHED */
346 }