1 /*
2  * Copyright (c) 2014, 2021, Oracle and/or its affiliates.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License, version 2.0,
6  * as published by the Free Software Foundation.
7  *
8  * This program is also distributed with certain software (including
9  * but not limited to OpenSSL) that is licensed under separate terms,
10  * as designated in a particular file or component or in included license
11  * documentation.  The authors of MySQL hereby grant you an additional
12  * permission to link the program and your derivative works with the
13  * separately licensed software that they have included with MySQL.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License, version 2.0, for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301  USA
24  */
25 
26 #ifndef _CMDLINE_OPTIONS_H_
27 #define _CMDLINE_OPTIONS_H_
28 
29 #include <stdlib.h>
30 #include <iostream>
31 
32 class Command_line_options
33 {
34 public:
35   int exit_code;
36   bool needs_password;
37 protected:
Command_line_options(int argc,char ** argv)38   Command_line_options(int argc, char **argv)
39           : exit_code(0)
40   {
41   }
42 
check_arg(char ** argv,int & argi,const char * arg,const char * larg)43   bool check_arg(char **argv, int &argi, const char *arg, const char *larg)
44   {
45     if ((arg && strcmp(argv[argi], arg) == 0) || (larg && strcmp(argv[argi], larg) == 0))
46       return true;
47     return false;
48   }
49 
is_quote_char(const char single_char)50   bool is_quote_char(const char single_char)
51   {
52     if (single_char == '\'' || single_char == '"' || single_char == '`')
53       return true;
54 
55     return false;
56   }
57 
should_remove_qoutes(const char first,const char last)58   bool should_remove_qoutes(const char first, const char last)
59   {
60     if (!is_quote_char(first) || !is_quote_char(last))
61       return false;
62 
63     return first == last;
64   }
65 
check_arg_with_value(char ** argv,int & argi,const char * arg,const char * larg,char * & value)66   bool check_arg_with_value(char **argv, int &argi, const char *arg, const char *larg, char *&value)
67   {
68     // --option value or -o value
69     if ((arg && strcmp(argv[argi], arg) == 0) || (larg && strcmp(argv[argi], larg) == 0))
70     {
71       // value must be in next arg
72       if (argv[argi+1] != NULL)
73       {
74         ++argi;
75         value = argv[argi];
76       }
77       else
78       {
79         std::cerr << argv[0] << ": option " << argv[argi] << " requires an argument\n";
80         exit_code = 1;
81         return false;
82       }
83       return true;
84     }
85     // -ovalue
86     else if (larg && strncmp(argv[argi], larg, strlen(larg)) == 0 && strlen(argv[argi]) > strlen(larg))
87     {
88       value = argv[argi] + strlen(larg);
89       std::size_t length = strlen(value);
90 
91       if (length > 0 && should_remove_qoutes(value[0], value[length - 1]))
92       {
93         value[length - 1] = 0;
94         value = value + 1;
95       }
96 
97       return true;
98     }
99     // --option=value
100     else if (arg && strncmp(argv[argi], arg, strlen(arg)) == 0 && argv[argi][strlen(arg)] == '=')
101     {
102       // value must be after =
103       value = argv[argi] + strlen(arg)+1;
104       std::size_t length = strlen(value);
105 
106       if (length > 0 && should_remove_qoutes(value[0], value[length - 1]))
107       {
108         value[length - 1] = 0;
109         value = value + 1;
110       }
111 
112       return true;
113     }
114     return false;
115   }
116 };
117 
118 
119 #endif
120