Mercurial > hg > CbC > CbC_gcc
comparison libgfortran/intrinsics/execute_command_line.c @ 111:04ced10e8804
gcc 7
author | kono |
---|---|
date | Fri, 27 Oct 2017 22:46:09 +0900 |
parents | |
children | 84e7813d76e9 |
comparison
equal
deleted
inserted
replaced
68:561a7518be6b | 111:04ced10e8804 |
---|---|
1 /* Implementation of the EXECUTE_COMMAND_LINE intrinsic. | |
2 Copyright (C) 2009-2017 Free Software Foundation, Inc. | |
3 Contributed by François-Xavier Coudert. | |
4 | |
5 This file is part of the GNU Fortran runtime library (libgfortran). | |
6 | |
7 Libgfortran is free software; you can redistribute it and/or modify it under | |
8 the terms of the GNU General Public License as published by the Free | |
9 Software Foundation; either version 3, or (at your option) any later | |
10 version. | |
11 | |
12 Libgfortran is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 Under Section 7 of GPL version 3, you are granted additional | |
18 permissions described in the GCC Runtime Library Exception, version | |
19 3.1, as published by the Free Software Foundation. | |
20 | |
21 You should have received a copy of the GNU General Public License and | |
22 a copy of the GCC Runtime Library Exception along with this program; | |
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 <http://www.gnu.org/licenses/>. */ | |
25 | |
26 #include "libgfortran.h" | |
27 #include <string.h> | |
28 | |
29 #ifdef HAVE_UNISTD_H | |
30 #include <unistd.h> | |
31 #endif | |
32 #ifdef HAVE_SYS_WAIT_H | |
33 #include <sys/wait.h> | |
34 #endif | |
35 | |
36 | |
37 enum { EXEC_SYNCHRONOUS = -2, EXEC_NOERROR = 0, EXEC_SYSTEMFAILED, | |
38 EXEC_CHILDFAILED, EXEC_INVALIDCOMMAND }; | |
39 static const char *cmdmsg_values[] = | |
40 { "", | |
41 "Termination status of the command-language interpreter cannot be obtained", | |
42 "Execution of child process impossible", | |
43 "Invalid command line" }; | |
44 | |
45 | |
46 | |
47 static void | |
48 set_cmdstat (int *cmdstat, int value) | |
49 { | |
50 if (cmdstat) | |
51 *cmdstat = value; | |
52 else if (value > EXEC_NOERROR) | |
53 { | |
54 #define MSGLEN 200 | |
55 char msg[MSGLEN] = "EXECUTE_COMMAND_LINE: "; | |
56 strncat (msg, cmdmsg_values[value], MSGLEN - strlen(msg) - 1); | |
57 runtime_error ("%s", msg); | |
58 } | |
59 } | |
60 | |
61 | |
62 static void | |
63 execute_command_line (const char *command, bool wait, int *exitstat, | |
64 int *cmdstat, char *cmdmsg, | |
65 gfc_charlen_type command_len, | |
66 gfc_charlen_type cmdmsg_len) | |
67 { | |
68 /* Transform the Fortran string to a C string. */ | |
69 char *cmd = fc_strdup (command, command_len); | |
70 | |
71 /* Flush all I/O units before executing the command. */ | |
72 flush_all_units(); | |
73 | |
74 #if defined(HAVE_FORK) | |
75 if (!wait) | |
76 { | |
77 /* Asynchronous execution. */ | |
78 pid_t pid; | |
79 | |
80 set_cmdstat (cmdstat, EXEC_NOERROR); | |
81 | |
82 if ((pid = fork()) < 0) | |
83 set_cmdstat (cmdstat, EXEC_CHILDFAILED); | |
84 else if (pid == 0) | |
85 { | |
86 /* Child process. */ | |
87 int res = system (cmd); | |
88 _exit (WIFEXITED(res) ? WEXITSTATUS(res) : res); | |
89 } | |
90 } | |
91 else | |
92 #endif | |
93 { | |
94 /* Synchronous execution. */ | |
95 int res = system (cmd); | |
96 | |
97 if (res == -1) | |
98 set_cmdstat (cmdstat, EXEC_SYSTEMFAILED); | |
99 #ifndef HAVE_FORK | |
100 else if (!wait) | |
101 set_cmdstat (cmdstat, EXEC_SYNCHRONOUS); | |
102 #endif | |
103 else if (res == 127 || res == 126 | |
104 #if defined(WEXITSTATUS) && defined(WIFEXITED) | |
105 || (WIFEXITED(res) && WEXITSTATUS(res) == 127) | |
106 || (WIFEXITED(res) && WEXITSTATUS(res) == 126) | |
107 #endif | |
108 ) | |
109 /* Shell return codes 126 and 127 mean that the command line could | |
110 not be executed for various reasons. */ | |
111 set_cmdstat (cmdstat, EXEC_INVALIDCOMMAND); | |
112 else | |
113 set_cmdstat (cmdstat, EXEC_NOERROR); | |
114 | |
115 if (res != -1) | |
116 { | |
117 #if defined(WEXITSTATUS) && defined(WIFEXITED) | |
118 *exitstat = WIFEXITED(res) ? WEXITSTATUS(res) : res; | |
119 #else | |
120 *exitstat = res; | |
121 #endif | |
122 } | |
123 } | |
124 | |
125 free (cmd); | |
126 | |
127 /* Now copy back to the Fortran string if needed. */ | |
128 if (cmdstat && *cmdstat > EXEC_NOERROR && cmdmsg) | |
129 fstrcpy (cmdmsg, cmdmsg_len, cmdmsg_values[*cmdstat], | |
130 strlen (cmdmsg_values[*cmdstat])); | |
131 } | |
132 | |
133 | |
134 extern void | |
135 execute_command_line_i4 (const char *command, GFC_LOGICAL_4 *wait, | |
136 GFC_INTEGER_4 *exitstat, GFC_INTEGER_4 *cmdstat, | |
137 char *cmdmsg, gfc_charlen_type command_len, | |
138 gfc_charlen_type cmdmsg_len); | |
139 export_proto(execute_command_line_i4); | |
140 | |
141 void | |
142 execute_command_line_i4 (const char *command, GFC_LOGICAL_4 *wait, | |
143 GFC_INTEGER_4 *exitstat, GFC_INTEGER_4 *cmdstat, | |
144 char *cmdmsg, gfc_charlen_type command_len, | |
145 gfc_charlen_type cmdmsg_len) | |
146 { | |
147 bool w = wait ? *wait : true; | |
148 int estat, estat_initial, cstat; | |
149 | |
150 if (exitstat) | |
151 estat_initial = estat = *exitstat; | |
152 | |
153 execute_command_line (command, w, &estat, cmdstat ? &cstat : NULL, | |
154 cmdmsg, command_len, cmdmsg_len); | |
155 | |
156 if (exitstat && estat != estat_initial) | |
157 *exitstat = estat; | |
158 if (cmdstat) | |
159 *cmdstat = cstat; | |
160 } | |
161 | |
162 | |
163 extern void | |
164 execute_command_line_i8 (const char *command, GFC_LOGICAL_8 *wait, | |
165 GFC_INTEGER_8 *exitstat, GFC_INTEGER_8 *cmdstat, | |
166 char *cmdmsg, gfc_charlen_type command_len, | |
167 gfc_charlen_type cmdmsg_len); | |
168 export_proto(execute_command_line_i8); | |
169 | |
170 void | |
171 execute_command_line_i8 (const char *command, GFC_LOGICAL_8 *wait, | |
172 GFC_INTEGER_8 *exitstat, GFC_INTEGER_8 *cmdstat, | |
173 char *cmdmsg, gfc_charlen_type command_len, | |
174 gfc_charlen_type cmdmsg_len) | |
175 { | |
176 bool w = wait ? *wait : true; | |
177 int estat, estat_initial, cstat; | |
178 | |
179 if (exitstat) | |
180 estat_initial = estat = *exitstat; | |
181 | |
182 execute_command_line (command, w, &estat, cmdstat ? &cstat : NULL, | |
183 cmdmsg, command_len, cmdmsg_len); | |
184 | |
185 if (exitstat && estat != estat_initial) | |
186 *exitstat = estat; | |
187 if (cmdstat) | |
188 *cmdstat = cstat; | |
189 } |