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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2008, Gabriel Dos Reis.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
- Neither the name of The Numerical Algorithms Group Ltd. nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _MACRO_C
#include "openaxiom-c-macros.h"
#include "debug.h"
#include "halloc.h"
#include "sockio.h"
#include "parse.h"
#include "hyper.h"
#include "lex.h"
#include "all_hyper_proto.H1"
/* #define DEBUG 1 */
/*
* This routine keeps scanning until it reaches it pops off 1 more
* right brace then left brace
*/
void
scan_HyperDoc(void)
{
HDWindow *twin = gWindow;
int ret_val;
int number_of_left_braces = 1;
gWindow = NULL;
while (number_of_left_braces) {
ret_val = get_token();
if (ret_val == EOF && number_of_left_braces) {
fprintf(stderr, "Scan_Hypertex: Unexpected End of File\n");
longjmp(jmpbuf, 1);
}
switch (token.type) {
case Page:
fprintf(stderr, "scan_HyperDoc: Unexpected Page Declaration\n");
break;
case NewCommand:
fprintf(stderr, "scan_HyperDoc: Unexpected Macro Declaration\n");
break;
case Lbrace:
number_of_left_braces++;
break;
case Endpatch:
case Rbrace:
number_of_left_braces--;
break;
default:
break;
}
}
gWindow = twin;
}
int
number(char *str)
{
char *t = str;
while (*t)
if (!isdigit(*t++))
return 0;
return 1;
}
/* Parse a given macro given the pointer to the unlaoded macro ** */
static char *
load_macro(MacroStore *macro)
{
int ret_val;
long start_fpos;
int size = 0;
char *trace;
char *macro_buff;
save_scanner_state();
cfile = find_fp(macro->fpos);
init_scanner();
/** First thing I should do is make sure that the name is correct ***/
get_expected_token(NewCommand);
get_expected_token(Lbrace);
get_expected_token(Macro);
if (strcmp(token.id, macro->name)) {
/** WOW, Somehow I had the location of the wrong macro **/
fprintf(stderr, "Expected macro name %s got insted %s in load_macro\n",
macro->name, token.id);
longjmp(jmpbuf, 1);
}
get_expected_token(Rbrace);
/** Next I should check to see if I have any parameters **/
get_token();
if (token.type == Lsquarebrace) {
/** The person is telling me the number of macros he is going to use **/
get_expected_token(Word);
if (!number(token.id)) {
fprintf(stderr, "load_macro: Expected A Value Instead Got %s\n",
token.id);
longjmp(jmpbuf, 1);
}
/** if it is a number, then I should store it in the parameter number
member of the macro structure **/
macro->number_parameters = atoi(token.id);
#ifdef DEBUG
fprintf(stderr,
"The number of parameters is %d\n", macro->number_parameters);
#endif
get_expected_token(Rsquarebrace);
get_token();
}
else
macro->number_parameters = 0;
/*** Now I should be able to check the token, and insure that I have read
a leftbrace, then the string will follow ****/
if (token.type != Lbrace) {
/** The macro is not in a group, uh oh **/
fprintf(stderr, "load_macro:Expected a Left Brace got type %d\n",
token.type);
longjmp(jmpbuf, 1);
}
start_fpos = fpos;
scan_HyperDoc();
ret_val = fseek(cfile, macro->fpos.pos + start_fpos, 0);
size = fpos - start_fpos;
macro_buff = (char *) halloc((size + 1) * sizeof(char), "Macro_buf");
for (size = 0, trace = macro_buff; size < fpos - (start_fpos) - 1; size++)
*trace++ = getc(cfile);
*trace = '\0';
macro->loaded = 1;
restore_scanner_state();
return macro_buff;
}
/** Here are the functions and declarations for the parameter stack **/
ParameterList parameters = NULL;
ParameterList
init_parameter_elem(int number)
{
ParameterList new;
int count;
/** allocate the space neeeded **/
new = (ParameterList) halloc(sizeof(struct parameter_list_type),
"ParameterList");
/** now allocate the memeory for the pointers to the parameters **/
if (number) {
new->list = (char **) halloc(number * sizeof(char *), "Parameter List");
/** initialize my pointers **/
for (count = 0; count < number; count++)
(new->list)[count] = NULL;
}
new->number = number;
return new;
}
int
push_parameters(ParameterList new)
{
if (new == NULL) {
fprintf(stderr, "Tried pushing a null list onto the parameter stack\n");
longjmp(jmpbuf, 1);
}
new->next = parameters;
parameters = new;
return 1;
}
int
pop_parameters(void)
{
/** Simply pops the top of the parameter list, being good and freeing
all the memory **/
ParameterList old;
int count;
if (!parameters) {
return 0;
}
old = parameters;
parameters = old->next;
/** Free the parameter text and pointers **/
if (old->number >0) {
for (count = 0; count < old->number; count++)
if ( (old->list)[count] ) free((char *) (old->list)[count]);
free(old->list);
}
free(old); /** free the parameter **/
return 1;
}
int
parse_macro(void)
{
/*
* This routine loads a macro if needed, and then parses it from the
* string
*/
MacroStore *macro;
int s;
curr_node->type = Macro;
curr_node->space = token.id[-1];
curr_node->next = alloc_node();
curr_node = curr_node->next;
macro = (MacroStore *) hash_find(gWindow->fMacroHashTable, token.id);
if (macro != NULL) {
if (!macro->loaded)
macro->macro_string = load_macro(macro);
get_parameter_strings(macro->number_parameters, macro->name);
parse_from_string(macro->macro_string);
if (gEndedPage) {
s = curr_node->type;
curr_node->type = Endmacro;
curr_node->next = alloc_node();
curr_node = curr_node->next;
curr_node->type = s;
}
else
curr_node->type = Endmacro;
if (pop_parameters())
return 1;
else {
fprintf(stderr,
"parse_macro: Tried to pop an empty paramter stack\n");
longjmp(jmpbuf, 1);
}
}
else {
fprintf(stderr, "parse_macro: Unknown keyword %s\n", token.id);
longjmp(jmpbuf, 1);
}
}
#define numeric(c) ((c >= '0' && c <= '9')?1:0)
static void
get_parameter_strings(int number,char * macro_name)
{
static char buffer[4096];
char *buffer_pntr;
int count;
int lbrace_counter;
char c;
int size;
ParameterList new = init_parameter_elem(number);
int pnum;
char pnum_chars[5];
int pc;
if (!number) { /* nothing to be done */
push_parameters(new);
return;
}
for (count = 0; count < number; count++) {
get_token();
if (token.type != Lbrace) {
/** The macro is not in a group, uh oh **/
fprintf(stderr, "Wrong number of arguments to the macro %s\n",
macro_name);
jump();
}
for (lbrace_counter = 1, buffer_pntr = buffer;
lbrace_counter;) {
switch (c = get_char()) {
case EOF:
fprintf(stderr, "GetParameterStrings: Unexpected EOF\n");
longjmp(jmpbuf, 1);
case '}':
lbrace_counter--;
if (lbrace_counter)
*buffer_pntr++ = c;
break;
case '{':
lbrace_counter++;
*buffer_pntr++ = c;
break;
case '#':
/* uh oh, I have a paramter reference inside a paramter */
/* get the number */
if (parameters == NULL) {
*buffer_pntr++ = c;
break;
}
if (
((buffer_pntr > buffer + 1) &&
*(buffer_pntr - 1) == '\\' &&
*(buffer_pntr - 2) != '\\') ||
((buffer_pntr > buffer) &&
*(buffer_pntr - 1) == '\\')) {
/* I had a \# */
*buffer_pntr++ = c;
}
else {
c = get_char();
for (pc = 0; numeric(c); pc++) {
pnum_chars[pc] = c;
c = get_char();
}
unget_char(c);
pnum_chars[pc] = '\0';
pnum = atoi(pnum_chars);
pc = 0;
/* Now copy the paramter */
while ((parameters->list)[pnum - 1][pc] != '\0')
*buffer_pntr++ = (parameters->list)[pnum - 1][pc++];
}
break;
default:
*buffer_pntr++ = c;
break;
}
}
*buffer_pntr = '\0';
/*** Now add it to the current parameter list **/
size = strlen(buffer) + 1;
new->list[count] = (char *) halloc(size, "Parameter Strings");
strcpy(new->list[count], buffer);
}
push_parameters(new);
return ;
}
void
parse_parameters(void)
{
int value;
if (!number(token.id)) {
fprintf(stderr,
"Parse_parameter: Error Expected a number, got %s instead\n", token.id);
longjmp(jmpbuf, 1);
}
if ((value = atoi(token.id)) > parameters->number) {
/** had a bad parameter number **/
fprintf(stderr,
"Parse_parameter: Had a bad parameter number %d\n", value);
longjmp(jmpbuf, 1);
}
parse_from_string((parameters->list)[value - 1]);
curr_node->type = Endparameter;
return;
}
|