dbuspolicy-printer: add xml format
[platform/core/system/libdbuspolicy.git] / src / dbuspolicy_printer.cpp
1 #include "internal/print_content.hpp"
2 #include "internal/storage_backend_serialized.hpp"
3 #include <iostream>
4 #include <getopt.h>
5 #include "libdbuspolicy1-private.h"
6
7 using namespace std;
8
9 static const struct option options[] = {
10         {"system", no_argument, 0, 0 },
11         {"session", no_argument, 0, 0},
12         {0, 0, 0, 0}
13 };
14
15 static void print_help(const char *name) {
16         cout << endl;
17         cout << "usage: " << name << " [-i serialized filename] [-v]" << endl;
18         cout << "       " << name << " {--system|--session} [-v]" << endl;
19         cout << " -v - just verify, don't print anything" << endl;
20         cout << " -x - print rule as xml format" << endl;
21         cout << endl;
22 }
23
24 int main(int argc, char *argv[]) {
25         std::string input_filename;
26         int c;
27         bool just_verify = false;
28         bool xml_format = false;
29
30         if (argc < 2) {
31                 print_help(argv[0]);
32                 return EXIT_FAILURE;
33         }
34
35         while (1) {
36                 int option_index;
37                 c = getopt_long(argc, argv, "i:v:x", options, &option_index);
38                 if (c == -1)
39                         break;
40
41                 switch(c) {
42                 case 0:
43                         if (option_index == 0)
44                                 input_filename = system_bus_conf_file_primary();
45                         else
46                                 input_filename = session_bus_conf_file_primary();
47                         input_filename.append(".serialized");
48                         break;
49                 case 'i':
50                         input_filename = optarg;
51                         break;
52                 case 'v':
53                         just_verify = true;
54                         break;
55                 case 'x':
56                         xml_format = true;
57                         break;
58                 case '?':
59                         print_help(argv[0]);
60                         return EXIT_FAILURE;
61                 }
62         }
63
64         if (optind < argc)
65                 input_filename = argv[optind];
66
67         if (input_filename.empty()) {
68                 cout << "No input filename" << endl;
69                 print_help(argv[0]);
70                 return EXIT_FAILURE;
71         }
72
73         tslog::init(tslog::ldp_log_level::DEFAULT);
74
75         ldp_serialized::StorageBackendSerialized storage;
76
77         if (!storage.init(input_filename.c_str(), true)) {
78                 if (just_verify)
79                         cout << input_filename << ": FAILED" << endl;
80                 return EXIT_FAILURE;
81         }
82
83         if (just_verify) {
84                 cout << input_filename << ": OK" << endl;
85                 return EXIT_SUCCESS;
86         }
87
88         storage.printContent(xml_format);
89
90         return EXIT_SUCCESS;
91 }