| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata, |
| 4 |
the dvipdfmx project team. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program; if not, write to the Free Software |
| 18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifdef HAVE_CONFIG_H |
| 22 |
#include <config.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <ctype.h> |
| 28 |
|
| 29 |
#include "system.h" |
| 30 |
#include "error.h" |
| 31 |
#include "mem.h" |
| 32 |
#include "mfileio.h" |
| 33 |
|
| 34 |
#include "otl_opt.h" |
| 35 |
|
| 36 |
struct bt_node { |
| 37 |
int flag; |
| 38 |
|
| 39 |
struct bt_node *left; |
| 40 |
struct bt_node *right; |
| 41 |
|
| 42 |
char data[4]; |
| 43 |
}; |
| 44 |
|
| 45 |
#define FLAG_NOT (1 << 0) |
| 46 |
#define FLAG_AND (1 << 1) |
| 47 |
|
| 48 |
static int match_expr (struct bt_node *expr, const char *key); |
| 49 |
static int |
| 50 |
match_expr (struct bt_node *expr, const char *key) |
| 51 |
{ |
| 52 |
int retval = 1; |
| 53 |
int i; |
| 54 |
|
| 55 |
if (expr) { |
| 56 |
if (!expr->left && !expr->right) { |
| 57 |
for (i = 0; i < 4; i++) { |
| 58 |
if (expr->data[i] != '?' && |
| 59 |
expr->data[i] != key[i]) { |
| 60 |
retval = 0; |
| 61 |
break; |
| 62 |
} |
| 63 |
} |
| 64 |
} else { |
| 65 |
if (expr->left) { |
| 66 |
retval = match_expr(expr->left, key); |
| 67 |
} |
| 68 |
if (expr->right) { |
| 69 |
if (retval && (expr->flag & FLAG_AND)) /* and */ |
| 70 |
retval &= match_expr(expr->right, key); |
| 71 |
else if (!retval && !(expr->flag & FLAG_AND)) /* or */ |
| 72 |
retval = match_expr(expr->right, key); |
| 73 |
} |
| 74 |
} |
| 75 |
if (expr->flag & FLAG_NOT) /* not */ |
| 76 |
retval = retval ? 0 : 1; |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
return retval; |
| 81 |
} |
| 82 |
|
| 83 |
static struct bt_node * |
| 84 |
bt_new_tree (void) |
| 85 |
{ |
| 86 |
struct bt_node *expr; |
| 87 |
|
| 88 |
expr = NEW(1, struct bt_node); |
| 89 |
expr->flag = 0; |
| 90 |
expr->left = NULL; |
| 91 |
expr->right = NULL; |
| 92 |
memset(expr->data, 0, 4); |
| 93 |
|
| 94 |
return expr; |
| 95 |
} |
| 96 |
|
| 97 |
static void bt_release_tree (struct bt_node *tree); |
| 98 |
|
| 99 |
static void |
| 100 |
bt_release_tree (struct bt_node *tree) |
| 101 |
{ |
| 102 |
if (tree) { |
| 103 |
if (tree->left) |
| 104 |
bt_release_tree(tree->left); |
| 105 |
if (tree->right) |
| 106 |
bt_release_tree(tree->right); |
| 107 |
RELEASE(tree); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
static struct bt_node * |
| 112 |
parse_expr (const char **pp, const char *endptr) |
| 113 |
{ |
| 114 |
struct bt_node *root, *curr; |
| 115 |
|
| 116 |
if (*pp >= endptr) |
| 117 |
return NULL; |
| 118 |
|
| 119 |
root = curr = bt_new_tree(); |
| 120 |
while (*pp < endptr) { |
| 121 |
switch (**pp) { |
| 122 |
case '!': |
| 123 |
if (curr->flag & 2) |
| 124 |
curr->flag &= ~FLAG_NOT; |
| 125 |
else |
| 126 |
curr->flag |= FLAG_NOT; |
| 127 |
(*pp)++; |
| 128 |
break; |
| 129 |
case '(': |
| 130 |
(*pp)++; |
| 131 |
if (*pp < endptr) { |
| 132 |
struct bt_node *expr; |
| 133 |
|
| 134 |
expr = parse_expr(pp, endptr); |
| 135 |
if (!expr) { |
| 136 |
WARN("Syntax error: %s\n", *pp); |
| 137 |
return NULL; |
| 138 |
} |
| 139 |
if (**pp != ')') { |
| 140 |
WARN("Syntax error: Unbalanced ()\n"); |
| 141 |
return NULL; |
| 142 |
} |
| 143 |
curr->left = expr->left; |
| 144 |
curr->right = expr->right; |
| 145 |
memcpy(curr->data, expr->data, 4); |
| 146 |
|
| 147 |
RELEASE(expr); |
| 148 |
} else { |
| 149 |
WARN("Syntax error: Unbalanced ()\n"); |
| 150 |
bt_release_tree(root); |
| 151 |
return NULL; |
| 152 |
} |
| 153 |
(*pp)++; |
| 154 |
break; |
| 155 |
case ')': |
| 156 |
return root; |
| 157 |
case '|': case '&': |
| 158 |
if (*pp >= endptr) { |
| 159 |
WARN("Syntax error: %s\n", *pp); |
| 160 |
bt_release_tree(root); |
| 161 |
return NULL; |
| 162 |
} else { |
| 163 |
struct bt_node *tmp; |
| 164 |
|
| 165 |
tmp = bt_new_tree(); |
| 166 |
tmp->left = root; |
| 167 |
tmp->right = curr = bt_new_tree(); |
| 168 |
if (**pp == '&') |
| 169 |
tmp->flag = 1; |
| 170 |
else |
| 171 |
tmp->flag = 0; |
| 172 |
root = tmp; |
| 173 |
} |
| 174 |
(*pp)++; |
| 175 |
break; |
| 176 |
case '*': |
| 177 |
memset(curr->data, '?', 4); |
| 178 |
(*pp)++; |
| 179 |
break; |
| 180 |
default: |
| 181 |
if (*pp + 4 <= endptr) { |
| 182 |
int i; |
| 183 |
|
| 184 |
for (i = 0; i < 4; i++) { |
| 185 |
if (**pp == ' ' || **pp == '?' || |
| 186 |
isalpha((unsigned char)**pp) || isdigit((unsigned char)**pp)) |
| 187 |
curr->data[i] = **pp; |
| 188 |
else if (**pp == '_') |
| 189 |
curr->data[i] = ' '; |
| 190 |
else { |
| 191 |
WARN("Invalid char in tag: %c\n", **pp); |
| 192 |
bt_release_tree(root); |
| 193 |
return NULL; |
| 194 |
} |
| 195 |
(*pp)++; |
| 196 |
} |
| 197 |
} else { |
| 198 |
WARN("Syntax error: %s\n", *pp); |
| 199 |
bt_release_tree(root); |
| 200 |
return NULL; |
| 201 |
} |
| 202 |
break; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return root; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
struct otl_opt |
| 211 |
{ |
| 212 |
struct bt_node *rule; |
| 213 |
}; |
| 214 |
|
| 215 |
otl_opt * |
| 216 |
otl_new_opt (void) |
| 217 |
{ |
| 218 |
struct otl_opt *opt; |
| 219 |
|
| 220 |
opt = NEW(1, struct otl_opt); |
| 221 |
opt->rule = NULL; |
| 222 |
|
| 223 |
return (otl_opt *) opt; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
void |
| 228 |
otl_release_opt (otl_opt *opt) |
| 229 |
{ |
| 230 |
if (opt->rule) { |
| 231 |
bt_release_tree(opt->rule); |
| 232 |
} |
| 233 |
opt->rule = NULL; |
| 234 |
RELEASE(opt); |
| 235 |
} |
| 236 |
|
| 237 |
#if 0 |
| 238 |
struct lv_range |
| 239 |
{ |
| 240 |
int start, end; |
| 241 |
}; |
| 242 |
|
| 243 |
struct uc_coverage |
| 244 |
{ |
| 245 |
int count; |
| 246 |
struct lv_range *ranges; |
| 247 |
}; |
| 248 |
|
| 249 |
static inline int |
| 250 |
range_cmp (const void *v1, const void *v2) |
| 251 |
{ |
| 252 |
struct lv_range *sv1, *sv2; |
| 253 |
|
| 254 |
sv1 = (struct lv_range *) v1; |
| 255 |
sv2 = (struct lv_range *) v2; |
| 256 |
|
| 257 |
if (sv1->start < sv2->start) |
| 258 |
return -1; |
| 259 |
else |
| 260 |
return 1; |
| 261 |
|
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
static inline int |
| 266 |
range_overlap (const void *v1, const void *v2) |
| 267 |
{ |
| 268 |
struct lv_range *sv1, *sv2; |
| 269 |
|
| 270 |
sv1 = (struct lv_range *) v1; |
| 271 |
sv2 = (struct lv_range *) v2; |
| 272 |
|
| 273 |
/* Must be first sort in increasing start order */ |
| 274 |
if (sv1->end >= sv2->start) |
| 275 |
return 0; |
| 276 |
else if (sv1->end < sv2->start) |
| 277 |
return -1; |
| 278 |
|
| 279 |
return 1; |
| 280 |
} |
| 281 |
|
| 282 |
static void |
| 283 |
check_uc_coverage (struct uc_coverage *coverage) |
| 284 |
{ |
| 285 |
struct lv_range *r1, *r2; |
| 286 |
int i; |
| 287 |
|
| 288 |
for (i = 0; i < coverage->count; i++) { |
| 289 |
r1 = &coverage->ranges[i]; |
| 290 |
r2 = bsearch(r1, coverage->ranges, |
| 291 |
coverage->count, sizeof(struct lv_range), |
| 292 |
range_overlap); |
| 293 |
if (r2 && r1 != r2) { |
| 294 |
WARN("Overlapping Unicode range found:"); |
| 295 |
WARN("[%x-%x], [%x-%x] ==> [%x-%x]", |
| 296 |
r1->start, r1->end, r2->start, r2->end, |
| 297 |
MIN(r1->start, r2->start), MAX(r1->end, r2->end)); |
| 298 |
r2->start = MIN(r1->start, r2->start); |
| 299 |
r2->end = MAX(r1->end , r2->end ); |
| 300 |
if (i < coverage->count - 1) { |
| 301 |
memmove(&coverage->ranges[i], &coverage->ranges[i+1], |
| 302 |
(coverage->count - i - 1) * sizeof(struct lv_range)); |
| 303 |
coverage->count -= 1; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
/* ... */ |
| 308 |
if (coverage->count == 0) { |
| 309 |
RELEASE(coverage->ranges); |
| 310 |
coverage->ranges = NULL; |
| 311 |
} |
| 312 |
} |
| 313 |
#endif |
| 314 |
|
| 315 |
int |
| 316 |
otl_parse_optstring (otl_opt *opt, const char *optstr) |
| 317 |
{ |
| 318 |
const char *p, *endptr; |
| 319 |
|
| 320 |
ASSERT(opt); |
| 321 |
|
| 322 |
if (optstr) { |
| 323 |
p = optstr; |
| 324 |
endptr = p + strlen(optstr); |
| 325 |
opt->rule = parse_expr(&p, endptr); |
| 326 |
} |
| 327 |
|
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
int |
| 332 |
otl_match_optrule (otl_opt *opt, const char *tag) |
| 333 |
{ |
| 334 |
ASSERT(tag); |
| 335 |
|
| 336 |
if (!opt || !opt->rule) |
| 337 |
return 1; |
| 338 |
|
| 339 |
return match_expr(opt->rule, tag); |
| 340 |
} |