1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2020 Red Hat, Inc.
5 //
6 // Author: Dodji Seketeli
7
8 /// @file
9 ///
10 /// This is a tool that reads an ini file and, if it could read it OK,
11 /// prints it on its standard output. It's mainly meant to test the
12 /// abigail::ini::* functions, but one could also use it to make sure
13 /// that an ini file can be handled by the abigail::ini::* facilities
14
15 #include <cstring>
16 #include <iostream>
17 #include "abg-ini.h"
18
19 using std::cout;
20 using std::cerr;
21 using std::cin;
22 using std::string;
23 using std::ostream;
24 using abigail::ini::config;
25 using abigail::ini::config_sptr;
26 using abigail::ini::read_config;
27 using abigail::ini::write_config;
28
29 struct options
30 {
31 bool display_usage;
32 bool read_from_stdin;
33 bool no_out;
34 bool wrong_command_line_usage;
35 string path;
36
optionsoptions37 options ()
38 : display_usage(false),
39 read_from_stdin(false),
40 no_out(false),
41 wrong_command_line_usage(false)
42 {}
43 };
44
45 static void
display_usage(const string & prog_name,ostream & out)46 display_usage(const string& prog_name, ostream& out)
47 {
48 out << "usage: " << prog_name << " [options] <ini file>\n"
49 << "where options can be:\n"
50 << "--help display this help\n"
51 << "--from-stdin read the input ini file from stdin\n"
52 << "--noout do not output anything on stdout\n"
53 ;
54 }
55
56 static bool
parse_command_line(int argc,char * argv[],options & opts)57 parse_command_line(int argc, char* argv[], options& opts)
58 {
59 if (argc == 1)
60 return false;
61
62 for (int i = 1; i < argc; ++i)
63 {
64 if (argv[i][0] != '-')
65 {
66 if (opts.path.empty())
67 opts.path = argv[i];
68 else
69 opts.wrong_command_line_usage = true;
70 }
71 else if (!strcmp(argv[i], "--help"))
72 opts.display_usage = true;
73 else if (!strcmp(argv[i], "--from-stdin"))
74 opts.read_from_stdin = true;
75 else if (!strcmp(argv[i], "--noout"))
76 opts.no_out = true;
77 else
78 return false;
79 }
80 return true;
81 }
82
83 int
main(int argc,char * argv[])84 main(int argc, char* argv[])
85 {
86 // First, parse the command line.
87
88 options opts;
89
90 if (argc == 1 || !parse_command_line(argc, argv, opts))
91 {
92 cerr << argv[0] << ": bad command usage\n"
93 << "Try " << argv[0] << " --help for more information\n";
94 return 1;
95 }
96
97 // Then if we need to display the help, display it and get out.
98
99 if (opts.display_usage)
100 {
101 display_usage(argv[0], cout);
102 return 0;
103 }
104
105 // Otherwise, do the real work we are supposed to do after all.
106 // That real work is driven by the options the user set; these
107 // options are recorded in the opts variable.
108
109 config_sptr conf;
110
111 if (opts.read_from_stdin)
112 conf = read_config(cin);
113 else if (!opts.path.empty())
114 conf = read_config(opts.path);
115
116 if (conf && !opts.no_out)
117 write_config(*conf, std::cout);
118
119 return !conf;
120 }
121