1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010 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/message.h"
34 #include "libpspp/str.h"
36 #include "gl/xalloc.h"
37 #include "gl/xmalloca.h"
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) msgid
43 #if HAVE_FORK && HAVE_EXECL
44 /* Spawn an interactive shell process. */
61 for (i = 3; i < 20; i++)
65 shell_fn = getenv ("SHELL");
70 const char *cp = strrchr (shell_fn, '/');
71 cp = cp ? &cp[1] : shell_fn;
72 shell_process = xmalloca (strlen (cp) + 8);
73 strcpy (shell_process, "-");
74 strcat (shell_process, cp);
75 if (strcmp (cp, "sh"))
76 shell_process[0] = '+';
79 execl (shell_fn, shell_process, NULL);
85 msg (SE, _("Couldn't fork: %s."), strerror (errno));
90 while (wait (NULL) != pid)
95 #else /* !(HAVE_FORK && HAVE_EXECL) */
96 /* Don't know how to spawn an interactive shell. */
100 msg (SE, _("Interactive shell not supported on this platform."));
105 /* Executes the specified COMMAND in a subshell. Returns true if
106 successful, false otherwise. */
108 run_command (const char *command)
110 if (system (NULL) == 0)
112 msg (SE, _("Command shell not supported on this platform."));
116 /* Execute the command. */
117 if (system (command) == -1)
118 msg (SE, _("Error executing command: %s."), strerror (errno));
124 cmd_host (struct lexer *lexer, struct dataset *ds UNUSED)
126 if (settings_get_safer_mode ())
128 msg (SE, _("This command not allowed when the SAFER option is set."));
132 if (lex_token (lexer) == T_ENDCMD)
133 return shell () ? CMD_SUCCESS : CMD_FAILURE;
134 else if (lex_match_id (lexer, "COMMAND"))
136 struct string command;
139 lex_match (lexer, T_EQUALS);
140 if (!lex_force_match (lexer, T_LBRACK))
143 ds_init_empty (&command);
144 while (lex_is_string (lexer))
146 if (!ds_is_empty (&command))
147 ds_put_byte (&command, '\n');
148 ds_put_substring (&command, lex_tokss (lexer));
151 if (!lex_force_match (lexer, T_RBRACK))
153 ds_destroy (&command);
157 ok = run_command (ds_cstr (&command));
158 ds_destroy (&command);
160 return ok ? lex_end_of_command (lexer) : CMD_FAILURE;
164 lex_error (lexer, NULL);