VirtualBox

source: kBuild/trunk/src/lib/quote_argv.c@ 2907

Last change on this file since 2907 was 2894, checked in by bird, 9 years ago

Included kDepObj in kWorker as a post execution option.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 KB
Line 
1/* $Id: quote_argv.c 2894 2016-09-08 13:27:56Z bird $ */
2/** @file
3 * quote_argv - Correctly quote argv for spawn, windows specific.
4 */
5
6/*
7 * Copyright (c) 2007-2016 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 *
27 * Alternatively, the content of this file may be used under the terms of the
28 * GPL version 2 or later, or LGPL version 2.1 or later.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "quote_argv.h"
36#include <stdlib.h>
37#include <string.h>
38#include <ctype.h>
39
40#ifndef KBUILD_OS_WINDOWS
41# error "KBUILD_OS_WINDOWS not defined"
42#endif
43
44
45/**
46 * Checks if this is an Watcom option where we must just pass thru the string
47 * as-is.
48 *
49 * This is currnetly only used for -d (defining macros).
50 *
51 * @returns 1 if pass-thru, 0 if not.
52 * @param pszArg The argument to consider.
53 */
54static int isWatcomPassThruOption(const char *pszArg)
55{
56 char ch = *pszArg++;
57 if (ch != '-' && ch != '/')
58 return 0;
59 ch = *pszArg++;
60 switch (ch)
61 {
62 /* Example: -d+VAR="string-value" */
63 case 'd':
64 if (ch == '+')
65 ch = *pszArg++;
66 if (!isalpha(ch) && ch != '_')
67 return 0;
68 return 1;
69
70 default:
71 return 0;
72 }
73}
74
75
76/**
77 * Replaces arguments in need of quoting.
78 *
79 * For details on how MSC parses the command line, see "Parsing C Command-Line
80 * Arguments": http://msdn.microsoft.com/en-us/library/a1y7w461.aspx
81 *
82 * @param argc The argument count.
83 * @param argv The argument vector.
84 * @param fWatcomBrainDamage Set if we're catering for wcc, wcc386 or similar
85 * OpenWatcom tools. They seem to follow some
86 * ancient or home made quoting convention.
87 * @param fFreeOrLeak Whether to free replaced argv members
88 * (non-zero), or just leak them (zero). This
89 * depends on which argv you're working on.
90 * Suggest doing the latter if it's main()'s argv.
91 */
92void quote_argv(int argc, char **argv, int fWatcomBrainDamage, int fFreeOrLeak)
93{
94 int i;
95 for (i = 0; i < argc; i++)
96 {
97 char *const pszOrgOrg = argv[i];
98 const char *pszOrg = pszOrgOrg;
99 size_t cchOrg = strlen(pszOrg);
100 const char *pszQuotes = (const char *)memchr(pszOrg, '"', cchOrg);
101 const char *pszProblem = NULL;
102 if ( pszQuotes
103 || cchOrg == 0
104 || (pszProblem = (const char *)memchr(pszOrg, ' ', cchOrg)) != NULL
105 || (pszProblem = (const char *)memchr(pszOrg, '\t', cchOrg)) != NULL
106 || (pszProblem = (const char *)memchr(pszOrg, '\n', cchOrg)) != NULL
107 || (pszProblem = (const char *)memchr(pszOrg, '\r', cchOrg)) != NULL
108 || (pszProblem = (const char *)memchr(pszOrg, '&', cchOrg)) != NULL
109 || (pszProblem = (const char *)memchr(pszOrg, '>', cchOrg)) != NULL
110 || (pszProblem = (const char *)memchr(pszOrg, '<', cchOrg)) != NULL
111 || (pszProblem = (const char *)memchr(pszOrg, '|', cchOrg)) != NULL
112 || (pszProblem = (const char *)memchr(pszOrg, '%', cchOrg)) != NULL
113 || (pszProblem = (const char *)memchr(pszOrg, '\'', cchOrg)) != NULL
114 || ( !fWatcomBrainDamage
115 && (pszProblem = (const char *)memchr(pszOrg, '=', cchOrg)) != NULL)
116 || ( fWatcomBrainDamage
117 && (pszProblem = (const char *)memchr(pszOrg, '\\', cchOrg)) != NULL)
118 )
119 {
120 char ch;
121 int fComplicated = pszQuotes || (cchOrg > 0 && pszOrg[cchOrg - 1] == '\\');
122 size_t cchNew = fComplicated ? cchOrg * 2 + 2 : cchOrg + 2;
123 char *pszNew = (char *)malloc(cchNew + 1 /*term*/ + 3 /*passthru hack*/);
124
125 argv[i] = pszNew;
126
127 /* Watcom does not grok stuff like "-i=c:\program files\watcom\h",
128 it think it's a source specification. In that case the quote
129 must follow the equal sign. */
130 if (fWatcomBrainDamage)
131 {
132 size_t cchUnquoted = 0;
133 if (pszOrg[0] == '@') /* Response file quoting: @"file name.rsp" */
134 cchUnquoted = 1;
135 else if (pszOrg[0] == '-' || pszOrg[0] == '/') /* Switch quoting. */
136 {
137 if (isWatcomPassThruOption(pszOrg))
138 cchUnquoted = strlen(pszOrg) + 1;
139 else
140 {
141 const char *pszNeedQuoting = (const char *)memchr(pszOrg, '=', cchOrg); /* For -i=dir and similar. */
142 if ( pszNeedQuoting == NULL
143 || (uintptr_t)pszNeedQuoting > (uintptr_t)(pszProblem ? pszProblem : pszQuotes))
144 pszNeedQuoting = pszProblem ? pszProblem : pszQuotes;
145 else
146 pszNeedQuoting++;
147 cchUnquoted = pszNeedQuoting - pszOrg;
148 }
149 }
150 if (cchUnquoted)
151 {
152 memcpy(pszNew, pszOrg, cchUnquoted);
153 pszNew += cchUnquoted;
154 pszOrg += cchUnquoted;
155 cchOrg -= cchUnquoted;
156 }
157 }
158
159 *pszNew++ = '"';
160 if (fComplicated)
161 {
162 while ((ch = *pszOrg++) != '\0')
163 {
164 if (ch == '"')
165 {
166 *pszNew++ = '\\';
167 *pszNew++ = '"';
168 }
169 else if (ch == '\\')
170 {
171 /* Backslashes are a bit complicated, they depends on
172 whether a quotation mark follows them or not. They
173 only require escaping if one does. */
174 unsigned cSlashes = 1;
175 while ((ch = *pszOrg) == '\\')
176 {
177 pszOrg++;
178 cSlashes++;
179 }
180 if (ch == '"' || ch == '\0') /* We put a " at the EOS. */
181 {
182 while (cSlashes-- > 0)
183 {
184 *pszNew++ = '\\';
185 *pszNew++ = '\\';
186 }
187 }
188 else
189 while (cSlashes-- > 0)
190 *pszNew++ = '\\';
191 }
192 else
193 *pszNew++ = ch;
194 }
195 }
196 else
197 {
198 memcpy(pszNew, pszOrg, cchOrg);
199 pszNew += cchOrg;
200 }
201 *pszNew++ = '"';
202 *pszNew = '\0';
203
204 if (fFreeOrLeak)
205 free(pszOrgOrg);
206 }
207 }
208
209 /*for (i = 0; i < argc; i++) fprintf(stderr, "argv[%u]=%s;;\n", i, argv[i]);*/
210}
211
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette