Initial original import from: fuse-2.4.2-2.fc4
[captive.git] / src / client / cmdline / main.c
index a8addad..76a5be7 100644 (file)
@@ -29,6 +29,7 @@
 #include <locale.h>
 
 #include <captive/client-vfs.h>
+#include <captive/client.h>
 
 #include "main.h"      /* self */
 #include "cmd_shell.h"
 #include "cmd_mkdir.h"
 #include "cmd_rmdir.h"
 #include "cmd_commit.h"
+#include "cmd_open.h"
+#include "cmd_create.h"
+#include "cmd_close.h"
 #include "cmd_quit.h"
 #include "cmd_help.h"
+#include "utf8.h"
 
 
 CaptiveVfsObject *cmdline_captive_vfs_object;
@@ -70,25 +75,49 @@ static const struct poptOption popt_table[]={
 
 const struct cmdline_command cmdline_command_table[]={
                /* First entry is the default if no command name was specified. */
-               { "shell" ,N_("Interactive commands shell.")                        ,cmd_shell_table ,cmd_shell ,0,0 },
-               { "cd"    ,N_("Print or change current guest-os directory[1].")     ,cmd_cd_table    ,cmd_cd    ,0,1 },
-               { "lcd"   ,N_("Print or change current host-os  directory[1].")     ,cmd_lcd_table   ,cmd_lcd   ,0,1 },
-               { "ls"    ,N_("Directory[1] listing.")                              ,cmd_ls_table    ,cmd_ls    ,0,1 },
-               { "get"   ,N_("Copy guest-os file[1] to host-os (opt. file[2]).")   ,cmd_get_table   ,cmd_get   ,1,2 },
-               { "put"   ,N_("Copy host-os file[1] to guest-os (opt. file[2]).")   ,cmd_put_table   ,cmd_put   ,1,2 },
-               { "info"  ,N_("Query information about guest-os item[1].")          ,cmd_info_table  ,cmd_info  ,1,1 },
-               { "volume",N_("Query information about guest-os volume.")           ,cmd_volume_table,cmd_volume,0,0 },
-               { "rm"    ,N_("Remove guest-os file[1].")                           ,cmd_rm_table    ,cmd_rm    ,1,1 },
-               { "mv"    ,N_("Move (rename) guest-os item[1] to guest-os item[2]."),cmd_mv_table    ,cmd_mv    ,2,2 },
-               { "mkdir" ,N_("Create guest-os directory[1].")                      ,cmd_mkdir_table ,cmd_mkdir ,1,1 },
-               { "rmdir" ,N_("Remove guest-os directory[1].")                      ,cmd_rmdir_table ,cmd_rmdir ,1,1 },
-               { "commit",N_("Write any pending changes and remount the volume.")  ,cmd_commit_table,cmd_commit,0,0 },
-               { "quit"  ,N_("Quit this program.")                                 ,cmd_quit_table  ,cmd_quit  ,0,0 },
-               { "help"  ,N_("Show this list of commands.")                        ,cmd_help_table  ,cmd_help  ,0,0 },
+               { "shell" ,N_("Interactive commands shell.")                                ,cmd_shell_table ,cmd_shell ,0,0 },
+               { "cd"    ,N_("Print or change current guest-os directory[1].")             ,cmd_cd_table    ,cmd_cd    ,0,1 },
+               { "lcd"   ,N_("Print or change current host-os  directory[1].")             ,cmd_lcd_table   ,cmd_lcd   ,0,1 },
+               { "ls"    ,N_("Directory[1] listing.")                                      ,cmd_ls_table    ,cmd_ls    ,0,1 },
+               { "get"   ,N_("Copy guest-os file[1] to host-os (opt. file[2]).")           ,cmd_get_table   ,cmd_get   ,1,2 },
+               { "put"   ,N_("Copy host-os file[1] to guest-os (opt. file[2]).")           ,cmd_put_table   ,cmd_put   ,1,2 },
+               { "info"  ,N_("Query information about guest-os item[1].")                  ,cmd_info_table  ,cmd_info  ,1,1 },
+               { "volume",N_("Query information about guest-os volume.")                   ,cmd_volume_table,cmd_volume,0,0 },
+               { "rm"    ,N_("Remove guest-os file[1].")                                   ,cmd_rm_table    ,cmd_rm    ,1,1 },
+               { "mv"    ,N_("Move (rename) guest-os item[1] to guest-os item[2].")        ,cmd_mv_table    ,cmd_mv    ,2,2 },
+               { "mkdir" ,N_("Create guest-os directory[1].")                              ,cmd_mkdir_table ,cmd_mkdir ,1,1 },
+               { "rmdir" ,N_("Remove guest-os directory[1].")                              ,cmd_rmdir_table ,cmd_rmdir ,1,1 },
+               { "commit",N_("Write any pending changes and remount the volume.")          ,cmd_commit_table,cmd_commit,0,0 },
+               { "open"  ,N_("Open as[1] file[2] in mode; see 'open --help'")              ,cmd_open_table  ,cmd_open  ,2,2 },
+               { "create",N_("Create as[1] file[2] in mode with perm; see 'create --help'"),cmd_create_table,cmd_create,2,2 },
+               { "close" ,N_("Close handle[1]")                                            ,cmd_close_table ,cmd_close ,1,1 },
+               { "quit"  ,N_("Quit this program.")                                         ,cmd_quit_table  ,cmd_quit  ,0,0 },
+               { "help"  ,N_("Show this list of commands or help for command[1].")         ,cmd_help_table  ,cmd_help  ,0,1 },
                { NULL },       /* G_N_ELEMENTS() not usable as sizeof() is not visible for 'extern' */
                };
 
 
+static gboolean displayArgs_hit;
+
+static void displayArgs(poptContext con,enum poptCallbackReason foo,struct poptOption *key,const char *arg,void *data)
+{
+       displayArgs_hit=TRUE;
+
+       if (key->shortName=='?')
+               poptPrintHelp(con,stdout,0);
+       else
+               poptPrintUsage(con,stdout,0);
+}
+
+const struct poptOption cmdline_poptHelpOptions[]={
+               { argInfo:POPT_ARG_INTL_DOMAIN,arg:"popt" },
+               { NULL   ,'\0',POPT_ARG_CALLBACK,(void *)&displayArgs,'\0',NULL,NULL },
+               { "help" ,'?' ,0                ,NULL,'?',/* N_ */("Show this help message"),     NULL },
+               { "usage",'\0',0                ,NULL,'u',/* N_ */("Display brief usage message"),NULL },
+               POPT_TABLEEND
+               };
+
+
 static void invoke_cmd_err(int cmd_argc,const char **cmd_argv,GError **errp)
 {
 const struct cmdline_command *commandp;
@@ -96,8 +125,8 @@ const char *cmd_name=NULL;
 poptContext cmd_context;
 int errint;
 const char **cmdarg_argv;
-int cmdarg_argc;
-const char **csp;
+int cmdarg_argc,argci;
+const char **csp,*cs;
 const char *emptyargv_NULL=NULL;
 
        g_return_if_fail(cmd_argc>=0);
@@ -113,6 +142,11 @@ const char *stub_shell[]={ cmdline_command_table[0].name,NULL };
                cmd_argv=stub_shell;
                }
 
+       for (argci=0;argci<cmd_argc;argci++) {
+               if ((cs=CMD_LOCALE_TO_UTF8_ALLOCA(cmd_argv[argci])))
+                       cmd_argv[argci]=cs;
+               }
+
        cmd_name=*cmd_argv;
        for (commandp=cmdline_command_table;commandp->name;commandp++) {
                if (!cmd_name   /* NULL cmd_name fallback to the first table entry - "shell" */
@@ -121,9 +155,10 @@ const char *stub_shell[]={ cmdline_command_table[0].name,NULL };
                }
        if (!commandp->name) {
                g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_UNKNOWN_COMMAND,
-                               _("Unknown command, try 'help': %s"),cmd_name);
+                               _("Unknown command, try 'help': %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(cmd_name));
                return;
                }
+       displayArgs_hit=FALSE;
        cmd_context=poptGetContext(
                        PACKAGE,        /* name */
                        cmd_argc,cmd_argv,      /* argc,argv */
@@ -131,36 +166,41 @@ const char *stub_shell[]={ cmdline_command_table[0].name,NULL };
                        POPT_CONTEXT_POSIXMEHARDER);    /* flags; !POPT_CONTEXT_KEEP_FIRST */
        if (cmd_context==NULL) {
                g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_INVALID_COMMAND_ARGUMENTS,
-                               _("Invalid arguments for command: %s"),cmd_name);
+                               _("Invalid arguments for command: %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(cmd_name));
                return;
                }
        errint=poptReadDefaultConfig(cmd_context,
                        TRUE);  /* useEnv */
        if (errint!=0) {
                g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_READING_COMMAND_CONFIG,
-                               _("Error '%s' reading default configuration for command: %s"),poptStrerror(errint),cmd_name);
-               return;
+                               _("Error '%s' reading default configuration for command: %s"),
+                               poptStrerror(errint),CMD_LOCALE_FROM_UTF8_ALLOCA(cmd_name));
+               goto err_free_context;
                }
        errint=poptGetNextOpt(cmd_context);
        if (errint!=-1) {
                g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_EXCEEDING_COMMAND_OPTION,
-                               _("Exceeding command option for command: %s"),cmd_name);
-               return;
+                               _("Exceeding command option for command: %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(cmd_name));
+               goto err_free_context;
                }
        if (!(cmdarg_argv=poptGetArgs(cmd_context)))
                cmdarg_argv=&emptyargv_NULL;
