2 Free Software Foundation, Inc.
3 Written by: Keith Marshall (keith.d.marshall@ntlworld.com)
5 This file is part of groff.
7 groff 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 2, or (at your option) any later
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with groff; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 #define SPAWN_FUNCTION_WRAPPERS 1
34 /* Define the default mechanism, and messages, for error reporting
35 * (user may substitute a preferred alternative, by defining his own
36 * implementation of the macros REPORT_ERROR and ARGV_MALLOC_ERROR,
37 * in the header file `nonposix.h').
43 # define REPORT_ERROR(WHY) fprintf(stderr, "%s:%s\n", program_name, WHY)
45 #ifndef ARGV_MALLOC_ERROR
46 # define ARGV_MALLOC_ERROR "malloc: Allocation for 'argv' failed"
49 extern char *program_name
;
51 extern char *quote_arg(char *string
);
52 extern void purge_quoted_args(char **argv
);
55 spawnvp_wrapper(int mode
, char *path
, char **argv
)
57 /* Invoke the system `spawnvp' service
58 * enclosing the passed arguments in double quotes, as required,
59 * so that the (broken) default parsing in the MSVC runtime doesn't
60 * split them at whitespace. */
62 char **quoted_argv
; /* used to build a quoted local copy of `argv' */
64 int i
; /* used as an index into `argv' or `quoted_argv' */
65 int status
= -1; /* initialise return code, in case we fail */
66 int argc
= 0; /* initialise argument count; may be none */
68 /* First count the number of arguments
69 * which are actually present in the passed `argv'. */
72 for (quoted_argv
= argv
; *quoted_argv
; ++argc
, ++quoted_argv
)
75 /* If we do not now have an argument count,
76 * then we must fall through and fail. */
79 /* We do have at least one argument:
80 * We will use a copy of the `argv', in which to do the quoting,
81 * so we must allocate space for it. */
83 if ((quoted_argv
= malloc(++argc
* sizeof(char **))) == NULL
) {
84 /* If we didn't get enough space,
85 * then complain, and bail out gracefully. */
87 REPORT_ERROR(ARGV_MALLOC_ERROR
);
91 /* Now copy the passed `argv' into our new vector,
92 * quoting its contents as required. */
94 for (i
= 0; i
< argc
; i
++)
95 quoted_argv
[i
] = quote_arg(argv
[i
]);
97 /* Invoke the MSVC `spawnvp' service
98 * passing our now appropriately quoted copy of `argv'. */
100 status
= spawnvp(mode
, path
, quoted_argv
);
102 /* Clean up our memory allocations
103 * for the quoted copy of `argv', which is no longer required. */
105 purge_quoted_args(quoted_argv
);
110 * return the status code returned by `spawnvp',
111 * or a failure code if we fell through. */
116 /* spawnvp.c: end of file */