summaryrefslogtreecommitdiff
path: root/elpa/irony-20220110.849/server/src/support/CommandLineParser.cpp
blob: a83809252b6d5966db7930aa9f2304fd7818d474 (plain)
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
/**
 * \file
 * \author Guillaume Papin <guillaume.papin@epitech.eu>
 *
 * This file is distributed under the GNU General Public License. See
 * COPYING for details.
 */

#include "CommandLineParser.h"

namespace {

/// \brief A parser for escaped strings of command line arguments.
///
/// Assumes \-escaping for quoted arguments (see the documentation of
/// unescapeCommandLine(...)).
class CommandLineArgumentParser {
public:
  CommandLineArgumentParser(const std::string &commandLine)
    : input_(commandLine), position_(input_.begin() - 1) {
  }

  std::vector<std::string> parse() {
    bool hasMoreInput = true;
    while (hasMoreInput && nextNonWhitespace()) {
      std::string argument;
      hasMoreInput = parseStringInto(argument);
      commandLine_.push_back(argument);
    }
    return commandLine_;
  }

private:
  // All private methods return true if there is more input available.

  bool parseStringInto(std::string &string) {
    do {
      if (*position_ == '"') {
        if (!parseDoubleQuotedStringInto(string))
          return false;
      } else if (*position_ == '\'') {
        if (!parseSingleQuotedStringInto(string))
          return false;
      } else {
        if (!parseFreeStringInto(string))
          return false;
      }
    } while (*position_ != ' ');
    return true;
  }

  bool parseDoubleQuotedStringInto(std::string &string) {
    if (!next())
      return false;
    while (*position_ != '"') {
      if (!skipEscapeCharacter())
        return false;
      string.push_back(*position_);
      if (!next())
        return false;
    }
    return next();
  }

  bool parseSingleQuotedStringInto(std::string &string) {
    if (!next())
      return false;
    while (*position_ != '\'') {
      string.push_back(*position_);
      if (!next())
        return false;
    }
    return next();
  }

  bool parseFreeStringInto(std::string &string) {
    do {
      if (!skipEscapeCharacter())
        return false;
      string.push_back(*position_);
      if (!next())
        return false;
    } while (*position_ != ' ' && *position_ != '"' && *position_ != '\'');
    return true;
  }

  bool skipEscapeCharacter() {
    if (*position_ == '\\') {
      return next();
    }
    return true;
  }

  bool nextNonWhitespace() {
    do {
      if (!next())
        return false;
    } while (*position_ == ' ');
    return true;
  }

  bool next() {
    ++position_;
    return position_ != input_.end();
  }

private:
  const std::string input_;
  std::string::const_iterator position_;
  std::vector<std::string> commandLine_;
};

} // unnamed namespace

std::vector<std::string>
unescapeCommandLine(const std::string &escapedCommandLine) {
  CommandLineArgumentParser parser(escapedCommandLine);
  return parser.parse();
}