~ubuntu-branches/ubuntu/utopic/texlive-bin/utopic

« back to all changes in this revision

Viewing changes to texk/dvipdfmx/dvipdfmx-20110311/src/otl_opt.c

  • Committer: Package Import Robot
  • Author(s): Norbert Preining
  • Date: 2012-05-07 10:47:49 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20120507104749-p00ot5sajjbkp1hp
Tags: 2011.20120507-1
* new upstream checkout: uptex 1.10
* drop patches for config file inclusion in (x)dvipdfmx, included upstream
* add man page for etex
* include pmpost patches and build it
* adapt/unfuzzify patches for current sources
* disable mtx building, we have prepmx package in Debian

Show diffs side-by-side

added added

removed removed

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