2 * client cmdline interface command "shell" for libcaptive
3 * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <glib/gmessages.h>
23 #include <glib/gerror.h>
26 #include <glib/gstrfuncs.h>
27 #include <glib/gmem.h>
30 #ifdef HAVE_LIBREADLINE
31 #include <readline/readline.h>
32 #ifdef HAVE_READLINE_HISTORY_H
33 #include <readline/history.h>
34 #endif /* HAVE_READLINE_HISTORY_H */
35 #endif /* HAVE_LIBREADLINE */
37 #include "cmd_shell.h" /* self */
38 #include "cmd_quit.h" /* for cmd_quit() */
39 #include "main.h" /* for invoke_cmd() */
44 #define PROMPT_STRING "%s$ " /* %s == 'cmdline_cwd' */
47 GQuark cmdline_cmd_shell_error_quark(void)
52 r=g_quark_from_static_string("cmdline-cmd-shell");
58 const struct poptOption cmd_shell_table[]={
64 /* Function mostly stolen from the project Partition Surprise.
66 void cmd_shell(const char **cmd_argv,GError **errp)
69 #ifndef HAVE_LIBREADLINE
70 char linebuf[1024],*s;
71 #endif /* HAVE_LIBREADLINE */
73 const char **line_argv=NULL;
75 const char *cmd_cd_args[]={NULL};
77 g_return_if_fail(!errp || !*errp);
79 #ifndef HAVE_LIBREADLINE
80 puts(_("Line editing not available, please recompile with readline library installed"));
81 #endif /* HAVE_LIBREADLINE */
83 cmd_cd(cmd_cd_args,errp); /* Show current directory. */
88 prompt=g_strdup_printf(PROMPT_STRING,cmdline_cwd);
89 #ifdef HAVE_LIBREADLINE
90 line=readline(prompt);
91 #ifdef HAVE_ADD_HISTORY
94 #endif /* HAVE_ADD_HISTORY */
95 #else /* HAVE_LIBREADLINE */
96 fputs(prompt,stdout); fflush(stdout);
97 line=fgets(linebuf,sizeof(linebuf),stdin);
98 #endif /* HAVE_LIBREADLINE */
104 g_assert_not_reached();
106 #ifndef HAVE_LIBREADLINE
107 if (line && (s=strchr(line,'\n')))
109 #endif /* HAVE_LIBREADLINE */
112 errint=poptParseArgvString(line,&line_argc,&line_argv);
113 #ifdef HAVE_LIBREADLINE
115 #endif /* HAVE_LIBREADLINE */
118 g_set_error(errp,CMDLINE_CMD_SHELL_ERROR,CMDLINE_CMD_SHELL_ERROR_LINE_PARSE_ARGUMENTS,
119 _("Error '%s' parsing arguments of text line: %s"),poptStrerror(errint),line);
124 invoke_cmd(line_argc,line_argv); /* errors catched inside */