| 1 | /* $Id: quote_argv.c 2912 2016-09-14 13:36:15Z 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 | */
|
|---|
| 54 | static 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 | * @returns 0 on success, -1 if out of memory.
|
|---|
| 83 | * @param argc The argument count.
|
|---|
| 84 | * @param argv The argument vector.
|
|---|
| 85 | * @param fWatcomBrainDamage Set if we're catering for wcc, wcc386 or similar
|
|---|
| 86 | * OpenWatcom tools. They seem to follow some
|
|---|
| 87 | * ancient or home made quoting convention.
|
|---|
| 88 | * @param fFreeOrLeak Whether to free replaced argv members
|
|---|
| 89 | * (non-zero), or just leak them (zero). This
|
|---|
| 90 | * depends on which argv you're working on.
|
|---|
| 91 | * Suggest doing the latter if it's main()'s argv.
|
|---|
| 92 | */
|
|---|
| 93 | int quote_argv(int argc, char **argv, int fWatcomBrainDamage, int fFreeOrLeak)
|
|---|
| 94 | {
|
|---|
| 95 | int i;
|
|---|
| 96 | for (i = 0; i < argc; i++)
|
|---|
| 97 | {
|
|---|
| 98 | char *const pszOrgOrg = argv[i];
|
|---|
| 99 | const char *pszOrg = pszOrgOrg;
|
|---|
| 100 | size_t cchOrg = strlen(pszOrg);
|
|---|
| 101 | const char *pszQuotes = (const char *)memchr(pszOrg, '"', cchOrg);
|
|---|
| 102 | const char *pszProblem = NULL;
|
|---|
| 103 | if ( pszQuotes
|
|---|
| 104 | || cchOrg == 0
|
|---|
| 105 | || (pszProblem = (const char *)memchr(pszOrg, ' ', cchOrg)) != NULL
|
|---|
| 106 | || (pszProblem = (const char *)memchr(pszOrg, '\t', cchOrg)) != NULL
|
|---|
| 107 | || (pszProblem = (const char *)memchr(pszOrg, '\n', cchOrg)) != NULL
|
|---|
| 108 | || (pszProblem = (const char *)memchr(pszOrg, '\r', 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 | || (pszProblem = (const char *)memchr(pszOrg, '\'', cchOrg)) != NULL
|
|---|
| 115 | || ( !fWatcomBrainDamage
|
|---|
| 116 | && (pszProblem = (const char *)memchr(pszOrg, '=', cchOrg)) != NULL)
|
|---|
| 117 | || ( fWatcomBrainDamage
|
|---|
| 118 | && (pszProblem = (const char *)memchr(pszOrg, '\\', cchOrg)) != NULL)
|
|---|
| 119 | )
|
|---|
| 120 | {
|
|---|
| 121 | char ch;
|
|---|
| 122 | int fComplicated = pszQuotes || (cchOrg > 0 && pszOrg[cchOrg - 1] == '\\');
|
|---|
| 123 | size_t cchNew = fComplicated ? cchOrg * 2 + 2 : cchOrg + 2;
|
|---|
| 124 | char *pszNew = (char *)malloc(cchNew + 1 /*term*/ + 3 /*passthru hack*/);
|
|---|
| 125 | if (!pszNew)
|
|---|
| 126 | return -1;
|
|---|
| 127 |
|
|---|
| 128 | argv[i] = pszNew;
|
|---|
| 129 |
|
|---|
| 130 | /* Watcom does not grok stuff like "-i=c:\program files\watcom\h",
|
|---|
| 131 | it think it's a source specification. In that case the quote
|
|---|
| 132 | must follow the equal sign. */
|
|---|
| 133 | if (fWatcomBrainDamage)
|
|---|
| 134 | {
|
|---|
| 135 | size_t cchUnquoted = 0;
|
|---|
| 136 | if (pszOrg[0] == '@') /* Response file quoting: @"file name.rsp" */
|
|---|
| 137 | cchUnquoted = 1;
|
|---|
| 138 | else if (pszOrg[0] == '-' || pszOrg[0] == '/') /* Switch quoting. */
|
|---|
| 139 | {
|
|---|
| 140 | if (isWatcomPassThruOption(pszOrg))
|
|---|
| 141 | cchUnquoted = strlen(pszOrg) + 1;
|
|---|
| 142 | else
|
|---|
| 143 | {
|
|---|
| 144 | const char *pszNeedQuoting = (const char *)memchr(pszOrg, '=', cchOrg); /* For -i=dir and similar. */
|
|---|
| 145 | if ( pszNeedQuoting == NULL
|
|---|
| 146 | || (uintptr_t)pszNeedQuoting > (uintptr_t)(pszProblem ? pszProblem : pszQuotes))
|
|---|
| 147 | pszNeedQuoting = pszProblem ? pszProblem : pszQuotes;
|
|---|
| 148 | else
|
|---|
| 149 | pszNeedQuoting++;
|
|---|
| 150 | cchUnquoted = pszNeedQuoting - pszOrg;
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 | if (cchUnquoted)
|
|---|
| 154 | {
|
|---|
| 155 | memcpy(pszNew, pszOrg, cchUnquoted);
|
|---|
| 156 | pszNew += cchUnquoted;
|
|---|
| 157 | pszOrg += cchUnquoted;
|
|---|
| 158 | cchOrg -= cchUnquoted;
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | *pszNew++ = '"';
|
|---|
| 163 | if (fComplicated)
|
|---|
| 164 | {
|
|---|
| 165 | while ((ch = *pszOrg++) != '\0')
|
|---|
| 166 | {
|
|---|
| 167 | if (ch == '"')
|
|---|
| 168 | {
|
|---|
| 169 | *pszNew++ = '\\';
|
|---|
| 170 | *pszNew++ = '"';
|
|---|
| 171 | }
|
|---|
| 172 | else if (ch == '\\')
|
|---|
| 173 | {
|
|---|
| 174 | /* Backslashes are a bit complicated, they depends on
|
|---|
| 175 | whether a quotation mark follows them or not. They
|
|---|
| 176 | only require escaping if one does. */
|
|---|
| 177 | unsigned cSlashes = 1;
|
|---|
| 178 | while ((ch = *pszOrg) == '\\')
|
|---|
| 179 | {
|
|---|
| 180 | pszOrg++;
|
|---|
| 181 | cSlashes++;
|
|---|
| 182 | }
|
|---|
| 183 | if (ch == '"' || ch == '\0') /* We put a " at the EOS. */
|
|---|
| 184 | {
|
|---|
| 185 | while (cSlashes-- > 0)
|
|---|
| 186 | {
|
|---|
| 187 | *pszNew++ = '\\';
|
|---|
| 188 | *pszNew++ = '\\';
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 | else
|
|---|
| 192 | while (cSlashes-- > 0)
|
|---|
| 193 | *pszNew++ = '\\';
|
|---|
| 194 | }
|
|---|
| 195 | else
|
|---|
| 196 | *pszNew++ = ch;
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 | else
|
|---|
| 200 | {
|
|---|
| 201 | memcpy(pszNew, pszOrg, cchOrg);
|
|---|
| 202 | pszNew += cchOrg;
|
|---|
| 203 | }
|
|---|
| 204 | *pszNew++ = '"';
|
|---|
| 205 | *pszNew = '\0';
|
|---|
| 206 |
|
|---|
| 207 | if (fFreeOrLeak)
|
|---|
| 208 | free(pszOrgOrg);
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | /*for (i = 0; i < argc; i++) fprintf(stderr, "argv[%u]=%s;;\n", i, argv[i]);*/
|
|---|
| 213 | return 0;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|