Graphviz 13.1.3~dev.20250831.0023
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->args = info->args;
50 state->errf = info->errf;
51 state->flags = info->flags;
52
53 return state;
54}
55
56
57static int
58bindingcmpf (const void *key, const void *ip)
59{
60 return strcmp (((const gvprbinding*)key)->name, ((const gvprbinding*)ip)->name);
61}
62
63/* findBinding:
64 */
66findBinding (Gpr_t* state, char* fname)
67{
68 if (!state->bindings) {
69 error(ERROR_ERROR,"call(\"%s\") failed: no bindings", fname);
70 return NULL;
71 }
72 if (!fname) {
73 error(ERROR_ERROR,"NULL function name for call()");
74 return NULL;
75 }
76
77 const gvprbinding key = {.name = fname};
78 gvprbinding *bp = bsearch(&key, state->bindings, state->n_bindings,
79 sizeof(gvprbinding), bindingcmpf);
80 if (!bp)
81 error(ERROR_ERROR, "No binding for \"%s\" in call()", fname);
82 return bp;
83}
84
85/* addBindings:
86 * Validate input, sort lexicographically, and attach
87 */
88void addBindings (Gpr_t* state, gvprbinding* bindings)
89{
90 size_t n = 0;
91 gvprbinding* bp = bindings;
92 gvprbinding* buf;
93 gvprbinding* bufp;
94
95 while (bp && bp->name) {
96 if (bp->fn) n++;
97 bp++;
98 }
99
100 if (n == 0) return;
101 bufp = buf = gv_calloc(n, sizeof(gvprbinding));
102 bp = bindings;
103 while (bp->name) {
104 if (bp->fn) {
105 *bufp = *bp;
106 bufp++;
107 }
108 bp++;
109 }
110 qsort (buf, n, sizeof(gvprbinding), bindingcmpf);
111
112 state->bindings = buf;
113 state->n_bindings = n;
114}
115
117{
118 if (!state) return;
119 name_used = state->name_used;
120 free(state->tgtname);
121 free (state->dp);
122 free (state);
123}
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:58
void initGPRState(Gpr_t *state)
Definition gprstate.c:30
gvprbinding * findBinding(Gpr_t *state, char *fname)
Definition gprstate.c:66
bool validTVT(long long c)
Definition gprstate.c:26
void addBindings(Gpr_t *state, gvprbinding *bindings)
Definition gprstate.c:88
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:116
@ TV_flat
Definition gprstate.h:32
@ TV_prepostrev
Definition gprstate.h:36
node NULL
Definition grammar.y:181
table Syntax error
Definition htmlparse.y:288
Agedge_t * tvedge
Definition gprstate.h:54
char * tgtname
Definition gprstate.h:48
int flags
Definition gprstate.h:57
FILE * outFile
Definition gprstate.h:50
size_t n_bindings
Definition gprstate.h:59
Exdisc_t * dp
Definition gprstate.h:45
Agnode_t * tvroot
Definition gprstate.h:52
Agnode_t * tvnext
Definition gprstate.h:53
gvprbinding * bindings
Definition gprstate.h:58
Exerror_f errf
Definition gprstate.h:46
int name_used
Definition gprstate.h:55
strviews_t args
Definition gprstate.h:56
trav_type tvt
Definition gprstate.h:51
char * name
Definition gvpr.h:59
gvpruserfn fn
Definition gvpr.h:60