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