windows-spawn: Export another auxiliary function.
[gnulib.git] / lib / windows-spawn.h
blob90f45e15ea7925c4efe3ac9d754ecff11f8c6932
1 /* Auxiliary functions for the creation of subprocesses. Native Windows API.
2 Copyright (C) 2001, 2003-2020 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #ifndef _WINDOWS_SPAWN_H
19 #define _WINDOWS_SPAWN_H
21 #include <stdint.h>
23 /* Get declarations of the native Windows API functions. */
24 #define WIN32_LEAN_AND_MEAN
25 #include <windows.h>
27 /* Prepares an argument vector before calling spawn().
29 Note that spawn() does not by itself call the command interpreter
30 (getenv ("COMSPEC") != NULL ? getenv ("COMSPEC") :
31 ({ OSVERSIONINFO v; v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
32 GetVersionEx(&v);
33 v.dwPlatformId == VER_PLATFORM_WIN32_NT;
34 }) ? "cmd.exe" : "command.com").
35 Instead it simply concatenates the arguments, separated by ' ', and calls
36 CreateProcess(). We must quote the arguments since Windows CreateProcess()
37 interprets characters like ' ', '\t', '\\', '"' (but not '<' and '>') in a
38 special way:
39 - Space and tab are interpreted as delimiters. They are not treated as
40 delimiters if they are surrounded by double quotes: "...".
41 - Unescaped double quotes are removed from the input. Their only effect is
42 that within double quotes, space and tab are treated like normal
43 characters.
44 - Backslashes not followed by double quotes are not special.
45 - But 2*n+1 backslashes followed by a double quote become
46 n backslashes followed by a double quote (n >= 0):
47 \" -> "
48 \\\" -> \"
49 \\\\\" -> \\"
50 - '*', '?' characters may get expanded through wildcard expansion in the
51 callee: By default, in the callee, the initialization code before main()
52 takes the result of GetCommandLine(), wildcard-expands it, and passes it
53 to main(). The exceptions to this rule are:
54 - programs that inspect GetCommandLine() and ignore argv,
55 - mingw programs that have a global variable 'int _CRT_glob = 0;',
56 - Cygwin programs, when invoked from a Cygwin program.
58 prepare_spawn creates and returns a new argument vector, where the arguments
59 are appropriately quoted and an additional argument "sh.exe" has been added
60 at the beginning. The new argument vector is freshly allocated. The memory
61 for all its elements is allocated within *MEM_TO_FREE, which is freshly
62 allocated as well. In case of memory allocation failure, NULL is returned,
63 with errno set.
65 extern const char ** prepare_spawn (const char * const *argv,
66 char **mem_to_free);
68 /* Composes the command to be passed to CreateProcess().
69 ARGV must contain appropriately quoted arguments, as returned by
70 prepare_spawn.
71 Returns a freshly allocated string. In case of memory allocation failure,
72 NULL is returned, with errno set. */
73 extern char * compose_command (const char * const *argv);
75 /* Composes the block of memory that contains the environment variables.
76 ENVP must contain an environment (a NULL-terminated array of string of the
77 form VARIABLE=VALUE).
78 Returns a freshly allocated block of memory. In case of memory allocation
79 failure, NULL is returned, with errno set. */
80 extern char * compose_envblock (const char * const *envp);
82 /* Creates a subprocess.
83 MODE is either P_WAIT or P_NOWAIT.
84 PROGNAME is the program to invoke.
85 ARGV is the NULL-terminated array of arguments, ARGV[0] being PROGNAME by
86 convention.
87 ENVP is the NULL-terminated set of environment variable assignments, or NULL
88 to inherit the initial environ variable assignments from the caller and
89 ignore all calls to putenv(), setenv(), unsetenv() done in the caller.
90 CURRDIR is the directory in which to start the program, or NULL to inherit
91 the working directory from the caller.
92 STDIN_HANDLE, STDOUT_HANDLE, STDERR_HANDLE are the handles to use for the
93 first three file descriptors in the callee process.
94 Returns
95 - 0 for success (if MODE is P_WAIT), or
96 - a handle that be passed to _cwait (on Windows) or waitpid (on OS/2), or
97 - -1 upon error, with errno set.
99 extern intptr_t spawnpvech (int mode,
100 const char *progname, const char * const *argv,
101 const char * const *envp,
102 const char *currdir,
103 HANDLE stdin_handle, HANDLE stdout_handle,
104 HANDLE stderr_handle);
106 #endif /* _WINDOWS_SPAWN_H */