Line data Source code
1 : /* File: main_execute.c; Copyright and License: see below */
2 :
3 : #include "main_execute.h"
4 : #include "main_commands.h"
5 : #include "u8/u8_trace.h"
6 : #include "u8/u8_log.h"
7 : #include "meta/meta_info.h"
8 : #include "meta/meta_version.h"
9 : #include "utf8stringbuf/utf8string.h"
10 : #include "u8stream/universal_stream_output_stream.h"
11 : #include "utf8stream/utf8stream_writer.h"
12 : #include <sys/types.h>
13 : #include <stdbool.h>
14 : #include <stdlib.h>
15 : #include <signal.h>
16 : #include <assert.h>
17 :
18 : static const char *const MAIN_HELP
19 : = "\nUsage:\n"
20 : #ifndef NDEBUG
21 : " G_ENABLE_DIAGNOSTIC=1 ./crystal-facet-uml : for mor debug information\n"
22 : #endif
23 : " -h : for help\n"
24 : " -v : for version\n"
25 : " -e <database_file> <export_format> <export_directory> : to export all diagrams,\n"
26 : " export_format: docbook|json|pdf|png|ps|svg|txt|html|xmi\n"
27 : " -i <database_file> <import_mode> <json_input_file> : to import elements,\n"
28 : " import_mode: check|add\n"
29 : " [-u] <database_file> : to use/create a database file\n"
30 : " extension for compact sqlite format: *.cfu1\n"
31 : " -g <database_file> : to upgrade the database tables from version 1.32.1 and older\n"
32 : " -t <database_file> : to test the database file\n"
33 : " -r <database_file> : to test and repair the database file\n";
34 :
35 0 : int main_execute (int argc, char **argv) {
36 0 : U8_TRACE_BEGIN();
37 0 : U8_TRACE_TIMESTAMP();
38 0 : u8_error_t exit_code = U8_ERROR_NONE;
39 0 : U8_LOG_INIT(META_INFO_PROGRAM_ID_STR);
40 0 : char *database_file = NULL;
41 0 : char *export_directory = NULL;
42 0 : char *import_file = NULL;
43 0 : bool do_not_start = false;
44 0 : bool do_repair = false;
45 0 : bool do_check = false;
46 0 : bool do_export = false;
47 0 : bool do_upgrade = false;
48 0 : bool do_import = false;
49 0 : io_file_format_t export_format = 0;
50 0 : io_import_mode_t import_mode = 0;
51 : universal_stream_output_stream_t out_stream;
52 0 : universal_stream_output_stream_init( &out_stream, stdout );
53 : utf8stream_writer_t writer;
54 0 : utf8stream_writer_init( &writer, universal_stream_output_stream_get_output_stream( &out_stream ) );
55 :
56 : /* handle options */
57 0 : if ( argc == 2 )
58 : {
59 0 : if ( utf8string_equals_str( argv[1], "-h" ) )
60 : {
61 0 : utf8stream_writer_write_str( &writer, MAIN_HELP );
62 0 : do_not_start = true;
63 : }
64 0 : if ( utf8string_equals_str( argv[1], "-v" ) )
65 : {
66 0 : utf8stream_writer_write_str( &writer, "\n" );
67 0 : utf8stream_writer_write_str( &writer, META_VERSION_STR );
68 0 : utf8stream_writer_write_str( &writer, "\n" );
69 0 : do_not_start = true;
70 : }
71 0 : if ( ! utf8string_starts_with_str( argv[1], "-" ) )
72 : {
73 : /* like -u, allows nice drag+drop integration on Windows(TM) */
74 0 : database_file = argv[1];
75 : }
76 : }
77 0 : if ( argc == 3 )
78 : {
79 0 : if ( utf8string_equals_str( argv[1], "-r" ) )
80 : {
81 0 : database_file = argv[2];
82 0 : do_not_start = true;
83 0 : do_repair = true;
84 : }
85 0 : if ( utf8string_equals_str( argv[1], "-t" ) )
86 : {
87 0 : database_file = argv[2];
88 0 : do_not_start = true;
89 0 : do_check = true;
90 : }
91 0 : if ( utf8string_equals_str( argv[1], "-g" ) )
92 : {
93 0 : database_file = argv[2];
94 0 : do_not_start = true;
95 0 : do_upgrade = true;
96 : }
97 0 : if ( utf8string_equals_str( argv[1], "-u" ) )
98 : {
99 0 : database_file = argv[2];
100 : }
101 : }
102 0 : if ( argc == 5 )
103 : {
104 0 : if ( utf8string_equals_str( argv[1], "-e" ) )
105 : {
106 0 : database_file = argv[2];
107 0 : export_format = main_execute_private_get_selected_format(argv[3]);
108 0 : export_directory = argv[4];
109 0 : do_not_start = true;
110 0 : do_export = true;
111 : }
112 0 : if ( utf8string_equals_str( argv[1], "-i" ) )
113 : {
114 0 : database_file = argv[2];
115 0 : import_mode = main_execute_private_get_selected_mode(argv[3]);
116 0 : import_file = argv[4];
117 0 : do_not_start = true;
118 0 : do_import = true;
119 : }
120 : }
121 :
122 : {
123 : main_commands_t commands;
124 0 : const int argc_remaining = 1;
125 0 : exit_code |= main_commands_init( &commands, ( ! do_not_start ), argc_remaining, argv );
126 :
127 : /* upgrade only if explicitly requested, otherwiese read-only mode of -t and -e options does not work: */
128 0 : if ( do_upgrade )
129 : {
130 0 : exit_code |= main_commands_upgrade( &commands, database_file, &writer );
131 : }
132 :
133 : /* repair database */
134 0 : if ( do_repair || do_check )
135 : {
136 0 : exit_code |= main_commands_repair( &commands, database_file, do_check, &writer );
137 : }
138 :
139 0 : if ( do_export )
140 : {
141 0 : exit_code |= main_commands_export( &commands, database_file, export_format, export_directory, &writer );
142 : }
143 :
144 0 : if ( do_import )
145 : {
146 0 : exit_code |= main_commands_import( &commands, database_file, import_mode, import_file, &writer );
147 : }
148 :
149 : /* run program */
150 0 : if ( ! do_not_start )
151 : {
152 0 : exit_code |= main_commands_start_gui( &commands, database_file, &writer );
153 : }
154 :
155 0 : main_commands_destroy( &commands );
156 : }
157 0 : utf8stream_writer_destroy( &writer );
158 0 : universal_stream_output_stream_destroy( &out_stream );
159 :
160 0 : U8_LOG_STATS();
161 0 : U8_LOG_DESTROY();
162 0 : U8_TRACE_TIMESTAMP();
163 0 : int exit_byte = ((exit_code >> 24)|(exit_code >> 16)|(exit_code >> 8)|(exit_code))&0xff;
164 0 : U8_TRACE_END_ERR(exit_byte);
165 0 : return exit_byte;
166 : }
167 :
168 0 : io_file_format_t main_execute_private_get_selected_format( char *arg_fmt )
169 : {
170 0 : U8_TRACE_BEGIN();
171 0 : assert( arg_fmt != NULL );
172 0 : io_file_format_t result = 0;
173 :
174 0 : if ( utf8string_equals_str( arg_fmt, "docbook" ) )
175 : {
176 0 : result = IO_FILE_FORMAT_DOCBOOK;
177 : }
178 0 : else if ( utf8string_equals_str( arg_fmt, "json" ) )
179 : {
180 0 : result = IO_FILE_FORMAT_JSON;
181 : }
182 0 : else if ( utf8string_equals_str( arg_fmt, "pdf" ) )
183 : {
184 0 : result = IO_FILE_FORMAT_PDF;
185 : }
186 0 : else if ( utf8string_equals_str( arg_fmt, "png" ) )
187 : {
188 0 : result = IO_FILE_FORMAT_PNG;
189 : }
190 0 : else if ( utf8string_equals_str( arg_fmt, "ps" ) )
191 : {
192 0 : result = IO_FILE_FORMAT_PS;
193 : }
194 0 : else if ( utf8string_equals_str( arg_fmt, "svg" ) )
195 : {
196 0 : result = IO_FILE_FORMAT_SVG;
197 : }
198 0 : else if ( utf8string_equals_str( arg_fmt, "txt" ) )
199 : {
200 0 : result = IO_FILE_FORMAT_TXT;
201 : }
202 0 : else if ( utf8string_equals_str( arg_fmt, "html" ) )
203 : {
204 0 : result = IO_FILE_FORMAT_HTML;
205 : }
206 0 : else if ( utf8string_equals_str( arg_fmt, "xhtml" ) /* legacy option */ )
207 : {
208 0 : result = IO_FILE_FORMAT_HTML;
209 : }
210 0 : else if ( utf8string_equals_str( arg_fmt, "xmi" ) )
211 : {
212 0 : result = IO_FILE_FORMAT_XMI2;
213 : }
214 :
215 0 : U8_TRACE_END();
216 0 : return result;
217 : }
218 :
219 0 : io_import_mode_t main_execute_private_get_selected_mode( char *arg_fmt )
220 : {
221 0 : U8_TRACE_BEGIN();
222 0 : assert( arg_fmt != NULL );
223 0 : io_import_mode_t result = IO_IMPORT_MODE_CHECK;
224 :
225 0 : if ( utf8string_equals_str( arg_fmt, "check" ) )
226 : {
227 0 : result = IO_IMPORT_MODE_CHECK;
228 : }
229 0 : else if ( utf8string_equals_str( arg_fmt, "add" ) )
230 : {
231 0 : result = IO_IMPORT_MODE_CREATE|IO_IMPORT_MODE_LINK;
232 : }
233 0 : else if ( utf8string_equals_str( arg_fmt, "update" ) ) /* legacy option */
234 : {
235 0 : result = IO_IMPORT_MODE_CREATE|IO_IMPORT_MODE_LINK;
236 : }
237 :
238 0 : U8_TRACE_END();
239 0 : return result;
240 : }
241 :
242 :
243 : /*
244 : Copyright 2016-2024 Andreas Warnke
245 :
246 : Licensed under the Apache License, Version 2.0 (the "License");
247 : you may not use this file except in compliance with the License.
248 : You may obtain a copy of the License at
249 :
250 : http://www.apache.org/licenses/LICENSE-2.0
251 :
252 : Unless required by applicable law or agreed to in writing, software
253 : distributed under the License is distributed on an "AS IS" BASIS,
254 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
255 : See the License for the specific language governing permissions and
256 : limitations under the License.
257 : */
|