Debian Codegraph @

texk/dvipdfm-x/pst.c

raw
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 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
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
26
27 #include <string.h>
28 #include <ctype.h>
29
30 #include "system.h"
31 #include "mem.h"
32 #include "error.h"
33 #include "dpxutil.h"
34 #include "pst_obj.h"
35 #include "pst.h"
36
37
38 #define TYPE_CHECK(o, t) do { \
39 if ((o) == NULL || pst_type_of((o)) != (t)) \
40 ERROR("typecheck: object %p not of type %d.", (o), (t)); \
41 } while (0)
42
43 static pst_obj *
44 pst_parse_any (unsigned char **inbuf, unsigned char *inbufend)
45 {
46 unsigned char *data;
47 unsigned char *cur = *inbuf;
48 unsigned int len;
49
50 while (cur < inbufend && !PST_TOKEN_END(cur, inbufend))
51 cur++;
52
53 len = cur - (*inbuf);
54 data = NEW(len+1, unsigned char);
55 memcpy(data, *inbuf, len);
56 data[len] = '\0';
57
58 *inbuf = cur;
59 return pst_new_obj(PST_TYPE_UNKNOWN, data);
60 }
61
62 static void
63 skip_line (unsigned char **inbuf, unsigned char *inbufend)
64 {
65 while (*inbuf < inbufend && **inbuf != '\n' && **inbuf != '\r')
66 (*inbuf)++;
67 if (*inbuf < inbufend && **inbuf == '\r')
68 (*inbuf)++;
69 if (*inbuf < inbufend && **inbuf == '\n')
70 (*inbuf)++;
71 }
72
73 static void
74 skip_comments (unsigned char **inbuf, unsigned char *inbufend)
75 {
76 while (*inbuf < inbufend && **inbuf == '%') {
77 skip_line(inbuf, inbufend);
78 skip_white_spaces(inbuf, inbufend);
79 }
80 }
81
82 #if 0
83 static pst_obj *
84 pst_parse_comment (unsigned char **inbuf, unsigned char *inbufend)
85 {
86 unsigned char *data;
87 unsigned char *cur = *inbuf;
88 unsigned int len;
89
90 if (*cur != '%')
91 return NULL;
92
93 while (cur < inbufend && *cur != '\n' && *cur != '\r')
94 cur++;
95 len = cur - (*inbuf);
96 data = NEW(len+1, unsigned char);
97 memcpy(data, *inbuf, len);
98 data[len] = '\0';
99
100 *inbuf = cur;
101 return pst_new_obj(PST_TYPE_UNKNOWN, data);
102 }
103 #endif
104
105 /* NOTE: the input buffer must be null-terminated, i.e., *inbufend == 0 */
106 pst_obj *
107 pst_get_token (unsigned char **inbuf, unsigned char *inbufend)
108 {
109 pst_obj *obj = NULL;
110 unsigned char c;
111
112 ASSERT(*inbuf <= inbufend && !*inbufend);
113
114 skip_white_spaces(inbuf, inbufend);
115 skip_comments(inbuf, inbufend);
116 if (*inbuf >= inbufend)
117 return NULL;
118 c = **inbuf;
119 switch (c) {
120 #if 0
121 case '%':
122 obj = pst_parse_comment(inbuf, inbufend);
123 break;
124 #endif
125 case '/':
126 obj = pst_parse_name(inbuf, inbufend);
127 break;
128 case '[': case '{': /* This is wrong */
129 obj = pst_new_mark();
130 (*inbuf)++;
131 break;
132 case '<':
133 if (*inbuf + 1 >= inbufend)
134 return NULL;
135 c = *(*inbuf+1);
136 if (c == '<') {
137 obj = pst_new_mark();
138 *inbuf += 2;
139 } else if (isxdigit(c))
140 obj = pst_parse_string(inbuf, inbufend);
141 else if (c == '~') /* ASCII85 */
142 obj = pst_parse_string(inbuf, inbufend);
143 break;
144 case '(':
145 obj = pst_parse_string(inbuf, inbufend);
146 break;
147 case '>':
148 if (*inbuf + 1 >= inbufend || *(*inbuf+1) != '>') {
149 ERROR("Unexpected end of ASCII hex string marker.");
150 } else {
151 char *mark;
152
153 mark = NEW(3, char);
154 mark[0] = '>'; mark[1] = '>'; mark[2] = '\0';
155 obj = pst_new_obj(PST_TYPE_UNKNOWN, mark);
156 (*inbuf) += 2;
157 }
158 break;
159 case ']': case '}':
160 {
161 char *mark;
162
163 mark = NEW(2, char);
164 mark[0] = c; mark[1] = '\0';
165 obj = pst_new_obj(PST_TYPE_UNKNOWN, mark);
166 (*inbuf)++;
167 }
168 break;
169 default:
170 if (c == 't' || c == 'f')
171 obj = pst_parse_boolean(inbuf, inbufend);
172 else if (c == 'n')
173 obj = pst_parse_null(inbuf, inbufend);
174 else if (c == '+' || c == '-' || isdigit(c) || c == '.')
175 obj = pst_parse_number(inbuf, inbufend);
176 break;
177 }
178
179 if (!obj) {
180 obj = pst_parse_any(inbuf, inbufend);
181 }
182
183 return obj;
184 }