summaryrefslogtreecommitdiff
path: root/src/pmdas/hotproc/src/gram_node.c
blob: d2c36b5e57e240e7207a8b5d39ef6dd1c094bbfe (plain)
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
/*
 * Copyright (c) 1995 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This program 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.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "./gram_node.h"

/* functions */
static void dump_comparison(FILE *, bool_node *);
static void dump_var(FILE *, bool_node *);

static bool_node *node_list = NULL;

void start_tree(void)
{
    node_list = NULL;
}

void free_tree(bool_node *root)
{
    bool_node *n, *next;

    if (root == NULL)
	root = node_list; /* use last tree */

    /* free all nodes in list */
    for (n = root; n != NULL; ) {
	next = n->next;
	if (n->tag == N_pat || n->tag == N_str)
	    free(n->data.str_val);
	free(n);
        n = next; 
    }    

    if (root == node_list)
	node_list = NULL;
}

bool_node *
create_tag_node(N_tag tag)
{
    bool_node *new_node;

    new_node = (bool_node*)malloc(sizeof(bool_node));
    if (new_node == NULL) {
	fprintf(stderr, "hotproc: malloc failed in config: %s", osstrerror());
	exit(1);
    }
    new_node->tag = tag;

    /* add to front of node-list */
    new_node->next = node_list;
    node_list = new_node;

    return new_node;
}

bool_node *
create_tnode(N_tag tag, bool_node *lnode, bool_node *rnode)
{
    bool_node *n = create_tag_node(tag);
    n->data.children.left = lnode;
    n->data.children.right = rnode;
    return n;
}

bool_node *
create_number_node(double x)
{
    bool_node *n = create_tag_node(N_number);
    n->data.num_val = x;
    return n;
}


bool_node *create_str_node(char *str)
{
    bool_node *n = create_tag_node(N_str);
    n->data.str_val = str;
    return n;
}

bool_node *create_pat_node(char *str)
{
    bool_node *n = create_tag_node(N_pat);
    n->data.str_val = str;
    return n;
}

void
dump_bool_tree(FILE *f, bool_node *tree)
{
    (void)fprintf(f, "--- bool tree ---\n");
    dump_predicate(f, tree);
    (void)fprintf(f, "\n--- end bool tree ---\n");
}

void
dump_predicate(FILE *f, bool_node *pred)
{
    bool_node *lhs, *rhs;

    switch(pred->tag) {
	case N_and:	
	    lhs = pred->data.children.left;
	    rhs = pred->data.children.right;
	    (void)fprintf(f, "(");
	    dump_predicate(f, lhs);
	    (void)fprintf(f, " && ");
	    dump_predicate(f, rhs);
	    (void)fprintf(f, ")");
	    break;
	case N_or:	
	    lhs = pred->data.children.left;
	    rhs = pred->data.children.right;
	    (void)fprintf(f, "(");
	    dump_predicate(f, lhs);
	    (void)fprintf(f, " || ");
	    dump_predicate(f, rhs);
	    (void)fprintf(f, ")");
	    break;
	case N_not:	
	    lhs = pred->data.children.left;
	    (void)fprintf(f, "(! ");
	    dump_predicate(f, lhs);
	    (void)fprintf(f, ")");
	    break;
	case N_true:
	    (void)fprintf(f, "(true)");
	    break;
	case N_false:
	    (void)fprintf(f, "(false)");
	    break;
	default:
	    dump_comparison(f, pred);
    }/*switch*/
}

static void
dump_comparison(FILE *f, bool_node *comp)
{
    bool_node *lhs = comp->data.children.left;
    bool_node *rhs = comp->data.children.right;

    (void)fprintf(f, "(");
    dump_var(f, lhs);
    switch(comp->tag) {
	case N_lt: (void)fprintf(f, " < "); break;
	case N_gt: (void)fprintf(f, " > "); break;
	case N_le: (void)fprintf(f, " <= "); break;
	case N_ge: (void)fprintf(f, " >= "); break;
	case N_eq: (void)fprintf(f, " == "); break;
	case N_seq: (void)fprintf(f, " == "); break;
	case N_sneq: (void)fprintf(f, " != "); break;
	case N_neq: (void)fprintf(f, " != "); break;
	case N_match: (void)fprintf(f, " ~ "); break;
	case N_nmatch: (void)fprintf(f, " !~ "); break;
	default: (void)fprintf(f, "<ERROR>"); break;
    }/*switch*/
    dump_var(f, rhs);
    (void)fprintf(f, ")");
}

static void
dump_var(FILE *f, bool_node *var)
{
    switch(var->tag) {
	case N_str: (void)fprintf(f, "\"%s\"", var->data.str_val); break;
	case N_pat: (void)fprintf(f, "\"%s\"", var->data.str_val); break;
	case N_number: (void)fprintf(f, "%f", var->data.num_val); break;
	case N_uid: (void)fprintf(f, "uid"); break;
	case N_gid: (void)fprintf(f, "gid"); break;
	case N_uname: (void)fprintf(f, "uname"); break;
	case N_gname: (void)fprintf(f, "gname"); break;
	case N_fname: (void)fprintf(f, "fname"); break;
	case N_psargs: (void)fprintf(f, "psargs"); break;
	case N_cpuburn: (void)fprintf(f, "cpuburn"); break;
	case N_syscalls: (void)fprintf(f, "syscalls"); break;
	case N_ctxswitch: (void)fprintf(f, "ctxswitch"); break;
	case N_virtualsize: (void)fprintf(f, "virtualsize"); break;
	case N_residentsize: (void)fprintf(f, "residentsize"); break;
	case N_iodemand: (void)fprintf(f, "iodemand"); break;
	case N_iowait: (void)fprintf(f, "iowait"); break;
	case N_schedwait: (void)fprintf(f, "schedwait"); break;
	default: (void)fprintf(f, "<ERROR>"); break;
    }/*switch*/
}