Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / qrcodetool / qrcodetool.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 #include <support/logging/CHIPLogging.h>
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include "qrcodetool_command_manager.h"
25
26 static int match_command(const char * command_name, const char * name)
27 {
28     return !strncmp(command_name, name, strlen(name));
29 }
30
31 static int help(int argc, char ** argv)
32 {
33     qrcodetool_command_t * cmd = nullptr;
34     for (cmd = commands; cmd->c_name != nullptr; cmd++)
35     {
36         ChipLogDetail(chipTool, "%s\t%s\n", cmd->c_name, cmd->c_help);
37     }
38     return 0;
39 }
40
41 static int usage(const char * prog_name)
42 {
43     ChipLogDetail(chipTool,
44                   "Usage: %s [-h] [command] [opt ...]\n"
45                   "%s commands are:\n",
46                   prog_name, prog_name);
47     help(0, nullptr);
48     return 2;
49 }
50
51 static int execute_command(int argc, char ** argv)
52 {
53     if (argc == 0)
54     {
55         return -1;
56     }
57     const qrcodetool_command_t * command_to_execute = nullptr;
58     bool found                                      = false;
59
60     for (command_to_execute = commands; command_to_execute->c_name; command_to_execute++)
61     {
62         if (match_command(command_to_execute->c_name, argv[0]))
63         {
64             found = true;
65             break;
66         }
67     }
68
69     if (found)
70     {
71         ChipLogDetail(chipTool, "Executing cmd %s\n", command_to_execute->c_name);
72         return command_to_execute->c_func(argc, argv);
73     }
74
75     return help(0, nullptr);
76 }
77
78 int main(int argc, char ** argv)
79 {
80     int result  = 0;
81     int do_help = 0;
82     int ch;
83
84     /* Remember my name. */
85     char * prog_name = strrchr(argv[0], '/');
86     prog_name        = prog_name ? prog_name + 1 : argv[0];
87     /* Do getopt stuff for global options. */
88     optind = 1;
89
90     while ((ch = getopt(argc, argv, "h")) != -1)
91     {
92         switch (ch)
93         {
94         case 'h':
95             do_help = 1;
96             break;
97
98         case '?':
99         default:
100             return usage(prog_name);
101         }
102     }
103
104     argc -= optind;
105     argv += optind;
106
107     if (do_help)
108     {
109         /* Munge argc/argv so that argv[0] is something. */
110         result = help(0, nullptr);
111     }
112     else if (argc > 0)
113     {
114         result = execute_command(argc, argv);
115     }
116     else
117     {
118         result = usage(prog_name);
119     }
120     return result;
121 }