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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/* ------------------------------------------------------------------------
@NAME : tex_tree.c
@DESCRIPTION: Functions for dealing with strings of TeX code: converting
them to tree representation, traversing the trees to glean
useful information, and converting back to string form.
@GLOBALS :
@CALLS :
@CALLERS :
@CREATED : 1997/05/29, Greg Ward
@MODIFIED :
@VERSION : $Id: tex_tree.c,v 1.4 1999/11/29 01:13:10 greg Rel $
@COPYRIGHT : Copyright (c) 1996-99 by Gregory P. Ward. All rights reserved.
This file is part of the btparse library. This library is
free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
-------------------------------------------------------------------------- */
/*#include "bt_config.h"*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "error.h"
#include "btparse.h"
/*#include "my_dmalloc.h"*/
/* blech! temp hack until I make error.c perfect and magical */
#define string_warning(w) fprintf (stderr, w);
typedef struct treestack_s
{
bt_tex_tree * node;
struct treestack_s
* prev,
* next;
} treestack;
/* ----------------------------------------------------------------------
* Stack manipulation functions
*/
/* ------------------------------------------------------------------------
@NAME : push_treestack()
@INPUT : *stack
node
@OUTPUT : *stack
@RETURNS :
@DESCRIPTION: Creates and initializes new node in a stack, and pushes it
onto the stack.
@GLOBALS :
@CALLS :
@CALLERS :
@CREATED : 1997/05/29, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
static void
push_treestack (treestack **stack, bt_tex_tree *node)
{
treestack *newtop;
newtop = (treestack *) malloc (sizeof (treestack));
newtop->node = node;
newtop->next = NULL;
newtop->prev = *stack;
if (*stack != NULL) /* stack already has some entries */
{
(*stack)->next = newtop;
*stack = newtop;
}
*stack = newtop;
} /* push_treestack() */
/* ------------------------------------------------------------------------
@NAME : pop_treestack
@INPUT : *stack
@OUTPUT : *stack
@RETURNS :
@DESCRIPTION: Pops an entry off of a stack of tex_tree nodes, frees up
the wrapper treestack node, and returns the popped tree node.
@GLOBALS :
@CALLS :
@CALLERS :
@CREATED : 1997/05/29, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
static bt_tex_tree *
pop_treestack (treestack **stack)
{
treestack * oldtop;
bt_tex_tree * node;
if (*stack == NULL)
internal_error ("attempt to pop off empty stack");
oldtop = (*stack)->prev;
node = (*stack)->node;
free (*stack);
if (oldtop != NULL)
oldtop->next = NULL;
*stack = oldtop;
return node;
} /* pop_treestack() */
/* ----------------------------------------------------------------------
* Tree creation/destruction functions
*/
/* ------------------------------------------------------------------------
@NAME : new_tex_tree
@INPUT : start
@OUTPUT :
@RETURNS : pointer to newly-allocated node
@DESCRIPTION: Allocates and initializes a bt_tex_tree node.
@GLOBALS :
@CALLS :
@CALLERS :
@CREATED : 1997/05/29, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
static bt_tex_tree *
new_tex_tree (char *start)
{
bt_tex_tree * node;
node = (bt_tex_tree *) malloc (sizeof (bt_tex_tree));
node->start = start;
node->len = 0;
node->child = node->next = NULL;
return node;
}
/* ------------------------------------------------------------------------
@NAME : bt_build_tex_tree
@INPUT : string
@OUTPUT :
@RETURNS : pointer to a complete tree; call bt_free_tex_tree() to free
the entire tree
@DESCRIPTION: Traverses a string looking for TeX groups ({...}), and builds
a tree containing pointers into the string and describing
its brace-structure.
@GLOBALS :
@CALLS :
@CALLERS :
@CREATED : 1997/05/29, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
bt_tex_tree *
bt_build_tex_tree (char * string)
{
int i;
int depth;
int len;
bt_tex_tree
* top,
* cur,
* new;
treestack
* stack;
i = 0;
depth = 0;
len = strlen (string);
top = new_tex_tree (string);
stack = NULL;
cur = top;
while (i < len)
{
switch (string[i])
{
case '{': /* go one level deeper */
{
if (i == len-1) /* open brace in last character? */
{
string_warning ("unbalanced braces: { at end of string");
goto error;
}
new = new_tex_tree (string+i+1);
cur->child = new;
push_treestack (&stack, cur);
cur = new;
depth++;
break;
}
case '}': /* pop level(s) off */
{
while (i < len && string[i] == '}')
{
if (stack == NULL)
{
string_warning ("unbalanced braces: extra }");
goto error;
}
cur = pop_treestack (&stack);
depth--;
i++;
}
i--;
if (i == len-1) /* reached end of string? */
{
if (depth > 0) /* but not at depth 0 */
{
string_warning ("unbalanced braces: not enough }'s");
goto error;
}
/*
* if we get here, do nothing -- we've reached the end of
* the string and are at depth 0, so will just fall out
* of the while loop at the end of this iteration
*/
}
else /* still have characters left */
{ /* to worry about */
new = new_tex_tree (string+i+1);
cur->next = new;
cur = new;
}
break;
}
default:
{
cur->len++;
}
} /* switch */
i++;
} /* while i */
if (depth > 0)
{
string_warning ("unbalanced braces (not enough }'s)");
goto error;
}
return top;
error:
bt_free_tex_tree (&top);
return NULL;
} /* bt_build_tex_tree() */
/* ------------------------------------------------------------------------
@NAME : bt_free_tex_tree
@INPUT : *top
@OUTPUT : *top (set to NULL after it's free()'d)
@RETURNS :
@DESCRIPTION: Frees up an entire tree created by bt_build_tex_tree().
@GLOBALS :
@CALLS : itself, free()
@CALLERS :
@CREATED : 1997/05/29, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
void
bt_free_tex_tree (bt_tex_tree **top)
{
if ((*top)->child) bt_free_tex_tree (&(*top)->child);
if ((*top)->next) bt_free_tex_tree (&(*top)->next);
free (*top);
*top = NULL;
}
/* ----------------------------------------------------------------------
* Tree traversal functions
*/
/* ------------------------------------------------------------------------
@NAME : bt_dump_tex_tree
@INPUT : node
depth
stream
@OUTPUT :
@RETURNS :
@DESCRIPTION: Dumps a TeX tree: one node per line, depth indented according
to depth.
@GLOBALS :
@CALLS : itself
@CALLERS :
@CREATED : 1997/05/29, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
void
bt_dump_tex_tree (bt_tex_tree *node, int depth, FILE *stream)
{
char buf[256];
if (node == NULL)
return;
if (node->len > 255)
internal_error ("augughgh! buf too small");
strncpy (buf, node->start, node->len);
buf[node->len] = (char) 0;
fprintf (stream, "%*s[%s]\n", depth*2, "", buf);
bt_dump_tex_tree (node->child, depth+1, stream);
bt_dump_tex_tree (node->next, depth, stream);
}
/* ------------------------------------------------------------------------
@NAME : count_length
@INPUT : node
@OUTPUT :
@RETURNS :
@DESCRIPTION: Counts the total number of characters that will be needed
to print a string reconstructed from a TeX tree. (Length
of string in each node, plus two [{ and }] for each down
edge.)
@GLOBALS :
@CALLS : itself
@CALLERS : bt_flatten_tex_tree
@CREATED : 1997/05/29, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
static int
count_length (bt_tex_tree *node)
{
if (node == NULL) return 0;
return
node->len +
(node->child ? 2 : 0) +
count_length (node->child) +
count_length (node->next);
}
/* ------------------------------------------------------------------------
@NAME : flatten_tree
@INPUT : node
*offset
@OUTPUT : *buf
*offset
@RETURNS :
@DESCRIPTION: Dumps a reconstructed string ("flat" representation of the
tree) into a pre-allocated buffer, starting at a specified
offset.
@GLOBALS :
@CALLS : itself
@CALLERS : bt_flatten_tex_tree
@CREATED : 1997/05/29, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
static void
flatten_tree (bt_tex_tree *node, char *buf, int *offset)
{
strncpy (buf + *offset, node->start, node->len);
*offset += node->len;
if (node->child)
{
buf[(*offset)++] = '{';
flatten_tree (node->child, buf, offset);
buf[(*offset)++] = '}';
}
if (node->next)
{
flatten_tree (node->next, buf, offset);
}
}
/* ------------------------------------------------------------------------
@NAME : bt_flatten_tex_tree
@INPUT : top
@OUTPUT :
@RETURNS : flattened string representation of the tree (as a string
allocated with malloc(), so you should free() it when
you're done with it)
@DESCRIPTION: Counts the number of characters needed for a "flat"
string representation of a tree, allocates a string of
that size, and generates the string.
@GLOBALS :
@CALLS : count_length, flatten_tree
@CALLERS :
@CREATED : 1997/05/29, GPW
@MODIFIED :
-------------------------------------------------------------------------- */
char *
bt_flatten_tex_tree (bt_tex_tree *top)
{
int len;
int offset;
char * buf;
len = count_length (top);
buf = (char *) malloc (sizeof (char) * (len+1));
offset = 0;
flatten_tree (top, buf, &offset);
return buf;
}
|