# $Id$
# automake source for development tests by command-line libcaptive client
-# Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
+# Copyright (C) 2002-2003 Jan Kratochvil <project-captive@jankratochvil.net>
#
# 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
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
--- /dev/null
+/* $Id$
+ * client cmdline interface command "cd" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
+#include <glib/gerror.h>
+#include <popt.h>
+#include <stdlib.h>
+#include <string.h>
+#include "captive/client-directory.h"
+#include <glib/gprintf.h>
+
+#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;s<r_end;) {
+ if (!strncmp(s,"./",2)) {
+ s+=2;
+ continue;
+ }
+ if (!strncmp(s,"../",3)) {
+ s+=3;
+ g_assert(d[-1]==G_DIR_SEPARATOR);
+ if (d>r+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);
+}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface command "cd" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gerror.h>
+#include <popt.h>
+
+
+#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 */
--- /dev/null
+/* $Id$
+ * client cmdline interface command "get" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
+#include <glib/gerror.h>
+#include <popt.h>
+#include "captive/client-file.h"
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+#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));
+ }
+}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface command "get" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gerror.h>
+#include <popt.h>
+
+
+#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 */
--- /dev/null
+/* $Id$
+ * client cmdline interface command "help" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
+#include <glib/gerror.h>
+#include <popt.h>
+#include <stdlib.h>
+#include <string.h>
+#include <glib/gprintf.h>
+
+#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);
+}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface command "help" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gerror.h>
+#include <popt.h>
+
+
+#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 */
--- /dev/null
+/* $Id$
+ * client cmdline interface command "lcd" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
+#include <glib/gerror.h>
+#include <popt.h>
+#include <glib/gprintf.h>
+#include <unistd.h>
+#include <errno.h>
+#include <glib/gstrfuncs.h>
+#include <glib/gutils.h>
+#include <glib/gmem.h>
+
+#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);
+}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface command "lcd" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gerror.h>
+#include <popt.h>
+
+
+#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 */
--- /dev/null
+/* $Id$
+ * client cmdline interface command "ls" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
+#include <glib/gerror.h>
+#include <popt.h>
+#include <stdlib.h>
+#include <string.h>
+#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);
+}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface command "ls" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gerror.h>
+#include <popt.h>
+
+
+#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 */
--- /dev/null
+/* $Id$
+ * client cmdline interface command "mkdir" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
+#include <glib/gerror.h>
+#include <popt.h>
+#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);
+}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface command "mkdir" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gerror.h>
+#include <popt.h>
+
+
+#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 */
--- /dev/null
+/* $Id$
+ * client cmdline interface command "mv" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
+#include <glib/gerror.h>
+#include <popt.h>
+#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);
+}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface command "mv" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gerror.h>
+#include <popt.h>
+
+
+#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 */
--- /dev/null
+/* $Id$
+ * client cmdline interface command "put" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
+#include <glib/gerror.h>
+#include <popt.h>
+#include "captive/client-file.h"
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+
+#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));
+ }
+}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface command "put" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gerror.h>
+#include <popt.h>
+
+
+#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 */
--- /dev/null
+/* $Id$
+ * client cmdline interface command "quit" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
+#include <glib/gerror.h>
+#include <popt.h>
+#include <stdlib.h>
+
+#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);
+}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface command "quit" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gerror.h>
+#include <popt.h>
+
+
+#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 */
--- /dev/null
+/* $Id$
+ * client cmdline interface command "rm" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
+#include <glib/gerror.h>
+#include <popt.h>
+#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);
+}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface command "rm" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gerror.h>
+#include <popt.h>
+
+
+#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 */
--- /dev/null
+/* $Id$
+ * client cmdline interface command "rmdir" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
+#include <glib/gerror.h>
+#include <popt.h>
+#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);
+}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface command "rmdir" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gerror.h>
+#include <popt.h>
+
+
+#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 */
--- /dev/null
+/* $Id$
+ * client cmdline interface command "shell" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
+#include <glib/gerror.h>
+#include <popt.h>
+#include <stdlib.h>
+#include <glib/gstrfuncs.h>
+#include <glib/gmem.h>
+
+#ifdef HAVE_LIBREADLINE
+#include <readline/readline.h>
+#ifdef HAVE_READLINE_HISTORY_H
+#include <readline/history.h>
+#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);
+ }
+}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface command "shell" for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gerror.h>
+#include <popt.h>
+
+
+#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 */
--- /dev/null
+/* $Id$
+ * client cmdline interface GnomeVFSFileInfo utils for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
+#include <glib/gerror.h>
+#include <libgnomevfs/gnome-vfs-file-info.h>
+#include <glib/gstrfuncs.h>
+#include <glib/gprintf.h>
+
+
+#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);
+}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface GnomeVFSFileInfo utils for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gerror.h>
+#include <libgnomevfs/gnome-vfs-file-info.h>
+
+
+#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 */
-/* FIXME: test source only! Don't pass into CVS! */
+/* $Id$
+ * client cmdline interface for libcaptive
+ * Copyright (C) 2002-2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gmessages.h>
-#include <stdio.h>
#include <stdlib.h>
#include <glib/giochannel.h>
+#include <glib/gerror.h>
+#include <popt.h>
+#include <string.h>
+#include <glib/gprintf.h>
#include <captive/client.h> /* 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 <captive_args> <image_filename>");
- 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_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;
+ }
+
+ (*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;
}
--- /dev/null
+/* $Id$
+ * Include file for client cmdline interface control functions for libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ *
+ * 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 <glib/gtypes.h>
+#include <libgnomevfs/gnome-vfs-result.h>
+
+
+#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 */