DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (6863f516ba38)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifndef mozilla_AssembleCmdLine_h
#define mozilla_AssembleCmdLine_h

#if defined(XP_WIN)

#  include "mozilla/UniquePtr.h"

#  include <stdlib.h>
#  include <windows.h>

#  ifdef MOZILLA_INTERNAL_API
#    include "nsString.h"
#  endif  // MOZILLA_INTERNAL_API

namespace mozilla {

// Out param `aWideCmdLine` must be free()d by the caller.
inline int assembleCmdLine(const char* const* aArgv, wchar_t** aWideCmdLine,
                           UINT aCodePage) {
  const char* const* arg;
  char* p;
  const char* q;
  char* cmdLine;
  int cmdLineSize;
  int numBackslashes;
  int i;
  int argNeedQuotes;

  /*
   * Find out how large the command line buffer should be.
   */
  cmdLineSize = 0;
  for (arg = aArgv; *arg; ++arg) {
    /*
     * \ and " need to be escaped by a \.  In the worst case,
     * every character is a \ or ", so the string of length
     * may double.  If we quote an argument, that needs two ".
     * Finally, we need a space between arguments, and
     * a null byte at the end of command line.
     */
    cmdLineSize += 2 * strlen(*arg) /* \ and " need to be escaped */
                   + 2              /* we quote every argument */
                   + 1;             /* space in between, or final null */
  }
  p = cmdLine = (char*)malloc(cmdLineSize * sizeof(char));
  if (!p) {
    return -1;
  }

  for (arg = aArgv; *arg; ++arg) {
    /* Add a space to separates the arguments */
    if (arg != aArgv) {
      *p++ = ' ';
    }
    q = *arg;
    numBackslashes = 0;
    argNeedQuotes = 0;

    /* If the argument contains white space, it needs to be quoted. */
    if (strpbrk(*arg, " \f\n\r\t\v")) {
      argNeedQuotes = 1;
    }

    if (argNeedQuotes) {
      *p++ = '"';
    }
    while (*q) {
      if (*q == '\\') {
        numBackslashes++;
        q++;
      } else if (*q == '"') {
        if (numBackslashes) {
          /*
           * Double the backslashes since they are followed
           * by a quote
           */
          for (i = 0; i < 2 * numBackslashes; i++) {
            *p++ = '\\';
          }
          numBackslashes = 0;
        }
        /* To escape the quote */
        *p++ = '\\';
        *p++ = *q++;
      } else {
        if (numBackslashes) {
          /*
           * Backslashes are not followed by a quote, so
           * don't need to double the backslashes.
           */
          for (i = 0; i < numBackslashes; i++) {
            *p++ = '\\';
          }
          numBackslashes = 0;
        }
        *p++ = *q++;
      }
    }

    /* Now we are at the end of this argument */
    if (numBackslashes) {
      /*
       * Double the backslashes if we have a quote string
       * delimiter at the end.
       */
      if (argNeedQuotes) {
        numBackslashes *= 2;
      }
      for (i = 0; i < numBackslashes; i++) {
        *p++ = '\\';
      }
    }
    if (argNeedQuotes) {
      *p++ = '"';
    }
  }

  *p = '\0';
  int numChars = MultiByteToWideChar(aCodePage, 0, cmdLine, -1, nullptr, 0);
  *aWideCmdLine = (wchar_t*)malloc(numChars * sizeof(wchar_t));
  MultiByteToWideChar(aCodePage, 0, cmdLine, -1, *aWideCmdLine, numChars);
  free(cmdLine);
  return 0;
}

#  ifdef MOZILLA_INTERNAL_API
inline UniquePtr<wchar_t[]> assembleSingleArgument(const nsString& aArg) {
  static_assert(sizeof(char16_t) == sizeof(wchar_t),
                "char16_t and wchar_t sizes differ");

  /*
   * \ and " need to be escaped by a \.  In the worst case,
   * every character is a \ or ", so the string of length
   * may double.  If we quote an argument, that needs two ".
   * Finally, we need a space between arguments, and
   * a null byte at the end of command line.
   */
  int cmdLineSize = 2 * aArg.Length() /* \ and " need to be escaped */
                    + 2               /* we quote every argument */
                    + 1;              /* space in between, or final null */

  auto assembledArg = MakeUnique<wchar_t[]>(cmdLineSize);
  if (!assembledArg) {
    return nullptr;
  }

  /* If the argument contains white space, it needs to be quoted. */
  int argNeedQuotes = 0;
  if (aArg.FindCharInSet(u" \f\n\r\t\v") != kNotFound) {
    argNeedQuotes = 1;
  }

  wchar_t* p = assembledArg.get();

  if (argNeedQuotes) {
    *p++ = '"';
  }

  const char16_t* q = aArg.get();

  int numBackslashes = 0;
  while (*q) {
    if (*q == '\\') {
      numBackslashes++;
      q++;
    } else if (*q == '"') {
      if (numBackslashes) {
        /*
         * Double the backslashes since they are followed
         * by a quote
         */
        for (int i = 0; i < 2 * numBackslashes; i++) {
          *p++ = '\\';
        }
        numBackslashes = 0;
      }
      /* To escape the quote */
      *p++ = '\\';
      *p++ = *q++;
    } else {
      if (numBackslashes) {
        /*
         * Backslashes are not followed by a quote, so
         * don't need to double the backslashes.
         */
        for (int i = 0; i < numBackslashes; i++) {
          *p++ = '\\';
        }
        numBackslashes = 0;
      }
      *p++ = *q++;
    }
  }

  /* Now we are at the end of this argument */
  if (numBackslashes) {
    /*
     * Double the backslashes if we have a quote string
     * delimiter at the end.
     */
    if (argNeedQuotes) {
      numBackslashes *= 2;
    }
    for (int i = 0; i < numBackslashes; i++) {
      *p++ = '\\';
    }
  }
  if (argNeedQuotes) {
    *p++ = '"';
  }

  *p = '\0';

  return assembledArg;
}
#  endif  // MOZILLA_INTERNAL_API

}  // namespace mozilla

#endif  // defined(XP_WIN)

#endif  // mozilla_AssembleCmdLine_h