• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #ifndef INCLUDE_PERFETTO_EXT_BASE_GETOPT_COMPAT_H_
18 #define INCLUDE_PERFETTO_EXT_BASE_GETOPT_COMPAT_H_
19 
20 #include <cstddef>  // For std::nullptr_t
21 
22 // No translation units other than base/getopt.h and getopt_compat_unittest.cc
23 // should directly include this file. Use base/getopt.h instead.
24 
25 namespace perfetto {
26 namespace base {
27 namespace getopt_compat {
28 
29 // A tiny getopt() replacement for Windows, which doesn't have <getopt.h>.
30 // This implementation is based on the subset of features that we use in the
31 // Perfetto codebase. It doesn't even try to deal with the full surface of GNU's
32 // getopt().
33 // Limitations:
34 // - getopt_long_only() is not supported.
35 // - optional_argument is not supported. That is extremely subtle and caused us
36 //   problems in the past with GNU's getopt.
37 // - It does not reorder non-option arguments. It behaves like MacOS getopt, or
38 //   GNU's when POSIXLY_CORRECT=1.
39 // - Doesn't expose optopt or opterr.
40 // - option.flag and longindex are not supported and must be nullptr.
41 
42 enum {
43   no_argument = 0,
44   required_argument = 1,
45 };
46 
47 struct option {
48   const char* name;
49   int has_arg;
50   std::nullptr_t flag;  // Only nullptr is supported.
51   int val;
52 };
53 
54 extern char* optarg;
55 extern int optind;
56 
57 int getopt_long(int argc,
58                 char** argv,
59                 const char* shortopts,
60                 const option* longopts,
61                 std::nullptr_t /*longindex is not supported*/);
62 
63 int getopt(int argc, char** argv, const char* shortopts);
64 
65 }  // namespace getopt_compat
66 }  // namespace base
67 }  // namespace perfetto
68 
69 #endif  // INCLUDE_PERFETTO_EXT_BASE_GETOPT_COMPAT_H_
70