1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
28 #include "data/settings.h"
29 #include "language/command.h"
30 #include "language/lexer/lexer.h"
31 #include "libpspp/assertion.h"
32 #include "libpspp/compiler.h"
33 #include "libpspp/i18n.h"
34 #include "libpspp/message.h"
35 #include "libpspp/str.h"
37 #include "gl/localcharset.h"
38 #include "gl/xalloc.h"
39 #include "gl/xmalloca.h"
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) msgid
45 #if HAVE_FORK && HAVE_EXECL
46 /* Spawn an interactive shell process. */
63 for (i = 3; i < 20; i++)
67 shell_fn = getenv ("SHELL");
72 const char *cp = strrchr (shell_fn, '/');
73 cp = cp ? &cp[1] : shell_fn;
74 shell_process = xmalloca (strlen (cp) + 8);
75 strcpy (shell_process, "-");
76 strcat (shell_process, cp);
77 if (strcmp (cp, "sh"))
78 shell_process[0] = '+';
81 execl (shell_fn, shell_process, NULL);
87 msg (SE, _("Couldn't fork: %s."), strerror (errno));
92 while (wait (NULL) != pid)
97 #else /* !(HAVE_FORK && HAVE_EXECL) */
98 /* Don't know how to spawn an interactive shell. */
102 msg (SE, _("Interactive shell not supported on this platform."));
107 /* Executes the specified COMMAND in a subshell. Returns true if
108 successful, false otherwise. */
110 run_command (const char *command)
112 if (system (NULL) == 0)
114 msg (SE, _("Command shell not supported on this platform."));
118 /* Execute the command. */
119 if (system (command) == -1)
120 msg (SE, _("Error executing command: %s."), strerror (errno));
126 cmd_host (struct lexer *lexer, struct dataset *ds UNUSED)
128 if (settings_get_safer_mode ())
130 msg (SE, _("This command not allowed when the SAFER option is set."));
134 if (lex_token (lexer) == T_ENDCMD)
135 return shell () ? CMD_SUCCESS : CMD_FAILURE;
136 else if (lex_match_id (lexer, "COMMAND"))
138 struct string command;
139 char *locale_command;
142 lex_match (lexer, T_EQUALS);
143 if (!lex_force_match (lexer, T_LBRACK))
146 ds_init_empty (&command);
147 while (lex_is_string (lexer))
149 if (!ds_is_empty (&command))
150 ds_put_byte (&command, '\n');
151 ds_put_substring (&command, lex_tokss (lexer));
154 if (!lex_force_match (lexer, T_RBRACK))
156 ds_destroy (&command);
160 locale_command = recode_string (locale_charset (), "UTF-8",
162 ds_length (&command));
163 ds_destroy (&command);
165 ok = run_command (locale_command);
166 free (locale_command);
168 return ok ? CMD_SUCCESS : CMD_FAILURE;
172 lex_error (lexer, NULL);