VirtualBox

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

Last change on this file since 2861 was 2851, checked in by bird, 9 years ago

updates

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1/* $Id: quote_argv.c 2851 2016-08-31 17:30:52Z bird $ */
2/** @file
3 * quote_argv - Correctly quote argv for spawn, windows specific.
4 */
5
6/*
7 * Copyright (c) 2007-2016 knut st. osmundsen <[email protected]>
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 )
117 {
118 char ch;
119 int fComplicated = pszQuotes || (cchOrg > 0 && pszOrg[cchOrg - 1] == '\\');
120 size_t cchNew = fComplicated ? cchOrg * 2 + 2 : cchOrg + 2;
121 char *pszNew = (char *)malloc(cchNew + 1 /*term*/ + 3 /*passthru hack*/);
122
123 argv[i] = pszNew;
124
125 /* Watcom does not grok stuff like "-i=c:\program files\watcom\h",
126 it think it's a source specification. In that case the quote
127 must follow the equal sign. */
128 if (fWatcomBrainDamage)
129 {
130 size_t cchUnquoted = 0;
131 if (pszOrg[0] == '@') /* Response file quoting: @"file name.rsp" */
132 cchUnquoted = 1;
133 else if (pszOrg[0] == '-' || pszOrg[0] == '/') /* Switch quoting. */
134 {
135 if (isWatcomPassThruOption(pszOrg))
136 cchUnquoted = strlen(pszOrg) + 1;
137 else
138 {
139 const char *pszNeedQuoting = (const char *)memchr(pszOrg, '=', cchOrg); /* For -i=dir and similar. */
140 if ( pszNeedQuoting == NULL
141 || (uintptr_t)pszNeedQuoting > (uintptr_t)(pszProblem ? pszProblem : pszQuotes))
142 pszNeedQuoting = pszProblem ? pszProblem : pszQuotes;
143 else
144 pszNeedQuoting++;
145 cchUnquoted = pszNeedQuoting - pszOrg;
146 }
147 }
148 if (cchUnquoted)
149 {
150 memcpy(pszNew, pszOrg, cchUnquoted);
151 pszNew += cchUnquoted;
152 pszOrg += cchUnquoted;
153 cchOrg -= cchUnquoted;
154 }
155 }
156
157 *pszNew++ = '"';
158 if (fComplicated)
159 {
160 while ((ch = *pszOrg++) != '\0')
161 {
162 if (ch == '"')
163 {
164 *pszNew++ = '\\';
165 *pszNew++ = '"';
166 }
167 else if (ch == '\\')
168 {
169 /* Backslashes are a bit complicated, they depends on
170 whether a quotation mark follows them or not. They
171 only require escaping if one does. */
172 unsigned cSlashes = 1;
173 while ((ch = *pszOrg) == '\\')
174 {
175 pszOrg++;
176 cSlashes++;
177 }
178 if (ch == '"' || ch == '\0') /* We put a " at the EOS. */
179 {
180 while (cSlashes-- > 0)
181 {
182 *pszNew++ = '\\';
183 *pszNew++ = '\\';
184 }
185 }
186 else
187 while (cSlashes-- > 0)
188 *pszNew++ = '\\';
189 }
190 else
191 *pszNew++ = ch;
192 }
193 }
194 else
195 {
196 memcpy(pszNew, pszOrg, cchOrg);
197 pszNew += cchOrg;
198 }
199 *pszNew++ = '"';
200 *pszNew = '\0';
201
202 if (fFreeOrLeak)
203 free(pszOrgOrg);
204 }
205 }
206
207 /*for (i = 0; i < argc; i++) fprintf(stderr, "argv[%u]=%s;;\n", i, argv[i]);*/
208}
209
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