Disk ARchive 2.7.16
Full featured and portable backup and archiving tool
line_tools.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// dar - disk archive - a backup/restoration program
3// Copyright (C) 2002-2024 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18//
19// to contact the author, see the AUTHOR file
20/*********************************************************************/
21
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 <deque>
34#include <memory>
35#include "libdar.hpp"
36#include "tlv_list.hpp"
37#include "tools.hpp"
38
39using namespace libdar;
40
43
44constexpr U_I LINE_TOOLS_SI_SUFFIX = 1000;
45constexpr U_I LINE_TOOLS_BIN_SUFFIX = 1024;
46
47class argc_argv
48{
49public:
50 argc_argv(S_I size = 0);
51 argc_argv(const argc_argv & ref) { throw Efeature("argc_argv"); };
52 argc_argv & operator = (const argc_argv & ref) { throw Efeature("argc_argv"); };
53 ~argc_argv() noexcept(false);
54
55 void resize(S_I size);
56 void set_arg(const std::string & arg, S_I index);
57 void set_arg(generic_file & f, U_I size, S_I index);
58
59 S_I argc() const { return x_argc; };
60 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...
61
62private:
63 S_I x_argc;
64 char **x_argv;
65};
66
67extern void line_tools_slice_ownership(const std::string & cmd, std::string & slice_permission, std::string & slice_user_ownership, std::string & slice_group_ownership);
68extern void line_tools_repeat_param(const std::string & cmd, infinint & repeat_count, infinint & repeat_byte);
69extern void line_tools_tlv_list2argv(user_interaction & dialog, tlv_list & list, argc_argv & arg);
70
73
74
75std::string::const_iterator line_tools_find_first_char_out_of_parenth(const std::string & argument, unsigned char to_find);
76std::string::const_iterator line_tools_find_last_char_out_of_parenth(const std::string & argument, unsigned char to_find);
77
78std::string line_tools_expand_user_comment(const std::string & user_comment, S_I argc, char *const argv[]);
79
81std::deque<std::string> line_tools_explode_PATH(const char *the_path);
82
84std::string line_tools_get_full_path_from_PATH(const std::deque<std::string> & the_path, const char * filename);
85
87void line_tools_split_at_first_space(const char *field, std::string & before_space, std::string & after_space);
88
89void line_tools_get_min_digits(std::string arg, infinint & num, infinint & ref_num, infinint & aux_num);
90
92
97#if HAVE_GETOPT_LONG
99#endif
102extern void line_tools_look_for(const std::deque<char> & arguments,
103 S_I argc,
104 char *const argv[],
105 const char *getopt_string,
106#if HAVE_GETOPT_LONG
107 const struct option *long_options,
108#endif
109 char stop_scan,
110 std::deque<char> & presence);
111
112
114
118#if HAVE_GETOPT_LONG
120#endif
123extern void line_tools_look_for_Q(S_I argc,
124 char *const argv[],
125 const char *getopt_string,
126#if HAVE_GETOPT_LONG
127 const struct option *long_options,
128#endif
129 char stop_scan,
130 bool & Q_is_present);
131
132
134
135template <class T> void line_tools_split(const std::string & val, char sep, T & split)
136{
137 std::string::const_iterator be = val.begin();
138 std::string::const_iterator ne = val.begin();
139 split.clear();
140
141 while(ne != val.end())
142 {
143 if(*ne != sep)
144 ++ne;
145 else
146 {
147 split.push_back(std::string(be, ne));
148 ++ne;
149 be = ne;
150 }
151 }
152
153 if(be != val.end())
154 split.push_back(std::string(be, ne));
155}
156
157extern std::set<std::string> line_tools_deque_to_set(const std::deque<std::string> & list);
158
159extern void line_tools_4_4_build_compatible_overwriting_policy(bool allow_over,
160 bool detruire,
161 bool more_recent,
162 const libdar::infinint & hourshift,
163 bool ea_erase,
164 const libdar::crit_action * & overwrite);
165
173 crypto_algo & algo,
174 secu_string & pass,
175 bool & no_cipher_given,
176 std::vector<std::string> & recipients);
177
179extern void line_tools_display_signatories(user_interaction & ui, const std::list<signator> & gnupg_signed);
180
182
186extern void line_tools_read_from_pipe(std::shared_ptr<user_interaction> & dialog, S_I fd, tlv_list & result);
187
189
193extern void line_tools_extract_basename(const char *command_name, std::string & basename);
194
196
202extern std::string::iterator line_tools_find_first_char_of(std::string &s, unsigned char v);
203
205
210extern void line_tools_split_path_basename(const char *all, path * &chemin, std::string & base);
211
213
218extern void line_tools_split_path_basename(const std::string &all, std::string & chemin, std::string & base);
219
221
230extern bool line_tools_split_entrepot_path(const std::string &all,
231 std::string & proto,
232 std::string & login,
233 secu_string & password,
234 std::string & hostname,
235 std::string & port,
236 std::string & path_basename);
237
239
242extern S_I line_tools_str2signed_int(const std::string & x);
243
245
249extern infinint line_tools_convert_date(const std::string & repres);
250
252
257
259
264extern const char *line_tools_get_from_env(const char **env, const char *clef);
265
267
275 const path & loc,
276 std::string & base,
277 const std::string & extension,
278 bool create);
279
281
288 const path & loc,
289 const std::string & base,
290 const std::string & extension,
291 infinint & num_digits);
292
293
295
303extern void line_tools_read_range(const std::string & s, S_I & min, U_I & max);
304
306
310template <class T> void line_tools_split_in_words(generic_file & f, T & mots)
311{
312 std::deque <char> quotes;
313 std::string current = "";
314 char a;
315 bool loop = true;
316 bool escaped = false;
317
318 mots.clear();
319 while(loop)
320 {
321 if(f.read(&a, 1) != 1) // reached end of file
322 {
323 loop = false;
324 a = ' '; // to close the last word
325 }
326
327 if(escaped)
328 {
329 current += a; // added without consideration of quoting of any sort
330 escaped = false;
331 continue; // continuing at beginning of the while loop
332 }
333 else
334 {
335 if(a == '\\')
336 {
337 escaped = true;
338 continue; // continuing at beginning of the while loop
339 }
340 }
341
342 if(quotes.empty()) // outside a word
343 switch(a)
344 {
345 case ' ':
346 case '\t':
347 case '\n':
348 case '\r':
349 break;
350 case '"':
351 case '\'':
352 case '`':
353 quotes.push_back(a);
354 break;
355 default:
356 quotes.push_back(' '); // the quote space means no quote
357 current += a; // a new argument is starting
358 break;
359 }
360 else // inside a word
361 switch(a)
362 {
363 case '\t':
364 if(quotes.back() != ' ')
365 {
366 // this is the end of the wor(l)d ;-)
367 // ...once again... 1000, 1999, 2012, and the next ones to come...
368 break;
369 }
370 // no break !
371 case '\n':
372 case '\r':
373 a = ' '; // replace carriage return inside quoted string by a space
374 // no break !
375 case ' ':
376 case '"':
377 case '\'':
378 case '`':
379 if(a == quotes.back()) // "a" is an ending quote
380 {
381 quotes.pop_back();
382 if(quotes.empty()) // reached end of word
383 {
384 mots.push_back(current);
385 current = "";
386 }
387 else
388 current += a;
389 }
390 else // "a" is a nested starting quote
391 {
392 if(a != ' ') // quote ' ' does not have ending quote
393 quotes.push_back(a);
394 current += a;
395 }
396 break;
397 default:
398 current += a;
399 }
400 }
401 if(!quotes.empty())
402 throw Erange("make_args_from_file", tools_printf(dar_gettext("Parse error: Unmatched `%c'"), quotes.back()));
403}
404
405
406
408
412template <class T> void line_tools_split_in_words(const std::string & arg, T & mots)
413{
414 memory_file mem;
415
416 mem.write(arg.c_str(), arg.size());
417 mem.skip(0);
418 line_tools_split_in_words(mem, mots);
419}
420
422
426extern std::string line_tools_build_regex_for_exclude_mask(const std::string & prefix,
427 const std::string & relative_part);
428
430extern std::string line_tools_get_euid();
431
433extern std::string line_tools_get_egid();
434
436extern std::string line_tools_get_hostname();
437
439extern std::string line_tools_get_date_utc();
440
442extern void line_tools_merge_to_deque(std::deque<std::string> & a, const std::deque<std::string> & b);
443
445extern std::deque<std::string> line_tools_substract_from_deque(const std::deque<std::string> & a, const std::deque<std::string> & b);
446
448
451
452extern void line_tools_split_compression_algo(const char *arg,
453 U_I base,
454 compression & algo,
455 U_I & level,
456 U_I & block_size
457 );
458
459
461
462#endif
exception used when a requested feature is not (yet) implemented
Definition: erreurs.hpp:257
exception used to signal range error
Definition: erreurs.hpp:220
the global action for overwriting
Definition: crit_action.hpp:81
this is the interface class from which all other data transfer classes inherit
virtual void write(const char *a, U_I size) override
write data to the generic_file inherited from proto_generic_file
virtual U_I read(char *a, U_I size) override
read data from the generic_file inherited from proto_generic_file
the arbitrary large positive integer class
generic_file stored in memory
Definition: memory_file.hpp:41
virtual bool skip(const infinint &pos) override
skip at the absolute position
the class path is here to manipulate paths in the Unix notation: using'/'
Definition: path.hpp:51
class secu_string
Definition: secu_string.hpp:54
This is a pure virtual class that is used by libdar when interaction with the user is required.
compression
the different compression algorithm available
Definition: compression.hpp:46
crypto_algo
the different cypher available for encryption (strong or weak)
Definition: crypto.hpp:50
const char * dar_gettext(const char *)
a routine to change NLS domaine forth and back for inline routines
void line_tools_split_path_basename(const char *all, path *&chemin, std::string &base)
split a given full path in path part and basename part
void line_tools_crypto_split_algo_pass(const secu_string &all, crypto_algo &algo, secu_string &pass, bool &no_cipher_given, std::vector< std::string > &recipients)
std::string line_tools_get_date_utc()
return a string containing the current time (UTC)
std::deque< std::string > line_tools_explode_PATH(const char *the_path)
split a PATH environement variable string into its components (/usr/lib:/lib => /usr/lib /lib)
S_I line_tools_reset_getopt()
returns the old position of parsing (next argument to parse)
delta_sig_block_size::fs_function_t line_tools_string_to_sig_block_size_function(const std::string &funname)
converts string name to function
std::string line_tools_get_euid()
return a string containing the Effective UID
void line_tools_read_from_pipe(std::shared_ptr< user_interaction > &dialog, S_I fd, tlv_list &result)
Extract from anonymous pipe a tlv_list.
void line_tools_merge_to_deque(std::deque< std::string > &a, const std::deque< std::string > &b)
add in 'a', element of 'b' not already found in 'a'
void line_tools_split(const std::string &val, char sep, T &split)
split a line in words given the separator character (sep)
Definition: line_tools.hpp:135
std::string line_tools_get_hostname()
return a string containing the hostname of the current host
void line_tools_split_in_words(generic_file &f, T &mots)
read a file and split its contents into words
Definition: line_tools.hpp:310
void line_tools_check_basename(user_interaction &dialog, const path &loc, std::string &base, const std::string &extension, bool create)
does sanity checks on a slice name, check presence and detect whether the given basename is not rathe...
std::string line_tools_build_regex_for_exclude_mask(const std::string &prefix, const std::string &relative_part)
builds a regex from root directory and user provided regex to be applied to the relative path
std::string::iterator line_tools_find_first_char_of(std::string &s, unsigned char v)
give a pointer to the last character of the given value in the given string
void line_tools_extract_basename(const char *command_name, std::string &basename)
extracts the basename of a file (removing path part)
std::string line_tools_get_full_path_from_PATH(const std::deque< std::string > &the_path, const char *filename)
return the full path of the given filename (eventually unchanged of pointing to the first file of tha...
void line_tools_check_min_digits(user_interaction &dialog, const path &loc, const std::string &base, const std::string &extension, infinint &num_digits)
if a slice number 1 is met with the provided basename, set the num_digits accordingly
void line_tools_read_range(const std::string &s, S_I &min, U_I &max)
from a string with a range notation (min-max) extract the range values
std::deque< std::string > line_tools_substract_from_deque(const std::deque< std::string > &a, const std::deque< std::string > &b)
remove from 'a' elements found in 'b' and return the resulting deque
const char * line_tools_get_from_env(const char **env, const char *clef)
isolate the value of a given variable from the environment vector
std::string line_tools_get_egid()
return a string containing the Effective UID
infinint line_tools_convert_date(const std::string &repres)
convert a human readable date representation in number of second since the system reference date
void line_tools_display_signatories(user_interaction &ui, const std::list< signator > &gnupg_signed)
display information about the signatories
void line_tools_display_features(user_interaction &dialog)
display the compilation time features of libdar
void line_tools_look_for(const std::deque< char > &arguments, S_I argc, char *const argv[], const char *getopt_string, char stop_scan, std::deque< char > &presence)
test the presence of a set of argument on the command line
bool line_tools_split_entrepot_path(const std::string &all, std::string &proto, std::string &login, secu_string &password, std::string &hostname, std::string &port, std::string &path_basename)
split a given full remote repository path in parts
S_I line_tools_str2signed_int(const std::string &x)
convert a signed integer written in decimal notation to the corresponding value
void line_tools_look_for_Q(S_I argc, char *const argv[], const char *getopt_string, char stop_scan, bool &Q_is_present)
test the presence of -Q and -j options on the command line
void line_tools_split_compression_algo(const char *arg, U_I base, compression &algo, U_I &level, U_I &block_size)
void line_tools_split_at_first_space(const char *field, std::string &before_space, std::string &after_space)
return split at the first space met the string given as first argument, and provide the two splitted ...
std::string tools_printf(const char *format,...)
make printf-like formating to a std::string
the main file of the libdar API definitions
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47
fs_function_t
defines the function to use to derivate block size from file size
List of Generic Type Length Value data structures.
a set of general purpose routines