Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
gprstate.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11
12/*
13 * gpr state
14 *
15 */
16
17#include <gvpr/gprstate.h>
18#include <ast/error.h>
19#include <stdbool.h>
20#include <stdlib.h>
21#include <string.h>
22#include <util/alloc.h>
23
24static int name_used;
25
26bool validTVT(long long c) {
27 return TV_flat <= c && c <= TV_prepostrev;
28}
29
30void initGPRState(Gpr_t *state) {
31 state->tgtname = strdup("gvpr_result");
32}
33
35{
36 Gpr_t *state;
37
38 if (!(state = calloc(1, sizeof(Gpr_t)))) {
39 error(ERROR_ERROR, "Could not create gvpr state: out of memory");
40 return state;
41 }
42
43 state->tvt = TV_flat;
44 state->name_used = name_used;
45 state->tvroot = 0;
46 state->tvnext = 0;
47 state->tvedge = 0;
48 state->outFile = info->outFile;
49 state->argc = info->argc;
50 state->argv = info->argv;
51 state->errf = info->errf;
52 state->flags = info->flags;
53
54 return state;
55}
56
57
58static int
59bindingcmpf (const void *key, const void *ip)
60{
61 return strcmp (((const gvprbinding*)key)->name, ((const gvprbinding*)ip)->name);
62}
63
64/* findBinding:
65 */
67findBinding (Gpr_t* state, char* fname)
68{
69 gvprbinding key;
70 gvprbinding* bp;
71
72 if (!state->bindings) {
73 error(ERROR_ERROR,"call(\"%s\") failed: no bindings", fname);
74 return NULL;
75 }
76 if (!fname) {
77 error(ERROR_ERROR,"NULL function name for call()");
78 return NULL;
79 }
80
81 key.name = fname;
82 bp = bsearch(&key, state->bindings, state->n_bindings, sizeof(gvprbinding), bindingcmpf);
83 if (!bp)
84 error(ERROR_ERROR, "No binding for \"%s\" in call()", fname);
85 return bp;
86}
87
88/* addBindings:
89 * Validate input, sort lexicographically, and attach
90 */
91void addBindings (Gpr_t* state, gvprbinding* bindings)
92{
93 size_t n = 0;
94 gvprbinding* bp = bindings;
95 gvprbinding* buf;
96 gvprbinding* bufp;
97
98 while (bp && bp->name) {
99 if (bp->fn) n++;
100 bp++;
101 }
102
103 if (n == 0) return;
104 bufp = buf = gv_calloc(n, sizeof(gvprbinding));
105 bp = bindings;
106 while (bp->name) {
107 if (bp->fn) {
108 *bufp = *bp;
109 bufp++;
110 }
111 bp++;
112 }
113 qsort (buf, n, sizeof(gvprbinding), bindingcmpf);
114
115 state->bindings = buf;
116 state->n_bindings = n;
117}
118
120{
121 if (!state) return;
122 name_used = state->name_used;
123 free(state->tgtname);
124 free (state->dp);
125 free (state);
126}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
static char * fname
#define ERROR_ERROR
Definition error.h:36
void free(void *)
static int bindingcmpf(const void *key, const void *ip)
Definition gprstate.c:59
void initGPRState(Gpr_t *state)
Definition gprstate.c:30
gvprbinding * findBinding(Gpr_t *state, char *fname)
Definition gprstate.c:67
bool validTVT(long long c)
Definition gprstate.c:26
void addBindings(Gpr_t *state, gvprbinding *bindings)
Definition gprstate.c:91
static int name_used
Definition gprstate.c:24
Gpr_t * openGPRState(gpr_info *info)
Definition gprstate.c:34
void closeGPRState(Gpr_t *state)
Definition gprstate.c:119
@ TV_flat
Definition gprstate.h:27
@ TV_prepostrev
Definition gprstate.h:31
node NULL
Definition grammar.y:163
table Syntax error
Definition htmlparse.y:294
char ** argv
Definition gprstate.h:52
Agedge_t * tvedge
Definition gprstate.h:49
char * tgtname
Definition gprstate.h:43
int flags
Definition gprstate.h:53
FILE * outFile
Definition gprstate.h:45
size_t n_bindings
Definition gprstate.h:55
Exdisc_t * dp
Definition gprstate.h:40
Agnode_t * tvroot
Definition gprstate.h:47
Agnode_t * tvnext
Definition gprstate.h:48
gvprbinding * bindings
Definition gprstate.h:54
Exerror_f errf
Definition gprstate.h:41
int name_used
Definition gprstate.h:50
int argc
Definition gprstate.h:51
trav_type tvt
Definition gprstate.h:46
char * name
Definition gvpr.h:59
gvpruserfn fn
Definition gvpr.h:60