1 /*
2  *  Copyright (c) 2022, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must strain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <errno.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <sys/select.h>
33 #include <unistd.h>
34 
35 #include "cli.hpp"
36 
37 static ot::Fct::Cli sCli;
38 
main(int argc,char * argv[])39 int main(int argc, char *argv[])
40 {
41     if (argc >= 2)
42     {
43         const int                     kMaxArgs = 20;
44         ot::Utils::CmdLineParser::Arg args[kMaxArgs + 1];
45 
46         if (argc - 1 > kMaxArgs)
47         {
48             fprintf(stderr, "Too many arguments!\r\n");
49             exit(EXIT_FAILURE);
50         }
51 
52         for (int i = 0; i < argc - 1; i++)
53         {
54             args[i].SetCString(argv[i + 1]);
55         }
56         args[argc - 1].Clear();
57 
58         sCli.ProcessCommand(args);
59     }
60     else
61     {
62         fd_set rset;
63         int    maxFd;
64         int    ret;
65 
66         sCli.OutputPrompt();
67 
68         while (true)
69         {
70             FD_ZERO(&rset);
71             FD_SET(STDIN_FILENO, &rset);
72             maxFd = STDIN_FILENO + 1;
73 
74             ret = select(maxFd, &rset, nullptr, nullptr, nullptr);
75 
76             if ((ret == -1) && (errno != EINTR))
77             {
78                 fprintf(stderr, "select: %s\n", strerror(errno));
79                 break;
80             }
81             else if (ret > 0)
82             {
83                 if (FD_ISSET(STDIN_FILENO, &rset))
84                 {
85                     const int kBufferSize = 512;
86                     char      buffer[kBufferSize];
87 
88                     if (fgets(buffer, sizeof(buffer), stdin) != nullptr)
89                     {
90                         sCli.ProcessLine(buffer);
91                     }
92                     else
93                     {
94                         break;
95                     }
96                 }
97             }
98         }
99     }
100 
101     return EXIT_SUCCESS;
102 }
103