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