author | František Kučera <franta-hg@frantovo.cz> |
Sat, 04 Dec 2021 21:14:49 +0100 | |
branch | v_0 |
changeset 27 | 3d2d4c863431 |
parent 26 | b4c4c7f937e9 |
permissions | -rw-r--r-- |
7 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
19
3cc1b9be97bc
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
7 | 8 |
* |
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#include <cstdlib> |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
18 |
#include <fstream> |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <memory> |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
20 |
#include <regex> |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
21 |
#include <algorithm> |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
22 |
#include <unistd.h> |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
|
5
549a40d276c3
relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
24 |
#include <relpipe/writer/RelationalWriter.h> |
549a40d276c3
relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
25 |
#include <relpipe/writer/RelpipeWriterException.h> |
549a40d276c3
relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
26 |
#include <relpipe/writer/Factory.h> |
549a40d276c3
relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
27 |
#include <relpipe/writer/TypeId.h> |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
|
4
0afc596749a1
move CLI.h to a common header-only library
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
29 |
#include <relpipe/cli/CLI.h> |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
|
25
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
31 |
#include "CLIParser.h" |
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
32 |
#include "Configuration.h" |
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
33 |
#include "FstabCommand.h" |
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
34 |
|
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
using namespace relpipe::cli; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
using namespace relpipe::writer; |
25
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
37 |
using namespace relpipe::in::fstab; |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
38 |
|
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
int main(int argc, char** argv) { |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
setlocale(LC_ALL, ""); |
8
21b14ec0bc60
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
41 |
CLI::untieStdIO(); |
25
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
42 |
CLI cli(argc, argv); |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
try { |
25
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
47 |
CLIParser cliParser; |
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
48 |
Configuration configuration = cliParser.parse(cli.arguments()); |
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
49 |
FstabCommand command; |
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
50 |
std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout)); |
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
51 |
|
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
if (isatty(fileno(stdin))) { |
3
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
53 |
/** |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
54 |
* Our program is executed on TTY without input stream redirection → read from default file location. |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
55 |
* e.g. $ relpipe-in-fstab | … |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
56 |
* (we don't expect that user is writing the content of fstab by hand on the terminal) |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
57 |
*/ |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
ifstream s("/etc/fstab"); |
25
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
59 |
command.process(s, writer, configuration); |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
} else { |
3
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
61 |
/** |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
62 |
* Input stream comes from a file or is piped from other command → read it instead of default file. |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
63 |
* e.g. $ cat /etc/fstab | grep '/mnt/' | relpipe-in-fstab | … |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
64 |
* or $ relpipe-in-fstab < /etc/fstab | … # without UUoC |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
65 |
* or $ ssh example.com cat /etc/fstab | relpipe-in-fstab | … |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
66 |
* or $ cat | relpipe-in-fstab | … # user writes the fstab content by hand |
25
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
67 |
* or $ cat /etc/mtab | relpipe-in-fstab | relpipe-out-tabular | less -RSi # currently mounted filesystems |
3
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
68 |
*/ |
25
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
69 |
command.process(std::cin, writer, configuration); |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
} |
25
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
71 |
|
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
72 |
resultCode = CLI::EXIT_CODE_SUCCESS; |
25
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
73 |
} catch (RelpipeCLIException e) { |
26
b4c4c7f937e9
exception: fix typo: getMessge() → getMessage()
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
74 |
fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessage().c_str()); |
25
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
75 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
f71eb7aefd25
add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
76 |
resultCode = e.getExitCode(); |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
} catch (RelpipeWriterException e) { |
26
b4c4c7f937e9
exception: fix typo: getMessge() → getMessage()
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
78 |
fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessage().c_str()); |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
79 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
80 |
resultCode = CLI::EXIT_CODE_DATA_ERROR; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
81 |
} |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
82 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
83 |
return resultCode; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
84 |
} |