Flutter Engine
The Flutter Engine
options.cc
Go to the documentation of this file.
1// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "bin/options.h"
6
7namespace dart {
8namespace bin {
9
10OptionProcessor* OptionProcessor::first_ = nullptr;
11
13 return name[0] == '-' && name[1] == '-' && name[2] != '\0';
14}
15
17 return name[0] == '-' && name[1] != '\0';
18}
19
20const char* OptionProcessor::ProcessOption(const char* option,
21 const char* name) {
22 const intptr_t length = strlen(name);
23 for (intptr_t i = 0; i < length; i++) {
24 if (option[i] != name[i]) {
25 if ((name[i] == '_') && (option[i] == '-')) {
26 continue;
27 }
28 return nullptr;
29 }
30 }
31 return option + length;
32}
33
34bool OptionProcessor::TryProcess(const char* option,
35 CommandLineOptions* vm_options) {
36 for (OptionProcessor* p = first_; p != nullptr; p = p->next_) {
37 if (p->Process(option, vm_options)) {
38 return true;
39 }
40 }
41 return false;
42}
43
44static bool IsPrefix(const char* prefix, size_t prefix_len, const char* str) {
45 ASSERT(prefix != nullptr);
46 ASSERT(str != nullptr);
47 const size_t str_len = strlen(str);
48 if (str_len < prefix_len) {
49 return false;
50 }
51 return strncmp(prefix, str, prefix_len) == 0;
52}
53
55 const char* arg,
56 CommandLineOptions* vm_options,
58 ASSERT(arg != nullptr);
59 ASSERT(environment != nullptr);
60 const char* kShortPrefix = "-D";
61 const char* kLongPrefix = "--define=";
62 const int kShortPrefixLen = strlen(kShortPrefix);
63 const int kLongPrefixLen = strlen(kLongPrefix);
64 const bool is_short_form = IsPrefix(kShortPrefix, kShortPrefixLen, arg);
65 const bool is_long_form = IsPrefix(kLongPrefix, kLongPrefixLen, arg);
66 if (is_short_form) {
67 arg = arg + kShortPrefixLen;
68 } else if (is_long_form) {
69 arg = arg + kLongPrefixLen;
70 } else {
71 return false;
72 }
73 if (*arg == '\0') {
74 return true;
75 }
76 if (*environment == nullptr) {
78 }
79 // Split the name=value part of the -Dname=value argument.
80 char* name;
81 char* value = nullptr;
82 const char* equals_pos = strchr(arg, '=');
83 if (equals_pos == nullptr) {
84 // No equal sign (name without value) currently not supported.
85 if (is_short_form) {
86 Syslog::PrintErr("No value given to -D option\n");
87 } else {
88 Syslog::PrintErr("No value given to --define option\n");
89 }
90 return true;
91 }
92 int name_len = equals_pos - arg;
93 if (name_len == 0) {
94 if (is_short_form) {
95 Syslog::PrintErr("No name given to -D option\n");
96 } else {
97 Syslog::PrintErr("No name given to --define option\n");
98 }
99 return true;
100 }
101 // Split name=value into name and value.
102 name = reinterpret_cast<char*>(malloc(name_len + 1));
103 strncpy(name, arg, name_len);
104 name[name_len] = '\0';
105 value = Utils::StrDup(equals_pos + 1);
106 SimpleHashMap::Entry* entry =
107 (*environment)
110 ASSERT(entry != nullptr); // Lookup adds an entry if key not found.
111 if (entry->value != nullptr) {
112 free(name);
113 free(entry->value);
114 }
115 entry->value = value;
116 return true;
117}
118
119} // namespace bin
120} // namespace dart
static bool SameStringValue(void *key1, void *key2)
Definition: hashmap.h:41
static uint32_t StringHash(const char *key)
Definition: hashmap.h:26
static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
static char * StrDup(const char *s)
static bool TryProcess(const char *option, CommandLineOptions *options)
Definition: options.cc:34
static bool IsValidShortFlag(const char *name)
Definition: options.cc:16
static const char * ProcessOption(const char *option, const char *name)
Definition: options.cc:20
static bool ProcessEnvironmentOption(const char *arg, CommandLineOptions *vm_options, dart::SimpleHashMap **environment)
Definition: options.cc:54
static bool IsValidFlag(const char *name)
Definition: options.cc:12
#define ASSERT(E)
uint8_t value
size_t length
static void * GetHashmapKeyFromString(char *key)
Definition: dartutils.h:38
static bool IsPrefix(const char *prefix, size_t prefix_len, const char *str)
Definition: options.cc:44
static dart::SimpleHashMap * environment
Definition: gen_snapshot.cc:59
Definition: dart_vm.cc:33
const char *const name
void * malloc(size_t size)
Definition: allocation.cc:19