]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: utilsexec.cpp | |
3 | // Purpose: Execution-related utilities | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation | |
14 | #endif | |
15 | ||
16 | #include "wx/utils.h" | |
17 | #include "wx/app.h" | |
18 | ||
19 | #include <stdio.h> | |
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
22 | ||
23 | #ifdef VMS | |
24 | /*steve*/ | |
25 | #ifdef __HIDE_FORBIDDEN_NAMES | |
26 | #undefine __HIDE_FORBIDDEN_NAMES | |
27 | #endif | |
28 | #include <socket.h> | |
29 | #ifdef VAX | |
30 | /*because 'noshare' is not valid in vax C++*/ | |
31 | #define CC$VAXCSHR 1 | |
32 | #endif | |
33 | #include <unixlib.h> | |
34 | #define unlink DELETE | |
35 | ||
36 | #else | |
37 | ||
38 | #if defined(_AIX) || defined(__xlC__) | |
39 | #include <sys/socket.h> | |
40 | #include <sys/select.h> | |
41 | #else | |
42 | #ifndef DG | |
43 | #include <sys/syscall.h> | |
44 | #endif | |
45 | #endif | |
46 | ||
47 | #include <sys/wait.h> | |
48 | #include <unistd.h> | |
49 | #include <dirent.h> | |
50 | #include <pwd.h> | |
51 | ||
52 | #endif | |
53 | ||
54 | #include <sys/time.h> | |
55 | ||
56 | #include <Xm/Xm.h> | |
57 | ||
58 | #define wxEXECUTE_WIN_MESSAGE 10000 | |
59 | ||
60 | void xt_notify_end_process(XtPointer client, int *fid, | |
61 | XtInputId *id) | |
62 | { | |
63 | Bool *flag = (Bool *) client; | |
64 | *flag = TRUE; | |
65 | ||
66 | XtRemoveInput(*id); | |
67 | } | |
68 | ||
69 | long wxExecute(char **argv, bool sync, wxProcess *WXUNUSED(handler)) | |
70 | { | |
71 | #ifdef VMS | |
72 | return(0); | |
73 | #else | |
74 | if (*argv == NULL) | |
75 | return 0; // Nothing??? | |
76 | ||
77 | int proc_link[2]; | |
78 | if (pipe(proc_link)) | |
79 | return 0; | |
80 | ||
81 | /* fork the process */ | |
82 | #if defined(sun) || defined(__ultrix) || defined(__bsdi__) | |
83 | pid_t pid = vfork (); | |
84 | #else | |
85 | pid_t pid = fork (); | |
86 | #endif | |
87 | ||
88 | if (pid == -1) | |
89 | { | |
90 | return 0; | |
91 | } | |
92 | else if (pid == 0) | |
93 | { | |
94 | /* GUILHEM: Close all fds when sync == 0 */ | |
95 | if (sync == 0) | |
96 | for (int fd=0;fd<FD_SETSIZE;fd++) { | |
97 | if (proc_link[1] != fd) | |
98 | close(fd); | |
99 | } | |
100 | /* child */ | |
101 | #ifdef _AIX | |
102 | execvp ((const char *)*argv, (const char **)argv); | |
103 | #else | |
104 | execvp (*argv, argv); | |
105 | #endif | |
106 | /* GUILHEM: Reopen output stream */ | |
107 | // open("/dev/console", O_WRONLY); | |
108 | /* GUILHEM: End */ | |
109 | if (errno == ENOENT) | |
110 | printf ("%s: command not found\n", *argv); | |
111 | else | |
112 | perror (*argv); | |
113 | printf ("wxWindows: could not execute '%s'\n", *argv); | |
114 | _exit (-1); | |
115 | } | |
116 | ||
117 | int end_process = 0; | |
118 | ||
119 | close(proc_link[1]); | |
120 | XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(), proc_link[0], | |
121 | (XtPointer *) XtInputReadMask, | |
122 | (XtInputCallbackProc) xt_notify_end_process, | |
123 | (XtPointer) &end_process); | |
124 | ||
125 | if (sync) { | |
126 | while (!end_process) | |
127 | XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll); | |
128 | ||
129 | if (WIFEXITED(end_process) != 0) | |
130 | return WEXITSTATUS(end_process); | |
131 | } | |
132 | ||
133 | return pid; | |
134 | #endif | |
135 | // end VMS | |
136 | } | |
137 | ||
138 | long wxExecute (const wxString& command, bool sync) | |
139 | { | |
140 | #ifdef VMS | |
141 | return(0); | |
142 | #else | |
143 | if (command.IsNull() || command == "") | |
144 | return 0; // Nothing to do | |
145 | ||
146 | // Run a program the recomended way under X (XView) | |
147 | int argc = 0; | |
148 | char *argv[127]; | |
149 | char tmp[1024]; | |
150 | const char *IFS = " \t\n"; | |
151 | ||
152 | // Build argument vector | |
153 | strncpy (tmp, (const char*) command, sizeof (tmp) / sizeof (char) - 1); | |
154 | tmp[sizeof (tmp) / sizeof (char) - 1] = '\0'; | |
155 | argv[argc++] = strtok (tmp, IFS); | |
156 | while ((argv[argc++] = strtok (NULL, IFS)) != NULL) | |
157 | /* loop */ ; | |
158 | ||
159 | return wxExecute(argv, sync); | |
160 | #endif | |
161 | // VMS | |
162 | } |