File: main_execute.c

package info (click to toggle)
crystal-facet-uml 1.63.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 21,640 kB
  • sloc: ansic: 109,610; xml: 3,085; makefile: 138; sh: 113
file content (242 lines) | stat: -rw-r--r-- 7,344 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* File: main_execute.c; Copyright and License: see below */

#include "main_execute.h"
#include "main_commands.h"
#include "u8/u8_trace.h"
#include "u8/u8_log.h"
#include "meta/meta_info.h"
#include "meta/meta_version.h"
#include "utf8stringbuf/utf8string.h"
#include "u8stream/universal_stream_output_stream.h"
#include "utf8stream/utf8stream_writer.h"
#include <sys/types.h>
#include <stdbool.h>
#include <stdlib.h>
#include <signal.h>
#include <assert.h>

static const char *const MAIN_HELP
    = "\nUsage:\n"
#ifndef NDEBUG
    "    G_ENABLE_DIAGNOSTIC=1 ./crystal-facet-uml : for more debug information\n"
#endif
    "    -h : for help\n"
    "    -v : for version\n"
    "    -e <file> <export_format> <export_directory> : to export all diagrams,\n"
    "       export_format: docbook|json|pdf|png|ps|svg|txt|html|xmi\n"
    "    -i <file> <import_mode>   <json_input_file>  : to import elements,\n"
    "       import_mode: check|add\n"
    "   [-u] <file> : to use/create a database file\n"
    "    -t <file> : to test the database file\n"
    "    -r <file> : to test and repair the database file\n";

int main_execute (int argc, char **argv) {
    U8_TRACE_BEGIN();
    U8_TRACE_TIMESTAMP();
    u8_error_t exit_code = U8_ERROR_NONE;
    U8_LOG_INIT(META_INFO_PROGRAM_ID_STR);
    char *database_file = NULL;
    char *export_directory = NULL;
    char *import_file = NULL;
    bool do_not_start = false;
    bool do_repair = false;
    bool do_check = false;
    bool do_export = false;
    bool do_import = false;
    io_file_format_t export_format = 0;
    io_import_mode_t import_mode = 0;
    universal_stream_output_stream_t out_stream;
    universal_stream_output_stream_init( &out_stream, stdout );
    utf8stream_writer_t writer;
    utf8stream_writer_init( &writer, universal_stream_output_stream_get_output_stream( &out_stream ) );

    /* handle options */
    if ( argc == 2 )
    {
        if ( utf8string_equals_str( argv[1], "-h" ) )
        {
            utf8stream_writer_write_str( &writer, MAIN_HELP );
            do_not_start = true;
        }
        if ( utf8string_equals_str( argv[1], "-v" ) )
        {
            utf8stream_writer_write_str( &writer, "\n" );
            utf8stream_writer_write_str( &writer, META_VERSION_STR );
            utf8stream_writer_write_str( &writer, "\n" );
            do_not_start = true;
        }
        if ( ! utf8string_starts_with_str( argv[1], "-" ) )
        {
            /* like -u, allows nice drag+drop integration on Windows(TM) */
            database_file = argv[1];
        }
    }
    if ( argc == 3 )
    {
        if ( utf8string_equals_str( argv[1], "-r" ) )
        {
            database_file = argv[2];
            do_not_start = true;
            do_repair = true;
        }
        if ( utf8string_equals_str( argv[1], "-t" ) )
        {
            database_file = argv[2];
            do_not_start = true;
            do_check = true;
        }
        if ( utf8string_equals_str( argv[1], "-u" ) )
        {
            database_file = argv[2];
        }
    }
    if ( argc == 5 )
    {
        if ( utf8string_equals_str( argv[1], "-e" ) )
        {
            database_file = argv[2];
            export_format = main_execute_private_get_selected_format(argv[3]);
            export_directory = argv[4];
            do_not_start = true;
            do_export = true;
        }
        if ( utf8string_equals_str( argv[1], "-i" ) )
        {
            database_file = argv[2];
            import_mode = main_execute_private_get_selected_mode(argv[3]);
            import_file = argv[4];
            do_not_start = true;
            do_import = true;
        }
    }

    {
        main_commands_t commands;
        const int argc_remaining = 1;
        exit_code |= main_commands_init( &commands, ( ! do_not_start ), argc_remaining, argv );

        /* repair database */
        if ( do_repair || do_check )
        {
            exit_code |= main_commands_repair( &commands, database_file, do_check, &writer );
        }

        if ( do_export )
        {
            exit_code |= main_commands_export( &commands, database_file, export_format, export_directory, &writer );
        }

        if ( do_import )
        {
            exit_code |= main_commands_import( &commands, database_file, import_mode, import_file, &writer );
        }

        /* run program */
        if ( ! do_not_start )
        {
            exit_code |= main_commands_start_gui( &commands, database_file, &writer );
        }

        main_commands_destroy( &commands );
    }
    utf8stream_writer_destroy( &writer );
    universal_stream_output_stream_destroy( &out_stream );

    U8_LOG_STATS();
    U8_LOG_DESTROY();
    U8_TRACE_TIMESTAMP();
    int exit_byte = ((exit_code >> 24)|(exit_code >> 16)|(exit_code >> 8)|(exit_code))&0xff;
    U8_TRACE_END_ERR(exit_byte);
    return exit_byte;
}

