diff options
author | David Robillard <d@drobilla.net> | 2023-04-05 07:01:18 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:08 -0500 |
commit | 3cc5a4fcaea8f1e2fd47c53135b53f8edcb8619d (patch) | |
tree | 4496d7af98b1033630d992db1b8788077ebda8f6 /tools/console.h | |
parent | 89acd9d099bd46c1cbf17ee3a7bb78610a8138fe (diff) | |
download | serd-3cc5a4fcaea8f1e2fd47c53135b53f8edcb8619d.tar.gz serd-3cc5a4fcaea8f1e2fd47c53135b53f8edcb8619d.tar.bz2 serd-3cc5a4fcaea8f1e2fd47c53135b53f8edcb8619d.zip |
Factor out and rewrite command-line interface
Diffstat (limited to 'tools/console.h')
-rw-r--r-- | tools/console.h | 96 |
1 files changed, 92 insertions, 4 deletions
diff --git a/tools/console.h b/tools/console.h index a7e8423f..d475aebc 100644 --- a/tools/console.h +++ b/tools/console.h @@ -6,29 +6,85 @@ #include "serd/env.h" #include "serd/input_stream.h" +#include "serd/memory.h" #include "serd/output_stream.h" #include "serd/reader.h" +#include "serd/sink.h" #include "serd/status.h" #include "serd/string_view.h" #include "serd/syntax.h" #include "serd/world.h" #include "serd/writer.h" +#include <stdbool.h> +#include <stdint.h> #include <stdio.h> +// Iterator over command-line options with support for BSD-style flag merging +typedef struct { + char* const* argv; ///< Complete argument vector (from main) + int argc; ///< Total number of arguments (from main) + int a; ///< Argument index (index into argv) + int f; ///< Flag index (offset in argv[arg]) +} OptionIter; + +// Options for the input or output syntax +typedef struct { + SerdSyntax syntax; ///< User-specified syntax, or empty + uint32_t flags; ///< SerdReaderFlags or SerdWriterFlags + bool overridden; ///< True if syntax was explicitly given +} SerdSyntaxOptions; + +// Options common to all command-line tools +typedef struct { + const char* base_uri; + const char* out_filename; + size_t block_size; + size_t stack_size; + SerdSyntaxOptions input; + SerdSyntaxOptions output; +} SerdCommonOptions; + +// Common "global" state of a command-line tool that writes data +typedef struct { + SerdOutputStream out; + SerdWorld* world; + SerdEnv* env; + SerdWriter* writer; +} SerdTool; + +bool +serd_option_iter_is_end(OptionIter iter); + +SerdStatus +serd_option_iter_advance(OptionIter* iter); + +SerdStatus +serd_tool_setup(SerdTool* tool, const char* program, SerdCommonOptions options); + +SerdStatus +serd_tool_cleanup(SerdTool tool); + void serd_set_stream_utf8_mode(FILE* stream); -int +SerdStatus serd_print_version(const char* program); SerdStatus serd_set_base_uri_from_path(SerdEnv* env, const char* path); SerdSyntax -serd_choose_syntax(SerdWorld* world, - SerdSyntax requested, - const char* filename); +serd_choose_syntax(SerdWorld* world, + SerdSyntaxOptions options, + const char* filename, + SerdSyntax fallback); + +SerdStatus +serd_get_argument(OptionIter* iter, const char** argument); + +SerdStatus +serd_get_size_argument(OptionIter* iter, size_t* argument); SerdStatus serd_set_input_option(SerdStringView name, @@ -36,14 +92,46 @@ serd_set_input_option(SerdStringView name, SerdReaderFlags* flags); SerdStatus +serd_parse_input_argument(OptionIter* iter, SerdSyntaxOptions* options); + +SerdStatus serd_set_output_option(SerdStringView name, SerdSyntax* syntax, SerdWriterFlags* flags); +SerdStatus +serd_parse_output_argument(OptionIter* iter, SerdSyntaxOptions* options); + +SerdStatus +serd_parse_common_option(OptionIter* iter, SerdCommonOptions* opts); + +SerdEnv* +serd_create_env(SerdAllocator* allocator, + const char* program, + const char* base_string, + const char* out_filename); + SerdInputStream serd_open_tool_input(const char* filename); SerdOutputStream serd_open_tool_output(const char* filename); +SerdStatus +serd_read_source(SerdWorld* world, + SerdCommonOptions opts, + SerdEnv* env, + SerdSyntax syntax, + SerdInputStream* in, + const char* name, + const SerdSink* sink); + +SerdStatus +serd_read_inputs(SerdWorld* world, + SerdCommonOptions opts, + SerdEnv* env, + intptr_t n_inputs, + char* const* inputs, + const SerdSink* sink); + #endif // SERD_TOOLS_CONSOLE_H |