1 : /*
2 : * Copyright 2009 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 :
7 : /*
8 : * Some useful routines to handle command line arguments.
9 : */
10 :
11 : #include <stdlib.h>
12 : #include <string.h>
13 :
14 : #include "native_client/src/shared/utils/flags.h"
15 :
16 : /*
17 : * Given a pointer to a flag name to be
18 : * checked against the pointer to the corresponding
19 : * argument, return true iff the argument begins with
20 : * the flag name. During the match, advance both pointers
21 : * to the maximal matched prefix.
22 : */
23 : static Bool MatchFlagPrefix(const char** name,
24 2 : const char** arg) {
25 2 : while (*(*name)) {
26 2 : if (*(*name) != *(*arg)) {
27 2 : return FALSE;
28 : }
29 2 : ++(*name);
30 2 : ++(*arg);
31 2 : }
32 1 : return TRUE;
33 2 : }
34 :
35 : /*
36 : * Given a pointer to a flag name to be checked
37 : * against the pointer to the corresponding argument, return true
38 : * iff the argument begins with the flag name, followed by
39 : * and equals sign. During the match, advance both pointers
40 : * to the maximal matched prefix.
41 : */
42 : static Bool MatchFlagPrefixEquals(const char** name,
43 2 : const char** arg) {
44 2 : const char* equals = "=";
45 : return MatchFlagPrefix(name, arg) &&
46 2 : MatchFlagPrefix(&equals, arg);
47 2 : }
48 :
49 : Bool GrokBoolFlag(const char* name,
50 : const char* arg,
51 2 : Bool* flag) {
52 2 : if (strcmp(name, arg) == 0) {
53 2 : *flag = TRUE;
54 2 : return TRUE;
55 2 : } else if (MatchFlagPrefixEquals(&name, &arg)) {
56 0 : if (strcmp(arg, "true") == 0) {
57 0 : *flag = TRUE;
58 0 : return TRUE;
59 0 : } else if (strcmp(arg, "false") == 0) {
60 0 : *flag = FALSE;
61 0 : return TRUE;
62 : }
63 : }
64 2 : return FALSE;
65 2 : }
66 :
67 0 : static Bool IsZero(const char* arg) {
68 0 : while (*arg) {
69 0 : if ('0' != *arg) {
70 0 : return FALSE;
71 : }
72 0 : ++arg;
73 0 : }
74 0 : return TRUE;
75 0 : }
76 :
77 : Bool GrokIntFlag(const char* name,
78 : const char* arg,
79 0 : int* flag) {
80 0 : if (MatchFlagPrefixEquals(&name, &arg)) {
81 0 : int value = atoi(arg);
82 : /* verify that arg is all zeros. Otherwise,
83 : * assume that 0 was returned due to an error.
84 : */
85 0 : if (0 == value && !IsZero(arg)) return FALSE;
86 0 : *flag = value;
87 0 : return TRUE;
88 : }
89 0 : return FALSE;
90 0 : }
91 :
92 : Bool GrokUint32HexFlag(const char* name,
93 : const char* arg,
94 1 : uint32_t* flag) {
95 1 : if (MatchFlagPrefixEquals(&name, &arg)) {
96 1 : unsigned long value = strtoul(arg, NULL, 16);
97 : /* Verify that arg is all zeros. Otherwise,
98 : * assume that 0 was returned due to an error.
99 : */
100 1 : if (0L == value && !IsZero(arg)) return FALSE;
101 1 : *flag = (uint32_t) value;
102 1 : return TRUE;
103 : }
104 1 : return FALSE;
105 1 : }
106 :
107 :
108 : Bool GrokCstringFlag(const char* name,
109 : const char* arg,
110 1 : char** flag) {
111 1 : if (MatchFlagPrefixEquals(&name, &arg)) {
112 1 : *flag = (char*) arg;
113 1 : return TRUE;
114 : }
115 1 : return FALSE;
116 1 : }
|