| 1 |
/* This file is part of GNU Dico. |
| 2 |
Copyright (C) 2012-2024 Sergey Poznyakoff |
| 3 |
|
| 4 |
GNU Dico is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 3, or (at your option) |
| 7 |
any later version. |
| 8 |
|
| 9 |
GNU Dico is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with GNU Dico. If not, see <http://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
#include <config.h> |
| 18 |
#include <dico.h> |
| 19 |
#include <unistd.h> |
| 20 |
#include <sys/stat.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <sysexits.h> |
| 25 |
#include <string.h> |
| 26 |
#include "gcide.h" |
| 27 |
|
| 28 |
static void |
| 29 |
usage(FILE *fp) |
| 30 |
{ |
| 31 |
fprintf(fp, "usage: %s [-debug] [-struct] [-nopr] [-help] FILE [OFF SIZE]\n", dico_program_name); |
| 32 |
} |
| 33 |
|
| 34 |
struct output_closure { |
| 35 |
FILE *stream; |
| 36 |
int flags; |
| 37 |
int level; |
| 38 |
}; |
| 39 |
|
| 40 |
static int |
| 41 |
print_tag(int end, struct gcide_tag *tag, void *data) |
| 42 |
{ |
| 43 |
size_t i; |
| 44 |
struct output_closure *clos = data; |
| 45 |
|
| 46 |
if (end) { |
| 47 |
clos->level--; |
| 48 |
printf("%*.*s", 2*clos->level,2*clos->level, ""); |
| 49 |
printf("END"); |
| 50 |
if (tag->v.tag.tag_parmc) |
| 51 |
printf(" %s", tag->tag_name); |
| 52 |
putchar('\n'); |
| 53 |
return 0; |
| 54 |
} |
| 55 |
printf("%*.*s", 2*clos->level,2*clos->level, ""); |
| 56 |
switch (tag->tag_type) { |
| 57 |
case gcide_content_top: |
| 58 |
printf("BEGIN\n"); |
| 59 |
clos->level++; |
| 60 |
break; |
| 61 |
case gcide_content_text: |
| 62 |
printf("TEXT"); |
| 63 |
printf(":\n%s\n%*.*sENDTEXT", tag->v.text, |
| 64 |
2*clos->level, 2*clos->level, ""); |
| 65 |
putchar('\n'); |
| 66 |
break; |
| 67 |
case gcide_content_tag: |
| 68 |
printf("BEGIN"); |
| 69 |
for (i = 0; i < tag->v.tag.tag_parmc; i++) |
| 70 |
printf(" %s", tag->v.tag.tag_parmv[i]); |
| 71 |
putchar('\n'); |
| 72 |
clos->level++; |
| 73 |
break; |
| 74 |
case gcide_content_nl: |
| 75 |
printf("NL\n"); |
| 76 |
break; |
| 77 |
case gcide_content_br: |
| 78 |
printf("BR\n"); |
| 79 |
} |
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
#define GCIDE_NOPR 0x0000001 |
| 84 |
#define GOF_INIT 0x0001000 |
| 85 |
#define GOF_IGNORE 0x0002000 |
| 86 |
|
| 87 |
static int |
| 88 |
print_text(int end, struct gcide_tag *tag, void *data) |
| 89 |
{ |
| 90 |
struct output_closure *clos = data; |
| 91 |
static char *quote[2] = { "``", "''" }; |
| 92 |
static char *ref[2] = { "{" , "}" }; |
| 93 |
|
| 94 |
switch (tag->tag_type) { |
| 95 |
case gcide_content_top: |
| 96 |
break; |
| 97 |
case gcide_content_text: |
| 98 |
if (clos->flags & GOF_IGNORE) |
| 99 |
break; |
| 100 |
fprintf(clos->stream, "%s", tag->v.text); |
| 101 |
break; |
| 102 |
case gcide_content_tag: |
| 103 |
if (tag->v.tag.tag_parmc) { |
| 104 |
if (end) { |
| 105 |
if (strcmp(tag->tag_name, "pr") == 0 && |
| 106 |
clos->flags & GCIDE_NOPR) |
| 107 |
clos->flags &= ~GOF_IGNORE; |
| 108 |
else if (clos->flags & GOF_IGNORE) |
| 109 |
break; |
| 110 |
else if (strcmp(tag->tag_name, "as") == 0) |
| 111 |
fprintf(clos->stream, "%s", quote[1]); |
| 112 |
else if (strcmp(tag->tag_name, "er") == 0) |
| 113 |
fprintf(clos->stream, "%s", ref[1]); |
| 114 |
else if (strcmp(tag->tag_name, "source") == 0) |
| 115 |
fputc(']', clos->stream); |
| 116 |
} else { |
| 117 |
if (strcmp(tag->tag_name, "pr") == 0 && |
| 118 |
clos->flags & GCIDE_NOPR) |
| 119 |
clos->flags |= GOF_IGNORE; |
| 120 |
else if (clos->flags & GOF_IGNORE) |
| 121 |
break; |
| 122 |
|
| 123 |
if (gcide_is_block_tag(tag)) { |
| 124 |
if (clos->flags & GOF_INIT) |
| 125 |
fputc('\n', clos->stream); |
| 126 |
else |
| 127 |
clos->flags |= GOF_INIT; |
| 128 |
} |
| 129 |
|
| 130 |
if (strcmp(tag->tag_name, "as") == 0) |
| 131 |
fprintf(clos->stream, "%s", quote[0]); |
| 132 |
else if (strcmp(tag->tag_name, "er") == 0) |
| 133 |
fprintf(clos->stream, "%s", ref[0]); |
| 134 |
else if (strcmp(tag->tag_name, "source") == 0) |
| 135 |
fputc('[', clos->stream); |
| 136 |
} |
| 137 |
} |
| 138 |
break; |
| 139 |
case gcide_content_nl: |
| 140 |
fputc(' ', clos->stream); |
| 141 |
break; |
| 142 |
case gcide_content_br: |
| 143 |
fputc('\n', clos->stream); |
| 144 |
break; |
| 145 |
} |
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
int |
| 150 |
main(int argc, char **argv) |
| 151 |
{ |
| 152 |
char *file; |
| 153 |
unsigned long offset = 0; |
| 154 |
unsigned long size = 0; |
| 155 |
FILE *fp; |
| 156 |
char *textbuf; |
| 157 |
struct gcide_parse_tree *tree; |
| 158 |
struct output_closure clos; |
| 159 |
int show_struct = 0; |
| 160 |
int check_only = 0; |
| 161 |
int dbglex = 0; |
| 162 |
struct gcide_locus locus; |
| 163 |
|
| 164 |
dico_set_program_name(argv[0]); |
| 165 |
clos.flags = 0; |
| 166 |
clos.stream = stdout; |
| 167 |
|
| 168 |
while (--argc) { |
| 169 |
char *arg = *++argv; |
| 170 |
|
| 171 |
if (strcmp(arg, "-debug") == 0) |
| 172 |
dbglex = 1; |
| 173 |
else if (strcmp(arg, "-h") == 0 || strcmp(arg, "-help") == 0) { |
| 174 |
usage(stdout); |
| 175 |
exit(0); |
| 176 |
} else if (strcmp(arg, "-struct") == 0) |
| 177 |
show_struct = 1; |
| 178 |
else if (strcmp(arg, "-nopr") == 0) |
| 179 |
clos.flags = GCIDE_NOPR; |
| 180 |
else if (strcmp(arg, "-c") == 0 || strcmp(arg, "--check") == 0) |
| 181 |
check_only = 1; |
| 182 |
else if (strcmp(arg, "--") == 0) { |
| 183 |
--argc; |
| 184 |
++argv; |
| 185 |
break; |
| 186 |
} else if (arg[0] == '-') { |
| 187 |
usage(stderr); |
| 188 |
exit(EX_USAGE); |
| 189 |
} else |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
if (argc == 0 || argc > 3) { |
| 194 |
usage(stderr); |
| 195 |
exit(EX_USAGE); |
| 196 |
} |
| 197 |
|
| 198 |
file = argv[0]; |
| 199 |
if (argc > 1) { |
| 200 |
offset = atoi(argv[1]); |
| 201 |
if (argc == 3) |
| 202 |
size = atoi(argv[2]); |
| 203 |
} |
| 204 |
|
| 205 |
if (size == 0) { |
| 206 |
struct stat st; |
| 207 |
if (stat(file, &st)) { |
| 208 |
dico_log(L_ERR, errno, "stat"); |
| 209 |
exit(EX_UNAVAILABLE); |
| 210 |
} |
| 211 |
if (st.st_size < offset) { |
| 212 |
dico_log(L_ERR, 0, "invalid offset"); |
| 213 |
exit(EX_UNAVAILABLE); |
| 214 |
} |
| 215 |
size = st.st_size - offset; |
| 216 |
} |
| 217 |
|
| 218 |
textbuf = malloc(size); |
| 219 |
if (!textbuf) { |
| 220 |
DICO_LOG_ERRNO(); |
| 221 |
exit(EX_UNAVAILABLE); |
| 222 |
} |
| 223 |
|
| 224 |
fp = fopen(file, "r"); |
| 225 |
if (!fp) { |
| 226 |
dico_log(L_ERR, errno, "cannot open file %s", file); |
| 227 |
exit(EX_UNAVAILABLE); |
| 228 |
} |
| 229 |
|
| 230 |
if (fseek(fp, offset, SEEK_SET)) { |
| 231 |
dico_log(L_ERR, errno, "%s", file); |
| 232 |
exit(EX_UNAVAILABLE); |
| 233 |
} |
| 234 |
|
| 235 |
if (fread(textbuf, size, 1, fp) != 1) { |
| 236 |
dico_log(L_ERR, errno, "%s: read error", file); |
| 237 |
exit(EX_UNAVAILABLE); |
| 238 |
} |
| 239 |
|
| 240 |
locus.file = file; |
| 241 |
locus.offset = offset; |
| 242 |
tree = gcide_markup_parse(textbuf, size, dbglex, &locus); |
| 243 |
if (!tree) |
| 244 |
exit(EX_UNAVAILABLE); |
| 245 |
|
| 246 |
if (!check_only) { |
| 247 |
clos.level = 0; |
| 248 |
gcide_parse_tree_inorder(tree, show_struct ? print_tag : print_text, |
| 249 |
&clos); |
| 250 |
if (!show_struct) |
| 251 |
fputc('\n', stdout); |
| 252 |
} |
| 253 |
|
| 254 |
gcide_parse_tree_free(tree); |
| 255 |
exit(0); |
| 256 |
} |