-
Notifications
You must be signed in to change notification settings - Fork 50
/
putils.c
100 lines (93 loc) · 2.88 KB
/
putils.c
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
/* Copyright (c) 1994 Burra Gopal, Udi Manber. All Rights Reserved. */
#include "agrep.h"
int
is_complex_boolean(buffer, len)
CHAR *buffer;
int len;
{
int i = 0;
CHAR cur = '\0';
while (i < len) {
if (buffer[i] == '\\') i+=2;
else if (buffer[i] == ',') {
if ((cur == ';') || (cur == '~')) return 1;
else cur = ',';
i++;
}
else if (buffer[i] == ';') {
if ((cur == ',') || (cur == '~')) return 1;
else cur = ';';
i++;
}
/* else if ((buffer[i] == '~') || (buffer[i] == '{') || (buffer[i] == '}')) { */
else if (buffer[i] == '~') {
/* even if pattern has just ~s... user must use -v option for single NOT */
return 1;
}
else i++;
}
return 0;
}
/* The possible tokens are: ; , a e ~ { } */
int
get_token_bool(buffer, len, ptr, tokenbuf, tokenlen)
CHAR *buffer, *tokenbuf;
int len, *ptr, *tokenlen;
{
if ((*ptr>=len) || (buffer[*ptr] == '\n') || (buffer[*ptr] == '\0')) return 'e';
while ((*ptr<len) && (buffer[*ptr] != '\n') && (buffer[*ptr] != '\0') && ((buffer[*ptr] == ' ') || (buffer[*ptr] == '\t'))) (*ptr)++;
if ((*ptr>=len) || (buffer[*ptr] == '\n') || (buffer[*ptr] == '\0')) return 'e';
if ((buffer[*ptr] == ',') || (buffer[*ptr] == ';') || (buffer[*ptr] == '~') || (buffer[*ptr] == '{') || (buffer[*ptr] == '}')) {
tokenbuf[0] = buffer[*ptr];
*tokenlen = 1;
return buffer[(*ptr)++];
}
*tokenlen = 0;
if (buffer[*ptr] == '\\') {
tokenbuf[(*tokenlen)++] = buffer[(*ptr)++];
tokenbuf[(*tokenlen)++] = buffer[(*ptr)++];
}
else tokenbuf[(*tokenlen)++] = buffer[(*ptr)++];
while ( (*ptr<len) && (buffer[*ptr] != '\n') && (buffer[*ptr] != '\0') &&
(buffer[*ptr] != ',') && (buffer[*ptr] != ';') && (buffer[*ptr] != '~') && (buffer[*ptr] != '{') && (buffer[*ptr] != '}')) {
if (buffer[*ptr] == '\\') {
tokenbuf[(*tokenlen)++] = buffer[(*ptr)++];
tokenbuf[(*tokenlen)++] = buffer[(*ptr)++];
}
else tokenbuf[(*tokenlen)++] = buffer[(*ptr)++];
}
tokenbuf[*tokenlen] = '\0';
return 'a';
/* It will return next 'e' next time if we broke out of the loop because (*ptr >= len) */
}
void
print_tree(t, level)
ParseTree *t;
{
int i;
if (t == NULL) printf("NULL");
else if (t->type == LEAF) {
for (i=0; i<level; i++) printf(" ");
printf("LEAF %x %x %s\n", t->op, t->terminalindex, t->data.leaf.value);
}
else if (t->type == INTERNAL) {
if (t->data.internal.left != NULL) print_tree(t->data.internal.left, level + 1);
for (i=0; i<level; i++) printf(" ");
printf("INTERNAL %x\n", t->op);
if (t->data.internal.right != NULL) print_tree(t->data.internal.right, level + 1);
}
}
void
destroy_tree(t)
ParseTree *t;
{
if (t == NULL) return;
if (t->type == LEAF) {
free(t->data.leaf.value); /* t itself should not be freed: static allocation */
}
else if (t->type == INTERNAL) {
if (t->data.internal.left != NULL) destroy_tree(t->data.internal.left);
if (t->data.internal.right != NULL) destroy_tree(t->data.internal.right);
free(t);
}
}