| 1 |
// Copyright 2011-2016 David Robillard <[email protected]> |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include "sord_config.h" |
| 5 |
|
| 6 |
#include <serd/serd.h> |
| 7 |
#include <sord/sord.h> |
| 8 |
#include <zix/allocator.h> |
| 9 |
#include <zix/filesystem.h> |
| 10 |
|
| 11 |
#include <stdbool.h> |
| 12 |
#include <stdint.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
#define SORDI_ERROR(msg) fprintf(stderr, "sordi: " msg) |
| 18 |
#define SORDI_ERRORF(fmt, ...) fprintf(stderr, "sordi: " fmt, __VA_ARGS__) |
| 19 |
|
| 20 |
typedef struct { |
| 21 |
SerdWriter* writer; |
| 22 |
SerdEnv* env; |
| 23 |
SerdNode base_uri_node; |
| 24 |
SerdURI base_uri; |
| 25 |
SordModel* sord; |
| 26 |
} State; |
| 27 |
|
| 28 |
static int |
| 29 |
print_version(void) |
| 30 |
{ |
| 31 |
printf("sordi " SORD_VERSION " <http://drobilla.net/software/sord>\n"); |
| 32 |
printf("Copyright 2011-2021 David Robillard <[email protected]>.\n" |
| 33 |
"License: <http://www.opensource.org/licenses/isc>\n" |
| 34 |
"This is free software; you are free to change and redistribute it." |
| 35 |
"\nThere is NO WARRANTY, to the extent permitted by law.\n"); |
| 36 |
return 0; |
| 37 |
} |
| 38 |
|
| 39 |
static int |
| 40 |
print_usage(const char* name, bool error) |
| 41 |
{ |
| 42 |
FILE* const os = error ? stderr : stdout; |
| 43 |
fprintf(os, "%s", error ? "\n" : ""); |
| 44 |
fprintf(os, "Usage: %s [OPTION]... INPUT [BASE_URI]\n", name); |
| 45 |
fprintf(os, "Load and re-serialise RDF data.\n"); |
| 46 |
fprintf(os, "Use - for INPUT to read from standard input.\n\n"); |
| 47 |
fprintf(os, " -h Display this help and exit\n"); |
| 48 |
fprintf(os, " -i SYNTAX Input syntax (`turtle' or `ntriples')\n"); |
| 49 |
fprintf(os, " -o SYNTAX Output syntax (`turtle' or `ntriples')\n"); |
| 50 |
fprintf(os, " -s INPUT Parse INPUT as string (terminates options)\n"); |
| 51 |
fprintf(os, " -v Display version information and exit\n"); |
| 52 |
return error ? 1 : 0; |
| 53 |
} |
| 54 |
|
| 55 |
static bool |
| 56 |
set_syntax(SerdSyntax* syntax, const char* name) |
| 57 |
{ |
| 58 |
if (!strcmp(name, "turtle")) { |
| 59 |
*syntax = SERD_TURTLE; |
| 60 |
} else if (!strcmp(name, "ntriples")) { |
| 61 |
*syntax = SERD_NTRIPLES; |
| 62 |
} else { |
| 63 |
SORDI_ERRORF("unknown syntax `%s'\n", name); |
| 64 |
return false; |
| 65 |
} |
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
int |
| 70 |
main(int argc, char** argv) |
| 71 |
{ |
| 72 |
if (argc < 2) { |
| 73 |
return print_usage(argv[0], true); |
| 74 |
} |
| 75 |
|
| 76 |
FILE* in_fd = NULL; |
| 77 |
SerdSyntax input_syntax = SERD_TURTLE; |
| 78 |
SerdSyntax output_syntax = SERD_NTRIPLES; |
| 79 |
bool from_file = true; |
| 80 |
const uint8_t* in_name = NULL; |
| 81 |
int a = 1; |
| 82 |
for (; a < argc && argv[a][0] == '-'; ++a) { |
| 83 |
if (argv[a][1] == '\0') { |
| 84 |
in_name = (const uint8_t*)"(stdin)"; |
| 85 |
in_fd = stdin; |
| 86 |
break; |
| 87 |
} else if (argv[a][1] == 'h') { |
| 88 |
return print_usage(argv[0], false); |
| 89 |
} else if (argv[a][1] == 'v') { |
| 90 |
return print_version(); |
| 91 |
} else if (argv[a][1] == 's') { |
| 92 |
in_name = (const uint8_t*)"(string)"; |
| 93 |
from_file = false; |
| 94 |
++a; |
| 95 |
break; |
| 96 |
} else if (argv[a][1] == 'i') { |
| 97 |
if (++a == argc) { |
| 98 |
SORDI_ERROR("option requires an argument -- 'i'\n\n"); |
| 99 |
return print_usage(argv[0], true); |
| 100 |
} |
| 101 |
if (!set_syntax(&input_syntax, argv[a])) { |
| 102 |
return print_usage(argv[0], true); |
| 103 |
} |
| 104 |
} else if (argv[a][1] == 'o') { |
| 105 |
if (++a == argc) { |
| 106 |
SORDI_ERROR("option requires an argument -- 'o'\n\n"); |
| 107 |
return print_usage(argv[0], true); |
| 108 |
} |
| 109 |
if (!set_syntax(&output_syntax, argv[a])) { |
| 110 |
return print_usage(argv[0], true); |
| 111 |
} |
| 112 |
} else { |
| 113 |
SORDI_ERRORF("invalid option -- '%s'\n", argv[a] + 1); |
| 114 |
return print_usage(argv[0], true); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if (a == argc) { |
| 119 |
SORDI_ERROR("missing input\n"); |
| 120 |
return print_usage(argv[0], true); |
| 121 |
} |
| 122 |
|
| 123 |
uint8_t* input_path = NULL; |
| 124 |
const uint8_t* input = (const uint8_t*)argv[a++]; |
| 125 |
if (from_file) { |
| 126 |
in_name = in_name ? in_name : input; |
| 127 |
if (!in_fd) { |
| 128 |
if (!strncmp((const char*)input, "file:", 5)) { |
| 129 |
input_path = serd_file_uri_parse(input, NULL); |
| 130 |
input = input_path; |
| 131 |
} |
| 132 |
if (!input || !(in_fd = fopen((const char*)input, "rb"))) { |
| 133 |
return 1; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
SerdURI base_uri = SERD_URI_NULL; |
| 139 |
SerdNode base = SERD_NODE_NULL; |
| 140 |
if (a < argc) { // Base URI given on command line |
| 141 |
const uint8_t* const base_uri_string = (const uint8_t*)argv[a]; |
| 142 |
base = serd_node_new_uri_from_string(base_uri_string, NULL, &base_uri); |
| 143 |
} else if (from_file && in_fd != stdin) { // Use input file URI |
| 144 |
char* const abs_path = zix_canonical_path(NULL, (const char*)input); |
| 145 |
base = |
| 146 |
serd_node_new_file_uri((const uint8_t*)abs_path, NULL, &base_uri, true); |
| 147 |
zix_free(NULL, abs_path); |
| 148 |
} |
| 149 |
|
| 150 |
SordWorld* world = sord_world_new(); |
| 151 |
SordModel* sord = sord_new(world, SORD_SPO | SORD_OPS, false); |
| 152 |
SerdEnv* env = serd_env_new(&base); |
| 153 |
SerdReader* reader = sord_new_reader(sord, env, input_syntax, NULL); |
| 154 |
|
| 155 |
SerdStatus status = (from_file) |
| 156 |
? serd_reader_read_file_handle(reader, in_fd, in_name) |
| 157 |
: serd_reader_read_string(reader, input); |
| 158 |
|
| 159 |
serd_reader_free(reader); |
| 160 |
|
| 161 |
FILE* out_fd = stdout; |
| 162 |
SerdEnv* write_env = serd_env_new(&base); |
| 163 |
|
| 164 |
int output_style = SERD_STYLE_RESOLVED; |
| 165 |
if (output_syntax == SERD_NTRIPLES) { |
| 166 |
output_style |= SERD_STYLE_ASCII; |
| 167 |
} else { |
| 168 |
output_style |= SERD_STYLE_CURIED | SERD_STYLE_ABBREVIATED; |
| 169 |
} |
| 170 |
|
| 171 |
SerdWriter* writer = serd_writer_new(output_syntax, |
| 172 |
(SerdStyle)output_style, |
| 173 |
write_env, |
| 174 |
&base_uri, |
| 175 |
serd_file_sink, |
| 176 |
stdout); |
| 177 |
|
| 178 |
// Write @prefix directives |
| 179 |
serd_env_foreach(env, (SerdPrefixSink)serd_writer_set_prefix, writer); |
| 180 |
|
| 181 |
// Write statements |
| 182 |
sord_write(sord, writer, NULL); |
| 183 |
|
| 184 |
serd_writer_finish(writer); |
| 185 |
serd_writer_free(writer); |
| 186 |
|
| 187 |
serd_env_free(env); |
| 188 |
serd_env_free(write_env); |
| 189 |
serd_node_free(&base); |
| 190 |
free(input_path); |
| 191 |
|
| 192 |
sord_free(sord); |
| 193 |
sord_world_free(world); |
| 194 |
|
| 195 |
if (from_file) { |
| 196 |
fclose(in_fd); |
| 197 |
} |
| 198 |
|
| 199 |
if (fclose(out_fd)) { |
| 200 |
perror("sordi: write error"); |
| 201 |
status = SERD_ERR_UNKNOWN; |
| 202 |
} |
| 203 |
|
| 204 |
return (status > SERD_FAILURE) ? 1 : 0; |
| 205 |
} |