1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/* $Xorg: PRead.c,v 1.4 2001/02/09 02:05:58 xorgcvs Exp $ */
/*
Copyright 1996, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABIL-
ITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization from
The Open Group.
*/
#include "RxI.h"
#include <ctype.h>
/* Not null terminated string */
typedef struct {
char *ptr; /* beginning of string */
int length; /* length of string */
} NString;
#ifdef NETSCAPE_PLUGIN
/* utility function for netscape plugin where realloc is not available
* it's too bad we need both the old size and the new one...
*/
void *
_RxRealloc(void *p, size_t olds, size_t s)
{
void *np = Malloc(s);
if (np) {
memcpy(np, p, olds);
Free(p);
}
return np;
}
#endif
/* get next word and return trailing stream */
static char *
NextWord(char *stream, char *limit, NString *word)
{
/* skip leading whitespace */
while (isspace((int) *stream) && *stream && stream != limit)
stream++;
word->ptr = stream;
/* go to first whitespace */
while (!isspace((int) *stream) && *stream && stream != limit)
stream++;
word->length = stream - word->ptr;
return stream;
}
/* get next chunk of text, with possible quoted sections,
and return trailing stream */
static char *
NextChunk(char *stream, char *limit, NString *word)
{
/* skip leading whitespace */
while (isspace((int) *stream) && *stream && stream != limit)
stream++;
word->ptr = stream;
/* go to first whitespace or end of string */
while (!isspace((int) *stream) && *stream && stream != limit) {
if (*stream == '"' || *stream == '\'') {
char quote = *stream;
do
stream++;
while (*stream != quote && *stream && stream != limit);
if (*stream && stream != limit)
stream++;
break;
} else
stream++;
}
word->length = stream - word->ptr;
return stream;
}
/* get next SGML element "< ... >" and return next trailing stream */
static char *
NextElement(char *stream, NString *element)
{
/* look for opening bracket */
while (*stream != '<' && *stream)
stream++;
element->ptr = stream;
/* look for closing bracket */
while (*stream != '>' && *stream)
stream++;
element->length = stream - element->ptr;
return *stream ? stream + 1 : stream;
}
/* seek next PARAM element content: "<PARAM ... >" (delimiters excluded)
and return next trailing stream */
static char *
NextParam(char *stream, NString *param)
{
NString element, word;
do {
stream = NextElement(stream, &element);
if (element.length)
/* get element name */
(void) NextWord(element.ptr + 1, element.ptr + element.length - 1,
&word);
else {
/* no more elements stop here */
param->ptr = 0;
param->length = 0;
return stream;
}
/* check if it's a PARAM element */
} while(word.length != 5 && memcmp("PARAM", word.ptr, 5) != 0 && *stream);
param->ptr = word.ptr + word.length;
param->length = element.length - word.length - 1; /* delimiters excluded */
return stream;
}
/* return literal value as a Null terminated string,
removing possible quotes and extra whitespace */
static char *
GetLiteralValue(NString *literal)
{
char *ptr, *limit, *value, *vptr;
char quote;
int skip;
value = vptr = (char *)Malloc(literal->length + 1);
if (!value)
return 0;
ptr = literal->ptr;
limit = ptr + literal->length;
quote = (*ptr == '"' || *ptr == '\'') ? *ptr++ : '\0';
skip = 0;
do
if (isspace((int) *ptr)) {
if (!skip) {
*vptr++ = ' ';
skip = 1;
}
ptr++;
} else {
skip = 0;
*vptr++ = *ptr++;
}
while (*ptr != quote && ptr != limit);
*vptr = '\0';
return value;
}
/* parse PARAM tag content: " NAME=... VALUE=... " */
static int
ParseParam(NString *param, char **name_ret, char **value_ret)
{
NString word, name, value;
char *stream = param->ptr;
char *limit = param->ptr + param->length;
/* look for the name part */
do
stream = NextChunk(stream, limit, &word);
while (word.length < 5 && memcmp("NAME=", word.ptr, 5 != 0) && *stream);
if (stream == limit)
return 1;
name.ptr = word.ptr + 5;
name.length = word.length - 5;
*name_ret = GetLiteralValue(&name);
/* look for the value part */
do
stream = NextChunk(stream, limit, &word);
while (word.length < 6 && memcmp("VALUE=", word.ptr, 6) != 0 && *stream);
value.ptr = word.ptr + 6;
value.length = word.length - 6;
*value_ret = GetLiteralValue(&value);
return 0;
}
/* how much we make a params array bigger every time we reach the limit */
#define PARAMSINC 10
/* read an RX document stream and augment the given list of arguments */
int
RxReadParams(char *stream,
char **argn_ret[], char **argv_ret[], int *argc_ret)
{
char **argv, **argn;
int argc, n;
NString param;
char *name, *value;
int status;
status = 0;
argc = n = 0;
argn = argv = NULL;
if (stream != NULL) {
do {
stream = NextParam(stream, ¶m);
if (param.length != 0 && ParseParam(¶m, &name, &value) == 0) {
argc++;
if (n == 0) { /* alloc first block */
n = PARAMSINC;
argn = (char **)Malloc(sizeof(char *) * n);
if (!argn)
return 1;
argv = (char **)Malloc(sizeof(char *) * n);
if (!argv) {
Free(argn);
return 1;
}
}
if (argc % PARAMSINC == 0) { /* we need to add a block */
n += PARAMSINC;
argn = (char **)
Realloc(argn, sizeof(char *) * argc, sizeof(char *) * n);
argv = (char **)
Realloc(argv, sizeof(char *) * argc, sizeof(char *) * n);
if (!argn || !argv) {
argc--;
status = 1;
break;
}
}
argn[argc - 1] = name;
argv[argc - 1] = value;
}
} while (*stream);
}
*argn_ret = argn;
*argv_ret = argv;
*argc_ret = argc;
return status;
}
|