annotate PC/w9xpopen.c @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 6bea2f1351b1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13461
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1 /*
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2 * w9xpopen.c
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
3 *
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
4 * Serves as an intermediate stub Win32 console application to
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
5 * avoid a hanging pipe when redirecting 16-bit console based
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
6 * programs (including MS-DOS console based programs and batch
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
7 * files) on Window 95 and Windows 98.
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
8 *
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
9 * This program is to be launched with redirected standard
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
10 * handles. It will launch the command line specified 16-bit
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
11 * console based application in the same console, forwarding
35276
4adbd331d335 Fix all wrong instances of "it's".
Georg Brandl <georg@python.org>
parents: 24233
diff changeset
12 * its own redirected standard handles to the 16-bit child.
13461
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
13
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
14 * AKA solution to the problem described in KB: Q150956.
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
15 */
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
16
24233
11c4253698dc WINDOWS_LEAN_AND_MEAN: There is no such symbol, although a very few
Tim Peters <tim.peters@gmail.com>
parents: 23067
diff changeset
17 #define WIN32_LEAN_AND_MEAN
13461
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
18 #include <windows.h>
22876
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
19 #include <stdio.h>
24233
11c4253698dc WINDOWS_LEAN_AND_MEAN: There is no such symbol, although a very few
Tim Peters <tim.peters@gmail.com>
parents: 23067
diff changeset
20 #include <stdlib.h> /* for malloc and its friends */
13461
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
21
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
22 const char *usage =
23067
f3677414bc3d Fix grammar
Neal Norwitz <nnorwitz@gmail.com>
parents: 22876
diff changeset
23 "This program is used by Python's os.popen function\n"
13461
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
24 "to work around a limitation in Windows 95/98. It is\n"
23067
f3677414bc3d Fix grammar
Neal Norwitz <nnorwitz@gmail.com>
parents: 22876
diff changeset
25 "not designed to be used as a stand-alone program.";
13461
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
26
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
27 int main(int argc, char *argv[])
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
28 {
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
29 BOOL bRet;
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
30 STARTUPINFO si;
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
31 PROCESS_INFORMATION pi;
13961
c9aa49e8748e Patch #101032, from David Bolen:
Mark Hammond <mhammond@skippinet.com.au>
parents: 13461
diff changeset
32 DWORD exit_code=0;
46084
6bea2f1351b1 Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 35276
diff changeset
33 size_t cmdlen = 0;
22876
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
34 int i;
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
35 char *cmdline, *cmdlinefill;
13461
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
36
22876
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
37 if (argc < 2) {
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
38 if (GetFileType(GetStdHandle(STD_INPUT_HANDLE))==FILE_TYPE_CHAR)
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
39 /* Attached to a console, and therefore not executed by Python
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
40 Display a message box for the inquisitive user
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
41 */
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
42 MessageBox(NULL, usage, argv[0], MB_OK);
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
43 else {
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
44 /* Eeek - executed by Python, but args are screwed!
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
45 Write an error message to stdout so there is at
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
46 least some clue for the end user when it appears
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
47 in their output.
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
48 A message box would be hidden and blocks the app.
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
49 */
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
50 fprintf(stdout, "Internal popen error - no args specified\n%s\n", usage);
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
51 }
13461
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
52 return 1;
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
53 }
22876
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
54 /* Build up the command-line from the args.
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
55 Args with a space are quoted, existing quotes are escaped.
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
56 To keep things simple calculating the buffer size, we assume
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
57 every character is a quote - ie, we allocate double what we need
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
58 in the worst case. As this is only double the command line passed
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
59 to us, there is a good chance this is reasonably small, so the total
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
60 allocation will almost always be < 512 bytes.
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
61 */
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
62 for (i=1;i<argc;i++)
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
63 cmdlen += strlen(argv[i])*2 + 3; /* one space, maybe 2 quotes */
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
64 cmdline = cmdlinefill = (char *)malloc(cmdlen+1);
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
65 if (cmdline == NULL)
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
66 return -1;
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
67 for (i=1;i<argc;i++) {
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
68 const char *arglook;
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
69 int bQuote = strchr(argv[i], ' ') != NULL;
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
70 if (bQuote)
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
71 *cmdlinefill++ = '"';
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
72 /* escape quotes */
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
73 for (arglook=argv[i];*arglook;arglook++) {
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
74 if (*arglook=='"')
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
75 *cmdlinefill++ = '\\';
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
76 *cmdlinefill++ = *arglook;
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
77 }
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
78 if (bQuote)
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
79 *cmdlinefill++ = '"';
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
80 *cmdlinefill++ = ' ';
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
81 }
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
82 *cmdlinefill = '\0';
13461
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
83
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
84 /* Make child process use this app's standard files. */
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
85 ZeroMemory(&si, sizeof si);
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
86 si.cb = sizeof si;
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
87 si.dwFlags = STARTF_USESTDHANDLES;
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
88 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
89 si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
90 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
91
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
92 bRet = CreateProcess(
22876
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
93 NULL, cmdline,
13461
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
94 NULL, NULL,
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
95 TRUE, 0,
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
96 NULL, NULL,
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
97 &si, &pi
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
98 );
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
99
22876
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
100 free(cmdline);
1a3b08663ebe Backport of bug
Anthony Baxter <anthonybaxter@gmail.com>
parents: 13961
diff changeset
101
13461
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
102 if (bRet) {
13961
c9aa49e8748e Patch #101032, from David Bolen:
Mark Hammond <mhammond@skippinet.com.au>
parents: 13461
diff changeset
103 if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_FAILED) {
c9aa49e8748e Patch #101032, from David Bolen:
Mark Hammond <mhammond@skippinet.com.au>
parents: 13461
diff changeset
104 GetExitCodeProcess(pi.hProcess, &exit_code);
c9aa49e8748e Patch #101032, from David Bolen:
Mark Hammond <mhammond@skippinet.com.au>
parents: 13461
diff changeset
105 }
13461
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
106 CloseHandle(pi.hProcess);
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
107 CloseHandle(pi.hThread);
13961
c9aa49e8748e Patch #101032, from David Bolen:
Mark Hammond <mhammond@skippinet.com.au>
parents: 13461
diff changeset
108 return exit_code;
13461
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
109 }
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
110
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
111 return 1;
f6cdeac44a10 - win95/98 helper for new os.popen code
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
112 }