• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "Command.h"
18 
19 #include <iomanip>
20 #include <iostream>
21 #include <string>
22 #include <vector>
23 
24 #include "android-base/stringprintf.h"
25 #include "android-base/utf8.h"
26 #include "androidfw/StringPiece.h"
27 
28 #include "trace/TraceBuffer.h"
29 #include "util/Util.h"
30 
31 using android::base::StringPrintf;
32 using android::StringPiece;
33 
34 namespace aapt {
35 
GetSafePath(StringPiece arg)36 std::string GetSafePath(StringPiece arg) {
37 #ifdef _WIN32
38   // If the path exceeds the maximum path length for Windows, encode the path using the
39   // extended-length prefix
40   std::wstring path16;
41   CHECK(android::base::UTF8PathToWindowsLongPath(arg.data(), &path16))
42       << "Failed to convert file path to UTF-16: file path " << arg.data();
43 
44   std::string path8;
45   CHECK(android::base::WideToUTF8(path16, &path8))
46       << "Failed to convert file path back to UTF-8: file path " << arg.data();
47 
48   return path8;
49 #else
50   return std::string(arg);
51 #endif
52 }
53 
AddRequiredFlag(StringPiece name,StringPiece description,std::string * value,uint32_t flags)54 void Command::AddRequiredFlag(StringPiece name, StringPiece description, std::string* value,
55                               uint32_t flags) {
56   auto func = [value, flags](StringPiece arg) -> bool {
57     *value = (flags & Command::kPath) ? GetSafePath(arg) : std::string(arg);
58     return true;
59   };
60 
61   flags_.emplace_back(Flag(name, description, /* required */ true, /* num_args */ 1, func));
62 }
63 
AddRequiredFlagList(StringPiece name,StringPiece description,std::vector<std::string> * value,uint32_t flags)64 void Command::AddRequiredFlagList(StringPiece name, StringPiece description,
65                                   std::vector<std::string>* value, uint32_t flags) {
66   auto func = [value, flags](StringPiece arg) -> bool {
67     value->push_back((flags & Command::kPath) ? GetSafePath(arg) : std::string(arg));
68     return true;
69   };
70 
71   flags_.emplace_back(Flag(name, description, /* required */ true, /* num_args */ 1, func));
72 }
73 
AddOptionalFlag(StringPiece name,StringPiece description,std::optional<std::string> * value,uint32_t flags)74 void Command::AddOptionalFlag(StringPiece name, StringPiece description,
75                               std::optional<std::string>* value, uint32_t flags) {
76   auto func = [value, flags](StringPiece arg) -> bool {
77     *value = (flags & Command::kPath) ? GetSafePath(arg) : std::string(arg);
78     return true;
79   };
80 
81   flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 1, func));
82 }
83 
AddOptionalFlagList(StringPiece name,StringPiece description,std::vector<std::string> * value,uint32_t flags)84 void Command::AddOptionalFlagList(StringPiece name, StringPiece description,
85                                   std::vector<std::string>* value, uint32_t flags) {
86   auto func = [value, flags](StringPiece arg) -> bool {
87     value->push_back((flags & Command::kPath) ? GetSafePath(arg) : std::string(arg));
88     return true;
89   };
90 
91   flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 1, func));
92 }
93 
AddOptionalFlagList(StringPiece name,StringPiece description,std::unordered_set<std::string> * value)94 void Command::AddOptionalFlagList(StringPiece name, StringPiece description,
95                                   std::unordered_set<std::string>* value) {
96   auto func = [value](StringPiece arg) -> bool {
97     value->emplace(arg);
98     return true;
99   };
100 
101   flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 1, func));
102 }
103 
AddOptionalSwitch(StringPiece name,StringPiece description,bool * value)104 void Command::AddOptionalSwitch(StringPiece name, StringPiece description, bool* value) {
105   auto func = [value](StringPiece arg) -> bool {
106     *value = true;
107     return true;
108   };
109 
110   flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 0, func));
111 }
112 
AddOptionalSubcommand(std::unique_ptr<Command> && subcommand,bool experimental)113 void Command::AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand, bool experimental) {
114   subcommand->full_subcommand_name_ = StringPrintf("%s %s", name_.data(), subcommand->name_.data());
115   if (experimental) {
116     experimental_subcommands_.push_back(std::move(subcommand));
117   } else {
118     subcommands_.push_back(std::move(subcommand));
119   }
120 }
121 
SetDescription(StringPiece description)122 void Command::SetDescription(StringPiece description) {
123   description_ = std::string(description);
124 }
125 
Usage(std::ostream * out)126 void Command::Usage(std::ostream* out) {
127   constexpr size_t kWidth = 50;
128 
129   *out << full_subcommand_name_;
130 
131   if (!subcommands_.empty()) {
132     *out << " [subcommand]";
133   }
134 
135   *out << " [options]";
136   for (const Flag& flag : flags_) {
137     if (flag.is_required) {
138       *out << " " << flag.name << " arg";
139     }
140   }
141 
142   *out << " files...\n";
143 
144   if (!subcommands_.empty()) {
145     *out << "\nSubcommands:\n";
146     for (auto& subcommand : subcommands_) {
147       std::string argline = subcommand->name_;
148 
149       // Split the description by newlines and write out the argument (which is
150       // empty after the first line) followed by the description line. This will make sure
151       // that multiline descriptions are still right justified and aligned.
152       for (StringPiece line : util::Tokenize(subcommand->description_, '\n')) {
153         *out << " " << std::setw(kWidth) << std::left << argline << line << "\n";
154         argline = " ";
155       }
156     }
157   }
158 
159   *out << "\nOptions:\n";
160 
161   for (const Flag& flag : flags_) {
162     std::string argline = flag.name;
163     if (flag.num_args > 0) {
164       argline += " arg";
165     }
166 
167     // Split the description by newlines and write out the argument (which is
168     // empty after the first line) followed by the description line. This will make sure
169     // that multiline descriptions are still right justified and aligned.
170     for (StringPiece line : util::Tokenize(flag.description, '\n')) {
171       *out << " " << std::setw(kWidth) << std::left << argline << line << "\n";
172       argline = " ";
173     }
174   }
175   *out << " " << std::setw(kWidth) << std::left << "-h"
176        << "Displays this help menu\n";
177   out->flush();
178 }
179 
Execute(const std::vector<StringPiece> & args,std::ostream * out_error)180 int Command::Execute(const std::vector<StringPiece>& args, std::ostream* out_error) {
181   TRACE_NAME_ARGS("Command::Execute", args);
182   std::vector<std::string> file_args;
183 
184   for (size_t i = 0; i < args.size(); i++) {
185     StringPiece arg = args[i];
186     if (*(arg.data()) != '-') {
187       // Continue parsing as the subcommand if the first argument matches one of the subcommands
188       if (i == 0) {
189         for (auto& subcommand : subcommands_) {
190           if (arg == subcommand->name_ || (!subcommand->short_name_.empty()
191                                            && arg == subcommand->short_name_)) {
192             return subcommand->Execute(
193                 std::vector<StringPiece>(args.begin() + 1, args.end()), out_error);
194           }
195         }
196         for (auto& subcommand : experimental_subcommands_) {
197           if (arg == subcommand->name_ || (!subcommand->short_name_.empty()
198                                            && arg == subcommand->short_name_)) {
199             return subcommand->Execute(
200               std::vector<StringPiece>(args.begin() + 1, args.end()), out_error);
201           }
202         }
203       }
204 
205       file_args.push_back(GetSafePath(arg));
206       continue;
207     }
208 
209     if (arg == "-h" || arg == "--help") {
210       Usage(out_error);
211       return 1;
212     }
213 
214     bool match = false;
215     for (Flag& flag : flags_) {
216       if (arg == flag.name) {
217         if (flag.num_args > 0) {
218           i++;
219           if (i >= args.size()) {
220             *out_error << flag.name << " missing argument.\n\n";
221             Usage(out_error);
222             return false;
223           }
224           flag.action(args[i]);
225         } else {
226           flag.action({});
227         }
228         flag.found = true;
229         match = true;
230         break;
231       }
232     }
233 
234     if (!match) {
235       *out_error << "unknown option '" << arg << "'.\n\n";
236       Usage(out_error);
237       return 1;
238     }
239   }
240 
241   for (const Flag& flag : flags_) {
242     if (flag.is_required && !flag.found) {
243       *out_error << "missing required flag " << flag.name << "\n\n";
244       Usage(out_error);
245       return 1;
246     }
247   }
248 
249   return Action(file_args);
250 }
251 
252 }  // namespace aapt
253