+
+       if (displayArgs_hit)
+               goto err_free_context;
        
        for (csp=cmdarg_argv,cmdarg_argc=0;*csp;csp++)
                cmdarg_argc++;
        if (cmdarg_argc<commandp->argsn_min || cmdarg_argc>commandp->argsn_max) {
                g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_INVALID_COMMAND_ARGUMENT_COUNT,
                                _("Invalid number of command '%s' arguments: %d; expected from %d to %d incl."),
-                               cmd_name,cmdarg_argc,commandp->argsn_min,commandp->argsn_max);
-               return;
+                               CMD_LOCALE_FROM_UTF8_ALLOCA(cmd_name),cmdarg_argc,commandp->argsn_min,commandp->argsn_max);
+               goto err_free_context;
                }
 
        (*commandp->func)(cmdarg_argv,errp);
 
+err_free_context:
        poptFreeContext(cmd_context);
 }
 
@@ -198,8 +238,6 @@ gboolean errvfsresult_to_gerr(GError **errp,GnomeVFSResult errvfsresult)
        return FALSE;
 }
 
-static GIOChannel *main_giochannel;
-
 void main_exit(void) G_GNUC_NORETURN;
 void main_exit(void)
 {
@@ -207,10 +245,6 @@ void main_exit(void)
                g_object_unref(cmdline_captive_vfs_object);
                cmdline_captive_vfs_object=NULL;
                }
-       if (main_giochannel) {
-               g_io_channel_unref(main_giochannel);
-               main_giochannel=NULL;
-               }
        exit(EXIT_SUCCESS);
 }
 
@@ -227,17 +261,7 @@ struct captive_options options;
         * to restart failed children due to communication-failure alarms.
         */
 
-       /* Prevent output block buffering if redirecting stdout to file. */
-       setvbuf(stdout,(char *)NULL,_IONBF,0);
-       setvbuf(stderr,(char *)NULL,_IONBF,0);
-
-       /* Initialize the i18n stuff */
-       setlocale(LC_ALL,"");
-       bindtextdomain(PACKAGE,LOCALEDIR);
-       textdomain(PACKAGE);
-
-       /* Initialize GObject subsystem of GLib. */
-       g_type_init();
+       captive_standalone_init();
 
        captive_options_init(&options);
        captive_options=&options;       /* for parsing by 'CAPTIVE_POPT_INCLUDE' */