1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 
22     /// \file line_tools.hpp
23     /// \brief a set of general command line targeted routines
24     /// \ingroup CMDLINE
25 
26 #ifndef LINE_TOOLS_HPP
27 #define LINE_TOOLS_HPP
28 
29 #include "../my_config.h"
30 
31 #include <string>
32 #include <vector>
33 #include "infinint.hpp"
34 #include "tlv_list.hpp"
35 #include "integers.hpp"
36 #include "criterium.hpp"
37 
38 using namespace libdar;
39 
40     /// \addtogroup CMDLINE
41     /// @{
42 
43 class argc_argv
44 {
45 public:
46     argc_argv(S_I size = 0);
argc_argv(const argc_argv & ref)47     argc_argv(const argc_argv & ref) { throw Efeature("argc_argv"); };
operator =(const argc_argv & ref)48     const argc_argv & operator = (const argc_argv & ref) { throw Efeature("argc_argv"); };
49     ~argc_argv() throw(Ebug);
50 
51     void resize(S_I size);
52     void set_arg(const std::string & arg, S_I index);
53     void set_arg(generic_file & f, U_I size, S_I index);
54 
argc() const55     S_I argc() const { return x_argc; };
argv() const56     char* const * argv() const { return x_argv; }; // well, the const method is a bit silly, as the caller has the possibility to modify what is pointed to by the returned value...
57 
58 private:
59     S_I x_argc;
60     char **x_argv;
61 };
62 
63 extern void line_tools_slice_ownership(const std::string & cmd, std::string & slice_permission, std::string & slice_user_ownership, std::string & slice_group_ownership);
64 extern void line_tools_repeat_param(const std::string & cmd, infinint & repeat_count, infinint & repeat_byte);
65 extern void line_tools_tlv_list2argv(user_interaction & dialog, tlv_list & list, argc_argv & arg);
66 
67     /// returns the old position of parsing (next argument to parse)
68 extern S_I line_tools_reset_getopt();
69 
70 
71 std::string::const_iterator line_tools_find_first_char_out_of_parenth(const std::string & argument, unsigned char to_find);
72 std::string::const_iterator line_tools_find_last_char_out_of_parenth(const std::string & argument, unsigned char to_find);
73 
74 std::string line_tools_expand_user_comment(const std::string & user_comment, S_I argc, char *const argv[]);
75 
76     /// split a PATH environement variable string into its components (/usr/lib:/lib => /usr/lib /lib)
77 std::vector<std::string> line_tools_explode_PATH(const char *the_path);
78 
79     /// return the full path of the given filename (eventually unchanged of pointing to the first file of that name present in the_path directories
80 std::string line_tools_get_full_path_from_PATH(const std::vector<std::string> & the_path, const char * filename);
81 
82     /// return split at the first space met the string given as first argument, and provide the two splitted string as second and third argument
83 void line_tools_split_at_first_space(const char *field, std::string & before_space, std::string & after_space);
84 
85 void line_tools_get_min_digits(std::string arg, infinint & num, infinint & ref_num, infinint & aux_num);
86 
87     /// test the presence of a set of argument on the command line
88     ///
89     /// \param[in] arguments is the list of options to look for
90     /// \param[in] argc is the number of argument on the command line
91     /// \param[in] argv is the list of arguments on the command line
92     /// \param[in] getopt_string is the parsing string to pass to getopt
93 #if HAVE_GETOPT_LONG
94     /// \param[in] long_options is the optional list of long options  (an nullptr pointer is acceptable for no long option)
95 #endif
96     /// \param[in] stop_scan if this (char) option is met, stop scanning for wanted options
97     /// \param[out] presence is a subset of arguments containing the option found on command-line
98 extern void line_tools_look_for(const std::vector<char> & arguments,
99 				S_I argc,
100 				char *const argv[],
101 				const char *getopt_string,
102 #if HAVE_GETOPT_LONG
103 				const struct option *long_options,
104 #endif
105 				char stop_scan,
106 				std::vector<char> & presence);
107 
108 
109     /// test the presence of -Q and -j options on the command line
110     ///
111     /// \param[in] argc is the number of argument on the command line
112     /// \param[in] argv is the list of arguments on the command line
113     /// \param[in] getopt_string is the parsing string to pass to getopt
114 #if HAVE_GETOPT_LONG
115     /// \param[in] long_options is the optional list of long options (an nullptr pointer is acceptable for no long option)
116 #endif
117     /// \param[in] stop_scan if this (char) option is met, stop scanning for -j and -Q options
118     /// \param[out] Q_is_present is set to true if -Q option or its equivalent long option has been found on command-line
119 extern void line_tools_look_for_Q(S_I argc,
120 				   char *const argv[],
121 				   const char *getopt_string,
122 #if HAVE_GETOPT_LONG
123 				   const struct option *long_options,
124 #endif
125 				   char stop_scan,
126 				   bool & Q_is_present);
127 
128 
129     /// split a line in words given the separator character (sep)
130 
131 extern std::vector<std::string> line_tools_split(const std::string & val, char sep);
132 
133 extern void line_tools_4_4_build_compatible_overwriting_policy(bool allow_over,
134 							       bool detruire,
135 							       bool more_recent,
136 							       const libdar::infinint & hourshift,
137 							       bool ea_erase,
138 							       const libdar::crit_action * & overwrite);
139 
140     /// split the argument to -K, -J and -$ in their different parts
141     /// \param[in] all is what the user provided on command-line
142     /// \param[out] algo is the symmetrical algorithm to use
143     /// \param[out] pass is either the passphrase
144     /// \param[out] no_cipher_given is true if the use did not specified the cipher (which defaults to blowfish)
145     /// \param[out] recipients emails recipients to use (empty list if gnupg has not to be used)
146 extern void line_tools_crypto_split_algo_pass(const secu_string & all,
147 					      crypto_algo & algo,
148 					      secu_string & pass,
149 					      bool & no_cipher_given,
150 					      std::vector<std::string> & recipients);
151 
152     /// display information about the signatories
153 extern void line_tools_display_signatories(user_interaction & ui, const std::list<signator> & gnupg_signed);
154 
155     /// @}
156 
157 #endif
158