io_file_format_t main_execute_private_get_selected_format( char *arg_fmt )
{
    U8_TRACE_BEGIN();
    assert( arg_fmt != NULL );
    io_file_format_t result = 0;

    if ( utf8string_equals_str( arg_fmt, "docbook" ) )
    {
        result = IO_FILE_FORMAT_DOCBOOK;
    }
    else if ( utf8string_equals_str( arg_fmt, "json" ) )
    {
        result = IO_FILE_FORMAT_JSON;
    }
    else if ( utf8string_equals_str( arg_fmt, "pdf" ) )
    {
        result = IO_FILE_FORMAT_PDF;
    }
    else if ( utf8string_equals_str( arg_fmt, "png" ) )
    {
        result = IO_FILE_FORMAT_PNG;
    }
    else if ( utf8string_equals_str( arg_fmt, "ps" ) )
    {
        result = IO_FILE_FORMAT_PS;
    }
    else if ( utf8string_equals_str( arg_fmt, "svg" ) )
    {
        result = IO_FILE_FORMAT_SVG;
    }
    else if ( utf8string_equals_str( arg_fmt, "txt" ) )
    {
        result = IO_FILE_FORMAT_TXT;
    }
    else if ( utf8string_equals_str( arg_fmt, "html" ) )
    {
        result = IO_FILE_FORMAT_HTML;
    }
    else if ( utf8string_equals_str( arg_fmt, "xhtml" ) /* legacy option */ )
    {
        result = IO_FILE_FORMAT_HTML;
    }
    else if ( utf8string_equals_str( arg_fmt, "xmi" ) )
    {
        result = IO_FILE_FORMAT_XMI2;
    }

    U8_TRACE_END();
    return result;
}

io_import_mode_t main_execute_private_get_selected_mode( char *arg_fmt )
{
    U8_TRACE_BEGIN();
    assert( arg_fmt != NULL );
    io_import_mode_t result = IO_IMPORT_MODE_CHECK;

    if ( utf8string_equals_str( arg_fmt, "check" ) )
    {
        result = IO_IMPORT_MODE_CHECK;
    }
    else if ( utf8string_equals_str( arg_fmt, "add" ) )
    {
        result = IO_IMPORT_MODE_CREATE|IO_IMPORT_MODE_LINK;
    }
    else if ( utf8string_equals_str( arg_fmt, "update" ) )  /* legacy option */
    {
        result = IO_IMPORT_MODE_CREATE|IO_IMPORT_MODE_LINK;
    }

    U8_TRACE_END();
    return result;
}


/*
Copyright 2016-2025 Andreas Warnke

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/