From: short <> Date: Sat, 22 Mar 2003 20:00:24 +0000 (+0000) Subject: +Real implementation of 'captive-cmdline' client X-Git-Tag: captive-0_2~144 X-Git-Url: http://git.jankratochvil.net/?a=commitdiff_plain;h=d72d5377b127b03dbd55e08153f255aa8bd5b045;p=captive.git +Real implementation of 'captive-cmdline' client --- diff --git a/src/client/cmdline/Makefile.am b/src/client/cmdline/Makefile.am index 1fb6095..e4d5e90 100644 --- a/src/client/cmdline/Makefile.am +++ b/src/client/cmdline/Makefile.am @@ -1,6 +1,6 @@ # $Id$ # automake source for development tests by command-line libcaptive client -# Copyright (C) 2002 Jan Kratochvil +# Copyright (C) 2002-2003 Jan Kratochvil # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -18,7 +18,36 @@ include $(top_srcdir)/Makefile-head.am -captivecmdline_SOURCES= \ - main.c -captivecmdline_LDADD=$(captive_library) $(INTLLIBS) -sbin_PROGRAMS+=captivecmdline +captive_cmdline_SOURCES= \ + cmd_cd.c \ + cmd_cd.h \ + cmd_get.c \ + cmd_get.h \ + cmd_help.c \ + cmd_help.h \ + cmd_lcd.c \ + cmd_lcd.h \ + cmd_ls.c \ + cmd_ls.h \ + cmd_mkdir.c \ + cmd_mkdir.h \ + cmd_mv.c \ + cmd_mv.h \ + cmd_put.c \ + cmd_put.h \ + cmd_quit.c \ + cmd_quit.h \ + cmd_rm.c \ + cmd_rm.h \ + cmd_rmdir.c \ + cmd_rmdir.h \ + cmd_shell.c \ + cmd_shell.h \ + file_info.c \ + file_info.h \ + main.c \ + main.h +captive_cmdline_CFLAGS=$(GNOME_VFS_MODULE_CFLAGS) +captive_cmdline_LDFLAGS=$(READLINE_LDFLAGS) +captive_cmdline_LDADD=$(captive_library) $(READLINE_LIBS) $(GNOME_VFS_MODULE_LIBS) $(INTLLIBS) +bin_PROGRAMS+=captive-cmdline diff --git a/src/client/cmdline/cmd_cd.c b/src/client/cmdline/cmd_cd.c new file mode 100644 index 0000000..fbeb1b2 --- /dev/null +++ b/src/client/cmdline/cmd_cd.c @@ -0,0 +1,149 @@ +/* $Id$ + * client cmdline interface command "cd" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#include +#include +#include +#include +#include +#include "captive/client-directory.h" +#include + +#include "cmd_cd.h" /* self */ +#include "main.h" + + +/* Config: */ +#define STATICS_NUM (5) + + +const gchar *cmdline_cwd; + + +GQuark cmdline_cmd_cd_error_quark(void) +{ +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-cmd-cd"); + + return r; +} + + +const struct poptOption cmd_cd_table[]={ + POPT_AUTOHELP + POPT_TABLEEND + }; + + +/* Returns: statically allocated absolute pathname string */ +G_CONST_RETURN gchar *cmdline_path_from_cwd(const gchar *relative) +{ +static gchar *statics[STATICS_NUM]; +static int staticsi=0; +gchar *r,*r_end,*s,*d; + + /* 'relative' may be NULL for the '.' meaning */ + + if (!cmdline_cwd || (relative && *relative==G_DIR_SEPARATOR)) { /* bootstrap or absolute */ + g_assert(g_path_is_absolute(relative)); + r=g_strdup(relative); + } + else if (!relative) + r=g_strdup(cmdline_cwd); + else + r=g_build_filename(cmdline_cwd,relative,NULL); + g_assert(g_path_is_absolute(r)); + + /* coalesce '/'es */ + for (d=s=r;*s;s++) { + if (*s==G_DIR_SEPARATOR && d>r && d[-1]==G_DIR_SEPARATOR) + continue; + *d++=*s; + } + g_assert(d>r); + if (d>(r+1) && d[-1]==G_DIR_SEPARATOR) + d--; + *d++=G_DIR_SEPARATOR; + r_end=d; + + /* 'r' is NOT NULL-terminated here! */ + + for (d=s=r+1;sr+1) { + do { + d--; + } while (d[-1]!=G_DIR_SEPARATOR); + } + continue; + } + *d++=*s++; + } + g_assert(d[-1]==G_DIR_SEPARATOR); /* trailing '/' */ + if (d>r+1) /* leave at least "/" */ + d--; + *d='\0'; + + g_assert(g_path_is_absolute(r)); + + g_free(statics[staticsi]); + statics[staticsi++]=r; + staticsi%=G_N_ELEMENTS(statics); + + return r; +} + + +void cmd_cd(const char **cmd_argv,GError **errp) +{ +CaptiveDirectoryObject *captive_directory_object; + + g_return_if_fail(!errp || !*errp); + + if (cmd_argv[0]) { +const gchar *targetdir=cmdline_path_from_cwd(cmd_argv[0]); + + if (!errvfsresult_to_gerr(errp,captive_directory_new_open( + &captive_directory_object, /* captive_directory_object_return */ + targetdir))) { /* pathname */ + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_CD_ERROR,CMDLINE_CMD_CD_ERROR_CANNOT_OPEN_DIRECTORY, + _("Cannot open directory: %s"),targetdir); + return; + } + + g_object_unref(captive_directory_object); + + g_free((/*de-const*/ gchar *)cmdline_cwd); + cmdline_cwd=g_strdup(targetdir); + g_assert(g_path_is_absolute(cmdline_cwd)); + } + + g_printf("Guest-OS CWD: %s\n",cmdline_cwd); +} diff --git a/src/client/cmdline/cmd_cd.h b/src/client/cmdline/cmd_cd.h new file mode 100644 index 0000000..ea7ac1d --- /dev/null +++ b/src/client/cmdline/cmd_cd.h @@ -0,0 +1,45 @@ +/* $Id$ + * Include file for client cmdline interface command "cd" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_CMD_CD_H +#define _CAPTIVE_CLIENT_CMDLINE_CMD_CD_H 1 + + +#include +#include + + +#define CMDLINE_CMD_CD_ERROR (cmdline_cmd_cd_error_quark()) +GQuark cmdline_cmd_cd_error_quark(void); + +typedef enum { + CMDLINE_CMD_CD_ERROR_CANNOT_OPEN_DIRECTORY, + } CmdlineCmdCdError; + + +extern const struct poptOption cmd_cd_table[]; + +void cmd_cd(const char **cmd_argv,GError **errp); + +G_CONST_RETURN gchar *cmdline_path_from_cwd(const gchar *relative); + +extern const gchar *cmdline_cwd; + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_CMD_CD_H */ diff --git a/src/client/cmdline/cmd_get.c b/src/client/cmdline/cmd_get.c new file mode 100644 index 0000000..ea213d7 --- /dev/null +++ b/src/client/cmdline/cmd_get.c @@ -0,0 +1,142 @@ +/* $Id$ + * client cmdline interface command "get" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#include +#include +#include +#include "captive/client-file.h" +#include +#include +#include +#include "captive/macros.h" + +#include "cmd_get.h" /* self */ +#include "cmd_cd.h" /* for cmdline_path_from_cwd() */ +#include "main.h" + + +/* Config: */ +#define TRANSFER_BUFFER_SIZE (0x10000) + + +GQuark cmdline_cmd_get_error_quark(void) +{ +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-cmd-get"); + + return r; +} + + +const struct poptOption cmd_get_table[]={ + POPT_AUTOHELP + POPT_TABLEEND + }; + + +void cmd_get(const char **cmd_argv,GError **errp) +{ +CaptiveFileObject *captive_file_object; +const gchar *sourcefile,*targetfile; +guint perm=0644; +int fdtgt; +guint8 transfer_buffer[TRANSFER_BUFFER_SIZE]; + + g_return_if_fail(!errp || !*errp); + + sourcefile=cmd_argv[0]; + if (cmd_argv[1]) + targetfile=captive_strdup_alloca(cmd_argv[1]); + else { +char *s; + + targetfile=captive_strdup_alloca(sourcefile); + if ((s=strrchr(targetfile,G_DIR_SEPARATOR))) + targetfile=s+1; + } + + if (!(fdtgt=open(targetfile, + O_CREAT|O_WRONLY /* flags */ +#ifdef O_BINARY + | O_BINARY +#endif /* O_BINARY */ +#ifdef O_LARGEFILE + | O_LARGEFILE +#endif /* O_LARGEFILE */ + , + perm))) { /* mode */ + g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_CANNOT_CREATE_TARGET_HOSTOS_FILE, + _("Cannot create target host-os file: %s"),targetfile); + return; + } + + if (!errvfsresult_to_gerr(errp,captive_file_new_open( + &captive_file_object, /* captive_file_object_return */ + sourcefile, /* pathname */ + GNOME_VFS_OPEN_READ))) { /* mode */ + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_OPENING_SOURCE_FILE, + _("Error opening source host-os file '%s': %s"),sourcefile,g_strerror(errno)); + goto err_close_fdtgt; + } + + for (;;) { +GnomeVFSFileSize bytes_read; +GnomeVFSResult errvfsresult; +ssize_t gotssize; + + errvfsresult=captive_file_read( + captive_file_object, /* captive_file_object */ + transfer_buffer, /* buffer */ + sizeof(transfer_buffer), /* num_bytes */ + &bytes_read); /* bytes_read_return */ + g_assert((errvfsresult==GNOME_VFS_ERROR_EOF)==(bytes_read==0)); + if (errvfsresult==GNOME_VFS_ERROR_EOF) + break; + if (errvfsresult!=GNOME_VFS_OK) { +gboolean errbool; + + errbool=errvfsresult_to_gerr(errp,errvfsresult); + g_assert(errbool==FALSE); + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_READING_SOURCE_FILE, + _("Error reading source guest-os file '%s': %s"),sourcefile,g_strerror(errno)); + goto err_unref_captive_file_object; + } + + if (bytes_read!=(GnomeVFSFileSize)(gotssize=write(fdtgt,transfer_buffer,bytes_read))) { + g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_WRITING_TARGET_HOSTOS_FILE, + _("Error writing target host-os file: %s"),targetfile); + goto err_unref_captive_file_object; + } + } + +err_unref_captive_file_object: + g_object_unref(captive_file_object); +err_close_fdtgt: + if (close(fdtgt)) { + err_cleanup(errp); /* may be clean */ + g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_CLOSING_TARGET_HOSTOS_FILE, + _("Error closing target host-os file '%s': %s"),targetfile,g_strerror(errno)); + } +} diff --git a/src/client/cmdline/cmd_get.h b/src/client/cmdline/cmd_get.h new file mode 100644 index 0000000..217936a --- /dev/null +++ b/src/client/cmdline/cmd_get.h @@ -0,0 +1,45 @@ +/* $Id$ + * Include file for client cmdline interface command "get" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_CMD_GET_H +#define _CAPTIVE_CLIENT_CMDLINE_CMD_GET_H 1 + + +#include +#include + + +#define CMDLINE_CMD_GET_ERROR (cmdline_cmd_get_error_quark()) +GQuark cmdline_cmd_get_error_quark(void); + +typedef enum { + CMDLINE_CMD_GET_ERROR_OPENING_SOURCE_FILE, + CMDLINE_CMD_GET_ERROR_CANNOT_CREATE_TARGET_HOSTOS_FILE, + CMDLINE_CMD_GET_ERROR_WRITING_TARGET_HOSTOS_FILE, + CMDLINE_CMD_GET_ERROR_READING_SOURCE_FILE, + CMDLINE_CMD_GET_ERROR_CLOSING_TARGET_HOSTOS_FILE, + } CmdlineCmdGetError; + + +extern const struct poptOption cmd_get_table[]; + +void cmd_get(const char **cmd_argv,GError **errp); + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_CMD_GET_H */ diff --git a/src/client/cmdline/cmd_help.c b/src/client/cmdline/cmd_help.c new file mode 100644 index 0000000..49a0149 --- /dev/null +++ b/src/client/cmdline/cmd_help.c @@ -0,0 +1,62 @@ +/* $Id$ + * client cmdline interface command "help" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#include +#include +#include +#include +#include +#include + +#include "cmd_help.h" /* self */ +#include "main.h" /* for cmdline_command_table */ + + +GQuark cmdline_cmd_help_error_quark(void) +{ +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-cmd-help"); + + return r; +} + + +const struct poptOption cmd_help_table[]={ + POPT_AUTOHELP + POPT_TABLEEND + }; + + +void cmd_help(const char **cmd_argv,GError **errp) +{ +const struct cmdline_command *commandp; +int cmd_name_maxlen; + + g_return_if_fail(!errp || !*errp); + + cmd_name_maxlen=0; + for (commandp=cmdline_command_table+1;commandp->name;commandp++) + cmd_name_maxlen=MAX(cmd_name_maxlen,(int)strlen(commandp->name)); + for (commandp=cmdline_command_table+1;commandp->name;commandp++) + g_printf("%-*s\t%s\n",cmd_name_maxlen,commandp->name,commandp->description); +} diff --git a/src/client/cmdline/cmd_help.h b/src/client/cmdline/cmd_help.h new file mode 100644 index 0000000..c9f73a2 --- /dev/null +++ b/src/client/cmdline/cmd_help.h @@ -0,0 +1,41 @@ +/* $Id$ + * Include file for client cmdline interface command "help" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_CMD_HELP_H +#define _CAPTIVE_CLIENT_CMDLINE_CMD_HELP_H 1 + + +#include +#include + + +#define CMDLINE_CMD_HELP_ERROR (cmdline_cmd_help_error_quark()) +GQuark cmdline_cmd_help_error_quark(void); + +typedef enum { + CMDLINE_CMD_HELP_ERROR_UNUSED, + } CmdlineCmdHelpError; + + +extern const struct poptOption cmd_help_table[]; + +void cmd_help(const char **cmd_argv,GError **errp); + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_CMD_HELP_H */ diff --git a/src/client/cmdline/cmd_lcd.c b/src/client/cmdline/cmd_lcd.c new file mode 100644 index 0000000..3327495 --- /dev/null +++ b/src/client/cmdline/cmd_lcd.c @@ -0,0 +1,72 @@ +/* $Id$ + * client cmdline interface command "lcd" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cmd_lcd.h" /* self */ + + +const gchar *cmdline_cwd; + + +GQuark cmdline_cmd_lcd_error_quark(void) +{ +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-cmd-lcd"); + + return r; +} + + +const struct poptOption cmd_lcd_table[]={ + POPT_AUTOHELP + POPT_TABLEEND + }; + + +void cmd_lcd(const char **cmd_argv,GError **errp) +{ +gchar *currentdir; + + g_return_if_fail(!errp || !*errp); + + if (cmd_argv[0]) { + if (chdir(cmd_argv[0])) { + g_set_error(errp,CMDLINE_CMD_LCD_ERROR,CMDLINE_CMD_LCD_ERROR_CANNOT_CHANGE_HOSTOS_DIRECTORY, + _("Cannot change Host-OS directory to '%s': %s"),cmd_argv[0],g_strerror(errno)); + return; + } + } + + currentdir=g_get_current_dir(); + g_printf("Host-OS CWD: %s\n",currentdir); + g_free(currentdir); +} diff --git a/src/client/cmdline/cmd_lcd.h b/src/client/cmdline/cmd_lcd.h new file mode 100644 index 0000000..db4dcf9 --- /dev/null +++ b/src/client/cmdline/cmd_lcd.h @@ -0,0 +1,41 @@ +/* $Id$ + * Include file for client cmdline interface command "lcd" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_CMD_LCD_H +#define _CAPTIVE_CLIENT_CMDLINE_CMD_LCD_H 1 + + +#include +#include + + +#define CMDLINE_CMD_LCD_ERROR (cmdline_cmd_lcd_error_quark()) +GQuark cmdline_cmd_lcd_error_quark(void); + +typedef enum { + CMDLINE_CMD_LCD_ERROR_CANNOT_CHANGE_HOSTOS_DIRECTORY, + } CmdlineCmdLcdError; + + +extern const struct poptOption cmd_lcd_table[]; + +void cmd_lcd(const char **cmd_argv,GError **errp); + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_CMD_LCD_H */ diff --git a/src/client/cmdline/cmd_ls.c b/src/client/cmdline/cmd_ls.c new file mode 100644 index 0000000..38cde4c --- /dev/null +++ b/src/client/cmdline/cmd_ls.c @@ -0,0 +1,92 @@ +/* $Id$ + * client cmdline interface command "ls" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#include +#include +#include +#include +#include +#include "captive/client-directory.h" + +#include "cmd_ls.h" /* self */ +#include "cmd_cd.h" /* for cmdline_path_from_cwd() */ +#include "main.h" +#include "file_info.h" + + +GQuark cmdline_cmd_ls_error_quark(void) +{ +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-cmd-ls"); + + return r; +} + + +const struct poptOption cmd_ls_table[]={ + POPT_AUTOHELP + POPT_TABLEEND + }; + + +void cmd_ls(const char **cmd_argv,GError **errp) +{ +CaptiveDirectoryObject *captive_directory_object; +GnomeVFSFileInfo file_info; +GnomeVFSResult errvfsresult; +const gchar *targetdir; + + g_return_if_fail(!errp || !*errp); + + targetdir=cmdline_path_from_cwd(cmd_argv[0]); + + if (!errvfsresult_to_gerr(errp,captive_directory_new_open( + &captive_directory_object, /* captive_directory_object_return */ + targetdir))) { /* pathname */ + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_LS_ERROR,CMDLINE_CMD_LS_ERROR_CANNOT_OPEN_DIRECTORY, + _("Cannot open directory: %s"),targetdir); + return; + } + + while (GNOME_VFS_OK==(errvfsresult=captive_directory_read( + captive_directory_object, /* captive_directory_object */ + &file_info))) { /* file_info */ + file_info_dump_line(&file_info,errp); + if (*errp) + goto err_unref; + } + if (GNOME_VFS_ERROR_EOF!=errvfsresult) { +gboolean errbool; + + errbool=errvfsresult_to_gerr(errp,errvfsresult); + g_assert(errbool==FALSE); + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_LS_ERROR,CMDLINE_CMD_LS_ERROR_READING_DIRECTORY, + _("Error reading directory: %s"),targetdir); + goto err_unref; + } + +err_unref: + g_object_unref(captive_directory_object); +} diff --git a/src/client/cmdline/cmd_ls.h b/src/client/cmdline/cmd_ls.h new file mode 100644 index 0000000..5cbd69c --- /dev/null +++ b/src/client/cmdline/cmd_ls.h @@ -0,0 +1,42 @@ +/* $Id$ + * Include file for client cmdline interface command "ls" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_CMD_LS_H +#define _CAPTIVE_CLIENT_CMDLINE_CMD_LS_H 1 + + +#include +#include + + +#define CMDLINE_CMD_LS_ERROR (cmdline_cmd_ls_error_quark()) +GQuark cmdline_cmd_ls_error_quark(void); + +typedef enum { + CMDLINE_CMD_LS_ERROR_CANNOT_OPEN_DIRECTORY, + CMDLINE_CMD_LS_ERROR_READING_DIRECTORY, + } CmdlineCmdLsError; + + +extern const struct poptOption cmd_ls_table[]; + +void cmd_ls(const char **cmd_argv,GError **errp); + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_CMD_LS_H */ diff --git a/src/client/cmdline/cmd_mkdir.c b/src/client/cmdline/cmd_mkdir.c new file mode 100644 index 0000000..a6ff1fd --- /dev/null +++ b/src/client/cmdline/cmd_mkdir.c @@ -0,0 +1,70 @@ +/* $Id$ + * client cmdline interface command "mkdir" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#include +#include +#include +#include "captive/client-directory.h" + +#include "cmd_mkdir.h" /* self */ +#include "cmd_cd.h" /* for cmdline_path_from_cwd() */ +#include "main.h" + + +GQuark cmdline_cmd_mkdir_error_quark(void) +{ +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-cmd-mkdir"); + + return r; +} + + +const struct poptOption cmd_mkdir_table[]={ + POPT_AUTOHELP + POPT_TABLEEND + }; + + +void cmd_mkdir(const char **cmd_argv,GError **errp) +{ +CaptiveDirectoryObject *captive_directory_object; +guint perms=0755; +const gchar *targetdir; + + g_return_if_fail(!errp || !*errp); + + targetdir=cmdline_path_from_cwd(cmd_argv[0]); + + if (!errvfsresult_to_gerr(errp,captive_directory_new_make( + &captive_directory_object, /* captive_directory_object_return */ + targetdir, /* pathname */ + perms))) { /* perms */ + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_MKDIR_ERROR,CMDLINE_CMD_MKDIR_ERROR_CANNOT_CREATE_DIRECTORY, + _("Cannot create directory: %s"),targetdir); + return; + } + + g_object_unref(captive_directory_object); +} diff --git a/src/client/cmdline/cmd_mkdir.h b/src/client/cmdline/cmd_mkdir.h new file mode 100644 index 0000000..62c32ec --- /dev/null +++ b/src/client/cmdline/cmd_mkdir.h @@ -0,0 +1,41 @@ +/* $Id$ + * Include file for client cmdline interface command "mkdir" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_CMD_MKDIR_H +#define _CAPTIVE_CLIENT_CMDLINE_CMD_MKDIR_H 1 + + +#include +#include + + +#define CMDLINE_CMD_MKDIR_ERROR (cmdline_cmd_mkdir_error_quark()) +GQuark cmdline_cmd_mkdir_error_quark(void); + +typedef enum { + CMDLINE_CMD_MKDIR_ERROR_CANNOT_CREATE_DIRECTORY, + } CmdlineCmdMkdirError; + + +extern const struct poptOption cmd_mkdir_table[]; + +void cmd_mkdir(const char **cmd_argv,GError **errp); + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_CMD_MKDIR_H */ diff --git a/src/client/cmdline/cmd_mv.c b/src/client/cmdline/cmd_mv.c new file mode 100644 index 0000000..942f859 --- /dev/null +++ b/src/client/cmdline/cmd_mv.c @@ -0,0 +1,81 @@ +/* $Id$ + * client cmdline interface command "mv" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the temvs of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#include +#include +#include +#include "captive/client-file.h" + +#include "cmd_mv.h" /* self */ +#include "cmd_cd.h" /* for cmdline_path_from_cwd() */ +#include "main.h" + + +GQuark cmdline_cmd_mv_error_quark(void) +{ +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-cmd-mv"); + + return r; +} + + +const struct poptOption cmd_mv_table[]={ + POPT_AUTOHELP + POPT_TABLEEND + }; + + +void cmd_mv(const char **cmd_argv,GError **errp) +{ +CaptiveFileObject *captive_file_object; +const gchar *sourcefile,*targetfile; + + g_return_if_fail(!errp || !*errp); + + sourcefile=cmdline_path_from_cwd(cmd_argv[0]); + targetfile=cmdline_path_from_cwd(cmd_argv[1]); + + if (!errvfsresult_to_gerr(errp,captive_file_new_open( + &captive_file_object, /* captive_file_object_return */ + sourcefile, /* pathname */ + GNOME_VFS_OPEN_NONE))) { /* mode */ + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_MV_ERROR,CMDLINE_CMD_MV_ERROR_CANNOT_OPEN_FILE_TO_MOVE, + _("Cannot open file to be moved: %s"),targetfile); + return; + } + + if (!errvfsresult_to_gerr(errp,captive_file_move( + captive_file_object, /* captive_file_object_old */ + targetfile, /* pathname_new */ + FALSE))) { /* force_replace */ + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_MV_ERROR,CMDLINE_CMD_MV_ERROR_CANNOT_MOVE_FILE, + _("Cannot move file '%s' to its target name '%s'"),sourcefile,targetfile); + goto err_unref; + } + +err_unref: + g_object_unref(captive_file_object); +} diff --git a/src/client/cmdline/cmd_mv.h b/src/client/cmdline/cmd_mv.h new file mode 100644 index 0000000..198c1c0 --- /dev/null +++ b/src/client/cmdline/cmd_mv.h @@ -0,0 +1,42 @@ +/* $Id$ + * Include file for client cmdline interface command "mv" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the temvs of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_CMD_MV_H +#define _CAPTIVE_CLIENT_CMDLINE_CMD_MV_H 1 + + +#include +#include + + +#define CMDLINE_CMD_MV_ERROR (cmdline_cmd_mv_error_quark()) +GQuark cmdline_cmd_mv_error_quark(void); + +typedef enum { + CMDLINE_CMD_MV_ERROR_CANNOT_OPEN_FILE_TO_MOVE, + CMDLINE_CMD_MV_ERROR_CANNOT_MOVE_FILE, + } CmdlineCmdMvError; + + +extern const struct poptOption cmd_mv_table[]; + +void cmd_mv(const char **cmd_argv,GError **errp); + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_CMD_MV_H */ diff --git a/src/client/cmdline/cmd_put.c b/src/client/cmdline/cmd_put.c new file mode 100644 index 0000000..bebd5b5 --- /dev/null +++ b/src/client/cmdline/cmd_put.c @@ -0,0 +1,138 @@ +/* $Id$ + * client cmdline interface command "put" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#include +#include +#include +#include "captive/client-file.h" +#include +#include +#include + +#include "cmd_put.h" /* self */ +#include "cmd_cd.h" /* for cmdline_path_from_cwd() */ +#include "main.h" + + +/* Config: */ +#define TRANSFER_BUFFER_SIZE (0x10000) + + +GQuark cmdline_cmd_put_error_quark(void) +{ +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-cmd-put"); + + return r; +} + + +const struct poptOption cmd_put_table[]={ + POPT_AUTOHELP + POPT_TABLEEND + }; + + +void cmd_put(const char **cmd_argv,GError **errp) +{ +CaptiveFileObject *captive_file_object; +const gchar *sourcefile,*targetfile; +guint perm=0644; +int fdsrc; +guint8 transfer_buffer[TRANSFER_BUFFER_SIZE]; +int got; + + g_return_if_fail(!errp || !*errp); + + sourcefile=cmd_argv[0]; + if (cmd_argv[1]) + targetfile=cmdline_path_from_cwd(cmd_argv[1]); + else { +gchar *sourcefile_basename; + + sourcefile_basename=g_path_get_basename(sourcefile); + targetfile=cmdline_path_from_cwd(sourcefile_basename); + g_free(sourcefile_basename); + } + + if (!(fdsrc=open(sourcefile,O_RDONLY +#ifdef O_BINARY + | O_BINARY +#endif /* O_BINARY */ +#ifdef O_LARGEFILE + | O_LARGEFILE +#endif /* O_LARGEFILE */ + ))) { + g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_OPENING_SOURCE_FILE, + _("Error opening source host-os file '%s': %s"),sourcefile,g_strerror(errno)); + return; + } + + if (!errvfsresult_to_gerr(errp,captive_file_new_create( + &captive_file_object, /* captive_file_object_return */ + targetfile, /* pathname */ + GNOME_VFS_OPEN_WRITE, /* mode */ + FALSE, /* exclusive */ + perm))) { /* perm */ + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_CANNOT_CREATE_TARGET_GUESTOS_FILE, + _("Cannot create target guest-os file: %s"),targetfile); + goto err_close_fdsrc; + } + + while (0<(got=read(fdsrc,transfer_buffer,sizeof(transfer_buffer)))) { +GnomeVFSFileSize bytes_written; + + if (!errvfsresult_to_gerr(errp,captive_file_write( + captive_file_object, /* captive_file_object */ + transfer_buffer, /* buffer */ + got, /* num_bytes */ + &bytes_written))) { /* bytes_written_return */ + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_WRITING_TARGET_GUESTOS_FILE, + _("Error writing target guest-os file: %s"),targetfile); + goto err_unref_captive_file_object; + } + if (bytes_written!=(GnomeVFSFileSize)got) { + g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_WRITING_TARGET_GUESTOS_FILE, + _("Error writing target guest-os file '%s': requested %d, written %Lu"), + targetfile,got,(unsigned long long)bytes_written); + goto err_unref_captive_file_object; + } + } + if (got==-1) { + g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_READING_SOURCE_FILE, + _("Error reading source host-os file '%s': %s"),sourcefile,g_strerror(errno)); + goto err_unref_captive_file_object; + } + g_assert(got==0); + +err_unref_captive_file_object: + g_object_unref(captive_file_object); +err_close_fdsrc: + if (close(fdsrc)) { + err_cleanup(errp); /* may be clean */ + g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_CLOSING_SOURCE_FILE, + _("Error closing source host-os file '%s': %s"),sourcefile,g_strerror(errno)); + } +} diff --git a/src/client/cmdline/cmd_put.h b/src/client/cmdline/cmd_put.h new file mode 100644 index 0000000..9f0ffb9 --- /dev/null +++ b/src/client/cmdline/cmd_put.h @@ -0,0 +1,45 @@ +/* $Id$ + * Include file for client cmdline interface command "put" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_CMD_PUT_H +#define _CAPTIVE_CLIENT_CMDLINE_CMD_PUT_H 1 + + +#include +#include + + +#define CMDLINE_CMD_PUT_ERROR (cmdline_cmd_put_error_quark()) +GQuark cmdline_cmd_put_error_quark(void); + +typedef enum { + CMDLINE_CMD_PUT_ERROR_OPENING_SOURCE_FILE, + CMDLINE_CMD_PUT_ERROR_CANNOT_CREATE_TARGET_GUESTOS_FILE, + CMDLINE_CMD_PUT_ERROR_WRITING_TARGET_GUESTOS_FILE, + CMDLINE_CMD_PUT_ERROR_READING_SOURCE_FILE, + CMDLINE_CMD_PUT_ERROR_CLOSING_SOURCE_FILE, + } CmdlineCmdPutError; + + +extern const struct poptOption cmd_put_table[]; + +void cmd_put(const char **cmd_argv,GError **errp); + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_CMD_PUT_H */ diff --git a/src/client/cmdline/cmd_quit.c b/src/client/cmdline/cmd_quit.c new file mode 100644 index 0000000..6cef446 --- /dev/null +++ b/src/client/cmdline/cmd_quit.c @@ -0,0 +1,52 @@ +/* $Id$ + * client cmdline interface command "quit" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#include +#include +#include +#include + +#include "cmd_quit.h" /* self */ + + +GQuark cmdline_cmd_quit_error_quark(void) +{ +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-cmd-quit"); + + return r; +} + + +const struct poptOption cmd_quit_table[]={ + POPT_AUTOHELP + POPT_TABLEEND + }; + + +void cmd_quit(const char **cmd_argv,GError **errp) +{ + g_return_if_fail(!errp || !*errp); + + exit(EXIT_SUCCESS); +} diff --git a/src/client/cmdline/cmd_quit.h b/src/client/cmdline/cmd_quit.h new file mode 100644 index 0000000..a15d03f --- /dev/null +++ b/src/client/cmdline/cmd_quit.h @@ -0,0 +1,41 @@ +/* $Id$ + * Include file for client cmdline interface command "quit" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_CMD_QUIT_H +#define _CAPTIVE_CLIENT_CMDLINE_CMD_QUIT_H 1 + + +#include +#include + + +#define CMDLINE_CMD_QUIT_ERROR (cmdline_cmd_quit_error_quark()) +GQuark cmdline_cmd_quit_error_quark(void); + +typedef enum { + dfgdfgdfgfg, + } CmdlineCmdQuitError; + + +extern const struct poptOption cmd_quit_table[]; + +void cmd_quit(const char **cmd_argv,GError **errp); + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_CMD_QUIT_H */ diff --git a/src/client/cmdline/cmd_rm.c b/src/client/cmdline/cmd_rm.c new file mode 100644 index 0000000..0c3de67 --- /dev/null +++ b/src/client/cmdline/cmd_rm.c @@ -0,0 +1,78 @@ +/* $Id$ + * client cmdline interface command "rm" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#include +#include +#include +#include "captive/client-file.h" + +#include "cmd_rm.h" /* self */ +#include "cmd_cd.h" /* for cmdline_path_from_cwd() */ +#include "main.h" + + +GQuark cmdline_cmd_rm_error_quark(void) +{ +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-cmd-rm"); + + return r; +} + + +const struct poptOption cmd_rm_table[]={ + POPT_AUTOHELP + POPT_TABLEEND + }; + + +void cmd_rm(const char **cmd_argv,GError **errp) +{ +CaptiveFileObject *captive_file_object; +const gchar *targetfile; + + g_return_if_fail(!errp || !*errp); + + targetfile=cmdline_path_from_cwd(cmd_argv[0]); + + if (!errvfsresult_to_gerr(errp,captive_file_new_open( + &captive_file_object, /* captive_file_object_return */ + targetfile, /* pathname */ + GNOME_VFS_OPEN_NONE))) { /* mode */ + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_RM_ERROR,CMDLINE_CMD_RM_ERROR_CANNOT_CREATE_REMOVAL_FILE, + _("Cannot open file for removal: %s"),targetfile); + return; + } + + if (!errvfsresult_to_gerr(errp,captive_file_remove( + captive_file_object))) { /* captive_file_object */ + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_RM_ERROR,CMDLINE_CMD_RM_ERROR_CANNOT_SET_FILE_REMOVAL, + _("Cannot set file removal state: %s"),targetfile); + goto err_unref; + } + +err_unref: + g_object_unref(captive_file_object); +} diff --git a/src/client/cmdline/cmd_rm.h b/src/client/cmdline/cmd_rm.h new file mode 100644 index 0000000..0dc558e --- /dev/null +++ b/src/client/cmdline/cmd_rm.h @@ -0,0 +1,42 @@ +/* $Id$ + * Include file for client cmdline interface command "rm" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_CMD_RM_H +#define _CAPTIVE_CLIENT_CMDLINE_CMD_RM_H 1 + + +#include +#include + + +#define CMDLINE_CMD_RM_ERROR (cmdline_cmd_rm_error_quark()) +GQuark cmdline_cmd_rm_error_quark(void); + +typedef enum { + CMDLINE_CMD_RM_ERROR_CANNOT_CREATE_REMOVAL_FILE, + CMDLINE_CMD_RM_ERROR_CANNOT_SET_FILE_REMOVAL, + } CmdlineCmdRmError; + + +extern const struct poptOption cmd_rm_table[]; + +void cmd_rm(const char **cmd_argv,GError **errp); + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_CMD_RM_H */ diff --git a/src/client/cmdline/cmd_rmdir.c b/src/client/cmdline/cmd_rmdir.c new file mode 100644 index 0000000..06d4339 --- /dev/null +++ b/src/client/cmdline/cmd_rmdir.c @@ -0,0 +1,77 @@ +/* $Id$ + * client cmdline interface command "rmdir" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#include +#include +#include +#include "captive/client-directory.h" + +#include "cmd_rmdir.h" /* self */ +#include "cmd_cd.h" /* for cmdline_path_from_cwd() */ +#include "main.h" + + +GQuark cmdline_cmd_rmdir_error_quark(void) +{ +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-cmd-rmdir"); + + return r; +} + + +const struct poptOption cmd_rmdir_table[]={ + POPT_AUTOHELP + POPT_TABLEEND + }; + + +void cmd_rmdir(const char **cmd_argv,GError **errp) +{ +CaptiveDirectoryObject *captive_directory_object; +const gchar *targetdir; + + g_return_if_fail(!errp || !*errp); + + targetdir=cmdline_path_from_cwd(cmd_argv[0]); + + if (!errvfsresult_to_gerr(errp,captive_directory_new_open( + &captive_directory_object, /* captive_directory_object_return */ + targetdir))) { /* pathname */ + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_RMDIR_ERROR,CMDLINE_CMD_RMDIR_ERROR_CANNOT_CREATE_REMOVAL_DIRECTORY, + _("Cannot open directory for removal: %s"),targetdir); + return; + } + + if (!errvfsresult_to_gerr(errp,captive_directory_remove( + captive_directory_object))) { /* captive_directory_object */ + err_cleanup(errp); + g_set_error(errp,CMDLINE_CMD_RMDIR_ERROR,CMDLINE_CMD_RMDIR_ERROR_CANNOT_SET_DIRECTORY_REMOVAL, + _("Cannot set directory removal state: %s"),targetdir); + goto err_unref; + } + +err_unref: + g_object_unref(captive_directory_object); +} diff --git a/src/client/cmdline/cmd_rmdir.h b/src/client/cmdline/cmd_rmdir.h new file mode 100644 index 0000000..5649c3a --- /dev/null +++ b/src/client/cmdline/cmd_rmdir.h @@ -0,0 +1,42 @@ +/* $Id$ + * Include file for client cmdline interface command "rmdir" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_CMD_RMDIR_H +#define _CAPTIVE_CLIENT_CMDLINE_CMD_RMDIR_H 1 + + +#include +#include + + +#define CMDLINE_CMD_RMDIR_ERROR (cmdline_cmd_rmdir_error_quark()) +GQuark cmdline_cmd_rmdir_error_quark(void); + +typedef enum { + CMDLINE_CMD_RMDIR_ERROR_CANNOT_CREATE_REMOVAL_DIRECTORY, + CMDLINE_CMD_RMDIR_ERROR_CANNOT_SET_DIRECTORY_REMOVAL, + } CmdlineCmdRmdirError; + + +extern const struct poptOption cmd_rmdir_table[]; + +void cmd_rmdir(const char **cmd_argv,GError **errp); + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_CMD_RMDIR_H */ diff --git a/src/client/cmdline/cmd_shell.c b/src/client/cmdline/cmd_shell.c new file mode 100644 index 0000000..b9293d4 --- /dev/null +++ b/src/client/cmdline/cmd_shell.c @@ -0,0 +1,121 @@ +/* $Id$ + * client cmdline interface command "shell" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_LIBREADLINE +#include +#ifdef HAVE_READLINE_HISTORY_H +#include +#endif /* HAVE_READLINE_HISTORY_H */ +#endif /* HAVE_LIBREADLINE */ + +#include "cmd_shell.h" /* self */ +#include "cmd_quit.h" /* for cmd_quit() */ +#include "main.h" /* for invoke_cmd() */ +#include "cmd_cd.h" + + +/* Config: */ +#define PROMPT_STRING "%s$ " /* %s == 'cmdline_cwd' */ + + +GQuark cmdline_cmd_shell_error_quark(void) +{ +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-cmd-shell"); + + return r; +} + + +const struct poptOption cmd_shell_table[]={ + POPT_AUTOHELP + POPT_TABLEEND + }; + + +/* Function mostly stolen from the project Partition Surprise. + */ +void cmd_shell(const char **cmd_argv,GError **errp) +{ +#ifdef HAVE_LIBREADLINE +char *line; +#else /* HAVE_LIBREADLINE */ +char line[1024],*s; +#endif /* HAVE_LIBREADLINE */ +int errint,line_argc; +const char **line_argv=NULL; +gchar *prompt; + + g_return_if_fail(!errp || !*errp); + +#ifndef HAVE_LIBREADLINE + fputs(_("Line editing not available, please recompile with readline library installed\n"),stdout); +#endif /* HAVE_LIBREADLINE */ + + for (;;) { + prompt=g_strdup_printf(PROMPT_STRING,cmdline_cwd); +#ifdef HAVE_LIBREADLINE + line=readline(prompt); +#ifdef HAVE_ADD_HISTORY + if (line && *line) + add_history(line); +#endif /* HAVE_ADD_HISTORY */ +#else /* HAVE_LIBREADLINE */ + fputs(prompt,stdout); fflush(stdout); + line=fgets(line,sizeof(line),stdin); +#endif /* HAVE_LIBREADLINE */ + g_free(prompt); + if (!line) { + cmd_quit( + NULL, /* cmd_argv */ + errp); /* errp */ + g_assert_not_reached(); + } +#ifndef HAVE_LIBREADLINE + if (line && (s=strchr(line,'\n'))) + *s='\0'; +#endif /* HAVE_LIBREADLINE */ + + line_argv=NULL; + errint=poptParseArgvString(line,&line_argc,&line_argv); +#ifndef HAVE_LIBREADLINE + free(line); +#endif /* HAVE_LIBREADLINE */ + if (errint!=0) { + free(line_argv); + g_set_error(errp,CMDLINE_CMD_SHELL_ERROR,CMDLINE_CMD_SHELL_ERROR_LINE_PARSE_ARGUMENTS, + _("Error parsing arguments of text line: %s"),line); + return; + } + + invoke_cmd(line_argc,line_argv); /* errors catched inside */ + free(line_argv); + } +} diff --git a/src/client/cmdline/cmd_shell.h b/src/client/cmdline/cmd_shell.h new file mode 100644 index 0000000..7894fec --- /dev/null +++ b/src/client/cmdline/cmd_shell.h @@ -0,0 +1,41 @@ +/* $Id$ + * Include file for client cmdline interface command "shell" for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_CMD_SHELL_H +#define _CAPTIVE_CLIENT_CMDLINE_CMD_SHELL_H 1 + + +#include +#include + + +#define CMDLINE_CMD_SHELL_ERROR (cmdline_cmd_shell_error_quark()) +GQuark cmdline_cmd_shell_error_quark(void); + +typedef enum { + CMDLINE_CMD_SHELL_ERROR_LINE_PARSE_ARGUMENTS, + } CmdlineCmdShellError; + + +extern const struct poptOption cmd_shell_table[]; + +void cmd_shell(const char **cmd_argv,GError **errp); + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_CMD_SHELL_H */ diff --git a/src/client/cmdline/file_info.c b/src/client/cmdline/file_info.c new file mode 100644 index 0000000..d659973 --- /dev/null +++ b/src/client/cmdline/file_info.c @@ -0,0 +1,73 @@ +/* $Id$ + * client cmdline interface GnomeVFSFileInfo utils for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#include +#include +#include +#include +#include + + +#include "file_info.h" /* self */ + + +GQuark cmdline_file_info_error_quark(void) +{ +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-file_info"); + + return r; +} + + +void file_info_dump_line(const GnomeVFSFileInfo *file_info,GError **errp) +{ +const gchar *file_type,*file_perms; +gchar *file_size; + + g_return_if_fail(!errp || !*errp); + + switch (!(file_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) ? GNOME_VFS_FILE_TYPE_UNKNOWN : file_info->type) { + case GNOME_VFS_FILE_TYPE_REGULAR: file_type="FILE"; break; + case GNOME_VFS_FILE_TYPE_DIRECTORY: file_type="DIR "; break; + case GNOME_VFS_FILE_TYPE_SOCKET: file_type="DEV "; break; + default: file_type="??? "; break; + } + + if (!(file_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS)) + file_perms="???"; + else if (file_info->permissions & GNOME_VFS_PERM_USER_WRITE) + file_perms="r/w"; + else + file_perms="r/o"; + + if (file_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE) + file_size=g_strdup_printf("%8" GNOME_VFS_SIZE_FORMAT_STR,file_info->size); + else + file_size=g_strdup_printf("%8s","???"); + + /* type pm sz nm */ + g_printf("[%s] %s %s %s\n",file_type,file_perms,file_size,file_info->name); + + g_free(file_size); +} diff --git a/src/client/cmdline/file_info.h b/src/client/cmdline/file_info.h new file mode 100644 index 0000000..215ace4 --- /dev/null +++ b/src/client/cmdline/file_info.h @@ -0,0 +1,39 @@ +/* $Id$ + * Include file for client cmdline interface GnomeVFSFileInfo utils for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_FILE_INFO_H +#define _CAPTIVE_CLIENT_CMDLINE_FILE_INFO_H 1 + + +#include +#include + + +#define CMDLINE_FILE_INFO_ERROR (cmdline_file_info_error_quark()) +GQuark cmdline_file_info_error_quark(void); + +typedef enum { + CMDLINE_FILE_INFO_ERROR_UNUSED, + } CmdlineFileInfoError; + + +void file_info_dump_line(const GnomeVFSFileInfo *file_info,GError **errp); + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_FILE_INFO_H */ diff --git a/src/client/cmdline/main.c b/src/client/cmdline/main.c index 3b7f81e..9976446 100644 --- a/src/client/cmdline/main.c +++ b/src/client/cmdline/main.c @@ -1,36 +1,245 @@ -/* FIXME: test source only! Don't pass into CVS! */ +/* $Id$ + * client cmdline interface for libcaptive + * Copyright (C) 2002-2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + #include "config.h" #include -#include #include #include +#include +#include +#include +#include #include /* for captive_init() */ +#include "main.h" /* self */ +#include "cmd_shell.h" +#include "cmd_cd.h" +#include "cmd_lcd.h" +#include "cmd_ls.h" +#include "cmd_get.h" +#include "cmd_put.h" +#include "cmd_rm.h" +#include "cmd_mv.h" +#include "cmd_mkdir.h" +#include "cmd_rmdir.h" +#include "cmd_quit.h" +#include "cmd_help.h" -void captive_sandbox_init(void); -int main(int argc,char **argv) +GQuark cmdline_main_error_quark(void) { - if (argc>3) { - fprintf(stderr,"Syntax: captivecmdline "); - exit(EXIT_FAILURE); +GQuark r=0; + + if (!r) + r=g_quark_from_static_string("cmdline-main"); + + return r; +} + + +static const struct poptOption popt_table[]={ + CAPTIVE_POPT_INCLUDE, + POPT_AUTOHELP + POPT_TABLEEND + }; + +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 }, + { "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 }, + { "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 }, + { NULL }, /* G_N_ELEMENTS() not usable as sizeof() is not visible for 'extern' */ + }; + + +static void invoke_cmd_err(int cmd_argc,const char **cmd_argv,GError **errp) +{ +const struct cmdline_command *commandp; +const char *cmd_name=NULL; +poptContext cmd_context; +int errint; +const char **cmdarg_argv; +int cmdarg_argc; +const char **csp; +const char *emptyargv_NULL=NULL; + + g_return_if_fail(cmd_argc>=0); + g_return_if_fail(!errp || !*errp); + + /* poptGetContext() cannot be passed argc==0 even if we lass POPT_CONTEXT_KEEP_FIRST + * as it is buggy. Workaround it by keeping the command name as argv[0]. + */ + if (!cmd_argc) { +const char *stub_shell[]={ cmdline_command_table[0].name,NULL }; + + cmd_argc=1; + cmd_argv=stub_shell; } - if (FALSE!=captive_init( - argv[1], /* captive_args */ - NULL)) /* image_iochannel */ - g_error(_("captive_init captive_args phase FAIL")); + 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" */ + || !strcasecmp(cmd_name,commandp->name)) + break; + } + if (!commandp->name) { + g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_UNKNOWN_COMMAND, + _("Unknown command, try 'help': %s"),cmd_name); + return; + } + cmd_context=poptGetContext( + PACKAGE, /* name */ + cmd_argc,cmd_argv, /* argc,argv */ + commandp->table, /* options */ + 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); + return; + } + errint=poptReadDefaultConfig(cmd_context, + TRUE); /* useEnv */ + if (errint!=0) { + g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_READING_COMMAND_CONFIG, + _("Error reading default configuration for command: %s"),cmd_name); + return; + } + 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; + } + if (!(cmdarg_argv=poptGetArgs(cmd_context))) + cmdarg_argv=&emptyargv_NULL; + + for (csp=cmdarg_argv,cmdarg_argc=0;*csp;csp++) + cmdarg_argc++; + if (cmdarg_argcargsn_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; + } + + (*commandp->func)(cmdarg_argv,errp); + + poptFreeContext(cmd_context); +} + + +void err_cleanup(GError **errp) +{ + g_return_if_fail(errp!=NULL); - if (TRUE!=captive_init(NULL, /* captive_args */ + if (!*errp) + return; + g_printf("\nERROR: %s\n",(*errp)->message); + g_clear_error(errp); +} + + +void invoke_cmd(int cmd_argc,const char **cmd_argv) +{ +GError *gerr=NULL; + + invoke_cmd_err(cmd_argc,cmd_argv,&gerr); + err_cleanup(&gerr); +} + + +/* Returns: Success (no error occured). */ +gboolean errvfsresult_to_gerr(GError **errp,GnomeVFSResult errvfsresult) +{ + g_return_val_if_fail(!errp || !*errp,FALSE); + + if (errvfsresult==GNOME_VFS_OK) + return TRUE; + + g_set_error(errp,CMDLINE_MAIN_ERROR,CMDLINE_MAIN_ERROR_GENERIC_ERROR, + _("Generic error: %s"),gnome_vfs_result_to_string(errvfsresult)); + return FALSE; +} + +int main(int argc,char **argv) +{ +poptContext context; +int errint; +const char **cmd_argv,**csp; +int cmd_argc; +GError *gerr=NULL; +const char *cmd_cd_root_args[]={"/",NULL}; + + context=poptGetContext( + PACKAGE, /* name */ + argc,(/*en-const*/const char **)argv, /* argc,argv */ + popt_table, /* options */ + POPT_CONTEXT_POSIXMEHARDER); /* flags; && !POPT_CONTEXT_KEEP_FIRST */ + if (context==NULL) { + g_assert_not_reached(); /* argument recognization args_error */ + return EXIT_FAILURE; + } + errint=poptReadDefaultConfig(context, + TRUE); /* useEnv */ + if (errint!=0) { + g_assert_not_reached(); /* argument recognization args_error */ + return EXIT_FAILURE; + } + errint=poptGetNextOpt(context); + if (errint!=-1) { + g_assert_not_reached(); /* some non-callbacked argument reached */ + return EXIT_FAILURE; + } + cmd_argv=poptGetArgs(context); + for (csp=cmd_argv,cmd_argc=0;csp && *csp;csp++) + cmd_argc++; + + if (TRUE!=captive_init(NULL, /* captive_args; already parsed above */ ( /* image_iochannel */ - !argv[2] ? NULL : g_io_channel_new_file( /* FIXME: g_io_channel_new_file() is NOT 64-bit compliant! */ - argv[2], /* filename */ - (captive_option_rwmode==CAPTIVE_OPTION_RWMODE_RW ? "w+" : "r"), /* mode */ + !cmd_argc ? NULL : g_io_channel_new_file( /* FIXME: g_io_channel_new_file() is NOT 64-bit compliant! */ + cmd_argv[cmd_argc-1], /* filename */ + (captive_option_rwmode==CAPTIVE_OPTION_RWMODE_RW ? "r+" : "r"), /* mode */ NULL)))) /* error */ g_error(_("captive_init image_iochannel FAIL")); + cmd_argc--; /* image file */ + + cmd_cd(cmd_cd_root_args,&gerr); + if (gerr) { + err_cleanup(&gerr); + return EXIT_FAILURE; + } + + invoke_cmd(cmd_argc,cmd_argv); + + /* 'cmd_argv' gets cleared by 'poptFreeContext(context);' below */ + poptFreeContext(context); return EXIT_SUCCESS; } diff --git a/src/client/cmdline/main.h b/src/client/cmdline/main.h new file mode 100644 index 0000000..224b6f4 --- /dev/null +++ b/src/client/cmdline/main.h @@ -0,0 +1,56 @@ +/* $Id$ + * Include file for client cmdline interface control functions for libcaptive + * Copyright (C) 2003 Jan Kratochvil + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; exactly version 2 of June 1991 is required + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _CAPTIVE_CLIENT_CMDLINE_MAIN_H +#define _CAPTIVE_CLIENT_CMDLINE_MAIN_H 1 + + +#include +#include + + +#define CMDLINE_MAIN_ERROR (cmdline_main_error_quark()) +GQuark cmdline_main_error_quark(void); + +typedef enum { + CMDLINE_MAIN_ERROR_UNKNOWN_COMMAND, + CMDLINE_MAIN_ERROR_INVALID_COMMAND_ARGUMENTS, + CMDLINE_MAIN_ERROR_READING_COMMAND_CONFIG, + CMDLINE_MAIN_ERROR_EXCEEDING_COMMAND_OPTION, + CMDLINE_MAIN_ERROR_INVALID_COMMAND_ARGUMENT_COUNT, + CMDLINE_MAIN_ERROR_GENERIC_ERROR, + } CmdlineMainError; + + +struct cmdline_command { + const gchar *name,*description; + const struct poptOption *table; + void (*func)(const char **cmd_argv,GError **errp); + gint argsn_min,argsn_max; + }; + +extern const struct cmdline_command cmdline_command_table[]; + + +void invoke_cmd(int cmd_argc,const char **cmd_argv); +void err_cleanup(GError **errp); +gboolean errvfsresult_to_gerr(GError **errp,GnomeVFSResult errvfsresult); + + +#endif /* _CAPTIVE_CLIENT_CMDLINE_MAIN_H */