lexer: Reimplement for better testability and internationalization.
[pspp-builds.git] / src / language / utilities / host.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #if HAVE_SYS_WAIT_H
25 #include <sys/wait.h>
26 #endif
27
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"
36
37 #include "gl/localcharset.h"
38 #include "gl/xalloc.h"
39 #include "gl/xmalloca.h"
40
41 #include "gettext.h"
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) msgid
44 \f
45 #if HAVE_FORK && HAVE_EXECL
46 /* Spawn an interactive shell process. */
47 static bool
48 shell (void)
49 {
50   int pid;
51
52   pid = fork ();
53   switch (pid)
54     {
55     case 0:
56       {
57         const char *shell_fn;
58         char *shell_process;
59
60         {
61           int i;
62
63           for (i = 3; i < 20; i++)
64             close (i);
65         }
66
67         shell_fn = getenv ("SHELL");
68         if (shell_fn == NULL)
69           shell_fn = "/bin/sh";
70
71         {
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] = '+';
79         }
80
81         execl (shell_fn, shell_process, NULL);
82
83         _exit (1);
84       }
85
86     case -1:
87       msg (SE, _("Couldn't fork: %s."), strerror (errno));
88       return false;
89
90     default:
91       assert (pid > 0);
92       while (wait (NULL) != pid)
93         ;
94       return true;
95     }
96 }
97 #else /* !(HAVE_FORK && HAVE_EXECL) */
98 /* Don't know how to spawn an interactive shell. */
99 static bool
100 shell (void)
101 {
102   msg (SE, _("Interactive shell not supported on this platform."));
103   return false;
104 }
105 #endif
106
107 /* Executes the specified COMMAND in a subshell.  Returns true if
108    successful, false otherwise. */
109 static bool
110 run_command (const char *command)
111 {
112   if (system (NULL) == 0)
113     {
114       msg (SE, _("Command shell not supported on this platform."));
115       return false;
116     }
117
118   /* Execute the command. */
119   if (system (command) == -1)
120     msg (SE, _("Error executing command: %s."), strerror (errno));
121
122   return true;
123 }
124
125 int
126 cmd_host (struct lexer *lexer, struct dataset *ds UNUSED)
127 {
128   if (settings_get_safer_mode ())
129     {
130       msg (SE, _("This command not allowed when the SAFER option is set."));
131       return CMD_FAILURE;
132     }
133
134   if (lex_token (lexer) == T_ENDCMD)
135     return shell () ? CMD_SUCCESS : CMD_FAILURE;
136   else if (lex_match_id (lexer, "COMMAND"))
137     {
138       struct string command;
139       char *locale_command;
140       bool ok;
141
142       lex_match (lexer, T_EQUALS);
143       if (!lex_force_match (lexer, T_LBRACK))
144         return CMD_FAILURE;
145
146       ds_init_empty (&command);
147       while (lex_is_string (lexer))
148         {
149           if (!ds_is_empty (&command))
150             ds_put_byte (&command, '\n');
151           ds_put_substring (&command, lex_tokss (lexer));
152           lex_get (lexer);
153         }
154       if (!lex_force_match (lexer, T_RBRACK))
155         {
156           ds_destroy (&command);
157           return CMD_FAILURE;
158         }
159
160       locale_command = recode_string (locale_charset (), "UTF-8",
161                                       ds_cstr (&command),
162                                       ds_length (&command));
163       ds_destroy (&command);
164
165       ok = run_command (locale_command);
166       free (locale_command);
167
168       return ok ? CMD_SUCCESS : CMD_FAILURE;
169     }
170   else
171     {
172       lex_error (lexer, NULL);
173       return CMD_FAILURE;
174     }
175 }